Tiling Not working properly on creating mesh manually, but working fine on unity Default plane

I am trying to create a Plane using custom mesh.
I have applied the same material to a Unity Default plane and another plane is generated using code.
But the material looks different on both the mesh.

Mesh detail:

Left Mesh-> Unity Default plane
Right Mesh → Mesh Generated Using Code.

//    assigning vertices, traingles, and UVs
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();

        mesh.uv = GetUVs(mesh.vertices);
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.RecalculateTangents();


// Generating UVs for custom plane
    Vector2[] GetUVs(Vector3[] verticesList)
    {
        Vector2[] uvs = new Vector2[verticesList.Length];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(verticesList[i].x, verticesList[i].y);
        }
        return uvs;
    }

I have to generate a plane manually because it is a wall and I am placing an opened door and window between the wall, so the mesh needs to be cut, that’s why I am generating mesh manually.

Need help fixing the tiling issue, I didn’t need tiling when I passed 1,1 tiling in the material.

Material details →

in both case material tiling x,y is 1,1

I may be wrong, but it looks like this means that the x and y coordinates of each vertex are integers from 0 to 10, according to their index in the array. Therefore, each quad of the plane covers the UV space between these integers (effectively the 0-1 space), and thus the entire texture, which is why it tiles like this. So, what I would try is dividing the x and y coordinates by the width and height in quads (10, in this case), so that the coordinates are in the 0-1 range.

1 Like

After Updating UVs it is working now

    private Vector2[] GenerateUVs(Vector3[] vertices)
    {
        Vector2[] uvs = new Vector2[vertices.Length];

        // Calculate the width and height of the mesh
        float width = Vector3.Distance(BL, BR);
        float height = Vector3.Distance(BL, TL);

        for (int i = 0; i < vertices.Length; i++)
        {
            float u, v;

            // Handle UVs based on wall orientation, keeping the UVs consistent
            if (Mathf.Approximately(BL.y, BR.y) && Mathf.Approximately(BL.y, TL.y))
            {
                // Wall aligned along the X-Z plane (Horizontal UV mapping)
                u = (vertices[i].x - BL.x) / width;
                v = (vertices[i].z - BL.z) / height;
            }
            else if (Mathf.Approximately(BL.x, BR.x) && Mathf.Approximately(BL.x, TL.x))
            {
                // Wall aligned along the Y-Z plane (Vertical UV mapping)
                u = (vertices[i].z - BL.z) / width;
                v = (vertices[i].y - BL.y) / height;
            }
            else
            {
                // Wall aligned along the X-Y plane (Default UV mapping)
                u = (vertices[i].x - BL.x) / width;
                v = (vertices[i].y - BL.y) / height;
            }

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

        return uvs;
    }