【发布时间】:2012-07-24 19:39:34
【问题描述】:
在 Three.js 中,我想使用 THREE.quaternion 让相机对象旋转到所选对象。
我确实在网上搜索过,但没有找到有关如何使用此四元数类的示例/演示或文档。
我用下面的代码试试运气:
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 10;
camera.position.z = 0;
camera.position.x = radious;
camera.useQuaternion = true;
// I did use the TrackballTrackballControls. Maybe it causes the problem so I put it here
controls = new THREE.TrackballControls( camera, document.getElementById(_canvasElement) );
// function to make the camera rotate to the object
function focusOn3DObject(obj){
obj.useQuaternion = true;
obj.quaternion = new THREE.Quaternion(obj.position.x, obj.position.y, obj.position.z, 1);
var newQuaternion = new THREE.Quaternion();
THREE.Quaternion.slerp(camera.quaternion, obj.quaternion, newQuaternion, 0.07);
camera.quaternion = newQuaternion;
}
但它不起作用。我错过了什么? 请帮忙。提前致谢。
【问题讨论】:
-
请注意,在较新的 THREE.js 版本中,
useQuaternion已被删除。该库现在默认使用四元数。 -
另请注意,今天(2015 年 8 月)三个 Camera 对象有一个方法 .lookAt(vector3) - 它接收一个 Vector3 与要查看的对象的位置。您可以传递任何 object3d.position 作为参数,以便相机查看它。看看:threejs.org/docs/index.html#Reference/Cameras/Camera
标签: javascript three.js