submeshcount vs material[]

hi all!
i’m trying to do some optimizations and I’ve got a situation that a gameobject have 2 materials and 27 submeshes:

Renderer renderer = toBake.GetComponent<Renderer>();
MeshFilter meshFilter = toBake.GetComponent<MeshFilter>();

int materialsCount = renderer.sharedMaterials.Length;
int submeshesCount = meshFilter.sharedMesh.subMeshCount;

// materialsCount = 2
// submeshesCount = 27

now, how can I know which material is referred for each submesh?

thanks!

Hi ghiboz,

The list of materials and list of submeshes are in the same order. Material 0 goes on submesh 0, Material 1 goes on submesh 1 etc…

In your case material 0 goes on submesh 0, material 1 goes on submesh 1 and submesh 2…26 are ignored, invisible. One way to see what triangles are in these submeshes is to create a bunch of solid color materials and add them to the list of materials.

You can also get the triangle indexes for the 13 submesh using:

int[ ] submeshTriangles = meshFilter.sharedMesh.GetIndexes(13);

Sometimes this is useful

1 Like

thanks @Phong ,
now the things are clear!!!

have a nice day!