Spielerando

Lesson 1 of 24One rule for one pixel6 minFree

What a Shader Is: One Rule, Asked for Every Pixel

A shader is one small rule, and the game asks it for every pixel of a picture: what stands here now? The rule does not see the picture as a whole and it does not draw. It answers for one pixel at a time, sixty times a second, for every pixel there is. The rule of this lesson is the smallest one that is of use: it turns a sprite into a silhouette.

A pixel art beekeeper walks across a meadow between a tree and a beehive. She is a flat shape in one colour: first dark ink, then blue, then coral. The meadow behind her keeps its colours.

Only the beekeeper is given to the rule. Every pixel of her that is drawn becomes the one colour; the pixels around her were empty and stay empty, so the meadow shows through.

Also in this lesson 3 things to read up on 3 questions to check yourself

The idea

In pixel art a picture is a small grid, and in this course every pixel in it is a whole number: 0 where nothing is drawn, and 1 to 25 for the colours of the palette. The beekeeper's hat is a few pixels of 8, her boots are 1, and everything around her is 0.

A shader is given that grid and is asked, pixel by pixel: which number should stand here? This lesson's rule is one sentence. Where nothing is, nothing stays; everything else becomes one colour. That is a silhouette. Games use it for a figure seen behind a wall, for a shape that is not revealed yet, and as the first step of a shadow.

Because the rule answers with a number of the palette, it cannot invent a colour. That is what we mean by the pixel art way, and every lesson after this one keeps to it.

How it works

The rule looks at one thing: the number at its own place. If it is 0, the answer is 0. If it is anything else, the answer is the colour you chose, whatever stood there before. It never looks at a neighbour and it does not care what time it is, so the picture changes only when the sprite does.

In the picture above only the beekeeper is given to the rule. The meadow is a second picture that lies behind the rule's answer. That is why it keeps its colours: where the rule answers 0, you see through to it.

The slider sets the colour by its number in the palette: 1 is the dark ink, 12 the blue, 10 the coral.

What goes wrong

The first shader an AI writes for you often answers with the colour for every pixel and never asks whether anything was drawn there. A sprite's picture is a rectangle with the figure somewhere inside it, and most of that rectangle is empty. Colour all of it, and in a game a filled box walks across the meadow.

Switch the picture to "With the mistake". Here the picture given to the rule is as large as the whole stage, so the whole stage fills with the colour and the meadow is gone.

The test catches it with the smallest check there is: an empty pixel must stay empty.

The mistake, line by line

Lines with - are the rule with the mistake, lines with + the rule as it should be.

- /* Silhouette, with the mistake: the rule answers with the colour for every pixel and never asks
-    whether anything was drawn there. The sprite turns into a filled rectangle.
+ /* Silhouette: every pixel of a sprite that is drawn becomes one colour, and what was empty stays
+    empty. It is the smallest shader that is of use: a figure seen through a wall, a shape before it is
+    revealed, the base of a shadow.

+    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: the one colour everywhere, also where nothing was. */
- export function shade(_x: number, _y: number, _tick: number, _src: Frame, p: Params = DEFAULTS): number {
-   return p.color;
+ /** The rule: nothing stays nothing, everything else becomes the one colour. */
+ export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
+   return at(src, x, y) === 0 ? 0 : p.color;
  }

What the test says about the mistake

✖ an empty pixel stays empty
    got 1, expected 0
✔ a drawn pixel becomes the chosen colour, whatever it was
✖ the shape is the same: as many pixels are drawn after as before
    got 20, expected 12
✔ every answer is the chosen colour or nothing
✖ a place outside the picture is empty
    got 1, expected 0
5 tests: 2 passed, 3 failed

Tell your AI

Write a shader for my sprite that draws it as a silhouette in one colour.

- Work on whole pixels: no smoothing and no blending. Sample the sprite's texture with nearest filtering.
- For every pixel: if the sprite has nothing there (its alpha is 0), draw nothing. Otherwise draw exactly this colour: [the colour, as a number of my palette or as RGB].
- The colour must be a setting I can change from the game's code.

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: an empty pixel stays empty, a drawn pixel becomes the colour, and as many pixels are drawn after as before. 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

TypeScript, small, and it needs nothing else. Your AI can read it and write the same rule for whatever your game is built with.

/* Silhouette: every pixel of a sprite that is drawn becomes one colour, and what was empty stays
   empty. It is the smallest shader that is of use: a figure seen through a wall, a shape before it is
   revealed, the base of a shadow.

   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 = {
  /** The number of the silhouette'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: nothing stays nothing, everything else becomes the one colour. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
  return at(src, x, y) === 0 ? 0 : p.color;
}

The same rule as a shader: GLSL

For WebGL 2 and OpenGL ES 3.0. Only the part marked as the rule belongs to this lesson; the rest is the same in every lesson.

#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 silhouette's colour in the palette: 1 is the dark ink

// nothing stays nothing, everything else becomes the one colour
int shade(ivec2 p) {
  return at(p) == 0 ? 0 : color;
}
// ---- 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

The checked shader with Godot's names around it. We have not run this one: Godot is not part of our checks yet, so let your AI compare its picture with the one on this page.

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 silhouette's colour in the palette: 1 is the dark ink

// nothing stays nothing, everything else becomes the one colour
int shade(ivec2 p) {
  return at(p) == 0 ? 0 : color;
}
// ---- 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) };
}

const HAT = picture([
  "00300",
  "03330",
  "88888",
  "02520",
]);

function whole(src: Frame, params = DEFAULTS): number[] {
  const out: number[] = [];
  for (let y = 0; y < src.height; y++) for (let x = 0; x < src.width; x++) out.push(shade(x, y, 0, src, params));
  return out;
}

test("an empty pixel stays empty", () => {
  assert.equal(shade(0, 0, 0, HAT), 0);
  assert.equal(shade(4, 3, 0, HAT), 0);
});

test("a drawn pixel becomes the chosen colour, whatever it was", () => {
  assert.equal(shade(2, 0, 0, HAT), 1);
  assert.equal(shade(2, 3, 0, HAT, { color: 12 }), 12);
  assert.equal(shade(0, 2, 0, HAT, { color: 12 }), 12);
});

test("the shape is the same: as many pixels are drawn after as before", () => {
  const before = [...HAT.index].filter((n) => n !== 0).length;
  const after = whole(HAT).filter((n) => n !== 0).length;
  assert.equal(after, before);
});

test("every answer is the chosen colour or nothing", () => {
  for (const n of whole(HAT, { color: 7 })) assert.ok(n === 0 || n === 7);
});

test("a place outside the picture is empty", () => {
  assert.equal(shade(-1, 0, 0, HAT), 0);
  assert.equal(shade(2, 4, 0, HAT), 0);
});
What we checked
  • 5 tests pass on the rule.
  • With the mistake put in, 3 of them fail: "an empty pixel stays empty", "the shape is the same: as many pixels are drawn after as before", "a place outside the picture is empty".
  • 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.

Run by pnpm courses:check on 2026-11-27 with Node v24.15.0.

Try it

Read up

Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.

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.

Question 1 of 3Easy

A shader, as this course uses the word, is one rule. How much of the picture does that rule see when it is asked?

All questions of this course · The quiz and the Game Coder IQ

Next lesson Where Am I? Stripes by Pixel Position in a Pixel Art Shader

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.

How useful was this page?

Nobody has rated this page yet.

One click, no account. It tells us which pages to make better next.

Last updated: 2026-11-27