I have a group, which contains a GLTF and a number of sprites. I want to know which sprites are occluded by the model on each frame that the camera moves.
I have this working but it’s not very performant. In the screenshot above you can see that 1 is not occluded but 2 and 3 are - so they have their opacity adjusted.
To achieve this I loop through each sprite (only the ones not frustrum culled) and raycast from the camera to the sprite. If the sprite is the first intersect then I class it as non-occluded:
If you’re gonna be doing lots of raycasts per frame, I can hiiiighly recommend using three-mesh-bvh on your scene to accelerate the raycasts.
Built in raycasting falls over when your meshes are even moderately dense and just throwing three-mesh-bvh at it makes raycasting basically instantaneous.
import { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } from 'three-mesh-bvh';
// Add the extension functions
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
... and when you load your meshes:
mesh.geometry.computeBoundsTree();
And now you can go crazy(er) with the raycasting.
(bvh imposes some limitations but its so worth it in terms of what it unlocks)