Add a callback for initializing custom uniforms
With the introduction of caching multiple programs per material, the following use case is possible:
- Material is initialized and customized with onBeforeCompile, including setting up custom uniform values.
- Material compile-time parameters are changed. Custom uniforms are again changed in onBeforeCompile.
- 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.
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 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.
Thanks for the fiddle! I'll need to investigate the problem closer before I can provide feedback. Trying to find an alternative approach.
@Mugen87 @mrdoob friendly ping! I don't think I have very good ideas for an alternative approach here.
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.
@mrdoob Time to merge this perhaps? 🙂
@Oletus Would it be possible to also add an example for this to make sure we don't break it?
@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!