How to get Orientation of Camera in THREE.js

被刻印的时光 ゝ 提交于 2019-11-27 04:31:24

问题


I am creating a 3d game using THREE.JS and the Web Audio API. One of the problems I am having is that I want to use the web audio Listener orientation, and define the listener to be the camera, whose position and direction are constantly being updated

My question, is there anyway to easily get the vector direction of a THREE camera?

I was trying to calculate it by using the old camera position, and using the velocity vectors to calculate which way it is facing, but this won't work when the camera is standing still...

Would it be feasible to create a unit vector by getting using camera.rotation.x, camera.rotation.y, camera.rotation.z ?

or is there an even easier way?

Thanks so much for your time!


回答1:


You want to know the direction in world space in which the camera is looking.

In camera space, the camera is located at the origin and is looking down it's negative z-axis.

Pick a point in front of the camera in camera space:

var pLocal = new THREE.Vector3( 0, 0, -1 );

Now transform that point into world space:

var pWorld = pLocal.applyMatrix4( camera.matrixWorld );

You can now construct the desired direction vector:

var dir = pWorld.sub( camera.position ).normalize();

EDIT: Updated for three.js r.57

EDIT: Also see: three.js set and read camera look vector




回答2:


In revision 69 (not sure in what revision it was introduced), you can call

camera.getWorldDirection()

to get the camera orientation vector.




回答3:


if your camera is a child of the player object .e.g. in FPS game. Do the same calculation on the player (parent of camera) , and use this (the Bullit gets the right direction, in this case from obj)

Bullit.prototype.getDirection=function(obj){
this.position.add(obj.position);
var pLocal = new THREE.Vector3( 0, 0, -1 );
var pWorld = pLocal.applyMatrix4( obj.matrixWorld );
var dir = pWorld.sub( obj.position ).normalize();
this.direction=dir;

}



来源:https://stackoverflow.com/questions/14023764/how-to-get-orientation-of-camera-in-three-js

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