Shaders / Primitives / Ordered

Ordered

The Bayer threshold every other shader dithers with.

Drawn with
GLSL + CPU
Chain
field → pixelate → ordered → grid → palette
Code
@forgeron/shaders: orderedMeta, orderedGlsl, orderedCpu
ordered pass ·
/01 → What it is
  • A value becomes a tone index: on when it beats the matrix at that cell.
  • 2×2, 4×4 and 8×8, built by one recurrence (`bayer`, `matrix`). `ditherPath` draws it as a static SVG path at build time; `anvilPath` the logo.
  • No blur, no alpha: a gradient is a density of whole pixels.
  • 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
Transitionfield → pixelate → ordered → dissolve → palette
Progressconstant → pixelate → ordered → hatch → hatch → 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
  • ordered pass ·
    /01 Liveevery setting, on "field-ember"
  • /02 4×4matrix 4
  • /03 2×2ditherPath(64, 16, (x) => x / 63, 2)
  • /04 4×4ditherPath(64, 16, (x) => x / 63, 4)
  • /05 8×8ditherPath(64, 16, (x) => x / 63, 8)
  • /06 Anvil, ditheranvilPath(24, "dither")
  • /07 Anvil, fadeanvilPath(24, "fade")
/04 → Use it
/01 → In a preset, with its defaults
{
  "type": "ordered",
  "matrix": 8,
  "centered": true,
  "origin": "top",
  "steps": [
    [
      0,
      1
    ]
  ]
}
/02 → One side per page
import { orderedMeta, orderedGlsl, orderedCpu } from "@forgeron/shaders";

// orderedMeta: description and params schema (validation, the editor)
// orderedGlsl: the GPU side, for bindChain then glPipeline
// orderedCpu: the CPU side, for bindChain then runCpu
/03 → Static SVG, at build time
import { ditherPath } from "@forgeron/shaders";

// A ramp from left to right, 64 × 12 cells.
const d = ditherPath(64, 12, (x) => x / 63, 8);

<svg viewBox="0 0 64 12" shapeRendering="crispEdges" fill="currentColor">
  <path d={d} />
</svg>
/04 → In your own loop
import { bayer } from "@forgeron/shaders";

// on when the tone beats the threshold of this pixel
const on = tone > bayer(x, y, 8);
/05 → 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": []
    }
  ]
}
/06 → 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();
/07 → 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
matrix2 | 4 | 88Side of the Bayer matrix.
centeredbooleantrueThresholds at (m + 0.5) / n² instead of m / n².
origin"top" | "bottom""top"Row the matrix starts on.
steps[offset, scale][][[0,1]][offset, scale] per tone above the background: tone k when the value passes k consecutive steps.