three.js - check if object is still in view of the camera

喜欢而已 提交于 2019-12-01 18:04:16

问题


When working with a 2d canvas, if you want to check if something is no longer "on screen" you simply do something like this:

if( pos.x > window.innerWidth || pos.x < 0 ||
    pos.y > window.innerHeight || pos.y < 0 ) {
    // has left the screen
}

How would I check to see if something is still "on screen" ( in view of the camera ) in a three.js scene?


回答1:


Instead of check 2d canvas, you can check a 3d point is in frustum or not.

camera.updateMatrix();
camera.updateMatrixWorld();
var frustum = new THREE.Frustum();
frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse));  

// Your 3d point to check
var pos = new THREE.Vector3(x, y, z);
if (frustum.containsPoint(pos)) {
    // Do something with the position...
}


来源:https://stackoverflow.com/questions/29758233/three-js-check-if-object-is-still-in-view-of-the-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!