Shaders / Primitives / Sparks

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
sparks pass ·
/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
ExampleChain
Frame burnfront → pixelate → sparks → ordered → palette
Lobed burnfront → pixelate → sparks → ordered → palette
/03 → Versions
  • sparks pass ·
    /01 Liveevery setting, on "sparks"
  • /02 Sparsedensity 0.25, no flying sparks
/04 → Use it
/01 → In a preset, with its defaults
{
  "type": "sparks",
  "tick": 0.18,
  "offset": 6,
  "wobble": 1,
  "density": 0.5,
  "falloff": 0.07,
  "fly": 0.003,
  "reach": 10,
  "tones": [
    2,
    3,
    4
  ]
}
/02 → One side per page
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
/03 → Preset "sparks" (builtInPresets())
{
  "$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": []
    }
  ]
}
/04 → Plain DOM
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();
/05 → Static, at build time
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
OptionTypeDefaultDescription
ticknumber0.18Seconds per draw.
offsetnumber6Ember depth, cells.
wobblenumber1Wave amplitude, cells.
densitynumber0.5Chance, nearest layer.
falloffnumber0.07Chance lost a layer.
flynumber0.003Flying spark chance.
reachnumber10Flight distance, cells.
tonesnumber[][2,3,4]Levels written: outer, middle, inner.