【发布时间】:2021-03-08 19:08:24
【问题描述】:
camera.lookAt(myObject) 将立即将 three.js 相机旋转到给定对象。
我想使用 gsap 为这个旋转设置动画。我可以使用 gsap 为相机位置的变化设置动画,但是下面的相机旋转代码什么也不做。
const targetOrientation = myObject.quaternion.normalize();
gsap.to({}, {
duration: 2,
onUpdate: function() {
controls.update();
camera.quaternion.slerp(targetOrientation, this.progress());
}
});
如何以这种方式为相机旋转设置动画?
好的,现在已修复。主要问题是我的 render() 函数中有一个 controls.update() 行。轨道控件不适用于相机旋转,因此您需要确保它们在动画期间完全禁用。
我修改后的代码,包括旋转和位置动画:
const camrot = {'x':camera.rotation.x,'y':camera.rotation.y,'z':camera.rotation.z}
camera.lookAt(mesh.position);
const targetOrientation = camera.quaternion.clone().normalize();
camera.rotation.x = camrot.x;
camera.rotation.y = camrot.y;
camera.rotation.z = camrot.z;
const aabb = new THREE.Box3().setFromObject( mesh );
const center = aabb.getCenter( new THREE.Vector3() );
const size = aabb.getSize( new THREE.Vector3() );
controls.enabled = false;
const startOrientation = camera.quaternion.clone();
gsap.to({}, {
duration: 2,
onUpdate: function() {
camera.quaternion.copy(startOrientation).slerp(targetOrientation, this.progress());
},
onComplete: function() {
gsap.to( camera.position, {
duration: 8,
x: center.x,
y: center.y,
z: center.z+4*size.z,
onUpdate: function() {
camera.lookAt( center );
},
onComplete: function() {
controls.enabled = true;
controls.target.set( center.x, center.y, center.z);
}
} );
}
});
【问题讨论】: