Docs
Hooks
useParticles
useParticles
useParticles creates and controls a particle emitter for visual effects like fire, smoke, explosions, and more. It reads from the ParticleManager which is automatically set up by Game .
tsx Copy
import { useParticles } from "@carverjs/core/hooks" ;
Particles use GPU-instanced rendering via `InstancedMesh`. Each emitter is a single draw call regardless of particle count.
Quick Start #
tsx Copy
import { useRef } from "react" ;
import { Actor } from "@carverjs/core/components" ;
import { useParticles } from "@carverjs/core/hooks" ;
import type { Group } from "@carverjs/core/types" ;
function Torch ( ) {
const particles = useParticles ({ preset : "fire" });
return (
<Actor type ="primitive" shape ="box" color ="#333" >
<group ref ={particles.ref} position ={[0, 1 , 0 ]} />
</Actor >
);
}
Return Value #
Property Type Description refRefObject<Object3D>Attach to a <group> to position the emitter burst(count?)(number?) => voidEmit a burst of particles (default: 30) start()() => voidStart continuous emission stop()() => voidStop emission (alive particles finish their lifetime) clear()() => voidStop emission and kill all particles immediately setRate(rate)(number) => voidChange emission rate (stream mode) getActiveCount()() => numberCurrent number of alive particles isEmitting()() => booleanWhether the emitter is currently emitting reset()() => voidReset to initial state
Options #
useParticles accepts all ParticleEmitterConfig fields plus:
Option Type Default Description presetParticlePreset— Base preset: "fire", "smoke", "explosion", "sparks", "rain", "snow", "magic", "confetti". Individual props override preset values enabledbooleantrueEnable/disable the emitter
Emission Config #
Option Type Default Description maxParticlesnumber1000Maximum alive particles at once emission"stream" | "burst""stream"Emission mode rateValueRange50Particles per second (stream mode) burstsBurstConfig[]— Burst schedule (burst mode) durationnumberInfinityEmission duration. 0 = one-shot loopbooleantrueLoop after duration ends startDelaynumber0Delay before first emission (seconds) autoPlaybooleantrueStart emitting immediately
Emitter Shape #
Option Type Default Description shapeEmitterShapeConfig{ shape: "point" }Emission shape
Shapes #
tsx Copy
shape : { shape : "point" }
shape : { shape : "cone" , angle : Math .PI / 4 , radius : 1 , surface : false }
shape : { shape : "sphere" , radius : 1 , surface : false }
shape : { shape : "rectangle" , width : 1 , height : 1 }
shape : { shape : "edge" , from : [-0.5 , 0 , 0 ], to : [0.5 , 0 , 0 ] }
shape : { shape : "ring" , radius : 1 , innerRadius : 0.8 }
Particle Properties #
Option Type Default Description particle.speedValueRange5Initial speed particle.lifetimeValueRange1Lifetime in seconds particle.sizeValueRange1Initial scale particle.rotationValueRange0Initial rotation (radians) particle.rotationSpeedValueRange0Rotation speed (radians/sec) particle.colorColorRange"#ffffff"Initial color particle.alphaValueRange1Initial opacity particle.acceleration[x, y, z][0, 0, 0]Constant acceleration particle.gravitynumber0Downward gravity force particle.dragnumber0Linear drag (0 = none, 1 = full stop)
ValueRange can be a single number or [min, max] for randomization:
tsx Copy
particle : {
speed : [2 , 5 ],
lifetime : 1 ,
color : ["#ff0000" , "#ffff00" ],
}
Over-Lifetime Curves #
Modify particle properties over their lifetime using keyframe curves. t is normalized (0 = birth, 1 = death).
tsx Copy
overLifetime : {
size : [
{ t : 0 , value : 0 },
{ t : 0.2 , value : 1 },
{ t : 1 , value : 0 },
],
alpha : [
{ t : 0 , value : 1 },
{ t : 0.7 , value : 0.8 },
{ t : 1 , value : 0 },
],
color : [
{ t : 0 , color : "#ffffff" },
{ t : 0.3 , color : "#ff8800" },
{ t : 1 , color : "#331100" },
],
speed : [
{ t : 0 , value : 1 },
{ t : 1 , value : 0.2 },
],
rotationSpeed : [
{ t : 0 , value : 1 },
{ t : 1 , value : 0 },
],
}
Rendering #
Option Type Default Description blendMode"normal" | "additive" | "multiply" | "screen""normal"Particle blend mode textureTexture | string— Particle texture (URL or Three.js Texture) spriteSheetSpriteSheetConfig— Animated sprite sheet billboardbooleantrueParticles face the camera space"world" | "local""world"Coordinate space for simulation sortByDistancebooleanfalseSort back-to-front for transparency
Blend Modes #
Mode Best For "normal"Smoke, confetti, solid particles "additive"Fire, sparks, magic, glow effects "multiply"Shadows, dark effects "screen"Bright overlays
Sprite Sheets #
tsx Copy
spriteSheet : {
texture : "/textures/smoke-sheet.png" ,
columns : 8 ,
rows : 8 ,
totalFrames : 60 ,
fps : 30 ,
loop : true ,
randomStart : true ,
}
Burst Mode #
For one-shot effects like explosions:
tsx Copy
const explosion = useParticles ({
preset : "explosion" ,
autoPlay : false ,
});
function onHit ( ) {
explosion.burst (80 );
}
Burst Schedule #
tsx Copy
const particles = useParticles ({
emission : "burst" ,
bursts : [
{ time : 0 , count : [60 , 100 ] },
{ time : 0.1 , count : [20 , 40 ] },
],
duration : 0 ,
loop : false ,
});
Field Type Default Description timenumber0Offset from emitter start (seconds) countValueRange— Required. Particles to emitcyclesnumber1Repeat count. 0 = infinite intervalnumber1Delay between cycles (seconds)
Presets #
8 built-in presets for common effects:
Preset Mode Description "fire"stream Ascending glow, orange to transparent, additive "smoke"stream Slow rise, grey, expanding, normal blend "explosion"burst Radial burst, quick fade, additive "sparks"burst Fast bright particles with gravity "rain"stream Vertical downpour, additive "snow"stream Gentle falling, white, normal blend "magic"stream Colorful sphere emission, additive "confetti"burst Tumbling colored particles with gravity
Custom Presets #
tsx Copy
import { registerParticlePreset } from "@carverjs/core/systems" ;
registerParticlePreset ("myEffect" , {
maxParticles : 500 ,
rate : 60 ,
shape : { shape : "sphere" , radius : 0.5 },
particle : {
speed : [2 , 5 ],
lifetime : [1 , 3 ],
color : ["#00ffaa" , "#0044ff" ],
},
blendMode : "additive" ,
});
const particles = useParticles ({ preset : "myEffect" as any });
World/Local Space #
tsx Copy
const trail = useParticles ({
preset : "fire" ,
space : "world" ,
});
const aura = useParticles ({
preset : "magic" ,
space : "local" ,
});
Lifecycle Callbacks #
tsx Copy
const particles = useParticles ({
preset : "explosion" ,
autoPlay : false ,
onParticleBorn : (index ) => { },
onParticleDeath : (index ) => { },
onComplete : () => { },
});
Full Example — Rocket Thruster #
tsx Copy
import { useRef } from "react" ;
import { Actor } from "@carverjs/core/components" ;
import { useParticles, useGameLoop, useInput } from "@carverjs/core/hooks" ;
import type { Group } from "@carverjs/core/types" ;
function Rocket ( ) {
const ref = useRef<Group >(null );
const { isDown } = useInput ();
const thruster = useParticles ({
maxParticles : 300 ,
rate : 100 ,
shape : { shape : "cone" , angle : Math .PI / 8 , radius : 0.1 },
particle : {
speed : [5 , 10 ],
lifetime : [0.3 , 0.8 ],
size : [0.1 , 0.3 ],
color : ["#ff6600" , "#ffcc00" ],
},
overLifetime : {
alpha : [
{ t : 0 , value : 1 },
{ t : 1 , value : 0 },
],
size : [
{ t : 0 , value : 1 },
{ t : 1 , value : 0.3 },
],
},
blendMode : "additive" ,
autoPlay : false ,
});
useGameLoop (() => {
if (isDown ("Space" )) {
thruster.start ();
thruster.setRate (150 );
} else {
thruster.stop ();
}
});
return (
<Actor ref ={ref} type ="primitive" shape ="box" color ="#888" >
<group ref ={thruster.ref} position ={[0, -1 , 0 ]} />
</Actor >
);
}
Game Phase Integration #
Particles automatically respect the game phase:
Phase Behavior "loading"All emitters frozen "playing"Normal operation "paused"Emitters freeze — no emission, no updates "gameover"Same as paused
Cleanup #
Emitters created via useParticles are automatically destroyed when the component unmounts. The GPU resources (InstancedMesh, material, textures) are disposed.
Type Definitions #
See Types for ParticleEmitterConfig, ParticlePreset, EmitterShapeConfig, ValueRange, ColorRange, LifetimeCurve, ColorGradient, BurstConfig, SpriteSheetConfig, ParticleBlendMode, OverLifetimeConfig, UseParticlesOptions, and UseParticlesReturn.
Previous
useTween
Next
Types