Spielerando

Lesson 13 of 24Light with few colours8 minFree

Dithering in a Pixel Art Shader: Two Colours in 16 Steps

Dithering is how pixel art shows a shade the palette does not have: some pixels of a small block take the darker colour, the others keep theirs, and from a step back the eye mixes them. This lesson mixes every colour with its darker neighbour from the colour ramps lesson, in 16 steps, by a fixed 4 by 4 pattern. It turns the hard edge of a shade into a soft band.

A sandy bank in pixel art, seen from the side, in layers of sand, honey, cream and amber, full of small nest holes. Bees fly in and out, and the beekeeper stands at its foot in the meadow. A shade comes down from the top of the bank and draws back up; its edge is a band in which more and more pixels are darker the deeper it gets.

The whole bank is given to the rule. Above the edge every colour is mixed with its darker neighbour in 16 steps by a fixed 4 by 4 pattern, so the shade fades in without a colour that is not in the palette. As the edge moves, pixels turn one by one and stay where they are.

Also in this lesson 3 things to read up on 3 questions to check yourself

The idea

Between sand and wood, its darker neighbour, the palette has no colour. Put the two side by side in a fine weave, one wood pixel in four, then half and half, then three in four, and the eye reads shades between them. That is dithering, and it is part of the pixel art look: a sky that fades from one colour into the next, the soft edge of a shadow, light that thins out into the dark, mist.

In the picture, a sandy bank where wild bees nest is seen from the side. The overhang above throws a shade that reaches down the bank and draws back as the sun moves; dithering turns its edge from one hard line into a band of 16 steps.

How it works

The pattern is 16 numbers, 0 to 15, in a block of 4 by 4 pixels, laid over the whole screen like tiles. Each pixel reads the number at its own place in the tile: its column and its row, each counted round from 0 to 3. Then the rule needs one more number, the share: how many of every 16 pixels should be darker, from 0 to 16. A pixel takes the darker colour when its number in the pattern is below the share. At a share of 1, one pixel in every block is dark; at 8, half of them in an even weave; at 16, all.

The numbers in the pattern are placed so that the dark pixels lie spread out at every share, never in clumps, and a pixel that has turned dark stays dark while the share grows. This way of doing it is called ordered dithering, and the 4 by 4 table is known as a Bayer matrix: that is the name your AI knows it by.

Here the share comes from the row. From the edge of the shade upward it grows by one step every few rows, up to 16, full shade. The darker colour comes from the DARKER table of the colour ramps lesson, so sand mixes with wood, honey with amber, cream with sand.

The sliders: "edge of the shade" is the row where the shade ends, 64 at the start. "Softness" says how many rows each of the 16 steps takes, 3 at the start. At 1 the band is 16 rows tall; at 6 it is 96.

What goes wrong

Asked for a shade that is "smooth, without banding", an AI often reaches for noise: a random number for every pixel, new in every frame. On smooth graphics in high resolution that works, because the grain is finer than the eye can see. In pixel art every pixel is big. The grain clumps, a block gets more dark pixels than the share says or fewer, and sixty times a second all of it is thrown again: the shade boils.

Switch the picture to "With the mistake". The band turns into grain, and the grain keeps moving even where the edge stands still. Two tests catch it. "At each of the 16 steps, exactly that many pixels of every 4 by 4 block are darker" found four dark pixels in a block where the share was one. "The same pixel gives the same answer at every tick" found a pixel that changed while nothing moved.

There is a quieter mistake next to it: counting the pattern from the moving edge instead of from the screen. Then every pixel of the band flips whenever the edge moves by a row, and the band swims. The test "moving the edge changes only the pixels whose step changed: the pattern belongs to the screen" guards against that one. If your camera scrolls, count from the place in the world instead, so the pattern travels with the ground and not across it.

The mistake, line by line

Lines with - are the rule with the mistake, lines with + the rule as it should be.

