Override Material in gltf loader

Hello,

I am writing my own standard shader for my gltf meshes and I want to be able to apply the shader during mesh load instead of the PBRMaterial normally used by the glTF loader.

I found this snippet inside the glTF loader for babylon:

GLTFLoader.prototype._createDefaultMaterial = function (name, babylonDrawMode) {
    var babylonMaterial = new PBRMaterial(name, this._babylonScene);
    babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
    babylonMaterial.fillMode = babylonDrawMode;
    babylonMaterial.enableSpecularAntiAliasing = true;
    babylonMaterial.useRadianceOverAlpha = !this._parent.transparencyAsCoverage;
    babylonMaterial.useSpecularOverAlpha = !this._parent.transparencyAsCoverage;
    babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
    babylonMaterial.metallic = 1;
    babylonMaterial.roughness = 1;
    return babylonMaterial;
};

This is a perfect point since all I need to do is override that method with something that reads the gltf material and then builds my material instead of the normal PBRMaterial.
However, I can’t figure out how to override this method after creating a GLTF file loader. Right now my code to do this looks like this:

let loader = new GLTFFileLoader();
/* Somehow override the material creation? */
SceneLoader.RegisterPlugin(<any>loader);

If someone has some advice on how I can do this please let me know!

Hello!

completely untested but did you try something like:

loader.prototype._createDefaultMaterial = ...
1 Like

Hey, you can use maybe onMaterialLoaded function
https://www.babylonjs-playground.com/#10D6YT#53
but this means you let the loader to create the materials as it want, and you change or replace it after creation.
Or you can create your custom extension.
https://www.babylonjs-playground.com/#20XT9A#5
Second option I think is what are you looking for. Cheers!:beers: