Javascript 没有轨迹球控件或其他相机控制库的threeJS中的缩放相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10354574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
zooming camera in threeJS without trackball controls or other camera control library
提问by mheavers
I'm trying to use threeJS to control a camera in my scene. I currently have the camera set up to orbit in a circle around my object using the left and right keys on my keyboard. But does anyone know how I would zoom? Here's my current code:
我正在尝试使用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);
}
回答by Juan Mellado
If you want a real zoom, without moving the camera, then you can play with the field of view (fov) parameter of the camera:
如果您想要真正的变焦,而无需移动相机,那么您可以使用相机的视场 (fov) 参数:
camera.fov *= zoomFactor;
camera.updateProjectionMatrix();
See: http://jsfiddle.net/bvcCB/87/
见:http: //jsfiddle.net/bvcCB/87/
If you want to move the camera near (or far) of the target, then calculate the vector from the camera position to the target, and move the camera position along that vector.
如果要将相机移近(或远离)目标,则计算从相机位置到目标的向量,然后沿该向量移动相机位置。
回答by chrisarton
From r69 you can now use camera.zoom:
从 r69 开始,您现在可以使用 camera.zoom:
camera.zoom = zoomFactor;
camera.updateProjectionMatrix();