Shaders / Primitives / Palette

Palette

Tone indices to colours: tokens of the page, or colours.

Drawn with
GLSL + CPU
Chain
field → pixelate → ordered → grid → palette
Code
@forgeron/shaders: paletteMeta, paletteGlsl, paletteCpu
palette pass ·
/01 → What it is
  • The last cell pass: tone `i` takes `tones[i]`, a CSS token read on the canvas (with a fallback) or a colour.
  • `transparent` lists the tones that let the page under the canvas show through.
  • Both sides: GLSL and CPU, with the same result. A cell pass.
  • CPU against GLSL: exact: 0 pixels differ.
/02 → In the examples
ExampleChain
Backgroundfield → pixelate → ordered → grid → palette
Titletone → ordered → reveal → palette
Imageimage → pixelate → image-dither → palette
Transitionfield → pixelate → ordered → dissolve → palette
Progressconstant → pixelate → ordered → hatch → hatch → palette
Emberhearth → palette
Selectionconstant → pixelate → ordered → hatch → palette
Feedbackconstant → pixelate → ordered → ring → palette
Frame burnfront → pixelate → sparks → ordered → palette
Lobed burnfront → pixelate → sparks → ordered → palette
/03 → Versions
  • palette pass ·
    /01 Liveevery setting, on "field-ember"
  • /02 Greys--bg, --line-strong, --ink-dim
/04 → Use it
/01 → In a preset, with its defaults
{
  "type": "palette",
  "tones": [
    {
      "var": "--bg",
      "fallback": "#0a0a0a"
    },
    {
      "var": "--ink",
      "fallback": "#ededed"
    },
    {
      "var": "--accent",
      "fallback": "#ff4f1f"
    }
  ],
  "transparent": []
}
/02 → One side per page
import { paletteMeta, paletteGlsl, paletteCpu } from "@forgeron/shaders";

// paletteMeta: description and params schema (validation, the editor)
// paletteGlsl: the GPU side, for bindChain then glPipeline
// paletteCpu: the CPU side, for bindChain then runCpu
/03 → Preset "field-ember" (builtInPresets())
{
  "$schema": "https://shaders.forgeron.sh/schema/preset.v1.json",
  "version": 1,
  "name": "field-ember",
  "source": {
    "type": "field",
    "mode": "ember"
  },
  "passes": [
    {
      "type": "pixelate",
      "size": 4
    },
    {
      "type": "ordered",
      "matrix": 8,
      "centered": false,
      "origin": "bottom",
      "steps": [
        [
          0,
          1
        ],
        [
          0.86,
          0.12
        ]
      ]
    },
    {
      "type": "grid",
      "gap": 0.16,
      "min": 4,
      "tone": 0
    },
    {
      "type": "palette",
      "tones": [
        {
          "var": "--bg",
          "fallback": "#0a0a0a"
        },
        {
          "var": "--ink",
          "fallback": "#ededed"
        },
        {
          "var": "--accent",
          "fallback": "#ff4f1f"
        }
      ],
      "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()["field-ember"]);
// 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()["field-ember"]), 320, 200, { time: 1.7 }));
API → Options
OptionTypeDefaultDescription
tonesColorRef[][{"var":"--bg","fallback":"#0a0a0a"},{"var":"--ink","fallback":"#ededed"},{"var":"--accent","fallback":"#ff4f1f"}]One colour per tone index.
transparentnumber[][]Tone indices drawn transparent.