2012-04-27 75 views
9

我想用threeJS來控制我的場景中的相機。我現在使用鍵盤上的左右鍵將相機設置成圍繞我的物體繞着一圈。但有誰知道我會如何放大?這裏是我當前的代碼:3JS變焦相機沒有軌跡球控制或其他相機控制庫

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

見:相機

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

} 

回答

17

如果你想有一個真正的變焦,而無需移動相機,那麼你可以視場打(FOV)參數: http://jsfiddle.net/bvcCB/87/

如果您想移動相機靠近(或遠處)的目標,然後計算從相機位置到目標的矢量,並沿着該矢量移動相機位置。

+0

啊!嘗試了fov,但忘了更新投影矩陣。謝謝! – mheavers 2012-04-29 15:07:59

11

從R69您現在可以使用camera.zoom:

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

感謝'.updateProjectionMatrix()'! – LinusGeffarth 2017-09-04 19:55:10