예제 #1
0
    private void AssignVertices(LegacyHull a, LegacyHull b, bool[] pointAbovePlane, out int[] oldToNewVertex)
    {
        oldToNewVertex = new int[vertices.Count];

        for (int i = 0; i < vertices.Count; i++)
        {
            Point correspondingPoint = vertexToPointMap[i];

            if (pointAbovePlane[correspondingPoint.index])
            {
                // Assign this vertex to hull A
                a.AddVertex(vertices[i], normals[i], tangents[i], uvs[i], correspondingPoint, out oldToNewVertex[i]);
            }
            else
            {
                // Assign this vertex to hull B
                b.AddVertex(vertices[i], normals[i], tangents[i], uvs[i], correspondingPoint, out oldToNewVertex[i]);
            }
        }
    }
예제 #2
0
    private void FillCutEdges(LegacyHull a, LegacyHull b, IList <Edge> edgesA, IList <Edge> edgesB, Vector3 planeNormal, UvMapper uvMapper)
    {
        // Create outline data
        int outlineEdgeCount = edgesA.Count;

        Vector3[] outlinePoints = new Vector3[outlineEdgeCount];
        int[]     outlineEdges  = new int[outlineEdgeCount * 2];

        int startIndex = 0;

        for (int i = 0; i < outlineEdgeCount; i++)
        {
            int currentIndex = i;
            int nextIndex    = (i + 1) % outlineEdgeCount;

            Edge current = edgesA[currentIndex];
            Edge next    = edgesA[nextIndex];

            // Set point
            outlinePoints[i] = current.point0.position;

            // Set edge
            outlineEdges[i * 2 + 0] = currentIndex;

            if (current.point1 == next.point0)
            {
                outlineEdges[i * 2 + 1] = nextIndex;
            }
            else
            {
                outlineEdges[i * 2 + 1] = startIndex;

                startIndex = nextIndex;
            }
        }

        // Triangulate
        int[] newEdges, newTriangles, newTriangleEdges;

        ITriangulator triangulator = new Triangulator(outlinePoints, outlineEdges, planeNormal);

        triangulator.Fill(out newEdges, out newTriangles, out newTriangleEdges);

        // Calculate vertex properties
        Vector3 normalA = -planeNormal;
        Vector3 normalB = planeNormal;

        Vector4[] tangentsA, tangentsB;
        Vector2[] uvsA, uvsB;

        uvMapper.Map(outlinePoints, planeNormal, out tangentsA, out tangentsB, out uvsA, out uvsB);

        // Create new vertices
        int[] verticesA = new int[outlineEdgeCount];
        int[] verticesB = new int[outlineEdgeCount];

        for (int i = 0; i < outlineEdgeCount; i++)
        {
            a.AddVertex(outlinePoints[i], normalA, tangentsA[i], uvsA[i], edgesA[i].point0, out verticesA[i]);

            b.AddVertex(outlinePoints[i], normalB, tangentsB[i], uvsB[i], edgesB[i].point0, out verticesB[i]);
        }

        // Create new edges
        for (int i = 0; i < newEdges.Length / 2; i++)
        {
            int point0 = newEdges[i * 2 + 0];
            int point1 = newEdges[i * 2 + 1];

            Edge edgeA = new Edge(edgesA[point0].point0, edgesA[point1].point0);
            Edge edgeB = new Edge(edgesB[point0].point0, edgesB[point1].point0);

            edgesA.Add(edgeA);
            edgesB.Add(edgeB);

            a.edges.Add(edgeA);
            b.edges.Add(edgeB);
        }

        // Create new triangles
        for (int i = 0; i < newTriangles.Length / 3; i++)
        {
            int point0 = newTriangles[i * 3 + 0];
            int point1 = newTriangles[i * 3 + 1];
            int point2 = newTriangles[i * 3 + 2];

            int edge0 = newTriangleEdges[i * 3 + 0];
            int edge1 = newTriangleEdges[i * 3 + 1];
            int edge2 = newTriangleEdges[i * 3 + 2];

            Triangle triangleA = new Triangle(verticesA[point0], verticesA[point2], verticesA[point1], edgesA[point0].point0, edgesA[point2].point0, edgesA[point1].point0, edgesA[edge2], edgesA[edge1], edgesA[edge0]);
            Triangle triangleB = new Triangle(verticesB[point0], verticesB[point1], verticesB[point2], edgesB[point0].point0, edgesB[point1].point0, edgesB[point2].point0, edgesB[edge0], edgesB[edge1], edgesB[edge2]);

            a.triangles.Add(triangleA);
            b.triangles.Add(triangleB);
        }
    }
