6

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.setFromMatrix( camera.projectionMatrix );

if(frustum.containsPoint( mesh.position )){
    //stuff happens...
};

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

Kevin Kuyl
  • 1,100
  • 14
  • 27

2 Answers2

8

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

larsmoa
  • 11,450
  • 5
  • 54
  • 82
Leeft
  • 3,687
  • 1
  • 12
  • 22
0

Three.js is doing view frustum Culling internally and only rendering when in camera frustum. Assuming that your Boundig Volumes are calculated correct, you can track weather a renderable Object3D is inside the camera view frustum when Object3D.onBeforeRender callback is called in your current frame

air5
  • 61
  • 1
  • 7