Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hexagon image material

Discussion in 'General Graphics' started by Vedmak02, Dec 27, 2015.

  1. Vedmak02

    Vedmak02

    Joined:
    Dec 27, 2015
    Posts:
    4
    I've created a hexagon using script

    using UnityEngine;
    using System.Collections;

    public class someScript : MonoBehaviour {

    public float height = 50f;
    public float width = 50f;
    public float depth = 0;
    void Start () {
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;
    Vector3[] vertices2 = new Vector3[6];
    float x = 0;
    float y = 0;
    float r = 10;
    float n = 6;
    for (int i = 0; i < n; i++)
    {
    float xPos = x + r * Mathf.Cos(2 * Mathf.PI * i / n);
    float zPos = y + r * Mathf.Sin(2 * Mathf.PI * i / n);
    Debug.Log("xPos1 " + xPos);
    Debug.Log("zPos1 " + zPos);
    vertices2 = new Vector3(xPos, 0, zPos);
    };

    int[] tri2 = new int[12];
    tri2[0] = 2;
    tri2[1] = 1;
    tri2[2] = 0;
    tri2[3] = 3;
    tri2[4] = 2;
    tri2[5] = 0;
    tri2[6] = 4;
    tri2[7] = 3;
    tri2[8] = 0;
    tri2[9] = 5;
    tri2[10] = 4;
    tri2[11] = 0;


    Vector3[] normals2 = new Vector3[6];
    normals2[0] = -Vector3.forward;
    normals2[1] = -Vector3.forward;
    normals2[2] = -Vector3.forward;
    normals2[3] = -Vector3.forward;
    normals2[4] = -Vector3.forward;
    normals2[5] = -Vector3.forward;


    Vector2[] uv2 = new Vector2[6];
    uv2[0] = new Vector2(0, 0);
    uv2[1] = new Vector2(1, 0);
    uv2[2] = new Vector2(0, 1);
    uv2[3] = new Vector2(1, 1);
    uv2[4] = new Vector2(1, 1);
    uv2[5] = new Vector2(1, 1);


    mesh.vertices = vertices2;
    mesh.triangles = tri2;
    mesh.normals = normals2;
    mesh.uv = uv2;
    }

    }


    When I render it with adding simple color material - it looks like this:
    upload_2015-12-27_11-31-43.png

    But when Im adding some image on it -
    upload_2015-12-27_11-32-13.png

    Half of the hexagon looks fine - but the other part is smoothed, any idea where can be the problem?
     
  2. StevenGerrard

    StevenGerrard

    Joined:
    Jun 1, 2015
    Posts:
    97
    Texture mapping result decided by your uv texture coordinate.
    As your code the last three uv of vertex both (1,1) so it will be looks like that.
     
    Vedmak02 likes this.
  3. AkiraWong89

    AkiraWong89

    Joined:
    Oct 30, 2015
    Posts:
    662
    I'm not familiar with coding but I think the concept is same.
    Everything modeled out needed to do UV mapping.
    For your case. A planar UV mapping is perfect enough.
    But I don't know how to do UV mapping via scripting.
    Usually just do in 3D software and import it in.
     
  4. Vedmak02

    Vedmak02

    Joined:
    Dec 27, 2015
    Posts:
    4
    Thank you for the hint, yes, the problem was in uv, but Im not familiar in what the uv realy is, would be thankful for some link or info about it, thanks!
    And how is it correct to set uv in hexagon to fit image correctly?
     
  5. Vedmak02

    Vedmak02

    Joined:
    Dec 27, 2015
    Posts:
    4
    maximum what I was able to do is something like this from the image in preview
    upload_2015-12-27_19-15-41.png
    Would be glad for info hot get at least something similar :D
     
  6. Vedmak02

    Vedmak02

    Joined:
    Dec 27, 2015
    Posts:
    4
    You mean kinda create mech in Blender and import it into Unity?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    UVs are the x and y (u and v) position of a surface in texture space. For what is basically a 2d shape like this hexagon you can just apply the same positions you have for the vertices as the UVs and get decent results, though you may want to put them into 0 to 1 space instead of -1 to 1 space so the texturing isn't tiled.

    uv2 = new Vector2(vertices2.x * 0.5f + 0.5f, vertices2.z * 0.5f + 0.5f);

    On an aside, why are all of your array names suffixed with the number 2? It's a little confusing, especially since meshes actually do have a .uv2!
     
    theANMATOR2b likes this.
  8. AkiraWong89

    AkiraWong89

    Joined:
    Oct 30, 2015
    Posts:
    662