Dissolve Shader for Pixel Art: Crumbling Without Flicker
A dissolve lets a figure crumble away one pixel at a time, with a glow running ahead of the holes. It is how a defeated enemy leaves the screen, how a figure is carried off through a doorway, and, run backwards, how she arrives. The order in which the pixels go comes from the scramble of the lesson before, and that is where the mistake of this lesson hides.

Only the beekeeper is given to the rule. Each of her pixels has a number from 0 to 99 that never changes; as the progress climbs, the low numbers go first and the ones just above glow.
Also in this lesson 4 things to read up on 3 questions to check yourself
The idea
A figure that simply vanishes from one frame to the next feels like a cut. A dissolve gives the moment a second or two: holes open all over her until nothing is left. In pixel art it looks right when whole pixels go, one at a time, and a pixel that has gone stays gone. Then the eye reads it as crumbling.
For that, every pixel of the figure needs to know when it is its turn. A scramble of its place gives each pixel a number from 0 to 99, as the stars got theirs in the lesson before. The game then moves one setting, the progress, from 0 to 100. Low numbers go early, high numbers late, and because the numbers are scattered, so are the holes.
How it works
The rule looks at the pixel of the figure. If nothing is drawn there, it answers nothing, so the glow can never spill outside her shape.
For a drawn pixel it works out a line from the sliders "progress" and "edge": the progress times the sum of 100 and the edge, divided by 100 and rounded down, minus the edge. With the edge at 8, the line starts at minus 8 when the progress is 0, so nothing is gone and nothing glows, and ends at 100 when the progress is 100, so nothing is left.
Then the rule takes the pixel's number: its column, plus its row times 4099, plus the seed times 65537, scrambled, and the last two digits of the result. Below the line, the pixel is gone. From the line up to the line plus the edge, it glows, here in light violet, number 23 of the palette, which stands out against her and the dark doorway. Above that she keeps her own colour. So every pixel first glows and then goes, and the slider "edge" sets for how long: at 8, while the line climbs through 8 of the hundred numbers.
The number of a pixel depends on its place and the seed alone, never on the time. Pull the progress back, and the same pixels come back in reverse order, each one glowing first. The slider "seed" picks another order. In a game the rule usually sits on the sprite, and a pixel's place is its place in the sprite's own picture; two copies of the same enemy would then crumble in exactly the same pattern, unless each gets its own seed.
What goes wrong
Asked for a dissolve "over time", an AI often adds the time to the scramble, the way noise is made to move in fire, water or the twinkling stars of the lesson before. Then every tick gives every pixel a new number, and every tick picks other pixels to be gone. The progress still climbs, so about the right share of her is missing, but not the same pixels from one tick to the next.
Switch the picture to "With the mistake". Instead of crumbling, she flickers like a broken screen: holes jump around her from frame to frame, and pixels that had gone pop back. A single still picture hides it, so it slips through when the AI looks at one frame only.
Two tests catch it. One holds the progress at 40 and asks again at every tick; with the mistake, 493 of the 900 pixels of the test's figure differ already at the next tick. The other lets the progress climb one step a tick, as in a game, and finds "3 pixels that had gone came back" at tick 10.
The mistake, line by line
- /* Dissolve, with the mistake: the time was added to the seed of the scramble, as it is for noise that
- should move, such as fire. So every tick gives every pixel a new number, and every tick picks other
- pixels to be gone. Instead of crumbling, the figure flickers like a broken screen, and a pixel that
- has gone comes back on the next tick.
+ /* Dissolve: a figure crumbles away pixel by pixel, in an order that a scramble decides, and the
+ pixels that are about to go glow first. Every pixel of the figure gets its number from 0 to 99 from
+ the scramble of the lesson "Noise you can trust". As `progress` climbs from 0 to 100, a line climbs
+ through those numbers: a pixel whose number is below the line is gone, and one just above it glows.
+ The numbers never change from tick to tick, so a pixel that has gone stays gone and the figure
+ crumbles instead of flickering. Run backwards, the same rule lets her come back.
+ 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 */
…
/** The rule: below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came. */
- export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
+ export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
if (here === 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
const line = Math.floor((p.progress * (100 + p.edge)) / 100) - p.edge;
- // when this pixel goes, wrong: the tick is part of the seed, so the number is new every tick
- const order = noise(x, y, p.seed + tick);
+ // when this pixel goes: the lower its number, the sooner. Its place decides, never the time
+ const order = noise(x, y, p.seed);
if (order < line) return 0; // gone
if (order < line + p.edge) return p.glow; // about to go: it glowsWhat the test says about the mistake
✔ at progress 0 she is whole, and nothing glows
✔ at progress 100 nothing of her is left
✔ an empty pixel stays empty, and the glow never leaves her shape
✖ at the same progress the same pixels are gone, whatever the tick
at tick 1, 493 pixels differ from tick 0
got 493, expected 0
✖ a pixel that has gone stays gone while the progress climbs
at tick 10, 3 pixels that had gone came back
got 3, expected 0
✔ every pixel glows before it goes, and never the other way round
6 tests: 4 passed, 2 failedTell your AI
Write a dissolve shader for my sprite: it crumbles away pixel by pixel, and each pixel glows just before it goes.
- Whole pixels and whole numbers only: no fractions, no smoothing, no random numbers.
- Give every pixel of the sprite a number from 0 to 99 by scrambling its place in the sprite's own picture: hash(x + y * 4099 + seed * 65537) % 100, with this scramble on unsigned 32-bit whole numbers that wrap around: hash(n): n = (n xor (n >> 16)) * 0x7feb352d; n = (n xor (n >> 15)) * 0x846ca68b; return n xor (n >> 16).
- The time must never go into the scramble. Only a setting "progress" from 0 to 100 moves, and my game sets it.
- line = progress * (100 + edge) / 100 - edge, rounded down. A pixel whose number is below the line is empty; below line + edge it takes the glow colour [a colour of my palette]; otherwise it keeps its colour. Empty pixels stay empty. The edge is [how many numbers glow, for example 8]. The seed is a setting too, so that two copies of the sprite can crumble differently.
Before the shader, write the same rule as a small plain function and test it: at progress 0 the sprite is whole and nothing glows; at 100 nothing is left; at the same progress the same pixels are gone at every tick; a pixel that has gone stays gone while the progress climbs; every pixel glows before it goes. 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
/* Dissolve: a figure crumbles away pixel by pixel, in an order that a scramble decides, and the
pixels that are about to go glow first. Every pixel of the figure gets its number from 0 to 99 from
the scramble of the lesson "Noise you can trust". As `progress` climbs from 0 to 100, a line climbs
through those numbers: a pixel whose number is below the line is gone, and one just above it glows.
The numbers never change from tick to tick, so a pixel that has gone stays gone and the figure
crumbles instead of flickering. Run backwards, the same rule lets her come back.
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 far she has crumbled, from 0 (whole) to 100 (nothing left). */
progress: 0,
/** How many of the hundred numbers glow just before their pixels go. */
edge: 8,
/** Which order: another number lets her crumble in another order. */
seed: 1,
/** The number of the glow's colour in the palette: 23 is light violet. */
glow: 23,
};
export type Params = typeof DEFAULTS;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** The number from 0 to 99 for a place and a seed. */
export function noise(x: number, y: number, seed: number): number {
return hash(x + y * 4099 + seed * 65537) % 100;
}
/** The rule: below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
if (here === 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
const line = Math.floor((p.progress * (100 + p.edge)) / 100) - p.edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
const order = noise(x, y, p.seed);
if (order < line) return 0; // gone
if (order < line + p.edge) return p.glow; // about to go: it glows
return 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 progress; // how far she has crumbled, from 0 (whole) to 100 (nothing left)
uniform int edge; // how many of the hundred numbers glow just before their pixels go
uniform int seed; // which order: another number lets her crumble in another order
uniform int glow; // the number of the glow's colour in the palette: 23 is light violet
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}
// below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came
int shade(ivec2 p) {
int here = at(p);
if (here == 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
int line = progress * (100 + edge) / 100 - edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
int order = noise(p, seed);
if (order < line) return 0; // gone
if (order < line + edge) return glow; // about to go: it glows
return 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 the sprite, and give it the sprite's own picture once:
// material.set_shader_parameter("SRC", texture)
uniform sampler2D SRC : filter_nearest;
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 progress; // how far she has crumbled, from 0 (whole) to 100 (nothing left)
uniform int edge; // how many of the hundred numbers glow just before their pixels go
uniform int seed; // which order: another number lets her crumble in another order
uniform int glow; // the number of the glow's colour in the palette: 23 is light violet
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}
// below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came
int shade(ivec2 p) {
int here = at(p);
if (here == 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
int line = progress * (100 + edge) / 100 - edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
int order = noise(p, seed);
if (order < line) return 0; // gone
if (order < line + edge) return glow; // about to go: it glows
return here;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(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 figure of 30 by 30 pixels in one colour, 7, with nothing around it: enough pixels to count. */
const BLOCK: Frame = { width: 32, height: 32, index: new Uint8Array(32 * 32) };
for (let y = 1; y < 31; y++) for (let x = 1; x < 31; x++) BLOCK.index[y * 32 + x] = 7;
const BEETLE = picture([
"0011100",
"0144410",
"1444441",
"0144410",
"0100010",
]);
function whole(src: Frame, tick = 0, params = DEFAULTS): number[] {
const out: number[] = [];
for (let y = 0; y < src.height; y++) for (let x = 0; x < src.width; x++) out.push(shade(x, y, tick, src, params));
return out;
}
/** What became of each pixel: 0 gone, 1 glowing, 2 as it came. */
const states = (src: Frame, answers: number[], glow = DEFAULTS.glow) => answers.map((n, i) => (n === 0 ? 0 : n === glow && src.index[i] !== glow ? 1 : 2));
test("at progress 0 she is whole, and nothing glows", () => {
assert.deepEqual(whole(BEETLE, 0, { ...DEFAULTS, progress: 0 }), [...BEETLE.index]);
assert.deepEqual(whole(BLOCK, 0, { ...DEFAULTS, progress: 0 }), [...BLOCK.index]);
});
test("at progress 100 nothing of her is left", () => {
assert.equal(whole(BLOCK, 0, { ...DEFAULTS, progress: 100 }).filter((n) => n !== 0).length, 0);
assert.equal(whole(BEETLE, 0, { ...DEFAULTS, progress: 100, edge: 30 }).filter((n) => n !== 0).length, 0);
});
test("an empty pixel stays empty, and the glow never leaves her shape", () => {
for (let progress = 0; progress <= 100; progress += 5) {
const answers = whole(BEETLE, 0, { ...DEFAULTS, progress });
BEETLE.index.forEach((n, i) => {
if (n === 0) assert.equal(answers[i], 0, `at progress ${progress}`);
});
}
});
test("at the same progress the same pixels are gone, whatever the tick", () => {
const params = { ...DEFAULTS, progress: 40 };
const atStart = whole(BLOCK, 0, params);
for (let tick = 1; tick <= 120; tick += 1) {
const now = whole(BLOCK, tick, params);
const differ = now.filter((n, i) => n !== atStart[i]).length;
assert.equal(differ, 0, `at tick ${tick}, ${differ} pixels differ from tick 0`);
}
});
test("a pixel that has gone stays gone while the progress climbs", () => {
// as in a game: one step of progress every tick
let gone = new Set<number>();
for (let tick = 0; tick <= 100; tick++) {
const answers = whole(BLOCK, tick, { ...DEFAULTS, progress: tick });
const now = new Set(answers.flatMap((n, i) => (n === 0 && BLOCK.index[i] !== 0 ? [i] : [])));
const back = [...gone].filter((i) => !now.has(i)).length;
assert.equal(back, 0, `at tick ${tick}, ${back} pixels that had gone came back`);
gone = now;
}
});
test("every pixel glows before it goes, and never the other way round", () => {
const seen = Array.from(BLOCK.index, () => [] as number[]);
for (let progress = 0; progress <= 100; progress++) {
states(BLOCK, whole(BLOCK, 0, { ...DEFAULTS, progress })).forEach((s, i) => seen[i].push(s));
}
seen.forEach((steps, i) => {
if (BLOCK.index[i] === 0) return;
const order = [...steps].reverse(); // from progress 100 back to 0: gone, then glowing, then whole
assert.deepEqual(order, [...order].sort((a, b) => a - b), `the pixel at ${i % 32}, ${Math.floor(i / 32)} went out of order`);
assert.ok(steps.includes(1), `the pixel at ${i % 32}, ${Math.floor(i / 32)} never glowed`);
});
});
- 6 tests pass on the rule.
- With the mistake put in, 2 of them fail: "at the same progress the same pixels are gone, whatever the tick", "a pixel that has gone stays gone while the progress climbs".
- 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 "progress" slowly from 0 to 100 and back. Holes open and the glow runs ahead of them; on the way back the same pixels return in reverse order, each glowing first.
- Set "edge" to 0, then to 30. At 0 pixels just blink out without a glow; at 30 the glow takes 30 of the hundred numbers at once, nearly a third of her, before the holes catch up.
- Hold "progress" at 50, switch to "With the mistake" and watch: nothing on the slider moves, yet she keeps flickering. Move "seed" with the rule switched on instead: you get another order, and it holds still.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Shader Noise for Pixel Art: The Same Stars on Every Machine
Where each pixel's number comes from: the scramble step by step, why a single multiplication lines the numbers up in stripes, and why a sine can come out differently on another graphics card.
Pixel Art Screen Transition Shader: A Dithered Wipe
A wipe over the whole screen, run by the same kind of progress, and the mistake this lesson's line is built to avoid: a soft edge that has not finished when the progress reaches its end.
How a game can give every copy of an enemy its own seed, from a number or from a name, so that no two crumble alike, and why each system should draw from a stream of its own.
Godot's own reference for what the scramble needs: the type for unsigned whole numbers, the u after such a number, shifts and xor, and how a script sets the progress with set_shader_parameter.
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.
In the dissolve every pixel of the figure goes at a moment of its own. What decides when a pixel's turn comes?
Next lesson Drop Shadow Shader for Pixel Art: Look Back, Not Forward
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/dissolve
Write a dissolve shader for my sprite: it crumbles away pixel by pixel, and each pixel glows just before it goes.
- Whole pixels and whole numbers only: no fractions, no smoothing, no random numbers.
- Give every pixel of the sprite a number from 0 to 99 by scrambling its place in the sprite's own picture: hash(x + y * 4099 + seed * 65537) % 100, with this scramble on unsigned 32-bit whole numbers that wrap around: hash(n): n = (n xor (n >> 16)) * 0x7feb352d; n = (n xor (n >> 15)) * 0x846ca68b; return n xor (n >> 16).
- The time must never go into the scramble. Only a setting "progress" from 0 to 100 moves, and my game sets it.
- line = progress * (100 + edge) / 100 - edge, rounded down. A pixel whose number is below the line is empty; below line + edge it takes the glow colour [a colour of my palette]; otherwise it keeps its colour. Empty pixels stay empty. The edge is [how many numbers glow, for example 8]. The seed is a setting too, so that two copies of the sprite can crumble differently.
Before the shader, write the same rule as a small plain function and test it: at progress 0 the sprite is whole and nothing glows; at 100 nothing is left; at the same progress the same pixels are gone at every tick; a pixel that has gone stays gone while the progress climbs; every pixel glows before it goes. 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
/* Dissolve: a figure crumbles away pixel by pixel, in an order that a scramble decides, and the
pixels that are about to go glow first. Every pixel of the figure gets its number from 0 to 99 from
the scramble of the lesson "Noise you can trust". As `progress` climbs from 0 to 100, a line climbs
through those numbers: a pixel whose number is below the line is gone, and one just above it glows.
The numbers never change from tick to tick, so a pixel that has gone stays gone and the figure
crumbles instead of flickering. Run backwards, the same rule lets her come back.
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 far she has crumbled, from 0 (whole) to 100 (nothing left). */
progress: 0,
/** How many of the hundred numbers glow just before their pixels go. */
edge: 8,
/** Which order: another number lets her crumble in another order. */
seed: 1,
/** The number of the glow's colour in the palette: 23 is light violet. */
glow: 23,
};
export type Params = typeof DEFAULTS;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** The number from 0 to 99 for a place and a seed. */
export function noise(x: number, y: number, seed: number): number {
return hash(x + y * 4099 + seed * 65537) % 100;
}
/** The rule: below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
if (here === 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
const line = Math.floor((p.progress * (100 + p.edge)) / 100) - p.edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
const order = noise(x, y, p.seed);
if (order < line) return 0; // gone
if (order < line + p.edge) return p.glow; // about to go: it glows
return 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 figure of 30 by 30 pixels in one colour, 7, with nothing around it: enough pixels to count. */
const BLOCK: Frame = { width: 32, height: 32, index: new Uint8Array(32 * 32) };
for (let y = 1; y < 31; y++) for (let x = 1; x < 31; x++) BLOCK.index[y * 32 + x] = 7;
const BEETLE = picture([
"0011100",
"0144410",
"1444441",
"0144410",
"0100010",
]);
function whole(src: Frame, tick = 0, params = DEFAULTS): number[] {
const out: number[] = [];
for (let y = 0; y < src.height; y++) for (let x = 0; x < src.width; x++) out.push(shade(x, y, tick, src, params));
return out;
}
/** What became of each pixel: 0 gone, 1 glowing, 2 as it came. */
const states = (src: Frame, answers: number[], glow = DEFAULTS.glow) => answers.map((n, i) => (n === 0 ? 0 : n === glow && src.index[i] !== glow ? 1 : 2));
test("at progress 0 she is whole, and nothing glows", () => {
assert.deepEqual(whole(BEETLE, 0, { ...DEFAULTS, progress: 0 }), [...BEETLE.index]);
assert.deepEqual(whole(BLOCK, 0, { ...DEFAULTS, progress: 0 }), [...BLOCK.index]);
});
test("at progress 100 nothing of her is left", () => {
assert.equal(whole(BLOCK, 0, { ...DEFAULTS, progress: 100 }).filter((n) => n !== 0).length, 0);
assert.equal(whole(BEETLE, 0, { ...DEFAULTS, progress: 100, edge: 30 }).filter((n) => n !== 0).length, 0);
});
test("an empty pixel stays empty, and the glow never leaves her shape", () => {
for (let progress = 0; progress <= 100; progress += 5) {
const answers = whole(BEETLE, 0, { ...DEFAULTS, progress });
BEETLE.index.forEach((n, i) => {
if (n === 0) assert.equal(answers[i], 0, `at progress ${progress}`);
});
}
});
test("at the same progress the same pixels are gone, whatever the tick", () => {
const params = { ...DEFAULTS, progress: 40 };
const atStart = whole(BLOCK, 0, params);
for (let tick = 1; tick <= 120; tick += 1) {
const now = whole(BLOCK, tick, params);
const differ = now.filter((n, i) => n !== atStart[i]).length;
assert.equal(differ, 0, `at tick ${tick}, ${differ} pixels differ from tick 0`);
}
});
test("a pixel that has gone stays gone while the progress climbs", () => {
// as in a game: one step of progress every tick
let gone = new Set<number>();
for (let tick = 0; tick <= 100; tick++) {
const answers = whole(BLOCK, tick, { ...DEFAULTS, progress: tick });
const now = new Set(answers.flatMap((n, i) => (n === 0 && BLOCK.index[i] !== 0 ? [i] : [])));
const back = [...gone].filter((i) => !now.has(i)).length;
assert.equal(back, 0, `at tick ${tick}, ${back} pixels that had gone came back`);
gone = now;
}
});
test("every pixel glows before it goes, and never the other way round", () => {
const seen = Array.from(BLOCK.index, () => [] as number[]);
for (let progress = 0; progress <= 100; progress++) {
states(BLOCK, whole(BLOCK, 0, { ...DEFAULTS, progress })).forEach((s, i) => seen[i].push(s));
}
seen.forEach((steps, i) => {
if (BLOCK.index[i] === 0) return;
const order = [...steps].reverse(); // from progress 100 back to 0: gone, then glowing, then whole
assert.deepEqual(order, [...order].sort((a, b) => a - b), `the pixel at ${i % 32}, ${Math.floor(i / 32)} went out of order`);
assert.ok(steps.includes(1), `the pixel at ${i % 32}, ${Math.floor(i / 32)} never glowed`);
});
});
```
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 progress; // how far she has crumbled, from 0 (whole) to 100 (nothing left)
uniform int edge; // how many of the hundred numbers glow just before their pixels go
uniform int seed; // which order: another number lets her crumble in another order
uniform int glow; // the number of the glow's colour in the palette: 23 is light violet
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}
// below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came
int shade(ivec2 p) {
int here = at(p);
if (here == 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
int line = progress * (100 + edge) / 100 - edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
int order = noise(p, seed);
if (order < line) return 0; // gone
if (order < line + edge) return glow; // about to go: it glows
return 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 the sprite, and give it the sprite's own picture once:
// material.set_shader_parameter("SRC", texture)
uniform sampler2D SRC : filter_nearest;
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 progress; // how far she has crumbled, from 0 (whole) to 100 (nothing left)
uniform int edge; // how many of the hundred numbers glow just before their pixels go
uniform int seed; // which order: another number lets her crumble in another order
uniform int glow; // the number of the glow's colour in the palette: 23 is light violet
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the number from 0 to 99 for a place and a seed
int noise(ivec2 q, int s) {
return int(hash(uint(q.x) + uint(q.y) * 4099u + uint(s) * 65537u) % 100u);
}
// below the line a pixel is gone, just above it the pixel glows, and above that it stays as it came
int shade(ivec2 p) {
int here = at(p);
if (here == 0) return 0; // nothing stays nothing
// the line climbs from -edge (nothing gone, nothing glowing) to 100 (all gone) while progress goes from 0 to 100
int line = progress * (100 + edge) / 100 - edge;
// when this pixel goes: the lower its number, the sooner. Its place decides, never the time
int order = noise(p, seed);
if (order < line) return 0; // gone
if (order < line + edge) return glow; // about to go: it glows
return here;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(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.