Spielerando

Lesson 9 of 24One sprite8 minFree

Shader Noise for Pixel Art: The Same Stars on Every Machine

Noise in a shader is a number for every pixel that looks like chance and is not: it is the pixel's own place, scrambled. This lesson's scramble uses whole numbers only, so a place gets the same number on every machine and at every run, in the game's code and in the shader. Here it puts twinkling stars into a night sky.

A pixel art night: a beekeeper stands on a hill between a dark tree and a row of beehives, with the moon to the right. Stars fill the sky, more and more of them, each twinkling on its own beat; there are none on the hills or the moon.

The whole scene is given to the rule. Each pixel of the night sky gets a scrambled number from 0 to 99, and where it is below the setting a star stands. More stars keep the ones that were there.

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

The idea

Games need things that look scattered: stars, grain on a stone wall, the tufts of grass that carry a flower, the order in which a figure crumbles away (the next lesson). Real chance is the wrong tool. A shader has no dice, and if the game rolls new ones every frame, the stars jump about; if it rolls once at the start, every loaded game has another sky.

So the rule scrambles what a pixel knows, its column, its row and a seed the game sets, into a number from 0 to 99. The same place goes in, so the same number comes out.

It has to come out the same on every machine, too. A well-known shortcut in shaders scrambles with fractions, such as the sine of a number. Graphics cards do not all work a fraction out to the same last digit, and the sine least of all, so on a player's phone such a sky can differ from the one on your computer. Whole-number sums, products and shifts come out exactly the same on every graphics card. That is why the plain function and the shader can be compared pixel for pixel, and why the game's code can work out where the shader put a star or a flower and agree with it.

How it works

The rule looks at a pixel's colour first. Only the night sky gets stars; the setting night names its colour, 24 in the palette. Everything else comes back as it came.

A pixel of sky turns its place into one whole number: the column, plus the row times 4099, plus the seed times 65537. As 4099 is wider than the picture, no two places start from the same number. The scramble shifts that number's binary digits, mixes them back in and multiplies by a big odd number, twice, and mixes once more at the end. The last two digits of the result are the place's number from 0 to 99. Where it is below the slider "stars", a star stands: at 2, on about two places in a hundred.

Every star twinkles. Every "pace" ticks, 30 at the start, so twice a second, it picks how bright it is: grey violet, light slate or white, by scrambling its number together with the count of beats. Another part of its scrambled number sets when its beat falls, so the stars do not all change at once. A star never moves.

The slider "seed" picks the sky: another seed, other places; the same seed, the same sky, star for star. Raising "stars" keeps the old stars, because a number below 2 is also below 3.

What goes wrong

Ask an AI for a scramble in whole numbers and it often writes one multiplication by a big odd number. It looks the part: the number is huge, and the first few results seem to jump about. But a multiplication keeps the order of the places. The place to the right gets this number plus the same big step, the next place again, and because the number wraps around at its top, the step comes in only two sizes. Cut down to the last two digits, those even steps line up across the screen.

Switch the picture to "With the mistake". The stars stand in slanted rows at even distances, as if someone had combed the sky. Only a whole picture shows it, so an AI that checks a handful of numbers does not see it.

Two tests catch it. One counts the different steps between a place's number and its neighbour's: many with the real scramble, and on the mistake "only 2 different steps to the neighbour on the right". The other compares a few places with numbers written down once from the real scramble; the mistake gives 73 where 21 belongs.

The mistake, line by line

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

