0

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!

Jack Franzen
  • 768
  • 7
  • 21

1 Answers1

2

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'
Community
  • 1
  • 1
Jack Franzen
  • 768
  • 7
  • 21
  • This is not in general correct, as you are not taking the camera position into consideration. (If it works, it is only because you are assuming that the camera can "see" the plane.) Instead, you need to test what side of the plane the camera is on. To do that, you need to take the dot-product of (1) the plane's normal in world space and (2) a vector from any point or vertex in the rotated plane to the camera position in world space. If the dot-product is > 0, then the camera is on the front side of the plane. Then, if the camera can "see" the plane, it is looking at the plane's front. – WestLangley Aug 05 '13 at 15:40
  • It doesn't matter to me if the plane is in sight... I think. Rather, I just wanted which side the camera might be seeing regardless of whether the plane is in vision. Does that make sense? And I also don't care what's going on with this object if it's behind the camera. (Thanks for the clarification though I am going to need that later on <3) – Jack Franzen Aug 05 '13 at 16:55