Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UV Map to primitive geometry

Discussion in 'Scripting' started by doddsey65_unity, Jul 15, 2023.

  1. doddsey65_unity

    doddsey65_unity

    Joined:
    Jan 28, 2018
    Posts:
    5
    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.

    Code (CSharp):
    1.  
    2. GameObject primative = GameObject.CreatePrimitive(PrimitiveType.Cube);
    3. primative.hideFlags = HideFlags.HideAndDontSave;
    4. MeshFilter primativeFilter = primative.GetComponent<MeshFilter>();
    5. primative.SetActive(false);
    6.  
    After this, I do a loop to assign the UVs:
    Code (CSharp):
    1.  
    2. Vector2[] uvs = new Vector2[primativeFilter.mesh.vertices.Length];
    3. for (int i = 0; i < primativeFilter.mesh.vertices.Length; i += 4) {
    4.     uvs[i] = new Vector2(0, 1);
    5.     uvs[i + 1] = new Vector2(0.5f, 1);
    6.     uvs[i + 2] = new Vector2(0, 0.25f);
    7.     uvs[i + 3] = new Vector2(0.25f, 0.5f);
    8. }
    9.  
    And then assign all of that to the mesh
    Code (CSharp):
    1.  
    2. Mesh mesh = new Mesh();
    3. mesh.vertices = primativeFilter.mesh.vertices;
    4. mesh.triangles = primativeFilter.mesh.triangles;
    5. mesh.uv = uvs;
    6. mesh.Optimize();
    7. mesh.RecalculateNormals();
    8.  
    Given the following texture atlas, I'd expect every face to be green:
    Screenshot 2023-07-15 151355.png

    However, this is the result I get, where some faces are solid (wrong colour though) and some aren't:
    Screenshot 2023-07-15 151625.png

    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:

    Screenshot 2023-07-15 164312.png

    I also took a look at the UV layout in the inspector for my original cube, which definitely looks off.
    Screenshot 2023-07-15 164406.png
     
    Last edited: Jul 15, 2023
  2. doddsey65_unity

    doddsey65_unity

    Joined:
    Jan 28, 2018
    Posts:
    5
    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.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    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

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  4. doddsey65_unity

    doddsey65_unity

    Joined:
    Jan 28, 2018
    Posts:
    5
    Thanks. Given that my primitive cube has 24 verts, none of them are being shared. Maybe the ordering of the UVs is incorrect?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Great theory, start printing them out!
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    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:

    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

    Code (CSharp):
    1.  Quad  | Triangles |  0 ,  1 ,  2  |  3 ,  4 ,  5   |  Direction
    2.        |           |      T0       |      T1        |
    3. -----------------------------------------------------------------
    4.   0    |  0 +  1   | V00  V02  V03 | V00  V03  V01  | +forward
    5.   1    |  2 +  3   | V08  V04  V05 | V08  V05  V09  | +up
    6.   2    |  4 +  5   | V10  V06  V07 | V10  V07  V11  | +back
    7.   3    |  6 +  7   | V12  V13  V14 | V12  V14  V15  | +down
    8.   4    |  8 +  9   | V16  V17  V18 | V16  V18  V19  | +left
    9.   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.
     
    Last edited: Jul 17, 2023
  7. doddsey65_unity

    doddsey65_unity

    Joined:
    Jan 28, 2018
    Posts:
    5
    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.