Пример #1
0
        private static void CreateTrianglesFromOneVertices(CutMesh mesh, int v1Index, int v2Index, int v3Index, MeshTriangle triangle, Plane plane, int subMesh)
        {
            var v1In  = triangle.Vertices[v1Index];
            var v2Out = triangle.Vertices[v2Index];
            var v3Out = triangle.Vertices[v3Index];

            var v1ToV2Point = MeshVertex.InterpolateVertexOnPlane(v1In, v2Out, plane);
            var v1ToV3Point = MeshVertex.InterpolateVertexOnPlane(v1In, v3Out, plane);

            // create triangle

            mesh.AddTriangle(v1In, v1ToV2Point, v1ToV3Point, subMesh);
        }
Пример #2
0
        /// <summary>
        /// Perform a cut on a specific mesh, using the under-laying properties.
        /// </summary>
        /// <param name="vertices"></param>
        /// <param name="normals"></param>
        /// <param name="tangents"></param>
        /// <param name="uv"></param>
        /// <param name="subMeshes"></param>
        /// <param name="cutPlane"></param>
        /// <returns></returns>
        public static CutMesh[] PerformCut(List <Vector3> vertices, List <Vector3> normals, List <Vector4> tangents, List <Vector2> uv,
                                           List <int>[] subMeshes, Plane cutPlane)
        {
            var subMeshesCount = subMeshes.Length;

            var leftMesh  = new CutMesh(subMeshesCount);
            var rightMesh = new CutMesh(subMeshesCount);

            // iterate triangles
            for (var subMesh = 0; subMesh < subMeshesCount; subMesh++)
            {
                var meshIndices = subMeshes[subMesh];

                for (var indicesIndex = 0; indicesIndex < meshIndices.Count - 2; indicesIndex += 3)
                {
                    var v1Index = meshIndices[indicesIndex];
                    var v2Index = meshIndices[indicesIndex + 1];
                    var v3Index = meshIndices[indicesIndex + 2];

                    var triangle = new MeshTriangle(v1Index, v2Index, v3Index, vertices, normals, tangents, uv);

                    var v1Side = cutPlane.GetSide(triangle.Vertices[0].Point);
                    var v2Side = cutPlane.GetSide(triangle.Vertices[1].Point);
                    var v3Side = cutPlane.GetSide(triangle.Vertices[2].Point);

                    // add to proper sides
                    if (v1Side && v2Side && v3Side)
                    {
                        rightMesh.AddTriangle(triangle, subMesh);
                        continue;
                    }

                    if (!v1Side && !v2Side && !v3Side)
                    {
                        leftMesh.AddTriangle(triangle, subMesh);
                        continue;
                    }

                    if (v1Side == v2Side)
                    {
                        if (v1Side)
                        {
                            CreateTrianglesFromTwoVertices(rightMesh, 0, 1, 2, triangle, cutPlane, subMesh);
                            CreateTrianglesFromOneVertices(leftMesh, 2, 0, 1, triangle, cutPlane, subMesh);

                            continue;
                        }

                        CreateTrianglesFromTwoVertices(leftMesh, 0, 1, 2, triangle, cutPlane, subMesh);
                        CreateTrianglesFromOneVertices(rightMesh, 2, 0, 1, triangle, cutPlane, subMesh);

                        continue;
                    }

                    if (v2Side == v3Side)
                    {
                        if (v2Side)
                        {
                            CreateTrianglesFromTwoVertices(rightMesh, 1, 2, 0, triangle, cutPlane, subMesh);
                            CreateTrianglesFromOneVertices(leftMesh, 0, 1, 2, triangle, cutPlane, subMesh);

                            continue;
                        }

                        CreateTrianglesFromTwoVertices(leftMesh, 1, 2, 0, triangle, cutPlane, subMesh);
                        CreateTrianglesFromOneVertices(rightMesh, 0, 1, 2, triangle, cutPlane, subMesh);

                        continue;
                    }

                    // last possibility, v3Side == v1Side

                    if (v1Side)
                    {
                        CreateTrianglesFromTwoVertices(rightMesh, 2, 0, 1, triangle, cutPlane, subMesh);
                        CreateTrianglesFromOneVertices(leftMesh, 1, 2, 0, triangle, cutPlane, subMesh);

                        continue;
                    }

                    CreateTrianglesFromTwoVertices(leftMesh, 2, 0, 1, triangle, cutPlane, subMesh);
                    CreateTrianglesFromOneVertices(rightMesh, 1, 2, 0, triangle, cutPlane, subMesh);
                }
            }

            return(new[] { leftMesh, rightMesh });
        }