- /* Dithering, with the mistake: instead of the fixed 4 by 4 pattern the rule asks a scrambled number
-    from 0 to 15 for the place and the tick, so the dark pixels are spread by chance and spread anew
-    sixty times a second. The soft edge turns to grain that boils, and the same share of shade gives a
-    different count of dark pixels in every block.
+ /* Dithering: two colours mixed without a third. Between a colour and its darker neighbour there is no
+    colour in the palette, so the rule makes some pixels of every 4 by 4 block darker and leaves the
+    others: 0 of 16 is the colour, 16 of 16 its neighbour, and the 15 steps between look like shades
+    that are not there. A fixed pattern says which pixel turns first, so the mix is even and stays put.
+    Here it softens the edge of a shade that moves over a sandy bank.

+    The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
+    palette's colours. The rule is asked for every pixel and answers with a number.
+
     From the course "Shaders for pixel art" on spielerando.com.
     SPDX-License-Identifier: MIT */
…
  export type Params = typeof DEFAULTS;

+ /** The 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)] */
+ const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
  /** The next darker colour of the palette for each number. [0] is nothing. */
  const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];

- /** A whole number scrambled into another, the same on every machine. */
- function hash(n: number): number {
-   n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
-   n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
-   return (n ^ (n >>> 16)) >>> 0;
- }
-
  /** The number at a place of the picture; outside the picture nothing is. */
  export function at(src: Frame, x: number, y: number): number {
…
  }

- /** A colour mixed with its darker neighbour, wrong: a new scrambled number for every place at every tick. */
- export function mixDarker(n: number, x: number, y: number, tick: number, share: number): number {
-   return hash(x + y * 4099 + tick * 65537) % 16 < share ? DARKER[n] : n;
+ /** A colour mixed with its darker neighbour: of the 16 pixels of every 4 by 4 block, `share` are darker.
+     The pattern is the screen's own place, never the time: a pixel turns when the share passes its number. */
+ export function mixDarker(n: number, x: number, y: number, _tick: number, share: number): number {
+   return BAYER[(y & 3) * 4 + (x & 3)] < share ? DARKER[n] : n;
  }

What the test says about the mistake

✔ in the sun nothing changes, and in full shade every pixel is its darker neighbour
✔ the darker colour is the one the ramp's table names
✖ at each of the 16 steps, exactly that many pixels of every 4 by 4 block are darker
    share 1, block at 0, 0, tick 0
    got 4, expected 1
✖ the same pixel gives the same answer at every tick
    pixel 0, 17 changed at tick 52
    got 7, expected 20
✔ as the shade deepens, a pixel that has turned dark stays dark
✔ the soft edge has 16 steps, each as many rows tall as the soft setting says
✖ moving the edge changes only the pixels whose step changed: the pattern belongs to the screen
    pixel 8, 14
    got 20, expected 7
7 tests: 4 passed, 3 failed

Tell your AI

Add dithering to my pixel art game: where a colour should be only partly darker, mix it with its darker neighbour in 16 steps by a fixed 4 by 4 pattern (ordered dithering with a 4 by 4 Bayer matrix). Never blend colours and never use random noise.

- My palette and the darker neighbour of every colour: [my table, or "make one and show it to me"].
- The pattern is these numbers, row by row: 0, 8, 2, 10 / 12, 4, 14, 6 / 3, 11, 1, 9 / 15, 7, 13, 5. A pixel reads the number at (x mod 4, y mod 4) of its place [on the screen / in the world, because my camera scrolls].
- A pixel takes the darker colour when its number is below the share, from 0 (none) to 16 (all). The share comes from [the distance to a shade's edge / a fade / a setting].
- The pattern never depends on the time, and never on the position of the thing that moves.
- Whole pixels only: nearest filtering, no smoothing.

Before you write the shader, write the same rule as a small plain function on a grid of whole numbers and test it: at each share from 0 to 16, exactly that many pixels of every 4 by 4 block are darker; the same pixel gives the same answer in every frame; a pixel that is dark at one share stays dark at every higher share. Show me the test's output. Then write the shader so that it does exactly what the function does.

The code

Three times the same rule. The first is the one we test; the two shaders are that rule in a shader language, which is what your game runs.

The rule, tested: shader.ts

TypeScript, small, and it needs nothing else. Your AI can read it and write the same rule for whatever your game is built with.

/* Dithering: two colours mixed without a third. Between a colour and its darker neighbour there is no
   colour in the palette, so the rule makes some pixels of every 4 by 4 block darker and leaves the
   others: 0 of 16 is the colour, 16 of 16 its neighbour, and the 15 steps between look like shades
   that are not there. A fixed pattern says which pixel turns first, so the mix is even and stays put.
   Here it softens the edge of a shade that moves over a sandy bank.

   The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
   palette's colours. The rule is asked for every pixel and answers with a number.

   From the course "Shaders for pixel art" on spielerando.com.
   SPDX-License-Identifier: MIT */

export interface Frame {
  width: number;
  height: number;
  index: Uint8Array;
}

export const DEFAULTS = {
  /** The row where the shade ends: above it the shade, below it the sun. */
  edge: 64,
  /** How many rows each of the 16 steps of the soft edge takes. */
  soft: 3,
};
export type Params = typeof DEFAULTS;

/** The 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The next darker colour of the palette for each number. [0] is nothing. */
const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];

/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
  if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
  return src.index[y * src.width + x];
}

