Cinderfox

Shaders

A shader is a small program that decides the colour of every pixel of something as it is drawn. Cinderfox has two kinds, written in the same language (WGSL) and kept in the same kind of file (.wgsl, anywhere in the project — shaders/ is the usual place):

  • A sprite’s shader colours one sprite, or every particle of an emitter: a shine sweeping across a logo, a hit flash, an outline, a dissolve, grass swaying, glass you see the world through.
  • A pass over the picture colours the whole screen once it is drawn: a vignette, scanlines, a colour grade, a wipe between scenes.

You write only the interesting part — what colour a pixel is. Cinderfox puts everything else around it: where the sprite is, its picture, the time, the 2D lighting, the blending. A shader that will not compile never stops the game: the sprite is drawn plain, and the editor says what is wrong and on which line.

The fastest way to start is a starter: New → Shader in the Files dock gives you any of the ones at the end of this page, ready to change. An agent writes one with save_shader, which answers with whether it compiles.

A sprite’s shader

fn fragment(in: Fragment) -> vec4<f32> {
    return textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
}

That is the plain sprite: its picture, times its colour. fn fragment is called for every pixel of the sprite and returns its colour, red, green, blue and alpha, each 0 to 1.

Give a sprite a shader in its shader field — the Inspector has a picker — or from a script:

world.modify(logo, "Sprite", function(s)
    s.shader = "shaders/shine.wgsl"
    s.shader_params = { phase = 0.3 }
    return s
end)

A ParticleEmitter has the same fields, and its shader draws every particle.

What a sprite’s shader can read

Name What it is
in.uv Where this pixel is on the picture’s texture — what you sample it at.
in.local Where this pixel is across the sprite, 0 to 1 from its top-left corner, whatever part of a sheet its picture comes from. A flipped sprite’s local flips with it. A nine-slice panel’s runs across the whole panel.
in.size The sprite’s size, in world units.
in.screen_uv Where this pixel is on the screen, 0 to 1 across the game’s view.
in.color The sprite’s color.
u.time Seconds the game has been running — what makes a shader move. It runs in the editor while stopped, so you see a shader move while you place it.
u.resolution The game’s view in pixels: width, height, then 1 ÷ each.
t_diffuse, s_diffuse The sprite’s picture and its sampler.
forge_linear, forge_nearest Two samplers, smooth and pixel-sharp, for anything else you sample.
forge_texel() One pixel of the picture, in uv — the step to a neighbouring pixel.
forge_to_srgb(rgb), forge_to_linear(rgb) A colour sampled from a picture is linear; one an artist picked is sRGB. Convert to mix colours the way they look.
forge_screen(uv) What is already drawn under the sprite — see Reading the screen.

What fragment returns is lit by the scene’s 2D lighting as a plain sprite is (unless the sprite’s lit is false), and hidden behind terrain as a plain sprite is.

Params

A shader with numbers you want to set per sprite — a phase, a strength, a colour — declares them in struct Params, and gives them defaults with DEFAULTS:

struct Params { phase: f32, tint: vec4<f32> };
const DEFAULTS = Params(0.0, vec4<f32>(1.0, 1.0, 1.0, 1.0));

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
    return c * params.tint * (0.75 + 0.25 * sin(u.time * 3.0 + params.phase));
}

A field is a number (f32, i32, u32) or a vector of them (vec2<f32> … vec4<f32>). Each sprite sets its own in shader_params, by name — a number, or a list for a vector:

"Sprite": (texture_name: "art/logo/s.svg", width: 64.0, height: 80.0,
           shader: Some("shaders/shine.wgsl"),
           shader_params: {"phase": 0.3, "tint": [1.0, 0.9, 0.6, 1.0]})

A field a sprite does not set is the shader’s DEFAULTS (zero without them). The Inspector lists every field under the sprite, with the ones it sets highlighted. Sprites with the same shader and the same params are still drawn together, so a shader on five hundred bullets costs one draw.

A name the shader has not got, or a list of the wrong length, is said once in the log and left out.

Extra textures

A shader can read up to four more pictures — a noise pattern, a palette, a mask — declared at the top without anything else on the line:

var noise: texture_2d<f32>;

fn fragment(in: Fragment) -> vec4<f32> {
    let n = textureSample(noise, forge_linear, noise_uv(in.local)).r;
    return textureSample(t_diffuse, s_diffuse, in.uv) * in.color * n;
}

Each sprite names them in shader_textures: {"noise": "art/noise.png"}. Sample one through its _uv helper (noise_uv for noise): an export packs pictures onto shared sheets, and the helper finds yours on its sheet. Without it, the shader would read the whole sheet.