- /* Noise you can trust, with the mistake: the scramble is one multiplication by a big odd number, the
-    shortcut an AI often writes when it is asked for a scramble in whole numbers. A multiplication keeps
-    the order of the places: the place to the right gets this place's number plus one of two steps,
-    again and again, so the stars line up in slanted stripes instead of lying anywhere.
+ /* Noise you can trust: every place of the picture gets a number from 0 to 99 by scrambling the
+    place's own numbers, its column and its row. The scramble is made of whole-number steps only, so it
+    gives the same number for the same place on every machine and at every run, in the game's code and
+    in the shader alike. Here it puts stars into a night sky: a place of the sky whose number is below
+    `stars` holds a star, and the same scramble, fed the time in whole steps, lets each star twinkle on
+    a beat of its own.

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

- /** A whole number "scrambled", wrong: one multiplication by a big odd number, and nothing that mixes. */
+ /** A whole number scrambled into another, the same on every machine. */
  function hash(n: number): number {
-   return Math.imul(n, 0x9e3779b1) >>> 0;
+   n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
+   n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
+   return (n ^ (n >>> 16)) >>> 0;
  }

What the test says about the mistake

✔ every place gets a number from 0 to 99, and each tenth of them comes about as often
✖ the scramble gives the numbers written down here, on every machine
    got 73, expected 21
✖ the step from a place to its neighbour comes in many sizes, so no stripes form
    only 2 different steps to the neighbour on the right
✔ a place of the sky is a star exactly when its number is below the setting
✔ the same seed gives the same sky every time it is asked, another seed another sky
✔ a star stays in its place: the ticks change only how bright it is
✔ only the sky gets stars: everything else stays as it came
7 tests: 5 passed, 2 failed

Tell your AI

Write a shader that puts twinkling stars into the night sky of my game, with a scramble I can trust.

- Whole pixels and whole numbers only: no fractions, no sine, no random numbers. The time is a whole number of ticks that my game counts, 60 a second.
- Use exactly this scramble, in the game's code and in the shader: hash(n): n = (n xor (n >> 16)) * 0x7feb352d; n = (n xor (n >> 15)) * 0x846ca68b; return n xor (n >> 16). Every step works on unsigned 32-bit whole numbers that wrap around; where my game's language has none, keep the lowest 32 bits after each step.
- A place's number from 0 to 99 is hash(x + y * 4099 + seed * 65537) % 100. A pixel of my night sky's colour [the colour] holds a star where that number is below [stars in a hundred places, for example 2]. Every other pixel stays as it is.
- Every [ticks, for example 30] ticks each star picks how bright it is, at a moment of its own, by scrambling its number again together with the count of beats. Stars never move.

Before the shader, write the same rule as a small plain function and test it: hash(1) is 1753845952, hash(2) is 3507691905 and hash(65537) is 2560066121; the steps between the numbers of neighbouring places come in many sizes, not one or two; the same seed gives the same sky every time. Show me the test's output. Then write the shader so that it does exactly what the function does, draw one still picture with both, and tell me how many pixels differ.

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.

/* Noise you can trust: every place of the picture gets a number from 0 to 99 by scrambling the
   place's own numbers, its column and its row. The scramble is made of whole-number steps only, so it
   gives the same number for the same place on every machine and at every run, in the game's code and
   in the shader alike. Here it puts stars into a night sky: a place of the sky whose number is below
   `stars` holds a star, and the same scramble, fed the time in whole steps, lets each star twinkle on
   a beat of its own.

   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 many places of the sky in a hundred hold a star. */
  stars: 2,
  /** The ticks between two twinkles of one star: 60 is once a second. */
  pace: 30,
  /** Which sky: another number puts the stars in other places, and always in the same ones for that number. */
  seed: 1,
  /** The number of the night sky's colour in the palette: stars come only where it is. 24 is the night. */
  night: 24,
};
export type Params = typeof DEFAULTS;

/** 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];
}

/** 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 from 0 to 99 for a place and a seed. */
export function noise(x: number, y: number, seed: number): number {
  return hash(x + y * 4099 + seed * 65537) % 100;
}

/** A star's colours from dim to bright: grey violet, light slate, white, white. */
const TWINKLE = [3, 4, 5, 5];

