Spielerando

Lektion 18 von 24Wind, Wasser, Feuer und Wetter8 Min.Kostenlos

Wasserspiegelung als Shader in Pixel Art: spiegeln, dunkeln, kräuseln

Eine Wasserspiegelung zeigt, was am Ufer steht, ein zweites Mal im See: kopfüber, etwas dunkler und zitternd von den Wellen. Sie macht aus einem flachen blauen Streifen Wasser, das der Spieler glaubt. Die Regel dieser Lektion dreht das Bild an der Wasserlinie um, dunkelt es mit einer Tabelle der Palette ab und lässt es Zeile für Zeile in ganzen Pixeln kräuseln.

Ein See als Pixel Art bei Sonnenuntergang. Am Ufer stehen zwei Bienenstöcke und Schilf; die Imkerin geht daran entlang, und eine Biene fliegt vorbei. Im Wasser darunter liegt das alles kopfüber in dunkleren Farben: das Ufer, die Imkerin, die violetten Hügel, die Sonne und der rosa Himmel, und seine Zeilen kräuseln sich zur Seite.

Das ganze Bild bekommt die Regel. Über der Wasserlinie bleibt es, wie es ist; darunter zeigt jede Zeile die Zeile, die so weit über der Linie liegt wie sie darunter, eine Stufe dunkler nach der Tabelle und von der Welle zur Seite geschoben. Ab und zu liegt der See ruhig.

Auch in dieser Lektion 3 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest

Die Idee

Du triffst sie an Seen, Flüssen und Pfützen nach dem Regen. Drei Dinge lassen eine Spiegelung wie Wasser aussehen. Sie ist an der Wasserlinie umgedreht, also liegt, was am nächsten am Wasser steht, auch im Wasser am nächsten an der Linie. Sie ist dunkler als das, was sie spiegelt. Und sie zittert.

Beim Dunkler muss Pixel Art aufpassen. Du kannst eine Farbe nicht einfach dimmen: Die gedimmte Farbe ist keine der Palette. Deshalb nimmt die Regel die Tabelle DARKER aus der Lektion über Farbrampen: für jede Farbe der Palette ihre nächstdunklere. Im Bild wird das Rosa des Abendhimmels im Wasser zu Korallrot, das helle Violett zu Violett, der Honig der Bienenstöcke zu Bernstein.

Das Zittern kommt aus der Welle des Kurses, einer Liste aus sechzehn ganzen Zahlen: 0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1. Eine glatte Kurve würde eine Zeile fast bei jedem Tick um einen Teil eines Pixels verschieben, und ein Bild aus Pixeln kann das nur zeigen, indem es sie zwischen zwei Spalten verschmiert. Hier rückt jede Zeile um eine dieser Zahlen, deshalb bleiben die gespiegelten Bienenstöcke scharf. Jede Zeile Wasser ist einen Schritt weiter in der Liste als die Zeile darüber, und so laufen die Wellen gleichmäßig durch den See, bei jedem Tick und auf jedem Gerät gleich.

So funktioniert es

Die Regel sieht zuerst auf ihre eigene Zeile. Über der Wasserlinie, in diesem Bild Zeile 84, antwortet sie mit dem Bild, wie es ist: Himmel, Hügel, Ufer.

Unter der Linie zählt sie, wie tief ihre Zeile liegt. Die erste Zeile Wasser zeigt die letzte Zeile über der Linie, die zweite Zeile Wasser die darüber, und so weiter. Je tiefer die Zeile im Wasser, desto weiter oben am Ufer liest sie. Dann verschiebt sie die Spalte, die sie liest, um die Zahl der Welle beim Schritt dieser Zeile, mal die Wellen. Am linken und rechten Rand nimmt sie den Pixel des Randes selbst, statt über das Bild hinauszugreifen. Zuletzt geht sie mit der Farbe die Tabelle DARKER hinunter, einmal für jede Stufe.

Das ganze Bild bekommt die Regel, und der flache blaue See, den das Spiel gezeichnet hat, wird durch die Spiegelung ersetzt.

