public static Mesh Submesh(this Mesh mesh, int submeshIndex) { if (submeshIndex < 0 || submeshIndex >= mesh.subMeshCount) { return(null); } int[] indices = mesh.GetTriangles(submeshIndex); Vertices source = new Vertices(mesh); Vertices dest = new Vertices(); Dictionary <int, int> map = new Dictionary <int, int>(); int[] newIndices = new int[indices.Length]; for (int i = 0; i < indices.Length; i++) { int o = indices[i]; if (!map.TryGetValue(o, out int n)) { n = dest.Add(source, o); map.Add(o, n); } newIndices[i] = n; } Mesh submesh = new Mesh(); dest.AssignTo(submesh); submesh.triangles = newIndices; submesh.name = $"{mesh.NameFormatted()}_{submeshIndex}"; return(submesh); }
public static Mesh GetSubmesh(this Mesh aMesh, int aSubMeshIndex) { if (aSubMeshIndex < 0 || aSubMeshIndex >= aMesh.subMeshCount) { return(null); } int[] indices = aMesh.GetTriangles(aSubMeshIndex); Vertices source = new Vertices(aMesh); Vertices dest = new Vertices(); Dictionary <int, int> map = new Dictionary <int, int>(); int[] newIndices = new int[indices.Length]; for (int i = 0; i < indices.Length; i++) { int o = indices[i]; int n; if (!map.TryGetValue(o, out n)) { n = dest.Add(source, o); map.Add(o, n); } newIndices[i] = n; } Mesh m = new Mesh(); dest.AssignTo(m); m.triangles = newIndices; return(m); }