Water Reflection Shader in Pixel Art: Mirror, Darken, Ripple
A water reflection shows what stands on the bank a second time in the lake, upside down, a little darker and wavering with the ripples. It turns a flat blue strip into water the player believes in. This lesson's rule turns the picture over at the waterline, darkens it with a table of the palette, and ripples it row by row in whole pixels.

The whole picture is given to the rule. Above the waterline it stays as it is; below it, each row shows the row as far above the line as it lies below, one step darker by the table, and moved sideways by the wave. Now and then the lake lies calm.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
You meet it at lakes, rivers and puddles after rain. Three things make a reflection read as water. It is turned over at the waterline, so what stands nearest the water lies nearest the line in the water, too. It is darker than what it mirrors. And it wavers.
Darker is where pixel art needs care. You cannot simply dim a colour: the dimmed colour is not one of the palette's. So the rule uses the table DARKER from the lesson on colour ramps: for each colour of the palette, its next darker one. In the picture the pink of the evening sky becomes coral in the water, the light violet becomes violet, the honey of the hives becomes amber.
The waver comes from the course's wave, a list of sixteen whole numbers: 0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1. A smooth curve would move a row by part of a pixel at almost every tick, and a picture of pixels can only show that by smearing it between two columns. Here each row is moved by one of these numbers, so the mirrored hives stay crisp. Each row of water is one step further along the list than the row above it, and so the ripples run through the lake at an even pace, the same at every tick on every machine.
How it works
The rule looks at its own row first. Above the waterline, row 84 in this picture, it answers with the picture as it is: the sky, the hills, the bank.
Below the line it counts how deep its row is. The first row of water shows the last row above the line, the second row of water the one above that, and so on. The deeper the row in the water, the higher up on the bank it reads. Then it moves the column it reads by the wave's number at this row's step, times the ripple. Near the left and right edge it takes the edge's own pixel rather than reach past the picture. Last it takes the colour down the table DARKER, once for each step of darkening.
The whole picture is given to the rule, and the flat blue lake the game drew is replaced by the reflection.
The slider ripples goes from 0, a still mirror, to 2, where a row can move 4 pixels. Steps darker goes from 0 to 2. Pace is how many ticks one step of the wave lasts: at 6 the wave takes 96 ticks to come round.
What goes wrong
To get "what is above the water", an AI often reads the picture a fixed number of rows higher up: as far up as the water is deep. That moves the picture down but does not turn it over. Every row of water reads the same distance up, while a mirror has to read one row higher for every row it goes down.
Switch the picture to "With the mistake". The sky now lies right under the bank, the violet hills lie low in the lake, and at its bottom the hives stand upright, with the beekeeper walking past them on her feet.
Two tests catch it: "the row just below the line is the row just above it, one step darker" and "the reflection is upside down: what stands higher on the bank lies deeper in the water", both on a picture five pixels wide.
The mistake, line by line
- /* 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];What the test says about the mistake
✔ 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 failedTell your AI
Write a water reflection shader for my pixel art game, for the [lake / river / puddle] whose first row of water is row [waterline] on the screen.
- Work on whole pixels and only the colours of my palette: no blending, no transparency, nearest filtering.
- Above the waterline, show the picture as it is. Below it, turn the picture over at the line: the first row of water shows the last row above the line, the second row of water the second-to-last, and so on.
- Make the reflection [1] step darker with this table, which gives each colour of my palette its next darker colour: [the table]. Do not multiply or blend colours.
- Ripples: move each row of the reflection sideways by a whole number of pixels, taken from this list of 16 and multiplied by [ripple, for example 1]: 0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1. The step is the tick count divided by [pace, for example 6], plus how many rows below the line the row is, wrapped to 16.
- Near the left and right edge of the screen, take the edge's own pixel instead of reading past it.
Before the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = my palette), a place and the tick, and returns the number for that one pixel. Test it: above the line nothing changes; the first row of water is the last row above the line, one step darker; what stands higher lies deeper; with ripple 0 every row lines up with what it mirrors. 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
/* 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;
}
The same rule as a shader: GLSL
#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);
}
The same rule for Godot 4
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);
}
The 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}`);
}
});
- 8 tests pass on the rule.
- With the mistake put in, 3 of them fail: "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".
- 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.
Try it
- Pull ripples to 0. The lake lies still, and the hives, the reeds and the beekeeper hang upside down exactly below themselves, column for column. At 2 the reflection breaks into rows that waver four pixels each way.
- Pull steps darker to 0: the reflection is as bright as the bank and looks like a second world hanging under the real one. At 2 it sinks into late dusk.
- Switch between "With the rule" and "With the mistake" while she walks: with the mistake an upright beekeeper walks along the bottom of the lake.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Darker and Lighter in Pixel Art: Colour Ramps in a Shader
How the table of darker colours that this reflection steps down is made for your own palette, which the text to paste asks for, and the two checks that keep such a table honest.
The Order of Shader Effects in a Pixel Art Game
The reflection together with a lantern and the night in one rule: why the light has to be worked out for the place a pixel of water mirrors, or the lantern's reflection lies in the dark.
How a Godot 4 shader reads what is already on the screen, as a reflection must: the screen texture with nearest filtering, and why two such effects that overlap need a copy in between.
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.
A reflection reads each row of water from a row of the bank above it. Which row does the very first row of water, right under the line, show?
Next lesson A Pixel Art Fire Shader: Flames That Rise and Thin Out
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.
This is a lesson from the course "Shaders for Pixel Art" on spielerando.com: https://spielerando.com/en/courses/pixel-shaders/water-reflection
Write a water reflection shader for my pixel art game, for the [lake / river / puddle] whose first row of water is row [waterline] on the screen.
- Work on whole pixels and only the colours of my palette: no blending, no transparency, nearest filtering.
- Above the waterline, show the picture as it is. Below it, turn the picture over at the line: the first row of water shows the last row above the line, the second row of water the second-to-last, and so on.
- Make the reflection [1] step darker with this table, which gives each colour of my palette its next darker colour: [the table]. Do not multiply or blend colours.
- Ripples: move each row of the reflection sideways by a whole number of pixels, taken from this list of 16 and multiplied by [ripple, for example 1]: 0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1. The step is the tick count divided by [pace, for example 6], plus how many rows below the line the row is, wrapped to 16.
- Near the left and right edge of the screen, take the edge's own pixel instead of reading past it.
Before the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = my palette), a place and the tick, and returns the number for that one pixel. Test it: above the line nothing changes; the first row of water is the last row above the line, one step darker; what stands higher lies deeper; with ripple 0 every row lines up with what it mirrors. Show me the test's output. Then write the shader so that it does exactly what the function does.
The rule, tested (shader.ts):
```ts
/* 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;
}
```
Its test (shader.test.ts):
```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}`);
}
});
```
The same rule as a GLSL shader (WebGL 2, OpenGL ES 3.0), checked against the rule pixel for pixel:
```glsl
#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);
}
```
The same rule for Godot 4 (not run by the authors):
```gdshader
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);
}
```
How useful was this page?
Nobody has rated this page yet.