Example #1
0
        protected MyNavigationTriangle AddTriangle(ref Vector3 A, ref Vector3 B, ref Vector3 C, ref int edgeAB, ref int edgeBC, ref int edgeCA)
        {
            MyNavigationTriangle userData = this.m_triPool.Allocate();
            int num           = ((0 + ((edgeAB == -1) ? 1 : 0)) + ((edgeBC == -1) ? 1 : 0)) + ((edgeCA == -1) ? 1 : 0);
            int triangleIndex = -1;

            if (num == 3)
            {
                triangleIndex = this.m_mesh.MakeNewTriangle(userData, ref A, ref B, ref C, out edgeAB, out edgeBC, out edgeCA);
            }
            else if (num == 2)
            {
                triangleIndex = (edgeAB == -1) ? ((edgeBC == -1) ? this.m_mesh.ExtrudeTriangleFromEdge(ref B, edgeCA, userData, out edgeAB, out edgeBC) : this.m_mesh.ExtrudeTriangleFromEdge(ref A, edgeBC, userData, out edgeCA, out edgeAB)) : this.m_mesh.ExtrudeTriangleFromEdge(ref C, edgeAB, userData, out edgeBC, out edgeCA);
            }
            else if (num == 1)
            {
                triangleIndex = (edgeAB != -1) ? ((edgeBC != -1) ? this.GetTriangleOneNewEdge(ref edgeCA, ref edgeAB, ref edgeBC, userData) : this.GetTriangleOneNewEdge(ref edgeBC, ref edgeCA, ref edgeAB, userData)) : this.GetTriangleOneNewEdge(ref edgeAB, ref edgeBC, ref edgeCA, userData);
            }
            else
            {
                MyWingedEdgeMesh.Edge other = this.m_mesh.GetEdge(edgeAB);
                MyWingedEdgeMesh.Edge edge  = this.m_mesh.GetEdge(edgeBC);
                MyWingedEdgeMesh.Edge edge3 = this.m_mesh.GetEdge(edgeCA);
                int sharedB = edge3.TryGetSharedVertex(ref other);
                int sharedC = other.TryGetSharedVertex(ref edge);
                int num5    = edge.TryGetSharedVertex(ref edge3);
                int num6    = ((0 + ((sharedB == -1) ? 0 : 1)) + ((sharedC == -1) ? 0 : 1)) + ((num5 == -1) ? 0 : 1);
                if (num6 == 3)
                {
                    triangleIndex = this.m_mesh.MakeFace(userData, edgeAB);
                }
                else if (num6 == 2)
                {
                    triangleIndex = (sharedB != -1) ? ((sharedC != -1) ? this.GetTriangleTwoSharedVertices(edgeCA, edgeAB, ref edgeBC, sharedB, sharedC, userData) : this.GetTriangleTwoSharedVertices(edgeBC, edgeCA, ref edgeAB, num5, sharedB, userData)) : this.GetTriangleTwoSharedVertices(edgeAB, edgeBC, ref edgeCA, sharedC, num5, userData);
                }
                else if (num6 == 1)
                {
                    triangleIndex = (sharedB == -1) ? ((sharedC == -1) ? this.GetTriangleOneSharedVertex(edgeBC, edgeCA, ref edgeAB, num5, userData) : this.GetTriangleOneSharedVertex(edgeAB, edgeBC, ref edgeCA, sharedC, userData)) : this.GetTriangleOneSharedVertex(edgeCA, edgeAB, ref edgeBC, sharedB, userData);
                }
                else
                {
                    int num7;
                    int num8;
                    triangleIndex = this.m_mesh.ExtrudeTriangleFromEdge(ref C, edgeAB, userData, out num7, out num8);
                    this.m_mesh.MergeEdges(num8, edgeCA);
                    this.m_mesh.MergeEdges(num7, edgeBC);
                }
            }
            userData.Init(this, triangleIndex);
            return(userData);
        }
        // Adds a triangle and connects it to the other triangles in the mesh by the given edges.
        // Because connecting by vertices would produce non-manifold meshes, we connect triangles by their edges.
        // When a triangle is added that connects to another triangle only by a vertex, the two touching vertices will be regarded
        // as two separate vertices and they will only be merged when another triangle is added that shares two edges with the two
        // original triangles.
        //
        // When the method returns, edgeAB, edgeBC and edgeCA will contain the indices for the new edges.
        // The positions of vertices A, B and C will be unmodified and might not correspond to the real positions of the resulting
        // triangle's vertices (because the positions of the original edge vertices will sometimes be used due to edge merging).
        //
        // Note: The triangle's vertices and edges must be ordered clockwise:
        //                 B
        //                / \
        //               /   \
        //             AB     BC
        //             /       \
        //            /         \
        //           A -- CA --- C
        protected MyNavigationTriangle AddTriangle(ref Vector3 A, ref Vector3 B, ref Vector3 C, ref int edgeAB, ref int edgeBC, ref int edgeCA)
        {
            MyNavigationTriangle newTri = m_triPool.Allocate();

            // There are several cases that need to be handled and they can be distinguished by the number of
            // existing edges and their shared vertices.

            int newEdgeCount = 0;

            newEdgeCount += edgeAB == -1 ? 1 : 0;
            newEdgeCount += edgeBC == -1 ? 1 : 0;
            newEdgeCount += edgeCA == -1 ? 1 : 0;

            int newTriIndex = -1;

            if (newEdgeCount == 3)
            {
                newTriIndex = m_mesh.MakeNewTriangle(newTri, ref A, ref B, ref C, out edgeAB, out edgeBC, out edgeCA);
            }
            else if (newEdgeCount == 2)
            {
                if (edgeAB != -1)
                {
                    newTriIndex = m_mesh.ExtrudeTriangleFromEdge(ref C, edgeAB, newTri, out edgeBC, out edgeCA);
                }
                else if (edgeBC != -1)
                {
                    newTriIndex = m_mesh.ExtrudeTriangleFromEdge(ref A, edgeBC, newTri, out edgeCA, out edgeAB);
                }
                else
                {
                    newTriIndex = m_mesh.ExtrudeTriangleFromEdge(ref B, edgeCA, newTri, out edgeAB, out edgeBC);
                }
            }
            else if (newEdgeCount == 1)
            {
                if (edgeAB == -1)
                {
                    newTriIndex = GetTriangleOneNewEdge(ref edgeAB, ref edgeBC, ref edgeCA, newTri);
                }
                else if (edgeBC == -1)
                {
                    newTriIndex = GetTriangleOneNewEdge(ref edgeBC, ref edgeCA, ref edgeAB, newTri);
                }
                else
                {
                    newTriIndex = GetTriangleOneNewEdge(ref edgeCA, ref edgeAB, ref edgeBC, newTri);
                }
            }
            else
            {
                var entryAB = m_mesh.GetEdge(edgeAB);
                var entryBC = m_mesh.GetEdge(edgeBC);
                var entryCA = m_mesh.GetEdge(edgeCA);
                int sharedA = entryCA.TryGetSharedVertex(ref entryAB);
                int sharedB = entryAB.TryGetSharedVertex(ref entryBC);
                int sharedC = entryBC.TryGetSharedVertex(ref entryCA);

                int sharedVertCount = 0;
                sharedVertCount += sharedA == -1 ? 0 : 1;
                sharedVertCount += sharedB == -1 ? 0 : 1;
                sharedVertCount += sharedC == -1 ? 0 : 1;

                if (sharedVertCount == 3)
                {
                    newTriIndex = m_mesh.MakeFace(newTri, edgeAB);
                }
                else if (sharedVertCount == 2)
                {
                    if (sharedA == -1)
                    {
                        newTriIndex = GetTriangleTwoSharedVertices(edgeAB, edgeBC, ref edgeCA, sharedB, sharedC, newTri);
                    }
                    else if (sharedB == -1)
                    {
                        newTriIndex = GetTriangleTwoSharedVertices(edgeBC, edgeCA, ref edgeAB, sharedC, sharedA, newTri);
                    }
                    else
                    {
                        newTriIndex = GetTriangleTwoSharedVertices(edgeCA, edgeAB, ref edgeBC, sharedA, sharedB, newTri);
                    }
                }
                else if (sharedVertCount == 1)
                {
                    if (sharedA != -1)
                    {
                        newTriIndex = GetTriangleOneSharedVertex(edgeCA, edgeAB, ref edgeBC, sharedA, newTri);
                    }
                    else if (sharedB != -1)
                    {
                        newTriIndex = GetTriangleOneSharedVertex(edgeAB, edgeBC, ref edgeCA, sharedB, newTri);
                    }
                    else
                    {
                        newTriIndex = GetTriangleOneSharedVertex(edgeBC, edgeCA, ref edgeAB, sharedC, newTri);
                    }
                }
                else
                {
                    int next, prev;
                    newTriIndex = m_mesh.ExtrudeTriangleFromEdge(ref C, edgeAB, newTri, out next, out prev);
                    m_mesh.MergeEdges(prev, edgeCA);
                    m_mesh.MergeEdges(next, edgeBC);
                }
            }

            newTri.Init(this, newTriIndex);
            return(newTri);
        }