- Home /
Question by
qwee · Jun 21, 2013 at 08:57 AM ·
meshmaterialsfacesassignmentsubdivision
Split mesh faces C#
How can i split mesh faces without losing faces materials assignments. The code i used is here (the other variables you can't see are declared in the main code), it works but it loses materials assignments and gets the final mesh with only a material:
void defragMesh()
{
myMesh = new Mesh();
myMesh.vertices = startMesh.vertices;
myMesh.triangles = startMesh.triangles;
myMesh.uv = startMesh.uv;
myMesh.normals = startMesh.normals;
newVertices = new Vector3[myMesh.triangles.Length];
int[] newTris = new int[myMesh.triangles.Length];
Vector2[] newUV = new Vector2[myMesh.triangles.Length];
Vector3[] newNormals = new Vector3[myMesh.triangles.Length];
for(int i = 0; i < myMesh.triangles.Length; i++)
{
newVertices[i] = myMesh.vertices[myMesh.triangles[i]];
newUV[i] = myMesh.uv[myMesh.triangles[i]];
newNormals[i] = myMesh.normals[myMesh.triangles[i]];
newTris[i] = i;
}
myMesh.vertices = newVertices;
myMesh.triangles = newTris;
myMesh.uv = newUV;
myMesh.normals = newNormals;
normals = new Vector3[myMesh.triangles.Length / 3];
int index = 0;
for(int i = 0; i < normals.Length; i++)
{
Vector3 v = newVertices[index++];
normals[i] = Vector3.Cross(newVertices[index++] - v, newVertices[index++] - v).normalized;
}
}
Comment
Does this divide the triangles of the mesh in two? Did you manage to achieve the desired effect without loosing the material assignments?
Answer by whydoidoit · Jun 21, 2013 at 09:31 AM
If you use multiple materials you will need to use SetTriangles and GetTriangles to set the triangles for each of the submeshes. Using triangles directly removes the sub meshes.
And there is a script that allow me to get the vertices in a sub-mesh?