“设置三角形失败。某些索引正在引用超出范围的顶点。”当边缘索引大于顶点数量

时间:2019-05-23 03:09:16

标签: unity3d marching-cubes

我试图使行进多维数据集算法能够统一工作,但是当我尝试根据三角测量表将三角形添加到顶点时,出现标题中指出的错误

如果我尝试使用小于顶点长度的数字绘制三角形,那么它将起作用。

//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 [])“

An image of my corners and vertices I want to draw triangles between

An image of the way I have my edges mapped

1 个答案:

答案 0 :(得分:0)

Mesh.triangles中的整数是Mesh.vertices的索引。

3个连续索引组成一个三角形。所以:

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

例如,您有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
};