最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - THREE.js check if object is in frustum - Stack Overflow

programmeradmin1浏览0评论

I have 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();

const frustum = new THREE.Frustum();
const 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?

I have 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();

const frustum = new THREE.Frustum();
const 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?

Share Improve this question edited Jun 21, 2024 at 13:43 double-beep 5,52019 gold badges40 silver badges48 bronze badges asked Jul 22, 2014 at 2:21 Kevin KuylKevin Kuyl 1,2651 gold badge18 silver badges31 bronze badges 3
  • A Frustum takes 6 Planes as arguments, you're not providing anything. That's probably why it doesn't work. – Leeft Commented Jul 22, 2014 at 7:10
  • So how would i go about cloning the camera frustum? – Kevin Kuyl Commented Jul 22, 2014 at 7:40
  • An example of that is in this answer: stackoverflow./questions/10858599/… – Leeft Commented Jul 22, 2014 at 7:56
Add a ment  | 

4 Answers 4

Reset to default 9

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

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

Here is what I did to find if a mesh is in camera view (within 100ms):

mesh.onBeforeRender = function() {
    if (mesh.userData.inViewID) clearTimeout(mesh.userData.inViewID);
    mesh.userData.inViewID = setTimeout(()=>{
        mesh.userData.inView = false;
        console.log("out of view");
    }, 100);
    if (!mesh.userData.inView) { 
        mesh.userData.inView = true;
        console.log("in view");
    }
}

now in version r166 the code has changed slightly so I add an update.

const frustum = new THREE.Frustum();

camera.updateMatrix();
camera.updateMatrixWorld();
mesh.updateMatrix();
mesh.updateMatrixWorld();

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

const intesectedMesh = frustum.intersectsObject(mesh);

if(intesectedMesh){
//Do something
}

Regards

发布评论

评论列表(0)

  1. 暂无评论