Three.js中camera的lookAt函数与OrbitControls冲突

时间:2022-02-17 05:30:14

今天想把整个场景往下沉一点,想要照相机也跟着沉,于是设置了

camera.lookAt(new THREE.Vector3(0, -1000, 0));

结果发现视角并没有发生改变,在网上查了一下发现是与three.js的OrbitControls控件有关系,
OrbitControls的源代码中可以看到,cameralookAt对象被更改了:

THREE.OrbitControls = function ( object, domElement ) {
...
// "target" sets the location of focus, where the object orbits around
this.target = new THREE.Vector3();
...
var scope = this;
...
scope.object.lookAt( scope.target );
...
};

由于targetnew THREE.Vector3(),而不是cameralookAt的向量,不管怎么设置cameralookAt函数都没有用。
因此需要设置OrbitControls.target为目标向量,比如

controls.target = new THREE.Vector3(0,-1000,0);

现在就可以看到视角更新为你想要的视角啦。