When loading .babylon files exported by other tools, e.g. Blender, it’s useful to inspect/modify meshes/materials before they are instantiated.
For example, I’m replacing some materials with ShaderMaterial at runtime. This is actually a bit complicated. We need to find all materials that need replacing, replace them with ShaderMaterials, and then find all meshes that reference them, and update their material references. Not to mention that CPU cycles are wasted creating those original materials.
This can be greatly simplified if there were a way to modify the JSON before the scene loader instantiates all the nodes from it. Something like a callback on SceneLoader.LoadAsync for each mesh/material JSON definition, that allows us to return a different one.
I’d like to call it like this:
SceneLoader.LoadAsync(..., (json: object, type: string) => {
// type comes from the top-level nodes, e.g. materials|meshes|transformNodes|etc.
if (type === "materials" && myMaterials.has(json['name']) {
// return some other JSON to the loader
return {name: ..., customType: 'BABYLON.ShaderMaterial', ...}
}
return json;
});