My camera is rotated and looking at a random point in space. I have an Object3D somewhere that I want to test if it is behind the camera’s “infinite” view plane. We can just use the Object3D’s position to simplify things here. How can I test to find out if the Object3D is behind or in front (even out of view) of the camera view direction?
it is using a trick that has near zero expense by hooking into onBeforeRender, which is only called if the object actually rendered out. if it’s not called you can be sure that it’s either invisible or outside the camera frustum.
obj.onBeforeRender = () => (obj.insideFrustum = true)
function renderloop() {
obj.insideFrustum = false
// If the object is inside the frustum three will call oBR
gl.render(scene, camera)
if (obj.insideFrustum) console.log("in the cameras frustum")
else console.log("outside the cameras frustum")
}
the alternative to this is a frustum check. but why execute that twice if threejs already did it inside the render function.
PS here’s a practical application of that code, if you scroll down the images come popping in once they are near the frustum:
I can only give code, not mine. Maybe u need check that all of vertices is behind of camera, not only one centered point of object. behind.zip (970.3 KB)
Yeah, this one is nice as it checks to make sure the entire Object3D is fully behind the camera. I’m going to have to see how I can extract and contain the code that does the check so that it can be easily re-used. Thank you very much.
Interesting trick. I’m going to have to see how to contain this and set that object property without having to resort to the render loop to set it back to false. Maybe the onAfterRender is where I would set it to false… Thank you for this technique!
both oBR and oAR are only called when the object can actually render which requires it to be within the frustum. if it isn’t oAR isn’t called, you need to check after you called gl.render otherwise it won’t work.