Hi all !
I would like to ask the forum about advice on how to perform frustum culling on the instances of an InstancedMesh.
I know it’s a feature not supported natively by three.js, but I have some intuition that ‘something’ could be done in that regard, even if it’s not considered ‘true’ culling.
The most straigtforward way seems to be reordering the instances and all attribute buffers and then play with the ‘count’ property to draw the non-culled intances. If I’m not mistaken it’s what @agargaro did in his awesome InstancedMesh2 library. But accessing, reordering and computing all those arrays/buffers in javascript every frame (specially if you have a good amount of custom attribute buffers, which I do) seems to be way more computing work than just rendering all instances without doing the culling…
But instead I’ve been toying in my mind for some time with the idea of stopping early the computations in the vertex buffer, and therefore then discard also the computations in the fragment shader, for the culled instances as far as all the instanced meshes materials have some kind of texture/attribute that informs them if a certain instance should be visible.
All my ‘high level’ objects are static, and I compute on init a BVH to know their position and size. So in theory knowing if they should be culled or not is pretty fast and efficient (on the CPU side). How could I pass this data also in the most efficient way to all the meshes in the GPU then?
Maybe store the culling value every frame in some kind of shared buffer/attribute/texture among all affected InstancedMeshes? This is where I start to doubt if it would be worth it of the updating/uploading to the GPU, specially on low-end hardware which is the target I would like to improve the performance on.
Maybe there is some way using GPU compute to do the culling 100% on the GPU side?
Any idea would be super welcomed, maybe it could en up in something that we could add to the engine.
Thx!