Issue with generating mesh plane with high vertex count

I’m able to generate mesh plane by providing vertex count along x & z-axis. i.e,
x_count for x-vertices and z_count for z-vertices. So, total vertices in mesh equal to x_count*z_count.

But for a high value of vertices like values of x & z count above 300 or 500, it gives weird results.

Here’s my code:

using UnityEngine;

public class MeshGen : MonoBehaviour
{
    public Material mat;

    public int x_cnt = 2;
    public int z_cnt = 2;
    public float x_scale = 1f;
    public float z_scale = 1f;

    Vector3[] vert;
    int[] tri;
    //Vector2[] uvs;

    void Start()//public void Generate()
    {
        if (x_scale <= 0f)
        {
            x_scale = 1f;
            Debug.Log("Xscale less than or equal to 0");
        }
        if (z_scale <= 0f)
        {
            z_scale = 1f;
            Debug.Log("Zscale less than or equal to 0");
        }

        if(x_cnt >= 2 && z_cnt >= 2)
        {
            MakePlane();
        }
    }

    //make mesh with uv mapping...
    void MeshMake(ref Vector3[] v, ref int[] t, ref Vector2[] u, Material m)
    {
        Mesh me = new Mesh();
        me.vertices = v;
        me.triangles = t;
        me.uv = u;
        me.RecalculateNormals();
        me.Optimize();
        transform.gameObject.AddComponent<MeshFilter>();
        transform.gameObject.AddComponent<MeshRenderer>();
        transform.GetComponent<MeshFilter>().mesh = me;
        transform.gameObject.GetComponent<MeshRenderer>().material = m;
    }

    //make mesh without uv mapping...
    void MeshMake(ref Vector3[] v, ref int[] t, Material m)
    {
        Mesh me = new Mesh();
        if(!transform.GetComponent<MeshFilter>() || !transform.GetComponent<MeshRenderer>())
        {
            transform.gameObject.AddComponent<MeshFilter>();
            transform.gameObject.AddComponent<MeshRenderer>();
        }
        transform.GetComponent<MeshFilter>().mesh = me;
        me.vertices = v;
        me.triangles = t;
        me.RecalculateNormals();
        transform.gameObject.GetComponent<MeshRenderer>().material = m;
    }

    void MakePlane()
    {
        //define vertices...
        vert = new Vector3[x_cnt * z_cnt];
        for (int i = 0; i < z_cnt; i++)
        {
            for (int j = 0; j < x_cnt; j++)
            {
                int idx = i * x_cnt + j;
                vert[idx] = new Vector3(j * x_scale, 0f, i * z_scale);
            }
        }

        //define triangles...
        tri = new int[(x_cnt - 1) * (z_cnt - 1) * 6];
        int id = 0;
        for(int i = 0; i < z_cnt-1; i++)
        {
            for(int j =0; j < x_cnt-1; j++)
            {
                //first triangle..
                tri[id] = i * x_cnt + j;
                tri[id + 1] = tri[id] + x_cnt;
                tri[id + 2] = tri[id+1] + 1;

                //second triangle..
                tri[id + 3] = tri[id];
                tri[id + 4] = tri[id+2];
                tri[id + 5] = tri[id]+1;

                id += 6;
            }
        }

        //define uvs...
        /*uvs = new Vector2[x_cnt * z_cnt];
        for (int i = 0; i < z_cnt; i++)
        {
            for (int j = 0; j < x_cnt; j++)
            {
                int idx = i * x_cnt + j;
                uvs[idx] = new Vector2(j/(x_cnt-1),i/(z_cnt-1));
            }
        }*/

        MeshMake(ref vert, ref tri, mat);  //mesh without uv mappin..
    }
}

Here’s my results:

for x_count = 200 & z_count = 200

for x_count = 500 & z_count = 500

Are you sure it’s the mesh and not the rendering? this kinda looks like shadow acne. (edit: actually looks like artefacts from the wireframe?)
if you divide it into multiple meshes of the same total size does this still happen?

It’s in shaded wireframe mode.
I was using it to check if mesh is generated correctly or not. (like culling issue, triangles generated…)

The issue is with the integer bit size that exceeds the mesh vertices array length limit. Integers used here are 16 bit which makes them 2^16 = 65536. As you create 2-dimensional plane here with with lets say width=256 and height=256 and which makes the vertices count equal to wh which is 256256=65536, any width or height increase after this limit will result in the loss of vertices. For solving this issue, try to make more planes connected to each other, which is called ‘chunks’ in the terrain generation concept, or set the indexFormat to 32 bits using:
mesh.indexFormat = IndexFormat.UInt32;
.

1 Like

2^32 is not 65536.

2^16 is 65536.
2^32 is 4294967296.