Talk:MeshCreationHelper
The following code block may not yield the results you want. I tried this code with a mesh that has two sub-meshes, using two different materials, and my UVs got jacked up in one of my newly formed meshes.
<csharp> // Collect the new triangles indecies for (int i = 0; i < newTriangles.Length; i++) { Vector3 vec = oldMesh.vertices[triangles[i]];
int index = newVertices.IndexOf(vec); // BUG newTriangles[i] = index; } </csharp>
int index = newVertices.IndexOf(vec)
This line will probably give you the index to the FIRST (?) occurrence of vec, and not necessarily the Vector3 pointed to by triangles[i]. What we want to do is remap the indices stored in the old triangles list (triangles) to our newly created index list (newTriangles).
To achieve this, I did the following:
<csharp> Dictionary< int, int > oldToNewIndices = new Dictionary< int, int >(); int newIndex = 0;
// Collect the vertices and uvs for (int i = 0; i < oldMesh.vertices.Length; i++) { if (triangles.Contains(i)) { newVertices.Add(oldMesh.vertices[i]); newUvs.Add(oldMesh.uv[i]); oldToNewIndices.Add( i, newIndex ); ++newIndex; } }
int[] newTriangles = new int[triangles.Count];
// Collect the new triangles indecies for (int i = 0; i < newTriangles.Length; i++) { newTriangles[ i ] = oldToNewIndices[ triangles[ i ] ]; } </csharp>
This change yielded the results I expected.
Thanks
Thanks Mark, you're right. I'll make the changes as you suggested.