Hi,
I need to use CombineMeshes() but there is something unclear about how it works.
My situation is this: I need to combine multiple meshes of different materials into one mesh, with (I’m assuming) one submesh per unique material. Sometimes there may be multiple instances of the same mesh using a different material, or multiple instances of the same mesh using the same material but for different submeshes.
The confusion is, the Mesh object doesn’t contain information about which materials are being used for a particular instance, and CombineMesh doesn’t request that information from a MeshRenderer object, which is the only thing that DOES contain that information. Two different meshes may have multiple submeshes, say 2 submeshes, but mesh1’s submesh1’s material might not be the same as mesh2’s submesh1’s material. How would it know to put them into different submeshes?
I read that you can specify that through CombineInstance.subMeshIndex:
ArrayList materials = new ArrayList();
ArrayList combineInstances = new ArrayList();
MeshFilter[] meshFilters = <Object>.GetComponentsInChildren<MeshFilter>();
foreach( MeshFilter meshFilter in meshFilters )
{
MeshRenderer meshRenderer = meshFilter.GetComponent<MeshRenderer>();
for(int s = 0; s < meshFilter.sharedMesh.subMeshCount; s++)
{
// !! Assumes that submesh index will always correspond to material index !!
int materialArrayIndex;
for(materialArrayIndex = 0; materialArrayIndex < materials.Count; materialArrayIndex++)
{
if(materials[materialArrayIndex] == meshRenderer.materials~~)~~
break;
}
if(materialArrayIndex == materials.Count)
materials.Add(meshRenderer.materials~~);
CombineInstance combineInstance;
combineInstance.transform = meshRenderer.transform;
// ---------------------------------------------------------------
combineInstance.subMeshIndex = materialArrayIndex;
combineInstance.mesh = meshFilter.sharedMesh ??? .submesh ???
// ---------------------------------------------------------------
combineInstances.Add( combineInstance );
}
}~~
But as you can see, meshFilter.sharedMesh doesn’t give you access to the individual submeshes. So how is it supposed to know how to separate them?
Thanks for helping.