three.js - 检查对象是否仍然在相机视野中

时间:2015-04-20 20:55:44

标签: javascript camera three.js fieldofview

使用2d画布时,如果你想检查屏幕上是否有某些内容""你只需做这样的事情:

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

我如何检查屏幕上是否有什么东西?#34;&#34; (在相机视图中)在一个three.js场景中?

1 个答案:

答案 0 :(得分:9)

不是检查2d画布,而是检查3d点是否为平截头体。

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...
}