Tuesday, June 4, 2024
Homepage · 3d
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  150] [ 6]  / answers: 1 / hits: 31654  / 12 Years ago, fri, april 27, 2012, 12:00:00

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:



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

}

More From » 3d

 Answers
23

If you want a real zoom, without moving the camera, then you can play with the field of view (fov) parameter of the camera:



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


See: 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.


[#85927] Thursday, April 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
halleyb

Total Points: 604
Total Questions: 96
Total Answers: 115

Location: Tokelau
Member since Wed, Oct 14, 2020
4 Years ago
halleyb questions
;