如何在Unity 2018.1中使用具有超过64k顶点的网格

lan*_*ngs 9 c# rendering unity-game-engine

我听说Unity现在支持32位索引缓冲区.但是,当我尝试使用Unity 2018.1时,我无法使其工作.

我用这样的代码构建了网格:

    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>());
Run Code Online (Sandbox Code Playgroud)

我的着色器根据顶点颜色的alpha值绘制彩虹图案.

256 x 256网格是可以的,但512 x 512网格只显示其面积的1/4和许多错误的三角形.

在此输入图像描述

在此输入图像描述

Lec*_*ece 12

网格缓冲区默认为16位.请参见Mesh-indexFormat:

索引缓冲区可以是16位(在网格中最多支持65535个顶点),也可以是32位(最多支持40亿个顶点).默认索引格式为16位,因为这会占用更少的内存和带宽.

如果不仔细查看其余代码,我会注意到您没有设置32位缓冲区.尝试:

mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
Run Code Online (Sandbox Code Playgroud)