Spielerando

Lesson 16 of 24Light with few colours8 minFree

Fog in a Pixel Art Shader That Leaves the Figure Clear

Fog gives a flat picture depth: the far ridges fade into pale mist while what is near stays sharp. In pixel art the mist is not blended in. It is dithered in with the 4 by 4 pattern of the dithering lesson, a little more every few rows into the distance, and it drifts. What stands in front must stay clear, and for that the rule needs a second picture.

A valley of pines at dawn in pixel art, ridge behind ridge under a violet and pink sky, with five hives on a terrace. Close to us an old tree holds a wild bees' nest on a branch, and the beekeeper climbs a ladder up to it. Pale mist lies over the far ridges, thicker the further away, and drifts through in banks; the tree, the ladder and the beekeeper stay sharp.

The whole valley is given to the rule, and a second picture holds what stands in front. From the meadow upward the mist thickens one step every few rows and drifts to the right; on the tree, the ladder and the beekeeper the rule answers with the picture as it came, however high she climbs.

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

The idea

In a game seen from the side, fog is how you show that the hills behind are far away: each ridge a little paler than the one in front of it, until the last one is almost sky. It sets a mood as well, a valley at dawn, a swamp, a mountain pass, and it costs no new colour. The mist is one colour of the palette, mixed in pixel by pixel.

In such a picture, what is further away stands higher: the far ridges show above the near ones. So the row can stand for the distance, and the rule counts it from the front of the scene upward. That works for the land behind, but not for what stands close to us and reaches up, a tree nearby or the beekeeper on a ladder. They are near, however high they reach. A second picture holds them, and there the fog does not go.

In the picture a valley of pines with a row of hives lies in the morning mist. Close to us an old tree holds a wild bees' nest, and the beekeeper climbs a ladder up to it.

How it works

For every pixel the rule looks at the second picture first. If something is drawn there, it stands in front, and the rule answers with the pixel as it came. Otherwise it works out how much fog there is. Below the front row, row 118 here, the top of the meadow, there is none. From there upward the fog grows by one step every few rows, up to its thickest.

Banks of mist drift through: the course's wave table adds up to 4 steps or takes them away, laid along the row at 8 pixels a step, and moves one pixel to the right every 3 ticks. Far away, where the fog is already at its thickest, the banks do not show, so the distance is an even haze.

The steps of fog then pick the mist pixels the way the dithering lesson picks dark ones: where the pattern's number is below the fog, the pixel is mist, here the palest blue.

The sliders: "fog at its thickest" goes from 0 to 16 and starts at 12. "Rows for each step of fog" goes from 1 to 8 and starts at 5.

In a game, the second picture is the near things drawn once more on their own, into a picture as big as the screen: the player, the trees of the front layer. Most engines can draw a layer into a picture of its own; that is all the rule needs.

What goes wrong

Asked for fog that "gets thicker with distance" in a 2D game, an AI takes the height on the screen as the distance, which is right for the land, and stops there. The figure is just more pixels in the same picture, so she is fogged by her row like everything around her. Standing on the meadow she is barely touched, because the fog is thinnest just above it. Once she climbs or jumps, she fades as if she had walked off into the hills.

Switch the picture to "With the mistake". While she climbs, she turns to mist, the ladder fades, and the crown of the near tree turns pale and patchy. Two tests catch it: "what stands in front stays clear, however thick the fog behind it", and "a pixel in front looks the same at every tick while the mist drifts behind it". The second saw her shoulder turn to mist at tick 204, when a bank of mist came by.

The mistake, line by line

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

- /* Fog, with the mistake: the rule never looks at the second picture, the one that says what stands in
-    front. It fogs every pixel by its row alone, so the beekeeper on the ladder fades into the mist as
-    she climbs, as if she stood as far away as the hills behind her, and the near tree goes grey too.
+ /* Fog: mist that thickens with distance and drifts, mixed into the picture pixel by pixel with the
+    4 by 4 pattern, and kept off what stands in front. In a picture seen from the side, what is further
+    away stands higher: the far hills show above the near ones. So the rule counts the rows from the
+    front upward, one step of fog every few rows, up to its thickest, and lets banks of mist drift
+    through by the wave table. A second picture says what stands in front (the beekeeper, the near
+    tree); there the rule leaves the picture as it is, however thick the fog behind.

+    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 */
…
  }

