UV Map to primitive geometry

I’m trying to map UV coordinates from a texture atlas to a cube. I’m not creating the geometry for the cube manually, instead, I’m creating a primitive, then copying the verts and tris from that, to apply to my mesh.

GameObject primative = GameObject.CreatePrimitive(PrimitiveType.Cube);
primative.hideFlags = HideFlags.HideAndDontSave;
MeshFilter primativeFilter = primative.GetComponent<MeshFilter>();
primative.SetActive(false);

After this, I do a loop to assign the UVs:

Vector2[] uvs = new Vector2[primativeFilter.mesh.vertices.Length];
for (int i = 0; i < primativeFilter.mesh.vertices.Length; i += 4) {
    uvs[i] = new Vector2(0, 1);
    uvs[i + 1] = new Vector2(0.5f, 1);
    uvs[i + 2] = new Vector2(0, 0.25f);
    uvs[i + 3] = new Vector2(0.25f, 0.5f);
}

And then assign all of that to the mesh

Mesh mesh = new Mesh();
mesh.vertices = primativeFilter.mesh.vertices;
mesh.triangles = primativeFilter.mesh.triangles;
mesh.uv = uvs;
mesh.Optimize();
mesh.RecalculateNormals();

Given the following texture atlas, I’d expect every face to be green:

However, this is the result I get, where some faces are solid (wrong colour though) and some aren’t:

Are some verts sharing UVs or have I gone wrong somewhere else?

I also tried a more simple example. Given a cube that exists already, I used this example from the Unity docs so assign UVs. The output was the following:

I also took a look at the UV layout in the inspector for my original cube, which definitely looks off.

Apologies but I’m shamelessly bumping this as it took a while for this post to be moderated and was on the second page by the time it did.

Potentially. In Unity a vertex can only have one UV coordinate.

If you want different UVs on different faces, you need to not share verts between them.

You’re welcome to see more UV shenanigans in my MakeGeo project:

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

Thanks. Given that my primitive cube has 24 verts, none of them are being shared. Maybe the ordering of the UVs is incorrect?

Great theory, start printing them out!

Your coordinates you wrote on your atlas are completely off / all over the place. They make no sense at all. Yes, 0,0 is at the bottom left. However you have your intermediate values flipped around. “V” should grow from 0, to 0.25 to 0.5 to 0.75 to 1. Also your second “U” coordinate should go from left 0 to center 0.5 to right side 1. ALL “U” coordinates of the middle points should be 0.5, from top to bottom. So if you used this chart to pick your coordinates, no wonders they are all over the place ^^.

This is what your atlas should look like:

9152303--1272443--upload_2023-7-17_17-3-12.png

Finally you assume that 4 consecutive vertices actually form a face. That might be the case, but doesn’t have to be that way. The vertices of a mesh could be completely random and scrambled. The triangles array actually forms triangles out of the pool of vertices. When using my UVViewer which can show you the triangle list of a mesh, you can see that the vertices of the default code does not really align with the faces nicely.

The default cube’s triangle list consists of 6 triangle pairs (12 triangles in total) which seem to be laid out like this

 Quad  | Triangles |  0 ,  1 ,  2  |  3 ,  4 ,  5   |  Direction
       |           |      T0       |      T1        |
-----------------------------------------------------------------
  0    |  0 +  1   | V00  V02  V03 | V00  V03  V01  | +forward
  1    |  2 +  3   | V08  V04  V05 | V08  V05  V09  | +up
  2    |  4 +  5   | V10  V06  V07 | V10  V07  V11  | +back
  3    |  6 +  7   | V12  V13  V14 | V12  V14  V15  | +down
  4    |  8 +  9   | V16  V17  V18 | V16  V18  V19  | +left
  5    | 10 + 11   | V20  V21  V22 | V20  V22  V23  | +right

Note the vertices of quad 1 and 2. The vertices are not consecutive in the vertices array. So unless the mesh is your own mesh, you should not rely on a certain vertex order. As I said, you could completely scramble the vertices array and adjust the triangle indices so the triangles are still the same and the mesh would look the same.

ps: When you use my UVViewer, holding down “ctrl” will show you the closest UV coordinates. Also note that you can switch the UV channels as well. The TriangleList can be shown with the button on the top right.

How did I miss this! Staring at the same image for that length of time has caused me to miss this glaring thing! Thank you. I’ll have to fix this up later but it looks like this is the main issue.

I’ll have a look at your UV Viewer as I was getting a bit confused with figuring out which order UVs are going in.