Global uniforms for all materials

I’m unsure what the correct approach in three.js is to apply some global uniforms to many materials/objects.

I have these options:

  • patch ShaderChunks to add code and related uniforms to them
    • this will apply to all shaders using those chunks. How do I set their uniforms in a good way?
  • use Material.onBeforeCompile
    • requires access to invidual materials, which isn’t trivial when using e.g. GLTFLoader
    • still not entirely clear how/when to set uniforms in a good way

One approach I would imagine would be to have a “global” per-material beforeRender callback of sorts where I can set/update some uniforms for every material based on some condition (e.g. set tonemapping-related uniforms when tonemapping is set to a specific option).

1 Like

Related discussion at GitHub:

From what it looks like the discussed approaches still require access to all materials, e.g. by traversing the scene. I think that’s not feasible in applications with a mix of glTF loading, custom scripts creating and modifying materials, and so on. Not sure I can see from the linked issue how that would be different with nodes?

Yes, there is no solution for this issue yet.

I just wanted to add the link as a reference since it might be the better place for pushing this topic forward.

1 Like