public static IEnumerator MakeLODMeshInBackground(this Mesh mesh, float maxWeight, bool recalcNormals, float removeSmallParts, System.Action <Mesh> result) { Hashtable lodInfo = new Hashtable(); lodInfo["maxWeight"] = maxWeight; lodInfo["removeSmallParts"] = removeSmallParts; Vector3[] vs = mesh.vertices; if (vs.Length <= 0) { throw new ApplicationException("Mesh was empty"); } Vector3[] ns = mesh.normals; if (ns.Length == 0) // mesh has no normals { mesh.RecalculateNormals(); ns = mesh.normals; } Vector2[] uv1s = mesh.uv; Vector2[] uv2s = mesh.uv2; #if UNITY_4_3 Vector2[] uv3s = new Vector2[0]; Vector2[] uv4s = new Vector2[0]; #elif UNITY_4_4 Vector2[] uv3s = new Vector2[0]; Vector2[] uv4s = new Vector2[0]; #elif UNITY_4_5 Vector2[] uv3s = new Vector2[0]; Vector2[] uv4s = new Vector2[0]; #elif UNITY_4_6 Vector2[] uv3s = new Vector2[0]; Vector2[] uv4s = new Vector2[0]; #else Vector2[] uv3s = mesh.uv3; Vector2[] uv4s = mesh.uv4; #endif Color32[] colors32 = mesh.colors32; int[] ts = mesh.triangles; Matrix4x4[] bindposes = mesh.bindposes; BoneWeight[] bws = mesh.boneWeights; int[] subMeshOffsets = new int[mesh.subMeshCount]; if (mesh.subMeshCount > 1) // read triangles of submeshes 1 by 1 because I dont know the internal order of the mesh { for (int s = 0; s < mesh.subMeshCount; s++) { int[] subTs = mesh.GetTriangles(s); int t = 0; for (; t < subTs.Length; t++) { ts[subMeshOffsets[s] + t] = subTs[t]; } if (s + 1 < mesh.subMeshCount) { subMeshOffsets[s + 1] = subMeshOffsets[s] + t; } } } Bounds meshBounds = mesh.bounds; lodInfo["vertices"] = vs; lodInfo["normals"] = ns; lodInfo["uv1s"] = uv1s; lodInfo["uv2s"] = uv2s; lodInfo["uv3s"] = uv3s; lodInfo["uv4s"] = uv4s; lodInfo["colors32"] = colors32; lodInfo["triangles"] = ts; lodInfo["bindposes"] = bindposes; lodInfo["boneWeights"] = bws; lodInfo["subMeshOffsets"] = subMeshOffsets; lodInfo["meshBounds"] = meshBounds; Thread thread = new Thread(LODMaker.MakeLODMeshInBackground); thread.Start(lodInfo); while (!lodInfo.ContainsKey("ready")) { yield return(new WaitForSeconds(0.2f)); } result(LODMaker.CreateNewMesh((Vector3[])lodInfo["vertices"], (Vector3[])lodInfo["normals"], (Vector2[])lodInfo["uv1s"], (Vector2[])lodInfo["uv2s"], (Vector2[])lodInfo["uv3s"], (Vector2[])lodInfo["uv4s"], (Color32[])lodInfo["colors32"], (int[])lodInfo["triangles"], (BoneWeight[])lodInfo["boneWeights"], (Matrix4x4[])lodInfo["bindposes"], (int[])lodInfo["subMeshOffsets"], recalcNormals)); }
public static IEnumerator MakeLODMeshInBackground(this Mesh mesh, float maxWeight, bool recalcNormals, float removeSmallParts, Action <Mesh> result) { Hashtable lodInfo = new Hashtable(); lodInfo["maxWeight"] = maxWeight; lodInfo["removeSmallParts"] = removeSmallParts; Vector3[] vs = mesh.vertices; if (vs.Length <= 0) { throw new ApplicationException("Mesh was empty"); } Vector3[] ns = mesh.normals; if (ns.Length == 0) { mesh.RecalculateNormals(); ns = mesh.normals; } Vector2[] uv1s = mesh.uv; Vector2[] uv2s = mesh.uv2; Vector2[] uv3s = mesh.uv3; Vector2[] uv4s = mesh.uv4; Color32[] colors32 = mesh.colors32; int[] ts = mesh.triangles; Matrix4x4[] bindposes = mesh.bindposes; BoneWeight[] bws = mesh.boneWeights; int[] subMeshOffsets = new int[mesh.subMeshCount]; if (mesh.subMeshCount > 1) { for (int i = 0; i < mesh.subMeshCount; i++) { int[] triangles = mesh.GetTriangles(i); int j; for (j = 0; j < triangles.Length; j++) { ts[subMeshOffsets[i] + j] = triangles[j]; } if (i + 1 < mesh.subMeshCount) { subMeshOffsets[i + 1] = subMeshOffsets[i] + j; } } } Bounds meshBounds = mesh.bounds; lodInfo["vertices"] = vs; lodInfo["normals"] = ns; lodInfo["uv1s"] = uv1s; lodInfo["uv2s"] = uv2s; lodInfo["uv3s"] = uv3s; lodInfo["uv4s"] = uv4s; lodInfo["colors32"] = colors32; lodInfo["triangles"] = ts; lodInfo["bindposes"] = bindposes; lodInfo["boneWeights"] = bws; lodInfo["subMeshOffsets"] = subMeshOffsets; lodInfo["meshBounds"] = meshBounds; if (MeshExtensions.< > f__mg$cache0 == null) { MeshExtensions.< > f__mg$cache0 = new ParameterizedThreadStart(LODMaker.MakeLODMeshInBackground); } Thread thread = new Thread(MeshExtensions.< > f__mg$cache0); thread.Start(lodInfo); while (!lodInfo.ContainsKey("ready")) { yield return(new WaitForSeconds(0.2f)); } result(LODMaker.CreateNewMesh((Vector3[])lodInfo["vertices"], (Vector3[])lodInfo["normals"], (Vector2[])lodInfo["uv1s"], (Vector2[])lodInfo["uv2s"], (Vector2[])lodInfo["uv3s"], (Vector2[])lodInfo["uv4s"], (Color32[])lodInfo["colors32"], (int[])lodInfo["triangles"], (BoneWeight[])lodInfo["boneWeights"], (Matrix4x4[])lodInfo["bindposes"], (int[])lodInfo["subMeshOffsets"], recalcNormals)); yield break; }