question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Material.onBeforeCompile on multiple materials causes cache conflicts

See original GitHub issue
Description 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pwmarczcommented, May 17, 2020

Yes, it would! I would just add a short warning to the description of onBeforeCompile as well.

0reactions
sjptcommented, Jun 7, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found