Pixel Art Rain Shader: Streaks That Never Fall in Step
Rain in pixel art is thin streaks that fall fast down the screen, one pixel wide and a few long. This lesson's rule draws them over the whole picture from one number per column: scrambled, that number says whether the column rains and how far its streaks have fallen. Nothing is kept from one frame to the next; every streak is where its column's own clock says it is.

The whole square is given to the rule. Each column of pixels has its own scrambled number: one part decides whether it rains there, another where in the fall its streaks are. So no two streaks keep step, and the rain never lines up in rows.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
A shower over a market, a storm over a harbour, drizzle on a graveyard at night: a player reads rain from many short streaks that fall quickly and never together. The moment they line up, the eye sees a pattern instead of weather.
A game can draw rain as hundreds of falling drops, each one a thing with a place that is moved every frame. The rule here keeps nothing. For each pixel it works out, from the pixel's column, its row and the tick, whether a streak is passing through it right now. The same rule draws a drizzle and a downpour; only one number changes. And since the streaks follow the game's tick, a paused game has paused rain.
How it works
Each column asks the scramble from the lesson on noise you can trust two questions, feeding it the column's number with a different seed each time.
Does it rain here? The answer to this one is a number from 0 to 99. If it is below density, the column rains; otherwise every pixel in it comes back as it came. As the answer belongs to the column, the same columns rain at every tick.
How late is this column? The second answer, cut down to 0 to 47, is the column's own delay. Every column has a streak every 48 rows, and the delay says where those streaks are.
Then the fall. The rule takes the pixel's row, takes away the tick times speed, adds the column's delay, and wraps the result into 0 to 47. Where that comes out below streak, the pixel is rain, in the palest blue (number 15); everywhere else the picture stays. Because the tick is taken away, a streak moves speed rows down each tick, and while it runs out at the bottom, the column's next streak is already on its way in at the top.
The sliders are density, how many columns of a hundred rain (40 when nobody touches it; the picture swings it from 6 to 54 and back), speed in rows a tick (3, which is 180 rows a second at 60 ticks), and streak, how many pixels long a streak is (5).
What goes wrong
Every column falls in step. The AI scrambles the column to decide where it rains, which is right, and then lets one clock drive the fall of all the columns. Each column alone is correct, and a test of one column passes. But the streaks of all the raining columns now stand in the same rows, and the rain comes down in level bands of dashes, like a curtain, sliding down the screen as one.
It happens because the scramble is already there, the motion is already there, and nothing looks missing in the code. What is missing is the second number: the column's own delay.
Switch the picture to "With the mistake". The streaks snap into flat rows. Move density up and the rows only fill in; they never break apart.
Two tests catch it. "In no row is every raining column wet at once" fails because the curtain wets them all in the same rows. "Most raining columns have a timing of their own" counts the rows in which the columns' streaks start; on the mistake it reports that the streaks of 10 raining columns start in 1 of 48 rows.
The mistake, line by line
- /* Rain, with the mistake: the column's scramble decides only whether it rains there, and every column
- falls on the same clock. The streaks of all raining columns stand in the same rows, so the rain comes
- down in level bands, like a curtain of dashes, instead of as single drops.
+ /* Rain: thin streaks that fall down the screen, each column in its own time. A scramble of the
+ column's number decides whether it rains there at all, and another one where in the fall its
+ streaks are. Every tick the streaks move down by `speed` rows. Where a streak is, the pixel takes
+ the rain's colour; everywhere else the picture comes back as it came.
+ The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
+ palette's colours. The rule is asked for every pixel and answers with a number.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
const wrap = (a: number, n: number) => a - n * Math.floor(a / n);
- /** The rule, wrong: in a column where it rains, a streak wherever the one fall for all columns has reached. */
+ /** The rule: in a column where it rains, a streak wherever the column's own fall has reached. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (hash(x + 1 * 65537) % 100 >= p.density) return here;
- // the mistake: no scramble for when it rains here, so every column is at the same place in its fall
+ // when does it rain here? another scramble of the column's number: where in the fall its streaks are
+ const late = hash(x + 2 * 65537) % GAP;
// how far the fall has come at this row: it moves down by `speed` rows every tick
- const fall = wrap(y - tick * p.speed, GAP);
+ const fall = wrap(y - tick * p.speed + late, GAP);
return fall < p.streak ? p.color : here;
}What the test says about the mistake
✔ with density 0 nothing changes, and with 100 every column rains
✔ every pixel is either the rain's colour or what was there
✔ a dry column stays dry, and a raining column rains at every tick
✔ a streak falls speed rows every tick
✔ a streak is streak pixels long, and the next one in its column comes 48 rows later
✖ in no row is every raining column wet at once
at tick 0 all 10 raining columns are wet in row 0
✖ most raining columns have a timing of their own: their streaks start in different rows
the streaks of 10 raining columns start in 1 of 48 rows
7 tests: 5 passed, 2 failedTell your AI
Write a rain shader for my pixel art game: thin streaks that fall down the whole screen, over everything.
- Whole pixels only: streaks one pixel wide, no smoothing and no blending. The rain is one colour: [my rain colour].
- Use a hash of whole numbers, not sin and not a random function. Hash the column's number once to decide whether it rains in that column ([40] columns in 100), and once more, with another seed, for that column's own delay.
- Time is a whole number of ticks that my game counts. A streak falls [3] pixels every tick and is [5] pixels long; the next one in the same column comes 48 pixels behind it.
- Every column falls by its own delay. No two columns may move in step just because they share the clock.
Before you write the shader, write the same rule as a small plain function that takes a place and a tick and returns the colour's number. Test it: a dry column stays dry, a streak moves [3] rows down each tick, in no row is every raining column wet at once, and most raining columns start their streaks in different rows. Show me the tests' 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
/* Rain: thin streaks that fall down the screen, each column in its own time. A scramble of the
column's number decides whether it rains there at all, and another one where in the fall its
streaks are. Every tick the streaks move down by `speed` rows. Where a streak is, the pixel takes
the rain's colour; everywhere else the picture comes back as it came.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How many columns of a hundred it rains in. */
density: 40,
/** How many rows a streak falls each tick. */
speed: 3,
/** How many pixels long a streak is. */
streak: 5,
/** The number of the rain's colour in the palette: 15 is the palest blue. */
color: 15,
};
export type Params = typeof DEFAULTS;
/** How many rows lie between the tops of two streaks in one column. */
const GAP = 48;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** a wrapped into 0 .. n-1, also for a below zero */
const wrap = (a: number, n: number) => a - n * Math.floor(a / n);
/** The rule: in a column where it rains, a streak wherever the column's own fall has reached. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (hash(x + 1 * 65537) % 100 >= p.density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
const late = hash(x + 2 * 65537) % GAP;
// how far the fall has come at this row: it moves down by `speed` rows every tick
const fall = wrap(y - tick * p.speed + late, GAP);
return fall < p.streak ? p.color : here;
}
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 density; // how many columns of a hundred it rains in
uniform int speed; // how many rows a streak falls each tick
uniform int streak; // how many pixels long a streak is
uniform int color; // the number of the rain's colour in the palette: 15 is the palest blue
// how many rows lie between the tops of two streaks in one column
const int GAP = 48;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// in a column where it rains, a streak wherever the column's own fall has reached
int shade(ivec2 p) {
int here = at(p);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (int(hash(uint(p.x) + 1u * 65537u) % 100u) >= density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
int late = int(hash(uint(p.x) + 2u * 65537u) % uint(GAP));
// how far the fall has come at this row: it moves down by `speed` rows every tick
int fall = wrap(p.y - TICK * speed + late, GAP);
return fall < streak ? color : here;
}
// ---- 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 density; // how many columns of a hundred it rains in
uniform int speed; // how many rows a streak falls each tick
uniform int streak; // how many pixels long a streak is
uniform int color; // the number of the rain's colour in the palette: 15 is the palest blue
// how many rows lie between the tops of two streaks in one column
const int GAP = 48;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// in a column where it rains, a streak wherever the column's own fall has reached
int shade(ivec2 p) {
int here = at(p);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (int(hash(uint(p.x) + 1u * 65537u) % 100u) >= density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
int late = int(hash(uint(p.x) + 2u * 65537u) % uint(GAP));
// how far the fall has come at this row: it moves down by `speed` rows every tick
int fall = wrap(p.y - TICK * speed + late, GAP);
return fall < streak ? color : here;
}
// ---- 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 street: 40 rows of sky (2) over 20 rows of road (1), 30 columns wide. */
const STREET = picture([
...Array.from({ length: 40 }, () => "222222222222222222222222222222"),
...Array.from({ length: 20 }, () => "111111111111111111111111111111"),
]);
/** The rows between the tops of two streaks in one column, as in shader.ts. */
const GAP = 48;
const wet = (x: number, y: number, tick: number, p = DEFAULTS) => shade(x, y, tick, STREET, p) === p.color;
const rainy = (p = DEFAULTS) => Array.from({ length: STREET.width }, (_, x) => x).filter((x) => Array.from({ length: STREET.height }, (_, y) => y).some((y) => wet(x, y, 0, p)));
test("with density 0 nothing changes, and with 100 every column rains", () => {
for (let tick = 0; tick < 40; tick++) {
for (let y = 0; y < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) assert.equal(shade(x, y, tick, STREET, { ...DEFAULTS, density: 0 }), STREET.index[y * STREET.width + x]);
}
}
assert.equal(rainy({ ...DEFAULTS, density: 100 }).length, STREET.width);
});
test("every pixel is either the rain's colour or what was there", () => {
for (let tick = 0; tick < 40; tick++) {
for (let y = 0; y < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) {
const n = shade(x, y, tick, STREET);
assert.ok(n === DEFAULTS.color || n === STREET.index[y * STREET.width + x], `${n} at ${x}, ${y}`);
}
}
}
});
test("a dry column stays dry, and a raining column rains at every tick", () => {
const raining = rainy();
assert.ok(raining.length > 0 && raining.length < STREET.width, `${raining.length} of ${STREET.width} columns rain`);
for (let tick = 0; tick < 100; tick += 7) {
for (let x = 0; x < STREET.width; x++) {
const any = Array.from({ length: STREET.height }, (_, y) => y).some((y) => wet(x, y, tick));
assert.equal(any, raining.includes(x), `column ${x} at tick ${tick}`);
}
}
});
test("a streak falls speed rows every tick", () => {
for (const speed of [1, 3, 6]) {
const p = { ...DEFAULTS, speed };
for (let tick = 0; tick < 30; tick++) {
for (let y = 0; y + speed < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) assert.equal(wet(x, y + speed, tick + 1, p), wet(x, y, tick, p), `at ${x}, ${y}, tick ${tick}`);
}
}
}
});
test("a streak is streak pixels long, and the next one in its column comes 48 rows later", () => {
for (const streak of [1, 5, 12]) {
const p = { ...DEFAULTS, streak };
for (const x of rainy(p)) {
// where a streak starts: wet here and dry above, both within the picture
const tops = Array.from({ length: STREET.height - 1 }, (_, i) => i + 1).filter((y) => wet(x, y, 0, p) && !wet(x, y - 1, 0, p));
for (const top of tops) {
for (let y = top; y < Math.min(top + streak, STREET.height); y++) assert.ok(wet(x, y, 0, p), `column ${x}: row ${y} should be wet`);
if (top + streak < STREET.height) assert.ok(!wet(x, top + streak, 0, p), `column ${x}: the streak from row ${top} is longer than ${streak}`);
if (top + GAP < STREET.height) assert.ok(wet(x, top + GAP, 0, p), `column ${x}: no streak ${GAP} rows below row ${top}`);
}
}
}
});
test("in no row is every raining column wet at once", () => {
const raining = rainy();
for (let tick = 0; tick < 48; tick++) {
for (let y = 0; y < STREET.height; y++) {
assert.ok(!raining.every((x) => wet(x, y, tick)), `at tick ${tick} all ${raining.length} raining columns are wet in row ${y}`);
}
}
});
test("most raining columns have a timing of their own: their streaks start in different rows", () => {
const raining = rainy();
const starts = new Set(raining.map((x) => Array.from({ length: GAP }, (_, y) => y).find((y) => wet(x, y, 0) && !wet(x, y - 1, 0))));
assert.ok(starts.size * 2 > raining.length, `the streaks of ${raining.length} raining columns start in ${starts.size} of ${GAP} rows`);
});
- 7 tests pass on the rule.
- With the mistake put in, 2 of them fail: "in no row is every raining column wet at once", "most raining columns have a timing of their own: their streaks start in different rows".
- 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
densityto 0, then push it up slowly. Columns join one at a time, always the same ones in the same order, because each column's own number decides when it joins. - Set
speedto 6 andstreakto 1. The drops jump six rows at a time and the eye loses them. Then setstreakto 6: a streak at least as long as its jump reads as a falling line again. - Switch to "With the mistake" and push
densityto 100. Every column rains, and the square vanishes behind solid bars that slide down. With the rule at 100 it is a downpour, but still made of drops.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
What decides that it rains, and how hard: clear, cloudy, rain and storm, never from clear straight to storm, each change faded in. That fade can drive this rule's density instead of a slider.
Shader Noise for Pixel Art: The Same Stars on Every Machine
The scramble this rain asks twice, worked through step by step, and the quick one with a single multiplication that an AI likes to write, whose numbers line up in even steps across the screen.
Prospecting for Hash Functions
Where the shape of this scramble comes from: shift and mix, multiply, twice, then mix again, found by a program that scores each hash by how evenly a flipped input bit flips the bits that come out.
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.
This rain keeps nothing from one frame to the next. What happens to it when the player pauses the game?
Next lesson Heat Haze Shader for Pixel Art: Shimmer Without Ragged Edges
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/rain
Write a rain shader for my pixel art game: thin streaks that fall down the whole screen, over everything.
- Whole pixels only: streaks one pixel wide, no smoothing and no blending. The rain is one colour: [my rain colour].
- Use a hash of whole numbers, not sin and not a random function. Hash the column's number once to decide whether it rains in that column ([40] columns in 100), and once more, with another seed, for that column's own delay.
- Time is a whole number of ticks that my game counts. A streak falls [3] pixels every tick and is [5] pixels long; the next one in the same column comes 48 pixels behind it.
- Every column falls by its own delay. No two columns may move in step just because they share the clock.
Before you write the shader, write the same rule as a small plain function that takes a place and a tick and returns the colour's number. Test it: a dry column stays dry, a streak moves [3] rows down each tick, in no row is every raining column wet at once, and most raining columns start their streaks in different rows. Show me the tests' output. Then write the shader so that it does exactly what the function does.
The rule, tested (shader.ts):
```ts
/* Rain: thin streaks that fall down the screen, each column in its own time. A scramble of the
column's number decides whether it rains there at all, and another one where in the fall its
streaks are. Every tick the streaks move down by `speed` rows. Where a streak is, the pixel takes
the rain's colour; everywhere else the picture comes back as it came.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How many columns of a hundred it rains in. */
density: 40,
/** How many rows a streak falls each tick. */
speed: 3,
/** How many pixels long a streak is. */
streak: 5,
/** The number of the rain's colour in the palette: 15 is the palest blue. */
color: 15,
};
export type Params = typeof DEFAULTS;
/** How many rows lie between the tops of two streaks in one column. */
const GAP = 48;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** a wrapped into 0 .. n-1, also for a below zero */
const wrap = (a: number, n: number) => a - n * Math.floor(a / n);
/** The rule: in a column where it rains, a streak wherever the column's own fall has reached. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (hash(x + 1 * 65537) % 100 >= p.density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
const late = hash(x + 2 * 65537) % GAP;
// how far the fall has come at this row: it moves down by `speed` rows every tick
const fall = wrap(y - tick * p.speed + late, GAP);
return fall < p.streak ? p.color : here;
}
```
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 street: 40 rows of sky (2) over 20 rows of road (1), 30 columns wide. */
const STREET = picture([
...Array.from({ length: 40 }, () => "222222222222222222222222222222"),
...Array.from({ length: 20 }, () => "111111111111111111111111111111"),
]);
/** The rows between the tops of two streaks in one column, as in shader.ts. */
const GAP = 48;
const wet = (x: number, y: number, tick: number, p = DEFAULTS) => shade(x, y, tick, STREET, p) === p.color;
const rainy = (p = DEFAULTS) => Array.from({ length: STREET.width }, (_, x) => x).filter((x) => Array.from({ length: STREET.height }, (_, y) => y).some((y) => wet(x, y, 0, p)));
test("with density 0 nothing changes, and with 100 every column rains", () => {
for (let tick = 0; tick < 40; tick++) {
for (let y = 0; y < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) assert.equal(shade(x, y, tick, STREET, { ...DEFAULTS, density: 0 }), STREET.index[y * STREET.width + x]);
}
}
assert.equal(rainy({ ...DEFAULTS, density: 100 }).length, STREET.width);
});
test("every pixel is either the rain's colour or what was there", () => {
for (let tick = 0; tick < 40; tick++) {
for (let y = 0; y < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) {
const n = shade(x, y, tick, STREET);
assert.ok(n === DEFAULTS.color || n === STREET.index[y * STREET.width + x], `${n} at ${x}, ${y}`);
}
}
}
});
test("a dry column stays dry, and a raining column rains at every tick", () => {
const raining = rainy();
assert.ok(raining.length > 0 && raining.length < STREET.width, `${raining.length} of ${STREET.width} columns rain`);
for (let tick = 0; tick < 100; tick += 7) {
for (let x = 0; x < STREET.width; x++) {
const any = Array.from({ length: STREET.height }, (_, y) => y).some((y) => wet(x, y, tick));
assert.equal(any, raining.includes(x), `column ${x} at tick ${tick}`);
}
}
});
test("a streak falls speed rows every tick", () => {
for (const speed of [1, 3, 6]) {
const p = { ...DEFAULTS, speed };
for (let tick = 0; tick < 30; tick++) {
for (let y = 0; y + speed < STREET.height; y++) {
for (let x = 0; x < STREET.width; x++) assert.equal(wet(x, y + speed, tick + 1, p), wet(x, y, tick, p), `at ${x}, ${y}, tick ${tick}`);
}
}
}
});
test("a streak is streak pixels long, and the next one in its column comes 48 rows later", () => {
for (const streak of [1, 5, 12]) {
const p = { ...DEFAULTS, streak };
for (const x of rainy(p)) {
// where a streak starts: wet here and dry above, both within the picture
const tops = Array.from({ length: STREET.height - 1 }, (_, i) => i + 1).filter((y) => wet(x, y, 0, p) && !wet(x, y - 1, 0, p));
for (const top of tops) {
for (let y = top; y < Math.min(top + streak, STREET.height); y++) assert.ok(wet(x, y, 0, p), `column ${x}: row ${y} should be wet`);
if (top + streak < STREET.height) assert.ok(!wet(x, top + streak, 0, p), `column ${x}: the streak from row ${top} is longer than ${streak}`);
if (top + GAP < STREET.height) assert.ok(wet(x, top + GAP, 0, p), `column ${x}: no streak ${GAP} rows below row ${top}`);
}
}
}
});
test("in no row is every raining column wet at once", () => {
const raining = rainy();
for (let tick = 0; tick < 48; tick++) {
for (let y = 0; y < STREET.height; y++) {
assert.ok(!raining.every((x) => wet(x, y, tick)), `at tick ${tick} all ${raining.length} raining columns are wet in row ${y}`);
}
}
});
test("most raining columns have a timing of their own: their streaks start in different rows", () => {
const raining = rainy();
const starts = new Set(raining.map((x) => Array.from({ length: GAP }, (_, y) => y).find((y) => wet(x, y, 0) && !wet(x, y - 1, 0))));
assert.ok(starts.size * 2 > raining.length, `the streaks of ${raining.length} raining columns start in ${starts.size} of ${GAP} rows`);
});
```
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 density; // how many columns of a hundred it rains in
uniform int speed; // how many rows a streak falls each tick
uniform int streak; // how many pixels long a streak is
uniform int color; // the number of the rain's colour in the palette: 15 is the palest blue
// how many rows lie between the tops of two streaks in one column
const int GAP = 48;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// in a column where it rains, a streak wherever the column's own fall has reached
int shade(ivec2 p) {
int here = at(p);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (int(hash(uint(p.x) + 1u * 65537u) % 100u) >= density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
int late = int(hash(uint(p.x) + 2u * 65537u) % uint(GAP));
// how far the fall has come at this row: it moves down by `speed` rows every tick
int fall = wrap(p.y - TICK * speed + late, GAP);
return fall < streak ? color : here;
}
// ---- 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 density; // how many columns of a hundred it rains in
uniform int speed; // how many rows a streak falls each tick
uniform int streak; // how many pixels long a streak is
uniform int color; // the number of the rain's colour in the palette: 15 is the palest blue
// how many rows lie between the tops of two streaks in one column
const int GAP = 48;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// in a column where it rains, a streak wherever the column's own fall has reached
int shade(ivec2 p) {
int here = at(p);
// does it rain in this column? one scramble of the column's number decides, 0 to 99
if (int(hash(uint(p.x) + 1u * 65537u) % 100u) >= density) return here;
// when does it rain here? another scramble of the column's number: where in the fall its streaks are
int late = int(hash(uint(p.x) + 2u * 65537u) % uint(GAP));
// how far the fall has come at this row: it moves down by `speed` rows every tick
int fall = wrap(p.y - TICK * speed + late, GAP);
return fall < streak ? color : here;
}
// ---- 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.