# Dynamically Loading in Materials

**URL:** <https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483>\
**Category:** Questions\
**Tags:** blender, material, multimaterial, gltf\
**Created:** [July 19, 2021, 4:35pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483 "2021-07-19T16:35:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johntdaly7](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@johntdaly7](https://forum.babylonjs.com/u/johntdaly7)\
**Post date:** [July 19, 2021, 4:35pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483/1 "2021-07-19T16:35:42Z")

</div>

Hey everyone,

So I am trying to figure out a way to decrease our large furnished-office-space models, beyond just compression, and the current angle we are looking at is dynamically loading in furniture sets and materials upon in-app selection.

I can load in another glb easily enough, but what I am struggling with is figuring out how to dynamically load a material. Correct me if I’m wrong, but it seems for the most part a material must be associated with a mesh in Blender. Because of this, we have created a lot of duplicate meshes just to carry variant materials based on design selections.

So, in Blender, when exporting a glb/gltf, we are looking at creating an extension that decouples the duplicate meshes that carry material variants for the same type of object (ie a chair) from the scene, and instead, create a reference JSON that links a URL of a texture, its associated property data, UVMap, and shader data, etc. to the mesh. Then, when in Babylon, when that material is selected, I want to create a new PBRMaterial on the fly by loading in the texture and setting all the associated property values. My issue, however, is that when grabbing data from the .gltf, the UVMap and atlassing data needed to create the material is obviously missing (in the .bin I presume).

Does anyone know where I can find how Babylon reads in all the data necessary to identify and create a Material from the gltf/glb binary?  
I’ve been looking through the github here: [Babylon.js/babylon.glTFFileLoader.js at 6eddbac20810c74a2a6e08ba632f50842ae83f07 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/blob/6eddbac20810c74a2a6e08ba632f50842ae83f07/dist/loaders/babylon.glTFFileLoader.js#L2042)  
but there’s so much going on that I could use a bit of direction.

Also, during my research into this process, I discovered the Khronos extension for material variants, which is cool and probably useful:

> **[Streamlining 3D Commerce with Material Variant Support in glTF Assets](https://www.khronos.org/blog/streamlining-3d-commerce-with-material-variant-support-in-gltf-assets)**
>
> In the world of e-commerce, many products come in different options, or variants. When shopping online, for example, colors and materials of a brand of shoe might have an image representing each option. And now, in addition to using 2D images, more...

However, I am wondering if, given the size of our office building models with furniture, it would still make sense to decouple the variant materials from the geometry and load them in as needed based on what the user selects. In which case, even if I use the Khronos extension, I need to figure out how to load in a material (shader, texture, properties, UVMaps for atlassing, etc.) dynamically (possibly from a JSON file that was created via our export extension in Blender) and create it during runtime.

Finding out what to extract from the .gltf and especially .bin when exporting from Blender, based on how the exported material data may be read/dynamically created on the Babylon side, is the hard part, and any guidance would be very helpful!

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 19, 2021, 4:39pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483/2 "2021-07-19T16:39:13Z")

</div>

Adding @bghgary as I wonder if Variants could be split out of the main material ?

---

<div class="post-metadata">

**Author:** ![johntdaly7](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@johntdaly7](https://forum.babylonjs.com/u/johntdaly7)\
**Post date:** [July 19, 2021, 4:44pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483/3 "2021-07-19T16:44:23Z")

</div>

Thanks! And even if they can be or can’t be, just figuring out all the data in the .gltf/.bin that I would need to extract in order to create a new PBRMaterial in Babylon from scratch based on the variant is what I am probably looking for.

@bghgary, I noticed you are involved in the Khronos forum as well. Do you think it would make more sense to ask this question there? I figured I’d ask it here first because I think this implementation is reliant on how Babylon reads in the data from the file.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [July 19, 2021, 8:30pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483/4 "2021-07-19T20:30:30Z")

</div>

> [@johntdaly7](#):
>
> I can load in another glb easily enough, but what I am struggling with is figuring out how to dynamically load a material. Correct me if I’m wrong, but it seems for the most part a material must be associated with a mesh in Blender. Because of this, we have created a lot of duplicate meshes just to carry variant materials based on design selections.

I don’t know much about Blender, but can you associate the material with a super simple geometry just to carry the pay load for the material?

> [@johntdaly7](#):
>
> So, in Blender, when exporting a glb/gltf, we are looking at creating an extension that decouples the duplicate meshes that carry material variants for the same type of object (ie a chair) from the scene, and instead, create a reference JSON that links a URL of a texture, its associated property data, UVMap, and shader data, etc. to the mesh

This sounds exactly like the material variants extension you already found.

> [@johntdaly7](#):
>
> Then, when in Babylon, when that material is selected, I want to create a new PBRMaterial on the fly by loading in the texture and setting all the associated property values.

This is interesting. The current implementation loads all of the materials on load to avoid having to wait when switching materials, but I can see how that isn’t always the right thing to do. I wonder if we can add a flag to the existing implementation to load the material async upon request instead of at load?

> [@johntdaly7](#):
>
> My issue, however, is that when grabbing data from the .gltf, the UVMap and atlassing data needed to create the material is obviously missing (in the .bin I presume).

I’m not sure what this means. What do you mean by “grabbing data from the .gltf”? Are you trying to parse the glTF yourself? Can you explain this more?

> [@johntdaly7](#):
>
> However, I am wondering if, given the size of our office building models with furniture, it would still make sense to decouple the variant materials from the geometry and load them in as needed based on what the user selects. In which case, even if I use the Khronos extension, I need to figure out how to load in a material (shader, texture, properties, UVMaps for atlassing, etc.) dynamically (possibly from a JSON file that was created via our export extension in Blender) and create it during runtime.

When you say “decouple”, do you mean separate files? You can already do this. GLB files are typically a single file, but it doesn’t have to be. You can reference external buffers with a GLB. For example, you can put all of the binary data for each material in a separate buffer with a separate url. Another choice is to use glTF and not use GLB at all.

> [@johntdaly7](#):
>
> Finding out what to extract from the .gltf and especially .bin when exporting from Blender, based on how the exported material data may be read/dynamically created on the Babylon side, is the hard part, and any guidance would be very helpful!

I think given what I understand of your scenario, a good solution would be to generate a glTF/GLB with external references for each material and modify the Babylon.js glTF loader extension for material variants to support lazy loading of each material.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [July 19, 2021, 8:36pm UTC](https://forum.babylonjs.com/t/dynamically-loading-in-materials/22483/5 "2021-07-19T20:36:04Z")

</div>

> [@johntdaly7](#):
>
> @bghgary, I noticed you are involved in the Khronos forum as well. Do you think it would make more sense to ask this question there? I figured I’d ask it here first because I think this implementation is reliant on how Babylon reads in the data from the file.

I’m not sure. It depends if your question is about the Babylon.js implementation or something about glTF in general.
