@margoengine/vfx 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1642 -3
  4. package/dist/base-C9upJytz.js +658 -0
  5. package/dist/base.d.ts +767 -0
  6. package/dist/base.js +1 -0
  7. package/dist/blown-sand.d.ts +218 -0
  8. package/dist/blown-sand.js +700 -0
  9. package/dist/clouds.d.ts +229 -0
  10. package/dist/clouds.js +566 -0
  11. package/dist/compat-CVSEj1Bp.js +217 -0
  12. package/dist/compat.d.ts +59 -0
  13. package/dist/curves.d.ts +11 -0
  14. package/dist/curves.js +59 -0
  15. package/dist/debris.d.ts +111 -0
  16. package/dist/debris.js +258 -0
  17. package/dist/depth-collide.d.ts +7 -0
  18. package/dist/depth-collide.js +96 -0
  19. package/dist/emitter.d.ts +10 -0
  20. package/dist/emitter.js +125 -0
  21. package/dist/events.d.ts +9 -0
  22. package/dist/events.js +131 -0
  23. package/dist/extra-CgGjbojB.js +4 -0
  24. package/dist/extra.d.ts +2 -0
  25. package/dist/fields.d.ts +11 -0
  26. package/dist/fields.js +95 -0
  27. package/dist/fire-front.d.ts +539 -0
  28. package/dist/fire-front.js +840 -0
  29. package/dist/fire.d.ts +119 -0
  30. package/dist/fire.js +173 -0
  31. package/dist/flame-ramp-VoBKpYnM.js +75 -0
  32. package/dist/flame-ramp.d.ts +61 -0
  33. package/dist/flame.d.ts +260 -0
  34. package/dist/flame.js +605 -0
  35. package/dist/flipbook.d.ts +7 -0
  36. package/dist/flipbook.js +23 -0
  37. package/dist/fog-B-kBv7g0.js +32 -0
  38. package/dist/frame-BuvMFBPp.js +4 -0
  39. package/dist/frame.d.ts +7 -0
  40. package/dist/gunfire.d.ts +696 -0
  41. package/dist/gunfire.js +1204 -0
  42. package/dist/hash-Btsp6fGK.js +7 -0
  43. package/dist/hash.d.ts +2 -0
  44. package/dist/index.d.ts +17 -0
  45. package/dist/index.js +60 -0
  46. package/dist/json.d.ts +53 -0
  47. package/dist/json.js +237 -0
  48. package/dist/lit.d.ts +11 -0
  49. package/dist/lit.js +105 -0
  50. package/dist/mesh.d.ts +14 -0
  51. package/dist/mesh.js +225 -0
  52. package/dist/node-system.d.ts +315 -0
  53. package/dist/node-system.js +384 -0
  54. package/dist/nodes.d.ts +14 -0
  55. package/dist/nodes.js +293 -0
  56. package/dist/obstacles.d.ts +3 -0
  57. package/dist/obstacles.js +25 -0
  58. package/dist/over-life.d.ts +14 -0
  59. package/dist/over-life.js +93 -0
  60. package/dist/require-webgpu--tUXPsmA.js +7 -0
  61. package/dist/ribbons.d.ts +20 -0
  62. package/dist/ribbons.js +151 -0
  63. package/dist/schedule.d.ts +29 -0
  64. package/dist/schedule.js +47 -0
  65. package/dist/settings.d.ts +362 -0
  66. package/dist/settings.js +707 -0
  67. package/dist/shader.d.ts +44 -0
  68. package/dist/shadows.d.ts +26 -0
  69. package/dist/shadows.js +182 -0
  70. package/dist/shapes.d.ts +34 -0
  71. package/dist/shapes.js +81 -0
  72. package/dist/shared/fog.d.ts +33 -0
  73. package/dist/shared/pass.d.ts +53 -0
  74. package/dist/shared/settings.d.ts +89 -0
  75. package/dist/skinned.d.ts +11 -0
  76. package/dist/skinned.js +104 -0
  77. package/dist/smoke.d.ts +215 -0
  78. package/dist/smoke.js +280 -0
  79. package/dist/soft.d.ts +17 -0
  80. package/dist/soft.js +55 -0
  81. package/dist/solid.d.ts +168 -0
  82. package/dist/solid.js +403 -0
  83. package/dist/sort.d.ts +3 -0
  84. package/dist/sort.js +58 -0
  85. package/dist/space.d.ts +14 -0
  86. package/dist/space.js +88 -0
  87. package/dist/stretch.d.ts +7 -0
  88. package/dist/stretch.js +40 -0
  89. package/dist/texture.d.ts +8 -0
  90. package/dist/texture.js +45 -0
  91. package/dist/view-RKprOPAy.js +30 -0
  92. package/dist/view-rect-DkeiiUtw.js +13 -0
  93. package/dist/wrap.d.ts +3 -0
  94. package/dist/wrap.js +5 -0
  95. package/package.json +192 -6
