How to UV map all 6 faces of a cube but draw it using Indices with only 8 Vertex data?

Started by
6 comments, last by LorenzoGatti 3 years, 1 month ago

I'm drawing a cube with the following code but the problem is that I can not map all the UVs correctly. I know that the reason is that I only define 8 vertex positions and then I use 36 indices to draw all the 6 faces of the cube and this leads to the problem that I can't write down all the Uv's because each vertex (8 in total in this case) can only have 8 Uvs as well.

I don't want to use 36 Vertex data to map all the Uvs for each vertex. There must be a way to do this else Index drawing would be meaningless…

using UnityEngine;

public class Vortex : MonoBehaviour
{

    [SerializeField] private MeshFilter   m_MeshFilter;
    [SerializeField] private MeshRenderer m_MeshRenderer;




    // Start is called before the first frame update
    void Start()
    {

        //Create a new mesh objet.
        Mesh mesh = new Mesh();

        //Create the vertex data.
        mesh.vertices = new Vector3[]
        {
            new Vector3(0.0f, 0.0f, 0.0f), //Front-Left Bottom   (0)
            new Vector3(0.0f, 1.0f, 0.0f), //Front-Left Top      (1)
            new Vector3(1.0f, 1.0f, 0.0f), //Front-Right Top     (2)
            new Vector3(1.0f, 0.0f, 0.0f), //Front-Right Bottom  (3)

            new Vector3(0.0f, 0.0f, 1.0f), //Back-Left Bottom    (4)
            new Vector3(0.0f, 1.0f, 1.0f), //Back-Left Top       (5)
            new Vector3(1.0f, 1.0f, 1.0f), //Back-Right Top      (6)
            new Vector3(1.0f, 0.0f, 1.0f)  //Back-Right Bottom   (7)
        };


        //Create the Indices.
        mesh.triangles = new int[]
        {
            0, 1, 2, 3, 0, 2, //Front Face.
            1, 5, 6, 2, 1, 6, //Top Face.
            4, 5, 1, 0, 4, 1, //Left Face.
            3, 2, 6, 7, 3, 6, //Right Face.
            6, 5, 4, 6, 4, 7, //Back Face.
            4, 0, 3, 7, 4, 3, //Bottom Face.
        };



        //Texture Coordinates.
        mesh.uv = new Vector2[]
        {
            new Vector2(0.0f, 0.0f), 
            new Vector2(0.0f, 1.0f), 
            new Vector2(1.0f, 1.0f),
            new Vector2(1.0f, 0.0f),

            new Vector2(1.0f, 0.0f),
            new Vector2(1.0f, 1.0f),
            new Vector2(0.0f, 1.0f),
            new Vector2(0.0f, 0.0f),
        };


        //Recalculate Normals.
        mesh.RecalculateNormals();


        //Set the mesh to the mesh filter.
        m_MeshFilter.mesh = mesh;
    }
}

void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Advertisement

There is no other way, you must duplicate verticies with unique UV. Indexing of course makes sense, you can have a flat land on one wall of many polygons?

Maybe I learned it the wrong way? Learn OpenGL, said that indices are used to avoid having duplicate vertices, but in this case, I must have 36 different UVs so I must use 36 Vertex data with duplicate Vertex Positions and in order to draw the cube with index drawing I must also specify 36 indices as well.

I can't understand what do you mean by

Indexing of course makes sense, you can have a flat land on one wall of many polygons?

In my case, the only reason I need indexing is that I must specify the triangle draw order for Face Culling to work properly. Else I could draw everything using the drawArrays command. (Well Unity is probably using only Index Drawing but I'm just saying…)


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

A vertex consists of all data including the texture coordinate, not just the position.
If your goal is efficiency then with how small the dataset is it isn't a problem.

If you don't want a lot of vertex data defined in your code you could try to generate the data with a more compact algorithm.

My current game project Platform RPG

babaliaris said:
Maybe I learned it the wrong way? Learn OpenGL, said that indices are used to avoid having duplicate vertices

You are probably confused because the example is bad. Think of a tessellated cube, each face having 1000 triangles. In this case the vertices which need to be duplicated are only a few in comparison to all.

JoeJ said:

babaliaris said:
Maybe I learned it the wrong way? Learn OpenGL, said that indices are used to avoid having duplicate vertices

You are probably confused because the example is bad. Think of a tessellated cube, each face having 1000 triangles. In this case the vertices which need to be duplicated are only a few in comparison to all.

Haha, this makes sense thanks!!!


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

The same point can be obviously be shared between edges and triangles, but it can only be the same vertex for drawing purposes, with the same UV coordinates, if all the triangles are drawn with the same texture without seams along incident edges. Whenever the UV coordinates on the left and right side of an edge aren't identical it means you are interpolating them between different UV coordinates at either or both ends of the edge.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement