Update geometries' boundingBox taking Shader displacement into account

Hi there! In my current project I have a few GPU Instanced Geometries. Means I duplicate and place the same model via shader code as described in this tutorial series https://medium.com/@pailhead011/instancing-with-three-js-36b4b62bc127.
This way of placing static assets in a scene reduces draw calls and improves overall performance enormously.

BUT there is one little problem I couldn’t figure out by now. Since I use the mesh’s shader to translate duplicates of the actual geometry, the geo’s boundingBox won’t update – which means – I do have to set frustumCulled to true in order to make sure the mesh gets rendered. This results in the mesh getting rendered even if it’s not in the viewport, which again causes unnecessary draw calls.

So the question is: Is it possible to recalculate my geo’s BoundingBox and taking displacements via shaders into account?

Thanks in advance!

Sadly, the boundingBox class only reads the geometry position attribute to calculate its dimensions. If you manipulate those positions via shader code, the boundingBox will not be aware of these changes.

Maybe you could manually tell the boundingbox where you plan to create more instances by using its .expandByPoint method: https://threejs.org/docs/#api/en/math/Box3.expandByPoint

For instance, if you’re planning on making an instance at [5, 10, 3], you could do something like

var point = new THREE.Vector3(5, 10, 3);
geom.boundingBox.expandByPoint(point);

This of course, doesn’t take into consideration the width, height, or depth of this instance, but it’s an approximation. There are a bunch of other helper methods in Box3 that can help you get better calculations, such as https://threejs.org/docs/#api/en/math/Box3.setFromBufferAttribute