GUIDE // THREE.JS
How to play an explosion flipbook once in three.js and remove it
Record the time when your explosion starts, compute the frame from the time since then, and stop at the last frame instead of wrapping. Once the frame passes the end, remove the sprite from the scene and dispose of its material and its cloned texture, so a scene full of repeated blasts does not leak memory.
- Godot 4 Available
- Unity Manual setup
- Unreal Engine Manual setup
- Roblox Studio Manual setup
- three.js Available
A draft compile is from $0.05, and new accounts start with $5 of usage.
01 // HOW TO
Spawn and clean up a fireball
- Load the fireball sheet once with
loadAsyncand keep it as the base texture. - For each blast, clone the base texture so this sprite can show its own frame, and set its repeat to 1/8 by 1/8.
- Store the start time with the sprite, then each tick find the frame for the time since the start in the frame times from
layout.json. The fireball's 64 frames span 4 s and bunch up early, so a fixed rate would play the burst too slowly. - When the time passes the clip's end, remove the sprite and dispose of the clone and the material.
import * as THREE from 'three';
const FRAMES = 64;
const CLIP = 4; // fireball: its 64 frames span 4 s
const base = await new THREE.TextureLoader().loadAsync('/fx/fireball_rgba.png');
base.colorSpace = THREE.SRGBColorSpace;
const active = [];
export function spawnExplosion(scene, position, size, now) {
const map = base.clone();
map.needsUpdate = true;
map.repeat.set(1 / 8, 1 / 8);
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map, transparent: true, depthWrite: false }));
sprite.position.copy(position);
sprite.scale.set(size, size, 1);
scene.add(sprite);
active.push({ sprite, start: now });
}
// times: the 64 frame start times in seconds, from layout.json
export function updateExplosions(now, times) {
for (let i = active.length - 1; i >= 0; i--) {
const { sprite, start } = active[i];
const t = now - start;
if (t >= CLIP) {
sprite.removeFromParent();
sprite.material.map.dispose();
sprite.material.dispose();
active.splice(i, 1);
} else {
let frame = 0;
while (frame + 1 < FRAMES && times[frame + 1] <= t) frame++;
sprite.material.map.offset.set((frame % 8) / 8, 1 - (Math.floor(frame / 8) + 1) / 8);
}
}
}
Pass the same clock time, in seconds, as now to both functions, for example from clock.getElapsedTime(), and pass the frame times you read from the compile's layout.json as times.
02 // REAL OUTPUT
A shorter blast
Impact is a flash, thrown earth and a little smoke. Its frames span 3.2 s, so set CLIP to 3.2 and use its own frame times.
03 // HOW IT ENDS
Why the sprite can vanish at the end
A one-shot compile runs until the effect has faded, so the fireball's frame 63 is almost empty and removing the sprite after it shows no pop. The clone shares the image with the base texture, so each blast costs a material and a texture object, not another copy of the PNG.
If you spawn many blasts a second, keep a small pool of sprites, hide finished ones with sprite.visible = false and reuse them instead of disposing.
Godot 4 gets a ready scene that relights the smoke. three.js uses the documented snippet. A native six-way material is not built yet.
Is there a one shot explosion effect for three.js?
Yes. Ask your agent for an explosion that plays once, starting from the fireball or impact example, then play the packed sheet.
How do I play a smoke column once and remove it in three.js?
Pick each frame from layout.frame_times_s, stop after frame 63 at 5 seconds, then call sprite.removeFromParent() and material.dispose().
How do I play an explosion flipbook once and remove it in three.js?
Advance frames with THREE.Clock, stop at the last frame and call sprite.removeFromParent and material.dispose to clean up.
How many frames does a fireball effect use?
The fireball compile uses 64 frames over a 4 s clip with more frames on the fast burst and an empty final frame.
Compile your first effect
You pay per compile and see the price before it runs. New accounts start with $5 of usage.