在没有轨迹球控制或其他相机控制库的情况下缩放三个相机中的相机 [英] zooming camera in threeJS without trackball controls or other camera control library

查看:116
本文介绍了在没有轨迹球控制或其他相机控制库的情况下缩放三个相机中的相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用threeJS控制场景中的相机。我目前使用键盘上的左右键将摄像机设置为围绕我的物体绕圈运行。但有谁知道我会如何缩放?这是我目前的代码:

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

}


推荐答案

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

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

参见: 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.

这篇关于在没有轨迹球控制或其他相机控制库的情况下缩放三个相机中的相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