20

I've heard that Unity supports 32-bit index buffer now. But when I try Unity 2018.1 I can't make it work.

I built meshes in code like this:

    int nVertices = nx * ny;
    Vector3[] vertices = new Vector3[nVertices];
    Color[] colors = new Color[nVertices];
    for(int i = 0; i < nx; i++) {
        float x = i * w / (nx - 1);
        for (int j = 0; j < ny; j++) {
            float y = j * h / (ny - 1);
            int vindex = i * ny + j;
            vertices[vindex] = new Vector3(x, y, 0);
            float colorx = Mathf.Sin(x) / 2 + 0.5f;
            float colory = Mathf.Cos(y) / 2 + 0.5f;
            colors[vindex] = new Color(0, 0, 0, colorx * colory);
        }
    }
    List<int> triangles = new List<int>();
    for (int i = 1; i < nx; i++) {
        for (int j = 1; j < ny; j++) {
            int vindex1 = (i - 1) * ny + (j - 1);
            int vindex2 = (i - 1) * ny + j;
            int vindex3 = i * ny + (j - 1);
            int vindex4 = i * ny + j;
            triangles.Add(vindex1);
            triangles.Add(vindex2);
            triangles.Add(vindex3);
            triangles.Add(vindex4);
            triangles.Add(vindex3);
            triangles.Add(vindex2);
        }
    }
    Mesh mesh = new Mesh();
    mesh.SetVertices(vertices.ToList<Vector3>());
    mesh.SetIndices(triangles.ToArray(), MeshTopology.Triangles, 0);
    mesh.SetColors(colors.ToList<Color>());

My shader draws rainbow pattern according to vertex color's alpha value.

256 x 256 grid is OK, but 512 x 512 grid only shows 1/4 of its area and many wrong triangles.

enter image description here

enter image description here

Lece
  • 2,339
  • 1
  • 17
  • 21
landings
  • 686
  • 1
  • 8
  • 25

1 Answers1

38

Mesh buffers are 16 bit by default. See Mesh-indexFormat:

Index buffer can either be 16 bit (supports up to 65535 vertices in a mesh), or 32 bit (supports up to 4 billion vertices). Default index format is 16 bit, since that takes less memory and bandwidth.

Without looking too closely at the rest of your code, I do note that you've not set a 32 bit buffer. Try:

mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
Lece
  • 2,339
  • 1
  • 17
  • 21
  • Thanks! This is exactly what I want. – landings May 20 '18 at 11:28
  • @Lece I really appreciate the way you express and articulate your answers. I would be glad if you could look at this question for me https://stackoverflow.com/questions/50788120/how-to-make-individual-anchor-points-of-bezier-continuous-or-non-continuous – SuperHyperMegaSomething Jun 13 '18 at 09:41