Skip to content

animations

animations is an abstraction around THREE.AnimationMixer that provides type-safe animations handling.

Usage

import { animations } from 'angular-three-soba/misc';
export class MyCmp {
protected gltf = gltfResource(() => 'my/gltf.glb');
protected animations = animations(this.gltf.value, this.gltf.scene);
constructor() {
effect(() => {
if (!this.animations.isReady) return;
this.animations.actions; //
})
}
}

isReady is a signal-getter (getter that returns a signal) that indicates whether the animations are ready to use. This also acts as a type-guard to ensure the animations are typed correctly. You should always check isReady before accessing actions.

Providing generics

With GLTF type

Usually, you will want to provide the GLTF type to gltfResource then animations will be able to infer the animations type from this.gltf.value

import { NgtsAnimationClips } from 'angular-three-soba/misc';
interface MyGLTF extends GLTF {
animations: NgtsAnimationClips<'Dance'>[];
}
export class MyCmp {
protected gltf = gltfResource<MyGLTF>(() => 'my/gltf.glb');
protected animations = animations(this.gltf.value, this.gltf.scene);
// this.animations.actions.Dance is strongly-typed
}

With animations type

If you don’t want to or don’t have the GLTF type, you can provide the animations type directly to animations

import { NgtsAnimation } from 'angular-three-soba/misc';
export class MyCmp {
protected animations = animations<NgtsAnimation<'Dance'>>(...);
// this.animations.actions.Dance is strongly-typed
}

Automatic cleanup

The animations are automatically cleaned up when the component is destroyed:

  • Cached actions are cleared
  • All actions are stopped
  • Actions are uncached from the mixer

Arguments

name type description required
animations () => NgtsAnimation<TAnimation> | undefined | null A signal/function that returns either an array of AnimationClips or an object with animations property containing an array of AnimationClips yes
object ElementRef<Object3D> | Object3D | (() => ElementRef<Object3D> | Object3D | undefined | null) The Object3D instance that will be animated yes
options { injector?: Injector } Optional configuration object with injector no

Returns

typedescription
NgtsAnimationApiThe animations API object

Properties

nametypedescription
clipsT[]Array of animation clips
mixerTHREE.AnimationMixerThe AnimationMixer instance
namesT['name'][]Array of animation names
isReadybooleanWhether the animations are initialized and ready to use
actions{ [key in T['name']]: THREE.AnimationAction }Map of animation names to their corresponding AnimationAction (only available when isReady is true)