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

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-

  • MYCode-
    public void PlayAutoUV()
    {
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        if (mesh != null)
        {
            // Check if the mesh already has UVs
            if (mesh.uv.Length == 0)
            {
                // Generate cylinder UVs
                Vector2[] uvs = new Vector2[mesh.vertices.Length];


                for (int i = 0; i < mesh.vertices.Length; i++)
                {
                    Vector3 vertexPos = mesh.vertices[i];
                    Vector3 normalizedPos = new Vector3(vertexPos.x, 0f, vertexPos.z).normalized;

                    //float angle = Mathf.Atan2(vertexPos.z, vertexPos.x);
                    float u = Mathf.Atan2(normalizedPos.x, normalizedPos.z) / (2f * Mathf.PI) + 0.5f;//angle / (2 * Mathf.PI) + 0f;
                                                                                                     //float v = vertexPos.y / mesh.bounds.size.y;
                    float v = (vertexPos.y - mesh.bounds.min.y) / mesh.bounds.size.y;


                    uvs[i] = new Vector2(u, v);
                }

                // Assign the new UVs to the mesh
                mesh.uv = uvs;
            }
        }
    }

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.

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