// Taking bindposes optional parameter is ugly, but saves an additional array allocation if it was already // accessed to get the length public static void ApplyToWorkingMesh(this Mesh mesh, ref WorkingMesh wm, Matrix4x4[] bindposes = null) { wm.indexFormat = mesh.indexFormat; wm.vertices = mesh.vertices; wm.normals = mesh.normals; wm.tangents = mesh.tangents; wm.uv = mesh.uv; wm.uv2 = mesh.uv2; wm.uv3 = mesh.uv3; wm.uv4 = mesh.uv4; wm.colors = mesh.colors; wm.boneWeights = mesh.boneWeights; wm.bindposes = bindposes ?? mesh.bindposes; wm.subMeshCount = mesh.subMeshCount; for (int i = 0; i < mesh.subMeshCount; i++) { wm.SetTriangles(mesh.GetTriangles(i), i); } wm.name = mesh.name; wm.bounds = mesh.bounds; }
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; }
public static void ApplyToWorkingMesh(this Mesh mesh, WorkingMesh wm) { #if UNITY_2017_3_OR_NEWER wm.indexFormat = mesh.indexFormat; #endif wm.vertices = mesh.vertices; wm.normals = mesh.normals; wm.tangents = mesh.tangents; wm.uv = mesh.uv; wm.uv2 = mesh.uv2; wm.uv3 = mesh.uv3; wm.uv4 = mesh.uv4; wm.colors = mesh.colors; wm.boneWeights = mesh.boneWeights; wm.bindposes = mesh.bindposes; wm.subMeshCount = mesh.subMeshCount; for (int i = 0; i < mesh.subMeshCount; i++) { wm.SetTriangles(mesh.GetTriangles(i), i); } wm.name = mesh.name; wm.bounds = mesh.bounds; }
static void FromMeshDecimatorMesh(DMesh mesh, bool recalculateNormals, ref WMesh destMesh) { if (recalculateNormals) { // If we recalculate the normals, we also recalculate the tangents mesh.RecalculateNormals(); mesh.RecalculateTangents(); } int subMeshCount = mesh.SubMeshCount; var newNormals = FromVector3(mesh.Normals); var newTangents = FromVector4(mesh.Tangents); var newUV1 = FromVector2(mesh.UV1); var newUV2 = FromVector2(mesh.UV2); var newUV3 = FromVector2(mesh.UV3); var newUV4 = FromVector2(mesh.UV4); var newColors = FromColor(mesh.Colors); //var newBoneWeights = FromSimplifyBoneWeights(mesh.BoneWeights); //if (bindposes != null) newMesh.bindposes = bindposes; destMesh.subMeshCount = subMeshCount; destMesh.vertices = FromVector3d(mesh.Vertices); if (newNormals != null) { destMesh.normals = newNormals; } if (newTangents != null) { destMesh.tangents = newTangents; } if (newUV1 != null) { destMesh.uv = newUV1; } if (newUV2 != null) { destMesh.uv2 = newUV2; } if (newUV3 != null) { destMesh.uv3 = newUV3; } if (newUV4 != null) { destMesh.uv4 = newUV4; } if (newColors != null) { destMesh.colors = newColors; } //if (newBoneWeights != null) // newMesh.boneWeights = newBoneWeights; for (int i = 0; i < subMeshCount; i++) { var subMeshIndices = mesh.GetIndices(i); destMesh.SetTriangles(subMeshIndices, i); } destMesh.RecalculateBounds(); }