Documentation
¶
Overview ¶
Package particles loads particle effects (*.fx.json files), simulates them and draws them.
An effect is an asset, Effect, loaded with LoadEffect into the gfx.EffectHandle that a gfx.Particles component holds, as gfx.Animator holds a clip. Package fxfile (particles/fxfile) reads and writes the file without Ebiten; the file format is documented in doc/guide/engine/particles.md.
The simulation has two halves. System spawns, moves and removes the particles of every entity that has a gfx.Transform and a gfx.Particles; Extract queues one render.KindSprite item per live particle, which the renderer batches into few draw calls. Particles are not entities: they live in pools owned by the System, one per entity, so a burst of thousands creates no archetype churn and no deferred deletes.
The package makes no Ebiten calls. The simulation is deterministic: each pool owns a PCG random source seeded from gfx.Particles.Seed or the entity, so the same level and inputs give the same particles.
Index ¶
- Constants
- func HoldEffect(sc *asset.Scope, h gfx.EffectHandle)
- func LoadEffect(sc *asset.Scope, key string) gfx.EffectHandle
- func LoadEffectSync(sc *asset.Scope, key string) (gfx.EffectHandle, error)
- func RegisterLoaders(srv *asset.Server)
- func Resolve(eff *EffectEmitter, c *gfx.Particles) fxfile.Emitter
- func ShaderOf(eff *EffectEmitter, c *gfx.Particles) gfx.ShaderHandle
- func SpriteOf(eff *EffectEmitter, c *gfx.Particles) gfx.SpriteHandle
- type Effect
- type EffectEmitter
- type EffectStore
- func (s EffectStore) Err(h gfx.EffectHandle) error
- func (s EffectStore) Generic() *asset.Store[Effect]
- func (s EffectStore) Get(h gfx.EffectHandle) *Effect
- func (s EffectStore) Key(h gfx.EffectHandle) asset.Key
- func (s EffectStore) Lookup(h gfx.EffectHandle) (*Effect, asset.State)
- func (s EffectStore) Ready(h gfx.EffectHandle) bool
- func (s EffectStore) Version(h gfx.EffectHandle) uint32
- type Extract
- type Seek
- type System
Constants ¶
const AssetKindEffect = "effect"
AssetKindEffect is the asset kind of an Effect (a *.fx.json file): the name a component field carries in its +game:asset:kind=... marker.
Variables ¶
This section is empty.
Functions ¶
func HoldEffect ¶
func HoldEffect(sc *asset.Scope, h gfx.EffectHandle)
HoldEffect adds sc as an owner of h, like asset.Hold.
func LoadEffect ¶
func LoadEffect(sc *asset.Scope, key string) gfx.EffectHandle
LoadEffect returns the handle for an effect key ("fx/embers.fx.json") and adds a hold for sc. It never blocks and panics like asset.Load.
func LoadEffectSync ¶
LoadEffectSync is LoadEffect followed by loading until the effect is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.
func RegisterLoaders ¶
RegisterLoaders registers Effect and its loader on srv. The render loaders are needed when an effect names a sprite or a shader: a missing one fails the effect with "type not registered". It panics like asset.Register if Effect is already registered.
func Resolve ¶
func Resolve(eff *EffectEmitter, c *gfx.Particles) fxfile.Emitter
Resolve returns the emitter that c plays: eff's, with every non-nil override field of c applied. It copies the emitter and does not allocate. The Sprite and Shader keys stay the emitter's; the System and Extract draw with SpriteOf and ShaderOf.
The System resolves every tick instead of caching, so a value c does not override follows the effect file at once, hot reloads included.
func ShaderOf ¶
func ShaderOf(eff *EffectEmitter, c *gfx.Particles) gfx.ShaderHandle
ShaderOf returns the shader of c: its Shader override when set, else the effect's.
func SpriteOf ¶
func SpriteOf(eff *EffectEmitter, c *gfx.Particles) gfx.SpriteHandle
SpriteOf returns the particle image of c: its Sprite override when set, else the effect's.
Types ¶
type Effect ¶
type Effect struct {
// Emitters are the effect's emitters in file order; a Ready effect has
// at least one.
Emitters []EffectEmitter
// contains filtered or unexported fields
}
Effect is a loaded *.fx.json file: its emitters and the handles of their images and shaders. Load one with LoadEffect; it is immutable once loaded and a hot reload replaces it.
type EffectEmitter ¶
type EffectEmitter struct {
// Emitter is the authored emitter. Its Sprite and Shader keys are kept
// for tools; the simulation reads the handles below.
fxfile.Emitter
// SpriteRef and ShaderRef are the image and shader, loaded into the
// effect's own scope, so they live as long as the effect. A shader that
// is loading or failed draws the particles normally, as does a missing
// image (the placeholder).
SpriteRef gfx.SpriteHandle
ShaderRef gfx.ShaderHandle
}
EffectEmitter is one emitter of an Effect.
type EffectStore ¶
type EffectStore struct {
// contains filtered or unexported fields
}
EffectStore resolves gfx.EffectHandle values to effects. Get one with EffectStoreOf. It is a small value; copy it freely.
func EffectStoreOf ¶
func EffectStoreOf(srv *asset.Server) EffectStore
EffectStoreOf returns the effect store of srv. It panics like asset.StoreOf if RegisterLoaders has not run on srv.
func (EffectStore) Err ¶
func (s EffectStore) Err(h gfx.EffectHandle) error
Err returns why the effect failed to load, or nil.
func (EffectStore) Generic ¶
func (s EffectStore) Generic() *asset.Store[Effect]
Generic returns the underlying generic store, for loaders and tools that work with asset.Handle[Effect].
func (EffectStore) Get ¶
func (s EffectStore) Get(h gfx.EffectHandle) *Effect
Get returns the effect, or the placeholder; see EffectStore.Lookup.
func (EffectStore) Key ¶
func (s EffectStore) Key(h gfx.EffectHandle) asset.Key
Key returns the key h was loaded from, or the zero Key for a zero or stale handle.
func (EffectStore) Lookup ¶
func (s EffectStore) Lookup(h gfx.EffectHandle) (*Effect, asset.State)
Lookup returns the effect and its load state. Only a Ready effect is real: otherwise the pointer is the placeholder, an effect without an emitter. The pointer is valid until the next asset.Server.Update and must not be stored. It does not allocate.
func (EffectStore) Ready ¶
func (s EffectStore) Ready(h gfx.EffectHandle) bool
Ready reports whether h refers to a loaded effect.
func (EffectStore) Version ¶
func (s EffectStore) Version(h gfx.EffectHandle) uint32
Version is 0 until the effect is committed and goes up with every hot reload that replaced it; the System compares it to restart the pools of a changed effect.
type Extract ¶
type Extract struct {
// Margin grows the culling rectangle on every side, in world units. Set
// it to about half of the largest particle. The default zero culls
// exactly at the view edge.
Margin float32
// contains filtered or unexported fields
}
Extract queues one world-space render.KindSprite item for every live particle of the System it was built with, so the renderer batches them (consecutive sprites with the same texture and blend mode share a draw call). Run it in the render-prepare stage, right after the SpriteExtract.
Particles outside the camera bounds (plus Margin) are skipped and counted in render.Queue.Culled, once per Run. Fully transparent particles are skipped without counting. Run does not allocate.
It needs the same resources as the extract package's systems.
func NewExtract ¶
NewExtract returns the extract for w that draws the particles sys simulates.
type Seek ¶
type Seek struct {
// Sec is the time in seconds of the pending [Seek.SeekTo].
Sec float32
// contains filtered or unexported fields
}
Seek is an optional World resource that editors use to jump the simulation of every emitter to a time. The zero Seek, and a world without one, change nothing. The engine registers a zero Seek; only tools write it. Pausing, single steps and the speed are the game clock's (gfx.Time, written by the engine from its tool clock).
type System ¶
type System struct {
// contains filtered or unexported fields
}
System simulates the particles of every entity that has a gfx.Transform and a gfx.Particles: it spawns, moves and removes particles, and writes the component's runtime fields (Pool, Time, Live, Done). Run it in the anim stage, after the AnimationSystem, so it sees the tick's final position and keeps playing while a Tool pauses the game. It follows the game clock: the tick's delta is scaled, and stops while time stands still.
An entity whose effect is not loaded yet (or failed, or is the zero handle) has no particles. The component's override fields are applied on top of the effect every tick (see Resolve). When a hot reload or an override change alters an effect's emitter, the pools that play it start over if the cycle, the capacity or the spawn area changed (Duration, Loop, Burst, Max, Shape, ShapeSize), which live particles cannot follow; any other change applies to the live particles at once.
The particles live in pools owned by the System, one per entity, with a lane for each emitter of its effect (see Extract for drawing them). A pool is released in the tick after its entity is deleted or loses the component, and its buffers are reused by the next emitter. Run does not allocate once the pools exist.
func NewSystem ¶
func NewSystem(w *ecs.World, effects EffectStore) *System
NewSystem returns the system for w. effects resolves the components' effect handles; take it from EffectStoreOf.
func (*System) Pools ¶
Pools returns the number of pools (entities with particle state) in use, for tests and diagnostics.
func (*System) Run ¶
Run implements system.Runner: it advances every emitter by the tick's delta, and those with the gfx.Unscaled marker by gfx.Time.Unscaled. A zero delta (the game clock stands still) only keeps the pools: live particles stay where they are and no emitter spawns, because an emitter's time would stay at 0 and burst every tick. A pending Seek in the world replaces the tick with the seek.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package fxfile reads, checks and writes the *.fx.json particle effect file, without Ebiten, the ECS runtime or any GPU work: the authored emitters (Emitter, Spec), Decode and Encode.
|
Package fxfile reads, checks and writes the *.fx.json particle effect file, without Ebiten, the ECS runtime or any GPU work: the authored emitters (Emitter, Spec), Decode and Encode. |