-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Material: Added callback to identify customized programs #17567
Conversation
We've found this API to be very useful for more complex customizations of materials, though we're not quite sure of the naming - maybe "programHash" would be more understandable than "programCode"? Also suggestions on how to make an example for this would be welcome. Maybe we could change the existing webgl_materials_modified example so that it switches between two different materials using the same onBeforeCompile function but with different parameters? |
This PR provides a solution to a major gotcha when extending materials with
https://medium.com/@pailhead011/extending-three-js-materials-with-glsl-78ea7bbb9270 |
Related PRs for modified webgl material examples: Chunk replacements. You can provide a chunk replacement to be consumed by the material when the time comes. Ie. you're not bound to modify the materials when
And another |
I've struggled a bit with the name of |
I think "customProgramHash" or "customShaderHash" might be more understandable, even if an actual hash algorithm isn't necessarily involved. Or maybe "customProgramCacheKey"? If we do change it I'd like to go and change the core to also use "programHash" or "programCacheKey" instead of "programCode" for consistency. "programCode" can be a bit confusing since it sounds like it might refer to the shader source code rather than a key used for caching the compiled program. |
True. Related #16674 |
a304a9b
to
df64dc6
Compare
I suggest to undo the changes in |
Is there any chance that #14231 could be revisited? IIRC the biggest pro of I was never convinced that these kind of operations need to be called asynchronously in the It seems like |
e70f5db
to
df64dc6
Compare
Force-pushed to remove the commit that contained the example changes. @mrdoob this should be ready to merge! :) |
df64dc6
to
9720a54
Compare
In case onBeforeCompile has conditional statements in it, the parameter values affecting the shader code can be returned from customProgramCode so the shader program is correctly fetched from cache or recompiled as needed.
9720a54
to
11e7f63
Compare
Do you mind elaborating a bit? I'm having a hard time to understand what the problem is. |
@mrdoob @pailhead described the issue fairly well. Without this change three.js is only able to cache one material shader per unique onBeforeCompile callback. With this change several material shaders can be cached in case they have the same onBeforeCompile callback but that callback is run with different parameters (from a closure for example) for each material. Earlier this PR included an example but I was asked to remove that. Should I reintroduce the example code to make this easier to understand? |
It's not an issue unfortunately, but a feature, by design. So to be more specific, the request is can we change the behavior of
It is also something that might be unnecessary since I fought this battle for years, don't take it to personally if the PR is not merged despite being approved :) (btw it's me, @pailhead, just lazy to log in) |
Thanks! |
Woohoo! |
THANK YOU! I spent 7 hours straight trying to debug my code and eventually realized it was related to three.js material cache. So happy to find this. |
In case onBeforeCompile has conditional statements in it, the parameter values affecting the shader code can be returned from customProgramCode so the shader program is correctly fetched from cache or recompiled as needed.
Fixed #19377.