x


Setting custom UV Mapping

Hey All,

I've created a procedural hexagon mesh and I'm trying to texture it. When I do apply a texture it is completed messed up. I'm sure it's because I'm not setting up a proper UV map for the mesh. The problem is I'm not exactly sure how to go about setting it up. I understand how I would do it for a cube, but I'm puzzled how to go about it for a hexagon.

Here's the code where I setup the mesh. Thanks for the help in advance.

public class HexTile : MonoBehaviour
{
    public float height = 0.5f;
    public float zWidth = 2;

    void Awake()
    {
        Mesh m = ((MeshFilter)gameObject.GetComponent("MeshFilter")).mesh;

        float angle = 360 / 6;     

        float b = zWidth / 2;
        float c = b / Mathf.Cos((Mathf.PI / 180) * (angle / 2));
        float a = b * Mathf.Tan((Mathf.PI / 180) * (angle / 2));        

        int[] pt = { 0,2,1, 0,3,2, 0,4,3, 0,5,4, 0,6,5, 0,1,6, //top
                 7,8,9, 7,9,10, 7,10,11, 7,11,12, 7,12,13, 7,13,8, //bottom
                 2,8,1, 2,9,8, //sides
                 3,9,2, 3,10,9,
                 4,10,3, 4,11,10,
                 5,11,4, 5,12,11,
                 6,12,5, 6,13,12,
                 1,13,6, 1,8,13};

        Vector3[] pv = new Vector3[14];

        //top face
        pv[0] = new Vector3(0, height, 0);
        pv[1] = new Vector3(c, height, 0);
        pv[2] = new Vector3(a, height, b);
        pv[3] = new Vector3(-a, height, b);
        pv[4] = new Vector3(-c, height, 0);
        pv[5] = new Vector3(-a, height, -b);
        pv[6] = new Vector3(a, height, -b);


        //bottom face
        pv[7] = new Vector3(0, 0, 0);
        pv[8] = new Vector3(c, 0, 0);
        pv[9] = new Vector3(a, 0, b);
        pv[10] = new Vector3(-a, 0, b);
        pv[11] = new Vector3(-c, 0, 0);
        pv[12] = new Vector3(-a, 0, -b);
        pv[13] = new Vector3(a, 0, -b);

        m.vertices = pv;
        m.triangles = pt;

        m.RecalculateNormals();
        m.RecalculateBounds();
   }
}
more ▼

asked Aug 16 '10 at 03:19 AM

meroon gravatar image

meroon
28 3 3 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Add these codes after this two line.

m.vertices = pv;
m.triangles = pt;

int nVertices = m.vertices.Length;
Vector2 uvs = new Vector2[nVertices];
for(int i=0; i<nVertices; i++)
{
   uvs[i] = m.vertices[i];
}
m.uvs = uvs;

Good luck!

more ▼

answered Jun 18 '11 at 03:21 PM

Yanger_xy gravatar image

Yanger_xy
270 22 25 33

(comments are locked)
10|3000 characters needed characters left
more ▼

answered Sep 26 '10 at 12:49 PM

Robotron18 gravatar image

Robotron18
113 3 5 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2203
x1364
x238

asked: Aug 16 '10 at 03:19 AM

Seen: 4259 times

Last Updated: Jun 18 '11 at 03:21 PM