我听说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 的网格只显示了它区域的四分之一,并且有很多错误的三角形。