The Order of Shader Effects in a Pixel Art Game
A game runs many effects on the same picture, and their order decides what you see. This last lesson puts three into one rule: a reflection in water, a lantern's light, and the night. In the wrong order, the reflection misses the light.

One rule in three steps: the world (which place each pixel shows, mirrored below the waterline), then the light (from the lantern, for the place shown), then the grade (the night, less the light). So the reflection carries the lantern's light.
Also in this lesson 4 things to read up on 3 questions to check yourself
The idea
A game uses many of this course's rules at once, each on the picture the one before it left. Looking back, this order works:
- Effects on single sprites come first: the hit flash, the palette swap, the outline, the dissolve, the shadow under a figure. They belong to the figure and travel with it, so every later rule sees the figure as it now looks: its reflection flashes with it, and the night darkens its outline.
- The world comes next: grass in the wind, water that mirrors, heat haze, rain. They move or copy pixels, so afterwards each pixel shows its final place of the world.
- Light falls on the world as it now stands: the lantern lights what each pixel shows, the reflection included.
- The grade of the whole picture, the night or the hour of the day, comes last before the screen. It works on finished colours. Once a colour is graded down to night, nobody can tell what it was: ink, slate, dark wood and the darkest blue all end in the same night colour, and a light after the grade could not give them back.
- Enlarging the picture to the screen comes at the very end. Every rule here works on whole pixels of the small picture; enlarge first, and the dithering comes out finer than the art's pixels. Only what imitates the screen itself, like scanlines, goes with the enlarging: it belongs to the screen's rows.
How it works
The rule in the picture is written as three steps, so your AI can see the order.
Step 1, the world. Which place of the world does this pixel show? Above the waterline (row 100) its own. Below it, the place as far above the line as this pixel is below, moved sideways by "ripples" (0 is still water), as in the lesson on water reflections.
Step 2, the light. How many steps of the night does the lantern take away, for the place the pixel shows? As in the lesson on lantern light: full light for "reach of the lantern's full light" (16 pixels, flickering by one until you touch a slider), then rings 9 pixels wide with dithered edges. Because the light is worked out for the place shown, the reflection of the lantern is as lit as the lantern.
Step 3, the grade. "Steps of night" (4) less what the light took away, and one more step for the water. Only here is the colour made darker, all its steps at once, through the palette's table.
If your game draws its light as a separate layer after the world, mirror that layer with the water too, or give each lamp a twin below the waterline, as far below it as the lamp is above.
What goes wrong
An AI often works the light out first, because the pixel's own place is the first thing a shader is given. Or it adds the reflection to a rule that already lights the scene. Either way the light is measured at the pixel's own place, and the reflection only changes where the colour comes from.
Above the water nothing looks wrong. But the water is now lit for its own place, below the waterline, not for what it mirrors near the lantern, so the lantern's reflection and the lit boards in the water lie in the dark. Switch the picture to "With the mistake" while the beekeeper stands under the lantern: above the water she is lit, and her reflection is gone.
Two tests catch it: "the lantern's reflection is as lit as the lantern, less the water's one step", and "the water shows the lit picture above it", which holds every reflected pixel to one step darker than the lit pixel it mirrors.
The mistake, line by line
- /* The order of effects, with the mistake: the same three steps, but the light comes first. At that
- point the world has not yet said which place the pixel shows, so the light is worked out for the
- pixel's own place. Above the water nothing changes. In the water, the reflection gets the light of
- the water's own place instead of the light of what it mirrors, and the water's own place lies further
- below a lantern than what it mirrors: the lantern's reflection, and the lit boards around it, lie in
- the dark.
+ /* The order of effects: night, a lantern and a reflection in water in one rule, as three small steps in
+ the order that is right. First the world: which place of the world this pixel shows (below the
+ waterline, the place mirrored above it, moved by the ripples). Then the light: how much of the night
+ the lantern takes away, for the place the pixel shows, so that a reflection carries the light of what
+ it mirrors. Last the grade of the whole picture: the night, less the light, and one step for the water.
+ 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. Distances are compared squared; the ripples come from a table of whole pixels.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: the light first, then the world, then the grade. */
+ /** The rule: the world, then the light, then the grade. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
- // 1. The light, too early: the world has not said yet which place this pixel shows, so the light is
- // worked out for the pixel's own place.
- const dx = x - p.lanternX;
- const dy = y - p.lanternY;
- const far = dx * dx + dy * dy;
- const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
- let lit = 0;
- for (let k = 0; k < p.night; k++) {
- const reach = p.radius + k * p.ring + jitter;
- if (far <= reach * reach) lit++;
- }
-
- // 2. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
+ // 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
…
if (n === 0) n = at(src, x, fromY);
+ // 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
+ // pixel shows, so that a reflection gets the light of what it mirrors.
+ const dx = fromX - p.lanternX;
+ const dy = fromY - p.lanternY;
+ const far = dx * dx + dy * dy;
+ const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
+ let lit = 0;
+ for (let k = 0; k < p.night; k++) {
+ const reach = p.radius + k * p.ring + jitter;
+ if (far <= reach * reach) lit++;
+ }
+
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, p.night - lit + water);What the test says about the mistake
✔ above the water and far from the lantern, a colour is as many steps darker as `night` says
✔ at the lantern a colour is its own, and each ring further out is one step darker
✔ below the waterline the picture is the place mirrored above it, one step darker
✖ the lantern's reflection is as lit as the lantern, less the water's one step
got 24, expected 9
✖ the water shows the lit picture above it: every reflected pixel is one step darker than the pixel it mirrors
at 3, 5
got 24, expected 21
✔ the ripple moves the reflection sideways by whole pixels and leaves the world above the water alone
✔ no pixel of the water is left empty where the ripple reaches past the picture's edge
7 tests: 5 passed, 2 failedTell your AI
Write one shader for the whole screen of my pixel art game that does a water reflection, a lantern's light and the night, in this order, as three numbered steps with a comment each.
- Work on whole pixels and the colours of my palette only. Each colour's next darker colour: [a table, or each ramp of my palette from light to dark].
- Step 1, the world: below the waterline at row [100], show the place mirrored above the line, moved sideways by a ripple of whole pixels from a table.
- Step 2, the light: count how many steps of the night the lantern at [its place] takes away, with full light for [16] pixels and rings of [9] pixels, for the place the pixel shows, not for the pixel's own place.
- Step 3, the grade: make the colour [4] steps darker, less the light, plus one step in the water. Darken only in this step.
- Write the order of all effects in my game as a list in a comment at the top: effects on single sprites, then the world, then light, then the grade, then the enlarging to the screen.
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 colours), a place, the time and the settings, and returns the number for that one pixel. Test it: the lantern's reflection is as lit as the lantern, less one step; every reflected pixel is one step darker than the lit pixel it mirrors; far from the lantern a colour is [4] steps darker. 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
/* The order of effects: night, a lantern and a reflection in water in one rule, as three small steps in
the order that is right. First the world: which place of the world this pixel shows (below the
waterline, the place mirrored above it, moved by the ripples). Then the light: how much of the night
the lantern takes away, for the place the pixel shows, so that a reflection carries the light of what
it mirrors. Last the grade of the whole picture: the night, less the light, and one step for the water.
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. Distances are compared squared; the ripples come from a table of whole pixels.
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: below it, the picture is the reflection of what stands above it. */
waterline: 100,
/** How far the ripples move the reflection sideways: 0 is still water. */
ripple: 1,
/** Where the lantern is, in pixels from the left. */
lanternX: 150,
/** Where the lantern is, in pixels from the top. */
lanternY: 74,
/** How far the lantern's full light reaches, in pixels. */
radius: 16,
/** How wide each ring of less light is, in pixels. */
ring: 9,
/** How many pixels at the outside of each ring are dithered. */
edge: 3,
/** How many steps darker the night is where no light reaches. */
night: 4,
};
export type Params = typeof DEFAULTS;
/** A wave in whole pixels, 16 steps long. */
const WAVE = [0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1];
/** The 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** 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];
}
/** A colour made darker by the table, `steps` times. */
function darker(n: number, steps: number): number {
let c = n;
for (let i = 0; i < steps; i++) c = DARKER[c];
return c;
}
/** The rule: the world, then the light, then the grade. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
let fromX = x;
let fromY = y;
let water = 0;
if (y >= p.waterline) {
const below = y - p.waterline;
fromY = p.waterline - 1 - below;
fromX = x + WAVE[(below + (tick >> 3)) & 15] * p.ripple;
water = 1;
}
let n = at(src, fromX, fromY);
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n === 0) n = at(src, x, fromY);
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
const dx = fromX - p.lanternX;
const dy = fromY - p.lanternY;
const far = dx * dx + dy * dy;
const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
let lit = 0;
for (let k = 0; k < p.night; k++) {
const reach = p.radius + k * p.ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, p.night - lit + water);
}
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: below it, the picture is the reflection of what stands above it
uniform int ripple; // how far the ripples move the reflection sideways: 0 is still water
uniform int lanternX; // where the lantern is, in pixels from the left
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the lantern's full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered
uniform int night; // how many steps darker the night is where no light reaches
// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// 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);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// the world, then the light, then the grade
int shade(ivec2 p) {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
int fromX = p.x;
int fromY = p.y;
int water = 0;
if (p.y >= waterline) {
int below = p.y - waterline;
fromY = waterline - 1 - below;
fromX = p.x + WAVE[(below + (TICK >> 3)) & 15] * ripple;
water = 1;
}
int n = at(ivec2(fromX, fromY));
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n == 0) n = at(ivec2(p.x, fromY));
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
int dx = fromX - lanternX;
int dy = fromY - lanternY;
int far = dx * dx + dy * dy;
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
int lit = 0;
for (int k = 0; k < night; k++) {
int reach = radius + k * ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, night - lit + water);
}
// ---- 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: below it, the picture is the reflection of what stands above it
uniform int ripple; // how far the ripples move the reflection sideways: 0 is still water
uniform int lanternX; // where the lantern is, in pixels from the left
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the lantern's full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered
uniform int night; // how many steps darker the night is where no light reaches
// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// 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);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// the world, then the light, then the grade
int shade(ivec2 p) {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
int fromX = p.x;
int fromY = p.y;
int water = 0;
if (p.y >= waterline) {
int below = p.y - waterline;
fromY = waterline - 1 - below;
fromX = p.x + WAVE[(below + (TICK >> 3)) & 15] * ripple;
water = 1;
}
int n = at(ivec2(fromX, fromY));
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n == 0) n = at(ivec2(p.x, fromY));
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
int dx = fromX - lanternX;
int dy = fromY - lanternY;
int far = dx * dx + dy * dy;
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
int lit = 0;
for (int k = 0; k < night; k++) {
int reach = radius + k * ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, night - lit + water);
}
// ---- 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) };
}
// above the water, rows 0 to 4: a white wall (5) with the lantern's honey flame (8) at 3, 1 and a cream
// jetty (6) in row 4. Rows 5 to 9 are the water as it came (4); the reflection takes their place.
const POND = picture([
"5555555",
"5558555",
"5555555",
"5555555",
"6666666",
"4444444",
"4444444",
"4444444",
"4444444",
"4444444",
]);
// still water from row 5; the lantern at the flame, full light 1 pixel far, rings 1 pixel wide, 3 steps of night
const STILL = { ...DEFAULTS, waterline: 5, ripple: 0, lanternX: 3, lanternY: 1, radius: 1, ring: 1, edge: 0, night: 3 };
// the next darker colour of each colour in this picture, from the palette's table
const NEXT: Record<number, number> = { 5: 15, 15: 16, 16: 14, 14: 12, 12: 13, 6: 7, 7: 20, 20: 21, 21: 24, 8: 9, 9: 20, 24: 24 };
test("above the water and far from the lantern, a colour is as many steps darker as `night` says", () => {
// white goes darker as the palest blue, sky, light blue (14)
assert.equal(shade(0, 3, 0, POND, STILL), 14);
assert.equal(shade(6, 0, 0, POND, STILL), 14);
});
test("at the lantern a colour is its own, and each ring further out is one step darker", () => {
assert.equal(shade(3, 1, 0, POND, STILL), 8);
assert.equal(shade(4, 1, 0, POND, STILL), 5);
assert.equal(shade(5, 1, 0, POND, STILL), 15);
assert.equal(shade(6, 1, 0, POND, STILL), 16);
});
test("below the waterline the picture is the place mirrored above it, one step darker", () => {
// with no night at all only the world's step is left: the mirror and the water's one step
const day = { ...STILL, night: 0 };
assert.equal(shade(0, 5, 0, POND, day), 7);
assert.equal(shade(0, 6, 0, POND, day), 15);
assert.equal(shade(3, 8, 0, POND, day), 9);
assert.equal(shade(0, 9, 0, POND, day), 15);
});
test("the lantern's reflection is as lit as the lantern, less the water's one step", () => {
assert.equal(shade(3, 8, 0, POND, STILL), 9);
});
test("the water shows the lit picture above it: every reflected pixel is one step darker than the pixel it mirrors", () => {
for (let below = 0; below < 5; below++) {
for (let x = 0; x < POND.width; x++) {
const above = shade(x, 4 - below, 0, POND, STILL);
assert.equal(shade(x, 5 + below, 0, POND, STILL), NEXT[above], `at ${x}, ${5 + below}`);
}
}
});
test("the ripple moves the reflection sideways by whole pixels and leaves the world above the water alone", () => {
const rippled = { ...STILL, ripple: 1 };
// in the second row of water the wave's step at tick 0 is 1 pixel: each pixel shows its right neighbour's reflection
for (let x = 0; x < POND.width - 1; x++) assert.equal(shade(x, 6, 0, POND, rippled), shade(x + 1, 6, 0, POND, STILL));
for (const tick of [0, 8, 40, 77]) {
for (let y = 0; y < 5; y++) for (let x = 0; x < POND.width; x++) assert.equal(shade(x, y, tick, POND, rippled), shade(x, y, 0, POND, STILL));
}
});
test("no pixel of the water is left empty where the ripple reaches past the picture's edge", () => {
const rough = { ...STILL, ripple: 2 };
for (let tick = 0; tick < 128; tick += 8) {
for (let y = 5; y < POND.height; y++) for (let x = 0; x < POND.width; x++) assert.notEqual(shade(x, y, tick, POND, rough), 0, `at ${x}, ${y}, tick ${tick}`);
}
});
- 7 tests pass on the rule.
- With the mistake put in, 2 of them fail: "the lantern's reflection is as lit as the lantern, less the water's one step", "the water shows the lit picture above it: every reflected pixel is one step darker than the pixel it mirrors".
- 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
- Move "reach of the lantern's full light" up to 40. The light spills past the boathouse onto the sky and the far trees, and the water mirrors all of it.
- Set "ripples" to 0. The water is a still mirror, and the circle of light in it is the circle above, upside down.
- Set "steps of night" to 2: the sky turns blue, and the trees and hives show again in the water. At 5 almost nothing is left but the light and its reflection.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Lantern Light in Pixel Art: a Shader with Round, Dithered Rings
The lantern's rings on their own: distance compared without a square root, a ring for each step of the dark, dithered edges, and why an AI's first light often comes out as a diamond.
Day and Night Shader for Pixel Art: Darker by the Hour
The grade by the hour, with a second picture of what gives light so that windows and lamps keep glowing: another way to keep a light from going dark with the night.
One clock for the whole world that says from the hour how bright it is: where the steps of night in this lesson's grade can come from, so that light, events and sleep never disagree.
How Godot 4 runs effects on the whole screen one after another: a layer with a full-screen shader for each, every pass taking the one before as its picture, in the order the layers stand in.
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.
Why does the night, the grade of the whole picture, come after the lantern's light and not before it?
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/order-of-effects
Write one shader for the whole screen of my pixel art game that does a water reflection, a lantern's light and the night, in this order, as three numbered steps with a comment each.
- Work on whole pixels and the colours of my palette only. Each colour's next darker colour: [a table, or each ramp of my palette from light to dark].
- Step 1, the world: below the waterline at row [100], show the place mirrored above the line, moved sideways by a ripple of whole pixels from a table.
- Step 2, the light: count how many steps of the night the lantern at [its place] takes away, with full light for [16] pixels and rings of [9] pixels, for the place the pixel shows, not for the pixel's own place.
- Step 3, the grade: make the colour [4] steps darker, less the light, plus one step in the water. Darken only in this step.
- Write the order of all effects in my game as a list in a comment at the top: effects on single sprites, then the world, then light, then the grade, then the enlarging to the screen.
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 colours), a place, the time and the settings, and returns the number for that one pixel. Test it: the lantern's reflection is as lit as the lantern, less one step; every reflected pixel is one step darker than the lit pixel it mirrors; far from the lantern a colour is [4] steps darker. 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
/* The order of effects: night, a lantern and a reflection in water in one rule, as three small steps in
the order that is right. First the world: which place of the world this pixel shows (below the
waterline, the place mirrored above it, moved by the ripples). Then the light: how much of the night
the lantern takes away, for the place the pixel shows, so that a reflection carries the light of what
it mirrors. Last the grade of the whole picture: the night, less the light, and one step for the water.
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. Distances are compared squared; the ripples come from a table of whole pixels.
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: below it, the picture is the reflection of what stands above it. */
waterline: 100,
/** How far the ripples move the reflection sideways: 0 is still water. */
ripple: 1,
/** Where the lantern is, in pixels from the left. */
lanternX: 150,
/** Where the lantern is, in pixels from the top. */
lanternY: 74,
/** How far the lantern's full light reaches, in pixels. */
radius: 16,
/** How wide each ring of less light is, in pixels. */
ring: 9,
/** How many pixels at the outside of each ring are dithered. */
edge: 3,
/** How many steps darker the night is where no light reaches. */
night: 4,
};
export type Params = typeof DEFAULTS;
/** A wave in whole pixels, 16 steps long. */
const WAVE = [0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1];
/** The 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** 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];
}
/** A colour made darker by the table, `steps` times. */
function darker(n: number, steps: number): number {
let c = n;
for (let i = 0; i < steps; i++) c = DARKER[c];
return c;
}
/** The rule: the world, then the light, then the grade. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
let fromX = x;
let fromY = y;
let water = 0;
if (y >= p.waterline) {
const below = y - p.waterline;
fromY = p.waterline - 1 - below;
fromX = x + WAVE[(below + (tick >> 3)) & 15] * p.ripple;
water = 1;
}
let n = at(src, fromX, fromY);
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n === 0) n = at(src, x, fromY);
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
const dx = fromX - p.lanternX;
const dy = fromY - p.lanternY;
const far = dx * dx + dy * dy;
const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
let lit = 0;
for (let k = 0; k < p.night; k++) {
const reach = p.radius + k * p.ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, p.night - lit + water);
}
```
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) };
}
// above the water, rows 0 to 4: a white wall (5) with the lantern's honey flame (8) at 3, 1 and a cream
// jetty (6) in row 4. Rows 5 to 9 are the water as it came (4); the reflection takes their place.
const POND = picture([
"5555555",
"5558555",
"5555555",
"5555555",
"6666666",
"4444444",
"4444444",
"4444444",
"4444444",
"4444444",
]);
// still water from row 5; the lantern at the flame, full light 1 pixel far, rings 1 pixel wide, 3 steps of night
const STILL = { ...DEFAULTS, waterline: 5, ripple: 0, lanternX: 3, lanternY: 1, radius: 1, ring: 1, edge: 0, night: 3 };
// the next darker colour of each colour in this picture, from the palette's table
const NEXT: Record<number, number> = { 5: 15, 15: 16, 16: 14, 14: 12, 12: 13, 6: 7, 7: 20, 20: 21, 21: 24, 8: 9, 9: 20, 24: 24 };
test("above the water and far from the lantern, a colour is as many steps darker as `night` says", () => {
// white goes darker as the palest blue, sky, light blue (14)
assert.equal(shade(0, 3, 0, POND, STILL), 14);
assert.equal(shade(6, 0, 0, POND, STILL), 14);
});
test("at the lantern a colour is its own, and each ring further out is one step darker", () => {
assert.equal(shade(3, 1, 0, POND, STILL), 8);
assert.equal(shade(4, 1, 0, POND, STILL), 5);
assert.equal(shade(5, 1, 0, POND, STILL), 15);
assert.equal(shade(6, 1, 0, POND, STILL), 16);
});
test("below the waterline the picture is the place mirrored above it, one step darker", () => {
// with no night at all only the world's step is left: the mirror and the water's one step
const day = { ...STILL, night: 0 };
assert.equal(shade(0, 5, 0, POND, day), 7);
assert.equal(shade(0, 6, 0, POND, day), 15);
assert.equal(shade(3, 8, 0, POND, day), 9);
assert.equal(shade(0, 9, 0, POND, day), 15);
});
test("the lantern's reflection is as lit as the lantern, less the water's one step", () => {
assert.equal(shade(3, 8, 0, POND, STILL), 9);
});
test("the water shows the lit picture above it: every reflected pixel is one step darker than the pixel it mirrors", () => {
for (let below = 0; below < 5; below++) {
for (let x = 0; x < POND.width; x++) {
const above = shade(x, 4 - below, 0, POND, STILL);
assert.equal(shade(x, 5 + below, 0, POND, STILL), NEXT[above], `at ${x}, ${5 + below}`);
}
}
});
test("the ripple moves the reflection sideways by whole pixels and leaves the world above the water alone", () => {
const rippled = { ...STILL, ripple: 1 };
// in the second row of water the wave's step at tick 0 is 1 pixel: each pixel shows its right neighbour's reflection
for (let x = 0; x < POND.width - 1; x++) assert.equal(shade(x, 6, 0, POND, rippled), shade(x + 1, 6, 0, POND, STILL));
for (const tick of [0, 8, 40, 77]) {
for (let y = 0; y < 5; y++) for (let x = 0; x < POND.width; x++) assert.equal(shade(x, y, tick, POND, rippled), shade(x, y, 0, POND, STILL));
}
});
test("no pixel of the water is left empty where the ripple reaches past the picture's edge", () => {
const rough = { ...STILL, ripple: 2 };
for (let tick = 0; tick < 128; tick += 8) {
for (let y = 5; y < POND.height; y++) for (let x = 0; x < POND.width; x++) assert.notEqual(shade(x, y, tick, POND, rough), 0, `at ${x}, ${y}, 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: below it, the picture is the reflection of what stands above it
uniform int ripple; // how far the ripples move the reflection sideways: 0 is still water
uniform int lanternX; // where the lantern is, in pixels from the left
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the lantern's full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered
uniform int night; // how many steps darker the night is where no light reaches
// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// 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);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// the world, then the light, then the grade
int shade(ivec2 p) {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
int fromX = p.x;
int fromY = p.y;
int water = 0;
if (p.y >= waterline) {
int below = p.y - waterline;
fromY = waterline - 1 - below;
fromX = p.x + WAVE[(below + (TICK >> 3)) & 15] * ripple;
water = 1;
}
int n = at(ivec2(fromX, fromY));
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n == 0) n = at(ivec2(p.x, fromY));
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
int dx = fromX - lanternX;
int dy = fromY - lanternY;
int far = dx * dx + dy * dy;
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
int lit = 0;
for (int k = 0; k < night; k++) {
int reach = radius + k * ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, night - lit + water);
}
// ---- 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: below it, the picture is the reflection of what stands above it
uniform int ripple; // how far the ripples move the reflection sideways: 0 is still water
uniform int lanternX; // where the lantern is, in pixels from the left
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the lantern's full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered
uniform int night; // how many steps darker the night is where no light reaches
// a wave in whole pixels, 16 steps long
const int WAVE[16] = int[16](0, 1, 1, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2, -1, -1);
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// 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);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// the world, then the light, then the grade
int shade(ivec2 p) {
// 1. The world: which place of the world does this pixel show? Above the waterline its own. Below it,
// the place as far above the line as this one is below, moved sideways by the ripple; the water
// will take one step of light.
int fromX = p.x;
int fromY = p.y;
int water = 0;
if (p.y >= waterline) {
int below = p.y - waterline;
fromY = waterline - 1 - below;
fromX = p.x + WAVE[(below + (TICK >> 3)) & 15] * ripple;
water = 1;
}
int n = at(ivec2(fromX, fromY));
// where the ripple reaches past the picture's edge, the place straight above is taken
if (n == 0) n = at(ivec2(p.x, fromY));
// 2. The light: how many of the night's steps the lantern takes away, worked out for the place the
// pixel shows, so that a reflection gets the light of what it mirrors.
int dx = fromX - lanternX;
int dy = fromY - lanternY;
int far = dx * dx + dy * dy;
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
int lit = 0;
for (int k = 0; k < night; k++) {
int reach = radius + k * ring + jitter;
if (far <= reach * reach) lit++;
}
// 3. The grade of the whole picture: the night, less what the light took away, and the water's step.
return darker(n, night - lit + water);
}
// ---- 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.