Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

THREE.js check if object is in frustum

i've been tinkering around with three.js and i have a canvas i'd like to use as kind of a GUI. for that i have to check if an object is in the camera frustum.

my current code:

camera.updateMatrix(); 
camera.updateMatrixWorld(); 
        
var frustum = new THREE.Frustum();
var projScreenMatrix = new THREE.Matrix4();
projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
        
frustum.setFromProjectionMatrix( camera.projectionMatrix );
        
if(frustum.containsPoint( mesh.position )){
    //stuff happens...
};

frustum.containsPoint()keeps returning false. what am i doing wrong here?

like image 534
Kevin Kuyl Avatar asked Jul 22 '14 02:07

Kevin Kuyl


1 Answers

Your code is using

frustum.setFromMatrix( camera.projectionMatrix );

But that isn't the matrix you want. Instead use:

frustum.setFromMatrix( new THREE.Matrix4().multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ) );

as answered in How to determine if plane is in Three.js camera Frustum

like image 99
Leeft Avatar answered Nov 09 '22 14:11

Leeft