How do I intercept and edit PBR material shaders from loaded GLTF asset?

How do I intercept and edit PBR material shaders from loaded GLTF asset?
I need to customize my shaders dynamically building upon the base PBR material loaded from a GTLF model.

Will I need to create an effect and call rawVertexSourceCode() and rawFragmentSourceCode() in order to carry out my own preprocessing?

By name or id.

scene.executeWhenReady( () => {
    const mat = scene.getMaterialByName('blah');
    mat.roughness = 0.88;
});

Also intercept is bad terminology. Intercept is probably impossible, unless you change it in the system that generated the export. More of a post load adjustment.

2 Likes

Thanks @JCPalmer.

I was able to get the aforementioned shader source with this hack. I imagine there to be a much cleaner, idiomatic Babylonian way of doing this. Now to editing the source and creating a custom shader material and effect.

Hi. You simply can use node material and drive shader through your ‘public’ properties Node Material | Babylon.js Documentation look at Creating PBR materials section. Copy properties from your gltf material and set this properties for your new material in onload callback of your loader

1 Like

Thanks @kvasss however, I wish to do more than change properties but also add shader functions and conditional logic. The default blocks don’t match my use case. Should I create a custom block by instantiating or extending NodeMaterialBlock? I will dig into the BJS source for how the default block classes are defined. Thanks again.

Hi. I think you cant add more block after shader compilation in any way.

1 Like

No doubt. I was assuming I would have to generate a Node Material separately and assign it to the mesh. :+1:

Maybe this helps you Shader Material | Babylon.js Documentation

1 Like

Oh, I’m solid with that. I am studying examples of NodeMaterialBlock, such as crossBlock! I’m going to make my own block! :innocent:

2 Likes