Skeletal Animation
Work with rigged character animations:
// Import a rigged model
BABYLON.SceneLoader.ImportMesh("", "models/", "character.glb", scene, (meshes, particleSystems, skeletons, animationGroups) => {
const character = meshes[0];
const skeleton = skeletons[0];
// Play an animation by name
const idleAnim = animationGroups.find(a => a.name === "Idle");
const walkAnim = animationGroups.find(a => a.name === "Walking");
const runAnim = animationGroups.find(a => a.name === "Running");
if (idleAnim) idleAnim.play(true);
Switch between animations with blending:
// Later, switch animations with blending
document.getElementById("walk").addEventListener("click", () => {
idleAnim.stop();
walkAnim.play(true);
walkAnim.speedRatio = 1.0;
});
document.getElementById("run").addEventListener("click", () => {
walkAnim.stop();
runAnim.play(true);
runAnim.speedRatio = 1.5;
});
Access skeleton bones for fine control:
// Access skeleton bones for fine control
const rightArm = skeleton.bones.find(b => b.name === "RightArm");
if (rightArm) {
// Override bone rotation
rightArm.rotation.z += Math.PI / 8;
}
});