Moving the corners

fn vertex is called for each corner of the sprite before the camera, and may move it — sway, a squash, a wobble:

fn vertex(v: SpriteVertex) -> SpriteVertex {
    var out = v;
    out.position.x += sin(u.time) * 4.0 * (1.0 - v.local.y);
    return out;
}

SpriteVertex has position (world units), color, uv, local and size. A sprite has four corners, so vertex moves the rectangle; it cannot bend it.

Blending

A sprite’s blend says how it lands on what is under it, shaded or not: "alpha" (the default), "add" (light, fire, sparks), "screen" (a glow that rolls off instead of clipping) or "multiply" (a tinted shadow).

Sharing code

// #include "shaders/lib/noise.wgsl"

adds that file’s functions to this one. A file that defines neither fragment nor post is only for including, and is not offered in the picker. A mistake in it is reported against its own name and line.

A pass over the picture

fn post(p: PostIn) -> vec4<f32> {
    let c = screen_at(p.uv);
    return vec4<f32>(1.0 - c.rgb, 1.0);
}

fn post is called for every pixel of the game’s view once the picture is drawn, and returns what it becomes — this one inverts it. It reads:

Name What it is
p.uv Where this pixel is across the view, 0 to 1 from the top-left.
p.pixel The same, in the view’s pixels.
p.design_uv Where it is across the game’s design frame, where the HUD is: 0 to 1 inside it, beyond that outside. The same as p.uv unless the passes cover a 3D world past the letterbox (below).
screen_at(uv) The picture so far at uv — clamped at the edges, so a wobble near one reads the edge.
screen_texel() One pixel of the view, in uv.
u.resolution The view’s size in pixels: what the pass covers.
u.time, params, extra textures, forge_to_srgb, forge_to_linear, forge_linear, forge_nearest As for a sprite.

A pass goes on the scene’s Camera, in PostEffects:

"PostEffects": (
    passes: [(shader: "shaders/vignette.wgsl", params: {"strength": 0.6})],
    under: Some(10),
)

Passes run in order, each reading what the one before drew. under is the layer they stop below: layer 10 and up — a HUD — is drawn over the passes, untouched, so scanlines do not blur the score. Without it the passes cover everything.

What the view is. A 2D game with a fixed design size is drawn inside the design’s frame, letterboxed on a screen of another shape, and the passes cover that frame: the bars are left as they are. A scene with an active Camera3d draws its world to the window’s edges, bars and all, and there the passes cover the whole window too, as rain or a colour grade should. The HUD stays in the design frame either way, and a pass that lines up with it reads p.design_uv.

From a script

screen.post({ { shader = "shaders/wipe.wgsl", params = { amount = 0 } } }, { under = 10 })
-- each tick, as the curtain closes:
screen.post_params(1, { amount = t })
-- and when it is done:
screen.post(nil)

screen.post stands in for the camera’s PostEffects, and lasts across scene.replace — so a wipe that closes in one scene opens in the next. screen.post(nil) hands back to the scene’s own; Stop ends it.

Reading the screen

forge_screen(uv) in a sprite’s shader reads what is drawn under it so far: the layers below it, and what its own layer drew before it. That is glass, heat haze, a lens, water that shows the world through it:

fn fragment(in: Fragment) -> vec4<f32> {
    let behind = forge_screen(in.screen_uv + vec2<f32>(sin(u.time + in.local.y * 20.0) * 0.005, 0.0));
    return vec4<f32>(behind.rgb, in.color.a);
}

Give such a sprite lit: false: what it reads is already lit. Reading the screen costs a copy of the picture, once for each layer that has such a sprite on it; a frame with none costs nothing. In a canvas, and in a layer drawn over a pass’s under, there is no screen behind to read.

When it goes wrong

  • The sprite is drawn plain. The shader did not compile. script_trouble lists it with kind: "shader" and the compiler’s message, which names the file and the line. Fix the file and save: it is compiled again, and the sprite shows it at once.
  • An export stops with a shader that will not compile: the export refuses to ship one, with the same message.
  • A param does nothing. The log says so once, if the shader has no field of that name.
  • An extra texture shows the wrong picture in an export. Sample it through its _uv helper.

Starters

Each is on New → Shader, and in editor/assets/shaders/ of Cinderfox’s source.

Shine

