How to tell if the camera is looking at the face of a plane

我们两清 提交于 2020-01-25 08:11:27

问题


I have a floating plane in three js. It's double sided and my camera moves and rotates about the scene. My question is, given the position and rotation of the plane and position and rotation of the camera, how can I tell which side of the plane I am looking at?

My old solution is this (check the distance from camera to front and back of the plane) but obviously this isn't the best solution.

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'

Thanks!


回答1:


Great, Thanks to George and three.js set and read camera look vector, this code works

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'


来源:https://stackoverflow.com/questions/18058776/how-to-tell-if-the-camera-is-looking-at-the-face-of-a-plane

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