GUIDE // THREE.JS
How to randomize the starting frame of many three.js flipbook particles
Give each of your sprites its own clone of the texture and a random offset between 0 and the frame count, then compute its frame as the time times the frame rate plus that offset, rounded down and wrapped. Clones share one image, and every sprite plays the same loop out of step with its neighbors.
- 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
Start six smoke plumes on different frames
- Load the smoke sheet once with
loadAsync. - For each plume, clone the texture, set its repeat to 1/8 by 1/8 and make a sprite with it.
- Store a random offset in
sprite.userData, from 0 up to 64. - Each tick, add the offset to the time-based frame before wrapping with
% 64.
import * as THREE from 'three';
const FPS = 16; // smoke plume: 64 frames over 4 s
const FRAMES = 64;
const base = await new THREE.TextureLoader().loadAsync('/fx/smoke_rgba.png');
base.colorSpace = THREE.SRGBColorSpace;
const plumes = [];
for (let i = 0; i < 6; i++) {
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.set(i * 8, 5, 0);
sprite.scale.set(10, 10, 1);
sprite.userData.offset = Math.random() * FRAMES;
scene.add(sprite);
plumes.push(sprite);
}
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const t = clock.getElapsedTime();
for (const sprite of plumes) {
const frame = Math.floor(t * FPS + sprite.userData.offset) % FRAMES;
sprite.material.map.offset.set((frame % 8) / 8, 1 - (Math.floor(frame / 8) + 1) / 8);
}
renderer.render(scene, camera);
});
02 // REAL OUTPUT
Fire on random frames
Set the rate to 32 for the 2 s fire, and a few copies on random frames read as one wide blaze.
03 // AT SCALE
Hundreds of particles
One sprite per plume is fine for dozens. For hundreds, draw them all in one draw call: an InstancedMesh of planes with a ShaderMaterial, and the random offset stored per instance in an InstancedBufferAttribute that the shader adds to the frame. Every instance then reads the same texture with no clones at all.
Keep one-shot explosions on frame 0. A random start would skip their flash, so vary them with different compiles instead, such as the same explosion with a new seed.
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.
How do I randomize the starting frame of several three.js fire sprites?
Give each sprite its own texture with texture.clone() and start its frame counter at a random integer from 0 to 63.
How do I randomize the starting frame of several three.js smoke sprites?
Give each sprite its own texture with texture.clone() and start its frame counter at a random integer from 0 to 63.
How do I calculate UV coordinates for a sprite sheet with rows and columns in three.js?
Set texture.repeat to 1/C by 1/R. Compute column n % C and row Math.floor(n / C), then offset X by column / C and Y from the top.
How do I organize different particle textures in an atlas?
Use a regular grid of equal cells with matching padding around every cell so your particle system can address each image by index.
Compile your first effect
You pay per compile and see the price before it runs. New accounts start with $5 of usage.