Stimmt das? Einen Pixel-Art-Shader prüfen, Pixel für Pixel
Nach vier Regeln ist es Zeit für die Frage, die zu jeder von ihnen gehört: Woran erkennst du, dass sie stimmt? Nicht daran, dass du genauer hinsiehst. Diese Lektion zeichnet einen Rahmen um das Bild, die einfachste Regel, bei der man leicht um einen Pixel danebenliegt, und geht die vier Schritte durch, die jede Lektion dieses Kurses macht: die Regel, ihren Test, den Shader und den Vergleich des Shader-Bildes mit dem Bild der Regel.

Die Regel fragt, wie weit jeder Pixel vom nächsten Rand entfernt ist. Die Spalten laufen von 0 bis 239 und die Zeilen von 0 bis 134, also beginnt der Rahmen rechts bei 240 minus seiner Dicke, und er ist dort so dick wie links.
Auch in dieser Lektion 3 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest
Die Idee
Ein Rahmen um das ganze Bild ist ein eigener Effekt. Spiele nutzen ihn für einen Fotomodus, für eine Zwischensequenz oder als roten Rand, wenn die Spielfigur getroffen wird. Die Regel ist ein Satz: Jeder Pixel, der weniger als so und so viele Pixel von einem Rand entfernt ist, bekommt die Farbe des Rahmens, und alles innen bleibt, wie es kam.
Sie ist auch die richtige Regel, um das Prüfen zu lernen, denn die Stelle, an der sie schiefgehen kann, ist leicht zu sehen, sobald du sie kennst, und leicht zu übersehen, solange nicht. Die vier Schritte sind für jeden Effekt dieselben:
- Die Regel als kleine Funktion, die für einen Pixel antwortet.
- Ihr Test: winzige Bilder, klein genug, um sie von Hand nachzuzählen, und was aus ihnen herauskommen muss.
- Der Shader: dieselbe Regel in einer Shader-Sprache, Zeile für Zeile aus der Funktion geschrieben.
- Der Vergleich: Shader und Regel werden für dieselben Bilder gefragt, und die Pixel, in denen sie sich unterscheiden, werden gezählt. Es darf keinen geben.
So funktioniert es
Das Bild ist 240 Pixel breit, seine Spalten tragen also die Nummern 0 bis 239; seine Zeilen 0 bis 134. Ein Pixel liegt in einem Rahmen von 4 Pixeln Dicke, wenn seine Spalte kleiner als 4 ist, links, oder mindestens 236, rechts; genauso für die Zeilen, oben und unten. 236 ist 240 minus 4: Der rechte Rahmen sind die Spalten 236, 237, 238 und 239, und das sind vier.
Der Test zeichnet ein Bild von 8 mal 6 Pixeln als Reihen aus Ziffern: einen Bienenkorb im Wald, mit Himmel darüber und Laub darunter. Bei einem Rahmen von 1 Pixel Dicke müssen genau die Pixel am Rand eingerahmt sein: 8 oben, 8 unten und 4 auf jeder Seite dazwischen, zusammen 24. Diese Zahl kann jeder nachzählen. Weitere Tests verlangen, dass der Rahmen rechts so dick ist wie links, unten so dick wie oben, und dass er bei einem Bild jeder Größe die letzte Spalte und die letzte Zeile erreicht.
Der Shader ist dieselbe Regel in einer Shader-Sprache. Er macht nur eines anders: Er fragt das Bild nach seiner Größe, wo die Funktion sie übergeben bekommt.
Für den Vergleich lässt unsere Prüfung den Shader in einem Browser auf Bildern dieser Lektion laufen, liest jeden Pixel zurück und zählt die, bei denen die Zahl des Shaders nicht die der Regel ist. „Was wir geprüft haben“, weiter unten auf dieser Seite, zeigt das Ergebnis dieses Laufs.
Ein Regler stellt die Dicke des Rahmens ein, von 0 bis 12; er beginnt bei 4. Der andere stellt seine Farbe über ihre Nummer in der Palette ein; er beginnt bei 1, der dunklen Tinte.
Was schiefgeht
Der Fehler ist ein Pixel zu wenig, rechts und unten. Eine KI schreibt „mehr als 240 minus 4“, wo „mindestens“ gemeint war, oder hält 240 für die letzte Spalte, obwohl es 239 ist. Links und oben stimmt alles, weil das Zählen ab 0 dort keine Falle hat. Rechts und unten ist der Rahmen einen Pixel dünner, und ein Rahmen von 1 Pixel Dicke hat gar keine rechte Seite und keinen unteren Rand.
Bei einer Dicke von 4 sieht man das kaum: Ist die rechte Seite 3 Pixel dick oder 4? Deshalb zählt der Test, statt hinzusehen. Mit dem Fehler kommen 13 Randpixel heraus, wo 24 hingehören, und der Rahmen ist rechts dünner als links.
Schalte das Bild auf „Mit dem Fehler“, während der Rahmen 1 Pixel dick ist: Er läuft nur oben und links entlang.
Der Fehler, Zeile für Zeile
- /* A frame around the whole picture, with the mistake: the frame on the right and at the bottom is
- worked out from the picture's width and height as if the last column were the width itself, with
- "more than" where "at least" was meant. The left and the top are right; the right and the bottom are
- one pixel short, and a frame 1 pixel thick has no right side and no bottom at all.
+ /* A frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
+ the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
+ cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
+ be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
+ its test, the shader, and the shader's picture compared with the rule's.
+ The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
+ palette's colours. The rule is asked for every pixel and answers with a number.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** The rule, wrong: "more than" on the right and at the bottom, so the frame there is one pixel short. */
+ /** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
+ from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
- const edge = x < p.width || y < p.width || x > src.width - p.width || y > src.height - p.width;
+ const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : at(src, x, y);
}Was der Test zum Fehler sagt
✖ a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them
got 13, expected 24
✖ the frame is as thick on the right as on the left
a frame 1 thick
got 0, expected 1
✖ the frame is as thick at the bottom as at the top
a frame 1 thick
got 0, expected 1
✔ inside the frame the picture is as it came
✔ a frame 0 pixels thick changes nothing
✔ the frame has the chosen colour
✖ on a picture of any size the frame reaches the last column and the last row
a picture of 3 by 2
got 5, expected 1
7 tests: 3 passed, 4 failedDas sagst du deiner KI
Schreib einen Shader für mein Spiel, der einen Rahmen um das ganze Bild zeichnet: Jeder Pixel, der weniger als [4] Pixel von einem Rand entfernt ist, bekommt die Farbe [die Farbe, als Nummer meiner Palette oder als RGB], und alles andere bleibt, wie es ist.
- Das Bild meines Spiels ist [Breite] mal [Höhe] Pixel groß. Die Spalten gehen von 0 bis Breite - 1 und die Zeilen von 0 bis Höhe - 1. Ein Pixel liegt im Rahmen, wenn seine Spalte kleiner als [4] oder mindestens Breite - [4] ist, oder seine Zeile kleiner als [4] oder mindestens Höhe - [4].
- Dicke und Farbe des Rahmens müssen Einstellungen sein, die ich aus dem Code des Spiels ändern kann. Arbeite mit ganzen Pixeln und mische nie Farben.
Arbeite in vier Schritten und zeig mir jeden davon:
1. Schreib die Regel als kleine, einfache Funktion. Sie bekommt ein Raster aus ganzen Zahlen (0 = leer, 1 bis N = die Farben meiner Palette) und einen Platz und gibt die Zahl für diesen einen Pixel zurück.
2. Teste sie an einem winzigen Bild von 8 mal 6 Pixeln: Bei einem Rahmen von 1 Pixel Dicke sind genau die 24 Pixel am Rand eingerahmt; der Rahmen ist rechts so dick wie links und unten so dick wie oben; innerhalb des Rahmens ändert sich nichts. Zeig mir die Ausgabe des Tests.
3. Schreib den Shader Zeile für Zeile aus der Funktion.
4. Zeichne dasselbe Bild mit dem Shader und mit der Funktion, vergleiche beide Pixel für Pixel und sag mir, wie viele Pixel sich unterscheiden. Es müssen 0 sein.
Der Code
Dreimal dieselbe Regel. Die erste ist die, die wir testen; die beiden Shader sind diese Regel in einer Shader-Sprache, und die läuft in deinem Spiel.
Die Regel, getestet: shader.ts
/* A frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
its test, the shader, and the shader's picture compared with the rule's.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How many pixels thick the frame is. 0 is no frame. */
width: 4,
/** The number of the frame's colour in the palette: 1 is the darkest ink. */
color: 1,
};
export type Params = typeof DEFAULTS;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : at(src, x, y);
}
Dieselbe Regel als Shader: GLSL
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : at(p);
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
Dieselbe Regel für Godot 4
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : at(p);
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
Der Test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
/** A hive (8) in a wood: sky (4) above, fallen leaves (9) and a path of sand (7) below. 8 pixels wide, 6 tall, small enough to count by hand. */
const WOOD = picture([
"44444444",
"44444444",
"44488444",
"99988999",
"77777777",
"99999999",
]);
/** The whole picture through the rule. */
const whole = (src: Frame, width: number, color = 1) =>
Array.from({ length: src.height }, (_, y) => Array.from({ length: src.width }, (_, x) => shade(x, y, 0, src, { width, color })));
/** How many pixels of a line, from its start, are the frame's colour. */
const run = (line: number[], color = 1) => {
let n = 0;
while (n < line.length && line[n] === color) n++;
return n;
};
test("a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", () => {
const out = whole(WOOD, 1);
const framed = out.flat().filter((n) => n === 1).length;
assert.equal(framed, 24);
for (let x = 0; x < 8; x++) assert.equal(out[5][x], 1, `the bottom row, column ${x}`);
for (let y = 0; y < 6; y++) assert.equal(out[y][7], 1, `the last column, row ${y}`);
});
test("the frame is as thick on the right as on the left", () => {
for (const width of [1, 2, 3]) {
const middle = whole(WOOD, width)[2];
assert.equal(run([...middle].reverse()), run(middle), `a frame ${width} thick`);
}
});
test("the frame is as thick at the bottom as at the top", () => {
for (const width of [1, 2]) {
const column = whole(WOOD, width).map((row) => row[3]);
assert.equal(run([...column].reverse()), run(column), `a frame ${width} thick`);
}
});
test("inside the frame the picture is as it came", () => {
const out = whole(WOOD, 2);
assert.deepEqual(out[2].slice(2, 6), [4, 8, 8, 4]);
assert.deepEqual(out[3].slice(2, 6), [9, 8, 8, 9]);
});
test("a frame 0 pixels thick changes nothing", () => {
assert.deepEqual(whole(WOOD, 0).flat(), [...WOOD.index]);
});
test("the frame has the chosen colour", () => {
assert.equal(shade(0, 3, 0, WOOD, { width: 1, color: 10 }), 10);
assert.equal(shade(4, 0, 0, WOOD, { width: 1, color: 10 }), 10);
});
test("on a picture of any size the frame reaches the last column and the last row", () => {
for (const [w, h] of [[1, 1], [3, 2], [240, 135]]) {
const src: Frame = { width: w, height: h, index: new Uint8Array(w * h).fill(5) };
assert.equal(shade(w - 1, h - 1, 0, src, { width: 1, color: 1 }), 1, `a picture of ${w} by ${h}`);
}
});
- 7 Tests bestehen mit der Regel.
- Mit dem eingebauten Fehler schlagen 4 davon fehl: "a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", "the frame is as thick on the right as on the left", "the frame is as thick at the bottom as at the top", "on a picture of any size the frame reaches the last column and the last row".
- Der Fehler ist im Bild oben zu sehen: Schalte auf „Mit dem Fehler“.
- Der Shader lief in einem Browser (Chrome/158.0.8127.37) auf 12 Bildern und ergab in 388.800 von 388.800 Pixeln das Bild der Regel.
Probier es aus
- Stell die Dicke auf 1 und schalte zwischen „Mit der Regel“ und „Mit dem Fehler“ hin und her. Mit dem Fehler fehlt der Rahmen rechts und unten.
- Stell sie auf 4 und sieh dir mit eingeschaltetem Fehler den rechten Rand genau an. Er ist 3 Pixel dick statt 4, und mit bloßem Auge ist das schwer zu erkennen. Stell dann die Farbe auf 10, Korallrot, den roten Rand eines Treffers, und versuch es noch einmal.
- Lies weiter unten „Was wir geprüft haben“: wie viele Tests bei der Regel bestehen, welche davon mit dem Fehler scheitern und in wie vielen Pixeln der Shader das Bild der Regel getroffen hat.
Zum Nachlesen
Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.
Macht Modellieren zuerst KI-Spielcode richtig? Wir haben es getestet
Unsere eigene Messung: Sollte eine KI zuerst ein Modell zeichnen, bestand ihr Code die Tests nicht öfter, und ihre Fehler waren kleine der Reihenfolge, des Rundens und der Bereiche, die erst das Ausführen der Tests zeigte.
Wie deine KI eine PNG-Datei ohne Installation in ihre echten Pixel liest, damit ein Bild aus deinem eigenen Spiel gezählt und Pixel für Pixel verglichen werden kann, statt beschrieben zu werden.
Why numbering should start at zero
Eine kurze Notiz darüber, warum man einen Bereich am besten mit seiner ersten Zahl darin schreibt und der Zahl nach seiner letzten als Grenze, die nicht mehr dazugehört: die Gewohnheit hinter „letzte Spalte gleich Breite minus eins“.
Teste dich: drei Fragen
Jede Antwort wird sofort geprüft und erklärt. Die Fragen gehören zum Quiz und zählen für deinen Game Coder IQ wie jede andere.
Mit dem Fehler wird ein Rahmen, der vier Pixel dick sein soll, rechts einen Pixel dünner, und das sieht man kaum. Wie sorgt diese Lektion dafür, dass so ein Ausrutscher gefunden wird?
Nächste Lektion Treffer-Blitz als Shader für Pixel Art: weiß blinken nach dem Treffer
Gib deiner KI die ganze Lektion
Der Knopf kopiert, was du ihr sagst, zusammen mit der getesteten Regel, ihrem Test und beiden Shadern. So hat deine KI alles, um das in dein Spiel zu bauen und dir zu zeigen, dass es funktioniert.
Das ist eine Lektion aus dem Kurs „Shader für Pixel Art“ auf spielerando.com: https://spielerando.com/de/courses/pixel-shaders/is-it-right
Schreib einen Shader für mein Spiel, der einen Rahmen um das ganze Bild zeichnet: Jeder Pixel, der weniger als [4] Pixel von einem Rand entfernt ist, bekommt die Farbe [die Farbe, als Nummer meiner Palette oder als RGB], und alles andere bleibt, wie es ist.
- Das Bild meines Spiels ist [Breite] mal [Höhe] Pixel groß. Die Spalten gehen von 0 bis Breite - 1 und die Zeilen von 0 bis Höhe - 1. Ein Pixel liegt im Rahmen, wenn seine Spalte kleiner als [4] oder mindestens Breite - [4] ist, oder seine Zeile kleiner als [4] oder mindestens Höhe - [4].
- Dicke und Farbe des Rahmens müssen Einstellungen sein, die ich aus dem Code des Spiels ändern kann. Arbeite mit ganzen Pixeln und mische nie Farben.
Arbeite in vier Schritten und zeig mir jeden davon:
1. Schreib die Regel als kleine, einfache Funktion. Sie bekommt ein Raster aus ganzen Zahlen (0 = leer, 1 bis N = die Farben meiner Palette) und einen Platz und gibt die Zahl für diesen einen Pixel zurück.
2. Teste sie an einem winzigen Bild von 8 mal 6 Pixeln: Bei einem Rahmen von 1 Pixel Dicke sind genau die 24 Pixel am Rand eingerahmt; der Rahmen ist rechts so dick wie links und unten so dick wie oben; innerhalb des Rahmens ändert sich nichts. Zeig mir die Ausgabe des Tests.
3. Schreib den Shader Zeile für Zeile aus der Funktion.
4. Zeichne dasselbe Bild mit dem Shader und mit der Funktion, vergleiche beide Pixel für Pixel und sag mir, wie viele Pixel sich unterscheiden. Es müssen 0 sein.
Die Regel, getestet (shader.ts):
```ts
/* A frame around the whole picture: every pixel that is fewer than `width` pixels from an edge takes
the frame's colour, and everything inside stays as it came. Games use it for a photo mode, a
cut-scene or a red edge when the player is hit. It is also the smallest rule in which it is easy to
be one pixel off, which makes it the rule for learning how to know that a rule is right: the rule,
its test, the shader, and the shader's picture compared with the rule's.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How many pixels thick the frame is. 0 is no frame. */
width: 4,
/** The number of the frame's colour in the palette: 1 is the darkest ink. */
color: 1,
};
export type Params = typeof DEFAULTS;
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: a pixel is in the frame if it is fewer than `width` pixels from an edge. The columns go
from 0 to src.width - 1, so the frame on the right begins at src.width - width; the rows likewise. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const edge = x < p.width || y < p.width || x >= src.width - p.width || y >= src.height - p.width;
return edge ? p.color : at(src, x, y);
}
```
Ihr Test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
/** A hive (8) in a wood: sky (4) above, fallen leaves (9) and a path of sand (7) below. 8 pixels wide, 6 tall, small enough to count by hand. */
const WOOD = picture([
"44444444",
"44444444",
"44488444",
"99988999",
"77777777",
"99999999",
]);
/** The whole picture through the rule. */
const whole = (src: Frame, width: number, color = 1) =>
Array.from({ length: src.height }, (_, y) => Array.from({ length: src.width }, (_, x) => shade(x, y, 0, src, { width, color })));
/** How many pixels of a line, from its start, are the frame's colour. */
const run = (line: number[], color = 1) => {
let n = 0;
while (n < line.length && line[n] === color) n++;
return n;
};
test("a frame 1 pixel thick covers exactly the pixels at the edge: 2 × (8 + 6) − 4 = 24 of them", () => {
const out = whole(WOOD, 1);
const framed = out.flat().filter((n) => n === 1).length;
assert.equal(framed, 24);
for (let x = 0; x < 8; x++) assert.equal(out[5][x], 1, `the bottom row, column ${x}`);
for (let y = 0; y < 6; y++) assert.equal(out[y][7], 1, `the last column, row ${y}`);
});
test("the frame is as thick on the right as on the left", () => {
for (const width of [1, 2, 3]) {
const middle = whole(WOOD, width)[2];
assert.equal(run([...middle].reverse()), run(middle), `a frame ${width} thick`);
}
});
test("the frame is as thick at the bottom as at the top", () => {
for (const width of [1, 2]) {
const column = whole(WOOD, width).map((row) => row[3]);
assert.equal(run([...column].reverse()), run(column), `a frame ${width} thick`);
}
});
test("inside the frame the picture is as it came", () => {
const out = whole(WOOD, 2);
assert.deepEqual(out[2].slice(2, 6), [4, 8, 8, 4]);
assert.deepEqual(out[3].slice(2, 6), [9, 8, 8, 9]);
});
test("a frame 0 pixels thick changes nothing", () => {
assert.deepEqual(whole(WOOD, 0).flat(), [...WOOD.index]);
});
test("the frame has the chosen colour", () => {
assert.equal(shade(0, 3, 0, WOOD, { width: 1, color: 10 }), 10);
assert.equal(shade(4, 0, 0, WOOD, { width: 1, color: 10 }), 10);
});
test("on a picture of any size the frame reaches the last column and the last row", () => {
for (const [w, h] of [[1, 1], [3, 2], [240, 135]]) {
const src: Frame = { width: w, height: h, index: new Uint8Array(w * h).fill(5) };
assert.equal(shade(w - 1, h - 1, 0, src, { width: 1, color: 1 }), 1, `a picture of ${w} by ${h}`);
}
});
```
Dieselbe Regel als GLSL-Shader (WebGL 2, OpenGL ES 3.0), Pixel für Pixel gegen die Regel geprüft:
```glsl
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : at(p);
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
Dieselbe Regel für Godot 4 (von den Autoren nicht ausgeführt):
```gdshader
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int width; // how many pixels thick the frame is; 0 is no frame
uniform int color; // the number of the frame's colour in the palette: 1 is the darkest ink
// a pixel is in the frame if it is fewer than width pixels from an edge. The columns go from 0 to
// size.x - 1, so the frame on the right begins at size.x - width; the rows likewise.
int shade(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
bool edge = p.x < width || p.y < width || p.x >= size.x - width || p.y >= size.y - width;
return edge ? color : at(p);
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
Wie hilfreich war diese Seite?
Diese Seite hat noch niemand bewertet.