"Failed setting triangles. Some indices are referencing out of bounds vertices." 当边索引大于顶点数时

"Failed setting triangles. Some indices are referencing out of bounds vertices." when the the edge index is greater than the number of vertices

我试图让行进立方体算法统一工作,但是当我尝试根据三角剖分向我的顶点添加三角形时 table 我得到标题中所述的错误

如果我尝试使用小于顶点长度的数字绘制三角形,它会成功。

//Store the values and corner positions of the cube
public class Cube
{

    public int[] values;
    public Vector3[] corners;

    public Cube() { 
    }
    public Cube(int[] val, Vector3[] cor)
    {
        values = val;
        corners = cor;
    }


}

//Generate the grid
        gridOfPoints = new Vector3[8];
        gridOfPoints[0] = new Vector3(0, 0, 1);
        gridOfPoints[1] = new Vector3(1, 0, 1);
        gridOfPoints[2] = new Vector3(1, 0, 0);
        gridOfPoints[3] = new Vector3(0, 0, 0);
        gridOfPoints[4] = new Vector3(0, 1, 1);
        gridOfPoints[5] = new Vector3(1, 1, 1);
        gridOfPoints[6] = new Vector3(1, 1, 0);
        gridOfPoints[7] = new Vector3(0, 1, 0);

//Add values and poistions
Cube firstCube = new Cube(new int[8] { -1, -1, 0, 0, 0, 0, 0, 0 }, gridOfPoints);

//Populate cubes array with cubes
cubes = new Cube[] { firstCube };

List<Vector3> vertices = new List<Vector3>();

List<int> triangles = new List<int>();


//Triangulation lookup table
int[,] triTable = new int[256,16]{ ... 
{5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1}
...}


int cubeIndex = 162;

//Getting the array of edges
int[] triangulation = new int[16];


        for (int i = 0; i < 16; i++)
        {
            triangulation[i] = triTable[cubeIndex, i];
        }

//corners connected to edges lookup table
int[,] cornerPointsToEdge = new int[12, 2] { 
                                                { 0, 1 }, 
                                                { 1, 2 }, 
                                                { 2, 3 }, 
                                                { 3, 0 }, 
                                                { 4, 5 }, 
                                                { 5, 6 }, 
                                                { 6, 7 },
                                                { 7, 4 }, 
                                                { 4, 0 }, 
                                                { 5, 1 }, 
                                                { 6, 2 }, 
                                                { 7, 3 } 
                                            };
//Getting the centre point of the edge in given by the triangulation table
foreach (int edgeIndex in triangulation)
        {   

            if(edgeIndex == -1) {
                continue;
            }


            int indexA = cornerPointsToEdge[edgeIndex, 0];
            int indexB = cornerPointsToEdge[edgeIndex, 1];

            Vector3 vertexPos = (cubes[0].corners[indexA] + cubes[0].corners[indexB]) / 2;

            //Adding the centre point to the vertices
            vertices.Add(vertexPos);
            //Adding the edge to the triangles list 
            triangles.Add(edgeIndex);

        }


mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();

这是我收到的完整错误消息“设置三角形失败。一些索引引用了越界顶点。IndexCount:9,VertexCount:9 UnityEngine.Mesh:set_triangles(Int32[])"

Mesh.triangles are indices of Mesh.vertices中的整数。

3 个顺序索引组成一个三角形。所以:

  • 值必须在范围内:[0, Mesh.vertices.Length)
  • Mesh.triangles 的长度必须是 3 的倍数。
  • 三角形的正面由 3 个索引的顺序使用 left-handed rule
  • 定义

例如你有 4 个顶点:

mesh.vertices = new Vector3[]
{
    new Vector3(0, 0, 0), //0
    new Vector3(0, 1, 0), //1
    new Vector3(1, 0, 0), //2
    new Vector3(1, 1, 0), //3
};

您可以生成一个带有 2 个三角形的垂直矩形

mesh.triangles = new int[]
{
     0, 1, 3, //triangle 0
     0, 3, 2, //triangle 1
};