three.js - 检查物体是否仍在相机的视野中 [英] three.js - check if object is still in view of the camera

查看:77
本文介绍了three.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...
}

这篇关于three.js - 检查物体是否仍在相机的视野中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