instanceColor attribute is not defined when InstancedMesh is populated asynchronously
See original GitHub issueJust 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
- Prepare a scene with InstancedMesh
- Render it
- Populate the instanced mesh asynchronously
Live example
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:
- Created 2 years ago
- Comments:10 (4 by maintainers)
Top 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 >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
Hmm, I wonder… what would happen in this case?
And asynchronously we did:
Because material is shared between
mesh1
andmesh2
then it’s possible thatWebGLRenderer
will handlemesh1
first and setmaterial.needsUpdate
tofalse
and by the time it handlesmesh2
(which now has instanced colors) it won’t know that it needs to update becausematerial.needsUpdate
was set tofalse
…@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 withoutinstanceColor
. 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.