Mesh Rendering or Generation Problem

The problem is that this script works fine for small mesh generation, for size (xSize and zSize both) up to 200+, but for higher sizes ~250+, it generates rectangles instead of squares. It’s not generating the mesh completely. I’ll attach a picture where dots are gizmos spheres for the vertices which should also be filled by triangles.

I saw on unity discussions that it is because of 65535 limit of vertices on a mesh at a time because of 16bit index buffer but the thing is, when I increases the size of the mesh, it also generates and renders more compared to low size mesh. So, it might not be because of that limit.

Here’s the script:

using UnityEngine;
public class WorldGeneration : MonoBehaviour
{
    readonly int xSize = 280;
    readonly int zSize = 280;

    Vector3[] vertices;
    int[] triangles;

    Mesh mesh;

    private void Start()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;

        CreateShape();
    }
    void CreateShape()
    {
        vertices = new Vector3[(xSize + 1) * (zSize + 1)];
        triangles = new int[xSize * zSize * 6];

        for (int i = 0, x = 0; x <= xSize; x++)
        {
            for (int z = 0; z <= zSize; z++)
            {
                //float y = Mathf.PerlinNoise(x * .3f, z * .3f) * 2f;
                // Add different terrains

                vertices[i] = new Vector3(x, 0, z);
                i++;
            }
        }

        int triIndex = 0, vertexIndex = 0;
        for (int x = 0; x < xSize; x++)
        {
            for (int z = 0; z < zSize; z++)
            {
                // Define the two triangles of the quad
                triangles[triIndex] = vertexIndex;       // First
                triangles[triIndex + 1] = vertexIndex + 1;   // Second
                triangles[triIndex + 2] = vertexIndex + zSize + 1;    // Third

                triangles[triIndex + 3] = vertexIndex + 1;   // First 
                triangles[triIndex + 4] = vertexIndex + zSize + 2;    //Second
                triangles[triIndex + 5] = vertexIndex + zSize + 1;   // Third

                triIndex += 6;
                vertexIndex++;
            }
            vertexIndex++;
        }

        mesh.Clear();

        mesh.vertices = vertices;
        mesh.triangles = triangles;

        mesh.RecalculateNormals();
    }

    private void OnDrawGizmos()
    {
        if(vertices == null) return;

        for(int j = 0; j < vertices.Length; j++)
        {
            Gizmos.DrawSphere(vertices[j], 0.1f);
        }
    }
}

Yes, this is the 16bit index buffer limitation. So when using a 16 bit index buffer, you can only create 256x256 vertices (though a bit less because Unity requires a bit of reserve it seems).

However you can simply switch to a 32 bit index buffer via the indexFormat. So just set it to IndexFormat.UInt32. before you assign anything to your mesh and your problems should be gone. Keep in mind that a 32 bit index buffer requires twice the size, but does support up to 4 billion (milliard) vertices.

Note that 281 * 281 == 78961. So that’s clearly larger than 65536. So about 13k vertices can’t be addressed or about 48 rows.