- /** The rule, wrong: every pixel is fogged by its row, also what stands in front. */
- export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS, _other?: Frame): number {
+ /** The rule: what stands in front stays as it is; behind it the mist is mixed in by the pattern. */
+ export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS, other?: Frame): number {
    const n = at(src, x, y);
+   // the second picture holds what stands in front: it is not behind the fog
+   if (other && at(other, x, y) !== 0) return n;
    return BAYER[(y & 3) * 4 + (x & 3)] < fog(x, y, tick, p) ? p.mist : n;
  }

What the test says about the mistake

✔ below the front row there is no fog
✔ the fog thickens one step every few rows above the front, as rise says
✔ far away the fog stops growing at its thickest, and is even there while banks pass
✔ the mist drifts one pixel to the right every few ticks, as pace says, and not in between
✔ behind, the mist is mixed in by the 4 by 4 pattern: at 8 steps, 8 of every 16 pixels are mist
✖ what stands in front stays clear, however thick the fog behind it
    pixel 3, 1
    got 15, expected 14
✖ a pixel in front looks the same at every tick while the mist drifts behind it
    tick 204
    got 15, expected 14
✔ every answer is the picture's own colour or the mist
8 tests: 6 passed, 2 failed

Tell your AI

Add fog to my 2D pixel art game as a shader: pale mist that gets thicker with distance, drifts slowly, and never covers what stands in front.

- Distance: [how my game knows it: the row on the screen, higher meaning further away, or the layer each thing is drawn on]. The fog begins at [the front row or layer] and grows one step every [few] rows, up to [its thickest, from 0 to 16].
- Mix the mist in with the 4 by 4 pattern of ordered dithering (0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5), read at (x mod 4, y mod 4): a pixel becomes the mist colour [a colour of my palette] where the pattern's number is below the fog's step. No blending and no transparency.
- What stands in front ([the player, the trees of the front layer]) is drawn once more into a second picture of the same size. Where that picture has anything, the shader answers with the pixel as it came.
- Banks of mist: a wave of whole numbers that adds or takes a few steps and moves one pixel every [few] frames. Where the fog is at its thickest, keep it even.
- Whole pixels only: nearest filtering, no smoothing.

Before you write the shader, write the same rule as a small plain function on grids of whole numbers (the picture, and the picture of what stands in front) and test it: below the front there is no fog; every pixel of what stands in front comes out as it came, in every frame; at 8 steps, 8 of every 16 pixels behind are mist. 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.

/* Fog: mist that thickens with distance and drifts, mixed into the picture pixel by pixel with the
   4 by 4 pattern, and kept off what stands in front. In a picture seen from the side, what is further
   away stands higher: the far hills show above the near ones. So the rule counts the rows from the
   front upward, one step of fog every few rows, up to its thickest, and lets banks of mist drift
   through by the wave table. A second picture says what stands in front (the beekeeper, the near
   tree); there the rule leaves the picture as it is, however thick the fog behind.

   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 = {
  /** How thick the fog gets far away: 0 is none, 16 is mist only. */
  thick: 12,
  /** How many rows each step of fog takes, counted up from the front row. */
  rise: 5,
  /** The row where the fog begins: the front of the scene. Below it there is none. */
  front: 118,
  /** The mist's colour, by its number in the palette: 15 is the palest blue. */
  mist: 15,
  /** How many ticks the mist takes to drift one pixel. */
  pace: 3,
};
export type Params = typeof DEFAULTS;

/** A wave in whole pixels, 16 steps long. */
const WAVE = [0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1];
/** 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];

const wrap = (a: number, n: number) => a - n * Math.floor(a / n);

/** 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 much fog is at a place: 0 to 16 of every 16 pixels are mist. */
export function fog(x: number, y: number, tick: number, p: Params): number {
  const d = p.front - y;
  if (d <= 0) return 0;
  // banks of mist: the wave laid along the row, 8 pixels a step, moved right one pixel every `pace` ticks.
  // It adds or takes up to 4 steps; far away, where the fog is at its thickest, it is even.
  const drift = WAVE[wrap(x - Math.floor(tick / Math.max(1, p.pace)), 128) >> 3];
  return Math.max(0, Math.min(p.thick, Math.floor(d / Math.max(1, p.rise)) + 2 * drift));
}

