在没有轨迹球控制或其他相机控制库的情况下缩放三个相机中的相机

mhe*_*ers 9 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(){

    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);

}
Run Code Online (Sandbox Code Playgroud)

Jua*_*ado 18

如果你想要一个真正的变焦,而不需要移动相机,那么你可以玩相机的视野(fov)参数:

  camera.fov *= zoomFactor;
  camera.updateProjectionMatrix();
Run Code Online (Sandbox Code Playgroud)

请参阅:http://jsfiddle.net/bvcCB/87/

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


chr*_*ton 15

从r69你现在可以使用camera.zoom:

camera.zoom = zoomFactor;
camera.updateProjectionMatrix();
Run Code Online (Sandbox Code Playgroud)