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

20

我听说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>());

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

256 x 256 的网格没问题,但 512 x 512 的网格只显示了它区域的四分之一,并且有很多错误的三角形。

这里输入图片描述

这里输入图片描述

1个回答

39

默认情况下,网格缓冲区为16位。请参见Mesh-indexFormat

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

不需要太仔细检查您的其余代码,我注意到您没有设置32位缓冲区。尝试使用以下内容:

mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

谢谢!这正是我想要的。 - landings
@Lece,我非常欣赏你回答问题的表达和阐述方式。如果你能帮我看看这个问题,我会很高兴的 https://dev59.com/TVUL5IYBdhLWcg3wI1Ip - SuperHyperMegaSomething

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接