コード例 #1
0
        private void UpdateMeshNow()
        {
            MassProperties = Hull.ComputeMassProperties();

            Hull.CompactVertices();
            Hull.BuildFaceIndices((float)(MaxFaceAngle * System.Math.PI / 180));

            {
                HullData.Faces        = new Face[Hull.NumFaces];
                HullData.FaceVertices = new int[Hull.NumFaceVertices];
                int nextVertex = 0;
                for (var faceEdge = Hull.GetFirstFace(); faceEdge.IsValid; faceEdge = Hull.GetNextFace(faceEdge))
                {
                    var  triangleIndex = ((ConvexHullBuilder.Edge)faceEdge).TriangleIndex;
                    Face newFace       = new Face
                    {
                        Plane       = Hull.ComputePlane(triangleIndex),
                        FirstVertex = nextVertex,
                        NumVertices = 0
                    };
                    for (var edge = faceEdge; edge.IsValid; edge = Hull.GetNextFaceEdge(edge))
                    {
                        HullData.FaceVertices[nextVertex++] = Hull.StartVertex(edge);
                    }
                    newFace.NumVertices = nextVertex - newFace.FirstVertex;
                    HullData.Faces[Hull.Triangles[triangleIndex].FaceIndex] = newFace;
                }

                var indices = new List <int>();
                var set     = new List <int>();
                HullData.VertexFaces = new VertexFaces[Hull.Vertices.PeakCount];
                for (int i = 0; i < Hull.Vertices.PeakCount; ++i)
                {
                    var cardinality = Hull.Vertices[i].Cardinality;
                    var edge        = Hull.GetVertexEdge(i);
                    for (int j = Hull.Vertices[i].Cardinality; j > 0; --j)
                    {
                        int faceIndex = Hull.Triangles[edge.TriangleIndex].FaceIndex;
                        if (set.IndexOf(faceIndex) == -1)
                        {
                            set.Add(faceIndex);
                        }
                        edge = Hull.GetLinkedEdge(edge).Next;
                    }
                    set.Sort();
                    HullData.VertexFaces[i] = new VertexFaces {
                        FirstFace = indices.Count, NumFaces = set.Count
                    };
                    indices.AddRange(set);
                    set.Clear();
                }
                HullData.FaceIndices = indices.ToArray();
            }

            Vector3[] vertices  = null;
            int[]     triangles = null;
            switch (Hull.Dimension)
            {
            case 2:
                vertices  = new Vector3[Hull.Vertices.PeakCount];
                triangles = new int[(Hull.Vertices.PeakCount - 2) * 2 * 3];
                for (int i = 0; i < Hull.Vertices.PeakCount; ++i)
                {
                    vertices[i] = Hull.Vertices[i].Position;
                }
                for (int i = 2; i < Hull.Vertices.PeakCount; ++i)
                {
                    int j = (i - 2) * 6;
                    triangles[j + 0] = 0; triangles[j + 1] = i - 1; triangles[j + 2] = i;
                    triangles[j + 3] = 0; triangles[j + 4] = i; triangles[j + 5] = i - 1;
                }
                break;

            case 3:
                vertices  = new Vector3[Hull.Triangles.PeakCount * 3];
                triangles = new int[Hull.Triangles.PeakCount * 3];
                for (int i = 0; i < Hull.Triangles.PeakCount; ++i)
                {
                    if (Hull.Triangles[i].IsAllocated)
                    {
                        vertices[i * 3 + 0]  = Hull.Vertices[Hull.Triangles[i].Vertex0].Position;
                        vertices[i * 3 + 1]  = Hull.Vertices[Hull.Triangles[i].Vertex1].Position;
                        vertices[i * 3 + 2]  = Hull.Vertices[Hull.Triangles[i].Vertex2].Position;
                        triangles[i * 3 + 0] = i * 3 + 0;
                        triangles[i * 3 + 1] = i * 3 + 1;
                        triangles[i * 3 + 2] = i * 3 + 2;
                    }
                    else
                    {
                        triangles[i * 3 + 0] = 0;
                        triangles[i * 3 + 1] = 0;
                        triangles[i * 3 + 2] = 0;
                    }
                }
                break;
            }

            var mesh = GetComponent <MeshFilter>().sharedMesh;

            mesh.Clear();
            mesh.vertices  = vertices;
            mesh.triangles = triangles;
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
            mesh.RecalculateTangents();
        }