Why mesh.materials is not a thing?

Good day,

I have a mesh with many materials.
Why Babylon.Mesh or Babylon.AbstractMesh does not give access to all materials? There is only an option to access a material.

I can use

const sceneMesh = scene.getMeshByName(mesh.name);
sceneMesh.material.subMaterials

but it is always null.

I am probably missing some fundamentals. However, any other engine, as Unity or Unreal, always give an option to grab materials from any type of mesh.

Thank you.

Hi Lauris47 !

Actually, you can apply only one single Material to a Mesh.
However, this material may be an instance of the class MultiMaterial. It allows to apply different materials to different parts of the same mesh.

Read more here :

Then you could access to all subMaterials using the subMaterials attribute which is specific to this class.
See log line 40.

2 Likes

Thank you for the info.
What happens if merged mesh, that has many materials is exported to GLB for Babylon, does it get split based on the amount of materials it has?
(Assuming this, sine you state that mesh has only one material)

cc @bghgary for the exporter.

A mesh with multiple submeshes using a MultiMaterial will be written to a gltf/glb as a single glTF mesh with multiple glTF primitives pointing to different glTF materials.