本文介绍了三.js - 检查对象是否仍在相机视野中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 2d 画布时,如果您想检查某些内容是否不再在屏幕上",您只需执行以下操作:
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
}
我将如何检查在three.js 场景中是否仍然在屏幕上"(从相机的角度看)某些东西?
How would I check to see if something is still "on screen" ( in view of the camera ) in a three.js scene?
推荐答案
您可以检查 3d 点是否为截锥体,而不是检查 2d 画布.
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...
}
这篇关于三.js - 检查对象是否仍在相机视野中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!