Pixel Art Screen Transition Shader: A Dithered Wipe
A screen transition takes the player from one picture to the next: out of the snow and into the glasshouse, from the map into a level. This lesson's rule does it with a wipe. An edge sweeps across the screen as progress goes from 0 to 100, and along the edge the two pictures are mixed pixel by pixel by the 4 by 4 pattern, so the edge looks soft without a single blended colour.

The winter yard is the old picture, the glasshouse the new one. An edge sweeps across as progress goes from 0 to 100; in the soft edge behind it each pixel is taken whole from one picture or the other, by the 4 by 4 pattern. At 100 not one pixel of snow is left.
Also in this lesson 4 things to read up on 3 questions to check yourself
The idea
A player meets transitions at doors, at the start of a fight and on the way into a menu. They hide the moment one scene is swapped for another, and a good one also tells the player which way they went.
Pixel art cannot simply fade one picture into the other. Halfway between the deep blue of the winter evening and the pale green of the glasshouse lies a colour that is not in the palette, so a blend invents colours. A dither does the same job with whole pixels: each pixel is taken whole from one picture or from the other, and across the edge the share of new pixels grows. From a little distance the eye mixes them.
The rule knows nothing of time. The game hands it progress and moves that number on, in the picture above from 0 to 100 in 120 ticks. So the game decides how fast the wipe runs, can hold it halfway, or run it backwards.
How it works
For each pixel the rule works out three things.
Where is the edge? It is progress times the picture's width plus the soft edge, divided by 100. At 0 the edge stands at the left side. At 100 it has gone past the right side by the soft edge's own width.
How far past this pixel is the edge? That is the edge's place minus the pixel's column. At 0 or below, the edge has not reached the pixel, and the old picture stays.
Has it come far enough for this pixel? Every pixel has a number from 0 to 15 from the 4 by 4 pattern, chosen by its column and its row. The pixel turns to the new picture when the distance times 16 is more than its number times the width of the soft edge. A pixel with 0 turns as soon as the edge reaches it, one with 15 only when the edge is almost a whole soft edge past. Once the edge is a whole soft edge past, every pixel has turned.
Each pixel's moment is fixed by its place, so it turns once and stays turned. When progress goes back down, the pixels turn back in the opposite order.
The sliders are progress, from 0 for the old picture to 100 for the new one (50 when nobody touches it; the picture holds 0 for a second, wipes in for two, holds 100 for two and wipes back in one), and soft, how many pixels wide the edge is (32; at 1 the edge is hard).
What goes wrong
At 100 a strip of the old picture is left at the right side. The AI moves the edge from 0 to the picture's width, which is what "progress" seems to mean, and forgets that the soft edge trails behind the edge. At 100 the edge stands at the right side, and the soft edge behind it is still on the screen, half old and half new. It is the obvious way to write a soft wipe, and it is easy to miss, because halfway through it looks perfect.
In a game this shows as a ragged strip of the old room that lingers at the side of the screen, then jumps away when the transition is switched off.
Switch the picture to "With the mistake" and wait for the glasshouse. A dithered strip of the winter evening stays at the right side, dark sky over the yellow glow of the glasshouse front, for as long as the picture holds at 100.
Two tests catch it. "At 100 every pixel is the new picture, however wide the soft edge" finds a pixel at the right side that is still from the old picture. "Every pixel turns once on the way from 0 to 100 and never turns back" finds a pixel that never turned at all.
The mistake, line by line
- /* A screen transition, with the mistake: the edge sweeps only across the picture's width and forgets
- that the soft edge trails behind it. At 100 the edge stands at the right side of the screen, and the
- soft edge, still on the screen, leaves a dithered strip of the old picture there. Everything else is
- the same as in shader.ts.
+ /* A screen transition: a wipe from one picture to the next, with a dithered edge. An edge sweeps
+ across the screen from left to right as `progress` goes from 0 to 100. Behind it the new picture
+ stands, ahead of it the old one, and in between, for `soft` pixels, the two are mixed by the 4 by 4
+ pattern: a pixel turns once the edge has passed it by its number in the pattern, in sixteenths of
+ the soft edge.
+ 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. It takes each pixel
+ whole from one picture or the other and never blends them.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: the new picture where the edge has passed far enough, but the edge stops one soft edge short. */
+ /** The rule: the new picture where the edge has passed far enough, the old one everywhere else. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, other: Frame = src): number {
- // the mistake: the edge comes only as far as the picture's width at 100, and the soft edge is still on it
- const edge = Math.floor((p.progress * src.width) / 100);
+ // how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
+ const edge = Math.floor((p.progress * (src.width + p.soft)) / 100);
// how far the edge is past this pixel: below 0 it has not come yet
const past = edge - x;What the test says about the mistake
✔ at 0 every pixel is the old picture
✖ at 100 every pixel is the new picture, however wide the soft edge
at 11, 0, soft 4: still the old picture
got 2, expected 4
✖ every pixel turns once on the way from 0 to 100 and never turns back
at 11, 0, soft 4: turned 0 times
got 0, expected 1
✔ every pixel is taken whole from one picture or the other
✔ the wipe runs from left to right: going right, each strip 4 pixels wide holds no more of the new picture
✔ in the soft edge a column holds both pictures, pixel by pixel; with a soft edge of 1 none does
6 tests: 4 passed, 2 failedTell your AI
Write a screen transition shader for my pixel art game: a wipe from left to right, from the old screen to the new one, with a dithered edge.
- Whole pixels only and no blending: every pixel is taken whole from the old picture or from the new one.
- My game sets progress, a whole number from 0 (all old) to 100 (all new). The edge is [32] pixels wide.
- Use the 4 by 4 dither pattern 0, 8, 2, 10 / 12, 4, 14, 6 / 3, 11, 1, 9 / 15, 7, 13, 5, picked by the pixel's column and row, each taken modulo 4.
- The edge must travel the screen's width plus its own width, so that at 100 not one pixel of the old picture is left, and at 0 not one of the new.
Before you write the shader, write the same rule as a small plain function that takes a place and progress and returns which picture to show there. Test it: at 0 every pixel is old, at 100 every pixel is new for an edge of 1, [32] and 80 pixels, and every pixel turns once and never turns back. 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
/* A screen transition: a wipe from one picture to the next, with a dithered edge. An edge sweeps
across the screen from left to right as `progress` goes from 0 to 100. Behind it the new picture
stands, ahead of it the old one, and in between, for `soft` pixels, the two are mixed by the 4 by 4
pattern: a pixel turns once the edge has passed it by its number in the pattern, in sixteenths of
the soft edge.
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. It takes each pixel
whole from one picture or the other and never blends them.
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 the change has come: 0 is the old picture, 100 the new one. */
progress: 50,
/** How many pixels wide the dithered edge is. */
soft: 32,
};
export type Params = typeof DEFAULTS;
/** The 4 by 4 pattern for dithering: 0 to 15, each once. */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: the new picture where the edge has passed far enough, the old one everywhere else. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, other: Frame = src): number {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
const edge = Math.floor((p.progress * (src.width + p.soft)) / 100);
// how far the edge is past this pixel: below 0 it has not come yet
const past = edge - x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(y & 3) * 4 + (x & 3)] * p.soft ? at(other, x, y) : at(src, x, y);
}
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 the change has come: 0 is the old picture, 100 the new one
uniform int soft; // how many pixels wide the dithered edge is
// the 4 by 4 pattern for dithering: 0 to 15, each once
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the new picture where the edge has passed far enough, the old one everywhere else
int shade(ivec2 p) {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
int edge = (progress * (textureSize(SRC, 0).x + soft)) / 100;
// how far the edge is past this pixel: below 0 it has not come yet
int past = edge - p.x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(p.y & 3) * 4 + (p.x & 3)] * soft ? otherAt(p) : at(p);
}
// ---- 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 progress; // how far the change has come: 0 is the old picture, 100 the new one
uniform int soft; // how many pixels wide the dithered edge is
// the 4 by 4 pattern for dithering: 0 to 15, each once
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the new picture where the edge has passed far enough, the old one everywhere else
int shade(ivec2 p) {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
int edge = (progress * (textureSize(SRC, 0).x + soft)) / 100;
// how far the edge is past this pixel: below 0 it has not come yet
int past = edge - p.x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(p.y & 3) * 4 + (p.x & 3)] * soft ? otherAt(p) : at(p);
}
// ---- 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) };
}
/** The old picture has only 1 and 2 in it, the new one only 3 and 4, so every pixel tells where it came from. */
const OLD = picture(["121212121212", "212121212121", "121212121212", "212121212121", "121212121212", "212121212121", "121212121212", "212121212121"]);
const NEW = picture(["343434343434", "343434343434", "434343434343", "434343434343", "343434343434", "343434343434", "434343434343", "434343434343"]);
const SOFTS = [1, 4, 8, 32, 80];
const isNew = (x: number, y: number, progress: number, soft = 4) => shade(x, y, 0, OLD, { progress, soft }, NEW) >= 3;
const everyPixel = (check: (x: number, y: number) => void) => {
for (let y = 0; y < OLD.height; y++) for (let x = 0; x < OLD.width; x++) check(x, y);
};
test("at 0 every pixel is the old picture", () => {
for (const soft of SOFTS) everyPixel((x, y) => assert.equal(shade(x, y, 0, OLD, { progress: 0, soft }, NEW), OLD.index[y * OLD.width + x], `at ${x}, ${y}, soft ${soft}`));
});
test("at 100 every pixel is the new picture, however wide the soft edge", () => {
for (const soft of SOFTS) everyPixel((x, y) => assert.equal(shade(x, y, 0, OLD, { progress: 100, soft }, NEW), NEW.index[y * NEW.width + x], `at ${x}, ${y}, soft ${soft}: still the old picture`));
});
test("every pixel turns once on the way from 0 to 100 and never turns back", () => {
for (const soft of SOFTS) {
everyPixel((x, y) => {
let turns = 0;
for (let progress = 1; progress <= 100; progress++) {
const was = isNew(x, y, progress - 1, soft);
const now = isNew(x, y, progress, soft);
assert.ok(!(was && !now), `at ${x}, ${y}, soft ${soft}: back to the old picture at ${progress}`);
if (!was && now) turns++;
}
assert.equal(turns, 1, `at ${x}, ${y}, soft ${soft}: turned ${turns} times`);
});
}
});
test("every pixel is taken whole from one picture or the other", () => {
for (let progress = 0; progress <= 100; progress += 5) {
everyPixel((x, y) => {
const n = shade(x, y, 0, OLD, { ...DEFAULTS, progress, soft: 8 }, NEW);
assert.ok(n === OLD.index[y * OLD.width + x] || n === NEW.index[y * NEW.width + x], `${n} at ${x}, ${y}`);
});
}
});
test("the wipe runs from left to right: going right, each strip 4 pixels wide holds no more of the new picture", () => {
for (const soft of [4, 8, 32]) {
for (let progress = 0; progress <= 100; progress++) {
const strips = [0, 1, 2].map((s) => {
let count = 0;
for (let y = 0; y < OLD.height; y++) for (let x = s * 4; x < s * 4 + 4; x++) if (isNew(x, y, progress, soft)) count++;
return count;
});
assert.ok(strips[0] >= strips[1] && strips[1] >= strips[2], `soft ${soft}, progress ${progress}: new pixels in the strips ${strips.join(", ")}`);
}
}
});
test("in the soft edge a column holds both pictures, pixel by pixel; with a soft edge of 1 none does", () => {
const mixedColumns = (soft: number) => {
let mixed = 0;
for (let progress = 0; progress <= 100; progress++) {
for (let x = 0; x < OLD.width; x++) {
const column = Array.from({ length: OLD.height }, (_, y) => isNew(x, y, progress, soft));
if (column.some(Boolean) && !column.every(Boolean)) mixed++;
}
}
return mixed;
};
assert.ok(mixedColumns(32) > 0, "with a soft edge of 32 no column ever held both pictures");
assert.equal(mixedColumns(1), 0, "with a soft edge of 1 the edge should be hard");
});
- 6 tests pass on the rule.
- With the mistake put in, 2 of them fail: "at 100 every pixel is the new picture, however wide the soft edge", "every pixel turns once on the way from 0 to 100 and never turns back".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 12 pictures and gave the rule's picture in 388,800 of 388,800 pixels.
Try it
- Set
softto 1 and moveprogress: a hard line crosses the screen. Then setsoftto 80: the edge covers a third of the screen, and the two places melt into each other in a fine pattern. - Hold
progressstill in the middle and look closely at the soft edge. Next to the old picture only a few single pixels are new; towards the new picture, only a few are old. - Switch to "With the mistake", pull
progressto 100 and see the strip at the right. Now setsoftto 1: the strip is gone. With a hard edge the mistake hides, which is how it gets through when nobody tries a soft one.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Dithering in a Pixel Art Shader: Two Colours in 16 Steps
The 4 by 4 pattern this wipe uses, the name your AI knows it by, and why a pixel that has turned stays turned while the share grows: what keeps this wipe from flickering back on its way.
When a game needs a transition at all: a room camera slides to the next room in a set time and, when the character lands far away, fades out, moves and fades in. That fade is where a wipe can stand.
Curves for how the game moves progress, so the wipe starts slowly and lands softly, or jumps in steps for a retro look. Each one ends exactly at its end, so the wipe really arrives.
How a Godot 4 game can run a number like this lesson's progress: a Tween moves a value to its end in a set time, waits for an interval, pauses with the game and says when it is done.
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 this wipe mix the old and the new picture with a dither pattern instead of fading one into the other?
Next lesson Scanlines Shader for Pixel Art, and When to Leave It Off
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/transitions
Write a screen transition shader for my pixel art game: a wipe from left to right, from the old screen to the new one, with a dithered edge.
- Whole pixels only and no blending: every pixel is taken whole from the old picture or from the new one.
- My game sets progress, a whole number from 0 (all old) to 100 (all new). The edge is [32] pixels wide.
- Use the 4 by 4 dither pattern 0, 8, 2, 10 / 12, 4, 14, 6 / 3, 11, 1, 9 / 15, 7, 13, 5, picked by the pixel's column and row, each taken modulo 4.
- The edge must travel the screen's width plus its own width, so that at 100 not one pixel of the old picture is left, and at 0 not one of the new.
Before you write the shader, write the same rule as a small plain function that takes a place and progress and returns which picture to show there. Test it: at 0 every pixel is old, at 100 every pixel is new for an edge of 1, [32] and 80 pixels, and every pixel turns once and never turns back. Show me the tests' output. Then write the shader so that it does exactly what the function does.
The rule, tested (shader.ts):
```ts
/* A screen transition: a wipe from one picture to the next, with a dithered edge. An edge sweeps
across the screen from left to right as `progress` goes from 0 to 100. Behind it the new picture
stands, ahead of it the old one, and in between, for `soft` pixels, the two are mixed by the 4 by 4
pattern: a pixel turns once the edge has passed it by its number in the pattern, in sixteenths of
the soft edge.
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. It takes each pixel
whole from one picture or the other and never blends them.
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 the change has come: 0 is the old picture, 100 the new one. */
progress: 50,
/** How many pixels wide the dithered edge is. */
soft: 32,
};
export type Params = typeof DEFAULTS;
/** The 4 by 4 pattern for dithering: 0 to 15, each once. */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: the new picture where the edge has passed far enough, the old one everywhere else. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, other: Frame = src): number {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
const edge = Math.floor((p.progress * (src.width + p.soft)) / 100);
// how far the edge is past this pixel: below 0 it has not come yet
const past = edge - x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(y & 3) * 4 + (x & 3)] * p.soft ? at(other, x, y) : at(src, x, y);
}
```
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) };
}
/** The old picture has only 1 and 2 in it, the new one only 3 and 4, so every pixel tells where it came from. */
const OLD = picture(["121212121212", "212121212121", "121212121212", "212121212121", "121212121212", "212121212121", "121212121212", "212121212121"]);
const NEW = picture(["343434343434", "343434343434", "434343434343", "434343434343", "343434343434", "343434343434", "434343434343", "434343434343"]);
const SOFTS = [1, 4, 8, 32, 80];
const isNew = (x: number, y: number, progress: number, soft = 4) => shade(x, y, 0, OLD, { progress, soft }, NEW) >= 3;
const everyPixel = (check: (x: number, y: number) => void) => {
for (let y = 0; y < OLD.height; y++) for (let x = 0; x < OLD.width; x++) check(x, y);
};
test("at 0 every pixel is the old picture", () => {
for (const soft of SOFTS) everyPixel((x, y) => assert.equal(shade(x, y, 0, OLD, { progress: 0, soft }, NEW), OLD.index[y * OLD.width + x], `at ${x}, ${y}, soft ${soft}`));
});
test("at 100 every pixel is the new picture, however wide the soft edge", () => {
for (const soft of SOFTS) everyPixel((x, y) => assert.equal(shade(x, y, 0, OLD, { progress: 100, soft }, NEW), NEW.index[y * NEW.width + x], `at ${x}, ${y}, soft ${soft}: still the old picture`));
});
test("every pixel turns once on the way from 0 to 100 and never turns back", () => {
for (const soft of SOFTS) {
everyPixel((x, y) => {
let turns = 0;
for (let progress = 1; progress <= 100; progress++) {
const was = isNew(x, y, progress - 1, soft);
const now = isNew(x, y, progress, soft);
assert.ok(!(was && !now), `at ${x}, ${y}, soft ${soft}: back to the old picture at ${progress}`);
if (!was && now) turns++;
}
assert.equal(turns, 1, `at ${x}, ${y}, soft ${soft}: turned ${turns} times`);
});
}
});
test("every pixel is taken whole from one picture or the other", () => {
for (let progress = 0; progress <= 100; progress += 5) {
everyPixel((x, y) => {
const n = shade(x, y, 0, OLD, { ...DEFAULTS, progress, soft: 8 }, NEW);
assert.ok(n === OLD.index[y * OLD.width + x] || n === NEW.index[y * NEW.width + x], `${n} at ${x}, ${y}`);
});
}
});
test("the wipe runs from left to right: going right, each strip 4 pixels wide holds no more of the new picture", () => {
for (const soft of [4, 8, 32]) {
for (let progress = 0; progress <= 100; progress++) {
const strips = [0, 1, 2].map((s) => {
let count = 0;
for (let y = 0; y < OLD.height; y++) for (let x = s * 4; x < s * 4 + 4; x++) if (isNew(x, y, progress, soft)) count++;
return count;
});
assert.ok(strips[0] >= strips[1] && strips[1] >= strips[2], `soft ${soft}, progress ${progress}: new pixels in the strips ${strips.join(", ")}`);
}
}
});
test("in the soft edge a column holds both pictures, pixel by pixel; with a soft edge of 1 none does", () => {
const mixedColumns = (soft: number) => {
let mixed = 0;
for (let progress = 0; progress <= 100; progress++) {
for (let x = 0; x < OLD.width; x++) {
const column = Array.from({ length: OLD.height }, (_, y) => isNew(x, y, progress, soft));
if (column.some(Boolean) && !column.every(Boolean)) mixed++;
}
}
return mixed;
};
assert.ok(mixedColumns(32) > 0, "with a soft edge of 32 no column ever held both pictures");
assert.equal(mixedColumns(1), 0, "with a soft edge of 1 the edge should be hard");
});
```
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 the change has come: 0 is the old picture, 100 the new one
uniform int soft; // how many pixels wide the dithered edge is
// the 4 by 4 pattern for dithering: 0 to 15, each once
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the new picture where the edge has passed far enough, the old one everywhere else
int shade(ivec2 p) {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
int edge = (progress * (textureSize(SRC, 0).x + soft)) / 100;
// how far the edge is past this pixel: below 0 it has not come yet
int past = edge - p.x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(p.y & 3) * 4 + (p.x & 3)] * soft ? otherAt(p) : at(p);
}
// ---- 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 progress; // how far the change has come: 0 is the old picture, 100 the new one
uniform int soft; // how many pixels wide the dithered edge is
// the 4 by 4 pattern for dithering: 0 to 15, each once
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the new picture where the edge has passed far enough, the old one everywhere else
int shade(ivec2 p) {
// how far the edge has come: from 0 at the start to the picture's width and the soft edge's own at 100
int edge = (progress * (textureSize(SRC, 0).x + soft)) / 100;
// how far the edge is past this pixel: below 0 it has not come yet
int past = edge - p.x;
// the pixel turns once the edge is past it by its number in the pattern, counted in sixteenths of the soft edge
return past * 16 > BAYER[(p.y & 3) * 4 + (p.x & 3)] * soft ? otherAt(p) : at(p);
}
// ---- 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.