我正在创建这个程序,你可以点击一个对象,缩放它,然后按住鼠标右键并拖动,从各个角度查看它。我需要相机绕过物体,而不是用相机看着它旋转物体。老实说,我根本不知道如何数学出来!
对于测试,已经有一个游戏对象,其中包含我们选择并正在查看的xyz
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。
我在这里更新场景:
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?!?!
答案 0 :(得分:4)
正如gaitat所提到的,轨迹球控制是开始使用许多可配置参数以使相机旋转/旋转变得容易的最佳位置。这种方法的一个巨大潜在好处(特别是对于您的项目)是避免"万向节锁定"这是使用旋转时非常沮丧的根源。这是一个可以帮助您使用轨迹球控件和Orbitcontrols的链接:
Rotate camera in Three.js with mouse
另一种选择是在动画循环中自己设置摄像机坐标,这实际上非常简单:
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;
...
}
另一种选择是将相机连接到枢轴物体,然后旋转枢轴:
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
如果您选择此选项,请注意相机对象位于相机枢轴空间中,并且进一步操作可能更具挑战性。