Mapping UVs to the 3D hex mesh

Hi,
I have generated 3D mesh in Unity and now I am trying to add UVs to this mesh. I managed to do that when it was in 2D. But I don’t know how to do it in 3D. Do you guys have any idea? Thank you for your help.

EDIT: I have added code for my hex mesh

using UnityEngine;
using System.Collections;

public class HexInfo : MonoBehaviour {

    public Texture texture;

    // Basix hexagon mesh making
    private Vector3[] vertices;
    private Vector2[] uv;
    private int[] triangles;

    void Start ()
    {
        MeshSetup();
    }

    void MeshSetup()
    {
        #region VERTICES

        float height = 0.125f;
        float floorLevel = 0.0f + height;
        vertices = new Vector3[]
        {
            // Top
            new Vector3(-1.0f, floorLevel, -0.5f),
            new Vector3(-1.0f, floorLevel, 0.5f),
            new Vector3(0.0f, floorLevel, 1.0f),
            new Vector3(1.0f, floorLevel, 0.5f),
            new Vector3(1.0f, floorLevel, -0.5f),
            new Vector3(0.0f, floorLevel, -1.0f),

            // Bottom
            new Vector3(-1.0f, floorLevel - height, -0.5f),
            new Vector3(-1.0f, floorLevel - height, 0.5f),
            new Vector3(0.0f, floorLevel - height, 1.0f),
            new Vector3(1.0f, floorLevel - height, 0.5f),
            new Vector3(1.0f, floorLevel - height, -0.5f),
            new Vector3(0.0f, floorLevel - height, -1.0f)
        };

        #endregion


        #region TRIANGLES

        triangles = new int[]
        {
            // Top
            1, 5, 0,
            1, 4, 5,
            1, 2, 4,
            2, 3, 4,
           
            // Bottom
            7, 6, 11,
            7, 11, 10,
            7, 10, 8,
            8, 10, 9,

            // Sides
            0, 11, 6,
            0, 5, 11,

            5, 10, 11,
            5, 4, 10,

            4, 9, 10,
            4, 3, 9,

            3, 8, 9,
            3, 2, 8,

            2, 7, 8,
            2, 1, 7,

            1, 6, 7,
            1, 0, 6

        };

        #endregion


        #region UV

        uv = new Vector2[]
        {
            /*
            new Vector2(0.0f, 0.25f),
            new Vector2(0.0f, 0.75f),
            new Vector2(0.5f, 1.0f),
            new Vector2(1.0f, 0.75f),
            new Vector2(1.0f, 0.25f),
            new Vector2(0.5f, 0.0f),
            */
        };

        #endregion


        #region FINALIZE

        // Add a mesh filter to the game object the script is attached to. Cache it for later
        MeshFilter meshFilter = this.gameObject.AddComponent<MeshFilter>();
        // Add a mesh renderer to the game object to the script is attached to
        this.gameObject.AddComponent<MeshRenderer>();

        // Create a mesh object to pass our data into
        Mesh mesh = new Mesh();

        // Add our vertices to the mesh
        mesh.vertices = vertices;
        // Add our triangles to the mesh
        mesh.triangles = triangles;
        // Add our uv coordinates to the mesh
        //mesh.uv = uv;


        // Recalculate normals for lighting
        mesh.RecalculateNormals();

        // Set the gameobject's meshFilter's mesh to tbe the one we just made
        meshFilter.mesh = mesh;

        // Test our uv
        this.renderer.material.mainTexture = texture;

        #endregion
    }
}

It depends entirely on how you want it to be mapped.
Note: Since you only have one vertex at each point, you can only have one UV coord (vertex count must == uv count)
This means no texture seams (Which is likely going to be prohibitive to desired results). Youll probably have to add more verts, and change the triangles, etc.

(vertex count must == uv count)…Youll probably have to add more verts, and change the triangles, etc.

So if I have a particular vertex in a mesh, that is used in say… four triangles: In order to use one UV coordinate for triangles 1&2, and another UV coordinate for triangles 3&4, I must create another vertex (even though it will have identical coordinates)?

Yep. And as a consequence, you must change the vert index used in each of those triangles

Hmm, I have always thought that vertex coordinates should be between 0 and 1.
I am not sure if I understand it right, where am I suppose to add those vertices? Can you provide me a example please?

Heres a cylinder with a gradient texture, mapped to wrap all the way around


The cylinder has 20 vertex positions around it’s circumference, but in order to get the sharp boundary at the edge of the texture, you need 2 different uvs ( [0,0] and [1,0] ) at the vertex position, so there are actually 21 verts around each ring, with the first and last being in the exact same position

1 Like

And if we wanted the END of those cylinders to map the same gradient across their surface, we would need ANOTHER 20 vertices with their own uv mapping [to (sin angle,cos angle) and (0.5,0.5) for the center]

This seems kind of odd to me. is there a reason the UV (and Normals array too, I see) are not also indexed like vertices? This would allow us to mix and match UNIQUE vertex, normal, and uv information. Is it because SPEED is more important that memory usage? In which case, why use indexes for vertices at all? (not complaining, just curious)

The vertex coordinate is a point in 3D space (model space, specifically).
A UV coordinate, is a 2D point on your texture. THESE x,y UV values are between 0 and 1 (top/bottom of texture, leftside /rightside of texture).
[details: The graphics engine interpolates what texture pixels go BETWEEN your triangles vertices(in 3D-space), by looking at their UV coordinates. I would guess it does this by using some kind of projection matrix to convert the 3D Vertex coordinates into 2d coordinates, and then interpolating between each of the triangle’s vertex’s UV coordinates (a 2D texture-space coordinate), to find the pixel color.]

Edit: It is standard practice for shapes to be a 1 unit in size, in model space (x,y,z)… perhaps this is why you thought vertices should be between 0 and 1? Though this usually yields a min/max for each coordinate of -0.5 to 0.5