three.js OrbitControls。如何更新旋转相机?

three.js OrbitControls. How update rotation camera?

我使用OrbitControls,设置为camera.rotation.y = Math.PI/2controls.object.rotation.y = Math.PI/2
相机旋转,没关系。

controls.update()之后。相机返回其原始位置。鼠标移动时也会发生同样的情况。

如何更新相机的旋转度?


OrbitControls控制您的相机。

您不能独立于OrbitControls旋转相机。

我所做的是修改OrbitControls.js本身,以公开一个称为" rotateLeft"的内部方法,以便可以从我的代码中调用它。

将此添加到OrbitControls:

1
2
3
4
this.rotate = function(degrees) {
    rotateLeft(degrees);
    this.update();
}

,然后您可以手动执行controls.rotate( degrees * Math.PI / 180 )


OrbitControls使"注视"在target位置,因此仅旋转摄像机不会影响控件。但是,移动相机将会。

如果要基于旋转来设置摄像机的位置,则可以旋转摄像机,从摄像机获取前向矢量,并将其相对于目标位置进行定位。这是一个粗略的示例:

1
2
3
4
5
6
7
8
9
10
11
12
// ... rotate the camera and update the camera's matrices

// get the forward vector
const fwd = new THREE.Vector3(0,0,1);
fwd.applyMatrix3(cam.matrixWorld).normalize();

// ... generate an offset vector by multiplying the forward vector
// by a desired distance from the target

cam.position.set(controls.target).sub(offset);

controls.update();

希望有帮助!