/** How deep a row lies in the shade: 0 in the sun, 16 in full shade, one step every `soft` rows. */
export function depth(y: number, p: Params): number {
  const d = p.edge - y;
  if (d <= 0) return 0;
  return Math.min(16, Math.floor(d / Math.max(1, p.soft)));
}

/** A colour mixed with its darker neighbour: of the 16 pixels of every 4 by 4 block, `share` are darker.
    The pattern is the screen's own place, never the time: a pixel turns when the share passes its number. */
export function mixDarker(n: number, x: number, y: number, _tick: number, share: number): number {
  return BAYER[(y & 3) * 4 + (x & 3)] < share ? DARKER[n] : n;
}

/** The rule: every pixel is mixed with its darker neighbour as deep as its row lies in the shade. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
  return mixDarker(at(src, x, y), x, y, tick, depth(y, p));
}

The same rule as a shader: GLSL

For WebGL 2 and OpenGL ES 3.0. Only the part marked as the rule belongs to this lesson; the rest is the same in every lesson.

#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;

uniform sampler2D SRC;   // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK;        // the time in ticks, 60 a second, counted by the game

const vec3 PALETTE[26] = vec3[26](
  vec3(0.0),
  vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
  vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
  vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
  vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
  vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
  vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
  vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
  vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
  vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
  vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
  vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
  vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
  vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
  vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
  vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
  vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
  vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
  vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
  vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
  vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
  vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
  vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
  vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
  vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
  vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);

// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
  if (c.a < 0.5) return 0;
  int best = 1;
  float least = 9.0;
  for (int i = 1; i <= 25; i++) {
    vec3 d = c.rgb - PALETTE[i];
    float away = dot(d, d);
    if (away < least) {
      least = away;
      best = i;
    }
  }
  return best;
}

// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
  ivec2 size = textureSize(SRC, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(SRC, p, 0));
}

// The same for the second picture.
int otherAt(ivec2 p) {
  ivec2 size = textureSize(OTHER, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(OTHER, p, 0));
}

// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
  return a - n * int(floor(float(a) / float(n)));
}

// ---- the rule ----
uniform int edge; // the row where the shade ends: above it the shade, below it the sun
uniform int soft; // how many rows each of the 16 steps of the soft edge takes

// the 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number; [0] is nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);

// how deep a row lies in the shade: 0 in the sun, 16 in full shade, one step every `soft` rows
int depth(int y) {
  int d = edge - y;
  if (d <= 0) return 0;
  return min(16, d / max(1, soft));
}

// a colour mixed with its darker neighbour: of the 16 pixels of every 4 by 4 block, `share` are darker.
// The pattern is the screen's own place, never the time: a pixel turns when the share passes its number.
int mixDarker(int n, ivec2 p, int share) {
  return BAYER[(p.y & 3) * 4 + (p.x & 3)] < share ? DARKER[n] : n;
}

// every pixel is mixed with its darker neighbour as deep as its row lies in the shade
int shade(ivec2 p) {
  return mixDarker(at(p), p, depth(p.y));
}
// ---- end of the rule ----

out vec4 FRAGMENT;

void main() {
  // the pixel's place, counted from the top left as in the picture
  ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
  int n = shade(p);
  FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}

The same rule for Godot 4

The checked shader with Godot's names around it. We have not run this one: Godot is not part of our checks yet, so let your AI compare its picture with the one on this page.

shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
//   tick += 1
//   material.set_shader_parameter("TICK", tick)
uniform int TICK;

const vec3 PALETTE[26] = vec3[26](
  vec3(0.0),
  vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
  vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
  vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
  vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
  vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
  vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
  vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
  vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
  vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
  vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
  vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
  vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
  vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
  vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
  vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
  vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
  vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
  vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
  vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
  vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
  vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
  vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
  vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
  vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
  vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);

// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
  if (c.a < 0.5) return 0;
  int best = 1;
  float least = 9.0;
  for (int i = 1; i <= 25; i++) {
    vec3 d = c.rgb - PALETTE[i];
    float away = dot(d, d);
    if (away < least) {
      least = away;
      best = i;
    }
  }
  return best;
}

// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
  ivec2 size = textureSize(SRC, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(SRC, p, 0));
}

// The same for the second picture.
int otherAt(ivec2 p) {
  ivec2 size = textureSize(OTHER, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(OTHER, p, 0));
}

// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
  return a - n * int(floor(float(a) / float(n)));
}

// ---- the rule ----
uniform int edge; // the row where the shade ends: above it the shade, below it the sun
uniform int soft; // how many rows each of the 16 steps of the soft edge takes

// the 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number; [0] is nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);

// how deep a row lies in the shade: 0 in the sun, 16 in full shade, one step every `soft` rows
int depth(int y) {
  int d = edge - y;
  if (d <= 0) return 0;
  return min(16, d / max(1, soft));
}

// a colour mixed with its darker neighbour: of the 16 pixels of every 4 by 4 block, `share` are darker.
// The pattern is the screen's own place, never the time: a pixel turns when the share passes its number.
int mixDarker(int n, ivec2 p, int share) {
  return BAYER[(p.y & 3) * 4 + (p.x & 3)] < share ? DARKER[n] : n;
}

// every pixel is mixed with its darker neighbour as deep as its row lies in the shade
int shade(ivec2 p) {
  return mixDarker(at(p), p, depth(p.y));
}
// ---- end of the rule ----

void fragment() {
  ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
  int n = shade(p);
  COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, depth, mixDarker, shade } from "./shader.ts";

/** A tiny picture from rows of numbers: 0 is nothing, 1 to 25 are the palette's colours. */
function picture(rows: string[]): Frame {
  const cells = rows.map((r) => r.trim().split(/\s+/).map(Number));
  return { width: cells[0].length, height: cells.length, index: Uint8Array.from(cells.flat()) };
}

