【问题标题】:How to tell if the camera is looking at the face of a plane如何判断相机是否在看飞机的表面
【发布时间】:2013-08-05 22:09:34
【问题描述】:

我在三个 js 中有一个浮动飞机。它是双面的,我的相机围绕场景移动和旋转。我的问题是,给定平面的位置和旋转以及相机的位置和旋转,我怎么知道我在看平面的哪一侧?

我的旧解决方案是这样(检查相机到飞机前后的距离),但显然这不是最好的解决方案。

this.back.matrixWorldNeedsUpdate = true;
this.front.matrixWorldNeedsUpdate = true;

this.d1 = (new THREE.Vector3()).getPositionFromMatrix( this.back.matrixWorld ).distanceTo(cameraSystem.camera.position);
this.d2 = (new THREE.Vector3()).getPositionFromMatrix( this.front.matrixWorld ).distanceTo(cameraSystem.camera.position);

if((this.d1 - this.d2) < 0)'Facing the back Side'
else 'Facing the front Side'

谢谢!

【问题讨论】:

  • 您可以检查两个向量之间的角度:相机指向的向量和平面的法线。
  • 非常感谢,希望我可以投票作为答案以获得更多荣誉! :)

标签: 3d three.js


【解决方案1】:

太好了,感谢 George 和 three.js set and read camera look vector,此代码有效

var planeVector = (new THREE.Vector3( 0, 0, 1 )).applyQuaternion(planeMesh.quaternion);
var cameraVector = (new THREE.Vector3( 0, 0, -1 )).applyQuaternion(camera.quaternion );

if(planeVector.angleTo(cameraVector)>Math.PI/2) 'facing front'
else 'facing back'

【讨论】:

  • 这通常是不正确的,因为您没有考虑相机位置。 (如果它有效,那只是因为您假设相机可以“看到”飞机。)相反,您需要测试相机在飞机的哪一侧。为此,您需要获取 (1) 平面在世界空间中的法线和 (2) 从旋转平面中的任何点或顶点到世界空间中相机位置的向量的点积。如果点积 > 0,则相机位于平面的正面。然后,如果相机可以“看到”飞机,那么它就是在看飞机的前部。
  • 飞机是否在视线内对我来说并不重要……我想。相反,我只是想不管飞机是否在视野中,相机可能会看到哪一侧。那有意义吗?如果它在相机后面,我也不关心这个物体发生了什么。 (感谢您的澄清,尽管我稍后会需要它
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 2011-06-06
  • 2017-05-09
相关资源
最近更新 更多