예제 #3
0
    private void SplitTriangle(LegacyHull topHull, LegacyHull bottomHull, Edge topEdge0, Edge topEdge1, Edge topCutEdge, Edge bottomEdge0, Edge bottomEdge1, Edge bottomCutEdge, Edge bottomEdge2, int vertex0, int vertex1, int vertex2, float scalar0, float scalar1, int[] oldToNewVertex)
    {
        Vector3 n0 = normals[vertex0];
        Vector3 n1 = normals[vertex1];
        Vector3 n2 = normals[vertex2];

        Vector4 t0 = tangents[vertex0];
        Vector4 t1 = tangents[vertex1];
        Vector4 t2 = tangents[vertex2];

        Vector2 uv0 = uvs[vertex0];
        Vector2 uv1 = uvs[vertex1];
        Vector2 uv2 = uvs[vertex2];

        // Calculate the cut vertex data by interpolating original triangle values
        Vector3 cutNormal0 = new Vector3();

        cutNormal0.x = n0.x + (n1.x - n0.x) * scalar0;
        cutNormal0.y = n0.y + (n1.y - n0.y) * scalar0;
        cutNormal0.z = n0.z + (n1.z - n0.z) * scalar0;

        cutNormal0.Normalize();

        Vector3 cutNormal1 = new Vector3();

        cutNormal1.x = n1.x + (n2.x - n1.x) * scalar1;
        cutNormal1.y = n1.y + (n2.y - n1.y) * scalar1;
        cutNormal1.z = n1.z + (n2.z - n1.z) * scalar1;

        cutNormal1.Normalize();

        Vector4 cutTangent0 = new Vector4();

        cutTangent0.x = t0.x + (t1.x - t0.x) * scalar0;
        cutTangent0.y = t0.y + (t1.y - t0.y) * scalar0;
        cutTangent0.z = t0.z + (t1.z - t0.z) * scalar0;

        cutTangent0.Normalize();
        cutTangent0.w = t0.w;

        Vector4 cutTangent1 = new Vector4();

        cutTangent1.x = t1.x + (t2.x - t1.x) * scalar1;
        cutTangent1.y = t1.y + (t2.y - t1.y) * scalar1;
        cutTangent1.z = t1.z + (t2.z - t1.z) * scalar1;

        cutTangent1.Normalize();
        cutTangent1.w = t1.w;

        Vector2 cutUv0 = new Vector2();

        cutUv0.x = uv0.x + (uv1.x - uv0.x) * scalar0;
        cutUv0.y = uv0.y + (uv1.y - uv0.y) * scalar0;

        Vector2 cutUv1 = new Vector2();

        cutUv1.x = uv1.x + (uv2.x - uv1.x) * scalar1;
        cutUv1.y = uv1.y + (uv2.y - uv1.y) * scalar1;

        // Add the cut vertices to the hulls
        int topCutVertex0, topCutVertex1;

        topHull.AddVertex(topEdge0.point0.position, cutNormal0, cutTangent0, cutUv0, topEdge0.point0, out topCutVertex0);

        topHull.AddVertex(topEdge1.point0.position, cutNormal1, cutTangent1, cutUv1, topEdge1.point0, out topCutVertex1);

        int bottomCutVertex0, bottomCutVertex1;

        bottomHull.AddVertex(bottomEdge0.point0.position, cutNormal0, cutTangent0, cutUv0, bottomEdge0.point0, out bottomCutVertex0);

        bottomHull.AddVertex(bottomEdge1.point0.position, cutNormal1, cutTangent1, cutUv1, bottomEdge1.point0, out bottomCutVertex1);

        // Create the top of the original triangle
        Triangle topTriangle = new Triangle(topCutVertex0, oldToNewVertex[vertex1], topCutVertex1, topEdge0.point0, topEdge0.point1, topEdge1.point0, topEdge0, topEdge1, topCutEdge);

        topHull.triangles.Add(topTriangle);

        // Create the bottom of the original triangle
        Edge bottomCrossEdge = new Edge(bottomEdge0.point1, bottomEdge1.point0);

        Triangle bottomTriangle0 = new Triangle(oldToNewVertex[vertex0], bottomCutVertex0, bottomCutVertex1, bottomEdge0.point1, bottomEdge0.point0, bottomEdge1.point0, bottomEdge0, bottomCutEdge, bottomCrossEdge);
        Triangle bottomTriangle1 = new Triangle(oldToNewVertex[vertex0], bottomCutVertex1, oldToNewVertex[vertex2], bottomEdge0.point1, bottomEdge1.point0, bottomEdge1.point1, bottomCrossEdge, bottomEdge1, bottomEdge2);

        bottomHull.edges.Add(bottomCrossEdge);
        bottomHull.triangles.Add(bottomTriangle0);
        bottomHull.triangles.Add(bottomTriangle1);
    }