Modifying StandardMaterial or custom ShaderMaterial?

Hello wonderful people !

I have a loaded mesh and point light in the scene, THREE.StandardMaterial works very well for my needs, but i wanted to add some custom glsl code to animate my material and i successfully did it by using onBeforeCompile it is very easy to use method. The problem is when i clone() my newly modified material it does not carry any modifications i introduced. I have total 4 instances i need this material to work, i guess i can create 4 unique materials but it feels like very wordy and repetitive and i wonder will it impact performance of my scene.


Is it possible to clone material and keep onBeforeCompile mods introduced in origin al material ?
Any recommendations how to deal with this ?

I tried to create my own ShaderMaterial with pointlight and specular and texture normal maps… and it does not look great, not as great as Standard material, i would love to completely extract three.js point light setup but unfortunately i cant find whole code and chunks does not help alot without seeing full picture of how material works. Another problem, i cant figure out how ti view compiled StandardMaterial vertex and fragment shader (i would be able to copy entire code and use in my ShaderMaterial… maybe?)

Also found some examples of how to include point light to ShaderMaterial, but this exmaples for the most part are very old and does not include how to enable mapNormal usage (since my objects have normal textures). If anyone has recent working examples somewhere on codepent available that would be great.

Thank you !

Just for clarification. Are you talking about 3D object when using the word “instances”? If so, would it be possible to share a single material? Or do you need to parameterize the materials differently?

No, it’s not. It’s probably best if you develop a helper function that clones the material and applies the onBeforeCompile() callback to the cloned materials.

I would not create a custom material from scratch via ShaderMaterial if you only want to modify the built-in shaders at a few places. For more extensive rewrites, a custom material would be indeed better.