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.
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);
}
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:
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:
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
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.