Search Unity

Question I don't know why the clumping occurs during automatic UV generation.

Discussion in 'General Graphics' started by chealin, May 19, 2023.

  1. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    76
    hello
    I'm using a script that automatically generates UVs
    However, only one part of the cylinder is formed by putting it together.

    In my opinion, it seems that the UV was created repeatedly in that small part.

    I'm not sure how to solve it.
    Is there any way to solve this?

    -My Result-
    upload_2023-5-19_11-46-6.png

    - MYCode-
    Code (CSharp):
    1.     public void PlayAutoUV()
    2.     {
    3.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    4.         if (mesh != null)
    5.         {
    6.             // Check if the mesh already has UVs
    7.             if (mesh.uv.Length == 0)
    8.             {
    9.                 // Generate cylinder UVs
    10.                 Vector2[] uvs = new Vector2[mesh.vertices.Length];
    11.  
    12.  
    13.                 for (int i = 0; i < mesh.vertices.Length; i++)
    14.                 {
    15.                     Vector3 vertexPos = mesh.vertices[i];
    16.                     Vector3 normalizedPos = new Vector3(vertexPos.x, 0f, vertexPos.z).normalized;
    17.  
    18.                     //float angle = Mathf.Atan2(vertexPos.z, vertexPos.x);
    19.                     float u = Mathf.Atan2(normalizedPos.x, normalizedPos.z) / (2f * Mathf.PI) + 0.5f;//angle / (2 * Mathf.PI) + 0f;
    20.                                                                                                      //float v = vertexPos.y / mesh.bounds.size.y;
    21.                     float v = (vertexPos.y - mesh.bounds.min.y) / mesh.bounds.size.y;
    22.  
    23.  
    24.                     uvs[i] = new Vector2(u, v);
    25.                 }
    26.  
    27.                 // Assign the new UVs to the mesh
    28.                 mesh.uv = uvs;
    29.             }
    30.         }
    31.     }
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,917
    This happens because UV coordinates are interpolated across triangles, just like all other per-vertex properties (color, normal, tangent, etc). So the last triangle wraps around the entire UV space, since its vertices have u = 0 and u = 1.

    Duplicate the last row of vertices to avoid them being shared by triangles, one copy should have the U coordinate set to 0 and the other one set to 1, creating a UV seam. This will prevent wraparound.
     
  3. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    76

    When loading the mesh, I don't know which part is the last row
    What is the criterion for the last row?

    All I know now is to use only the values in a one-dimensional array.

    []
    *Vector3[] - mesh.vertices;
    *int[] - mesh.triangles;
    *Vector2[] - mesh.uv;

    Is there a way to find out the last row through some other approach besides these?
    I create UVs with a script and proceed.
    I don't know how to figure out the last row with this