如何在 THREE.js 中获取相机的方向 [英] How to get Orientation of Camera in THREE.js

查看:86
本文介绍了如何在 THREE.js 中获取相机的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

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

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?

非常感谢您的时间!

推荐答案

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

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

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

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();

更新了three.js r.57

Updated for three.js r.57

另见:three.js set并读取相机外观向量

这篇关于如何在 THREE.js 中获取相机的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