package/README.md CHANGED
@@ -1,3 +1,1642 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @margoengine/vfx
2
+
3
+ Import `margoengine/vfx` from the `margoengine` package, or `@margoengine/vfx` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
4
+
5
+ Optional GPU particle simulation and indirect billboard rendering, with capacity up to 2,097,152 on a default WebGPU device (64 bytes a slot in one 128 MiB storage binding; an adapter that grants larger bindings holds up to 16,776,960). One storage buffer holds particle position/life, velocity/size, color and rotation (birth angle, angular velocity, initial lifetime); each invocation owns one particle. Workgroups of 256 integrate gravity, analytic curl turbulence and ground-plane collisions, touching only position/life and velocity/size after birth. Births take the dead slots in slot order and the particles whose billboard can reach the screen are listed in slot order for the indirect draw, one triangle per particle (workgroup prefix sums, not atomic tickets), so the same frames give the same particles, the same draw order and the same blended pixels on every run.
6
+
7
+ ```ts
8
+ import {EngineKernel} from 'margoengine/core';
9
+ import {VFX} from 'margoengine/vfx';
10
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
11
+ const sparks = VFX.attach(engine, {
12
+ capacity:500_000, rate:20_000, position:[0,1,0], velocity:[0,5,0],
13
+ spread:3, lifetime:[0.5,2], size:0.08, color:[1,0.4,0.05,1],
14
+ gravity:[0,-9.81,0], ground:0, bounce:0.3, turbulence:1, angularVelocity:2
15
+ });
16
+ sparks.burst(10_000);
17
+ engine.start();
18
+ ```
19
+
20
+ `rate` is births/second (default capacity/2). `burst(count)` adds births on the next frame, limited by capacity and available dead slots; `burst(count, {position, velocity, spread, emissionExtent})` is born where it says (what it leaves out is the effect's), and bursts with a place of their own never share one, so two in one frame start where each asked. Fractional emission budgets carry across frames. Options other than capacity can be adjusted before a frame; validation runs before GPU commands. `frequency` controls the curl field's spatial frequency. Life controls fade-out; rendering uses soft circular, camera-facing billboards with alpha blending and read-only scene depth. A billboard is never drawn under 1.5 px across: a smaller (far) particle is drawn at 1.5 px with its alpha scaled by its true area over that area, so it keeps its average light instead of flickering between pixels. Simulation starts with dead slots and performs no CPU particle initialization or GPU readback. `reset()` takes the effect back to that start: no particles, its clock at 0 and `initialCount` waiting, so the same frames after it give the same particles, ribbons included (`@margoengine/cutscene/vfx` seeks this way). After `dispose()`, `burst` and `reset` throw `MargoError` `'disposed'`.
21
+
22
+ `emissionExtent: [x,y,z]` distributes births within a box of these half extents
23
+ around `position` (default zero). `intensity` multiplies RGB for HDR emission,
24
+ 0 to 65,504, the largest half float of the HDR target (default 1). Billboards draw at frame-graph order 6, after water and scatter.
25
+
26
+ One million particles use 64 MB for state plus 4 MB for the draw list, regardless of the live count. Per-frame CPU work is bounded uniform updates and pass submission; there is no JavaScript object/array per particle. A steady frame of 100,000 particles sends the GPU 224 bytes in one write ([counted](../../docs/whitepaper/batch-170-vfx.md)). GPU performance still depends on capacity, fill rate and hardware.
27
+
28
+ This is Margo's one particle system; `GPUParticles` in `@margoengine/creative/particles` is a leaf-field preset
29
+ over it. Six optional features patch the shaders only when set, so effects without them compile the same WGSL
30
+ and make the same GPU calls:
31
+
32
+ - `obstacles: [{kind:'sphere', center, radius}, {kind:'box', center, halfSize}]` (up to 64): the flow bends
33
+ around each within one unit, and particles inside are pushed out and reflected with `bounce`. The array is read
34
+ every frame (move or replace entries freely); pass one, even `[]`, before attaching. Adds a 2,096-byte uniform.
35
+ - `wrap: true` keeps particles inside the emission box (`position` ± `emissionExtent`, every extent > 0): a
36
+ particle leaving one face re-enters on the opposite face at the emitter velocity. Use with a long `lifetime`
37
+ and `rate: 0` for a persistent weather field.
38
+ - `sort: true` draws back to front: each frame a bitonic sort orders the live list by view depth. The list is
39
+ padded to a power of two; the sort costs log2(n)·(log2(n)+1)/2 dispatches, so prefer it for thousands, not a
40
+ million.
41
+ - `texture: {width, height, data}` (RGBA8, sRGB, up to 4096²) replaces the soft disc; texel RGB multiplies the
42
+ particle colour and texel alpha its opacity. Copied at construction.
43
+ - `texture: {width, height, data, atlas: {columns, rows, frames}}` treats the texture as `columns` x `rows` equal
44
+ frames, numbered left to right from the top row. `frames: 'random'` gives each particle one frame picked when it
45
+ spawns (block-break debris showing pieces of the block's texture); `'lifetime'` steps each particle through every
46
+ frame in order from spawn to death (a flipbook). `@margoengine/vfx/flipbook` blends each frame into the next;
47
+ without it the step is instant. Sampling stays half a texel inside the frame, so linear filtering
48
+ never shows a neighbouring frame, and the billboard triangle grows to hold the whole square frame. Without `atlas`
49
+ the WGSL and GPU calls are unchanged.
50
+
51
+ ```ts
52
+ // Block-break debris: 24 pieces of a 16 x 16 block texture, a random 4 x 4 texel piece each.
53
+ const stonePixels = new Uint8Array(16 * 16 * 4); // the block's RGBA8 texels
54
+ const [x, y, z] = [3, 0, -2]; // the broken block
55
+ const debris = VFX.attach(engine, {
56
+ capacity: 256, rate: 0, size: 0.1, lifetime: [0.5, 1], velocity: [0, 3, 0], spread: 2, ground: 0,
57
+ texture: {width: 16, height: 16, data: stonePixels, atlas: {columns: 4, rows: 4, frames: 'random'}},
58
+ });
59
+ debris.options.position = [x + 0.5, y + 0.5, z + 0.5];
60
+ debris.burst(24);
61
+ ```
62
+
63
+ `node packages/vfx/test/browser-check.mjs` (real GPU; run it under `node scripts/run-with-timeout.mjs`) draws 64
64
+ particles with a 2 x 2 atlas at 1x and 4x MSAA and checks each one's centre and four points near its corners against
65
+ the frame it must show (the shader's hash of its birth angle for `'random'`, its age quarter for `'lifetime'`).
66
+
67
+ `curves: {size, alpha, color}` shapes each particle over its life (birth 0, death 1):
68
+
69
+ - `size: [0, 1, 0.5]`: 2 to 32 multipliers of `size` (each 0..1), evenly spaced over the life and interpolated
70
+ linearly. The curve can only shrink a particle, never grow it past `size`, so culling stays exact.
71
+ - `alpha: [0, 1, 1, 0]`: 2 to 32 opacities (each 0..1); replaces the default fade-out, so end with 0 to fade away.
72
+ - `color: [{at: 0, color: [1, 1, 1]}, {at: 1, color: [1, 0.2, 0]}]`: 2 to 8 stops ascending by `at`, linear RGB 0..16,
73
+ multiplying the particle `color` (use `color: [1, 1, 1, 1]` to draw the gradient as it is).
74
+
75
+ Set at least one; a curve left out keeps the stock size or fade. Which curves are set is read at attach, their numbers
76
+ every frame. The shader samples them in the billboard stage only (one 400-byte uniform), so the simulation is unchanged.
77
+
78
+ `emitter` says where births start and which way they fly; `speed` (world units per second, -1000..1000, default 0,
79
+ negative flies inward) pushes along the shape's direction, on top of `velocity` and `spread`; `position` and
80
+ `emissionExtent` still apply:
81
+
82
+ - `{shape: 'sphere', radius = 1, shell = false}`: uniform through the ball, or on its surface with `shell: true`;
83
+ births fly radially.
84
+ - `{shape: 'cone', radius = 0, angle = PI / 6, axis = [0, 1, 0]}`: from a disc of `radius` at the apex, directions
85
+ uniform in solid angle within the half `angle` (0..PI/2) of `axis`.
86
+ - `{shape: 'mesh', positions, indices?, scale = 1}`: uniform over the surface of a triangle mesh (area-weighted,
87
+ at most 262,144 triangles, zero-area triangles ignored), flying along each face's normal (counter-clockwise faces
88
+ point out). Positions are in world units around `position`.
89
+
90
+ The shape (and a mesh's triangles) is read when the effect attaches, and again after device restoration; radius, angle,
91
+ axis, `speed` and `scale`-free numbers can change every frame. Changing `shape` afterwards throws a `TypeError`.
92
+
93
+ ```ts
94
+ import {EngineKernel} from 'margoengine/core';
95
+ import {VFX} from 'margoengine/vfx';
96
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
97
+ // Embers rising from a cone: swell then shrink, fade in and out, cool from white to orange to dark red.
98
+ const embers = VFX.attach(engine, {
99
+ capacity: 20_000, rate: 4_000, position: [0, 0.5, 0], velocity: [0, 0, 0], spread: 0.2, size: 0.12,
100
+ lifetime: [1, 2], color: [1, 1, 1, 1], intensity: 3,
101
+ emitter: {shape: 'cone', radius: 0.3, angle: 0.35, speed: 2.5},
102
+ curves: {
103
+ size: [0.3, 1, 0.6, 0],
104
+ alpha: [0, 1, 1, 0],
105
+ color: [{at: 0, color: [1, 1, 1]}, {at: 0.3, color: [1, 0.55, 0.1]}, {at: 1, color: [0.3, 0.02, 0.01]}],
106
+ },
107
+ });
108
+ // Sparks from the surface of a mesh (here two triangles of a roof), pushed out along the faces.
109
+ const roof = new Float32Array([0, 0, 0, 4, 0, 0, 0, 1, 3, 4, 1, 3]);
110
+ const shower = VFX.attach(engine, {
111
+ capacity: 5_000, rate: 800, position: [0, 3, 0], size: 0.05, velocity: [0, 0, 0], spread: 0.1,
112
+ emitter: {shape: 'mesh', positions: roof, indices: [0, 1, 2, 1, 3, 2], speed: 1.5},
113
+ });
114
+ ```
115
+
116
+ `node packages/vfx/test/browser-check.mjs` also reads back, on the real GPU, 320 curve probes (colour, alpha and falloff
117
+ against curves evaluated in the test, at 1x and 4x MSAA) and 4,096 births of each shape: a ball's volume (mean r^3 / R^3
118
+ is 1/2), a shell's radius, a cone's solid-angle and disc statistics, and a two-triangle mesh's area share, bounds and normals.
119
+
120
+ `obstacles`, `wrap`, `sort`, `texture`, `curves` and `emitter` are read when the effect attaches; `softness` and `depth` are read every frame.
121
+
122
+ ### Fog
123
+
124
+ `effect.setFog(fog)` fogs the particles with `@margoengine/environment`'s `Fog` or any object with its fields (`color`,
125
+ `near`, `far`, `density`, `heightFalloff`, `height`, `enabled`), read every frame with Fog's maths. Each pixel of a
126
+ particle fades toward the fog colour by the fog at its world position (particles blend alpha over the scene, so fog
127
+ covers them as it covers meshes). Particles write their colour as it is, so on an 8-bit canvas the fog colour goes in
128
+ sRGB-encoded and a fully fogged particle matches the fogged background. VFX does not import environment.
129
+ `fog.enabled = false` clears it without recompiling; `effect.setFog(null)` (the default) draws exactly as before.
130
+
131
+ ```ts
132
+ import {Fog} from 'margoengine/environment';
133
+ import {VFX} from 'margoengine/vfx';
134
+ import {Engine} from 'margoengine/core';
135
+ const engine = await Engine.create(document.querySelector('canvas')!);
136
+ const sparks = VFX.attach(engine, {capacity: 10_000, rate: 1_000, position: [0, 1, 0], velocity: [0, 5, 0]});
137
+ const fog = Fog.attach(engine, {color: '#8090a0', near: 5, far: 60});
138
+ sparks.setFog(fog);
139
+ ```
140
+
141
+ ### In an order-independent pass
142
+
143
+ Particles blend over the scene in a pass of their own, so they paint over glass in front of them. `addEffect` from
144
+ [`margoengine/oit/vfx`](../oit/README.md#particles-splats-and-points-addeffect) draws them with the glass instead, fog,
145
+ softness and textures included:
146
+
147
+ ```ts
148
+ import {Engine} from 'margoengine/core';
149
+ import {OIT} from 'margoengine/oit';
150
+ import {addEffect} from 'margoengine/oit/vfx';
151
+ import {VFX} from 'margoengine/vfx';
152
+ const engine = await Engine.create(document.querySelector('canvas')!);
153
+ addEffect(OIT.attach(engine), VFX.attach(engine, {capacity: 10_000}));
154
+ ```
155
+
156
+ ### Soft particles
157
+
158
+ `softness` (world units, default 0 = off) fades each particle over that distance in front of any opaque surface behind
159
+ it, measured along the view axis, so a particle that meets the floor or a wall no longer shows a hard line. A particle
160
+ `softness` or more clear of every surface is drawn exactly as before; one touching a surface fades to nothing. It needs
161
+ the `soft` feature (on the root `VFX`; `{soft}` from `@margoengine/vfx/soft` on `/base`) and the scene depth as a
162
+ sampleable depth texture of the drawing size, given by the `depth` option: a function returning it, so a texture that is
163
+ replaced on resize is picked up. `DepthTexture` from `@margoengine/post/depth` provides one (depth32float, reversed Z, 1 sample);
164
+ a multisampled scene depth works too (the nearest sample counts), as do views (the view's rectangle is used).
165
+ `softness` may change every frame; with it at 0 the effect compiles the same WGSL, binds the same resources and does the
166
+ same per-frame work as without the option. The first soft frame compiles a second billboard shader, like fog.
167
+
168
+ Without a depth texture the draw throws `MargoError` `'state'` (`depth()` returned undefined: add `DepthTexture` before the
169
+ first frame, or set `softness` to 0); a depth texture that cannot be sampled or is not the drawing size throws
170
+ `RangeError`; `softness` without the feature or without `depth` throws `TypeError` when the effect is made. Where the
171
+ depth is 0 (nothing drawn) the particle is left alone.
172
+
173
+ ```ts
174
+ import {DepthTexture} from 'margoengine/post/depth';
175
+ import {VFX} from 'margoengine/vfx';
176
+ import {Engine} from 'margoengine/core';
177
+ const engine = await Engine.create(document.querySelector('canvas')!);
178
+ const depth = DepthTexture.attach(engine);
179
+ // Steam that thins out over the last 0.5 m before the floor and walls.
180
+ const steam = VFX.attach(engine, {
181
+ capacity: 20_000, rate: 2_000, position: [0, 0.1, 0], size: 0.4, color: [0.9, 0.9, 1, 0.3],
182
+ softness: 0.5, depth: () => depth.texture,
183
+ });
184
+ ```
185
+
186
+ Measured on an RTX A6000 (Edge, 1280x720, one million particles on screen, 40 frames a batch, 9 alternating batches,
187
+ simulate + draw + the floor depth pass): see the table below.
188
+
189
+ ### GPU cost
190
+
191
+ Each billboard is one triangle around the soft disc (half the primitives and 3/4 of the vertex work of a quad), and
192
+ the simulation lists only particles whose triangle can reach the screen (a conservative clip-space sphere test, so
193
+ nothing visible is dropped). With 4x MSAA the fragment shader keeps exactly the samples the old quad covered. Measured
194
+ on an Intel Arc 140V laptop iGPU (Edge 155, AC power, 1432x768, Afterlight island options and camera, nothing occluding), median
195
+ of 16 alternating rounds against the previous build, all 16 rounds faster:
196
+
197
+ | Particles | MSAA | Before (simulate + draw) | After |
198
+ | --- | --- | --- | --- |
199
+ | 1,000,000 | 1x | 5.00 ms | 2.46 ms |
200
+ | 1,000,000 | 4x | 5.12 ms | 2.40 ms |
201
+ | 100,000 | 1x | 0.28 ms | 0.15 ms |
202
+ | 100,000 | 4x | 0.28 ms | 0.17 ms |
203
+
204
+ In the island demo itself (light tier, 1M particles) the particle passes went from 5.7 to 2.6 ms and the frame's GPU
205
+ span from 15.7 to 13.5 ms. Same particle state and pixels within the old renderer's own run-to-run blend-order noise
206
+ (numbers, scripts and the pixel comparison: `examples/package-benchmarks/vfx-gpu.mjs`, `vfx-island.mjs`, `results/`).
207
+
208
+ Since then births and the draw list follow slot order (four small passes of workgroup prefix sums around the
209
+ simulation instead of atomic tickets), so two runs of the same frames draw the same pixels; before, two runs of
210
+ one still frame differed on up to 6,000 of 65,536 pixels in the browser check. The simulation's compute time on an RTX
211
+ A6000 (Edge, 8 alternating rounds against the atomic build) went from 0.094 to 0.184 ms at 1,000,000 particles and
212
+ from 0.010 to 0.031 ms at 100,000; the draw is unchanged.
213
+
214
+ ### Load only what you use
215
+
216
+ `VFX` from `@margoengine/vfx` (31,135 B min+gzip) carries every feature. An effect that uses none of them can
217
+ import the same class from `@margoengine/vfx/base` (11,188 B min+gzip); add features as a second argument from their own
218
+ entries: `/obstacles` (1.1 kB alone), `/wrap` (0.2 kB), `/sort` (1.1 kB), `/texture` (1.5 kB; it exports
219
+ `textureFeature`, passed as `{texture: textureFeature}`), `/curves` (1.6 kB), `/emitter` (2.4 kB), `/soft` (1.3 kB), `/ribbons` (2.6 kB, the quality gate's figure), `/mesh` (3.2 kB, the same), `/events` (2.3 kB, the same), `/lit` (1.7 kB, the same), `/depth-collide` (2.0 kB, the same), `/stretch` (0.8 kB, the same), `/fields` (1.8 kB, the same), `/skinned` (2.1 kB, the same), `/flipbook` (0.9 kB, 911 B min+gzip, the quality gate's figure), `/shadows` (2.8 kB, 2,805 B min+gzip, the quality gate's figure) and `/nodes` (3.1 kB, 3,093 B min+gzip, the quality gate's figure). Sizes are vite library builds with bare imports external,
220
+ each entry compressed on its own ([entry-sizes.json](../../examples/package-benchmarks/results/entry-sizes.json)), so a
221
+ bundle that combines them adds less: the whole root is 31,135 B. `node examples/package-benchmarks/vfx-size.mjs` measures base-plus-feature bundles
222
+ (`results/vfx-size.json`, at an earlier commit, `76af7a0`).
223
+
224
+ ```ts
225
+ import {EngineKernel} from 'margoengine/core';
226
+ import {VFX} from 'margoengine/vfx/base';
227
+ import {sort} from 'margoengine/vfx/sort';
228
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
229
+ const smoke = VFX.attach(engine, {capacity: 4096, sort: true}, {sort});
230
+ ```
231
+
232
+ ```ts
233
+ import {EngineKernel} from 'margoengine/core';
234
+ import {VFX} from 'margoengine/vfx/base';
235
+ import {curves} from 'margoengine/vfx/curves';
236
+ import {emitter} from 'margoengine/vfx/emitter';
237
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
238
+ const fountain = VFX.attach(
239
+ engine,
240
+ {capacity: 8192, emitter: {shape: 'sphere', radius: 0.2, shell: true, speed: 2}, curves: {alpha: [0, 1, 0]}},
241
+ {curves, emitter},
242
+ );
243
+ ```
244
+
245
+ Either way the WGSL, uploads and GPU calls are byte-identical for every feature combination (`test/split.test.js`;
246
+ fixtures recorded from the root entry). An option whose feature is missing throws a `TypeError` naming the entry.
247
+
248
+ `initialCount` queues an initial burst on attachment and again after device
249
+ restoration. It defaults to 0 and must be an integer from 0 through capacity.
250
+ For a continuously populated field, use `initialCount: capacity`; this replaces
251
+ application restoration hooks. Restoration reseeds particles rather than preserving
252
+ their previous positions and ages.
253
+
254
+ `gpuState` exposes the current particle, indirect, draw-list (`alive`, the draw order) and `visible` buffers for tests/custom integrations. Each particle has 16 floats (64 bytes), split in two halves so the per-frame update reads and writes only the first: particle i's position/life and velocity/size are floats `8i..8i+7`; its color and rotation (birth angle, angular velocity, initial life, unused) are floats `8n+8i..8n+8i+7` (n = capacity). The drawn angle is birth angle + angular velocity x (initial life - remaining life), plus the `overLife` spin curve's turn; the stored birth angle never changes. `indirect` is five 32-bit words whose second word counts live particles; `visible` holds the draw's `drawIndirect` arguments (vertex count 3, then the number of on-screen particles listed at the start of `alive`). The current particle buffer is stable across frames and observes later updates; it is replaced after device restoration. `dispose()`/`destroy()` releases owned buffers. Device restoration recreates simulation resources. Attach multiple emitters independently; none enter the core/stage bundle.
255
+
256
+ ### Ribbons and trails
257
+
258
+ `ribbons` leaves a ribbon behind every particle, for smoke, magic and tracer trails: a strip through the particle and
259
+ its last positions, turned to face the camera, fading from the particle's own opacity at the head to nothing at the tail
260
+ and softly across its width. The `ribbons` feature records every live particle's position each `interval` seconds
261
+ (default 1/60; 0 records every frame) into a ring of `points - 1` records per particle (`points`, the particle included,
262
+ 2 to 64, default 16), so a ribbon covers about `(points - 1) x interval` seconds of flight. `width` (world units) defaults
263
+ to the particle `size`; `heads: false` draws the ribbons without the billboards. Each record is stamped with the
264
+ particle's life, so a particle that dies and is born again starts a new ribbon instead of joining the old one, and a
265
+ young particle's ribbon is as long as its life so far.
266
+
267
+ ```ts
268
+ import {EngineKernel} from 'margoengine/core';
269
+ import {VFX} from 'margoengine/vfx/base';
270
+ import {ribbons} from 'margoengine/vfx/ribbons';
271
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
272
+ // Magic sparks that leave half-second trails, drawn without their heads.
273
+ const trails = VFX.attach(engine, {
274
+ capacity: 2_000, rate: 400, position: [0, 1, 0], velocity: [0, 2, 0], spread: 2, size: 0.05,
275
+ lifetime: [1, 2], gravity: [0, -1, 0], turbulence: 2, color: [0.4, 0.7, 1, 1], intensity: 3,
276
+ ribbons: {points: 31, interval: 1 / 60, width: 0.04, heads: false},
277
+ }, {ribbons});
278
+ engine.start();
279
+ ```
280
+
281
+ Ribbons draw after the billboards in a pass of their own, alpha blended and depth tested (not written), one triangle
282
+ strip of `2 x points` vertices for every slot in slot order, so the same frames give the same pixels. The ring takes
283
+ `capacity x (points - 1) x 16` bytes, at most 64 MB (`capacity x (points - 1)` up to 4,194,304); every slot runs the
284
+ ribbon's vertices each frame, live or not, so ribbons suit thousands of particles, not a million. Ribbons are not fogged,
285
+ softened or drawn in an order-independent pass, and `interval`, `width` and `heads` may change every frame while
286
+ `points` is fixed at attach. `node packages/vfx/test/features-check.mjs` (real GPU) flies a particle at a known speed and
287
+ checks its ribbon's alpha along the centre row against the fades computed in the test, at 1x and 4x MSAA, every frame
288
+ and every second frame, and that a particle born again never joins its old ribbon.
289
+
290
+ ### Mesh particles
291
+
292
+ `mesh` draws every particle as a small triangle mesh instead of a billboard: debris, leaves, shell casings, coins. The
293
+ mesh is scaled so its farthest vertex from the mesh origin sits half the particle `size` away, takes the particle colour,
294
+ and is lit by one directional light (`light`, the direction towards it, default `[0.4, 1, 0.3]`; `lightColor`, linear
295
+ RGB, default white) plus `ambient` (0 to 1, default 0.35) on faces turned away. `align: 'random'` (the default) tumbles
296
+ each particle about its own random axis by its spin (`angularVelocity`); `align: 'velocity'` points the mesh's +y along
297
+ the particle's motion, so long shapes fly nose first. Without `normals` each triangle is shaded flat by its face normal
298
+ (counter-clockwise triangles face outwards).
299
+
300
+ ```ts
301
+ import {EngineKernel} from 'margoengine/core';
302
+ import {VFX} from 'margoengine/vfx/base';
303
+ import {mesh} from 'margoengine/vfx/mesh';
304
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
305
+ // Tumbling stone chips: a cube of 12 triangles for each particle.
306
+ const h = 1;
307
+ const cube = {
308
+ positions: [-h,-h,-h, h,-h,-h, h,h,-h, -h,h,-h, -h,-h,h, h,-h,h, h,h,h, -h,h,h],
309
+ indices: [4,5,6, 4,6,7, 1,0,3, 1,3,2, 5,1,2, 5,2,6, 0,4,7, 0,7,3, 7,6,2, 7,2,3, 0,1,5, 0,5,4],
310
+ };
311
+ const chips = VFX.attach(engine, {
312
+ capacity: 5_000, rate: 500, position: [0, 0.5, 0], velocity: [0, 4, 0], spread: 2, size: 0.15,
313
+ lifetime: [2, 3], angularVelocity: 6, turbulence: 0, color: [0.55, 0.5, 0.45, 1],
314
+ mesh: {...cube, align: 'random', light: [0.3, 1, 0.5], ambient: 0.3},
315
+ }, {mesh});
316
+ engine.start();
317
+ ```
318
+
319
+ The meshes are drawn instanced in one indirect draw over the billboards' own list of on-screen particles (the GPU
320
+ copies its count into the draw's arguments, no read-back), opaque, back faces culled and depth written, so they hide
321
+ each other and anything drawn later behind them; their alpha is ignored and they are not fogged, softened or drawn in an
322
+ order-independent pass. Up to 65,536 triangles a mesh; every corner runs for every drawn particle, so keep meshes small
323
+ (a 12-triangle cube for 100,000 particles is 3.6 million vertices a frame). The geometry and `align` are read at attach;
324
+ `light`, `lightColor` and `ambient` are read every frame from the same object. `node packages/vfx/test/features-check.mjs`
325
+ (real GPU) checks a cube lit head-on for its exact lit colour and width and that it hides a cube behind it, at 1x and
326
+ 4x MSAA, a cube turned by its random axis for the shade of the face turned to the camera, and a long box following its
327
+ velocity.
328
+
329
+ ### Solid particle system: many meshes as one mesh
330
+
331
+ `@margoengine/vfx/solid` is a CPU-driven solid particle system for what the GPU `mesh` feature cannot do: a few
332
+ thousand pieces (bricks of a wall, a shattering vase, a flock, a scatter of props) that each have their own colour,
333
+ scale and rotation, can be hidden or killed, and can be picked with a ray. Many shapes are merged into one vertex and
334
+ index buffer, so the whole system is one draw. Build it from `addShape(shape, count, options?)` (copies of a mesh) or
335
+ `digest(shape, {number | facetNb})` (the mesh split into pieces of whole triangles, `number` winning over `facetNb`,
336
+ default 1 triangle each), call `build()` once, then each frame `setParticles(start?, end?)` runs your `updateParticle(p)`
337
+ over the range and rewrites only those vertices.
338
+
339
+ | Particle field | Default | Meaning |
340
+ | --- | --- | --- |
341
+ | `position`, `scaling` | `[0,0,0]`, `[1,1,1]` | world position; scale per axis, applied before the rotation |
342
+ | `rotation`, `rotationQuaternion` | `[0,0,0]`, `null` | Euler radians (roll, pitch, then yaw), or a quaternion `[x,y,z,w]` that wins when set |
343
+ | `color` | white | rgba 0 to 1, written to every vertex |
344
+ | `uvs` | `[0,0,1,1]` | the rectangle `[u0,v0,u1,v1]` the shape's uvs map into (a sprite sheet cell) |
345
+ | `alive`, `isVisible` | `true` | dead: not updated, not drawn, not picked; invisible: updated but collapsed to a point, not picked |
346
+ | `boundingBox` | | world box as last written; `getBounds()` joins the drawn particles |
347
+
348
+ ```ts
349
+ import {EngineKernel} from 'margoengine/core';
350
+ import {SolidParticleSystem} from 'margoengine/vfx/solid';
351
+ import {UnlitMaterial, createMesh} from 'margoengine/materials';
352
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend: 'webgpu'});
353
+ const h = 0.5;
354
+ const cube = {
355
+ positions: [-h,-h,-h, h,-h,-h, h,h,-h, -h,h,-h, -h,-h,h, h,-h,h, h,h,h, -h,h,h],
356
+ indices: [4,5,6, 4,6,7, 1,0,3, 1,3,2, 5,1,2, 5,2,6, 0,4,7, 0,7,3, 7,6,2, 7,2,3, 0,1,5, 0,5,4],
357
+ };
358
+ const sps = new SolidParticleSystem({
359
+ updateParticle(p) { p.rotation[1] += 0.02; p.position[1] = 1 + Math.sin(p.idx + p.rotation[1]); },
360
+ });
361
+ sps.addShape(cube, 200, {
362
+ positionFunction: (p, i) => { p.position = [(i % 20) * 1.5 - 14, 0, Math.floor(i / 20) * 1.5 - 7]; },
363
+ color: [0.9, 0.5, 0.2, 1],
364
+ });
365
+ const geometry = sps.build(); // one merged mesh: positions, normals, uvs, colors (rgba), indices
366
+ const mesh = createMesh(engine, geometry, new UnlitMaterial({})); // vertex colours are on by default
367
+ engine.onFrame(() => {
368
+ sps.setParticles(); // or setParticles(0, 99) for a range
369
+ mesh.setGeometry(geometry, sps.takeDirty()!); // uploads only the vertices written
370
+ });
371
+ // Pick: the nearest drawn triangle along a ray, in world units.
372
+ const hit = sps.pick({origin: [0, 5, 20], direction: [0, -0.2, -1]});
373
+ if (hit) sps.particles[hit.particleIndex].alive = false; // {particleIndex, shapeId, faceId, localFaceId, distance, point}
374
+ engine.start();
375
+ ```
376
+
377
+ **One merged buffer, not instancing per shape.** Instancing would need a draw per shape and a per-instance colour and
378
+ transform path, and the CPU would still have to transform the geometry to pick it. A merged buffer rebuilt on the CPU
379
+ is one draw however many shapes there are, supports per-particle colour and uvs and any mix of shapes, and picking tests
380
+ exactly the vertices that were uploaded. The price is CPU work in proportion to the vertices written: `setParticles`
381
+ with a range, or the vertex range of `takeDirty()`, keeps a frame to the particles that moved. Colours are in the
382
+ geometry's `colors` stream, which `createMesh` of `@margoengine/materials` reads (`vertexColors` on); the core `Mesh`
383
+ has no vertex colours.
384
+
385
+ `pick` walks each drawn particle's box (slab test) and then its triangles with the Moller-Trumbore test; the ray
386
+ direction is normalised, so `distance` is in world units. Options: `maxDistance` (default `Infinity`) and `backFaces`
387
+ (default `true`). Hidden and dead particles are never hit.
388
+
389
+ Errors: a bad shape, count, range, option or non-finite particle value throws `TypeError` or `RangeError` naming the fix;
390
+ `addShape` after `build()`, `setParticles` or `pick` before it, an empty `build()`, or `setParticles` from inside
391
+ `updateParticle` throw `MargoError` `'state'`; any use after `dispose()` throws `'disposed'`. Limits: 1,000,000
392
+ particles and 1,000,000 vertices in all (the material mesh's own limit).
393
+ `node scripts/run-with-timeout.mjs --max=8 -- node packages/vfx/test/solid-check.mjs` (real GPU; `MARGO_COMPAT=1` for
394
+ the WebGL2 layer) draws four coloured boxes from one merged mesh, checks each box's colour at its projected centre, that
395
+ a hidden box shows the background, that rays pick the box drawn there, and a ranged upload after a move.
396
+
397
+ Sources (clean room, from the papers and the rivals' public feature descriptions, no rival code read): Reeves,
398
+ "Particle Systems - a Technique for Modeling a Class of Fuzzy Objects", ACM TOG 2(2), 1983; Carucci, "Inside Geometry
399
+ Instancing", GPU Gems 2 ch. 3, 2005 (the instancing alternative weighed above); Moller and Trumbore, "Fast, Minimum
400
+ Storage Ray-Triangle Intersection", Journal of Graphics Tools 2(1), 1997.
401
+
402
+ ### Sub-emitters and GPU events
403
+
404
+ `events` gives particles children: when a particle is born from the emitter (`on: 'birth'`: a spark trail, a muzzle
405
+ flash), dies (`on: 'death'`), or meets the ground faster than `speed` world units a second (`on: 'collision'`, default
406
+ 0.5), it raises an event on the GPU, and in the next frame `count` particles (1 to 64, default 8) are born where it was
407
+ into free slots of the same effect. They start with `inherit` (default 0) of
408
+ the parent's velocity at that moment plus `velocity` and a random `spread`, and take their own `lifetime`, `size` and
409
+ `color` (times `intensity`). Fireworks that burst where the rocket burns out, sparks where debris strikes the floor,
410
+ splashes where rain lands. Children never raise events of their own, so one event gives one generation.
411
+
412
+ ```ts
413
+ import {EngineKernel} from 'margoengine/core';
414
+ import {VFX} from 'margoengine/vfx/base';
415
+ import {events} from 'margoengine/vfx/events';
416
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
417
+ // Fireworks: rockets climb for about a second and burst into 48 sparks where they burn out.
418
+ const fireworks = VFX.attach(engine, {
419
+ capacity: 20_000, rate: 3, position: [0, 0, 0], velocity: [0, 12, 0], spread: 2, size: 0.15,
420
+ lifetime: [0.9, 1.2], gravity: [0, -4, 0], turbulence: 0, color: [1, 0.8, 0.5, 1], intensity: 4,
421
+ events: {on: 'death', count: 48, lifetime: [1, 1.6], size: 0.08, spread: 6, inherit: 0.3, color: [1, 0.4, 0.8, 1]},
422
+ }, {events});
423
+ // Muzzle sparks: every shot the emitter fires puts 6 sparks where it was born.
424
+ const gun = VFX.attach(engine, {
425
+ capacity: 2_000, rate: 0, position: [0, 1, 0], velocity: [30, 0, 0], lifetime: [0.4, 0.4], size: 0.02, turbulence: 0,
426
+ events: {on: 'birth', count: 6, lifetime: [0.1, 0.25], size: 0.04, spread: 3, color: [1, 0.7, 0.3, 1]},
427
+ }, {events});
428
+ gun.burst(1);
429
+ engine.start();
430
+ ```
431
+
432
+ Events are found, listed and turned into births entirely on the GPU in slot order (no atomics, no read-back), so the
433
+ same frames give the same particles byte for byte. Children take only free slots: when there are fewer free slots
434
+ than children wanted, the lowest events in slot order get theirs first and the rest are dropped; the emitter's own
435
+ births (`rate`, `burst`) come first. Collision events come from the ground plane only, not from `obstacles`. With
436
+ events on, the effect counts its free slots every frame, two small dispatches it skips without them, and adds two
437
+ storage buffers to the simulation (eight is the device minimum: events with a mesh `emitter` use all eight). On the
438
+ WebGL2 compatibility layer every kernel of an effect with events runs on the GPU, none on the layer's CPU tier. `on` is
439
+ read at attach; the other settings every frame. Both GPU runners also check disposal and device restoration during read-back, and centres at clip depths 0.98, 1, 1.02 and 1.08. `node packages/vfx/test/features-check.mjs` (real GPU) reads back the
440
+ children of a death and of an impact (position, velocity, life, size, colour, generation), fewer children when slots
441
+ run out, none from a slow impact or from a child, and the same bytes from the same frames.
442
+
443
+ ### Lit particles
444
+
445
+ `lit` shades each billboard as a ball of its own size, so smoke, dust and steam take light from the side it comes
446
+ from: the pixel's normal comes from where it lies on the disc (turning with the particle's spin), and its colour is
447
+ the particle colour (texture and curves included) lit by the engine's sun (`engine.lighting`), the ambient light, and
448
+ optionally `Clustered` point and spot lights and `Shadows` sun shadows. The sun uses wrap lighting: `wrap` (0 to 1,
449
+ default 0.5) lets light reach round the dark side, as it does through thin smoke. Clustered lights use the clustered
450
+ shader's own light loop (falloff, cones, IES and local shadows) at roughness 1 and no metal; sun shadows are looked up
451
+ at the ball's surface point.
452
+
453
+ ```ts
454
+ import {EngineKernel} from 'margoengine/core';
455
+ import {VFX} from 'margoengine/vfx/base';
456
+ import {lit} from 'margoengine/vfx/lit';
457
+ import {Clustered} from 'margoengine/clustered';
458
+ import {Shadows} from 'margoengine/shadows';
459
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
460
+ const lights = Clustered.attach(engine);
461
+ lights.setLights([{position: [1, 1.5, 0], range: 8, color: [1, 0.6, 0.3], intensity: 6}]);
462
+ const shadows = Shadows.attach(engine);
463
+ // Grey smoke rising past an orange lamp, shadowed where the sun is blocked.
464
+ const smoke = VFX.attach(engine, {
465
+ capacity: 4096, rate: 400, velocity: [0, 0.8, 0], spread: 0.3, gravity: [0, 0.2, 0], size: 0.6,
466
+ lifetime: [2, 4], color: [0.6, 0.6, 0.6, 0.35], lit: {lights, shadows, wrap: 0.6},
467
+ }, {lit});
468
+ engine.start();
469
+ ```
470
+
471
+ Under an order-independent pass (`addEffect` from `@margoengine/oit/vfx`) lit particles are lit the same way: the
472
+ pass's declarations take bind group 1 and the lights and shadows move to group 2, within the four groups every
473
+ device offers. `lights` and `shadows` are read at attach; `wrap` and `ambient` (linear RGB, default the engine's
474
+ `radiance[3]` times `ambient`) every frame, on the same object. Lighting is per pixel on the billboards only: ribbons
475
+ keep their own fade, and mesh particles their own light. `node packages/vfx/test/features-check.mjs` (real GPU)
476
+ compares every third pixel of a sun-lit particle with the wrap-lit ball shading at any spin and at 1x and 4x MSAA;
477
+ `node packages/vfx/test/lit-check.mjs` checks a real engine's clustered point light, a shadow caster and both again
478
+ in an `ExactOIT` pass against the same light computed on the CPU.
479
+
480
+ ### Depth buffer collision
481
+
482
+ `depthCollide` makes particles bounce off anything drawn on screen, with no colliders to set up: each frame, before
483
+ the simulation, every particle on last frame's draw list is projected with last frame's camera onto last frame's
484
+ scene depth (the `depth` option, as for `softness`). A particle that meets the surface under it, within `thickness`
485
+ (world units, default 0.5) behind it or less than its radius in front, has the part of its velocity going into the
486
+ surface reflected and scaled by the effect's `bounce`, and moves out until it is a radius in front. The surface
487
+ normal comes from the depth of the neighbouring pixels, each axis taking the neighbour nearer in depth, so a particle
488
+ at an object's edge is not bent by the wall behind it.
489
+
490
+ ```ts
491
+ import {EngineKernel} from 'margoengine/core';
492
+ import {VFX} from 'margoengine/vfx/base';
493
+ import {depthCollide} from 'margoengine/vfx/depth-collide';
494
+ import {DepthTexture} from 'margoengine/post/depth';
495
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
496
+ const depth = DepthTexture.attach(engine);
497
+ // Sparks that bounce off the floor, the walls and every prop in view.
498
+ const sparks = VFX.attach(engine, {
499
+ capacity: 8192, rate: 600, position: [0, 2, 0], velocity: [0, 2, 0], spread: 4, size: 0.04, bounce: 0.4,
500
+ lifetime: [1, 2], color: [1, 0.7, 0.3, 1], intensity: 3, depth: () => depth.texture,
501
+ depthCollide: {thickness: 0.3},
502
+ }, {depthCollide});
503
+ engine.start();
504
+ ```
505
+
506
+ Only what the camera saw can be hit: a particle off screen, or behind something nearer (more than `thickness`
507
+ behind the surface in front of it), flies on, and a particle fast enough to cross a thin object between two frames
508
+ can pass through it. Collisions start the frame after the depth texture exists and use the frame before's depth, so
509
+ attach `DepthTexture` at its default order (-5, after the particles simulate). One small dispatch a frame over the
510
+ listed particles; with `thickness` and `bounce` read every frame. `node packages/vfx/test/features-check.mjs` (real
511
+ GPU) reads back a particle bouncing off a wall and a 45 degree slope, at an object's edge in front of a far wall,
512
+ behind a wall, and without the feature, against the same frames stepped on the CPU.
513
+
514
+ ### Velocity-stretched billboards
515
+
516
+ `stretch` turns each billboard into a streak along its motion, for sparks, rain and tracers: the long axis follows the
517
+ particle's velocity across the screen and the billboard is `size + factor x speed` long, `factor` seconds of travel
518
+ (default 0.05), and keeps `size` across. A particle with no motion across the screen (flying straight at the camera)
519
+ stays round, and spin is ignored, since a streak points where it flies. The draw list's cull sphere grows to the
520
+ streak's length, so a streak reaching into view is never dropped.
521
+
522
+ ```ts
523
+ import {EngineKernel} from 'margoengine/core';
524
+ import {VFX} from 'margoengine/vfx/base';
525
+ import {stretch} from 'margoengine/vfx/stretch';
526
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
527
+ // Sparks that streak as they fly and shorten as they slow down.
528
+ VFX.attach(engine, {
529
+ capacity: 4096, rate: 400, position: [0, 1, 0], velocity: [0, 4, 0], spread: 6, size: 0.03, lifetime: [0.5, 1],
530
+ color: [1, 0.6, 0.2, 1], intensity: 4, stretch: {factor: 0.04},
531
+ }, {stretch});
532
+ engine.start();
533
+ ```
534
+
535
+ `factor` is read every frame (0 draws round billboards again). The streak is the same soft disc as a round
536
+ billboard, drawn longer, so it works with `texture`, `soft`, `curves` and `lit` unchanged; mesh particles and ribbons
537
+ keep their own shapes. `node packages/vfx/test/features-check.mjs` (real GPU) compares every pixel along and across a
538
+ fast particle's streak, flying across and up the screen, at 1x and 4x MSAA, and with factor 0, against the stretched
539
+ disc computed on the CPU.
540
+
541
+ ### Force fields
542
+
543
+ `fields` adds point attractors and a 3D vector field to each particle's acceleration, alongside gravity and
544
+ turbulence: for portals, vortices, wind volumes and magic that gathers to a point. An attractor pulls with `strength
545
+ x d / (|d|^2 + radius^2)^1.5` (an inverse square, softened by `radius`, default 0.5, so it stays finite at its centre);
546
+ a negative strength pushes. The vector field holds one acceleration per cell over a box, trilinear between the cell
547
+ centres, the edge cells' value out to the box's faces and nothing outside it, times `strength`. Up to 16 attractors;
548
+ up to 2,097,152 cells (128^3).
549
+
550
+ ```ts
551
+ import {EngineKernel} from 'margoengine/core';
552
+ import {VFX} from 'margoengine/vfx/base';
553
+ import {fields} from 'margoengine/vfx/fields';
554
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
555
+ // A swirl: each cell of an 8^3 field pushes around the y axis.
556
+ const n = 8, data = new Float32Array(n * n * n * 3);
557
+ for (let z = 0; z < n; z++) for (let y = 0; y < n; y++) for (let x = 0; x < n; x++) {
558
+ const i = (x + n * (y + n * z)) * 3;
559
+ data[i] = -(z - n / 2 + 0.5); data[i + 2] = x - n / 2 + 0.5;
560
+ }
561
+ const effect = VFX.attach(engine, {
562
+ capacity: 8192, rate: 800, position: [0, 0, 0], spread: 1, gravity: [0, 0, 0], size: 0.03, lifetime: [2, 4],
563
+ color: [0.4, 0.8, 1, 1], intensity: 2,
564
+ fields: {attractors: [{position: [0, 1, 0], strength: 2}], field: {size: [n, n, n], data, halfSize: [2, 2, 2]}},
565
+ }, {fields});
566
+ engine.start();
567
+ // Attractors, the box and strengths are read every frame.
568
+ effect.options.fields!.attractors![0]!.strength = -2;
569
+ ```
570
+
571
+ The field's cells are copied at attach, and whether an effect has a field is fixed then; attractors, the box's centre
572
+ and size and both strengths can change every frame. With `obstacles` the flow bends around them with the field forces
573
+ in it, as it does with gravity. `node packages/vfx/test/features-check.mjs` (real GPU) reads back particles under an
574
+ attractor and a repeller, and in a linear field inside the cell centres, in the band at the box's edge and outside it,
575
+ against the same frames stepped on the CPU.
576
+
577
+ ### Skinned mesh emitters
578
+
579
+ `skinned` emits from an animated character: each birth picks a triangle of a skinned or morphed primitive by its
580
+ bind-pose area (a stretched limb does not emit more than at rest) and a uniform point of it in this frame's pose, and
581
+ flies along the posed face's normal times `speed` on top of `velocity` and `spread`. Each frame the posed vertices are
582
+ copied from the deformer's output (`primitive.deformed` at `deformedOffset`, both read every frame, so a re-packed
583
+ batch is followed); until a deformer writes, births use the bind pose. `transform` (16 numbers, column-major, read
584
+ every frame) places the mesh in the world; the emitter's `position` is not used with `skinned`, and it cannot be
585
+ combined with `emitter`.
586
+
587
+ ```ts
588
+ import {EngineKernel} from 'margoengine/core';
589
+ import {loadGLTF} from 'margoengine/gltf/pbr';
590
+ import {Animation} from 'margoengine/animation';
591
+ import {VFX} from 'margoengine/vfx/base';
592
+ import {skinned} from 'margoengine/vfx/skinned';
593
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
594
+ const model = await loadGLTF(engine, '/RobotExpressive.glb');
595
+ Animation.attach(model).play('Walking');
596
+ const body = model.primitives[0]!;
597
+ // Embers rising off the robot as it walks.
598
+ VFX.attach(engine, {
599
+ capacity: 8192, rate: 600, spread: 0.2, gravity: [0, 0.5, 0], size: 0.01, lifetime: [0.5, 1.5],
600
+ color: [1, 0.5, 0.1, 1], intensity: 3,
601
+ skinned: {primitive: body, transform: body.node.matrix, speed: 0.3},
602
+ }, {skinned});
603
+ engine.start();
604
+ ```
605
+
606
+ The copy is recorded when the particles simulate, so a deformer that dispatches later in the same frame shows a frame
607
+ later. Births take only the pose: they do not inherit the surface's own motion. `node packages/vfx/test/features-check.mjs`
608
+ (real GPU) emits 2,000 particles from a posed two-triangle primitive, with and without an offset into the deformer's
609
+ buffer and with no deformer yet, and checks that every birth lies on a posed triangle and flies along its normal, and
610
+ that the births split by bind-pose area.
611
+
612
+ ### Flipbook frame blending
613
+
614
+ `flipbook` blends a lifetime atlas instead of stepping from one cell to the next. The vertex stage passes this frame,
615
+ the next frame (the last frame holds, so it does not wrap) and the fraction between them; the fragment stage samples
616
+ both cells, still half a texel in, and mixes them. It needs `texture` with an atlas whose `frames` are `'lifetime'`
617
+ (a random frame has no fraction). Pass `{}`.
618
+
619
+ ```ts
620
+ import {EngineKernel} from 'margoengine/core';
621
+ import {VFX} from 'margoengine/vfx/base';
622
+ import {textureFeature} from 'margoengine/vfx/texture';
623
+ import {flipbook} from 'margoengine/vfx/flipbook';
624
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
625
+ // Four solid frames, 4 x 4 texels each, blended across each spark's life.
626
+ const w = 8, frames = new Uint8Array(w * w * 4);
627
+ const cell = [255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255];
628
+ for (let i = 0; i < 4; i++) {
629
+ const x0 = (i % 2) * 4, y0 = (i >> 1) * 4;
630
+ for (let y = 0; y < 4; y++) for (let x = 0; x < 4; x++) frames.set(cell.slice(i * 4, i * 4 + 4), ((y0 + y) * w + x0 + x) * 4);
631
+ }
632
+ VFX.attach(engine, {
633
+ capacity: 256, rate: 40, size: 0.2, lifetime: [1, 1], color: [1, 1, 1, 1],
634
+ texture: {width: w, height: w, data: frames, atlas: {columns: 2, rows: 2, frames: 'lifetime'}},
635
+ flipbook: {},
636
+ }, {texture: textureFeature, flipbook});
637
+ engine.start();
638
+ ```
639
+
640
+ On the WebGL2 layer an effect with a prewarm (hundreds of steps) keeps the steps of its first frames until its compatibility route has loaded, then runs them in order on the route's draws, so the prewarm never runs on the layer's CPU tier; an effect with no prewarm simulates every frame on its WebGPU kernels until the route loads, so no frame is drawn differently; the `overLife` feature reads an effect's stored life and birth velocity before storing, so its route kernels plan as GPU draws too (`test/route-loading.test.js`).
641
+
642
+ On WebGPU the mix is in the billboard fragment shader. On the WebGL2 route the same shader translates to GLSL ES 3.00
643
+ (`mix` of the two cell samples) and the route's draw wrapper still drops instances past the frame's count.
644
+ `node packages/vfx/test/flipbook-webgl2.mjs` runs that page on the compatibility layer: on an RTX A6000 the same two
645
+ centre pixels read 111,111,0 and 0,79,79, within 1 of the CPU mix 112,112,0 and 0,80,80.
646
+ `node packages/vfx/test/features-check.mjs` (real GPU) holds two particles halfway between frames of a 2 x 2 solid
647
+ atlas, one of them wrapping onto the next row, and checks each centre pixel against the same mix computed on the CPU.
648
+
649
+ The WebGL2 visibility kernel retains the simulation declarations and feature bindings used by its cull expression. The full GPU suite checks velocity stretch on that route as well as native WebGPU. Lit particles use weighted `OIT` on WebGL2; the suite also requires the documented `ExactOIT` rejection there, while native WebGPU keeps its exact-pass pixel checks.
650
+
651
+ ### Particle shadows and lights
652
+
653
+ `shadows` makes particles cast sun shadows and emit point lights. Each live particle is a sphere impostor in the directional shadow map: under the cascade's orthographic matrix the front of the sphere is `centerZ - sqrt(r^2 - x^2 - y^2)`. The map's own receiver bias stays in the shadow pass. The first `count` live slots, in slot order, become point lights (default 8, at most 64). `range` is a distance in world units (default 4), not scaled by the particle's size. `intensity` (default 1) is the light's intensity. `decay` is always 2, so a clustered list uses its windowed inverse-square falloff. `cast` is a `Shadows` and `into` is a `Clustered`; either may be left out. `count`, `range` and `intensity` are read every frame. `range` and `intensity` must be finite f32 values. A finite double that overflows f32 is a RangeError (https://margoengine.com/errors#vfx-float). `cast` and `into` are fixed at attach. The feature sets `buffer` on the options object to the light list (a header, then up to 64 lights). `into` sees that list about two frames later, because the read-back waits for the frame to finish.
654
+
655
+ ```ts
656
+ import {EngineKernel} from 'margoengine/core';
657
+ import {VFX} from 'margoengine/vfx/base';
658
+ import {shadows} from 'margoengine/vfx/shadows';
659
+ import {Clustered} from 'margoengine/clustered';
660
+ import {Shadows} from 'margoengine/shadows';
661
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
662
+ const sun = Shadows.attach(engine);
663
+ const lights = Clustered.attach(engine);
664
+ // Sparks that block the sun and light what is near them.
665
+ VFX.attach(engine, {
666
+ capacity: 4096, rate: 200, size: 0.15, lifetime: [1, 2], color: [1, 0.4, 0.1, 1], intensity: 2,
667
+ shadows: {cast: sun, into: lights, count: 8, range: 5},
668
+ }, {shadows});
669
+ engine.start();
670
+ ```
671
+
672
+ The light list is packed with a workgroup prefix sum, so the same slots give the same lights and nothing is chosen with an atomic. The sphere draw is a depth-only pass, one per cascade, and it does not add a storage buffer to the simulation. A centre past the cascade far plane still draws when the front of the sphere is inside the map: the quad's clip depth stays inside the clip volume, and the fragment writes the front. Disposing the effect removes the point lights it published. Disposing the effect, or restoring the device, while the light read-back is still mapping settles that cancelled map. `node packages/vfx/test/features-check.mjs` (real GPU) reads the light buffer of four slots (live, dead, live, live) with `count` 2 and checks a sphere at the origin against the orthographic depth. `node packages/vfx/test/shadows-nodes-webgl2.mjs` runs that page on the WebGL2 compatibility layer: on an RTX A6000 the header is 2, 5, 3, the lights are (1, 2, 3) red and (4, 5, 6) blue, and the depths are 0.45005, 0.48770 and 1.00000. A particle that uses neither shadows nor nodes, with velocity (1, 0, 0), is at (0.20000, 0, 0) after four steps of 0.05 s.
673
+
674
+ ### Particle update nodes
675
+
676
+ `nodes` compiles a graph into the simulation. Each operator is `const`, `attribute` (`position`, `velocity`, `size`, `color`, `life`, `age`), `add`, `mul`, `sub`, `mix`, `min`, `max`, `scale`, `normalize`, `length`, `force` or `drag`. `force` adds a vector to the acceleration. `drag` subtracts a number times the velocity. `normalize` divides by the largest absolute component and then by that vector's length, on the CPU and in the shader. The GPU checks cover (1e-21, 0, 0), (1e20, 0, 0), mixed-component vectors and zero on WebGPU and WebGL2. An operator runs after the ones it reads. A cycle, a self input, an unknown operator or a bad width is a `TypeError`. At most 32 operators, and at most 16 numeric literals. Const and scale numbers are read every frame, and each one must be a finite f32. A finite double that overflows f32 is a RangeError (https://margoengine.com/errors#vfx-float). The operators and the wires are fixed at attach. The acceleration is added after gravity, curl and fields. Velocity moves by that acceleration times the step, then position moves by the new velocity times the step.
677
+
678
+ ```ts
679
+ import {EngineKernel} from 'margoengine/core';
680
+ import {VFX} from 'margoengine/vfx/base';
681
+ import {nodes} from 'margoengine/vfx/nodes';
682
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
683
+ // A constant push upward, with drag on the velocity.
684
+ const graph = [
685
+ {op: 'const', value: [0, 2, 0]},
686
+ {op: 'force', inputs: [0]},
687
+ {op: 'const', value: 0.5},
688
+ {op: 'drag', inputs: [2]},
689
+ ] as const;
690
+ VFX.attach(engine, {
691
+ capacity: 1024, rate: 40, size: 0.15, lifetime: [1, 2], color: [1, 0.6, 0.2, 1],
692
+ nodes: {graph},
693
+ }, {nodes});
694
+ engine.start();
695
+ ```
696
+
697
+ `evaluateNodes(graph, sample)` runs the same operators on the CPU. `node packages/vfx/test/features-check.mjs` (real GPU) holds one particle at velocity (1, 0, 0) under force (0, 2, 0) and drag 0.5, and checks position and velocity after four steps of 0.05 s. `node packages/vfx/test/shadows-nodes-webgl2.mjs` runs the same four steps on the WebGL2 compatibility layer: on an RTX A6000 the particle is at (0.18781, 0.04877, 0.00000) with velocity (0.90369, 0.38525, 0.00000).
698
+
699
+ ### Curves over life
700
+
701
+ `overLife` from `@margoengine/vfx/over-life` shapes each particle over its normalized life with 2 to 32 evenly spaced
702
+ samples per curve: `spin` adds radians per second to `angularVelocity` (each -1000..1000), `speed` multiplies the birth
703
+ velocity (forces stay additive), `size` multiplies the birth size (growth beyond 1 allowed) and `velocityLimit` caps the
704
+ world speed (a number or a curve). Curves are read every frame. The spin is applied where the angle is drawn: the drawn
705
+ angle is birth angle + angular velocity x age + initial life x the spin curve's integral over the life lived, and the
706
+ stored birth angle never moves, so ribbons, random atlas frames and mesh tumble axes stay with their particle.
707
+
708
+ ```ts
709
+ import {EngineKernel} from 'margoengine/core';
710
+ import {VFX} from 'margoengine/vfx/base';
711
+ import {overLife} from 'margoengine/vfx/over-life';
712
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
713
+ // Leaves that spin up, slow down and grow as they fall.
714
+ VFX.attach(engine, {
715
+ capacity: 2048, rate: 60, size: 0.2, lifetime: [2, 3], gravity: [0, -1, 0],
716
+ overLife: {spin: [0, 6], speed: [1, 0.2], size: [1, 1.5], velocityLimit: 3},
717
+ }, {overLife});
718
+ engine.start();
719
+ ```
720
+
721
+ ### Moving emitters: local space, inherited velocity and distance emission
722
+
723
+ `space` from `@margoengine/vfx/space` gives an emitter a moving frame. `transform` is a column-major rigid 4x4 matrix
724
+ (rotation and translation only; scale and shear throw RangeError), read every step, default identity. `mode: 'local'`
725
+ carries the live particles with the frame as it moves and turns; `'world'` (the default) leaves them where they were born.
726
+ `inheritVelocity` (0..1, default 0) adds that share of the frame origin's velocity to each birth, and `ratePerDistance`
727
+ (0..1,000,000, default 0) adds births per world unit the origin travels, spread along the path and carried across
728
+ frames, so a fast emitter leaves an unbroken trail. Particles stay in world coordinates in their buffers, so collisions,
729
+ fields and other draws read them as usual; `reset()` starts the distance count again without a jump.
730
+
731
+ ```ts
732
+ import {EngineKernel} from 'margoengine/core';
733
+ import {VFX} from 'margoengine/vfx/base';
734
+ import {space} from 'margoengine/vfx/space';
735
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
736
+ // A torch carried in a circle: a trail with eight births a metre, half the torch's speed given to each spark.
737
+ const transform = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]);
738
+ VFX.attach(engine, {
739
+ capacity: 4096, rate: 30, size: 0.05, lifetime: [0.5, 1], velocity: [0, 1, 0], spread: 0.5, color: [1, 0.6, 0.2, 1],
740
+ space: {mode: 'world', transform, inheritVelocity: 0.5, ratePerDistance: 8},
741
+ }, {space});
742
+ let angle = 0;
743
+ engine.onFrame((_engine, delta) => {
744
+ angle += delta;
745
+ transform[12] = 2 * Math.cos(angle);
746
+ transform[14] = 2 * Math.sin(angle);
747
+ });
748
+ engine.start();
749
+ ```
750
+
751
+ ### Prewarm and timed bursts
752
+
753
+ `schedule` from `@margoengine/vfx/schedule` fixes, at attach, a prewarm and a list of bursts. `prewarm` (0..3600 s,
754
+ default 0) runs that much simulation in ordinary steps of `step` seconds (0.001..1, default 1/60, at most 10,000 steps)
755
+ before the first visible frame, so a fire or a fountain starts full. Each burst gives `count` births (whole, up to
756
+ `capacity`) at `time` seconds after the start, repeated `cycles` times (1..16,384, default 1) every `interval` seconds
757
+ (default 0), each repetition happening with chance `probability` (0..1, default 1). The chances are drawn from `seed`
758
+ (an unsigned 32-bit integer, default 0) and each repetition's identity, so a seed gives the same bursts on every run,
759
+ and `reset()` replays the same schedule. An effect holds at most 1,024 burst definitions and 16,384 repetitions; a
760
+ frame's births are capped by its free slots.
761
+
762
+ ```ts
763
+ import {EngineKernel} from 'margoengine/core';
764
+ import {VFX} from 'margoengine/vfx/base';
765
+ import {schedule} from 'margoengine/vfx/schedule';
766
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
767
+ // Fireworks: a smouldering base already lit, a volley of five shells half a second apart, and crackle that may skip.
768
+ VFX.attach(engine, {
769
+ capacity: 8192, rate: 40, size: 0.06, lifetime: [1, 2], velocity: [0, 6, 0], spread: 3, color: [1, 0.8, 0.3, 1],
770
+ schedule: {
771
+ prewarm: 2,
772
+ seed: 7,
773
+ bursts: [
774
+ {time: 0.5, count: 400, cycles: 5, interval: 0.5},
775
+ {time: 3, count: 100, cycles: 20, interval: 0.1, probability: 0.6},
776
+ ],
777
+ },
778
+ }, {schedule});
779
+ engine.start();
780
+ ```
781
+
782
+ ### Node particle systems
783
+
784
+ `@margoengine/vfx/node-system` describes a whole particle system, or several that chain, as a graph of blocks in versioned JSON. It compiles to ordinary `VFX` options (every range is checked by `VFX` itself) and runs the chaining on a clock you drive.
785
+
786
+ Blocks, each with a unique `id`; a wire is a block id in place of a literal:
787
+
788
+ - Values: `number`, `vector`, `color`, `random` (`min`, `max`, optional `seed`, `vector: true` for three draws), `math` (`add`, `sub`, `mul`, `min`, `max`, `mix`), `gradient` (2..8 stops) and `curve` (2..32 samples). A `random` block draws one value per compile, deterministically from its `seed` and the compile `seed` (default 0); fed to a lifetime it stays the range `[min, max]`, so the GPU draws per particle.
789
+ - `shape`: `sphere`, `cone`, `hemisphere`, `cylinder`, `torus`, `rectangle-edge` or `grid`, with that shape's fields.
790
+ - Creation: `create-capacity`, `create-rate`, `create-initial`, `create-lifetime`, `create-size`, `create-color`, `create-position`, `create-velocity` (with `spread`), `create-prewarm`, `create-burst`.
791
+ - Update: `update-size`, `update-alpha`, `update-color` (a gradient), `update-spin`, `update-speed`, `update-velocity-limit`, `update-gravity`, `update-turbulence`, `update-ground`, `update-forces` (the `nodes` operator graph above).
792
+ - `system`: `shape`, `create` and `update` list block ids; `duration` is seconds it runs (default 0: until stopped).
793
+ - `trigger`: `on: 'start' | 'end'` starts `target` after `delay` seconds (default 0) of `source` starting or ending; `on: 'birth' | 'death' | 'collision'` makes `target` the sub-emitter of `source` (`count`, `inherit`, `speed`), built from the target's creation blocks, which then has no shape, update blocks or triggers. Systems no trigger targets start with `start()`.
794
+
795
+ `compileNodeSystem(graph, {seed})` returns `{systems, roots}`; `serializeNodeSystem` and `parseNodeSystem` save and read the JSON; `createNodeSystem(graphOrText, {create})` makes a `NodeParticleSystem` (`start()`, `update(dt)`, `running`, `elapsed`, `dispose()` and `Symbol.dispose`); `loadNodeSystem(url, {create, signal})` fetches one. `create(options, id)` runs when a system starts and may return `{stop, dispose}`; `stop` is called at the end of `duration` (else `dispose`). A refused graph throws `VFXNodeSystemError` with a stable `code` (`format`, `version`, `limit`, `duplicate-id`, `unknown-block`, `unknown-ref`, `wrong-type`, `bad-param`, `cycle`, `no-system`, `trigger`, `invalid-options`) and a link to https://margoengine.com/errors#vfx-node-system. At most 512 blocks and 32 systems.
796
+
797
+ ```ts
798
+ import {VFX} from 'margoengine/vfx';
799
+ import {createNodeSystem, serializeNodeSystem, type NodeSystemGraph} from 'margoengine/vfx/node-system';
800
+ // Fireworks: a rocket for two seconds, then a shower that starts half a second later; each rocket spark bursts when it dies.
801
+ const graph: NodeSystemGraph = {
802
+ format: 'margo-vfx-node-system',
803
+ version: 1,
804
+ blocks: [
805
+ {id: 'rocket', type: 'system', duration: 2, create: ['r-rate', 'r-life', 'r-up'], update: ['r-grav']},
806
+ {id: 'r-rate', type: 'create-rate', value: 20},
807
+ {id: 'r-life', type: 'create-lifetime', value: 'life'},
808
+ {id: 'life', type: 'random', min: 1, max: 1.5},
809
+ {id: 'r-up', type: 'create-velocity', value: [0, 8, 0], spread: 1},
810
+ {id: 'r-grav', type: 'update-gravity', value: [0, -6, 0]},
811
+ {id: 'pop', type: 'system', create: ['p-life', 'p-size', 'p-color']},
812
+ {id: 'p-life', type: 'create-lifetime', value: [0.4, 0.8]},
813
+ {id: 'p-size', type: 'create-size', value: 0.04},
814
+ {id: 'p-color', type: 'create-color', value: [1, 0.6, 0.2, 1]},
815
+ {id: 'burst', type: 'trigger', on: 'death', source: 'rocket', target: 'pop', count: 16, inherit: 0.2},
816
+ {id: 'shower', type: 'system', create: ['s-rate', 'p-color'], update: ['s-fall']},
817
+ {id: 's-rate', type: 'create-rate', value: 60},
818
+ {id: 's-fall', type: 'update-size', curve: [1, 0]},
819
+ {id: 'after', type: 'trigger', on: 'end', source: 'rocket', target: 'shower', delay: 0.5},
820
+ ],
821
+ };
822
+ const text = serializeNodeSystem(graph); // JSON you can save and load again with createNodeSystem(text, ...)
823
+ using show = createNodeSystem(text, {create: (options) => new VFX(options)});
824
+ show.start().update(2.6); // a real app calls update(dt) every frame and attaches each effect to its engine
825
+ console.log(show.running); // ['shower']
826
+ ```
827
+
828
+ ### More emitter shapes
829
+
830
+ `shapes` from `@margoengine/vfx/shapes` replaces the `emitter` feature with a superset: the original shapes plus
831
+ `hemisphere` (`radius`, default 1, the upper half, Y up), `cylinder` (`radius` 1, `height` 1, around Y), `torus`
832
+ (`radius` 1 to the tube's centre, `tube` 0.25, smaller than `radius`, lying in XZ), `rectangle-edge` (the perimeter of a
833
+ `width` by `height` rectangle in XZ, default 1 by 1) and `grid` (`columns`, `rows` and `layers` cells, whole, each
834
+ default 1 and at most 1,000,000 cells in all, `spacing` default `[1, 1, 1]`, centred). Births are spread evenly by
835
+ area or volume: `shell: true` places them on the surface of a hemisphere, cylinder or torus, otherwise they fill it.
836
+ `speed` (-1000..1000 m/s, default 0) adds a push along the shape's outward direction (up for a grid) to the effect's
837
+ `velocity`. All shapes are centred on the effect's `position`; the shape is fixed at attach (its sizes may change).
838
+
839
+ ```ts
840
+ import {EngineKernel} from 'margoengine/core';
841
+ import {VFX} from 'margoengine/vfx/base';
842
+ import {shapes} from 'margoengine/vfx/shapes';
843
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend:'webgpu'});
844
+ // A portal ring: sparks born on the surface of a torus and pushed outward from its tube.
845
+ VFX.attach(engine, {
846
+ capacity: 8192, rate: 1200, position: [0, 1.5, 0], size: 0.03, lifetime: [0.5, 1], gravity: [0, 0, 0],
847
+ velocity: [0, 0, 0], color: [0.4, 0.7, 1, 1],
848
+ emitter: {shape: 'torus', radius: 1.2, tube: 0.1, shell: true, speed: 0.6},
849
+ }, {emitter: shapes});
850
+ engine.start();
851
+ ```
852
+
853
+ ## Film time: seeking an effect to a cut
854
+
855
+ A film rendered scene by scene starts each scene in a fresh process, and every effect counts its own clock: the time,
856
+ the birth remainder of a fractional `rate`, the step number that seeds each birth, queued bursts and the `schedule`
857
+ cursor. `VFX` has `advanceTime(delta)`, the CPU part of its frame update, and a `time` getter; its `onPreUpdate` runs
858
+ the same `advanceTime` and the simulate pass then simulates the step it counted, once per frame however many views
859
+ draw; another view of the frame takes a step of 0 s that lists and sorts the particles for its own camera and moves
860
+ nothing, and an effect no frame update reaches (its passes run directly) steps in each pass by the frame delta. An update with a delta of 0 (a `Reflector`, `Refractor` or `CubeCamera` capture
861
+ inside the frame) counts nothing, so a mirror in view does not move the seeds or drop the prewarm. So `seek` from `@margoengine/cutscene/seek` replays a run's steps and the next frame births what a run started
862
+ at 0 births there (`test/seek.test.js` to 54,000 steps). The particles alive at a cut are history on the GPU: a seek
863
+ moves the counters only, and the scene's warm-up frames, at least the longest lifetime, bring the cloud back. A
864
+ birth is seeded by its step and its birth number in that step, never by the slot it lands in, so the warm-up births
865
+ the very particles a continuous run holds at the cut, in other slots (`test/seek-check.mjs`: every live particle of a
866
+ 2,000 particle effect equal bit for bit, births per step equal). That holds while every birth finds a free slot;
867
+ unsorted particles draw in slot order, so overlapping translucent particles can blend in another order after a seek.
868
+
869
+ ```ts
870
+ import {Engine} from 'margoengine/core';
871
+ import {VFX} from 'margoengine/vfx';
872
+ import {seek} from 'margoengine/cutscene/seek';
873
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
874
+ const sparks = VFX.attach(engine, {capacity: 2000, rate: 600, lifetime: [1, 2]});
875
+ // The scene starts 95 s into the film and draws 2 s of warm-up frames first: seek to 93 s.
876
+ await seek(engine, 93, {fps: 30});
877
+ for (let frame = 0; frame < 60; frame++) engine.render({delta: 1 / 30});
878
+ console.log(sparks.time);
879
+ ```
880
+
881
+ ### A film clock: the same particles at any frame rate
882
+
883
+ Give the effect a `FilmClock` from `@margoengine/math/clock` as `clock` and it steps in fixed ticks of 1/120 s
884
+ (`VFX_TICK_RATE`), as many as the clock owes since the last drawn frame, instead of one step of the frame's delta. Each
885
+ tick has its exact time (`ticks / 120`), its births and the step number that seeds them, and the features see the tick's
886
+ delta. So the same shot drawn at 24, 30, 60 or 5 fps holds the same particle buffer, every byte, at every time those
887
+ rates share; a seek is setting the clock (the next drawn frame runs every tick owed, and a seek back restarts the effect
888
+ and replays from 0); and no frame, however long, loses time (`test/film-clock.test.js` for the counters and uniforms,
889
+ `test/film-clock-check.mjs` for the particle buffer on WebGPU and the WebGL2 layer). `advanceTime` and `onPreUpdate`
890
+ move nothing while a clock is set; frames closer than a tick take the 0 s step a second view takes. A 60 fps frame runs
891
+ two steps of 1/120 s where it ran one of 1/60 s, a finer integration of the same motion. Every owed tick runs in the
892
+ next drawn frame, so a jump of T seconds costs 120 T simulation passes in that frame.
893
+
894
+ ```ts
895
+ import {Engine} from 'margoengine/core';
896
+ import {VFX} from 'margoengine/vfx';
897
+ import {FilmClock} from 'margoengine/math/clock';
898
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
899
+ const clock = new FilmClock({fps: 24});
900
+ const sparks = VFX.attach(engine, {capacity: 2000, rate: 600, lifetime: [1, 2], clock});
901
+ for (let frame = 0; frame <= 48; frame++) {
902
+ clock.frame = frame;
903
+ engine.render({delta: 1 / 24});
904
+ }
905
+ console.log(sparks.time); // 2: 240 ticks, the same particles a 60 fps render holds at 2 s
906
+ ```
907
+
908
+ `Flame` and `Smoke` are clocks too: each has `advanceTime(delta)` and `time`, and works everything it draws (the
909
+ puffing, the light's flicker, the dither shift, the wind it samples, a smoke column's speed and size curves and its
910
+ puffs' rate and wind) from that clock, so a seek moves them with the effects and a flame or column at a cut draws what a
911
+ continuous run draws there (`test/seek.test.js`, uniforms, lights and curves equal to 54,000 steps). A flame's noise
912
+ seed follows its place among the flames of its engine, so a scene drawn after another in the same process seeds its
913
+ flames as a fresh process does.
914
+
915
+ Several cuts in one process: `resetTime()` puts each vfx clock back as a fresh one with the same options has it
916
+ (`resetClocks` from `@margoengine/cutscene/seek` calls it on every clock), so a later `seek` gives what a fresh world
917
+ seeked there gives. `VFX.resetTime()` is `reset()` as a clock (particles, counters, step seeds, queued bursts and the
918
+ frame-update flag; nothing after `dispose()`). `Smoke` resets its puffs with it; `FireFront` unburns every cell,
919
+ queues the `ignitions` of its options again (ones added with `ignite` are dropped), restarts its random stream from
920
+ `seed` and idles its columns, flames and embers; `Battery` queues its `shots` again (ones added with `fire` or
921
+ `broadside` are dropped), idles its banks, flashes and lights and clears the holes it drew into its targets' masks;
922
+ `HullFire` repaints its mask for time 0. Evidence: `test/reset-time.test.js` (state of a reset clock equals a fresh
923
+ one's, before and after a seek) and `test/seek-check.mjs` (on the GPU, a reset effect drawn beside a fresh one holds the
924
+ same live particles, bit for bit, on WebGPU and on the WebGL2 layer).
925
+
926
+ ```ts
927
+ import {Engine} from 'margoengine/core';
928
+ import {VFX} from 'margoengine/vfx';
929
+ import {resetClocks, seek} from 'margoengine/cutscene/seek';
930
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
931
+ VFX.attach(engine, {capacity: 2000, rate: 600, lifetime: [1, 2]});
932
+ for (let frame = 0; frame < 60; frame++) engine.render({delta: 1 / 30});
933
+ // The next scene of the film starts at 40 s: put every clock back to 0, then seek.
934
+ resetClocks(engine);
935
+ await seek(engine, 40, {fps: 30});
936
+ ```
937
+
938
+ ### Wind
939
+
940
+ `wind: [x, z]` (m/s) or `{sample(x, y, z, t)}` (a `WindField` or a `WeatherMachine`'s `wind`) makes every particle's horizontal
941
+ velocity relax toward the wind with a time constant of 0.25 s, the drag of a parcel that moves with the air (Stokes
942
+ drag, dv/dt = (u - v) / tau; Crowe, Schwarzkopf, Sommerfeld and Tsuji, Multiphase Flows with Droplets and Particles,
943
+ 2nd ed., CRC Press 2011, chapter 1). Smoke that rises at w in a wind U leans atan(U / w) from the vertical once it has
944
+ had about a second; the vertical velocity stays with gravity and the launch. A field is read at the emitter every
945
+ frame. `setWind(wind)` changes it between frames (undefined and [0, 0] give no wind force; a bad value throws and
946
+ keeps the old one). A weather drive writes it for you: `WeatherDrive` (`@margoengine/weather/drive`) finds every
947
+ effect by name and gives it the field's wind. It costs no extra upload (the wind rides in a spare word of the 224-byte
948
+ frame block as two half floats, so each component is limited to 1,000 m/s and about three digits) and no extra shader
949
+ work for an effect with no wind.
950
+
951
+ ```ts
952
+ import {EngineKernel} from 'margoengine/core';
953
+ import {VFX} from 'margoengine/vfx';
954
+ import {WeatherMachine} from 'margoengine/weather';
955
+
956
+ const engine = await EngineKernel.create(document.querySelector<HTMLCanvasElement>('#viewport')!, {backend: 'webgpu'});
957
+ const smoke = VFX.attach(engine, {
958
+ capacity: 20_000, rate: 800, position: [0, 0.3, 0], velocity: [0, 3, 0], spread: 0.3,
959
+ gravity: [0, 0, 0], turbulence: 0.2, lifetime: [3, 5], size: 0.5, color: [0.5, 0.5, 0.5, 0.4],
960
+ wind: [6, 0], // leans atan(6 / 3) = 63 degrees toward +x
961
+ });
962
+ smoke.setWind(new WeatherMachine('storm').wind); // or follow a weather's wind field
963
+ engine.start();
964
+ ```
965
+
966
+ ### A fire from one call
967
+
968
+ `Fire` (`@margoengine/vfx/fire`) builds a wood fire from a kit's `hearth`: the flames, embers and optional kettle
969
+ steam (`@margoengine/vfx`), and the firelight as a flickering flame line over the fuel (`lineLights` of
970
+ `@margoengine/clustered/line`) whose points cast soft shadows the size of the flames. The light, its shadows and the
971
+ flames come from the same data, so they agree with no tuning; `power` turns the light and the flames up together.
972
+
973
+ ```ts
974
+ import {Engine, box} from 'margoengine/core';
975
+ import {Clustered} from 'margoengine/clustered';
976
+ import {LocalShadows} from 'margoengine/clustered/shadows';
977
+ import {LightGroups} from 'margoengine/clustered/groups';
978
+ import {Fire} from 'margoengine/vfx/fire';
979
+
980
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
981
+ const floor = engine.createMesh().setGeometry(box(6, 0.1, 6));
982
+ const logs = engine.createMesh().setGeometry(box(0.62, 0.12, 0.2)).setPosition([0, 0.11, 0]);
983
+ const lights = Clustered.attach(engine);
984
+ const shadows = LocalShadows.attach(engine, {lights, resolution: 1024});
985
+ shadows.add(floor);
986
+ shadows.add(logs);
987
+ const groups = LightGroups.attach(engine, lights);
988
+ // The flames are born on the logs' top (y 0.17); the light stands 0.3 m above, along x.
989
+ const fire = Fire.attach(engine, {position: [0, 0.17, 0], lights: groups, steam: [0.4, 0.6, 0]});
990
+ // Lights of your own sit beside the fire's in another group; its flicker never replaces them.
991
+ groups.group('room').setLights([{position: [2, 2.4, 1], range: 8, intensity: 2}]);
992
+ engine.start();
993
+ console.log(fire.lights.length); // 2
994
+ ```
995
+
996
+ - Data: `hearth` (default `snowNight().hearth`): `light.length` 0.62 m of flames, `light.height` 0.14 m over the fuel
997
+ top, `light.radius` 0.155 m per point (`lineShadowRadius` for that length), 1,860 K firelight (`FIRELIGHT_KELVIN`), intensity
998
+ 13.7 (5.11 cd at the peak) shared by the points, range 9 m, the `fire` flicker curve with one seed for the whole line; `emberHeight` 0.1 m; the `fire`,
999
+ `embers` and `steam` particles, each prewarmed for its longest lifetime.
1000
+ - A reflection is never brighter than what it reflects: each point is a sphere light (`PointLight.radius`) whose
1001
+ radiance, intensity / (pi radius^2), equals the flames' brightest radiance (their intensity times their brightest
1002
+ colour), so a metal near the fire mirrors it at most F0 times the flames. The default hearth's flames are as bright
1003
+ as its light (intensity 58.5, 33.9 in luminance), so each sphere is the flame line's own 0.155 m share; a light
1004
+ brighter than its flames grows the spheres instead. A wall 0.4 m behind the fire (albedo 0.5) is then outside them
1005
+ and reads about a ninth of the flames, where 0.634 m spheres sized to dimmer flames lit it about as bright as they
1006
+ were (KB-FIRE-RECEIVER1). Its shadow keeps `light.radius`. `power` scales the light and the flames alike, so the
1007
+ size does not change with it.
1008
+ - Options: `position` (required, the fuel top), `power` (default 1: light intensity and the flames' and embers'
1009
+ radiance), `width` (default `light.length`; the shadow radius scales with it) and `axis` (default [1, 0, 0]) of the
1010
+ flame line, `samples` (default 2), `castShadow` (default true), `depth` (the scene depth: the flames, embers and steam
1011
+ fade 0.08 m before the logs; default none, hard-edged particles), `embers` (default true) and `steam` (a point; default no steam).
1012
+ `wind` (`[x, z]` m/s or a field; the flames, embers and steam lean with it, see Wind above; default none) and `fire.setWind`.
1013
+ - `lights`: a `LightGroups` (the fire takes a group of its own, `fire`, `fire 2`, ...) or a `Clustered`, whose whole
1014
+ list the fire then owns. With `castShadow`, its `Clustered` needs `LocalShadows` attached first: `Fire.attach` throws
1015
+ `MargoError` `'state'` otherwise. Omit `lights` for flames with no light. The fire is a plugin of its scene, so
1016
+ disposing the scene disposes it.
1017
+ - `fire.lights` holds the steady points, `fire.particles` the `flames`, `embers` and `steam` effects. `fire.dispose()`
1018
+ stops the flicker, removes the fire's group (or empties a `Clustered` it owned) and disposes the particles.
1019
+ - Not yet: TAA's reactive mask for the flames (the shared mask registry is a later vfx and taa job) and a light
1020
+ source size for highlights on metal (later clustered and materials jobs). Smoke over the fire: `Smoke` (below);
1021
+ soot on the walls: `@margoengine/materials/soot`.
1022
+
1023
+ ### A smoke column from one call
1024
+
1025
+ `Smoke` (`@margoengine/vfx/smoke`) stands a smoke column on a source from its heat release: lit soft puffs that rise
1026
+ by the buoyant plume law, widen as they take in air, bend over and lean in the wind, and level off in stable air. One
1027
+ call gives a physical column; every number below is an option.
1028
+
1029
+ ```ts
1030
+ import {Engine, box} from 'margoengine/core';
1031
+ import {Smoke, airStability} from 'margoengine/vfx/smoke';
1032
+ import {SMOKE_LOOKS} from 'margoengine/vfx/settings';
1033
+
1034
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1035
+ backend: 'webgpu',
1036
+ camera: {position: [0, 60, 260], target: [0, 60, 0], far: 3000},
1037
+ });
1038
+ engine.createMesh().setGeometry(box(400, 1, 400)).setPosition([0, -0.5, 0]);
1039
+ // A burning house: 5 MW from a 3 m source, in a 4 m/s wind from the west, under air that stops it near 300 m.
1040
+ const smoke = Smoke.attach(engine, {position: [0, 6, 0], heat: 5e6, radius: 3, wind: [4, 0], stability: airStability(0.005)});
1041
+ engine.start();
1042
+ console.log(smoke.column(30).rise.toFixed(0)); // height (m) a puff has risen after 30 s
1043
+ console.log(Object.keys(SMOKE_LOOKS).join(', ')); // campfire, houseFire, wildfire, oil
1044
+ ```
1045
+
1046
+ - The law: a heat release Q (watts, `heat`) gives the buoyancy flux F = g Q / (pi c_p rho T) (Briggs 1969 eq. 4.20;
1047
+ `buoyancyFlux`, 8.8 m^4/s^3 a megawatt at 15 C). In calm air the column is the plume of Morton, Taylor and Turner
1048
+ 1956: it widens as (6/5) alpha z (alpha 0.093) and slows as z^(-1/3), from a virtual origin below a source of radius
1049
+ `radius`. In a wind U it is the lower of that and Briggs' bent-over "2/3 law" plume, rise 1.6 F^(1/3) x^(2/3) / U at
1050
+ x = U t downwind with radius 0.5 z (eqs. 4.31 and 4.23). In stable air (`stability` s = (g / T) dtheta/dz,
1051
+ `airStability(lapse)`) it levels off at Briggs' final rise 5.0 F^(1/4) s^(-3/8) in calm air or 2.9 (F / (U s))^(1/3)
1052
+ in a wind (eqs. 4.25 and 5.7) and spreads there. `plumeRise(t, F, U, r, s)` gives the column at any age;
1053
+ `smoke.column(t)` gives it for the smoke's own heat, wind and air.
1054
+ - How it is drawn: each puff is a `VFX` particle (`lit`: shaded as a ball by the sun, ambient and optional `lights` and
1055
+ `shadows`; `curves`: fading in and out; `overLife`: its speed follows the column's rise speed and its size the
1056
+ column's width; prewarmed so the column is whole on the first frame). The puffs' wind pulls their horizontal speed
1057
+ toward U (see Wind above), so the column leans atan(U / w); in calm air the same 0.25 s drag pulls it toward rest,
1058
+ so the curl-noise churn stirs the column and never adds up over a puff's minute of life into a walk that lays the
1059
+ smoke along the ground and over the camera. Measured on an RTX A6000, WebGPU and the WebGL2 layer
1060
+ (`packages/vfx/test/smoke-check.mjs`): puff heights within 0.6% of the law after their first second (bound 7%), the
1061
+ same particle bytes on every run, and 60 frames of every live setting make no pipeline, buffer or texture.
1062
+ - Wind: a `wind` of its own (`[x, z]` m/s or a field) overrides the weather; without one, a `WeatherDrive` in the scene
1063
+ writes the weather's wind on the puffs and the column bends with it, the rule of `Fire`. `fire`: stand the column on
1064
+ a `Fire` (`Smoke.attach(engine, {fire})`): it takes the flames' position and their wind, 20 kW and a 0.3 m source by
1065
+ default (a wood hearth).
1066
+ - Options, read every frame: `position`, `heat` (default 5 MW), `radius` (2 m), `airTemperature` (288.15 K),
1067
+ `stability` (0, neutral), `wind`, `rate` (20 puffs/s), `color` (linear [0.3, 0.29, 0.28]), `opacity` (0.5),
1068
+ `turbulence` (0.4 m/s^2 of curl noise), `glow` (1: the fire's glow from below, see next). Read at attach: `lifetime`
1069
+ (60 s: the column is a puff's rise at that age; the capacity is rate x lifetime), `prewarm` (true, in steps of 0.1 s
1070
+ or a 600th of a long life), `texture` (a puff sprite; default a soft disc), `lights`,
1071
+ `shadows` and `depth` (puffs fade 1 m before surfaces). `setHeat`, `setWind`, `setFog`, `refill` (empty the column
1072
+ and prewarm it again next frame, for a column moved to a new fire) and `dispose`; `smoke.effect` is the `VFX`.
1073
+ - Glow from below: the puffs are lit by a point at the source radiating `RADIANT_FRACTION` (0.3, Drysdale 2011;
1074
+ McGrattan, Baum and Hamins 2000) of the release, `glow` times 0.3 / 0.7 of `heat`, softened over the source radius
1075
+ (`VFXLit.glow` of `@margoengine/vfx/lit`: irradiance P / (4 pi (d^2 + r^2)) lights a puff as a 1,000 W/m^2 sun of
1076
+ intensity pi would). So the column over a fire is orange underneath and fades to grey as it climbs, at night with no
1077
+ other light; `glow: 0` turns it off. A new puff is opaque within `SMOKE_FADE_IN` (1 s) whatever its lifetime
1078
+ (`smokeAlphaCurve`; it was a fifth of the life, 12 s of a minute).
1079
+
1080
+ ```ts
1081
+ import {Smoke} from 'margoengine/vfx/smoke';
1082
+ import {Engine} from 'margoengine/core';
1083
+
1084
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
1085
+ const smoke = Smoke.attach(engine, {position: [0, 8, 0], heat: 20e6, radius: 6, glow: 1.5});
1086
+ console.log(smoke.effect.options.lit?.glow?.power); // 12857142.857142856
1087
+ ```
1088
+ - Settings: `SMOKE_SETTINGS` in `@margoengine/vfx/settings` (`applySmokeSettings`, `smokeSettingValues`,
1089
+ `SMOKE_LOOKS` campfire, houseFire, wildfire and oil); the wind rows are null by default and follow the weather.
1090
+ `applySmokeSettings(front, SMOKE_LOOKS.animatedFeature)` on a `FireFront` writes every smoke column it has and makes later
1091
+ (its `setSmokeLook`), except `heat` and `radius`, which the front keeps setting per column from its segments; a row left
1092
+ out gives back what the front built the column with (300 s life, 16 puffs a second, wood smoke, its `smoke` options).
1093
+ - Flame settings: `FLAME_SETTINGS` in `@margoengine/vfx/settings` (`applyFlameSettings`, `FLAME_LOOKS`) are the live look rows of a
1094
+ `Fire`: `flames.size`, `flames.radiance` (under 1 keeps the core from blowing out), `flames.opacity`, `flames.ramp` and
1095
+ `flames.edgeColor` (a core-to-edge colour ramp along the flame's life), `embers.size`, `embers.count` and `embers.radiance`.
1096
+ Each is a multiple of what the kit gave, so the defaults leave the fire as built; `FLAME_LOOKS.animatedFeature` is larger,
1097
+ softer flames with a ramp and fewer, larger embers. A `FireFront`'s smoke columns take a smoke look as one target:
1098
+ `applySmokeSettings(front, SMOKE_LOOKS.animatedFeature)`.
1099
+ `applyFlameSettings(front, rows)` writes a `FireFront`'s flame volumes (its `setFlameLook`), the ones it makes later included.
1100
+
1101
+ ```js
1102
+ import { applyFlameSettings, FLAME_LOOKS } from '@margoengine/vfx/settings';
1103
+
1104
+ const fire = { particles: { flames: { options: { size: 0.2, intensity: 40, curves: { color: [{ at: 0, color: [1, 0.8, 0.3] }, { at: 1, color: [0.8, 0.2, 0.05] }] } } } } }; // a Fire has these parts
1105
+ applyFlameSettings(fire, FLAME_LOOKS.animatedFeature);
1106
+ console.log(fire.particles.flames.options.size); // 0.30000000000000004
1107
+ ```
1108
+
1109
+ ### Flames at house and forest size
1110
+
1111
+ `Flame` (`@margoengine/vfx/flame`) draws a flame of any size, from a campfire to a burning house or a forest crown, as
1112
+ one ray-marched volume over its footprint. Tall tapering tongues rise at the buoyant speed 0.5 sqrt(g L) (Quintiere
1113
+ 1989), break up at the tip so that flame shows half the time at the flame length (Zukoski et al. 1985), and the whole
1114
+ zone puffs (necks, sheds a bulge that rises and burns out) at Cetegen and Ahmed 1993's 1.5 D^(-1/2) Hz
1115
+ (`puffingFrequency`, `puffingDiameter`). Each point carries a heat, highest deep in the zone and low in the flame,
1116
+ coloured by the blackbody of its temperature from `core` (2,200 K) to `edge` (1,000 K) (Planck's law through Wyman,
1117
+ Sloan and Shirley 2013's fits of the CIE observer, `blackbodyColor`); the ray collects emission and absorption, so a
1118
+ pixel is never brighter than the hottest gas it crosses, then goes through a camera's per-channel shoulder under
1119
+ `peak` (default 1, scene-linear): the core saturates red, then green, and reads yellow-white while blue stays low, so no
1120
+ pixel turns white whatever the rows ask. Give it a `length` in metres, an `intensity` in kW/m (Byram's flame length
1121
+ 0.0775 I^0.46, `byramFlameLength`) or a `heat` in watts over its footprint (Heskestad's height 0.235 Q^(2/5) - 1.02 D,
1122
+ `heskestadFlameLength`). With `lights` (a `LightGroups` or a `Clustered`) it lights the meshes added to that
1123
+ `Clustered`, and smoke lit by it, with up to four point lights over its footprint a third of the way up, as bright as
1124
+ its silhouette at its peak radiance (`FLAME_LIGHT_FILL`), each flickering with the puffing of the tongues above it.
1125
+ To meter a fire as a real emitter, `flameLuminousFlux(heat, kelvin, radiantFraction = FLAME_RADIANT_FRACTION)` gives
1126
+ its lumens: the radiated share of its heat release (chi_r about 0.3 for sooty fuels, Tewarson 2016, Koseki 1989) times the
1127
+ luminous efficacy of blackbody radiation at its soot temperature (`blackbodyLuminousEfficacy`, Planck's law through the
1128
+ photopic curve, about 0.5 lm/W at 1,760 K and 16.5 at CIE illuminant A). `heskestadHeat(length, diameter)` turns
1129
+ Heskestad's height round to give the heat of a flame of a given length.
1130
+ The luminous surface is thin, as the reaction zone of a diffusion flame is (Turns 2012, ch. 9): density rises over a
1131
+ short step of the field, fine streaks a quarter and an eighth of a tongue across, drawn up with the flow, tear the
1132
+ edge into wisps, and coverage follows 1 - T^2, so tongues keep crisp edges. The march steps at most 0.03 flame lengths
1133
+ (64 to 256 steps, two at a time through empty air), bisects back to the surface where a ray first meets the flame, and
1134
+ starts each ray at interleaved gradient noise shifted with the flame's clock (Jimenez 2014): a grazing view along a long
1135
+ front shows no layers or fixed hatch, frames averaged by motion blur or TAA converge, and a frame drawn again at the
1136
+ same time is the same picture. `crowns` (tree spacing, metres) makes it a crown fire: each tree torches as its own
1137
+ tapering column that starts down in its crown and tops out at 0.6 to 1.4 flame lengths (crown flames one to two and a
1138
+ half times the stand height, Van Wagner 1977, Alexander and Cruz 2011), so the foot and crest are ragged.
1139
+ The flame is hottest low down, over the burning fuel (Butler et al. 2004), and along a line of fire its tips rise and fall
1140
+ as separate tongues 0.7 to 1.3 times the mean length (Albini 1981). `slope` (`[dy/dx, dy/dz]`) stands it on the ground's
1141
+ plane, so a line across a hillside is not cut by the slope. It lights the air and the ground around it with no light
1142
+ setup: the light it scatters in the haze (`haze`, per metre, default 0.0005; a `FireFront` gives its flames 0.003) is
1143
+ Sun et al. 2005's closed form for a point source, and the ground plane within `glowRadius` (3) flame lengths takes its
1144
+ light at `groundAlbedo` (0.15; 0 with `lights`, whose point lights light the scene); `glow` scales both, 0 turns
1145
+ them off. Without `depth`, a surface standing in that light (a tree) takes the light of the ground behind it. A second
1146
+ pass writes the flame's depth where it is opaque, so a spark or a smoke puff behind the flames is hidden and one in front
1147
+ draws over them.
1148
+ Everything is read each frame and written into one uniform, so a flame that grows, moves, leans, changes its look or goes
1149
+ out makes no GPU object after its first frame; every flame volume on a device shares its three pipelines (glow, flame,
1150
+ depth).
1151
+ The scene behind the flames shimmers through `HeatHaze` of `@margoengine/post/heat-haze`: `flame.plume` is the hot plume
1152
+ it reads (base, length, footprint as drawn, lean in the wind, the flame's clock), so `new HeatHaze({sources: [flame]})`
1153
+ in a `PostStack` bends the light through it, and `() => front.flameVolumes` does it for a `FireFront`.
1154
+
1155
+ ```ts
1156
+ import {Engine} from 'margoengine/core';
1157
+ import {Clustered} from 'margoengine/clustered';
1158
+ import {LightGroups} from 'margoengine/clustered/groups';
1159
+ import {Flame, byramFlameLength, flameLuminousFlux} from 'margoengine/vfx/flame';
1160
+ import {FLAME_LOOKS, applyFlameSettings} from 'margoengine/vfx/settings';
1161
+
1162
+ const engine = await Engine.create(document.querySelector('canvas')!, {backend: 'webgpu'});
1163
+ const lights = LightGroups.attach(engine, Clustered.attach(engine));
1164
+ // A house fire: 20 MW through a 6 m by 6 m roof breach (Heskestad's 5.4 m flames), lighting the street.
1165
+ const house = Flame.attach(engine, {position: [0, 3, 0], heat: 20e6, footprint: [6, 6], wind: [3, 0], lights});
1166
+ // A crown fire front: a 10,000 kW/m fireline has Byram's 5.4 m flames, torching trees 8 m apart.
1167
+ const crown = Flame.attach(engine, {position: [40, 0, 0], intensity: 10000, footprint: [30, 4], crowns: 8, lights});
1168
+ // A line of fire across a 30 percent hillside in smoky air: it stands on the slope and lights the haze around it.
1169
+ const line = Flame.attach(engine, {position: [0, 0, 60], length: 6, footprint: [40, 4], slope: [0.3, 0], haze: 0.003});
1170
+ applyFlameSettings(house, {...FLAME_LOOKS.animatedFeature, 'flames.core': 2400, 'flames.tongues': 0.8});
1171
+ house.options.footprint = [8, 6]; // it spreads; the same uniform, rewritten in place
1172
+ console.log(byramFlameLength(10000).toFixed(1), house.puffing.toFixed(2)); // 5.4 0.54
1173
+ console.log(Math.round(flameLuminousFlux(1e6, 1760))); // about 150,000 lm from a 1 MW wood fire
1174
+ ```
1175
+
1176
+ - The flame rows of `FLAME_SETTINGS` (`applyFlameSettings`, all live): `flames.core` and `flames.edge` (kelvin),
1177
+ `flames.peak`, `flames.flicker` (puffing and light flicker, 0 holds it steady), `flames.tongues` (tongue size),
1178
+ `flames.size`, `flames.radiance`, `flames.opacity`, `flames.ramp`, `flames.edgeColor`, `flames.glow` and `flames.haze`
1179
+ (the glow and the haze of a flame volume as multiples of its own). On a hearth `Fire` they
1180
+ change its billboards after attach: core, edge and peak away from their defaults give them the blackbody ramp of
1181
+ `flameRamp`, flicker scales their turbulence and tongues their noise scale; every row at its default gives the kit back.
1182
+ - Under a tonemap, raise `peak` (up to 16 on the row) so the core blooms; the light follows the peak, so a wall is never
1183
+ brighter than the flame that lights it. With the animatedFeature looks of `@margoengine/post` (bloom over 1) and
1184
+ `@margoengine/tonemap` (AgX), `flames.peak` 4 makes the core glow and grade a bright yellow-white with no white pixel
1185
+ (flame-check `graded`); at peak 1 it never reaches that bloom and grades dim orange.
1186
+ - `Fire.attach(engine, {position, flameLength: 10, width: 12, lights})` makes the fire's flames such a `Flame`
1187
+ (`fire.flame`, along `axis`), lit through the fire's `lights`; its embers scale by the square root of the length over
1188
+ the hearth's 0.25 m. A `Smoke` column (`fire.smoke`) stands on the
1189
+ flame tips with the fire's convective heat: Heskestad's length turned back into a heat release, 70% convected
1190
+ (Heskestad 1983; Drysdale 2011 ch. 4), as wide as the flames and lit by the fire's `lights`. `smoke: {...}` gives it
1191
+ options of its own, `smoke: false` none. It is on by default with `flameLength` and off for the hearth's own flames.
1192
+ - A flame volume is drawn just before the particle effects, smoke and cloud puffs (order 5.95), then writes its depth
1193
+ where its transmittance falls below a half (5.96): a puff or ember behind the flames is hidden by them, one in front
1194
+ draws over them (flame-check `order`: a dark column in front of an 8 m flame leaves 254 of its 2,583 flame pixels, one
1195
+ behind leaves all of them; drawn after the particles with no depth, as before, the one in front left 2,301 of 2,458).
1196
+ Drawn before them with no depth, a smoke column behind the flame covered it as a hard-edged slab (flame-check `veil`).
1197
+ - `FireFront` draws flames: `flames` (16) volumes on its hottest stretches, each as long as Byram's flame of that
1198
+ stretch's fireline intensity, and `buildingFlames` (8) on burning buildings, each as long as Heskestad's height of the
1199
+ building's heat over its vent (`structureVent`, 0.1 of the floor). `flame: {lights}` lights the land, smoke and
1200
+ buildings from every volume (a bare `Clustered` is shared through a `LightGroups`). They are a fixed pool: the volumes
1201
+ move between stretches and no GPU object is made as the fire spreads. `front.flames()` lists what is drawn, with the
1202
+ ground's `slope` across each stretch, which the volume stands on. Each front volume throws embers (`flameEmbers`,
1203
+ default true; `front.flameEmbers` lists the effects): the hearth kit's embers scaled to a 20 m flame, born over the
1204
+ footprint a third of the flame up, their rate and launch speed growing as sqrt(L / 20) (embers loft with the plume,
1205
+ Sardoy et al. 2007), carried ahead of the front by the open wind; the ember rows of `FLAME_SETTINGS` scale them.
1206
+ - Pass the scene `depth` (`DepthTexture` of `@margoengine/post/depth`) so the march stops at a house inside its own
1207
+ flames and the camera may stand in the fire; without it the volume's box is depth-tested against the scene.
1208
+ - Limits: a flame is a procedural volume, not a fluid simulation; it has no soot of its own (a `Fire` adds `Smoke`), its
1209
+ light does not cast shadows, the glow lights a plane (the ground under the flame), not the scene's own surfaces, unless
1210
+ `depth` is given, and there is no heat shimmer (refraction needs the scene's colour, which a frame pass cannot read;
1211
+ KB-FLAME-HEATHAZE1).
1212
+
1213
+ ### A wildfire front from one call
1214
+
1215
+ `FireFront` (`@margoengine/vfx/fire-front`) spreads a fire over a fuel map by wind, slope and fuel moisture in fixed steps,
1216
+ leaves a burn texture behind it, feeds smoke columns along the whole front, throws embers that start spot fires and burns
1217
+ buildings. It stands on a ground function and a fuel function and nothing else.
1218
+
1219
+ ```ts
1220
+ import {Engine, box} from 'margoengine/core';
1221
+ import {FireFront} from 'margoengine/vfx/fire-front';
1222
+ import {FIRE_LOOKS, applyFireSettings} from 'margoengine/vfx/settings';
1223
+
1224
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1225
+ backend: 'webgpu',
1226
+ camera: {position: [0, 200, 520], target: [0, 20, 0], far: 6000},
1227
+ });
1228
+ engine.createMesh().setGeometry(box(1000, 1, 1000)).setPosition([0, -0.5, 0]);
1229
+ // A chaparral slope with a house on it, lit at the foot in a Santa Ana wind.
1230
+ const fire = FireFront.attach(engine, {
1231
+ size: [1024, 1024], cell: 8, ground: (x) => 0.25 * (x + 512) / 4,
1232
+ fuel: 'FM4', structures: [{at: [150, 0]}], ignitions: [{at: [-300, 0], radius: 12}],
1233
+ });
1234
+ applyFireSettings(fire, FIRE_LOOKS.santaAna);
1235
+ await fire.ready; // the smoke columns exist
1236
+ engine.start();
1237
+ console.log(fire.texture !== undefined, fire.smokeColumns.length); // true 12
1238
+ ```
1239
+
1240
+ - The spread: each fuel is a model of size classes (`FUEL_MODELS`: Anderson 1982's `FM1` to `FM13`, and a
1241
+ wood-frame `STRUCTURE`), combined by Albini 1976's area weights into Rothermel 1972's rate R = I_R xi (1 + phi_w +
1242
+ phi_s) / (rho_b eps Q_ig). `fireBehaviour(model, {moisture, wind, slope})` gives the head's rate, reaction and fireline
1243
+ intensity (Byram), flame length and length-to-breadth ratio; `fuelBed` is its precomputed part. Against the rates
1244
+ Anderson publishes at 5 mi/h midflame wind and 8 percent dead fuel moisture (`packages/vfx/test/fire-front.test.js`),
1245
+ with every dead class at Anderson's 8 percent (Albini's chart convention) the models are within 10 percent except FM5 (-19.5) and FM7 (+29.6), and FM13 is +4.8; the values were checked against the reports, and FM5 matches Albini 1976's own chart within 4 percent (its 18 is not the chart's), while FM7's chart runs 0.8 of ours: Albini's charts and Anderson's rates are a 1974 program run that differs from Rothermel 1972 as published, and the fuel models instead reproduce the values Andrews 2018 (RMRS-GTR-371) computes with BehavePlus, FM7 with its standard live surface-area-to-volume ratio of 1,500 1/ft (`docs/limits.md`). Each category is damped by its surface-area weighted moisture (Rothermel 1972). `moisture10` and `moisture100` set the heavier dead classes (default +0.01 and +0.02), in `fireBehaviour` and as `FireFront` options. A `WeatherDrive` (`@margoengine/weather/drive`) dries and wets a front's dead fuel from the weather's humidity, temperature and rain (`FuelMoistureModel`: Simard's equilibrium moisture, the timelag of each class, 35 percent in rain) while `moisture`, `moisture10` and `moisture100` are unset; the `moisture` rows of `FIRE_SETTINGS` are null by default (the option left unset), which is 0.06 with no weather. `smokeEmission` is the fine smoke the front gives off in grams a second (the fuel it burns times `smokeYield`, 15 g/kg, the extratropical forest PM2.5 of Akagi et al. 2011 table 1; temperate forest is 12.7, boreal 15.3); a `WeatherDrive` raises the weather's smoke from it (`@margoengine/weather`). `radiantPower` is the flames' radiant power in watts, the radiant 0.3 of the heat release (`RADIANT_SHARE`, the `RADIANT_FRACTION` its smoke columns glow with), and a `WeatherDrive` lights the smoke haze over the fire from below with it (KB-WEATHER-FIREHAZE1).
1246
+ Wind and slope add as vectors; each cell spreads as the focus of Richards' ellipse (`ellipseRate`), so a fire runs
1247
+ fast with the wind and uphill and creeps against them.
1248
+ - Crown fire (on by default, `crown: false` keeps a fire on the ground): the timber models `FM8`, `FM9` and `FM10`
1249
+ carry a `canopy` (base height, bulk density, stand height), or `canopy: (x, z) => stand | null` sets it per place. A
1250
+ surface fire past Van Wagner 1977's critical intensity (0.010 z (460 + 25.9 M))^1.5 kW/m (canopy base height z,
1251
+ `foliarMoisture` M, default 1 = 100 percent) lifts into the crowns: it torches (passive, the crown fraction burned
1252
+ 1 - exp(-0.23 (R - R0)) of the Canadian FBP System blends the surface and crown rates) or, where Rothermel 1991's crown
1253
+ rate (3.34 times FM10's rate at 0.4 of the 20 ft wind) passes Van Wagner's 3 / CBD m/min, runs as an active crown
1254
+ fire. A crown fire runs on into any stand where that rate criterion holds, even one too high to torch from below
1255
+ (the conditional crown fire of USDA report RMRS-RP-29, 2001). Burning crowns add their foliage to the intensity, so the flames
1256
+ (Thomas 1963's 0.0266 I^(2/3), drawn as torching trees), the smoke heat and the embers' throw all grow.
1257
+ `crownBehaviour` and `crownRate10` give the numbers alone; `crownFraction(x, z)` and `crowningCells` read the front.
1258
+ In a 20 m/s gale at 5 percent dead moisture an FM10 stand runs at 47 m/min (the Sundance fire of 1967 averaged 48 over
1259
+ nine hours; Cruz et al. 2005's active model gives 154, and Rothermel 1991 is known to run under it), releases about
1260
+ 50 MW/m and draws 36 m flames.
1261
+
1262
+ ```ts
1263
+ import {FUEL_MODELS, crownBehaviour, crownRate10, fireBehaviour} from 'margoengine/vfx/fire-front';
1264
+
1265
+ const surface = fireBehaviour(FUEL_MODELS.FM10!, {moisture: 0.05, wind: 8});
1266
+ const crown = crownBehaviour(surface, FUEL_MODELS.FM10!.canopy!, 1, crownRate10({moisture: 0.05, wind: 20}), 20);
1267
+ console.log(crown.type, Math.round(crown.rate * 60)); // active 47
1268
+ ```
1269
+ - Fixed steps: `step` (0.5 s). `advanceTime(delta)` runs whole steps (a frame does it once attached), `await
1270
+ fire.settle()` runs it until nothing burns, as `Flood` does. The same options and seed give the same fire, whatever the
1271
+ frame times.
1272
+ - The burn texture: `fire.texture`, rgba32float, one texel per cell, row z: r the fire time it caught, g the time it
1273
+ burns out, b the fireline intensity in MW/m (kept after the cell burns out, for the scorch of plants left standing:
1274
+ `fireSource(fire)` in `@margoengine/scatter/states`), a the state (`UNBURNT` 0, `BURNING` 1, `BURNT` 2, `NO_FUEL` 3).
1275
+ `fire.origin`, `fire.cell` and `fire.width` place it. `fire.read()` is the same on the CPU, `state(x, z)` and
1276
+ `arrivalTime(x, z)` look one cell up.
1277
+ - Flames: `flames` (16) and `buildingFlames` (8) `Flame` volumes, see above; `front.flames()` lists the stretches of front
1278
+ (Byram's length) and the burning buildings (Heskestad's) to draw. Each front volume lies along its stretch of fire line: `footprint`
1279
+ is `[along, across]` the heat-weighted spread of the burning cells on their principal axes (sqrt(12 var) plus a
1280
+ cell) and `yaw` turns it, so a diagonal or ragged line is not drawn as its axis-aligned box. A burning building releases its fuel over its burn
1281
+ time (about 200 kW per square metre of floor), so a house is megawatts of smoke heat.
1282
+ - Smoke: `columns` (12) `Smoke` columns follow the hottest `segments()` of the front (heat is 0.7 of the reaction
1283
+ intensity over the burning cells of a `segment`-metre block, a column at the heat-weighted centre); `await fire.ready`
1284
+ for them, `columns: 0` for none. Each column leaves the top of its segment's flames (`segments()` gives the
1285
+ `flameLength`), as wide as its burning ground (2 to 60 m), lives 300 s at 16 puffs a second in slightly stable air
1286
+ (1.5e-4 / s^2), so it climbs to Briggs' final rise and spreads there as a pall over the valley, wood smoke
1287
+ [0.24, 0.22, 0.2] at 0.6, glowing orange from the fire below; `smoke` options override any of these. A column that
1288
+ takes up a fire after standing idle starts full (`refill`).
1289
+ - Embers: a burning cell of more than 1,000 kW/m throws embers (`spotting`) that loft with the flame, fall at 4 m/s and
1290
+ land downwind at a lognormal distance (`spotSpread`); each lights dry fuel with chance `emberIgnition`. `onSpot` sees each.
1291
+ - Buildings: `structures` burn as the `STRUCTURE` fuel, igniting from flame within 1.5 flame lengths for `structureExposure`
1292
+ seconds or from an ember, and burning `structureBurnTime` seconds.
1293
+ - Wind: set `wind` (`[x, z]` m/s at 10 m, or a field) or leave it to a `WeatherDrive`, which writes it into a front it finds
1294
+ by name; `windReduction` (0.4) takes it to midflame height. Settings: `FIRE_SETTINGS` (`applyFireSettings`,
1295
+ `fireSettingValues`, `FIRE_LOOKS` mild, redFlag and santaAna) in `@margoengine/vfx/settings`; the wind rows follow the weather at null.
1296
+ - Not yet: crown fire and fire-driven wind, and a fuel map made from biomes. Live fuel moisture does not follow the weather (it changes over weeks).
1297
+
1298
+ ### Sliders for every particle control: settings
1299
+
1300
+ `@margoengine/vfx/settings` lists the look and motion options an effect reads every frame as one table for panels and
1301
+ tools. `VFX_SETTINGS` has 24 rows in four groups (emission: `rate`, `spread`, `emissionExtent.x/y/z`; motion:
1302
+ `velocity.x/y/z`, `gravity.x/y/z`, `turbulence`, `frequency`, `bounce`, `ground`, `angularVelocity`; wind: `wind.x`,
1303
+ `wind.z`; look: `size`, `lifetime.min`, `lifetime.max`, `color`, `alpha`, `intensity`), each with `name`, `group`,
1304
+ `kind`, `default`, `min`, `max`, `step`, `unit`, `about` and `live`; the row shape is the one every package's settings
1305
+ share (`packages/shared/settings.js`). Every row is `live`: writing it creates no pipeline, buffer or texture, so it can
1306
+ be keyed over a shot (`test/settings-check.mjs` counts 60 frames of every row and compares displayed frames byte for byte: the default rows draw the frame of an unconfigured effect and written rows the frame of the same options built in, native and on the WebGL2 layer).
1307
+ Not rows: what is fixed at attach (`capacity`, `wrap`, `sort`, `texture`, an emitter's shape), the structured options of
1308
+ the feature entries (`curves`, `fields`, `lit` ...) and `position`.
1309
+
1310
+ `applyVfxSettings(effect, values)` writes the values onto the effect (a name left out takes its default) and leaves an
1311
+ option unset when its rows are all at their defaults, so an untouched effect keeps the options it was built with. Start a
1312
+ panel from `vfxSettingValues(effect)` (each row from the effect's options, or its default) so the effect's own values
1313
+ stay: that works for the `flames`, `embers` and `steam` of a `Fire` too (`fire.particles.flames`). `rate` null is the
1314
+ effect's own rate (half its capacity a second). The two wind rows are `nullable` with `weather: 'wind'`: both null leave
1315
+ the option unset and `WeatherDrive` writes the weather's wind into the effect each frame; a number (the other reads as 0)
1316
+ sets `[x, z]` m/s and the drive leaves it alone, so a value overrides the weather. A name not in the table throws a
1317
+ `TypeError`, a value outside its row's range or `lifetime.min` above `lifetime.max` a `RangeError`.
1318
+
1319
+ ```ts
1320
+ import {VFX} from 'margoengine/vfx';
1321
+ import {VFX_SETTINGS, applyVfxSettings, vfxSettingValues} from 'margoengine/vfx/settings';
1322
+ import {Engine} from 'margoengine/core';
1323
+
1324
+ const engine = await Engine.create(document.querySelector('canvas')!);
1325
+ const smoke = VFX.attach(engine, {capacity: 4096, rate: 200, velocity: [0, 3, 0], color: [0.5, 0.5, 0.5, 0.6]});
1326
+ // The panel's state starts from the effect's own values.
1327
+ const values = vfxSettingValues(smoke);
1328
+ const apply = () => applyVfxSettings(smoke, values);
1329
+ // One slider per number row, built from the table; wind null follows the weather.
1330
+ for (const row of VFX_SETTINGS) {
1331
+ if (row.kind !== 'number') continue;
1332
+ const slider = Object.assign(document.createElement('input'), {type: 'range', title: row.about, min: String(row.min), max: String(row.max), step: String(row.step)});
1333
+ slider.value = String(values[row.name] ?? row.min);
1334
+ slider.oninput = () => {
1335
+ values[row.name] = Number(slider.value);
1336
+ apply(); // every frame is fine
1337
+ };
1338
+ document.body.append(slider);
1339
+ }
1340
+ values['velocity.y'] = 6; // smoke rises faster
1341
+ apply();
1342
+ ```
1343
+
1344
+ #### Sliders for blown sand
1345
+
1346
+ The same entry lists a `BlownSand` layer's controls: `SAND_SETTINGS` (18 rows, 13 live), `SAND_LIVE_SETTINGS`,
1347
+ `SAND_LOOKS` (`drift`, `sandstorm`, `weather`), `applySandSettings(sand, values)`, `sandSettingValues(sand)` and
1348
+ `sandSettingDefaults()`. The wind rows (`wind.speed`, `wind.direction` in degrees, `wind.gust`) and `fog` are `nullable`: at
1349
+ null the layer follows the weather (`WeatherDrive` writes the state's wind and the air's extinction, dust included), a
1350
+ number overrides it. `amount`, `seed`, `limit`, `range` and `softness` lay the particles out again or change the draw, so
1351
+ they are not live; the other 13 rows cost no pipeline, buffer or texture (`test/settings-check.mjs`, native and WebGL2
1352
+ layer, which also checks the default rows draw the layer's own frame exact).
1353
+
1354
+ ```js
1355
+ import {BlownSand} from 'margoengine/vfx/blown-sand';
1356
+ import {SAND_LOOKS, applySandSettings, sandSettingValues} from 'margoengine/vfx/settings';
1357
+
1358
+ const sand = new BlownSand({ground: () => 0, wind: {speed: 0}});
1359
+ applySandSettings(sand, SAND_LOOKS.sandstorm);
1360
+ console.log(sandSettingValues(sand)['wind.speed']); // 18
1361
+ ```
1362
+
1363
+ ### Puff clouds
1364
+
1365
+ `Clouds` (`@margoengine/vfx/clouds`) draws soft billboard clouds, drei's Clouds and Cloud for any engine: each cloud is
1366
+ a seeded set of camera-facing puffs inside a box, and every puff of every cloud is drawn back to front in one
1367
+ instanced draw (frame-graph order 6, with the particles). Each puff is lit by the light that reaches it through the
1368
+ puffs between it and the light and scattered toward the eye, so the tops are bright and the undersides grey; puffs
1369
+ fade out near the camera, can fade softly into the surfaces behind them, and take the scene fog.
1370
+
1371
+ ```ts
1372
+ import {Engine} from 'margoengine/core';
1373
+ import {Clouds} from 'margoengine/vfx/clouds';
1374
+
1375
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1376
+ backend: 'webgpu',
1377
+ camera: {position: [0, 2, 18], target: [0, 4, 0]},
1378
+ });
1379
+ const clouds = Clouds.attach(engine, {limit: 200, lightDirection: [0.4, 1, 0.3]});
1380
+ const cumulus = clouds.add({seed: 1, segments: 30, bounds: [10, 2, 3], volume: 6, speed: 0.2});
1381
+ cumulus.matrix.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 5, -6, 1]); // its world matrix: 5 m up, 6 m back
1382
+ clouds.add({seed: 2, segments: 12, color: '#ffd9c4', opacity: 0.8}).matrix[13] = 3;
1383
+ engine.start();
1384
+ console.log(clouds.clouds.length); // 2
1385
+ ```
1386
+
1387
+ - `Clouds` options, read every frame: `limit` (200, the most puffs drawn; later ones are counted in
1388
+ `stats.dropped`), `range` (draw only the first puffs; default all), `texture` (pixels `{width, height, data}` or an
1389
+ image; default a generated soft, lumpy puff, filtered with mips), `frustumCulled` (true: puffs wholly outside the
1390
+ view are skipped), `shading` (`'scatter'` default, `'unlit'` draws the colour as it is), `lightDirection` (toward the
1391
+ light, default [0.4, 1, 0.3]), `lightColor` (white), `ambient` (0.55), `density` (1, the optical depth through a
1392
+ full-size puff for the light behind it; a smaller puff shades in proportion to its width), `relief` (0; up to 1 each
1393
+ puff is shaded as a rounded lobe: the half-Lambert of its sphere normal for the direct light, the sky it faces and
1394
+ the sky light left under the puffs above it for the ambient, and three octaves of multiple scattering through the
1395
+ cloud, so a heap of puffs reads as a cauliflower cloud with lit tops and dark undersides), `softness` (0; world units over which a puff fades into the surface behind
1396
+ it) with `depth` (the scene depth, `() => depthTexture.texture` with `DepthTexture` from
1397
+ `@margoengine/post/depth`). `setFog(fog)` fogs the puffs with an `@margoengine/environment` `Fog`.
1398
+ - A cloud (`clouds.add(options)`, options read every frame; `cloud.matrix` is its world matrix, `cloud.remove()` takes
1399
+ it out): `seed` (default its number in its Clouds), `segments` (20 puffs), `bounds` ([5, 1, 1], the box the centres
1400
+ lie in), `concentrate` (`'inside'` makes puffs smaller toward the box edge, `'outside'` larger, `'random'`),
1401
+ `volume` (6, the width of a full-size puff), `smallestVolume` (0.25 of `volume`), `distribute(index, random)`
1402
+ (return `{point, volume}` in -1..1 box units to place each puff yourself, plus optional `color`, linear RGB in place
1403
+ of the cloud's, and `emission`, linear RGB light the puff gives off, added after the lighting, for a fire glowing
1404
+ inside smoke; `clouds.puffs()` reports each puff's `light`, `sky` and `emission`), `speed` (0; puffs turn at up to `speed`
1405
+ radians a second and breathe), `growth` (4: a puff breathes by up to 1 / growth of its size), `fade` (10: a puff
1406
+ closer to the camera than this fades out, to nothing at the camera), `opacity` (1) and `color` (white). Puff sizes
1407
+ follow the matrix scale.
1408
+ - Runs on WebGPU or the full WebGL2 compatibility layer. The small WebGL2 preview has no route for the puffs, so
1409
+ `Clouds.attach` refuses it at setup with `MargoError` code `unsupported` (https://margoengine.com/errors#webgpu).
1410
+ - The animation is a function of the clouds' clock (`time`, the sum of frame deltas), so the same frames draw the same
1411
+ pixels; `puffs()` lists every puff at the current time in world space, and `stats` counts the last frame's puffs,
1412
+ `dropped`, `culled` and `drawn`.
1413
+ - Lighting follows Harris, "Real-Time Cloud Rendering for Games" (GDC 2002): the light reaching a puff is the product
1414
+ of the transmittances of the puffs nearer the light (each weighted by a Gaussian of how far the puff lies off its
1415
+ light ray, in units of its radius), times the Rayleigh phase 3/4 (1 + cos^2) between the view ray and the light.
1416
+ It is computed per puff on the CPU, so a cloud of a few hundred puffs costs microseconds; the soft fade uses
1417
+ Lorach's symmetric contrast curve ("Soft Particles", NVIDIA 2007).
1418
+
1419
+ ### Ballistic debris
1420
+
1421
+ `Debris` (`@margoengine/vfx/debris`) throws earth clods, rock blocks or timber out of an explosion or an impact over
1422
+ film time: `count` pieces leave from within `radius` of `origin`, the inner ones first and fastest (launch speed
1423
+ `speed (x / R)^(-1 / mu)`, Housen and Holsapple 2011), fly through air drag (`1/2 rho Cd A v^2`, Mastin 2001), tumble,
1424
+ land on `ground` and raise a dust puff that grows and fades. Every path is solved once when the Debris is made, so
1425
+ `seek(t)` shows the same pieces from any frame in any order. `draw(instances)` writes the pieces into a gltf
1426
+ `PBRInstances` of any unit-sized mesh; `drawPuffs(clouds)` puts the landing dust into a `Clouds`.
1427
+
1428
+ ```ts
1429
+ import { Debris } from 'margoengine/vfx/debris';
1430
+
1431
+ const debris = new Debris({ origin: [0, 0, 0], radius: 60, count: 400, speed: 35, size: [0.3, 2], density: 2300 });
1432
+ debris.seek(4);
1433
+ const flying = debris.pieces().filter((p) => p.state === 'flying').length;
1434
+ console.log(flying > 0); // true: four seconds after the throw, clods are in the air
1435
+ ```
1436
+
1437
+ Options (all optional): `origin` ([0, 0, 0]), `radius` (30 m), `count` (300, up to 20,000), `start` (0 s), `spread`
1438
+ (1.5 s over which the pieces leave, inner first), `speed` (40 m/s at the rim), `maxSpeed` (4 x `speed`), `mu` (0.41),
1439
+ `angle` (50 degrees) and `angleSpread` (12), `size` ([0.2, 1.2] m, log-uniform), `density` (1,800 kg/m^3), `drag` (1),
1440
+ `airDensity` (1.2), `ground` ((x, z) => height; default flat at the origin), `tumble` (6 rad/s), `color`, `puffLife`
1441
+ (6 s), `puffColor`, `puffScale` (0.04 m per cube root of joule) and `seed`. `pieces(t)` gives each piece's position,
1442
+ velocity, size, state, launch and landing times; `puffs(t)` the live landing puffs.
1443
+
1444
+ ### Wind-blown sand
1445
+
1446
+ `BlownSand` (`@margoengine/vfx/blown-sand`) draws sand that the wind lifts off bare ground: streaks of saltating grains
1447
+ drifting low over the sand, plumes of spindrift streaming off dune crests, and a thin haze sheet of moving sand near the
1448
+ ground. It is one instanced draw (frame-graph order 6, with the particles). Where the wind is under the threshold of
1449
+ motion nothing is drawn, and the same time always draws the same sand, so it seeks to a cut like the rest of vfx.
1450
+
1451
+ ```ts
1452
+ import {Engine} from 'margoengine/core';
1453
+ import {BlownSand, sandThreshold} from 'margoengine/vfx/blown-sand';
1454
+
1455
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1456
+ backend: 'webgpu',
1457
+ camera: {position: [0, 1.6, -20], target: [0, 0.5, 0]},
1458
+ });
1459
+ const dune = (x: number, z: number) => 6 * Math.exp(-((z - 30) * (z - 30)) / 400); // a ridge along x, 6 m high
1460
+ const sand = BlownSand.attach(engine, {ground: dune, wind: {speed: 12, direction: 0, gust: 0.35}, seed: 7});
1461
+ engine.start();
1462
+ console.log(sandThreshold().toFixed(1)); // 6.6, m/s at 10 m for 0.25 mm sand
1463
+ console.log(sand.time); // 0
1464
+ ```
1465
+
1466
+ - Options, read every frame (a change of `ground`, `mask`, `amount`, `seed` or the wind direction lays the particles out
1467
+ again): `ground` (a height function `(x, z) => y` or a grid `{samples, width, depth, cellSize, origin}`; required),
1468
+ `wind` (`{speed, direction, gust, gustPeriod}`, or a `WindField` from `@margoengine/weather`; required for prescribed transport; `direction`
1469
+ is where it blows toward in radians, 0 toward +z and pi/2 toward +x), `mask` (0 to 1 at world x, z: 0 where the sand
1470
+ is wet, grassed or paved), `amount` (1), `threshold` (default `sandThreshold(grainSize)`), `grainSize` (0.00025 m),
1471
+ `windHeight` (10 m), `color` (the grain albedo, `[0.5, 0.42, 0.29]` linear), `streak` (0.6 m), `height` (0.3 m),
1472
+ `opacity` (0.6), `gustiness` (the wind's `gust`, else 0.35), `seed` (1), `limit` (32,768 particles), `range` (250 m
1473
+ around the camera), `fog` (exponential density per metre, 0), `softness` (0, with `depth` the scene depth of
1474
+ `@margoengine/post/depth`: the sand fades into surfaces behind it) and `sky` (a `Sky` with `lighting.ambient` or
1475
+ `environment`: the grains take its diffuse light and the sky behind them; without it the engine's ambient term).
1476
+ `BLOWN_SAND_SETTINGS` lists every option with its default, range and unit as data.
1477
+ - `sand.time` is the clock (the sum of frame deltas); set it or call `advanceTime(seconds)` to seek. `grains(eye)`
1478
+ lists the laid-out grains for a camera position. `sandEmission(options, x, z)` gives the flux, the speed-up and whether spindrift
1479
+ blows off there (`brink`), and `sandFlux(ratio)` the saturated flux for the ratio of shear velocity to its threshold.
1480
+ - Physics. The threshold is Bagnold's fluid threshold (1941), u*t = 0.1 sqrt((rho_s - rho_a) / rho_a g d), carried to
1481
+ `windHeight` up the logarithmic profile over a rippled bed (roughness 0.1 mm, Sherman and Farrell 2008). The flux over
1482
+ the threshold follows Owen (1964) and Kawamura (1951) as reviewed by Kok et al. (2012), saturating over metres
1483
+ (Duran and Herrmann 2006). The wind over a dune is sped up by 1 + A (H[h'] + B h') with the Hilbert transform H of
1484
+ the slope (Jackson and Hunt 1975; Sauermann et al. 2001; Kroy et al. 2002, with A = 3 and B = 0.3 chosen here,
1485
+ not their values), largest just upwind of the crest and zero behind the brink, where grains fly out over the slip face
1486
+ (Bagnold 1941). Streaks are about 0.2 m wide and a metre apart and follow the gusts (Baas and Sherman 2005; Anderson
1487
+ and Haff 1988). The grains scatter light as half a diffraction lobe (Henyey and Greenstein 1941) and half a Lambert
1488
+ sphere, with a single-scattering albedo from the bed colour (Hapke 1981). Sources are in `docs/clean-room.md`.
1489
+ - Limits: the sand is drawn within `range` of the camera, `limit` particles at most; with no `sky` the grains are lit
1490
+ by the engine's ambient term only, so under a bright clear sky the plumes read as faint smudges (`docs/limits.md`).
1491
+
1492
+ #### Blown snow from a live bed
1493
+
1494
+ Pass an attached `AeolianTransport` as `transport` to use the bed's moving mass and velocity. Supply a live terrain
1495
+ sampler as `ground`. `referenceMass` sets the moving mass at half visual strength (0.02 kg/m² by default).
1496
+ `transport.sample(x, z)` gives the local moving mass and velocity. Advance the physics world and
1497
+ set the effect clock to the corresponding time when seeking a shot.
1498
+
1499
+ ```ts
1500
+ import { Engine } from '@margoengine/core';
1501
+ import { AeolianTransport } from '@margoengine/physics/aeolian';
1502
+ import { TerrainBed } from '@margoengine/physics/earth';
1503
+ import { PhysicsWorld } from '@margoengine/physics/world';
1504
+ import { BlownSand } from '@margoengine/vfx/blown-sand';
1505
+
1506
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1507
+ backend: 'webgpu', camera: { position: [-4, 2, -5], target: [0, 0.4, 2] },
1508
+ });
1509
+ const grid = { columns: 16, rows: 16, width: 16, depth: 16, centre: [0, 0] as const };
1510
+ const world = new PhysicsWorld({ rate: { num: 60, den: 1 } });
1511
+ const terrain = new TerrainBed();
1512
+ const transport = await AeolianTransport.load();
1513
+ world.add(terrain, { ...grid, elevations: (_x, z) => Math.max(0, 0.8 - Math.abs(z) * 0.16) });
1514
+ world.add(transport, { grid, material: 'snow', frictionVelocity: [0, 0.55], open: { maxZ: true } });
1515
+ world.advance(180);
1516
+ const bed = terrain.bed;
1517
+ const snow = BlownSand.attach(engine, { ground: (x, z) => bed.sample(x, z).elevation, transport, color: [0.87, 0.90, 0.94] });
1518
+ snow.time = 3;
1519
+ engine.render({ delta: 0 });
1520
+ ```
1521
+
1522
+ ### Cannon fire and burning hulls
1523
+
1524
+ `@margoengine/vfx/gunfire` fires muzzle-loading cannon for sea battle films: the muzzle flash and its light, the white
1525
+ powder-smoke bank that rolls out of the barrel, hangs and drifts down the wind, the burning powder grains thrown out
1526
+ with the flash, the round shot's flight through the wind to the water or into a ship (splinters, holes, ricochets),
1527
+ and a burning wooden hull with flames over the deck, flames out of the gun ports, dark smoke above them, char on its
1528
+ timbers and masts that fall when their stays burn.
1529
+
1530
+ ```ts
1531
+ import {Engine} from 'margoengine/core';
1532
+ import {VFX} from 'margoengine/vfx';
1533
+ import {Battery, HullFire, hullMaskImage} from 'margoengine/vfx/gunfire';
1534
+ import {Texture} from 'margoengine/materials';
1535
+ import {GUNFIRE_LOOKS, applyGunfireSettings} from 'margoengine/vfx/settings';
1536
+ import {SPLASH_EMITTERS, ShotSplash} from 'margoengine/water/splash';
1537
+
1538
+ const engine = await Engine.create(document.querySelector('canvas')!, {
1539
+ backend: 'webgpu',
1540
+ camera: {position: [-60, 12, 140], target: [60, 5, 0]},
1541
+ });
1542
+ // Eight 32-pounders down the side of a ship, firing a ripple broadside two seconds in.
1543
+ const guns = Array.from({length: 8}, (_, i) => ({position: [6.5, 3, -21 + 6 * i] as [number, number, number], direction: [1, 0.02, 0] as [number, number, number]}));
1544
+ const battery = Battery.attach(engine, {guns, wind: [3, 1]}).broadside(2, 0.3);
1545
+ applyGunfireSettings(battery, GUNFIRE_LOOKS.trafalgar);
1546
+ // The enemy ship 300 m off, burning from her waist: her char and shot holes go in one mask for her hull's material.
1547
+ const enemy = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 300, 0, 0, 1];
1548
+ const mask = new Texture(hullMaskImage(), {colorSpace: 'linear'});
1549
+ battery.options.targets = [{ship: enemy, mask}];
1550
+ const fire = HullFire.attach(engine, {
1551
+ ship: enemy,
1552
+ ignition: {at: [0, 6, 0], time: 0},
1553
+ ports: [{position: [6.5, 3, -8]}, {position: [6.5, 3, 8]}],
1554
+ wind: [3, 1],
1555
+ mask,
1556
+ masts: [{position: [0, 6, 2], height: 45, yards: [{at: 30, length: 24}]}],
1557
+ });
1558
+ // Where each shot lands: crown, column and mist (from @margoengine/water/splash).
1559
+ ShotSplash.attach(engine, {source: battery, particles: (kind) => VFX.attach(engine, {...SPLASH_EMITTERS[kind]})});
1560
+ for (let i = 0; i < 120; i++) engine.render({delta: 1 / 30});
1561
+ console.log(battery.fired.length, battery.impacts().length, battery.hits().length, (fire.heat / 1e6).toFixed(1), 'MW', fire.rigging().length);
1562
+ ```
1563
+
1564
+ - `Battery` options: `guns` (up to 128; each a muzzle `position`, a barrel `direction`, `charge` 4.5 kg,
1565
+ `muzzleVelocity` 490 m/s and a `shot`, by default a 14.5 kg iron ball), `shots` (`{gun, time}` in film seconds; add
1566
+ more with `fire(gun, time)` or `broadside(time, interval, guns)`), `ship` (a `Floater` of `@margoengine/water/ships`
1567
+ or anything with `matrix()`: the guns ride it and are read at each shot), `wind` (`[x, z]` m/s or a field; leave it
1568
+ out and a `WeatherDrive` writes the weather's), `water` (a height source for the shots' fall; default the plane
1569
+ `seaLevel`), `lights` (a `Clustered` or a `LightGroups`: each gun lights the scene through a light of its own), and
1570
+ the look rows below. `banks` (48, at most 512) and `puffs` (24) size the pool of smoke banks, the oldest reused.
1571
+ - The flash. Black powder burns to 43% gas and 56% solids (Noble and Abel 1875). The hot gas, still fuel-rich, burns
1572
+ again in the air past the muzzle (Klingenberg and Heimerl 1992) as a fireball the size of the charge's gas at its
1573
+ explosion temperature (`muzzleFlash(charge)`), lasting `flashDuration` (0.04 s). A flash is far shorter than a frame,
1574
+ so each frame shows the share of it that its shutter caught (`flashExposure`, `shutter` 0.5 for a 180 degree shutter):
1575
+ the flash lands on one frame at any frame rate and carries the same total light. The light is a sphere of the
1576
+ fireball's radius at its radiance (`flashLight` scales it).
1577
+ - The smoke. Each shot's smoke is a puff that grows as a quarter of the distance it has moved through the air, keeps
1578
+ the gas's momentum along the barrel and rises on its heat (Richards 1965, Scorer 1957), levelling off in stable air
1579
+ (`stability`, N^2). Once still, the air's turbulence spreads it at Richardson's law (Batchelor 1952), at the
1580
+ dissipation of the sea's surface layer (Large and Pond 1981), and the wind carries it. `PowderPuff` gives the path.
1581
+ How dense it looks is its smoke mass over its area (`extinction`, 3 m^2/g for the white salts); `smokeColor(soot)`
1582
+ gives white powder smoke at 0 and black soot at 1 (Mulholland and Croarkin 2000). The banks are `Clouds` puffs (drawn
1583
+ with the puff-cloud lighting above), so `clouds` takes their light direction, colour and ambient.
1584
+ - The shot. `shotPath` (and `shotFlight`, its impact) flies a round shot under gravity and sphere drag through the
1585
+ sound barrier (`sphereDrag`, Hoerner 1965) on its velocity relative to the wind, so a crosswind moves it by McCoy's
1586
+ (1999) lag-time rule; the battery flies its shots in its wind (its own, or the one the weather wrote). `impacts()`
1587
+ lists where and when each shot meets the water, at what speed, for `ShotSplash` of `@margoengine/water/splash`,
1588
+ which plans skips and the entry splash from it.
1589
+ - Sparks. About 2% of a black-powder charge leaves the bore still burning (Margo's share). Each grain (`grain`, 3 mm)
1590
+ burns from its surface at about 1 cm/s (Piobert's law; Conkling and Mocella 2019), so it is out in d / 2r, and its
1591
+ quadratic drag slows it meanwhile: `powderGrains(charge, grain, speed)` gives the count, the burn time, the reach
1592
+ and the speed curve each shot's `sparks` emitter flies them with. They are drawn as the streaks the shutter records
1593
+ (`sparks` multiple, `sparkRadiance` 30, `sparkSize` 0.3 m for the flame and halation round each grain).
1594
+ - Hits on ships. `targets` lists ships the shots can strike (each a `ship` matrix or `Floater`, `length`, `beam`,
1595
+ `height`, `draft`, side `thickness` 0.5 m and `wood`, white oak by default: 680 kg/m^3 and 7.4 MPa across the
1596
+ grain, Wood Handbook 2010). At a side the ball meets Poncelet's resistance A (R + rho v^2 / 2) (`woodPenetration`,
1597
+ `woodExitSpeed`): a ball that cannot sink a radius in glances off and flies on (a ricochet, which may still reach
1598
+ the water); one whose reach is more than the side holes it and goes on at the exit speed, and out of the far side
1599
+ the same way; any other lodges. `hits()` lists the strikes whose time has come; each throws `splinters`
1600
+ (hand-sized pieces of the hole's timber) and draws its hole into the target's `mask`.
1601
+ - The hull mask. `hullMaskImage()` makes a blank RGBA8 image for a materials `Texture`; `HullFire` paints its char
1602
+ (red) and the glow of the fire over it now (green) into it, and the battery its holes (blue). `hullMaskUVs(positions,
1603
+ hull)` gives a hull mesh the UV set that reads it: starboard on the left half, port on the right, stern to bow, keel
1604
+ to deck. Use red as the mask of a dark char layer of `layered()`, green for an ember glow and blue for the holes.
1605
+ - Char. Timber under fire chars at `charRate` (0.5 mm/min for oak, 0.65 for softwood: EN 1995-1-2 table 3.1) for as
1606
+ long as the fire over it has burnt. `exposure(z)` is that time in closed form and `charDepth(point)` the depth in
1607
+ mm: the deck chars where its stretch burns and 1.5 m down the side, and each flaming port the side above it to its
1608
+ flame height. A face reads black past about half a millimetre.
1609
+ - Rigging. `masts` (a foot on the deck, a `height`, `yards` and the side it `falls` to) stand until the fire has
1610
+ reached them and burnt their stays (`mastFall`, 600 s, or `fallAt`), then topple on their feet as falling chimneys
1611
+ (`mastTopple`; Varieschi and Kamiya 2003) until their tops are in the water. `rigging()` gives each mast and yard
1612
+ as a world matrix for the scene's mast and yard meshes, riding the ship.
1613
+ - `HullFire` burns a wooden hull (`ship`, a `Floater` or a fixed matrix; `hull` 50 by 13 m, deck 6 m). The fire spreads
1614
+ from `ignition` along the hull at `spread` (0.03 m/s), each stretch burning at `deckHeat` (250 kW/m^2) for `burnTime`
1615
+ (2,400 s) and then dying down. Each open gun port (`ports`) burns as a room vents through a window: 0.09 A sqrt(H)
1616
+ kg/s of wood (Kawagoe 1958), with a flame out of it Law's (1978) height above the opening. Deck flames stand at
1617
+ Heskestad's length for their heat. `flames` (10) `Flame` volumes take the hottest; `columns` (2) `Smoke` columns
1618
+ carry 0.7 of the heat as smoke darkened by `soot` (0.35, wood and tar). `heat` is the fire's power in watts and
1619
+ `flames()` lists every flame.
1620
+ - Both have `advanceTime(delta)`: their state is a function of film time, so a cutscene `seek` (with them in `extra`)
1621
+ lands where playing lands. `time` is their clock.
1622
+ - Sliders: `GUNFIRE_SETTINGS` and `HULL_FIRE_SETTINGS` in `@margoengine/vfx/settings` hold every look row on the
1623
+ shared shape (flash size, radiance and light, shutter, smoke extinction, colour, dispersion and life, air stability,
1624
+ heat share, entrainment, sparks, spark radiance and size, grain size, splinters; spread, burn time, deck heat,
1625
+ soot, opacity, char rate and mast fall). The `wind.x` and `wind.z` rows are null by
1626
+ default and follow the weather. `GUNFIRE_LOOKS` names `trafalgar`, `breezy` and `animatedFeature`, and
1627
+ `HULL_FIRE_LOOKS` `tarred`, `smouldering` and `animatedFeature`; `applyGunfireSettings(battery, values)` and
1628
+ `applyHullFireSettings(fire, values)` write a set.
1629
+ - `test/gunfire.test.js` holds the laws: the products, the flash energy at 24, 30, 60 and 120 frames a second, the puff
1630
+ closed forms, the level-off scaling, the dispersion, a shot in vacuum, the vent laws and the hull's heat;
1631
+ `test/gunfire-hits.test.js` the lag-time rule, the grains' burn-out and drag, Poncelet's law and the three outcomes
1632
+ of a strike, the holes, the Eurocode char rate and a falling mast's energy and fall time. The GPU check
1633
+ (`test/gunfire-check.mjs`, WebGPU and MARGO_COMPAT=1) draws the flash on its frame only, the bank where the law puts
1634
+ it, flames and dark smoke over a hull, sparks past the flash, splinters at a strike, a charred hull side that matches
1635
+ its CPU twin, the same bytes twice, and 60 frames of live rows with no new GPU object.
1636
+ - Limits: a strike reads the target ships where they are at the shot; masts fall from fire, not from shot; the
1637
+ rigging is rigid pieces (no ropes or sails); sinking is a Blender bake (`docs/limits.md`). Sources are in
1638
+ `docs/clean-room.md`.
1639
+
1640
+ ## Preview tier contract
1641
+
1642
+ GPU-only attachments refuse the small WebGL2 preview during setup with `MargoError` code `unsupported` and a help link. Native WebGPU and the full WebGL2 compatibility layer expose the WebGPU-shaped device these passes need. Use [automatic escalation](../webgl2-compat/README.md#automatic-escalation) with awaited setup to load the full layer when required. Existing real preview routes remain available.