Der Regler Wellen geht von 0, einem stillen Spiegel, bis 2, wo eine Zeile 4 Pixel weit rücken kann. Stufen dunkler geht von 0 bis 2. Tempo sagt, wie viele Ticks ein Schritt der Welle dauert: Bei 6 braucht die Welle 96 Ticks für eine Runde.

Was schiefgeht

Um „das, was über dem Wasser ist“ zu bekommen, liest eine KI oft eine feste Zahl von Zeilen weiter oben im Bild: so weit nach oben, wie das Wasser tief ist. Das schiebt das Bild nach unten, dreht es aber nicht um. Jede Zeile Wasser liest denselben Abstand nach oben, während ein Spiegel für jede Zeile, die er tiefer geht, eine Zeile höher lesen muss.

Schalte das Bild auf „Mit dem Fehler“. Der Himmel liegt jetzt direkt unter dem Ufer, die violetten Hügel liegen tief im See, und an seinem Grund stehen die Bienenstöcke aufrecht, und die Imkerin läuft auf ihren Füßen an ihnen vorbei.

Zwei Tests fangen das, beide an einem Bild, das fünf Pixel breit ist: „the row just below the line is the row just above it, one step darker“ (die erste Zeile Wasser ist die letzte darüber, eine Stufe dunkler) und „the reflection is upside down: what stands higher on the bank lies deeper in the water“ (was am Ufer höher steht, liegt tiefer im Wasser).

Der Fehler, Zeile für Zeile

Zeilen mit - sind die Regel mit dem Fehler, Zeilen mit + die Regel, wie sie sein soll.

- /* Water reflection, with the mistake: the rule copies the picture above the water into the water
-    upright, as far down as the water is deep, instead of turning it over at the waterline. The bank
-    ends up at the bottom of the lake, the sky right under the shore, and everything in the water
-    stands on its feet.
+ /* Water reflection: below the waterline the picture shows what stands above it, upside down, a step
+    darker, and with ripples. The row just above the line is mirrored just below it, the row above
+    that one row further down, and so on: what stands higher on the bank lies deeper in the water.

+    The ripples move each row of the reflection sideways by whole pixels, by a wave in ticks. Each row
+    is one step further along the wave than the row above it, so the ripples travel through the water.
+
     From the course "Shaders for pixel art" on spielerando.com.
     SPDX-License-Identifier: MIT */
…
  }

- /** The rule, wrong: below the line the picture is copied upright, moved down by the water's depth. */
+ /** The rule: above the line the picture as it is; below it the picture turned over, rippled and darker. */
  export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
    // how many rows below the waterline this row is: 0 for the line's own row
…
    // the column to look at, kept inside the picture so that no ripple reaches past its edge
    const column = Math.min(Math.max(x - wave * p.ripple, 0), src.width - 1);
-   // wrong: read as many rows higher up as the water is deep. That is a copy moved down, not turned over
-   const row = y - (src.height - p.waterline);
+   // turned over: the row just above the line shows in the line's own row, and so on upward
+   const row = p.waterline - 1 - depth;
    let n = at(src, column, row);
    for (let i = 0; i < p.darken; i++) n = DARKER[n];

Was der Test zum Fehler sagt

✔ above the waterline the picture stays as it is
✖ the row just below the line is the row just above it, one step darker
    got [ 24, 24, 24, 24, 24 ], expected [ 3, 15, 7, 20, 3 ]
✖ the reflection is upside down: what stands higher on the bank lies deeper in the water
    got [ 24, 25, 25, 25, 24 ], expected [ 24, 2, 2, 2, 24 ]
✖ darken 0 keeps the colours, darken 2 takes them two steps down the table
    got [ 1, 1, 1, 1, 1 ], expected [ 4, 5, 6, 7, 4 ]
✔ a ripple moves a row by the wave's number times the ripple, in whole pixels
✔ each row is one step further along the wave than the row above it, so the ripples travel
✔ after 16 steps of the wave the water looks the same again
✔ at the picture's edge a ripple takes the edge's own pixel, never nothing
8 tests: 5 passed, 3 failed

Das sagst du deiner KI

Schreib einen Shader für eine Wasserspiegelung in meinem Pixel-Art-Spiel, für den [See / Fluss / die Pfütze], dessen erste Zeile Wasser Zeile [Wasserlinie] auf dem Bildschirm ist.

