@threlte/extras

<AnimatedSpriteMaterial>

Provides animation tools for spritesheets.

This material is most easily used by passing it a spritesheet URL and a JSON metadata file URL.

Currently, JSON metadata using Aseprite’s hash export format is supported.

Animation names from tags can be used to transition to specific animations in the spritesheet.

<T.Sprite>
  <AnimatedSpriteMaterial
    animation="Idle"
    textureUrl="./player.png"
    dataUrl="./player.json"
  />
</T.Sprite>

If no metadata file is provided, additional props must be passed to run an animation:

  • totalFrames, if the spritesheet is only a single row.
  • totalFrames, rows, and columns, otherwise.
<T.Sprite>
  <AnimatedSpriteMaterial
    textureUrl="./fire.png"
    totalFrames={14}
    rows={4}
    columns={4}
  />
</T.Sprite>

Additionally, if a sheet with no JSON supplied has multiple animations, start and end frames must be passed to run an animation within the sheet.

<T.Sprite>
  <AnimatedSpriteMaterial
    textureUrl="./fire.png"
    totalFrames={14}
    rows={4}
    columns={4}
    startFrame={4}
    endFrame={8}
  />
</T.Sprite>

<AnimatedSpriteMaterial> can be attached to a <T.Sprite> as well as a <T.Mesh>.

In the case of a Mesh parent a MeshBasicMaterial will be used by default, instead of a SpriteMaterial when attached to a Sprite. A custom depth material will be attached when parented to a mesh to support shadows.

Any other material type can be used as well.

<T.Mesh>
  <AnimatedSpriteMaterial
    is={THREE.MeshStandardMaterial}
    textureUrl="./fire.png"
    totalFrames={14}
  />
</T.Mesh>

Component Signature

Props

name
type
required
default
description

textureUrl
string
yes
The URL of the spritesheet texture image.

alphaTest
number
no
0.1
Sets the alpha value to be used when running an alpha test.

animation
string
no
The current playing animation name.

autoplay
boolean
no
true
Controls whether or not to automatically run an animation on load.

columns
number
no
The number of columns in the spritesheet.

dataUrl
string
no
The URL of the spritesheet JSON.

delay
number
no
0
Delay the start of the animation in ms.

endFrame
number
no
totalFrames
The end frame of the current animation.

filter
"nearest" | "linear"
no
"nearest"
The texture filtering applied to the spritesheet.

flipX
boolean
no
false
Whether or not the Sprite should flip sides on the x-axis.

fps
boolean
no
10
The desired frames per second of the animation.

loop
boolean
no
true
Whether or not the current animation should loop.

rows
number
no
1
The number of rows in the spritesheet.

startFrame
number
no
0
The start frame of the current animation.

totalFrames
number
no
rows * columns - 1
The total number of frames in the spritesheet.

transparent
boolean
no
true
Whether or not the material should be transparent.

Events

name
payload
description

load
void
Fires when all resources have loaded.

start
void
Fires when an animation starts.

end
void
Fires when an animation ends.

loop
void
Fires when an animation loop completes.

Bindings

name
type

play
() => void

pause
() => void