Outline Shader for Pixel Art: One Pixel Around the Sprite
An outline is a line one pixel wide around a figure, in a single colour, often the darkest of the palette. It keeps her readable in front of a busy or dark world, and it marks the figure the player has chosen or the thing that can be picked up. The rule draws it on the empty pixels right beside her, so she keeps every pixel she was drawn with, and that is also why her picture needs a pixel of empty room all round.

Only the beekeeper is given to the rule. Every empty pixel that shares a side with her becomes the outline; she keeps all her own pixels. At the top of the ladder there is no row above her hat, so the outline has no room there.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
Look at the picture: an orchard at dusk, a ladder, the green crown of a tall tree. The beekeeper's cream jacket and blue trousers would blur into all that, and the dark line around her keeps her shape clear wherever she goes. Games use the same line to show which figure is chosen, which chest can be opened, or where a figure is when she stands behind something.
An artist can draw the outline into the figure by hand. Then it is part of every frame of every animation, and a new colour for it means changing every frame. As a rule, the drawings stay without an outline, and the colour is one number the game can change at any moment: dark in the orchard, light in a cave, gold for the figure that is chosen.
How it works
The rule looks at the number at its own place. If something is drawn there, the answer is that number: the outline never paints over her. If the place is empty, the rule asks its four neighbours that share a side with it: left, right, above and below. If one of them is drawn, the answer is the outline's colour. Otherwise the place stays empty.
The corners are not asked. A single drawn dot therefore gets a plus of four outline pixels around it, not a square of eight. Around her, the line stays one pixel wide, follows her shape, and leaves the corners of her hat and boots a little round.
The slider sets the outline's colour by its number in the palette; it starts at 1, the dark ink.
The outline lies outside her, on pixels that were empty. So her picture needs at least one empty pixel on every side, or there is nowhere to draw it. In the picture above the rule is given a picture as large as the whole stage, so she has room everywhere, except at the top of the ladder: there her hat touches the top row, there is no row above it, and the outline is missing above her hat. In your game the picture the rule gets is her own sprite. Sprites are often cut tight around the figure, and then the outline is missing wherever she touches the sprite's edge: under her boots, on top of her hat, at the tip of a raised arm.
What goes wrong
Outline shaders that are passed around often look in eight directions, and an AI asked for an outline tends to write one of those. With eight neighbours, a pixel that touches her only at a corner becomes outline as well. Some artists draw a heavy outline with square corners on purpose, and for that, eight is right. For a line one pixel wide that follows her shape, it is the mistake.
Switch the picture to "With the mistake" and watch her walk. The line grows heavier, the corners of her hat brim turn square, her slanted legs get a thick staircase along each side, and mid-step the narrow gap between her legs fills up, so for a moment they look like one dark block.
Two tests catch it: "a pixel that touches her only at a corner stays empty", and "a single dot gets an outline of four pixels, a plus around it". With the mistake the dot gets a square of eight.
The mistake, line by line
- /* Outline, with the mistake: the rule asks all eight neighbours, the four at the corners too. A pixel
- that touches the figure only at a corner becomes outline as well. The outline grows heavy, its
- corners turn square, every slanted edge gets a thick staircase, and small gaps, such as the one
- between her legs, fill up.
+ /* Outline: an empty pixel right beside the figure becomes the outline's colour, and the figure keeps
+ every pixel she was drawn with. It makes her stand out against a busy or dark world, and it marks
+ the one the player has chosen or can pick up.
+ "Beside" means left, right, above or below: four neighbours. A pixel that touches her only at a
+ corner stays empty, so the outline follows her shape one pixel wide and her corners stay round.
+ Because the outline lies on empty pixels outside her, her picture needs one empty pixel of room on
+ every side: where she touches its edge, there is no pixel left to draw it on.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: an empty pixel with her anywhere around it, corners included, becomes the outline. */
+ /** The rule: she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n !== 0) return n; // a drawn pixel stays as it was drawn
- // wrong: all eight neighbours, the four corners too
- let beside = 0;
- for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) beside += at(src, x + dx, y + dy);
+ // the four neighbours that share a side with this pixel; the corners are not asked
+ const beside = at(src, x - 1, y) + at(src, x + 1, y) + at(src, x, y - 1) + at(src, x, y + 1);
return beside > 0 ? p.color : 0;
}What the test says about the mistake
✔ every drawn pixel keeps its own colour
✔ an empty pixel beside a drawn one becomes the outline
✖ a pixel that touches her only at a corner stays empty
the corner at 1, 1
got 1, expected 0
✖ a single dot gets an outline of four pixels, a plus around it
got [ '00000', '0###0', '0#3#0', '0###0', '00000' ], expected [ '00000', '00#00', '0#3#0', '00#00', '00000' ]
✔ an empty pixel two steps away stays empty
✖ with one pixel of room on every side, the outline goes all the way round
got [ '#####', '#386#', '#242#', '#####' ], expected [ '0###0', '#386#', '#242#', '0###0' ]
✔ where she touches the edge of her picture, the outline has no room
✔ the outline is drawn in the chosen colour
8 tests: 5 passed, 3 failedTell your AI
Write an outline shader for my pixel art sprite: a line one pixel wide around the figure, in one colour.
- Work on whole pixels: no smoothing and no blending. Sample the sprite's texture with nearest filtering.
- The outline goes on empty pixels (alpha 0) only. Every drawn pixel keeps its own colour.
- An empty pixel becomes the outline if one of the four neighbours that share a side with it (left, right, above, below) is drawn. Do not look at the four corners: a single drawn pixel gets a plus of 4 outline pixels around it, not a square of 8.
- The outline's colour must be a setting I can change from the game's code: [the colour, as a number of my palette or as RGB].
- The outline needs room: check that my sprite has at least 1 empty pixel on every side, in every frame. If it has not, tell me which frames touch the edge, then add the room or let the sprite's area grow by 1 pixel on each side.
Before you write the shader, write the same 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. Test it: a drawn pixel keeps its colour, a pixel that touches the figure only at a corner stays empty, and a single dot gets an outline of exactly 4 pixels. 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
/* Outline: an empty pixel right beside the figure becomes the outline's colour, and the figure keeps
every pixel she was drawn with. It makes her stand out against a busy or dark world, and it marks
the one the player has chosen or can pick up.
"Beside" means left, right, above or below: four neighbours. A pixel that touches her only at a
corner stays empty, so the outline follows her shape one pixel wide and her corners stay round.
Because the outline lies on empty pixels outside her, her picture needs one empty pixel of room on
every side: where she touches its edge, there is no pixel left to draw it on.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** The number of the outline's colour in the palette: 1 is the dark 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: she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n !== 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
const beside = at(src, x - 1, y) + at(src, x + 1, y) + at(src, x, y - 1) + at(src, x, y + 1);
return beside > 0 ? p.color : 0;
}
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 color; // the number of the outline's colour in the palette: 1 is the dark ink
// she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline
int shade(ivec2 p) {
int n = at(p);
if (n != 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
int beside = at(p + ivec2(-1, 0)) + at(p + ivec2(1, 0)) + at(p + ivec2(0, -1)) + at(p + ivec2(0, 1));
return beside > 0 ? color : 0;
}
// ---- 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 color; // the number of the outline's colour in the palette: 1 is the dark ink
// she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline
int shade(ivec2 p) {
int n = at(p);
if (n != 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
int beside = at(p + ivec2(-1, 0)) + at(p + ivec2(1, 0)) + at(p + ivec2(0, -1)) + at(p + ivec2(0, 1));
return beside > 0 ? color : 0;
}
// ---- 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) };
}
/** The whole picture after the rule, as rows of digits, with the outline drawn as "#". */
function after(src: Frame, color = 9): string[] {
const rows: string[] = [];
for (let y = 0; y < src.height; y++) {
let row = "";
for (let x = 0; x < src.width; x++) {
const n = shade(x, y, 0, src, { ...DEFAULTS, color });
row += n === color && src.index[y * src.width + x] === 0 ? "#" : String(n);
}
rows.push(row);
}
return rows;
}
const DOT = picture([
"00000",
"00000",
"00300",
"00000",
"00000",
]);
const BOX = picture([
"00000",
"03860",
"02420",
"00000",
]);
test("every drawn pixel keeps its own colour", () => {
assert.deepEqual(after(BOX).map((row) => row.replace(/#/g, "0")), ["00000", "03860", "02420", "00000"]);
});
test("an empty pixel beside a drawn one becomes the outline", () => {
assert.equal(shade(2, 1, 0, DOT), DEFAULTS.color, "above");
assert.equal(shade(2, 3, 0, DOT), DEFAULTS.color, "below");
assert.equal(shade(1, 2, 0, DOT), DEFAULTS.color, "to the left");
assert.equal(shade(3, 2, 0, DOT), DEFAULTS.color, "to the right");
});
test("a pixel that touches her only at a corner stays empty", () => {
for (const [x, y] of [[1, 1], [3, 1], [1, 3], [3, 3]]) assert.equal(shade(x, y, 0, DOT), 0, `the corner at ${x}, ${y}`);
assert.equal(shade(0, 0, 0, BOX), 0, "the corner of the box");
});
test("a single dot gets an outline of four pixels, a plus around it", () => {
assert.deepEqual(after(DOT), ["00000", "00#00", "0#3#0", "00#00", "00000"]);
});
test("an empty pixel two steps away stays empty", () => {
assert.equal(shade(2, 0, 0, DOT), 0);
assert.equal(shade(0, 2, 0, DOT), 0);
assert.equal(shade(4, 4, 0, DOT), 0);
});
test("with one pixel of room on every side, the outline goes all the way round", () => {
assert.deepEqual(after(BOX), ["0###0", "#386#", "#242#", "0###0"]);
});
test("where she touches the edge of her picture, the outline has no room", () => {
// the same box cut tight: no empty pixel is left beside it, so there is no outline at all
const tight = picture(["386", "242"]);
assert.deepEqual(after(tight), ["386", "242"]);
});
test("the outline is drawn in the chosen colour", () => {
assert.equal(shade(2, 1, 0, DOT, { color: 1 }), 1);
assert.equal(shade(2, 1, 0, DOT, { color: 25 }), 25);
});
- 8 tests pass on the rule.
- With the mistake put in, 3 of them fail: "a pixel that touches her only at a corner stays empty", "a single dot gets an outline of four pixels, a plus around it", "with one pixel of room on every side, the outline goes all the way round".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 10 pictures and gave the rule's picture in 324,000 of 324,000 pixels.
Try it
- Move the colour slider through the palette. The ink stands out against the dusk; some colours almost vanish against the sky or the tree. Choose the outline's colour for the world your figure stands in, not for the figure.
- Switch between "With the rule" and "With the mistake" while she walks. Watch her legs: with the rule there is a gap between them in mid-step, with the mistake it is filled with outline.
- Pause when she is at the top of the ladder and look above her hat. There is no line there, with the rule and with the mistake alike: no rule can draw on a pixel that her picture does not have.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Measures a sheet of frames before it goes into your game and names three things that spoil an outline: a background painted instead of see-through, a figure running across the cut, and soft edge pixels.
How long a figure's edge is, counted by the four sides of each pixel as this outline sees it, and how much rougher three ways of turning a sprite make that edge, so you can choose one by numbers.
The Order of Shader Effects in a Pixel Art Game
Where the outline goes among a game's effects: on the sprite itself, with the flash and the swap, before the world, the light and the night, so the night darkens her outline along with her.
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.
An outline rule is asked about one pixel. When does it answer with the outline's colour?
Next lesson Shader Noise for Pixel Art: The Same Stars on Every Machine
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/outline
Write an outline shader for my pixel art sprite: a line one pixel wide around the figure, in one colour.
- Work on whole pixels: no smoothing and no blending. Sample the sprite's texture with nearest filtering.
- The outline goes on empty pixels (alpha 0) only. Every drawn pixel keeps its own colour.
- An empty pixel becomes the outline if one of the four neighbours that share a side with it (left, right, above, below) is drawn. Do not look at the four corners: a single drawn pixel gets a plus of 4 outline pixels around it, not a square of 8.
- The outline's colour must be a setting I can change from the game's code: [the colour, as a number of my palette or as RGB].
- The outline needs room: check that my sprite has at least 1 empty pixel on every side, in every frame. If it has not, tell me which frames touch the edge, then add the room or let the sprite's area grow by 1 pixel on each side.
Before you write the shader, write the same 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. Test it: a drawn pixel keeps its colour, a pixel that touches the figure only at a corner stays empty, and a single dot gets an outline of exactly 4 pixels. 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
/* Outline: an empty pixel right beside the figure becomes the outline's colour, and the figure keeps
every pixel she was drawn with. It makes her stand out against a busy or dark world, and it marks
the one the player has chosen or can pick up.
"Beside" means left, right, above or below: four neighbours. A pixel that touches her only at a
corner stays empty, so the outline follows her shape one pixel wide and her corners stay round.
Because the outline lies on empty pixels outside her, her picture needs one empty pixel of room on
every side: where she touches its edge, there is no pixel left to draw it on.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** The number of the outline's colour in the palette: 1 is the dark 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: she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n !== 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
const beside = at(src, x - 1, y) + at(src, x + 1, y) + at(src, x, y - 1) + at(src, x, y + 1);
return beside > 0 ? p.color : 0;
}
```
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 whole picture after the rule, as rows of digits, with the outline drawn as "#". */
function after(src: Frame, color = 9): string[] {
const rows: string[] = [];
for (let y = 0; y < src.height; y++) {
let row = "";
for (let x = 0; x < src.width; x++) {
const n = shade(x, y, 0, src, { ...DEFAULTS, color });
row += n === color && src.index[y * src.width + x] === 0 ? "#" : String(n);
}
rows.push(row);
}
return rows;
}
const DOT = picture([
"00000",
"00000",
"00300",
"00000",
"00000",
]);
const BOX = picture([
"00000",
"03860",
"02420",
"00000",
]);
test("every drawn pixel keeps its own colour", () => {
assert.deepEqual(after(BOX).map((row) => row.replace(/#/g, "0")), ["00000", "03860", "02420", "00000"]);
});
test("an empty pixel beside a drawn one becomes the outline", () => {
assert.equal(shade(2, 1, 0, DOT), DEFAULTS.color, "above");
assert.equal(shade(2, 3, 0, DOT), DEFAULTS.color, "below");
assert.equal(shade(1, 2, 0, DOT), DEFAULTS.color, "to the left");
assert.equal(shade(3, 2, 0, DOT), DEFAULTS.color, "to the right");
});
test("a pixel that touches her only at a corner stays empty", () => {
for (const [x, y] of [[1, 1], [3, 1], [1, 3], [3, 3]]) assert.equal(shade(x, y, 0, DOT), 0, `the corner at ${x}, ${y}`);
assert.equal(shade(0, 0, 0, BOX), 0, "the corner of the box");
});
test("a single dot gets an outline of four pixels, a plus around it", () => {
assert.deepEqual(after(DOT), ["00000", "00#00", "0#3#0", "00#00", "00000"]);
});
test("an empty pixel two steps away stays empty", () => {
assert.equal(shade(2, 0, 0, DOT), 0);
assert.equal(shade(0, 2, 0, DOT), 0);
assert.equal(shade(4, 4, 0, DOT), 0);
});
test("with one pixel of room on every side, the outline goes all the way round", () => {
assert.deepEqual(after(BOX), ["0###0", "#386#", "#242#", "0###0"]);
});
test("where she touches the edge of her picture, the outline has no room", () => {
// the same box cut tight: no empty pixel is left beside it, so there is no outline at all
const tight = picture(["386", "242"]);
assert.deepEqual(after(tight), ["386", "242"]);
});
test("the outline is drawn in the chosen colour", () => {
assert.equal(shade(2, 1, 0, DOT, { color: 1 }), 1);
assert.equal(shade(2, 1, 0, DOT, { color: 25 }), 25);
});
```
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 color; // the number of the outline's colour in the palette: 1 is the dark ink
// she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline
int shade(ivec2 p) {
int n = at(p);
if (n != 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
int beside = at(p + ivec2(-1, 0)) + at(p + ivec2(1, 0)) + at(p + ivec2(0, -1)) + at(p + ivec2(0, 1));
return beside > 0 ? color : 0;
}
// ---- 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 color; // the number of the outline's colour in the palette: 1 is the dark ink
// she stays as drawn; an empty pixel with her to its left, right, top or bottom becomes the outline
int shade(ivec2 p) {
int n = at(p);
if (n != 0) return n; // a drawn pixel stays as it was drawn
// the four neighbours that share a side with this pixel; the corners are not asked
int beside = at(p + ivec2(-1, 0)) + at(p + ivec2(1, 0)) + at(p + ivec2(0, -1)) + at(p + ivec2(0, 1));
return beside > 0 ? color : 0;
}
// ---- 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.