agentSim

A simulation of many agents, spread into a definition: bake assembles the steps from your declared parts, frame writes the per-frame values, and the fragment samples the picture.

agentSim<TParams extends AnyWgslStruct>(cfg: AgentSimConfig<TParams>): AgentSimSpread

Import sim from shaders/std and use sim.agents.agentSim.

bake runs once per composition with the compile-time props (shape, color space) and returns the pipelines and their order. frame runs once per frame: read props, write sys.writeParams(...), return sys.frame({count}). The spawn step runs before the first frame and again whenever reseed changes. Without a GPU the fragment shows the fallback.

Parameters

NameTypeNotes
cfgAgentSimConfig<TParams>

Example

...agentSim<typeof SimParams>({layout: simLayout, params: SimParams, maxAgents: 4096, countCap: {desktop: 4096, mobile: 1200}, output: {key: 'outTex', name: 'boidsTexture', size: [1024, 1024], format: 'rgba16float'}, bake: ({getCpuValue}) => { const render = boidsRender(getCpuValue('agentShape') as string); return {pipelines: {init: {kernel: boidsInitKernel, threads: 'max'}, update: {kernel: boidsUpdateKernel, threads: 'agents'}, splat: render.splat, resolve: render.resolve}, initStep: 'init', program: ['update', 'splat', 'resolve']} }, frame: (sys, {getCpuValue}) => (frameParams) => { const {dt, aspect} = readAgentFrame(frameParams); const count = sys.resolveCount(getCpuValue('count') as number); sys.writeParams({...}); return sys.frame({count}) }, fragment: {output: 'boidsTexture', fallback: 'transparent'}})

Tip. Clamp the count prop with sys.resolveCount rather than reading it raw, so a desktop preset still loads on a phone.

See also

  • renderAgents — How the agents are drawn, as one declared part: a splat (each agent stamps its shape into a shared canvas) paired with the matching resolve (the canvas becomes the output picture).
  • integrator — The update step of an agent simulation: one per kind of agent.
  • force — The pushes an agent can feel.
  • agentFrame
  • simulate — Declare a simulation the engine runs for you.

Used by

Boids, FloatingParticles, MagneticFilings, ParticleField, ParticleFlow, Particles