Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js Get Camera lookAt Vector

Tags:

three.js

I'm looking to translate the camera along its lookAt vector. Once I have this vector, I can do scalar translation along it, use that point to move the camera position in global coordinates and re-render. The trick is getting the arbitrary lookAt vector? I've looked at several other questions and solutions but they don't seem to work for me.

like image 379
Frank Miller Avatar asked May 13 '14 22:05

Frank Miller


2 Answers

You can't get the lookAtVector from the camera itself, you can however create a new vector and apply the camera rotation to that.

var lookAtVector = new THREE.Vector3(0,0, -1);
lookAtVector.applyQuaternion(camera.quaternion);
like image 199
Kevin Kuyl Avatar answered Nov 02 '22 19:11

Kevin Kuyl


The first choice should be cam.translateZ();

There is a second option as well. You can extract the lookAt vector from the matrix property of the camera object. You just need to take the elements corresponding to the local z-axis.

var lookAtVector = new THREE.Vector3(cam.matrix[8], cam.matrix[9], cam.matrix[10]);
like image 1
Isolin Avatar answered Nov 02 '22 17:11

Isolin