/** The rule: a place of the night sky whose number is below `stars` is a star; everything else stays as it came. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
  const here = at(src, x, y);
  if (here !== p.night) return here; // only the sky gets stars
  const place = hash(x + y * 4099 + p.seed * 65537); // the place, scrambled: noise() is its last two digits
  if (place % 100 >= p.stars) return here; // most places hold no star
  // every star twinkles on its own beat: another part of its scrambled number says when its beat starts
  const beat = Math.floor((tick + ((place >>> 8) % p.pace)) / p.pace);
  // the star's number and the beat's number, scrambled once more, pick how bright it is now
  return TWINKLE[hash(place + beat) % 4];
}

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 stars; // how many places of the sky in a hundred hold a star
uniform int pace;  // the ticks between two twinkles of one star: 60 is once a second
uniform int seed;  // which sky: another number puts the stars in other places, and always in the same ones for that number
uniform int night; // the number of the night sky's colour in the palette: stars come only where it is. 24 is the night

// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
  n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
  return n;
}

// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
  return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}

// a star's colours from dim to bright: grey violet, light slate, white, white
const int TWINKLE[4] = int[4](3, 4, 5, 5);

// a place of the night sky whose number is below stars is a star; everything else stays as it came
int shade(ivec2 p) {
  int here = at(p);
  if (here != night) return here; // only the sky gets stars
  uint place = hash(uint(p.x) + uint(p.y) * 4099u + uint(seed) * 65537u); // the place, scrambled: noise() is its last two digits
  if (int(place % 100u) >= stars) return here; // most places hold no star
  // every star twinkles on its own beat: another part of its scrambled number says when its beat starts
  int beat = (TICK + int((place >> 8u) % uint(pace))) / pace;
  // the star's number and the beat's number, scrambled once more, pick how bright it is now
  return TWINKLE[int(hash(place + uint(beat)) % 4u)];
}
// ---- 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 stars; // how many places of the sky in a hundred hold a star
uniform int pace;  // the ticks between two twinkles of one star: 60 is once a second
uniform int seed;  // which sky: another number puts the stars in other places, and always in the same ones for that number
uniform int night; // the number of the night sky's colour in the palette: stars come only where it is. 24 is the night

// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
  n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
  return n;
}

// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
  return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}

// a star's colours from dim to bright: grey violet, light slate, white, white
const int TWINKLE[4] = int[4](3, 4, 5, 5);

// a place of the night sky whose number is below stars is a star; everything else stays as it came
int shade(ivec2 p) {
  int here = at(p);
  if (here != night) return here; // only the sky gets stars
  uint place = hash(uint(p.x) + uint(p.y) * 4099u + uint(seed) * 65537u); // the place, scrambled: noise() is its last two digits
  if (int(place % 100u) >= stars) return here; // most places hold no star
  // every star twinkles on its own beat: another part of its scrambled number says when its beat starts
  int beat = (TICK + int((place >> 8u) % uint(pace))) / pace;
  // the star's number and the beat's number, scrambled once more, pick how bright it is now
  return TWINKLE[int(hash(place + uint(beat)) % 4u)];
}
// ---- 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, noise, shade } from "./shader.ts";

/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
  return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}

/** A night sky of w by h pixels: every pixel is 24, the night's colour. */
function sky(w: number, h: number): Frame {
  return { width: w, height: h, index: new Uint8Array(w * h).fill(24) };
}

function whole(src: Frame, tick = 0, params = DEFAULTS): number[] {
  const out: number[] = [];
  for (let y = 0; y < src.height; y++) for (let x = 0; x < src.width; x++) out.push(shade(x, y, tick, src, params));
  return out;
}

/** The places of a picture that the rule made into stars. */
const starsIn = (answers: number[]) => answers.flatMap((n, i) => (n !== 24 ? [i] : []));

test("every place gets a number from 0 to 99, and each tenth of them comes about as often", () => {
  const tenths = new Array(10).fill(0);
  for (let y = 0; y < 100; y++) {
    for (let x = 0; x < 100; x++) {
      const n = noise(x, y, 1);
      assert.ok(Number.isInteger(n) && n >= 0 && n <= 99, `noise(${x}, ${y}, 1) is ${n}`);
      tenths[Math.floor(n / 10)]++;
    }
  }
  // 10 000 places: a tenth of them is 1000, and none may be off by more than a tenth of that
  for (const count of tenths) assert.ok(count >= 900 && count <= 1100, `the tenths came ${tenths.join(", ")} times`);
});