- Arbeite mit ganzen Pixeln und nur mit den Farben meiner Palette: kein Mischen, keine Transparenz, Textur ohne Filter (nearest).
- Über der Wasserlinie zeig das Bild, wie es ist. Darunter dreh das Bild an der Linie um: Die erste Zeile Wasser zeigt die letzte Zeile über der Linie, die zweite Zeile Wasser die vorletzte, und so weiter.
- Mach die Spiegelung [1] Stufe dunkler mit dieser Tabelle, die jeder Farbe meiner Palette ihre nächstdunklere gibt: [die Tabelle]. Multipliziere und mische keine Farben.
- Wellen: Verschieb jede Zeile der Spiegelung um eine ganze Zahl von Pixeln zur Seite, genommen aus dieser Liste aus 16 Zahlen und malgenommen mit [Wellen, zum Beispiel 1]: 0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1. Der Schritt ist die Zahl der Ticks geteilt durch [Tempo, zum Beispiel 6], plus wie viele Zeilen die Zeile unter der Linie liegt, umgebrochen auf 16.
- Nimm am linken und rechten Rand des Bildschirms den Pixel des Randes selbst, statt über ihn hinaus zu lesen.

Schreib vor dem Shader dieselbe Regel als kleine, einfache Funktion. Sie bekommt ein Raster aus ganzen Zahlen (0 = leer, 1 bis N = meine Palette), einen Platz und den Tick und gibt die Zahl für diesen einen Pixel zurück. Teste sie: Über der Linie ändert sich nichts; die erste Zeile Wasser ist die letzte Zeile über der Linie, eine Stufe dunkler; was höher steht, liegt tiefer; bei Wellen 0 liegt jede Zeile genau unter dem, was sie spiegelt. Zeig mir die Ausgabe des Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.

Der Code

Dreimal dieselbe Regel. Die erste ist die, die wir testen; die beiden Shader sind diese Regel in einer Shader-Sprache, und die läuft in deinem Spiel.

Die Regel, getestet: shader.ts

TypeScript, klein, und sie braucht nichts sonst. Deine KI kann sie lesen und dieselbe Regel für das schreiben, womit dein Spiel gebaut ist.

/* Water reflection: below the waterline the picture shows what stands above it, upside down, a step
   darker, and with ripples. The row just above the line is mirrored just below it, the row above
   that one row further down, and so on: what stands higher on the bank lies deeper in the water.

   The ripples move each row of the reflection sideways by whole pixels, by a wave in ticks. Each row
   is one step further along the wave than the row above it, so the ripples travel through the water.

   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 first row of water, counted from the top of the picture: the line the picture turns over at. */
  waterline: 84,
  /** How far the ripples move a row: the wave's number times this, in whole pixels. 0 or more. */
  ripple: 1,
  /** How many steps darker the reflection is, each step by the table DARKER. */
  darken: 1,
  /** Ticks for one step of the wave. Its 16 steps take 16 times this. */
  pace: 6,
};
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 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];
}

