How to Override Materials

This is caused by having an InstancedMesh in my scene, and probably related to this: Can not set material to an InstancedMesh even though it is an AbstractMesh · Issue #6942 · BabylonJS/Babylon.js · GitHub

I can easily fix it with a if (mesh.getClassName() === 'InstancedMesh') { return; } right on the start of the onBeforeRender callback. Anything wrong with that?

@deltakosh, I can submit the PR on that issue if you confirm what needs to be done. Thanks.

In fact, you can’t do it there but you should add something like:

            if (mesh.getClassName() == 'InstancedMesh') return;

right at the start of the forEach loop in both the onBeforeRender and onAfterRender callbacks, as else we are doing unnecessarily the same thing multiple times (for the source mesh + its clones, whereas we only need to do it for the source mesh):

Yep, inside the forEach, that’s what I meant, sorry. My question is if it has any drawbacks and could ignore significant parts of the models.

As I can see it, no, as when we get the material with instance.material we in fact get the material of the source mesh:

    public get material(): Nullable<Material> {
        return this._sourceMesh.material;
    }

-> it is the implementation in InstancedMesh.

And when setting a material with instance.material = XXX it is in fact a “do nothing” operation because there’s no setter in InstancedMesh (the code above creates a material property in the clone mesh but this property is never used, as accesses to the material is always done through the getter, so the material from the source mesh is always retrieved instead).

1 Like