three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Add a callback for initializing custom uniforms

Open Oletus opened this issue 4 years ago • 8 comments

With the introduction of caching multiple programs per material, the following use case is possible:

  1. Material is initialized and customized with onBeforeCompile, including setting up custom uniform values.
  2. Material compile-time parameters are changed. Custom uniforms are again changed in onBeforeCompile.
  3. Material compile-time parameters are changed back to the ones used in step 1. Since the program is fetched from cache, onBeforeCompile is not called. The new callback Material.onProgramChange() enables changing custom uniforms to the intended values in this case instead of being stuck with the custom uniform values set in step 2.

This contribution is funded by Higharc.

Oletus avatar Aug 18 '21 08:08 Oletus

Do you mind demonstrating the issue with a live example? Maybe based on https://jsfiddle.net/Lb8yeks6/?

Since the program is fetched from cache, onBeforeCompile is not called

Even if the program is fetched from cache, it just means the custom uniforms values are outdated, right? But shouldn't the uniforms get updated if you use the program for a render?

Mugen87 avatar Aug 20 '21 08:08 Mugen87

@Mugen87 Sorry about the slow reply, this got buried under some other work. But here's an example where this kind of callback would be needed: https://jsfiddle.net/7pga1kL4/

It should alternate between a slightly twisted face and a fast rotating one, however after the material is toggled the second time the uniform value doesn't get set since the program is fetched from cache and onBeforeCompile is not called.

Oletus avatar Sep 05 '21 17:09 Oletus

Thanks for the fiddle! I'll need to investigate the problem closer before I can provide feedback. Trying to find an alternative approach.

Mugen87 avatar Sep 08 '21 08:09 Mugen87

@Mugen87 @mrdoob friendly ping! I don't think I have very good ideas for an alternative approach here.

Oletus avatar Sep 28 '21 13:09 Oletus

I could not come up with something different either unless using the (fundamental differently) node material. For now, an additional callback like onProgramChange() seems the easiest solution for what you are looking for.

Mugen87 avatar Sep 28 '21 13:09 Mugen87

@mrdoob Time to merge this perhaps? 🙂

Oletus avatar Oct 19 '21 11:10 Oletus

@Oletus Would it be possible to also add an example for this to make sure we don't break it?

mrdoob avatar Nov 01 '21 19:11 mrdoob

@Oletus Would it be possible to also add an example for this to make sure we don't break it?

Sure, added a commit modifying the existing example slightly!

Oletus avatar Nov 02 '21 15:11 Oletus