/** The rule: above the line the picture as it is; below it the picture turned over, rippled and darker. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
  // how many rows below the waterline this row is: 0 for the line's own row
  const depth = y - p.waterline;
  if (depth < 0) return at(src, x, y);
  // the ripple: each row is one step further along the wave than the row above it
  const wave = WAVE[(Math.floor(tick / Math.max(1, p.pace)) + depth) & 15];
  // the column to look at, kept inside the picture so that no ripple reaches past its edge
  const column = Math.min(Math.max(x - wave * p.ripple, 0), src.width - 1);
  // turned over: the row just above the line shows in the line's own row, and so on upward
  const row = p.waterline - 1 - depth;
  let n = at(src, column, row);
  for (let i = 0; i < p.darken; i++) n = DARKER[n];
  return n;
}

Dieselbe Regel als Shader: GLSL

Für WebGL 2 und OpenGL ES 3.0. Nur der als Regel markierte Teil gehört zu dieser Lektion; der Rest ist in jeder Lektion gleich.

#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 waterline; // the first row of water, counted from the top of the picture: the line the picture turns over at
uniform int ripple;    // how far the ripples move a row: the wave's number times this, in whole pixels. 0 or more.
uniform int darken;    // how many steps darker the reflection is, each step by the table DARKER
uniform int pace;      // ticks for one step of the wave. Its 16 steps take 16 times this.

// 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 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);

// above the line the picture as it is; below it the picture turned over, rippled and darker
int shade(ivec2 p) {
  // how many rows below the waterline this row is: 0 for the line's own row
  int depth = p.y - waterline;
  if (depth < 0) return at(p);
  // the ripple: each row is one step further along the wave than the row above it
  int wave = WAVE[(TICK / max(1, pace) + depth) & 15];
  // the column to look at, kept inside the picture so that no ripple reaches past its edge
  int column = min(max(p.x - wave * ripple, 0), textureSize(SRC, 0).x - 1);
  // turned over: the row just above the line shows in the line's own row, and so on upward
  int row = waterline - 1 - depth;
  int n = at(ivec2(column, row));
  for (int i = 0; i < darken; i++) n = DARKER[n];
  return 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);
}

Dieselbe Regel für Godot 4

Der geprüfte Shader mit Godots Namen darum herum. Diesen haben wir nicht ausgeführt: Godot gehört noch nicht zu unseren Prüfungen. Lass deine KI sein Bild mit dem auf dieser Seite vergleichen.

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 waterline; // the first row of water, counted from the top of the picture: the line the picture turns over at
uniform int ripple;    // how far the ripples move a row: the wave's number times this, in whole pixels. 0 or more.
uniform int darken;    // how many steps darker the reflection is, each step by the table DARKER
uniform int pace;      // ticks for one step of the wave. Its 16 steps take 16 times this.

// 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 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);

// above the line the picture as it is; below it the picture turned over, rippled and darker
int shade(ivec2 p) {
  // how many rows below the waterline this row is: 0 for the line's own row
  int depth = p.y - waterline;
  if (depth < 0) return at(p);
  // the ripple: each row is one step further along the wave than the row above it
  int wave = WAVE[(TICK / max(1, pace) + depth) & 15];
  // the column to look at, kept inside the picture so that no ripple reaches past its edge
  int column = min(max(p.x - wave * ripple, 0), textureSize(SRC, 0).x - 1);
  // turned over: the row just above the line shows in the line's own row, and so on upward
  int row = waterline - 1 - depth;
  int n = at(ivec2(column, row));
  for (int i = 0; i < darken; i++) n = DARKER[n];
  return 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);
}
Der Test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, 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 bank with sky above it: rows 0 to 3 stand above the water, rows 4 to 7 are water as drawn. */
const SHORE = picture([
  "11111",
  "12221",
  "13331",
  "45674",
  "99999",
  "99999",
  "99999",
  "99999",
]);

/** One reed in the middle column, and water below it: shows how far a ripple has moved a row. */
const REED = picture([
  "00000500000",
  "00000500000",
  "00000500000",
  "00000500000",
  "99999999999",
  "99999999999",
  "99999999999",
  "99999999999",
]);

/** The water starts at row 4; no ripples and one step darker, unless a test says otherwise. */
const CALM = { ...DEFAULTS, waterline: 4, ripple: 0, darken: 1, pace: 1 };

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

/** How far the reed has moved in a row of the water: where it is now, minus the middle. */
const moved = (y: number, tick: number, params = CALM) => row(REED, y, tick, params).findIndex((n) => n !== 0 && n !== 9) - 5;

test("above the waterline the picture stays as it is", () => {
  for (let tick = 0; tick < 16; tick++) {
    for (let y = 0; y < 4; y++) assert.deepEqual(row(SHORE, y, tick, { ...CALM, ripple: 2 }), [...SHORE.index.slice(y * 5, y * 5 + 5)], `row ${y} at tick ${tick}`);
  }
});

test("the row just below the line is the row just above it, one step darker", () => {
  // 4, 5, 6 and 7 one step down the table DARKER are 3, 15, 7 and 20
  assert.deepEqual(row(SHORE, 4, 0), [3, 15, 7, 20, 3]);
});

