Javascript 在不使用轨迹球控件或其他摄影机控件库的情况下,以3JS缩放摄影机

Javascript 在不使用轨迹球控件或其他摄影机控件库的情况下,以3JS缩放摄影机,javascript,3d,three.js,Javascript,3d,Three.js,我正在尝试使用threeJS控制场景中的摄影机。目前,我已将相机设置为使用键盘上的左右键围绕对象旋转一圈。但有人知道我会如何变焦吗?以下是我当前的代码: camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000 ); camera.position.set(0,20,35); var rotSpeed = .02; function checkRotation(){

我正在尝试使用threeJS控制场景中的摄影机。目前,我已将相机设置为使用键盘上的左右键围绕对象旋转一圈。但有人知道我会如何变焦吗?以下是我当前的代码:

camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(0,20,35);
var rotSpeed = .02;

function checkRotation(){

    var x = camera.position.x,
        y = camera.position.y,
        z = camera.position.z;

    if (keyboard.pressed("left")){ //MH - find a way to do this in a switch statement 
        camera.position.x = x * Math.cos(rotSpeed) + z * Math.sin(rotSpeed);
        camera.position.z = z * Math.cos(rotSpeed) - x * Math.sin(rotSpeed);
    } else if (keyboard.pressed("right")){
        camera.position.x = x * Math.cos(rotSpeed) - z * Math.sin(rotSpeed);
        camera.position.z = z * Math.cos(rotSpeed) + x * Math.sin(rotSpeed);
    } else if(keyboard.pressed("up")){
        //zoom in
    } else if (keyboard.pressed("down")){
        //zoom out
    }

    camera.lookAt(scene.position);

}

如果希望在不移动摄影机的情况下进行真正的缩放,则可以使用摄影机的“视野”(fov)参数:

  camera.fov *= zoomFactor;
  camera.updateProjectionMatrix();
见:


如果要将相机移动到目标附近(或远处),则计算从相机位置到目标的矢量,并沿该矢量移动相机位置。

从r69开始,现在可以使用camera.zoom:

camera.zoom = zoomFactor;
camera.updateProjectionMatrix();

啊!!尝试fov,但忘记更新投影矩阵。谢谢