从点列表构建索引缓冲区
Building an index buffer from a list of points
我在 2d space 中有一个点列表,我需要从它们构建一个顶点和索引缓冲区(我使用的是 Unity)。我无法弄清楚如何以正确的顺序处理这些点来做到这一点。
下面的星是一个例子,我的输入保证有效但在编译时未知。
mesh0 = new Mesh();
// vertices for a star shape
List<Vector3> vertices0 = new List<Vector3>();
List<int> triangles0 = new List<int>();
vertices0.Add(new Vector3(150, 0, 0));
vertices0.Add(new Vector3(121, -90, 0));
vertices0.Add(new Vector3(198, -35, 0));
vertices0.Add(new Vector3(102, -35, 0));
vertices0.Add(new Vector3(179, -90, 0));
// this bit is wrong
for(int i = 0; i < vertices0.Count - 1; i++) {
triangles0.Add(i);
triangles0.Add(i + 1);
triangles0.Add(i + 2);
}
mesh0.SetVertices(vertices);
mesh0.SetTriangles(triangles, 0);
我希望这种情况下的三角形是 [0, 1, 2, 2, 3, 4, 0, 4, 3]
,这将是渲染星形的有效索引缓冲区。谁能指出我正确的方向?
你的三角形数组将是
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
5 让你失望,但你也只有 4 个三角形。
在这种情况下,您需要将三角形环绕起来
我建议将您的三角形生成重新格式化为:
for(int i = 0; i < vertices0.Count; i++)
{
triangles0.Add(i);
triangles0.Add((i + 1) % vertices0.Count);
triangles0.Add((i + 2) % vertices0.Count);
}
此处的模数运算符将 "wrap" 如果您的值大于计数,则它们会围绕您的值。我还从顶点数中删除了 -1 因为您实际上并没有添加第 5 个三角形
虽然生成了五边形,但仍然没有生成星星,并输出以下三角形:
0,1,2
1,2,3
2,3,4
3,4,0
4,0,1
它也有很多重叠的三角形,但我相信您可以从这里对其进行改造以执行您想要的操作
我在 2d space 中有一个点列表,我需要从它们构建一个顶点和索引缓冲区(我使用的是 Unity)。我无法弄清楚如何以正确的顺序处理这些点来做到这一点。
下面的星是一个例子,我的输入保证有效但在编译时未知。
mesh0 = new Mesh();
// vertices for a star shape
List<Vector3> vertices0 = new List<Vector3>();
List<int> triangles0 = new List<int>();
vertices0.Add(new Vector3(150, 0, 0));
vertices0.Add(new Vector3(121, -90, 0));
vertices0.Add(new Vector3(198, -35, 0));
vertices0.Add(new Vector3(102, -35, 0));
vertices0.Add(new Vector3(179, -90, 0));
// this bit is wrong
for(int i = 0; i < vertices0.Count - 1; i++) {
triangles0.Add(i);
triangles0.Add(i + 1);
triangles0.Add(i + 2);
}
mesh0.SetVertices(vertices);
mesh0.SetTriangles(triangles, 0);
我希望这种情况下的三角形是 [0, 1, 2, 2, 3, 4, 0, 4, 3]
,这将是渲染星形的有效索引缓冲区。谁能指出我正确的方向?
你的三角形数组将是
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
5 让你失望,但你也只有 4 个三角形。
在这种情况下,您需要将三角形环绕起来
我建议将您的三角形生成重新格式化为:
for(int i = 0; i < vertices0.Count; i++)
{
triangles0.Add(i);
triangles0.Add((i + 1) % vertices0.Count);
triangles0.Add((i + 2) % vertices0.Count);
}
此处的模数运算符将 "wrap" 如果您的值大于计数,则它们会围绕您的值。我还从顶点数中删除了 -1 因为您实际上并没有添加第 5 个三角形
虽然生成了五边形,但仍然没有生成星星,并输出以下三角形:
0,1,2
1,2,3
2,3,4
3,4,0
4,0,1
它也有很多重叠的三角形,但我相信您可以从这里对其进行改造以执行您想要的操作