/** The rule: what stands in front stays as it is; behind it the mist is mixed in by the pattern. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS, other?: Frame): number {
  const n = at(src, x, y);
  // the second picture holds what stands in front: it is not behind the fog
  if (other && at(other, x, y) !== 0) return n;
  return BAYER[(y & 3) * 4 + (x & 3)] < fog(x, y, tick, p) ? p.mist : n;
}

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 thick; // how thick the fog gets far away: 0 is none, 16 is mist only
uniform int rise;  // how many rows each step of fog takes, counted up from the front row
uniform int front; // the row where the fog begins: the front of the scene. Below it there is none
uniform int mist;  // the mist's colour, by its number in the palette: 15 is the palest blue
uniform int pace;  // how many ticks the mist takes to drift one pixel

// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// 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);

// how much fog is at a place: 0 to 16 of every 16 pixels are mist
int fog(ivec2 p) {
  int d = front - p.y;
  if (d <= 0) return 0;
  // banks of mist: the wave laid along the row, 8 pixels a step, moved right one pixel every `pace` ticks.
  // It adds or takes up to 4 steps; far away, where the fog is at its thickest, it is even.
  int drift = WAVE[wrap(p.x - TICK / max(1, pace), 128) >> 3];
  return max(0, min(thick, d / max(1, rise) + 2 * drift));
}

// what stands in front stays as it is; behind it the mist is mixed in by the pattern
int shade(ivec2 p) {
  int n = at(p);
  // the second picture holds what stands in front: it is not behind the fog
  if (otherAt(p) != 0) return n;
  return BAYER[(p.y & 3) * 4 + (p.x & 3)] < fog(p) ? mist : n;
}
// ---- 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 thick; // how thick the fog gets far away: 0 is none, 16 is mist only
uniform int rise;  // how many rows each step of fog takes, counted up from the front row
uniform int front; // the row where the fog begins: the front of the scene. Below it there is none
uniform int mist;  // the mist's colour, by its number in the palette: 15 is the palest blue
uniform int pace;  // how many ticks the mist takes to drift one pixel

// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// 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);

// how much fog is at a place: 0 to 16 of every 16 pixels are mist
int fog(ivec2 p) {
  int d = front - p.y;
  if (d <= 0) return 0;
  // banks of mist: the wave laid along the row, 8 pixels a step, moved right one pixel every `pace` ticks.
  // It adds or takes up to 4 steps; far away, where the fog is at its thickest, it is even.
  int drift = WAVE[wrap(p.x - TICK / max(1, pace), 128) >> 3];
  return max(0, min(thick, d / max(1, rise) + 2 * drift));
}

// what stands in front stays as it is; behind it the mist is mixed in by the pattern
int shade(ivec2 p) {
  int n = at(p);
  // the second picture holds what stands in front: it is not behind the fog
  if (otherAt(p) != 0) return n;
  return BAYER[(p.y & 3) * 4 + (p.x & 3)] < fog(p) ? mist : n;
}
// ---- 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, fog, 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()) };
}

// At tick 0 the columns 0 to 7 lie where the wave is 0: no bank of mist adds to the fog or takes from it.

/** A far hillside of dark wood, 20, and a beekeeper of light blue, 14, in front of it. */
const HILL = picture([
  "20 20 20 20 20 20 20 20",
  "20 20 20 14 14 20 20 20",
  "20 20 14 14 14 14 20 20",
  "20 20 20 14 14 20 20 20",
  "20 20 20 14 14 20 20 20",
  "20 20 20 14 14 20 20 20",
]);
/** The second picture: what stands in front, the beekeeper alone. */
const FRONT = picture([
  "0 0 0 0 0 0 0 0",
  "0 0 0 14 14 0 0 0",
  "0 0 14 14 14 14 0 0",
  "0 0 0 14 14 0 0 0",
  "0 0 0 14 14 0 0 0",
  "0 0 0 14 14 0 0 0",
]);

