Sparks
Embers and flying sparks along the edge of a front, on a slow tick.
- Drawn with
- GLSL + CPU
- Chain
- front → pixelate → sparks → ordered → palette
- Code
- @forgeron/shaders: sparksMeta, sparksGlsl, sparksCpu
/01 → What it is
- Reads the distance and life a `front` source writes, and catches cells near the edge with a chance that falls with their depth.
- An integer hash (24 bits, exact in float32) gives the same embers on the GPU and the CPU. With `front`, the burns.
- Both sides: GLSL and CPU, with the same result. A cell pass.
- CPU against GLSL: tolerance 3 % of pixels (measured 0 to 0.014 %): the front's distance is float.
/02 → In the examples
| Example | Chain |
|---|---|
| Frame burn | front → pixelate → sparks → ordered → palette |
| Lobed burn | front → pixelate → sparks → ordered → palette |
/03 → Versions
- /01 Live
every setting, on "sparks" - /02 Sparse
density 0.25, no flying sparks
/04 → Use it
{
"type": "sparks",
"tick": 0.18,
"offset": 6,
"wobble": 1,
"density": 0.5,
"falloff": 0.07,
"fly": 0.003,
"reach": 10,
"tones": [
2,
3,
4
]
}import { sparksMeta, sparksGlsl, sparksCpu } from "@forgeron/shaders";
// sparksMeta: description and params schema (validation, the editor)
// sparksGlsl: the GPU side, for bindChain then glPipeline
// sparksCpu: the CPU side, for bindChain then runCpu{
"$schema": "https://shaders.forgeron.sh/schema/preset.v1.json",
"version": 1,
"name": "sparks",
"source": {
"type": "front",
"shape": "rect",
"direction": "close",
"cycle": 4.2,
"hold": 0.9,
"ring": 4
},
"passes": [
{
"type": "pixelate",
"size": 4
},
{
"type": "sparks",
"tick": 0.18,
"fly": 0.003
},
{
"type": "ordered",
"matrix": 4,
"centered": false,
"origin": "top",
"steps": [
[
0,
1
],
[
1.5,
0
],
[
2.5,
0
],
[
3.5,
0
]
]
},
{
"type": "palette",
"tones": [
{
"var": "--bg",
"fallback": "#0a0a0a"
},
{
"var": "--line",
"fallback": "#2b2b2b"
},
{
"var": "--ink",
"fallback": "#ededed"
},
{
"var": "--accent",
"fallback": "#ff4f1f"
},
{
"var": "--danger",
"fallback": "#ff6b81"
}
],
"transparent": []
}
]
}import { builtInPresets } from "@forgeron/shaders";
import { mountPreset } from "@forgeron/shaders/dom";
// WebGL2: every step has GLSL. Validated first.
const handle = mountPreset(canvas, builtInPresets()["sparks"]);
// later: handle?.destroy();import { builtInPresets, chainOf, runCpu, toRgba } from "@forgeron/shaders";
// The CPU side of every step: RGBA bytes for a PNG, or tone paths for an SVG (toSvgPath).
const rgba = toRgba(runCpu(chainOf(builtInPresets()["sparks"]), 320, 200, { time: 1.7 }));API → Options
| Option | Type | Default | Description |
|---|---|---|---|
tick | number | 0.18 | Seconds per draw. |
offset | number | 6 | Ember depth, cells. |
wobble | number | 1 | Wave amplitude, cells. |
density | number | 0.5 | Chance, nearest layer. |
falloff | number | 0.07 | Chance lost a layer. |
fly | number | 0.003 | Flying spark chance. |
reach | number | 10 | Flight distance, cells. |
tones | number[] | [2,3,4] | Levels written: outer, middle, inner. |