Three.js: initMaterial called each time render is called on a material used on two scenes

Created on 14 Jul 2018  路  6Comments  路  Source: mrdoob/three.js

Description of the problem

I noticed that there was a significant slowdown in the app I'm developing. I narrowed it down to being caused by initMaterial being called each frame under certain very specific conditions

. The minimum requirements to reproduce the bug (afaik) are:

  • Create two meshes which share the same MeshLambertMaterial.
  • Create two scenes and add the meshes to them.
  • Create a single DirectionalLight in one of the scenes, not both.
  • Create an OrthographicCamera overlooking the objects (not a perspective camera).
  • Call render twice, once for each scene.

If you don't notice the slowdown, use the performance tab in Firefox to measure the performance and you should see WebGLShader at the top of the call tree.

Three.js version
  • [x] Dev
Browser
  • [x] Firefox
OS
  • [x] Windows
Duplicate

Most helpful comment

OK. Perhaps we can close duplicates like so:

_Closing as a duplicate. Please continue the discussion in #14121._

All 6 comments

performance

Hmm... If a material is shared between two scenes, initMaterial() is called with each use because:

materialProperties.lightsHash !== lights.state.hash

The hash is defined as:

state.hash = state.id + ',' + directionalLength + ... etc...

Even if the numbers and types of lights are the same in both scenes, state.id is different.

So, sharing a material between scenes will not be performant.

The work-around is to clone the material.

var mesh1 = new THREE.Mesh( geometry, material );
var mesh2 = new THREE.Mesh( geometry, material.clone() );

I guess I would call this a limitation.

And a duplicate of #14121.

The issue may be a duplicate, but the content is not.

There is useful information in this thread that is not in #14121, namely that there is a work around available.

And the implication is we should document this limitation and work-around somewhere if we are not going to consider this a bug and fix it.

I do not think we should close _any_ issue before it has been addressed.

In general we avoid multiple issues about the same topic. Since #14121 is the leading issue, you should transfer the workaround to this discussion.

OK. Perhaps we can close duplicates like so:

_Closing as a duplicate. Please continue the discussion in #14121._

Sounds good!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scrubs picture scrubs  路  3Comments

konijn picture konijn  路  3Comments

clawconduce picture clawconduce  路  3Comments

akshaysrin picture akshaysrin  路  3Comments

jack-jun picture jack-jun  路  3Comments