Shaders / Primitives / Hatch

Hatch

Diagonal stripes of whole cells that drift: the rail of the landing.

Drawn with
GLSL + CPU
Chain
constant → pixelate → ordered → hatch → palette
Code
@forgeron/shaders: hatchMeta, hatchGlsl, hatchCpu
hatch pass ·
/01 → What it is
  • A cell is striped when (x ± y + the drift) mod `period` is under `width`; the area is a rectangle given as shares of the frame.
  • In the Progress and Selection examples.
  • Both sides: GLSL and CPU, with the same result. A cell pass.
  • CPU against GLSL: exact: 0 pixels differ.
/02 → In the examples
ExampleChain
Progressconstant → pixelate → ordered → hatch → hatch → palette
Selectionconstant → pixelate → ordered → hatch → palette
/03 → Versions
  • hatch pass ·
    /01 Liveevery setting, on "hatch"
  • /02 Selectionperiod 6, width 1, up
/04 → Use it
/01 → In a preset, with its defaults
{
  "type": "hatch",
  "tone": 1,
  "over": [
    0
  ],
  "period": 4,
  "width": 2,
  "speed": 0,
  "direction": "down",
  "x0": 0,
  "x1": 1,
  "y0": 0,
  "y1": 1
}
/02 → One side per page
import { hatchMeta, hatchGlsl, hatchCpu } from "@forgeron/shaders";

// hatchMeta: description and params schema (validation, the editor)
// hatchGlsl: the GPU side, for bindChain then glPipeline
// hatchCpu: the CPU side, for bindChain then runCpu
/03 → Preset "hatch" (builtInPresets())
{
  "$schema": "https://shaders.forgeron.sh/schema/preset.v1.json",
  "version": 1,
  "name": "hatch",
  "source": {
    "type": "constant",
    "value": 0
  },
  "passes": [
    {
      "type": "pixelate",
      "size": 4
    },
    {
      "type": "ordered",
      "matrix": 8,
      "centered": true,
      "origin": "top",
      "steps": [
        [
          0,
          1
        ]
      ]
    },
    {
      "type": "hatch",
      "tone": 1,
      "over": [
        0
      ],
      "period": 4,
      "width": 2,
      "speed": 6,
      "direction": "down",
      "x0": 0.08,
      "x1": 0.6,
      "y0": 0.4,
      "y1": 0.6
    },
    {
      "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()["hatch"]);
// 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()["hatch"]), 320, 200, { time: 1.7 }));
API → Options
OptionTypeDefaultDescription
tonenumber1Tone of the stripes.
overnumber[][0]Tones the stripes may cover.
periodnumber4Cells between two stripes.
widthnumber2Cells of a stripe.
speednumber0Cells per second.
direction"down" | "up""down"Lean of the stripes.
x0number0Left edge of the area.
x1number1Right edge of the area.
y0number0Top edge of the area.
y1number1Bottom edge of the area.