I’m currently using a heavily modified PhongMaterial
to render my objects. The modifications are done as patches using onBeforeCompile
, and things are starting to become unwieldy and ugly. I have 500 lines of patch code just to make the material use array textures. You can’t even read and understand the shader code as a whole; it’s a huge unmaintainable mess of patchwork, impossible to reason about.
I’ve sensed from the start that this patch approach would be madness, but I did it anyway against my better judgement.
Then I want to adjust the material’s uniforms at runtime, but it looks like that’s not a simple thing to do? Like material.shader.uniforms.shininess.value = value
is not easily possible, you have to use a hack involving onBeforeRender
. And if I want to use different initial material parameters (uniforms) for every object, the same identical shader code is compiled again, for every object, because the value of a uniform is different. This makes no sense to me at all. Uniforms exist to prevent exactly this.
So here’s what I want to do: copy the PhongMaterial
, make it a ShaderMaterial
, using the GLSL shader code from the original material, so I can modify the shader code directly instead of having this patch madness that will break anyway sooner or later.
Use one shared instance of the material for every object, and adjust the uniforms on a per-object basis instead of per-material. I don’t need another instance of an identical shader just because one object uses a slightly different value for the shininess uniform.
Before I do this, I would appreciate advice from more experienced devs: is this a sensible thing to do? Am I missing something? Is the above rant accurate or am I doing it wrong? How would you solve the issues?