/** A picture of one colour: sand, 7, whose darker neighbour is wood, 20. */
function sand(width: number, height: number): Frame {
  return { width, height, index: new Uint8Array(width * height).fill(7) };
}

/** How many pixels of the 4 by 4 block at (left, top) come out darker at this share and tick. */
function darkInBlock(left: number, top: number, share: number, tick: number): number {
  let dark = 0;
  for (let y = top; y < top + 4; y++) for (let x = left; x < left + 4; x++) if (mixDarker(7, x, y, tick, share) === 20) dark++;
  return dark;
}

test("in the sun nothing changes, and in full shade every pixel is its darker neighbour", () => {
  const src = sand(8, 80);
  const p = { edge: 70, soft: 3 };
  for (let x = 0; x < 8; x++) {
    assert.equal(shade(x, 75, 0, src, p), 7, "below the edge is sun");
    assert.equal(shade(x, 2, 0, src, p), 20, "far above the edge is full shade");
  }
});

test("the darker colour is the one the ramp's table names", () => {
  // honey, coral, sky in full shade: amber, dark wood, light blue
  const src = picture(["8 10 16"]);
  assert.deepEqual([0, 1, 2].map((x) => shade(x, 0, 0, src, { edge: 60, soft: 1 })), [9, 21, 14]);
});

