如何在Unity 2018.1中使用具有超过64k顶点的网格 [英] How to use meshes with more than 64k vertices in Unity 2018.1
问题描述
我听说Unity现在支持32位索引缓冲区.但是当我尝试Unity 2018.1时,我无法使其正常工作.
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>());
我的着色器根据顶点颜色的alpha值绘制彩虹图案.
My shader draws rainbow pattern according to vertex color's alpha value.
256 x 256网格是可以的,但是512 x 512网格仅显示其面积的1/4和许多错误的三角形.
256 x 256 grid is OK, but 512 x 512 grid only shows 1/4 of its area and many wrong triangles.
推荐答案
网格缓冲区默认为16位.请参见 Mesh-indexFormat :
Mesh buffers are 16 bit by default. See Mesh-indexFormat:
索引缓冲区可以是16位(在一个缓冲区中最多支持65535个顶点) 网格)或32位(最多支持40亿个顶点).默认索引 格式为16位,因为它占用更少的内存和带宽.
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.
在没有仔细查看代码其余部分的情况下,我确实注意到您没有设置32位缓冲区.试试:
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;
这篇关于如何在Unity 2018.1中使用具有超过64k顶点的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!