javascript - 使用 ThreeJS 将相机朝它所面对的方向移动

标签 javascript vector three.js

假设我在以下位置放置了一个摄像头:

{
  x: 10,
  y: 30,
  z: 20
}

它的旋转是:

{
  x: 0.1,
  y: 0.2,
  z: 0.3
}

我想将相机从其当前位置朝它所面对的方向移动 1 个单位。

如何计算相机的新位置?

我正在使用 ThreeJS。

最佳答案

THREE.Camera 类扩展了 THREE.Object3D 类。 您可以使用 the getWorldDirection method from the base class 获取相机方向:

It returns a vector representing the direction in which the camera is looking, in world space.

当您有了这个direction 向量时,您可以使用它来将相机移动到所需的方向:

var direction = new THREE.Vector3();
camera.getWorldDirection( direction );

要在那个方向只移动 1,你可以简单地做:

camera.position.add( direction );

如果你想移动更多,你可以使用例如 the multiplyScalar method from the THREE.Vector3 class并乘以所需的距离。

distance = 10;
camera.position.add( direction.multiplyScalar(distance) );

关于javascript - 使用 ThreeJS 将相机朝它所面对的方向移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38052621/

相关文章:

javascript - AngularJS ng-重复父指令中的数据?

javascript - 什么时候需要转义正则表达式字符集中的字符([] 内)?

javascript - 当表单字段发生更改且未保存时提示用户

javascript - 将 Three.js 骨骼动画更新到新的基于混合器的系统

javascript - 添加参数或添加数组?

c++ - 使用 vector 的合并排序 C++

c++ - 如何将 vector<char*> 转换为 vector<string>/string

c++ - C++ 和字符串 vector 中的内存泄漏或其他问题

three.js - three.js ShaderLib 中有哪些着色器?

javascript - 阴影不适用于 THREE.js