如何使用 gsap 为 camera.lookAt 制作动画?

How to animate camera.lookAt using gsap?

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

试试看:

let mesh, renderer, scene, camera, controls;

init();
animate();

function init() {

    // renderer
    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.setPixelRatio( window.devicePixelRatio );
    document.body.appendChild( renderer.domElement );

    // scene
    scene = new THREE.Scene();
    
    // camera
    camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
    camera.position.set( 20, 10, 20 );
    camera.lookAt( 0, 0, 0 );

    // ambient
    scene.add( new THREE.AmbientLight( 0x222222 ) );
    
    // light
    const light = new THREE.DirectionalLight( 0xffffff, 1 );
    light.position.set( 20,20, 0 );
    scene.add( light );
    
    // axes
    scene.add( new THREE.AxesHelper( 20 ) );

    // geometry
    const geometry = new THREE.SphereBufferGeometry( 5, 12, 8 );
    
    // material
    const material = new THREE.MeshPhongMaterial( {
        color: 0x00ffff, 
        flatShading: true,
        transparent: true,
        opacity: 0.7,
    } );
    
    // mesh
    mesh = new THREE.Mesh( geometry, material );
    scene.add( mesh );
        
    const startOrientation = camera.quaternion.clone();
    const targetOrientation = mesh.quaternion.clone().normalize();
        
    gsap.to( {}, {
        duration: 2,
        onUpdate: function() {
            camera.quaternion.copy(startOrientation).slerp(targetOrientation, this.progress());
        }
    } );
    
}

function animate() {

    requestAnimationFrame( animate );
    renderer.render( scene, camera );

}
body {
  margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.123/build/three.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.4.0/gsap.min.js"></script>

您必须确保始终以起始方向而不是 camera.quaternion 调用 Quaternion.slerp()。否则插值不正确。