【问题标题】:Moving the camera in the direction it's facing, with ThreeJS使用 ThreeJS 将相机朝它所面对的方向移动
【发布时间】:2016-10-29 09:19:35
【问题描述】:

假设我有一个摄像头放置在:

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

它的旋转是:

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

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

如何计算相机的新位置?

我正在使用 ThreeJS。

【问题讨论】:

    标签: javascript vector three.js


    【解决方案1】:

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

    它返回一个向量,表示相机在世界空间中的观察方向。

    当你有这个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) );
    

    【讨论】:

    • 三个现在抱怨“.getWorldDirection() 目标现在是必需的”,所以你可以传递一个 Vector3 然后 `direction = camera.getWorldDirection(vector);'将结果返回到“方向”,并将其放入新向量中。更改的原因由 WestLangely 撰写,他在下面发布了有效的答案,在这里:github.com/mrdoob/three.js/issues/12231
    • @kellycode 感谢您的评论。我更新了答案,因此它可以使用最新的 three.js 版本。我还更新了损坏的链接。
    【解决方案2】:

    相机向下看它的局部负 z 轴。因此,要将相机朝它所面对的方向移动,请使用以下方法:

    camera.translateZ( - distance );
    

    这是最有效的方法。

    three.js r.78

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多