Lantern Light in Pixel Art: a Shader with Round, Dithered Rings
Light from a lantern, a torch or a glowing player in a dark place: bright near the flame, darker further out, in rings. This lesson's rule darkens every pixel by one step of the palette for every ring the pixel lies outside of, and it mixes the two steps where one ring meets the next.

The cellar is drawn in its own colours; the rule darkens each pixel by one step for every ring it lies outside of. The lantern's reach slowly grows and shrinks, and the beekeeper is lit only while she is near it.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
Light in pixel art rarely fades smoothly. A small palette has only a few shades of each colour, so light falls off in steps: a ring where everything has its own colour, a ring one shade darker, another darker still, and then the dark of the room. Where two rings meet, a pattern of dots mixes them, so the step looks softer without a colour the palette does not have.
In the picture the cellar is drawn in its own colours, as if it were lit everywhere. The rule makes it dark. You draw the world once, in full colour, and the rule decides how much of it the light reaches. The beekeeper is lit by the same rule: she comes out of the dark under the lantern and goes back into it when she walks on. Nobody drew her in the dark.
How it works
For every pixel the rule works out how far it is from the lantern, without a square root. It takes the steps across times themselves plus the steps down times themselves, and compares that with the reach times itself. A pixel 5 across and one 3 across and 4 down both give 25: equally far, as on a circle.
Then it counts rings. Inside the full light ("reach of the full light", 22 pixels at the start) every colour stays its own. Each ring of "width of each ring" pixels further out (12) adds one step, up to "steps of dark" (4). A step is the next darker colour from the palette's table: honey becomes amber, then wood, then dark wood, then night. Until you touch a slider, the reach moves from 14 to 30 pixels and back, like a wick turned up and down.
The dithered edge uses the 4 by 4 pattern from the lesson on dithering. Each pixel's number in the pattern, 0 to 15, is turned into 0 to 3 extra pixels when "dithered edge" is 4: that is how far past the ring this pixel still counts as inside it. In the first pixel past a ring, 4 of every 16 pixels take the darker step; in the next, 8; then 12; then all of them. The pattern is fixed to the screen, not to the light, so it stays in place while the light grows and shrinks.
The lantern's place is a setting too: in a game you set it every frame, and the rings go with it.
What goes wrong
The rule has to use whole numbers, and the true distance needs a square root. An AI that knows this often reaches for the distance it knows from grids and tiles: steps across plus steps down. It is a whole number and looks right straight to the side. But it measures how far you walk on a grid, not how far light reaches. A place on the slant counts as further away than it is, and the light comes out as a diamond.
Switch the picture to "With the mistake". The rings on the wall turn into diamonds whose corners point straight up, down and to both sides, and the stones on the slant between those corners fall into the dark too early.
Two tests catch it. Places 5 pixels away straight across, straight down and on the slant (3 across, 4 down) must all get the same step. And with a reach of 4, a place 3 across and 2 down must be fully lit. Across plus down makes those places 7 and 5 pixels away, so both tests fail.
The mistake, line by line
- /* Lantern light, with the mistake: the distance to the lantern is measured as the steps across plus the
- steps up or down, a whole number without squares. That is how far one walks on a grid, not how far
- light reaches: a place on the slant counts as further away than it is, and the light comes out as a
- diamond with corners above, below and to the sides of the lantern.
+ /* Lantern light: the picture is dark everywhere, and around a lantern it is lit in rings. Inside the
+ lantern's reach every colour is its own; each ring further out is one step darker, through the
+ palette's table of darker colours, until the dark of the room. Where one ring meets the next, the
+ 4 by 4 pattern mixes the two steps, so the edge is dithered instead of hard. It is the light of a
+ lantern in a cellar, of a torch in a cave, of the glow around the player in a dark level.
+ 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 distance to the lantern is compared squared, so no square root is needed and
+ the light still comes out round.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: the distance is across plus down, so the rings are diamonds. */
+ /** The rule: one step darker for every ring the pixel lies outside of. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
- // how far from the lantern, wrong: the steps across plus the steps down, without squares
+ // how far from the lantern, squared: no square root is needed, and the light comes out round
const dx = x - p.lanternX;
const dy = y - p.lanternY;
- const far = Math.abs(dx) + Math.abs(dy);
+ const far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
…
for (let k = 0; k < p.dark; k++) {
const reach = p.radius + k * p.ring + jitter;
- if (far > reach) steps++;
+ if (far > reach * reach) steps++;
}
return darker(at(src, x, y), steps);What the test says about the mistake
✔ at the lantern and within its reach every colour is its own
✔ where no light reaches, a colour is as many steps darker as `dark` says, by the table
✖ the light is round: places as far from the lantern straight across and on the slant get the same step
at 13, 14
got 20, expected 9
✖ a place on the slant inside the full light is fully lit
got 9, expected 8
✔ with a hard edge the light steps down exactly where a ring ends
✔ a dithered edge mixes the two steps by the pattern, only in the pixels just outside the ring
✔ the dithered edge stays in its place: a pixel gets the same answer at every tick
7 tests: 5 passed, 2 failedTell your AI
Write a shader for my pixel art game that makes the whole screen dark and lights it in rings around a lantern.
- Work on whole pixels and on the colours of my palette only: no smoothing, no blending, no new colours. Each colour's next darker colour: [a table, or each ramp of my palette from light to dark].
- One step darker means the next darker colour from that table, not a lower colour number and not a colour multiplied by a share.
- Within [22] pixels of the lantern every colour stays its own. Each ring of [12] pixels further out is one step darker, up to [4] steps. Beyond the last ring everything is that many steps darker.
- Measure the distance as a circle: compare across times across plus down times down with the reach times the reach. Do not use across plus down: that lights a diamond.
- Dither the outer [4] pixels of every ring with the 4 by 4 Bayer pattern, fixed to the screen, so that two steps mix instead of meeting in a hard line.
- All of these, and the lantern's place, are settings the game can change every frame.
Before the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = my colours), a place and the settings, and returns the number for that one pixel. Test it: the lantern's own pixel keeps its colour; places 5 pixels away straight across, straight down and 3 across with 4 down all get the same step; with a reach of 4, a place 3 across and 2 down is fully lit; far away a colour is as many steps darker as set. 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
/* Lantern light: the picture is dark everywhere, and around a lantern it is lit in rings. Inside the
lantern's reach every colour is its own; each ring further out is one step darker, through the
palette's table of darker colours, until the dark of the room. Where one ring meets the next, the
4 by 4 pattern mixes the two steps, so the edge is dithered instead of hard. It is the light of a
lantern in a cellar, of a torch in a cave, of the glow around the player in a dark level.
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 distance to the lantern is compared squared, so no square root is needed and
the light still comes out round.
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 = {
/** Where the lantern is, in pixels from the left. The game sets it every frame. */
lanternX: 120,
/** Where the lantern is, in pixels from the top. */
lanternY: 64,
/** How far the full light reaches, in pixels. */
radius: 22,
/** How wide each ring of less light is, in pixels. */
ring: 12,
/** How many pixels at the outside of each ring are dithered: 0 is a hard edge. */
edge: 4,
/** How many steps darker the picture is where no light reaches. */
dark: 4,
};
export type Params = typeof DEFAULTS;
/** The 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The next darker colour of the palette for each number. [0] is nothing. */
const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A colour made darker by the table, `steps` times. */
function darker(n: number, steps: number): number {
let c = n;
for (let i = 0; i < steps; i++) c = DARKER[c];
return c;
}
/** The rule: one step darker for every ring the pixel lies outside of. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
// how far from the lantern, squared: no square root is needed, and the light comes out round
const dx = x - p.lanternX;
const dy = y - p.lanternY;
const far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
// one step darker for every ring this pixel lies outside of
let steps = 0;
for (let k = 0; k < p.dark; k++) {
const reach = p.radius + k * p.ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(src, x, y), steps);
}
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 lanternX; // where the lantern is, in pixels from the left. The game sets it every frame.
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered: 0 is a hard edge
uniform int dark; // how many steps darker the picture is where no light reaches
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [0] is nothing.
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// one step darker for every ring the pixel lies outside of
int shade(ivec2 p) {
// how far from the lantern, squared: no square root is needed, and the light comes out round
int dx = p.x - lanternX;
int dy = p.y - lanternY;
int far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
// one step darker for every ring this pixel lies outside of
int steps = 0;
for (int k = 0; k < dark; k++) {
int reach = radius + k * ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(p), steps);
}
// ---- 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 lanternX; // where the lantern is, in pixels from the left. The game sets it every frame.
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered: 0 is a hard edge
uniform int dark; // how many steps darker the picture is where no light reaches
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [0] is nothing.
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// one step darker for every ring the pixel lies outside of
int shade(ivec2 p) {
// how far from the lantern, squared: no square root is needed, and the light comes out round
int dx = p.x - lanternX;
int dy = p.y - lanternY;
int far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
// one step darker for every ring this pixel lies outside of
int steps = 0;
for (int k = 0; k < dark; k++) {
int reach = radius + k * ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(p), steps);
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
// a wall of honey, 21 by 21 pixels; honey (8) goes darker as amber (9), wood (20), dark wood (21)
const WALL = picture(Array.from({ length: 21 }, () => "8".repeat(21)));
// the lantern in the middle of the wall: full light 4 pixels far, rings 2 pixels wide, 3 steps of dark
const LAMP = { ...DEFAULTS, lanternX: 10, lanternY: 10, radius: 4, ring: 2, edge: 0, dark: 3 };
test("at the lantern and within its reach every colour is its own", () => {
assert.equal(shade(10, 10, 0, WALL, LAMP), 8);
assert.equal(shade(14, 10, 0, WALL, LAMP), 8);
assert.equal(shade(10, 6, 0, WALL, LAMP), 8);
});
test("where no light reaches, a colour is as many steps darker as `dark` says, by the table", () => {
assert.equal(shade(0, 0, 0, WALL, LAMP), 21);
assert.equal(shade(0, 0, 0, WALL, { ...LAMP, dark: 1 }), 9);
assert.equal(shade(20, 20, 0, WALL, { ...LAMP, dark: 2 }), 20);
});
test("the light is round: places as far from the lantern straight across and on the slant get the same step", () => {
// 5 pixels away: straight to the right, straight down, and 3 across with 4 down, 4 across with 3 down
const places = [[15, 10], [10, 15], [13, 14], [6, 13]];
for (const [x, y] of places) assert.equal(shade(x, y, 0, WALL, LAMP), 9, `at ${x}, ${y}`);
});
test("a place on the slant inside the full light is fully lit", () => {
// 3 across and 2 down is less than 4 pixels away
assert.equal(shade(13, 12, 0, WALL, LAMP), 8);
assert.equal(shade(7, 8, 0, WALL, LAMP), 8);
});
test("with a hard edge the light steps down exactly where a ring ends", () => {
const row = [14, 15, 16, 17, 18, 19].map((x) => shade(x, 10, 0, WALL, LAMP));
assert.deepEqual(row, [8, 9, 9, 20, 20, 21]);
});
test("a dithered edge mixes the two steps by the pattern, only in the pixels just outside the ring", () => {
const soft = { ...LAMP, radius: 4, ring: 10, edge: 4, dark: 1 };
const inside = new Set<number>();
const edge = new Set<number>();
const outside = new Set<number>();
for (let y = 0; y < WALL.height; y++) {
for (let x = 0; x < WALL.width; x++) {
const far = (x - 10) * (x - 10) + (y - 10) * (y - 10);
const n = shade(x, y, 0, WALL, soft);
if (far <= 4 * 4) inside.add(n);
else if (far > 7 * 7) outside.add(n);
else edge.add(n);
}
}
assert.deepEqual([...inside], [8]);
assert.deepEqual([...outside], [9]);
assert.deepEqual([...edge].sort((a, b) => a - b), [8, 9]);
});
test("the dithered edge stays in its place: a pixel gets the same answer at every tick", () => {
const soft = { ...LAMP, edge: 4 };
for (const [x, y] of [[15, 11], [6, 12], [12, 16]]) {
const first = shade(x, y, 0, WALL, soft);
for (const tick of [1, 2, 59, 977]) assert.equal(shade(x, y, tick, WALL, soft), first);
}
});
- 7 tests pass on the rule.
- With the mistake put in, 2 of them fail: "the light is round: places as far from the lantern straight across and on the slant get the same step", "a place on the slant inside the full light is fully lit".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 16 pictures and gave the rule's picture in 518,400 of 518,400 pixels.
Try it
- Set "dithered edge" to 0: the rings meet in hard lines. Then move it up to 8: the steps blend over a wider band, and the pattern's dots begin to show.
- Set "steps of dark" to 1: the cellar is only one step darker, more like dusk than night. At 6 nearly everything beyond the last ring is one of the palette's two night colours.
- Set "width of each ring" to 4: many thin rings, like the light of a candle. Switch on the mistake, and every thin ring becomes a diamond of its own.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Filling a circle on a grid by comparing squared distances, as this rule does, with pictures you can drag, and why a radius of a whole number and a half makes small circles look rounder.
This lantern shines through walls. For light or sight that stops at a wall, the recipe works out which tiles can be seen from a place, the same both ways, and the whole field of view around it.
Make a Horror Game With AI: Less Monster, More Dread
When a small light is all the player has: dark that still lets them see just enough, and a flicker with random timing that Z3 checks for going out too often within one second.
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.
The cellar is drawn once, in full colour, as if it were lit everywhere. When the beekeeper walks away from the lantern, how does she end up in the dark?
Next lesson Day and Night Shader for Pixel Art: Darker by the Hour
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/lantern-light
Write a shader for my pixel art game that makes the whole screen dark and lights it in rings around a lantern.
- Work on whole pixels and on the colours of my palette only: no smoothing, no blending, no new colours. Each colour's next darker colour: [a table, or each ramp of my palette from light to dark].
- One step darker means the next darker colour from that table, not a lower colour number and not a colour multiplied by a share.
- Within [22] pixels of the lantern every colour stays its own. Each ring of [12] pixels further out is one step darker, up to [4] steps. Beyond the last ring everything is that many steps darker.
- Measure the distance as a circle: compare across times across plus down times down with the reach times the reach. Do not use across plus down: that lights a diamond.
- Dither the outer [4] pixels of every ring with the 4 by 4 Bayer pattern, fixed to the screen, so that two steps mix instead of meeting in a hard line.
- All of these, and the lantern's place, are settings the game can change every frame.
Before the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = my colours), a place and the settings, and returns the number for that one pixel. Test it: the lantern's own pixel keeps its colour; places 5 pixels away straight across, straight down and 3 across with 4 down all get the same step; with a reach of 4, a place 3 across and 2 down is fully lit; far away a colour is as many steps darker as set. 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
/* Lantern light: the picture is dark everywhere, and around a lantern it is lit in rings. Inside the
lantern's reach every colour is its own; each ring further out is one step darker, through the
palette's table of darker colours, until the dark of the room. Where one ring meets the next, the
4 by 4 pattern mixes the two steps, so the edge is dithered instead of hard. It is the light of a
lantern in a cellar, of a torch in a cave, of the glow around the player in a dark level.
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 distance to the lantern is compared squared, so no square root is needed and
the light still comes out round.
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 = {
/** Where the lantern is, in pixels from the left. The game sets it every frame. */
lanternX: 120,
/** Where the lantern is, in pixels from the top. */
lanternY: 64,
/** How far the full light reaches, in pixels. */
radius: 22,
/** How wide each ring of less light is, in pixels. */
ring: 12,
/** How many pixels at the outside of each ring are dithered: 0 is a hard edge. */
edge: 4,
/** How many steps darker the picture is where no light reaches. */
dark: 4,
};
export type Params = typeof DEFAULTS;
/** The 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The next darker colour of the palette for each number. [0] is nothing. */
const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** A colour made darker by the table, `steps` times. */
function darker(n: number, steps: number): number {
let c = n;
for (let i = 0; i < steps; i++) c = DARKER[c];
return c;
}
/** The rule: one step darker for every ring the pixel lies outside of. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
// how far from the lantern, squared: no square root is needed, and the light comes out round
const dx = x - p.lanternX;
const dy = y - p.lanternY;
const far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
const jitter = (BAYER[(y & 3) * 4 + (x & 3)] * p.edge) >> 4;
// one step darker for every ring this pixel lies outside of
let steps = 0;
for (let k = 0; k < p.dark; k++) {
const reach = p.radius + k * p.ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(src, x, y), steps);
}
```
Its test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
// a wall of honey, 21 by 21 pixels; honey (8) goes darker as amber (9), wood (20), dark wood (21)
const WALL = picture(Array.from({ length: 21 }, () => "8".repeat(21)));
// the lantern in the middle of the wall: full light 4 pixels far, rings 2 pixels wide, 3 steps of dark
const LAMP = { ...DEFAULTS, lanternX: 10, lanternY: 10, radius: 4, ring: 2, edge: 0, dark: 3 };
test("at the lantern and within its reach every colour is its own", () => {
assert.equal(shade(10, 10, 0, WALL, LAMP), 8);
assert.equal(shade(14, 10, 0, WALL, LAMP), 8);
assert.equal(shade(10, 6, 0, WALL, LAMP), 8);
});
test("where no light reaches, a colour is as many steps darker as `dark` says, by the table", () => {
assert.equal(shade(0, 0, 0, WALL, LAMP), 21);
assert.equal(shade(0, 0, 0, WALL, { ...LAMP, dark: 1 }), 9);
assert.equal(shade(20, 20, 0, WALL, { ...LAMP, dark: 2 }), 20);
});
test("the light is round: places as far from the lantern straight across and on the slant get the same step", () => {
// 5 pixels away: straight to the right, straight down, and 3 across with 4 down, 4 across with 3 down
const places = [[15, 10], [10, 15], [13, 14], [6, 13]];
for (const [x, y] of places) assert.equal(shade(x, y, 0, WALL, LAMP), 9, `at ${x}, ${y}`);
});
test("a place on the slant inside the full light is fully lit", () => {
// 3 across and 2 down is less than 4 pixels away
assert.equal(shade(13, 12, 0, WALL, LAMP), 8);
assert.equal(shade(7, 8, 0, WALL, LAMP), 8);
});
test("with a hard edge the light steps down exactly where a ring ends", () => {
const row = [14, 15, 16, 17, 18, 19].map((x) => shade(x, 10, 0, WALL, LAMP));
assert.deepEqual(row, [8, 9, 9, 20, 20, 21]);
});
test("a dithered edge mixes the two steps by the pattern, only in the pixels just outside the ring", () => {
const soft = { ...LAMP, radius: 4, ring: 10, edge: 4, dark: 1 };
const inside = new Set<number>();
const edge = new Set<number>();
const outside = new Set<number>();
for (let y = 0; y < WALL.height; y++) {
for (let x = 0; x < WALL.width; x++) {
const far = (x - 10) * (x - 10) + (y - 10) * (y - 10);
const n = shade(x, y, 0, WALL, soft);
if (far <= 4 * 4) inside.add(n);
else if (far > 7 * 7) outside.add(n);
else edge.add(n);
}
}
assert.deepEqual([...inside], [8]);
assert.deepEqual([...outside], [9]);
assert.deepEqual([...edge].sort((a, b) => a - b), [8, 9]);
});
test("the dithered edge stays in its place: a pixel gets the same answer at every tick", () => {
const soft = { ...LAMP, edge: 4 };
for (const [x, y] of [[15, 11], [6, 12], [12, 16]]) {
const first = shade(x, y, 0, WALL, soft);
for (const tick of [1, 2, 59, 977]) assert.equal(shade(x, y, tick, WALL, soft), first);
}
});
```
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 lanternX; // where the lantern is, in pixels from the left. The game sets it every frame.
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered: 0 is a hard edge
uniform int dark; // how many steps darker the picture is where no light reaches
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [0] is nothing.
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// one step darker for every ring the pixel lies outside of
int shade(ivec2 p) {
// how far from the lantern, squared: no square root is needed, and the light comes out round
int dx = p.x - lanternX;
int dy = p.y - lanternY;
int far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
// one step darker for every ring this pixel lies outside of
int steps = 0;
for (int k = 0; k < dark; k++) {
int reach = radius + k * ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(p), steps);
}
// ---- 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 lanternX; // where the lantern is, in pixels from the left. The game sets it every frame.
uniform int lanternY; // where the lantern is, in pixels from the top
uniform int radius; // how far the full light reaches, in pixels
uniform int ring; // how wide each ring of less light is, in pixels
uniform int edge; // how many pixels at the outside of each ring are dithered: 0 is a hard edge
uniform int dark; // how many steps darker the picture is where no light reaches
// the 4 by 4 pattern for dithering: 0 to 15, each once. BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [0] is nothing.
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
// a colour made darker by the table, steps times
int darker(int n, int steps) {
int c = n;
for (int i = 0; i < steps; i++) c = DARKER[c];
return c;
}
// one step darker for every ring the pixel lies outside of
int shade(ivec2 p) {
// how far from the lantern, squared: no square root is needed, and the light comes out round
int dx = p.x - lanternX;
int dy = p.y - lanternY;
int far = dx * dx + dy * dy;
// the pattern's number for this place, turned into 0 to edge - 1: how far into the edge this pixel stays lit
int jitter = (BAYER[(p.y & 3) * 4 + (p.x & 3)] * edge) >> 4;
// one step darker for every ring this pixel lies outside of
int steps = 0;
for (int k = 0; k < dark; k++) {
int reach = radius + k * ring + jitter;
if (far > reach * reach) steps++;
}
return darker(at(p), steps);
}
// ---- 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.