gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:45:43 24 4
gpt4 key购买 nike

听说 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/4 的面积和许多错误的三角形。

enter image description here

enter image description here

最佳答案

网格缓冲区默认为 16 位。参见 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.

无需仔细查看其余代码,我确实注意到您没有设置 32 位缓冲区。尝试:

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

关于c# - 如何在 Unity 2018.1 中使用超过 64k 个顶点的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50433894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com