Particles and lines
createParticleSystem bundles a ParticleEmitterBehavior with a GameEntity anchor so VFX spawns like any other node. createLine builds thick WebGPU polylines for trajectories, debug paths, and stylized beams. Use particle entities for bursts, smoke, and magic; use lines when you need editable point lists or ray picking.
Minimal example
website/snippets/entities/particles-and-lines.ts:
import { createLine, createParticleSystem } from '@zylem/game-lib/entity';
import { particlePresets } from '@zylem/game-lib/behavior';
const trail = createLine({
points: [
{ x: -4, y: 0.1, z: 0 },
{ x: 0, y: 0.1, z: 2 },
{ x: 4, y: 0.1, z: 0 },
],
linewidth: 2,
color: '#ffcc00',
});
const sparks = createParticleSystem({
preset: particlePresets.burst(),
autoplay: true,
followPosition: true,
});
Particle systems
Options merge GameEntityOptions with emitter behavior settings (ZylemParticleSystemOptions):
| Field | Purpose |
|---|---|
effect / preset | ParticleEffectDefinition factory |
autoplay | Start emitting on spawn |
followPosition / followRotation | Keep emitter aligned with the entity transform |
localOffset | Spawn offset in entity space |
autoDestroy | Remove entity when the effect completes |
The entity proxies playback methods (play, pause, stop, restart, isPlaying) to the underlying behavior handle. Import presets from @zylem/game-lib/behavior (particlePresets) or pass a custom effect factory.
Alternatively attach ParticleEmitterBehavior directly to any entity with entity.use(...) when you do not need a dedicated particle entity type.
Lines
ZylemLineOptions requires at least two points in world space. Optional fields:
colors— per-vertex colorslinewidth,worldUnits, dashed styles (dashed,dashSize,gapSize)opacity,transparent,alphaToCoveragepickThreshold— default raycast thickness for pickZylemLine
Use picking helpers for gameplay that tests clicks against splines or laser arcs.
Variations
- Clone particle entities for one-shot bursts; clone shares behavior wiring without duplicating preset definitions incorrectly (see unit tests in game-lib).
- Animate line points from
onUpdateby rebuilding or mutating the entity if your game edits paths frequently. - Particle entities expose editor selection bounds even though particles themselves are dynamic.
Pitfalls
- Effect required —
createParticleSystem()withouteffectorpresetthrows at construction time. - WebGPU lines — line materials target the WebGPU line pipeline; verify your renderer path supports
Line2in the target environment. - Autoplay vs manual — disable
autoplaywhen you need synchronized VFX from game events; callplay()from callbacks.