创建关键帧动画AnimationClip
### 1. 创建关键帧动画`AnimationClip`
mesh.name = "Box";
const times = [0, 3, 6];
const values = [0, 0, 0, 100, 0, 0, 0, 0, 100];
const posKF = new THREE.KeyframeTrack('Box.position', times, values);
const colorKF = new THREE.KeyframeTrack('Box.material.color', [2, 5], [1, 0, 0, 0, 0, 1]);
const clip = new THREE.AnimationClip("test", 6, [posKF, colorKF]);
AnimationMixer播放关键帧动画AnimationClip
const mixer = new THREE.AnimationMixer(mesh);
const clipAction = mixer.clipAction(clip);
clipAction.play();
mixer.update()更新播放器AnimationMixer时间
### `mixer.update()`更新播放器`AnimationMixer`时间
function loop() {
requestAnimationFrame(loop);
const frameT = clock.getDelta();
mixer.update(frameT);
}
loop();
动画播放(暂停、倍速、循环)
const clipAction = mixer.clipAction(clip);
clipAction.play();
clipAction.loop = THREE.LoopOnce;
clipAction.loop = THREE.LoopOnce;
动画播放(拖动任意时间状态)
clipAction.time = 1;
clip.duration = 5;
clipAction.paused = true;
clipAction.time = 1;
clipAction.time = 3;
gui.add(clipAction, 'time', 0, 6).step(0.1);
解析外部模型关键帧动画
let mixer = null;
loader.load("../工厂.glb", function (gltf) {
model.add(gltf.scene);
mixer = new THREE.AnimationMixer(gltf.scene);
const clipAction = mixer.clipAction(gltf.animations[0]);
clipAction.play();
})
const clock = new THREE.Clock();
function render() {
requestAnimationFrame(render);
if (mixer !== null) {
mixer.update(clock.getDelta());
}
}
render();