test("at each of the 16 steps, exactly that many pixels of every 4 by 4 block are darker", () => {
  for (let share = 0; share <= 16; share++) {
    for (const [left, top, tick] of [[0, 0, 0], [4, 8, 1], [100, 36, 7], [236, 128, 59]]) {
      assert.equal(darkInBlock(left, top, share, tick), share, `share ${share}, block at ${left}, ${top}, tick ${tick}`);
    }
  }
});

test("the same pixel gives the same answer at every tick", () => {
  const src = sand(16, 60);
  for (let y = 10; y < 60; y += 7) {
    for (let x = 0; x < 16; x++) {
      const first = shade(x, y, 0, src);
      for (let tick = 1; tick < 60; tick++) assert.equal(shade(x, y, tick, src), first, `pixel ${x}, ${y} changed at tick ${tick}`);
    }
  }
});

test("as the shade deepens, a pixel that has turned dark stays dark", () => {
  for (let y = 0; y < 4; y++) {
    for (let x = 0; x < 4; x++) {
      for (let share = 0; share < 16; share++) {
        if (mixDarker(7, x, y, 0, share) === 20) assert.equal(mixDarker(7, x, y, 0, share + 1), 20, `pixel ${x}, ${y} turned back at ${share + 1}`);
      }
    }
  }
});

test("the soft edge has 16 steps, each as many rows tall as the soft setting says", () => {
  const p = { edge: 64, soft: 3 };
  assert.equal(depth(64, p), 0, "on the edge is sun");
  assert.equal(depth(62, p), 0, "two rows above it still is");
  assert.equal(depth(61, p), 1);
  assert.equal(depth(64 - 8 * 3, p), 8);
  assert.equal(depth(64 - 16 * 3, p), 16);
  assert.equal(depth(0, p), 16, "deeper than full shade is full shade");
  assert.equal(depth(64 - 10, { edge: 64, soft: 1 }), 10);
});

test("moving the edge changes only the pixels whose step changed: the pattern belongs to the screen", () => {
  const src = sand(12, 70);
  const before = { ...DEFAULTS, edge: 60 };
  const after = { ...DEFAULTS, edge: 61 };
  for (let y = 0; y < 70; y++) {
    if (depth(y, before) !== depth(y, after)) continue;
    for (let x = 0; x < 12; x++) assert.equal(shade(x, y, 5, src, after), shade(x, y, 4, src, before), `pixel ${x}, ${y}`);
  }
});
What we checked
  • 7 tests pass on the rule.
  • With the mistake put in, 3 of them fail: "at each of the 16 steps, exactly that many pixels of every 4 by 4 block are darker", "the same pixel gives the same answer at every tick", "moving the edge changes only the pixels whose step changed: the pattern belongs to the screen".
  • The mistake shows in the picture above: switch to "With the mistake".
  • The shader ran in a browser (Chrome/158.0.8127.37) on 12 pictures and gave the rule's picture in 388,800 of 388,800 pixels.

Run by pnpm courses:check on 2026-11-27 with Node v24.15.0.

Try it

Read up

Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.

Check yourself: three questions

Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.

Question 1 of 3Easy

The share says how many of every sixteen pixels should take the darker colour. At half of them, what does each block of four by four pixels look like?

All questions of this course · The quiz and the Game Coder IQ

Next lesson Lantern Light in Pixel Art: a Shader with Round, Dithered Rings

Give the whole lesson to your AI

The button copies what to tell it together with the tested rule, its test and both shaders, so your AI has everything to build this in your game and to show you that it works.

How useful was this page?

Nobody has rated this page yet.

One click, no account. It tells us which pages to make better next.

Last updated: 2026-11-27