How does Face.MaterialId work in relation to SubMaterials

Oct 29, 2010 at 3:41 AM
Edited Oct 29, 2010 at 3:46 AM

Hi all,

I have a mesh with many faces. I'd like to show different textures on different faces in the same mesh.  I see that there is a materialId on the face, and that material has something called sub material.  Since a mesh can only have one material I think these components might be what I'm looking for.

Does anyone have an example of how Face.MaterialId works in relation to SubMaterials.  I did take a look at the box code and specificly SetMaterialOnSide.  I had thought that if I filled Material.SubMaterials with indexed materials then Face.MaterialId would be used to look up the sub material and thus find the material to actually use on the face.  This does not seem to work. Regardless of the materialID I set on the face only the material set on the mesh is displayed, never any of the sub materials (which have different textures)

But if that is how it works, then what material ID would I use to just use the base material and not a sub material stored in the material? 0?

Thanks for any pointers.

 Just for curiosities sake, why not just have a material reference on each face? That way each face could independently show its own material. Why bother with sub materials?

Oct 29, 2010 at 7:26 AM


the reason why SubMaterials was introduced is performance, in faces did in fact have a Material on each face. The performance I'm talking about is the new control in called InstancingNodes. If you want to change material during DataBinding of an element and subsequently change all submaterials - with it had to go through the entire Mesh and change all faces, with you can simply just change the Material on the node and all faces will be referencing the submaterials as before, one just needs to keep the same structure. This is helpful for InstancingNodes and typically scenarios were you change state of an object within the collection and want to reflect the change by changing the material.

I do see a problem here, you mention the usage of the base material. That is something I haven't really thought about, but it kinda works out anyways - the renderer will use the base material if it does not find a SubMaterial matching the Id. I did however discover a bug in the renderer that I've fixed straight away. The SubMaterials was a List before and it had a check if the MaterialId was less than the count of the SubMaterials - it would skip it altogether. I've pushed this fix now.

The way you describe how you do it should work just fine. You should set the SubMaterials by index and reflect the same index in your MaterialId for the face. Maybe the bug I found was affecting your results.

Oct 31, 2010 at 7:08 PM

Thanks for the quick fix adept,  I found a bug in my code and after fixing that I was still having issues viewing subMaterials.  Turns out I was skipping numbers, in fact I was going 100,200,300, etc for my sub Material IDs.  But with the latest source it's working great! Thanks again.