Cloning built-in materials without new shader program

I built a custom ShaderMaterial that can be cloned and have their uniforms changed for different Meshes, while using just one shader program (as described in this comment Three.js, sharing ShaderMaterial between meshes but with different uniform sets - Stack Overflow).

Is this possible with built-in materials? I’d like to tweak the shaders in MeshLambertMaterial via .onBeforeCompile(), clone it repeatedly, and then change uniforms for each clone. All while using one program.

Another route I’m trying is replicating MeshLambertMaterial by plugging in the shaders into ShaderMaterial, but that is trickier than I expected.

The renderer should detect that a single shader program can be shared across all materials.

However, keep in mind that Material.onBeforeCompile() is not honored by clone(), copy() and toJSON(). So you have to assign the callback manually after the clone.

1 Like