test("the reflection is upside down: what stands higher on the bank lies deeper in the water", () => {
  assert.deepEqual(row(SHORE, 5, 0), [24, 2, 2, 2, 24]); // the row of 3s, two rows above the line
  assert.deepEqual(row(SHORE, 6, 0), [24, 25, 25, 25, 24]); // the row of 2s
  assert.deepEqual(row(SHORE, 7, 0), [24, 24, 24, 24, 24]); // the top row, deepest in the water
});

test("darken 0 keeps the colours, darken 2 takes them two steps down the table", () => {
  assert.deepEqual(row(SHORE, 4, 0, { ...CALM, darken: 0 }), [4, 5, 6, 7, 4]);
  assert.deepEqual(row(SHORE, 4, 0, { ...CALM, darken: 2 }), [2, 16, 20, 21, 2]);
});

test("a ripple moves a row by the wave's number times the ripple, in whole pixels", () => {
  const wave = [0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1];
  for (let tick = 0; tick < 16; tick++) {
    assert.equal(moved(4, tick, { ...CALM, ripple: 1 }), wave[tick], `at tick ${tick}`);
    assert.equal(moved(4, tick, { ...CALM, ripple: 2 }), wave[tick] * 2, `at tick ${tick}`);
  }
});

test("each row is one step further along the wave than the row above it, so the ripples travel", () => {
  const wavy = { ...CALM, ripple: 1 };
  for (let tick = 0; tick < 16; tick++) {
    for (let depth = 1; depth < 4; depth++) assert.equal(moved(4 + depth, tick, wavy), moved(4, tick + depth, wavy), `depth ${depth} at tick ${tick}`);
  }
});

test("after 16 steps of the wave the water looks the same again", () => {
  const slow = { ...CALM, ripple: 2, pace: 3 };
  for (let tick = 0; tick < 48; tick++) {
    for (let y = 4; y < 8; y++) assert.deepEqual(row(SHORE, y, tick + 48, slow), row(SHORE, y, tick, slow), `row ${y} at tick ${tick}`);
  }
});

test("at the picture's edge a ripple takes the edge's own pixel, never nothing", () => {
  for (let tick = 0; tick < 16; tick++) {
    for (let y = 4; y < 8; y++) assert.ok(!row(SHORE, y, tick, { ...CALM, ripple: 2 }).includes(0), `row ${y} at tick ${tick}`);
  }
});
Was wir geprüft haben
  • 8 Tests bestehen mit der Regel.
  • Mit dem eingebauten Fehler schlagen 3 davon fehl: "the row just below the line is the row just above it, one step darker", "the reflection is upside down: what stands higher on the bank lies deeper in the water", "darken 0 keeps the colours, darken 2 takes them two steps down the table".
  • Der Fehler ist im Bild oben zu sehen: Schalte auf „Mit dem Fehler“.
  • Der Shader lief in einem Browser (Chrome/158.0.8127.37) auf 14 Bildern und ergab in 453.600 von 453.600 Pixeln das Bild der Regel.

Ausgeführt von pnpm courses:check am 2026-11-27 mit Node v24.15.0.

Probier es aus

Zum Nachlesen

Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.

Teste dich: drei Fragen

Jede Antwort wird sofort geprüft und erklärt. Die Fragen gehören zum Quiz und zählen für deinen Game Coder IQ wie jede andere.

Frage 1 von 3Leicht

Eine Spiegelung liest jede Zeile Wasser aus einer Zeile des Ufers darüber. Welche Zeile zeigt die allererste Zeile Wasser, direkt unter der Linie?

Alle Fragen dieses Kurses · Das Quiz und der Game Coder IQ

Nächste Lektion Feuer-Shader für Pixel Art: Flammen, die steigen und vergehen

Gib deiner KI die ganze Lektion

Der Knopf kopiert, was du ihr sagst, zusammen mit der getesteten Regel, ihrem Test und beiden Shadern. So hat deine KI alles, um das in dein Spiel zu bauen und dir zu zeigen, dass es funktioniert.

Wie hilfreich war diese Seite?

Diese Seite hat noch niemand bewertet.

Ein Klick, ohne Konto. So sehen wir, welche Seiten wir als Nächstes verbessern.

Stand: 2026-11-27