Is It Right? Testing a Pixel Art Shader, Pixel for Pixel
Four rules in, it is time for the question that comes with every one of them: how do you know it is right? Not by looking harder. This lesson draws a frame around the picture, the simplest rule in which it is easy to be one pixel off, and goes through the four steps every lesson of this course takes: the rule, its test, the shader, and the comparison of the shader's picture with the rule's.

The rule asks how far each pixel is from the nearest edge. The columns run from 0 to 239 and the rows from 0 to 134, so the frame on the right begins at 240 minus its thickness, and it is as thick there as on the left.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
A frame around the whole picture is an effect of its own. Games use it for a photo mode, for a cut-scene, or as a red edge when the player is hit. The rule is one sentence: every pixel fewer than so many pixels from an edge takes the frame's colour, and everything inside stays as it came.
It is also the right rule to learn checking on, because the place where it can go wrong is easy to see once you know it and easy to miss before. The four steps are the same for every effect:
- The rule as a small function that answers for one pixel.
- Its test: tiny pictures, small enough to count by hand, and what must come out of them.
- The shader: the same rule in a shader language, written line for line from the function.
- The comparison: the shader and the rule are asked for the same pictures, and the pixels in which they differ are counted. There must be none.
How it works
The picture is 240 pixels wide, so its columns are numbered 0 to 239; its rows are 0 to 134. A pixel is in a frame 4 pixels thick if its column is less than 4, on the left, or at least 236, on the right; the same for the rows, at the top and the bottom. 236 is 240 minus 4: the right frame is the columns 236, 237, 238 and 239, which are four.
The test draws a picture of 8 by 6 pixels as rows of digits: a hive in a wood, with sky above and leaves below. With a frame 1 pixel thick, exactly the pixels at the edge must be framed: 8 at the top, 8 at the bottom and 4 on each side between them, 24 in all. That is a number anyone can count. Further tests ask that the frame is as thick on the right as on the left, as thick at the bottom as at the top, and that it reaches the last column and the last row of a picture of any size.
The shader is the same rule in a shader language. The only thing it does differently is ask the picture for its size, where the function is handed it.
For the comparison, our check runs the shader in a browser on pictures of this lesson, reads every pixel back and counts those where the shader's number is not the rule's. "What we checked", further down this page, shows the result of that run.
One slider sets the frame's thickness, from 0 to 12; it starts at 4. The other sets its colour by its number in the palette; it starts at 1, the dark ink.
What goes wrong
The mistake is one pixel short, on the right and at the bottom. An AI writes "more than 240 minus 4" where "at least" was meant, or thinks of the last column as 240 when it is 239. The left and the top come out right, because counting from 0 holds no trap there. On the right and at the bottom the frame is one pixel thinner, and a frame 1 pixel thick has no right side and no bottom at all.
At a thickness of 4 it is hard to see: is the right side 3 pixels or 4? That is why the test counts instead of looking. With the mistake, the count of edge pixels comes out 13 where 24 belong, and the frame is thinner on the right than on the left.
Switch the picture to "With the mistake" while the frame is 1 pixel thick: the frame runs along the top and the left only.
The mistake, line by line
- /* A frame around the whole picture, with the mistake: the frame on the right and at the bottom is
- worked out from the picture's width and height as if the last column were the width itself, with
- "more than" where "at least" was meant. The left and the top are right; the right and the bottom are
- one pixel short, and a frame 1 pixel thick has no right side and no bottom at all.
+ /* A frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
+ the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
+ cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
+ be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
+ its test, the shader, and the shader's picture compared with the rule's.
+ 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, wrong: "more than" on the right and at the bottom, so the frame there is one pixel short. */
+ /** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
+ from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
- const edge = x < p.width || y < p.width || x > src.width - p.width || y > src.height - p.width;
+ const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : at(src, x, y);
}What the test says about the mistake
✖ a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them
got 13, expected 24
✖ the frame is as thick on the right as on the left
a frame 1 thick
got 0, expected 1
✖ the frame is as thick at the bottom as at the top
a frame 1 thick
got 0, expected 1
✔ inside the frame the picture is as it came
✔ a frame 0 pixels thick changes nothing
✔ the frame has the chosen colour
✖ on a picture of any size the frame reaches the last column and the last row
a picture of 3 by 2
got 5, expected 1
7 tests: 3 passed, 4 failedTell your AI
Write a shader for my game that draws a frame around the whole picture: every pixel fewer than [4] pixels from an edge takes the colour [the colour, as a number of my palette or as RGB], and everything else stays as it is.
- My game's picture is [width] by [height] pixels. Columns go from 0 to width - 1 and rows from 0 to height - 1. A pixel is in the frame if its column is less than [4] or at least width - [4], or its row is less than [4] or at least height - [4].
- The frame's thickness and colour must be settings I can change from the game's code. Work in whole pixels and never blend colours.
Work in four steps and show me each one:
1. Write the rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette) and a place, and returns the number for that one pixel.
2. Test it on a tiny picture of 8 by 6 pixels: with a frame 1 pixel thick, exactly the 24 pixels at the edge are framed; the frame is as thick on the right as on the left, and as thick at the bottom as at the top; inside the frame nothing changes. Show me the test's output.
3. Write the shader line for line from the function.
4. Draw the same picture with the shader and with the function, compare them pixel by pixel, and tell me how many pixels differ. It must be 0.
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 frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
its test, the shader, and the shader's picture compared with the rule's.
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 pixels thick the frame is. 0 is no frame. */
width: 4,
/** The number of the frame's colour in the palette: 1 is the darkest ink. */
color: 1,
};
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];
}
/** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : 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 width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : 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 width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : 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 { 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 hive (8) in a wood: sky (4) above, fallen leaves (9) and a path of sand (7) below. 8 pixels wide, 6 tall, small enough to count by hand. */
const WOOD = picture([
"44444444",
"44444444",
"44488444",
"99988999",
"77777777",
"99999999",
]);
/** The whole picture through the rule. */
const whole = (src: Frame, width: number, color = 1) =>
Array.from({ length: src.height }, (_, y) => Array.from({ length: src.width }, (_, x) => shade(x, y, 0, src, { width, color })));
/** How many pixels of a line, from its start, are the frame's colour. */
const run = (line: number[], color = 1) => {
let n = 0;
while (n < line.length && line[n] === color) n++;
return n;
};
test("a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", () => {
const out = whole(WOOD, 1);
const framed = out.flat().filter((n) => n === 1).length;
assert.equal(framed, 24);
for (let x = 0; x < 8; x++) assert.equal(out[5][x], 1, `the bottom row, column ${x}`);
for (let y = 0; y < 6; y++) assert.equal(out[y][7], 1, `the last column, row ${y}`);
});
test("the frame is as thick on the right as on the left", () => {
for (const width of [1, 2, 3]) {
const middle = whole(WOOD, width)[2];
assert.equal(run([...middle].reverse()), run(middle), `a frame ${width} thick`);
}
});
test("the frame is as thick at the bottom as at the top", () => {
for (const width of [1, 2]) {
const column = whole(WOOD, width).map((row) => row[3]);
assert.equal(run([...column].reverse()), run(column), `a frame ${width} thick`);
}
});
test("inside the frame the picture is as it came", () => {
const out = whole(WOOD, 2);
assert.deepEqual(out[2].slice(2, 6), [4, 8, 8, 4]);
assert.deepEqual(out[3].slice(2, 6), [9, 8, 8, 9]);
});
test("a frame 0 pixels thick changes nothing", () => {
assert.deepEqual(whole(WOOD, 0).flat(), [...WOOD.index]);
});
test("the frame has the chosen colour", () => {
assert.equal(shade(0, 3, 0, WOOD, { width: 1, color: 10 }), 10);
assert.equal(shade(4, 0, 0, WOOD, { width: 1, color: 10 }), 10);
});
test("on a picture of any size the frame reaches the last column and the last row", () => {
for (const [w, h] of [[1, 1], [3, 2], [240, 135]]) {
const src: Frame = { width: w, height: h, index: new Uint8Array(w * h).fill(5) };
assert.equal(shade(w - 1, h - 1, 0, src, { width: 1, color: 1 }), 1, `a picture of ${w} by ${h}`);
}
});
- 7 tests pass on the rule.
- With the mistake put in, 4 of them fail: "a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", "the frame is as thick on the right as on the left", "the frame is as thick at the bottom as at the top", "on a picture of any size the frame reaches the last column and the last row".
- 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 the thickness to 1 and switch between "With the rule" and "With the mistake". With the mistake, the frame is gone on the right and at the bottom.
- Set it to 4 and look hard at the right edge with the mistake switched on. It is 3 pixels instead of 4, and it is hard to tell by eye. Then set the colour to 10, coral, the red edge of a hit, and try again.
- Read "What we checked" further down: how many tests passed on the rule, which of them fail with the mistake, and in how many pixels the shader gave the rule's picture.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Does Modelling First Make AI Game Code Correct? We Tested It
Our own measurement: asked to draw a model first, an AI's code passed the tests no more often, and the mistakes it made were small ones of order, rounding and ranges that only running the tests showed.
How your AI reads a PNG file into its real pixels with nothing to install, so that a picture from your own game can be counted and compared pixel for pixel instead of being described.
Why numbering should start at zero
A short note on why a range is best written with its first number in it and the number after its last as the bound left out: the habit behind the last column being the width minus one.
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.
With the mistake, a frame meant to be four pixels thick comes out a pixel thinner on the right, which is hard to see. How does this lesson make sure that such a slip is found?
Next lesson Hit Flash Shader for Pixel Art: White Blinks After a Hit
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/is-it-right
Write a shader for my game that draws a frame around the whole picture: every pixel fewer than [4] pixels from an edge takes the colour [the colour, as a number of my palette or as RGB], and everything else stays as it is.
- My game's picture is [width] by [height] pixels. Columns go from 0 to width - 1 and rows from 0 to height - 1. A pixel is in the frame if its column is less than [4] or at least width - [4], or its row is less than [4] or at least height - [4].
- The frame's thickness and colour must be settings I can change from the game's code. Work in whole pixels and never blend colours.
Work in four steps and show me each one:
1. Write the rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette) and a place, and returns the number for that one pixel.
2. Test it on a tiny picture of 8 by 6 pixels: with a frame 1 pixel thick, exactly the 24 pixels at the edge are framed; the frame is as thick on the right as on the left, and as thick at the bottom as at the top; inside the frame nothing changes. Show me the test's output.
3. Write the shader line for line from the function.
4. Draw the same picture with the shader and with the function, compare them pixel by pixel, and tell me how many pixels differ. It must be 0.
The rule, tested (shader.ts):
```ts
/* A frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
its test, the shader, and the shader's picture compared with the rule's.
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 pixels thick the frame is. 0 is no frame. */
width: 4,
/** The number of the frame's colour in the palette: 1 is the darkest ink. */
color: 1,
};
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];
}
/** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : at(src, x, y);
}
```
Its test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { 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 hive (8) in a wood: sky (4) above, fallen leaves (9) and a path of sand (7) below. 8 pixels wide, 6 tall, small enough to count by hand. */
const WOOD = picture([
"44444444",
"44444444",
"44488444",
"99988999",
"77777777",
"99999999",
]);
/** The whole picture through the rule. */
const whole = (src: Frame, width: number, color = 1) =>
Array.from({ length: src.height }, (_, y) => Array.from({ length: src.width }, (_, x) => shade(x, y, 0, src, { width, color })));
/** How many pixels of a line, from its start, are the frame's colour. */
const run = (line: number[], color = 1) => {
let n = 0;
while (n < line.length && line[n] === color) n++;
return n;
};
test("a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", () => {
const out = whole(WOOD, 1);
const framed = out.flat().filter((n) => n === 1).length;
assert.equal(framed, 24);
for (let x = 0; x < 8; x++) assert.equal(out[5][x], 1, `the bottom row, column ${x}`);
for (let y = 0; y < 6; y++) assert.equal(out[y][7], 1, `the last column, row ${y}`);
});
test("the frame is as thick on the right as on the left", () => {
for (const width of [1, 2, 3]) {
const middle = whole(WOOD, width)[2];
assert.equal(run([...middle].reverse()), run(middle), `a frame ${width} thick`);
}
});
test("the frame is as thick at the bottom as at the top", () => {
for (const width of [1, 2]) {
const column = whole(WOOD, width).map((row) => row[3]);
assert.equal(run([...column].reverse()), run(column), `a frame ${width} thick`);
}
});
test("inside the frame the picture is as it came", () => {
const out = whole(WOOD, 2);
assert.deepEqual(out[2].slice(2, 6), [4, 8, 8, 4]);
assert.deepEqual(out[3].slice(2, 6), [9, 8, 8, 9]);
});
test("a frame 0 pixels thick changes nothing", () => {
assert.deepEqual(whole(WOOD, 0).flat(), [...WOOD.index]);
});
test("the frame has the chosen colour", () => {
assert.equal(shade(0, 3, 0, WOOD, { width: 1, color: 10 }), 10);
assert.equal(shade(4, 0, 0, WOOD, { width: 1, color: 10 }), 10);
});
test("on a picture of any size the frame reaches the last column and the last row", () => {
for (const [w, h] of [[1, 1], [3, 2], [240, 135]]) {
const src: Frame = { width: w, height: h, index: new Uint8Array(w * h).fill(5) };
assert.equal(shade(w - 1, h - 1, 0, src, { width: 1, color: 1 }), 1, `a picture of ${w} by ${h}`);
}
});
```
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 width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : 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 width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : 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.