test("below the front row there is no fog", () => {
  for (const tick of [0, 50, 200]) {
    for (let x = 0; x < 128; x++) {
      assert.equal(fog(x, DEFAULTS.front, tick, DEFAULTS), 0);
      assert.equal(fog(x, DEFAULTS.front + 9, tick, DEFAULTS), 0);
    }
  }
});

test("the fog thickens one step every few rows above the front, as rise says", () => {
  const p = { ...DEFAULTS, thick: 16, rise: 4, front: 100 };
  assert.equal(fog(0, 97, 0, p), 0, "three rows up is not yet a step");
  for (let step = 1; step <= 16; step++) assert.equal(fog(0, 100 - 4 * step, 0, p), step);
});

test("far away the fog stops growing at its thickest, and is even there while banks pass", () => {
  const p = { ...DEFAULTS, thick: 12, rise: 4, front: 100 };
  assert.equal(fog(0, 52, 0, p), 12);
  for (const tick of [0, 90, 200]) for (let x = 0; x < 128; x++) assert.equal(fog(x, 0, tick, p), 12, `${x} at tick ${tick}`);
});

test("the mist drifts one pixel to the right every few ticks, as pace says, and not in between", () => {
  const p = { ...DEFAULTS, pace: 3 };
  for (let x = 0; x < 256; x++) {
    for (const y of [10, 50, 90]) {
      assert.equal(fog(x + 1, y, 3, p), fog(x, y, 0, p), `${x}, ${y}`);
      assert.equal(fog(x, y, 2, p), fog(x, y, 0, p), `${x}, ${y} moved before its time`);
    }
  }
});

test("behind, the mist is mixed in by the 4 by 4 pattern: at 8 steps, 8 of every 16 pixels are mist", () => {
  const src = picture(["20 20 20 20", "20 20 20 20", "20 20 20 20", "20 20 20 20"]);
  const p = { ...DEFAULTS, thick: 8, front: 100 };
  let mist = 0;
  for (let y = 0; y < 4; y++) for (let x = 0; x < 4; x++) if (shade(x, y, 0, src, p) === p.mist) mist++;
  assert.equal(mist, 8);
});

test("what stands in front stays clear, however thick the fog behind it", () => {
  const p = { ...DEFAULTS, thick: 16, front: 100 };
  for (let y = 0; y < HILL.height; y++) {
    for (let x = 0; x < HILL.width; x++) {
      const want = FRONT.index[y * 8 + x] !== 0 ? HILL.index[y * 8 + x] : p.mist;
      assert.equal(shade(x, y, 0, HILL, p, FRONT), want, `pixel ${x}, ${y}`);
    }
  }
});

test("a pixel in front looks the same at every tick while the mist drifts behind it", () => {
  // the beekeeper's shoulder at (3, 2), 32 rows above the front: 8 steps of fog, and banks of mist pass by
  const p = { ...DEFAULTS, thick: 16, rise: 4, front: 34 };
  for (let tick = 0; tick < 400; tick++) assert.equal(shade(3, 2, tick, HILL, p, FRONT), 14, `tick ${tick}`);
});

test("every answer is the picture's own colour or the mist", () => {
  for (const thick of [0, 6, 16]) {
    for (let tick = 0; tick < 300; tick += 37) {
      for (let y = 0; y < HILL.height; y++) {
        for (let x = 0; x < HILL.width; x++) {
          const n = shade(x, y, tick, HILL, { ...DEFAULTS, thick, front: 40 }, FRONT);
          assert.ok(n === HILL.index[y * 8 + x] || n === DEFAULTS.mist, `${n} at ${x}, ${y}`);
        }
      }
    }
  }
});
What we checked
  • 8 tests pass on the rule.
  • With the mistake put in, 2 of them fail: "what stands in front stays clear, however thick the fog behind it", "a pixel in front looks the same at every tick while the mist drifts behind it".
  • 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 valley is seen from the side. How does the fog rule tell how far away a pixel of the land behind is?

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

Next lesson Wind Sway Shader for Pixel Art: Grass and Trees That Bend

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