test("the scramble gives the numbers written down here, on every machine", () => {
  assert.equal(noise(0, 0, 1), 21);
  assert.equal(noise(1, 0, 1), 83);
  assert.equal(noise(0, 1, 1), 31);
  assert.equal(noise(2, 3, 1), 75);
  assert.equal(noise(239, 134, 1), 33);
  assert.equal(noise(10, 20, 7), 12);
});

test("the step from a place to its neighbour comes in many sizes, so no stripes form", () => {
  const right = new Set<number>();
  const down = new Set<number>();
  for (let y = 0; y < 64; y++) {
    for (let x = 0; x < 64; x++) {
      right.add((noise(x + 1, y, 1) - noise(x, y, 1) + 100) % 100);
      down.add((noise(x, y + 1, 1) - noise(x, y, 1) + 100) % 100);
    }
  }
  assert.ok(right.size >= 50, `only ${right.size} different steps to the neighbour on the right`);
  assert.ok(down.size >= 50, `only ${down.size} different steps to the neighbour below`);
});

test("a place of the sky is a star exactly when its number is below the setting", () => {
  const night = sky(64, 64);
  for (const stars of [0, 3, 10, 50, 100]) {
    const answers = whole(night, 0, { ...DEFAULTS, stars });
    for (let i = 0; i < answers.length; i++) {
      const x = i % 64;
      const y = Math.floor(i / 64);
      assert.equal(answers[i] !== 24, noise(x, y, DEFAULTS.seed) < stars, `${x}, ${y} with stars ${stars}`);
    }
  }
});

test("the same seed gives the same sky every time it is asked, another seed another sky", () => {
  const night = sky(64, 64);
  const once = whole(night, 0, { ...DEFAULTS, seed: 1 });
  const again = whole(night, 0, { ...DEFAULTS, seed: 1 });
  const other = whole(night, 0, { ...DEFAULTS, seed: 2 });
  assert.deepEqual(again, once);
  assert.notDeepEqual(starsIn(other), starsIn(once));
});

test("a star stays in its place: the ticks change only how bright it is", () => {
  const night = sky(64, 64);
  const first = whole(night, 0);
  const places = starsIn(first);
  assert.ok(places.length > 0, "the sky has no star at all");
  let twinkled = 0;
  for (let tick = 1; tick <= 300; tick += 7) {
    const now = whole(night, tick);
    assert.deepEqual(starsIn(now), places, `at tick ${tick} the stars are in other places`);
    for (const i of places) if (now[i] !== first[i]) twinkled++;
  }
  assert.ok(twinkled > 0, "no star ever changed how bright it is");
});

test("only the sky gets stars: everything else stays as it came", () => {
  const hill = picture([
    "1111",
    "1191",
    "2662",
    "7777",
  ]);
  // with 1 as the sky's colour and every place of it a star, only the 1s change, and only into star colours
  const answers = whole(hill, 0, { ...DEFAULTS, night: 1, stars: 100 });
  hill.index.forEach((n, i) => {
    if (n === 1) assert.ok([3, 4, 5].includes(answers[i]), `the sky at ${i % 4}, ${Math.floor(i / 4)} became ${answers[i]}`);
    else assert.equal(answers[i], n);
  });
});
What we checked
  • 7 tests pass on the rule.
  • With the mistake put in, 2 of them fail: "the scramble gives the numbers written down here, on every machine", "the step from a place to its neighbour comes in many sizes, so no stripes form".
  • The mistake shows in the picture above: switch to "With the mistake".
  • The shader ran in a browser (Chrome/158.0.8127.37) on 14 pictures and gave the rule's picture in 453,600 of 453,600 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

This lesson puts stars into a night sky with noise. Why does a star stay in its place from frame to frame, and come back the same after the game is loaded again?

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

Next lesson Dissolve Shader for Pixel Art: Crumbling Without Flicker

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