Gltf multiple materials loading


I have same material in various objects, which I am using assets manager to load each object individually in its own task, I noticed that Babylon create a second material, for example if model1 has material name “black_frame” and model2 also has same material, Babylon creates a second material named “black_frame 2” when I load model2 using assets manager task.

How could I prevent this and force Babylon to use a material if its already loaded in other gltf object? Right now I am thinking, when all scene is loaded, I would loop through all materials and dispose duplicates.


It is normal because the script itself “doesn’t know” that those materials are identical.
If you want to reduce the quantity of materials in your scene you may need different wokflow (which is not always convenient). For example:

  1. Remove all duplicate materials (except one) from all GLTF files. So that model2 would have only geometry.
  2. var myMaterial = scene.getMaterialByName("black_frame");
  3. model2.material = myMaterial;

If you want to dispose material duplicates, you’ll need to assign the proper material to this mesh again, otherwise it will have no material at all.


Thanks @labris I would use this approach, as I am optimising my scene so found this issue, thought, I was doing something wrong.

1 Like