Trismer
Trismer

Reputation: 1

ThreeJS Object3D.frustumCulled

ThreeJS objects have a check to see if the object is in the camera area - Object3D.frustumCulled.

How to get the "Object3D.frustumCulled" check result?

Upvotes: 0

Views: 106

Answers (1)

fsdev
fsdev

Reputation: 36

I think frustum culling is handled internally, but you use your own Frustum instance, created from a ViewProjectionMatrix calculated from your Camera. You will have to recalculate Frustum every time the Camera changes.

Creating/updating a Frustum can be done like that:

function updateFrustum(camera: THREE.Camera): THREE.Frustum {
    const frustum = new THREE.Frustum();
    const cameraViewProjectionMatrix = new THREE.Matrix4();

    camera.updateMatrixWorld(); // make sure the camera matrix is updated
    camera.matrixWorldInverse.copy(camera.matrixWorld).invert();
    cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
    frustum.setFromProjectionMatrix(cameraViewProjectionMatrix);
    return frustum;
}

This way you can use Frustum.intersectsObject(object3d) for your scene objects.

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
...    
function animate() {
    requestAnimationFrame(animate);
    const frustum = updateFrustum(camera);

    scene.traverse((object) => {
        if (frustum.intersectsObject(object)) {
            // handle object culling
        }
    });
}

Hope that helps.

Upvotes: 1

Related Questions