javascript 如何在 THREE.js 中获取相机的方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14023764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:31:15  来源:igfitidea点击:

How to get Orientation of Camera in THREE.js

javascripthtmlthree.jsweb-audio-api

提问by Cabbibo

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

我正在使用 THREE.JS 和 Web Audio API 创建一个 3d 游戏。我遇到的问题之一是我想使用网络音频监听器方向,并将监听器定义为相机,其位置和方向不断更新

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 ?

通过使用 camera.rotation.x, camera.rotation.y, camera.rotation.z 创建单位向量是否可行?

or is there an even easier way?

或者有更简单的方法吗?

Thanks so much for your time!

非常感谢您的时间!

回答by WestLangley

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

您想知道相机在世界空间中观察的方向。

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

相机空间中,相机位于原点并向下看它的负 z 轴。

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

编辑:更新了three.js r.57

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

编辑:另见:three.js set and read camera look vector

回答by pkout

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

在修订版 69 中(不确定它是在哪个修订版中引入的),您可以调用

camera.getWorldDirection()

to get the camera orientation vector.

获取相机方向向量。

回答by Joris

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)

如果您的相机是玩家对象的子对象,例如在 FPS 游戏中。对玩家(相机的父级)进行相同的计算,并使用它(Bullit 获得正确的方向,在这种情况下来自 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;

}

}