Unity3d &引用;设置三角形失败。有些索引引用了超出边界的顶点。”;当边索引大于顶点数时

Unity3d &引用;设置三角形失败。有些索引引用了超出边界的顶点。”;当边索引大于顶点数时,unity3d,marching-cubes,Unity3d,Marching Cubes,我试图让marching cubes算法在unity中工作,但是当我试图基于三角剖分表向顶点添加三角形时,我得到了标题中说明的错误 如果我尝试使用小于顶点长度的数字绘制三角形,则效果会很好 //Store the values and corner positions of the cube public class Cube { public int[] values; public Vector3[] corners; public Cube() { }

我试图让marching cubes算法在unity中工作,但是当我试图基于三角剖分表向顶点添加三角形时,我得到了标题中说明的错误

如果我尝试使用小于顶点长度的数字绘制三角形,则效果会很好

//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();
//存储多维数据集的值和角点位置
公共类多维数据集
{
公共价值观;
公共向量3[]个角;
公共多维数据集(){
}
公共多维数据集(int[]val,Vector3[]cor)
{
值=val;
角=cor;
}
}
//生成网格
gridOfPoints=新矢量3[8];
gridOfPoints[0]=新向量3(0,0,1);
gridOfPoints[1]=新向量3(1,0,1);
gridOfPoints[2]=新向量3(1,0,0);
gridOfPoints[3]=新向量3(0,0,0);
gridOfPoints[4]=新向量3(0,1,1);
gridOfPoints[5]=新向量3(1,1,1);
gridOfPoints[6]=新向量3(1,1,0);
gridOfPoints[7]=新向量3(0,1,0);
//添加值和泊松
Cube firstCube=新的立方体(新的int[8]{-1,-1,0,0,0,0},gridOfPoints);
//用多维数据集填充多维数据集数组
多维数据集=新多维数据集[]{firstCube};
列表顶点=新列表();
列表三角形=新列表();
//三角测量查找表
int[,]triTable=新的int[256,16]{。。。
{5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1}
...}
int-cubeIndex=162;
//获取边数组
int[]三角剖分=新int[16];
对于(int i=0;i<16;i++)
{
三角测量[i]=可测[cubeIndex,i];
}
//连接到边查找表的角点
int[,]cornerPointsToEdge=新的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 } 
};
//由三角剖分表求边的中心点
foreach(三角测量中的int edgeIndex)
{   
如果(边索引==-1){
继续;
}
int indexA=拐角点至边缘[edgeIndex,0];
int indexB=拐角点至边缘[edgeIndex,1];
向量3顶点=(立方体[0]。角点[indexA]+立方体[0]。角点[indexB])/2;
//将中心点添加到顶点
顶点。添加(顶点);
//将边添加到三角形列表
三角形。添加(边索引);
}
mesh.vertices=顶点.ToArray();
mesh.triangles=triangles.ToArray();
mesh.normals();
这是我收到的完整错误消息“设置三角形失败。某些索引引用了超出边界的顶点。IndexCount:9,VertexCount:9 UnityEngine.Mesh:设置_三角形(Int32[])”

中的整数是的索引

3个连续索引组成一个三角形。因此:

  • 值必须在以下范围内:[0,
    Mesh.vertices.Length
  • 网格的长度。三角形必须是3的倍数
  • 三角形的正面由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
};

谢谢你的回答,但是如果我想画一个边11,3,2上有顶点的三角形,我应该怎么做?2条边和一个共享点可以组成一个三角形,你应该把它们转换成点序列,例如边{7,3},{3,0}->7,3,0…抱歉,我无法理解为什么11,3,2有四个点?非常感谢您的帮助,谢谢!从您的表格中,这三条边包含点0,2,3,7,因此如果我理解正确,它们是4个点。