Using Material.onBeforeCompile on multiple materials causes cache conflicts
See original GitHub issueDescription of the problem
Creating multiple materials using onBeforeCompile
breaks when the source code of onBeforeCompile
is exactly the same.
It looks like the compiled programs are cached based on the onBeforeCompile
source code (looking at material.program.cacheKey
).
Here is an example:
The makeMaterial
function is supposed to create two different materials (based on parameters), but the two objects render in the same color.
As a workaround, redefining material.defines
works. In the above example, adding the following line to makeMaterial
fixes the issue and makes the two object render differently:
material.defines = {'FOO': ''+r};
I realize that it might not be so easy to disambiguate the two materials, but maybe this could be documented as part of Material.onBeforeCompile
?
Three.js version
- Dev
- r116
Browser
- All of them
- Chrome
- Firefox
- Internet Explorer
OS
- All of them
- Windows
- macOS
- Linux
- Android
- iOS
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Material#onBeforeCompile – three.js docs
Abstract base class for materials. Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have...
Read more >javascript - Three.js how to combine multiple materials with ...
Now i can use the FOO version of the material. But what if i want to add another version of the MeshStandardMaterial that...
Read more >three LineBasicMaterial JavaScript Examples
This page shows JavaScript code examples of three LineBasicMaterial.
Read more >three.js - Apple Open Source
Matrix4: the constructor no longer reads arguments. use .set() instead. ... else { object.material = serialize( meta.materials, this.material ); } } // if ......
Read more >UNPKG - @google/model-viewer
lib/model-viewer.js"],"sourcesContent":["'use strict';\n\n/* eslint ... (2) a class is added to the\n * element that causes it to match different custom ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it would! I would just add a short warning to the description of
onBeforeCompile
as well.Can’t the cache use the results of the shader code as substituted by onBeforeCompile as (part of) the key in the case of non-trivial onBeforeCompile? That would have an overhead of long keys, but only in the case where onBeforeCompile was used, and would not burden the coder with the need for customProgramCacheKey.
Also would not burden the user with wasted debug time from the current unexpected behaviour. He would eventually find the relevant changed documentation (as I eventually found this issue thread) and be able to fix the code, but only after some time.