Beispiel #1
0
        IEnumerator Simplify(Mesh inputMesh, Mesh outputMesh, float quality)
        {
            var meshSimplifier = new MeshSimplifier();

            meshSimplifier.Vertices = inputMesh.vertices;
            meshSimplifier.Normals  = inputMesh.normals;
            meshSimplifier.Tangents = inputMesh.tangents;
            meshSimplifier.UV1      = inputMesh.uv;
            meshSimplifier.UV2      = inputMesh.uv2;
            meshSimplifier.UV3      = inputMesh.uv3;
            meshSimplifier.UV4      = inputMesh.uv4;
            meshSimplifier.Colors   = inputMesh.colors;

            var triangles = new int[inputMesh.subMeshCount][];

            for (var submesh = 0; submesh < inputMesh.subMeshCount; submesh++)
            {
                triangles[submesh] = inputMesh.GetTriangles(submesh);
            }

            meshSimplifier.AddSubMeshTriangles(triangles);

            meshSimplifier.SimplifyMesh(quality);

            outputMesh.vertices     = meshSimplifier.Vertices;
            outputMesh.normals      = meshSimplifier.Normals;
            outputMesh.tangents     = meshSimplifier.Tangents;
            outputMesh.uv           = meshSimplifier.UV1;
            outputMesh.uv2          = meshSimplifier.UV2;
            outputMesh.uv3          = meshSimplifier.UV3;
            outputMesh.uv4          = meshSimplifier.UV4;
            outputMesh.colors       = meshSimplifier.Colors;
            outputMesh.subMeshCount = meshSimplifier.SubMeshCount;
            for (var submesh = 0; submesh < outputMesh.subMeshCount; submesh++)
            {
                outputMesh.SetTriangles(meshSimplifier.GetSubMeshTriangles(submesh), submesh);
            }

            yield break;
        }
        DMesh ToMeshDecimatorMesh(WMesh mesh, out int totalTriangleCount)
        {
            var vertices = ToVector3d(mesh.vertices);

            int subMeshCount = mesh.subMeshCount;
            var meshNormals  = mesh.normals;
            var meshTangents = mesh.tangents;
            var meshUV1      = mesh.uv;
            var meshUV2      = mesh.uv2;
            var meshUV3      = mesh.uv3;
            var meshUV4      = mesh.uv4;
            var meshColors   = mesh.colors;

            //var meshBoneWeights = mesh.boneWeights;
            //var meshBindposes = mesh.bindposes;

            totalTriangleCount = 0;
            var meshIndices = new int[subMeshCount][];

            for (int i = 0; i < subMeshCount; i++)
            {
                meshIndices[i]      = mesh.GetTriangles(i);
                totalTriangleCount += meshIndices[i].Length / 3;
            }

            var dmesh = new DMesh(vertices, meshIndices);

            if (meshNormals != null && meshNormals.Length > 0)
            {
                dmesh.Normals = ToVector3(meshNormals);
            }

            if (meshTangents != null && meshTangents.Length > 0)
            {
                dmesh.Tangents = ToVector4(meshTangents);
            }

            if (meshUV1 != null && meshUV1.Length > 0)
            {
                dmesh.UV1 = ToVector2(meshUV1);
            }

            if (meshUV2 != null && meshUV2.Length > 0)
            {
                dmesh.UV2 = ToVector2(meshUV2);
            }

            if (meshUV3 != null && meshUV3.Length > 0)
            {
                dmesh.UV3 = ToVector2(meshUV3);
            }

            if (meshUV4 != null && meshUV4.Length > 0)
            {
                dmesh.UV4 = ToVector2(meshUV4);
            }

            if (meshColors != null && meshColors.Length > 0)
            {
                dmesh.Colors = ToVector4(meshColors);
            }

            //if (meshBoneWeights != null && meshBoneWeights.Length > 0)
            //    dmesh.BoneWeights = ToSimplifyBoneWeights(meshBoneWeights);

            return(dmesh);
        }