GltfAnimation


@RequiresApi(value = 26)
public final class GltfAnimation


GltfAnimation represents an animation in a GltfModelEntity.

Multiple animations can play simultaneously. If multiple animations affect the same node, the animation processed last takes precedence. Specifically, only the state of the last processed animation is reflected in the scene for a given frame. Updates from other animations (such as node transforms) for the same target will be overwritten.

A GltfAnimation cannot be used once its associated GltfModelEntity has been destroyed.

Summary

Nested types

public final class GltfAnimation.AnimationState

Specifies the current animation state of this GltfAnimation.

Public methods

final void

Registers a listener to be invoked on the main thread when the animation state of the GltfAnimation changes.

final void

Registers a listener to be invoked when the animation state of this GltfAnimation changes.

final @NonNull GltfAnimation.AnimationState

The current state of this animation.

final @NonNull Duration

The duration of this animation.

final int

The index of this animation in the source glTF model.

final String

The name of this animation, or null if the animation is unnamed.

final void

Pauses this animation.

final void

Unregisters a previously registered animation state update listener.

final void

Resumes this animation.

final void

Seeks the animation to a specific time position.

final void
@MainThread
setSpeed(float speed)

Sets the playback speed for this animation.

final void

Starts playing this animation.

final void

Stops this animation.

Public methods

addAnimationStateListener

Added in 1.0.0-alpha13
public final void addAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Registers a listener to be invoked on the main thread when the animation state of the GltfAnimation changes.

Parameters
@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to invoke when the state changes. It receives the new AnimationState.

addAnimationStateListener

Added in 1.0.0-alpha13
public final void addAnimationStateListener(
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Registers a listener to be invoked when the animation state of this GltfAnimation changes.

Parameters
@NonNull Executor executor

The executor on which the listener will be invoked.

@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to invoke when the state changes. It receives the new AnimationState.

getAnimationState

Added in 1.0.0-alpha13
@MainThread
public final @NonNull GltfAnimation.AnimationState getAnimationState()

The current state of this animation.

getDuration

Added in 1.0.0-alpha13
public final @NonNull Duration getDuration()

The duration of this animation.

getIndex

Added in 1.0.0-alpha13
public final int getIndex()

The index of this animation in the source glTF model.

getName

Added in 1.0.0-alpha13
public final String getName()

The name of this animation, or null if the animation is unnamed.

pause

Added in 1.0.0-alpha13
@MainThread
public final void pause()

Pauses this animation.

This freezes the animation at the current frame and transitions the animation state to AnimationState.PAUSED. Use resume to continue playback.

Note: Calling start while in the AnimationState.PAUSED state will reset the playback time to animationOptions.seekStartTime and transition the state to AnimationState.PLAYING.

removeAnimationStateListener

Added in 1.0.0-alpha13
public final void removeAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Unregisters a previously registered animation state update listener.

Parameters
@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to remove.

resume

Added in 1.0.0-alpha13
@MainThread
public final void resume()

Resumes this animation.

This continues the animation from the point where it was paused and transitions the animation state to AnimationState.PLAYING.

Note: Calling resume while in the AnimationState.PLAYING and AnimationState.STOPPED state will have no effect.

seekTo

Added in 1.0.0-alpha13
@MainThread
public final void seekTo(@NonNull Duration time)

Seeks the animation to a specific time position.

Note: This call is only valid during the AnimationState.PLAYING and AnimationState.PAUSED states. Calling this method while in the AnimationState.STOPPED state has no effect.

The behavior depends on whether the animation is looping:

  • Looping Enabled: The time is treated as cyclical. Values exceeding the duration will wrap around (modulo arithmetic).

  • Looping Disabled: The time is clamped to the valid playback range. Values less than GltfAnimationStartOptions.seekStartTime clamp to the start. Values exceeding the duration (or end time) clamp to the end.

Interaction with GltfAnimationStartOptions.seekStartTime:

Parameters
@NonNull Duration time

The offset from the beginning of the animation.

Throws
IllegalArgumentException

if time is negative.

setSpeed

Added in 1.0.0-alpha13
@MainThread
public final void setSpeed(float speed)

Sets the playback speed for this animation.

The speed multiplier determines the playback rate:

  • 1.0: Normal speed.

  • > 1.0: Faster playback.

  • > 0.0 and < 1.0: Slower playback (e.g., 0.5 is half speed).

  • 0.0: Freezes the animation at the current frame while keeping it active (unlike pausing).

  • < 0.0: Plays the animation in reverse.

Note: This call is only valid during the AnimationState.PLAYING and AnimationState.PAUSED states. Calling this method while in the AnimationState.STOPPED state will have no effect.

Parameters
float speed

The playback rate multiplier.

start

Added in 1.0.0-alpha13
@MainThread
public final void start(@NonNull GltfAnimationStartOptions options)

Starts playing this animation.

This transitions the animation state to AnimationState.PLAYING.

Parameters
@NonNull GltfAnimationStartOptions options

The options that describe how the glTF model will be animated. Using default values if not specified.

stop

Added in 1.0.0-alpha13
@MainThread
public final void stop()

Stops this animation.

This resets the playback time to 0 and transitions the animation state to AnimationState.STOPPED. If this animation is not currently playing or pausing, this method has no effect.