如何围绕物体旋转THREE.PerspectiveCamera [英] How to rotate a THREE.PerspectiveCamera around on object

查看:137
本文介绍了如何围绕物体旋转THREE.PerspectiveCamera的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建这个程序,你可以点击一个对象,缩放它,然后按住鼠标右键并拖动,从各个角度查看它。我需要相机绕过物体,而不是用相机看着它旋转物体。老实说,我根本不知道如何数学出来!

I am making this program where you can click on an object, zoom to it, then look at it from all angles by holding the right mouse button and dragging. I need the camera to be going around the object, not rotate the object with the camera looking at it. I honestly just have no idea how to math it out!

对于测试已经有一个游戏对象,我们选择了xyz并正在查看

For testing there is already a game object with an xyz we have selected and are looking at

var g = new GameObject(500, 0, 0);//The game object with xyz
this.selected = g;//set selected to g

//Create and set the camera
this.camera = new THREE.PerspectiveCamera(45, w/h, 1, 10000);
this.camera.position.x = 0;
this.camera.position.y = 0;
this.camera.position.z = 0;

//set camera to look at the object which is 500 away in the x direction
this.camera.lookAt(new THREE.Vector3(this.selected.x, this.selected.y, this.selected.z));

因此相机与物体之间的半径为500,在选择和旋转时,相机应始终距离500。

So the radius between the camera and the object is 500 and while selected and rotating, the camera should always be 500 away.

我在这里更新场景:

Main.prototype.update = function(){

    this.renderer.render(this.scene, this.camera);//scene is just some ambient lighting

    //what to do when mouse right is held down
    if(this.rightMouseDown){

        //placeholder functionality, needs to rotate around object based on mouse movements
        this.camera.position.x -= 5;

    }
}

如何旋转此相机g,半径为500?!?!

How do I rotate this camera around g with a radius of 500?!?!

推荐答案

正如gaitat所提到的,轨迹球控制是最好的开始,许多可配置使摄像机旋转/旋转容易的参数。这种方法的一个巨大潜在好处(特别是对于你的项目)是避免万向节锁定,这是使用旋转时非常沮丧的根源。这是一个可以帮助您使用轨迹球控件和Orbitcontrols的链接:

As gaitat mentioned, trackball controls are the best place to start with many configurable parameters to make camera rotation/revolution easy. One enormous potential benefit of this method ( especially for your project ) is avoiding "gimbal lock" which is the source of much frustration when working with rotations. Here's a link that might help you with Trackball controls and Orbitcontrols:

使用鼠标在Three.js中旋转相机

另一种选择是自己设置相机坐标动画循环实际上非常简单:

Another option would be setting camera coordinates yourself in the animation loop which is actually quite simple:

var angle = 0;
var radius = 500; 

function animate() {
...
// Use Math.cos and Math.sin to set camera X and Z values based on angle. 
camera.position.x = radius * Math.cos( angle );  
camera.position.z = radius * Math.sin( angle );
angle += 0.01;
...
}

另一种选择是将相机连接到一个数据透视对象,只需旋转枢轴:

Another option would be to connect the camera to a pivot object and just rotate the pivot:

var camera_pivot = new THREE.Object3D()
var Y_AXIS = new THREE.Vector3( 0, 1, 0 );

scene.add( camera_pivot );
camera_pivot.add( camera );
camera.position.set( 500, 0, 0 );
camera.lookAt( camera_pivot.position );
...
camera_pivot.rotateOnAxis( Y_AXIS, 0.01 );    // radians

如果您选择此选项,请注意相机对象位于相机枢轴空间中,进一步操纵可能更具挑战性。

If you pursue this option, be aware that the camera object is in "camera pivot space", and might be more challenging to manipulate further.

这篇关于如何围绕物体旋转THREE.PerspectiveCamera的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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