1

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

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

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

我想在它们之间绘制三角形的角和顶点的图像

我的边缘映射方式的图像

4

1 回答 1

4

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

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
};
于 2019-05-23T04:10:29.570 回答