Changing attribute of InstancedMesh


I’m trying to realize is it possible to change attributes of InstancedMesh and update it ? I’m trying to change custom attribute (uv offset that used in shader) in render() by getting this attribute and change its values and set instanceMatrix.needsUpdate = true; but nothing happened… what I’m doing wrong?
The code in render():

let uvOffsets = instancedMesh.geometry.getAttribute( 'instanceUV');
for(let k=0; k<uvOffsets.count;k++)
	uvOffsets.setY(k,0);//reset all inited uv values
instancedMesh.instanceMatrix.needsUpdate = true;

Also would like to know is it possible to hide some of the instance instead of move it off screen somewhere ? And if it moved somewhere off screen/camera will it be also rendered and be a performance issue?

UPD Forgot notice that all shader modification is inside of onBeforeCompile of a material. I hope it executes not once?

Yes, all instances are rendered no matter how the camera is oriented. View frustum culling is disabled for InstancedMesh by default. If you enable it, you have to provide a proper AABB that encloses all instances. Even then, the engine only culls all instances or none of them.

If you don’t want to render certain instances, it’s recommended to arrange InstanceMesh.instanceMatrix in a way so the rendered instances appear first in the buffer attribute. You can then use InstanceMesh.count to control the number of rendered instances.

In any event, if you update the instanceUV attribute, it’s not necessary to set the needsUpdate flag of instanceMatrix to true. This buffer attribute is totally unrelated to this use case. Instead, set the needsUpdate flag of uvOffsets to true.


Thank you :)! Now it works just fine!

1 Like