How to get Orientation of Camera in THREE.js

后端 未结 3 863
后悔当初
后悔当初 2020-12-05 21:21

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 listen

相关标签:
3条回答
  • 2020-12-05 22:07

    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;
    

    }

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 22:13

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

    camera.getWorldDirection()
    

    to get the camera orientation vector.

    0 讨论(0)
提交回复
热议问题