1
\$\begingroup\$

I have this code below that I found on catlikecoding that generates a procedural grid that I want to apply noise to, however once I go over a certain value it does expand vertically. Can anyone see why it wouldn't?

For example, 100x100: 100x100 procedural grid

However, 500x500 is clearly wrong: enter image description here

Code used:

using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Island : MonoBehaviour {

public int xSize, ySize;

    private Mesh mesh;
    private Vector3[] vertices;

    private void Awake () {
        Generate();
    }

    private void Generate () {
        GetComponent<MeshFilter>().mesh = mesh = new Mesh();
        mesh.name = "Procedural Island";

        vertices = new Vector3[(xSize + 1) * (ySize + 1)];
        Vector2[] uv = new Vector2[vertices.Length];

        for (int i = 0, y = 0; y <= ySize; y++) {
            for (int x = 0; x <= xSize; x++, i++) {
                vertices[i] = new Vector3(x, y);
                uv[i] = new Vector2((float)x / xSize, (float)y / ySize);
            }
        }

        mesh.vertices = vertices;
        mesh.uv = uv;

        int[] triangles = new int[xSize * ySize * 6];
        for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++) {
            for (int x = 0; x < xSize; x++, ti += 6, vi++) {
                triangles[ti] = vi;
                triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
                triangles[ti + 5] = vi + xSize + 2;
            }
        }


        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ A unity mesh can only have 65536 indices, 100x100 is just barely below that. There should be an error about this in the console \$\endgroup\$
    – Bálint
    Mar 20, 2019 at 11:13
  • \$\begingroup\$ @Bálint that looks like it could be an answer, especially if you pair it with a tip or two about what to do instead. \$\endgroup\$
    – DMGregory
    Mar 20, 2019 at 11:34
  • \$\begingroup\$ @Bálint There's no console error, but that's useful to know. Thanks \$\endgroup\$
    – Daniel
    Mar 20, 2019 at 11:49

2 Answers 2

2
\$\begingroup\$

Been there, done that. I followed that series and encountered the exact same problem. I wrote directly to the author on facebook and he was kind enough to answer me that the problem lies in https://docs.unity3d.com/ScriptReference/Mesh-indexFormat.html

In this case the default IndexFormat.UInt16 is used, so the max vertices number you can have is 65536 (= 256x256) - thats why a 500x500 verticies list gives you a badly generated mesh. This can be solved simply by changing the mesh's index format to UInt32 (then you have max 4294967296 vertices )

Another alternative solution is spliting the mesh into smaller pieces as Bálint has suggested.

\$\endgroup\$
0
\$\begingroup\$

Unity meshes can only contain 65536 indices (=triangles array). Since 100x100 has 10000 squares and each needs 6 indices, that's just barely below 65536.

To solve this you can split it into multiple pieces, which would also allow you to implement LODs and view distance.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .