Animation Transitions
Transition seamlessly between GLTF animations.
<script lang="ts">
import { Pane, Button } from 'svelte-tweakpane-ui'
import { Canvas } from '@threlte/core'
import Scene from './Scene.svelte'
import { buttonIdle, buttonWalk, buttonRun } from './state'
</script>
<Pane
title="Transitions"
position="fixed"
>
<Button
title="Idle"
on:click={() => {
$buttonIdle = !$buttonIdle
}}
/>
<Button
title="Walk"
on:click={() => {
$buttonWalk = !$buttonWalk
}}
/>
<Button
title="Run"
on:click={() => {
$buttonRun = !$buttonRun
}}
/>
</Pane>
<div>
<Canvas>
<Scene />
</Canvas>
</div>
<style>
div {
position: relative;
height: 100%;
width: 100%;
}
</style>Explanation
glTF is a comprehensive file format for 3D models, and it supports animations. In this example, we extract the animations from the gltf file, play them, and crossfade between them.
What is the code doing?
-
Extract the variables
gltfandactions;const { gltf, actions } = useGltfAnimations() -
Bind
gltfto the<GLTF>component,
<GLTF
bind:gltf={$gltf}
url="https://threejs.org/examples/models/gltf/Xbot.glb"
/>this causes actions to be populated with an array of the available animations in that gltf file
run console.log(Object.entries($actions)) to see the available action strings, and the shape of
the animation object. By doing this, you’ll discover that this example is only using 3 of the 7
available animations attached to this gltf file.
- selecting a different animation calls
transitionTofunction, which crossfades between the two animations