List <Vertex> IndexVertices(TriangleNet.Data.Triangle triangle, Dictionary <int, int> indexDict, ref int highestIndex, out int[] newIndices)
        {
            var newVertices = new List <Vertex>();

            newIndices = new int[3];

            if (indexDict.ContainsKey(triangle.P0))
            {
                newIndices[0] = indexDict[triangle.P0];
            }
            else
            {
                TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0);
                newVertices.Add(new Vertex((float)vertex0.X, (float)vertex0.Y));
                indexDict.Add(vertex0.ID, highestIndex);
                newIndices[0] = highestIndex;
                highestIndex++;
            }

            if (indexDict.ContainsKey(triangle.P1))
            {
                newIndices[1] = indexDict[triangle.P1];
            }
            else
            {
                TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1);
                newVertices.Add(new Vertex((float)vertex1.X, (float)vertex1.Y));
                indexDict.Add(vertex1.ID, highestIndex);
                newIndices[1] = highestIndex;
                highestIndex++;
            }

            if (indexDict.ContainsKey(triangle.P2))
            {
                newIndices[2] = indexDict[triangle.P2];
            }
            else
            {
                TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2);
                newVertices.Add(new Vertex((float)vertex2.X, (float)vertex2.Y));
                indexDict.Add(vertex2.ID, highestIndex);
                newIndices[2] = highestIndex;
                highestIndex++;
            }

            return(newVertices);
        }
Exemple #2
0
        protected void GenerateTrisForShape()
        {
            if (vertices.Length >= 6 && vertices.Length % 2 == 0)
            {
                InputGeometry bottomGeometry = new InputGeometry();
                List <Point>  shape          = new List <Point> ();
                int           halfVertLen    = vertices.Length / 2;
                for (int i = 0; i < halfVertLen; i++)
                {
                    shape.Add(new Point(vertices[i].x, vertices[i].z));
                }
                bottomGeometry.AddRing(shape);

                TriangleNet.Mesh bottomMesh = new TriangleNet.Mesh();
                bottomMesh.Triangulate(bottomGeometry);

                List <Vector3> verts           = new List <Vector3>(bottomMesh.triangles.Count * 3);
                List <int>     triangleIndices = new List <int>(bottomMesh.triangles.Count * 3);
                foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in bottomMesh.triangles)
                {
                    TriangleNet.Data.Triangle triangle = pair.Value;

                    TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0);
                    TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1);
                    TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2);

                    Vector3 p0 = new Vector3(vertex0.x, 0, vertex0.y);
                    Vector3 p1 = new Vector3(vertex1.x, 0, vertex1.y);
                    Vector3 p2 = new Vector3(vertex2.x, 0, vertex2.y);

                    verts.Add(p0);
                    verts.Add(p1);
                    verts.Add(p2);

                    triangleIndices.Add(GetIndexForVertex(p0));
                    triangleIndices.Add(GetIndexForVertex(p1));
                    triangleIndices.Add(GetIndexForVertex(p2));
                }

                // Create top Side
                List <int> triangleIndecesReversed = new List <int>();
                for (int i = triangleIndices.Count - 1; i >= 0; i--)
                {
                    triangleIndecesReversed.Add(triangleIndices[i] + halfVertLen);
                }
                triangleIndices.AddRange(triangleIndecesReversed);

                // Add edges
                List <int> edgeIndeces = new List <int>();
                for (int i = 0; i < halfVertLen; i++)
                {
                    int leftBottom  = i;
                    int rightBottom = (i == halfVertLen - 1 ? 0 : i + 1);
                    int leftTop     = i + halfVertLen;
                    int rightTop    = (i == halfVertLen - 1 ? halfVertLen : i + halfVertLen + 1);

                    edgeIndeces.Add(leftTop);
                    edgeIndeces.Add(rightTop);
                    edgeIndeces.Add(leftBottom);

                    edgeIndeces.Add(rightBottom);
                    edgeIndeces.Add(leftBottom);
                    edgeIndeces.Add(rightTop);
                }
                triangleIndices.AddRange(edgeIndeces);

                tris = triangleIndices.ToArray();
            }
            else
            {
                Debug.LogError("[BaseShape.GenerateTrisForShape] Invalid vertices set for shape!");
            }
        }