Пример #1
0
        private Mesh CreatePreviewMesh()
        {
            var pointsAndQuads = splitManifold.ToIdfs();

            var points = pointsAndQuads.Key;

            List <int> edges    = new List <int>();
            var        vertices = new Vector3[splitManifold.NumberOfAllocatedVertices()];

            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new Vector3((float)points[3 * i], (float)points[3 * i + 1], (float)points[3 * i + 2]);
            }

            int[]          polygons      = pointsAndQuads.Value;
            List <int>     polygonsFinal = new List <int>();
            List <Vector3> verticesFinal = new List <Vector3>();
            List <Vector3> normalsFinal  = new List <Vector3>();

            for (var i = 0; i < polygons.Length;)
            {
                int polyCount = polygons[i];
                i++;

                // add final vertices
                int vertexBase = verticesFinal.Count;
                for (int j = 0; j < polyCount; j++)
                {
                    verticesFinal.Add(vertices[polygons[i + j]]);
                }
                Vector3 normal = Vector3.zero;

                // triangulate polygon
                for (int j = 1; j + 1 < polyCount; j++)
                {
                    polygonsFinal.Add(vertexBase);
                    polygonsFinal.Add(vertexBase + j);
                    polygonsFinal.Add(vertexBase + j + 1);

                    normal += -Vector3.Cross(verticesFinal[vertexBase] - verticesFinal[vertexBase + j],
                                             verticesFinal[vertexBase + j + 1] - verticesFinal[vertexBase + j]).normalized;
                }
                normal.Normalize();

                for (int j = 0; j < polyCount; j++)
                {
                    normalsFinal.Add(normal);
                    edges.Add(vertexBase + j);
                    edges.Add(vertexBase + (j + 1) % polyCount);
                }

                i += polyCount;
            }

            Mesh splitMesh = new Mesh();

            splitMesh.name = "extruded";
            splitMesh.SetIndices(new int[0], MeshTopology.Triangles, 0);
            //splitMesh.SetIndices(new int[0], MeshTopology.Lines, 1);
            splitMesh.vertices = verticesFinal.ToArray();
            //splitMesh.normals = normalsFinal.ToArray();
            splitMesh.subMeshCount = 2;
            splitMesh.SetIndices(polygonsFinal.ToArray(), MeshTopology.Triangles, 0);
            splitMesh.SetIndices(edges.ToArray(), MeshTopology.Lines, 1);
            splitMesh.UploadMeshData(false);

            return(splitMesh);
        }