// A band of light sweeping across the picture, again and again. Give each
// of a row of letters its own `phase` and the shine runs along the word.
struct Params { phase: f32, speed: f32, width: f32, tint: vec4<f32> };
const DEFAULTS = Params(0.0, 0.5, 0.12, vec4<f32>(1.0, 1.0, 1.0, 0.8));

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
    // Where the band is now, from just before the left edge to just past the right.
    let at = fract(u.time * params.speed + params.phase) * 1.6 - 0.3;
    let band = 1.0 - smoothstep(0.0, params.width, abs(in.local.x + in.local.y * 0.3 - at));
    return vec4<f32>(c.rgb + params.tint.rgb * params.tint.a * band * c.a, c.a);
}

Flash

// The whole picture towards one colour — a hit flash. A sprite's own
// `color` multiplies, so it can only darken; this can go to white.
struct Params { amount: f32, colour: vec4<f32> };
const DEFAULTS = Params(0.0, vec4<f32>(1.0, 1.0, 1.0, 1.0));

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
    return vec4<f32>(mix(c.rgb, params.colour.rgb, params.amount * params.colour.a), c.a);
}

Outline

// A line round the picture's shape, where it meets the clear around it.
// The picture needs a clear margin as wide as the line: nothing is drawn
// outside the sprite's own rectangle.
struct Params { colour: vec4<f32>, thickness: f32 };
const DEFAULTS = Params(vec4<f32>(1.0, 1.0, 1.0, 1.0), 1.0);

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
    let step = forge_texel() * params.thickness;
    var around = textureSample(t_diffuse, s_diffuse, in.uv + vec2<f32>(step.x, 0.0)).a;
    around = max(around, textureSample(t_diffuse, s_diffuse, in.uv - vec2<f32>(step.x, 0.0)).a);
    around = max(around, textureSample(t_diffuse, s_diffuse, in.uv + vec2<f32>(0.0, step.y)).a);
    around = max(around, textureSample(t_diffuse, s_diffuse, in.uv - vec2<f32>(0.0, step.y)).a);
    // Only outside the shape: where this pixel is clear and a neighbour is not.
    let edge = around * (1.0 - c.a);
    return mix(c, params.colour, edge);
}

Dissolve

// The picture burning away in a ragged pattern, its edge glowing — run
// `amount` from 0 to 1. The pattern is made here, so it needs no texture.
struct Params { amount: f32, edge: f32, glow: vec4<f32> };
const DEFAULTS = Params(0.0, 0.06, vec4<f32>(1.0, 0.5, 0.1, 1.0));

fn hash(p: vec2<f32>) -> f32 {
    return fract(sin(dot(p, vec2<f32>(127.1, 311.7))) * 43758.5453);
}

fn noise(p: vec2<f32>) -> f32 {
    let i = floor(p);
    let f = fract(p);
    let s = f * f * (3.0 - 2.0 * f);
    let top = mix(hash(i), hash(i + vec2<f32>(1.0, 0.0)), s.x);
    let bottom = mix(hash(i + vec2<f32>(0.0, 1.0)), hash(i + vec2<f32>(1.0, 1.0)), s.x);
    return mix(top, bottom, s.y);
}

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
    let n = noise(in.local * 8.0);
    if (n < params.amount) {
        discard;
    }
    let rim = 1.0 - smoothstep(params.amount, params.amount + params.edge, n);
    return vec4<f32>(mix(c.rgb, params.glow.rgb, rim * params.glow.a), c.a);
}

Sway

// The top of the picture swaying while its foot stays put — grass, a
// flag, a tree in the wind. `fn vertex` moves the corners before the
// camera; the bottom ones (`local.y` 1) do not move.
struct Params { amount: f32, speed: f32, phase: f32 };
const DEFAULTS = Params(6.0, 2.0, 0.0);

fn vertex(v: SpriteVertex) -> SpriteVertex {
    var out = v;
    out.position.x += sin(u.time * params.speed + params.phase) * params.amount * (1.0 - v.local.y);
    return out;
}

fn fragment(in: Fragment) -> vec4<f32> {
    return textureSample(t_diffuse, s_diffuse, in.uv) * in.color;
}

Palette

// Recolour a picture from a palette strip: the picture is drawn in greys,
// and each grey picks a column of the strip (black the left end, white
// the right). Set `shader_textures: {"palette": "art/palettes/red-team.png"}`
// and the same picture is another team.
var palette: texture_2d<f32>;

fn fragment(in: Fragment) -> vec4<f32> {
    let c = textureSample(t_diffuse, s_diffuse, in.uv);
    // The grey as it was drawn (sRGB), not as it is lit (linear).
    let index = forge_to_srgb(c.rgb).r;
    let swapped = textureSample(palette, forge_nearest, palette_uv(vec2<f32>(index, 0.5)));
    return vec4<f32>(swapped.rgb, c.a) * in.color;
}

