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.

instanceColor attribute is not defined when InstancedMesh is populated asynchronously

See original GitHub issue

Just as the title says. It all boils down to this line https://github.com/mrdoob/three.js/blob/dev/src/renderers/webgl/WebGLPrograms.js#L171. Setting any color for any instance just after the instanced mesh is created, solves the issue. But this is not obvious and looks like it’s not documented.

To Reproduce

  1. Prepare a scene with InstancedMesh
  2. Render it
  3. Populate the instanced mesh asynchronously

Live example

jsfiddle

Expected behavior

When setColorAt is called, the shader should be recompiled to include the instanceColor. Or there should be a valid and documented way to make sure that this attribute is always defined.

Platform

  • Device: Desktop
  • OS: Windows
  • Browser: Chrome
  • Three.js version: r128

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mrdoobcommented, May 10, 2021

Hmm, I wonder… what would happen in this case?

const material = new THREE.MeshBasicMaterial();

const mesh1 = new THREE.InstancedMesh( geometry, material, 10 );
scene.add( mesh1 );

const mesh2 = new THREE.InstancedMesh( geometry, material 10 );
scene.add( mesh2 );

And asynchronously we did:

mesh2.setColorAt();
material.needsUpdate = true;

Because material is shared between mesh1 and mesh2 then it’s possible that WebGLRenderer will handle mesh1 first and set material.needsUpdate to false and by the time it handles mesh2 (which now has instanced colors) it won’t know that it needs to update because material.needsUpdate was set to false

1reaction
Mugen87commented, May 6, 2021

@mrdoob It does not crash when a built-in material is used. https://jsfiddle.net/we76xsvr/1/

The OP also missed to set material.needsUpdate = true;. InstancedMesh is created without instanceColor. When added, the shader program needs a recompile. If the renderer should hide this, it would be necessary to check each frame if colors are present or not. For performance reasons, I do not vote to do this since the presented use case seems rare to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InstancedMesh#instanceColor – three.js docs
instanceColor : InstancedBufferAttribute. Represents the colors of all instances. null by default. You have to set its needsUpdate flag to true if you ......
Read more >
can not set color in InstancedMesh in three.js 125 version
It should be: stars2.setColorAt(i, color.setHex( 0xffffff * Math.random() ));. let camera, scene, renderer; init(); render(); function ...
Read more >
PDF version - Introduction to Computer Graphics
Geometric shapes by themselves are not very interesting. You have to be able to set their appearance. This is done by assigning attributes...
Read more >
model-viewer.min.js.map - UNPKG
ShadyCSS === 'undefined') ;\nelse if (typeof window.ShadyCSS. ... _ensureClassProperties();\n // initialize Map populated in observedAttributes\n this.
Read more >
https://raw.githubusercontent.com/mrdoob/three.js/...
_listeners === undefined) return false; const listeners = this. ... instanceColor !== null) attributes.remove(instancedMesh.
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