Heat haze

// The world behind this sprite, shimmering — heat over a fire, a jet's
// exhaust. Draw it over what should shimmer, with `lit: false`; its own
// picture is not used, only its rectangle.
struct Params { strength: f32, speed: f32 };
const DEFAULTS = Params(0.01, 3.0);

fn fragment(in: Fragment) -> vec4<f32> {
    let t = u.time * params.speed;
    let shimmer = vec2<f32>(sin(in.local.y * 30.0 + t), cos(in.local.x * 25.0 + t * 0.8)) * params.strength;
    let behind = forge_screen(in.screen_uv + shimmer);
    // Fading out towards the edges, so the patch has no seam.
    let edge = min(min(in.local.x, 1.0 - in.local.x), min(in.local.y, 1.0 - in.local.y));
    return vec4<f32>(behind.rgb, smoothstep(0.0, 0.2, edge) * in.color.a);
}

Vignette

// The picture darkening towards its edges, and a colour flooding in from
// them on a hit — set `flash` to 1 and ease it back to 0. A pass over the
// whole picture: on the Camera's PostEffects, or in `screen.post`.
struct Params { strength: f32, flash: f32, flash_colour: vec4<f32> };
const DEFAULTS = Params(0.5, 0.0, vec4<f32>(1.0, 0.0, 0.0, 1.0));

fn post(p: PostIn) -> vec4<f32> {
    let c = screen_at(p.uv);
    let d = distance(p.uv, vec2<f32>(0.5));
    let dark = smoothstep(0.35, 0.85, d) * params.strength;
    let flood = params.flash * params.flash_colour.a * smoothstep(0.2, 0.8, d);
    return vec4<f32>(mix(c.rgb * (1.0 - dark), params.flash_colour.rgb, flood), 1.0);
}

Wipe

// A colour sweeping across the picture on a slant — a scene change's
// curtain. `amount` 0 is nothing covered, 1 everything: run it up, change
// the scene, run it down. In `screen.post`, which lasts across the change.
struct Params { amount: f32, softness: f32, colour: vec4<f32> };
const DEFAULTS = Params(0.0, 0.05, vec4<f32>(0.0, 0.0, 0.0, 1.0));

fn post(p: PostIn) -> vec4<f32> {
    let c = screen_at(p.uv);
    let x = p.uv.x * 0.8 + p.uv.y * 0.2;
    let edge = params.amount * (1.0 + params.softness);
    let cover = 1.0 - smoothstep(edge - params.softness, edge, x);
    return mix(c, params.colour, cover * params.colour.a);
}

CRT

// An old television: the picture bowed as a tube's glass bows it, with
// dark lines across it every `spacing` pixels of the screen. Keep the HUD
// out of it with `under`.
struct Params { spacing: f32, strength: f32, curve: f32 };
const DEFAULTS = Params(3.0, 0.35, 0.08);

fn post(p: PostIn) -> vec4<f32> {
    let centred = p.uv - 0.5;
    let bent = p.uv + centred * dot(centred, centred) * params.curve;
    if (any(bent < vec2<f32>(0.0)) || any(bent > vec2<f32>(1.0))) {
        return vec4<f32>(0.0, 0.0, 0.0, 1.0);
    }
    let c = screen_at(bent);
    let line = 0.5 + 0.5 * cos(bent.y * u.resolution.y * 6.2832 / max(params.spacing, 1.0));
    return vec4<f32>(c.rgb * (1.0 - params.strength * line), 1.0);
}

Grade

// The whole picture's colour: how saturated, how contrasty, and a tint —
// a cold night, a warm dusk, a faded memory. Worked in sRGB, the colours as
// an artist picks them.
struct Params { saturation: f32, contrast: f32, tint: vec4<f32> };
const DEFAULTS = Params(1.0, 1.0, vec4<f32>(1.0, 1.0, 1.0, 1.0));

fn post(p: PostIn) -> vec4<f32> {
    let c = forge_to_srgb(screen_at(p.uv).rgb);
    let grey = vec3<f32>(dot(c, vec3<f32>(0.299, 0.587, 0.114)));
    let graded = (mix(grey, c, params.saturation) - 0.5) * params.contrast + 0.5;
    return vec4<f32>(forge_to_linear(clamp(graded, vec3<f32>(0.0), vec3<f32>(1.0))) * params.tint.rgb, 1.0);
}