Was ein Shader ist: eine Regel, für jeden Pixel gefragt
Ein Shader ist eine kleine Regel, und das Spiel fragt sie für jeden Pixel eines Bildes: Was steht hier jetzt? Die Regel sieht das Bild nicht als Ganzes, und sie zeichnet nicht. Sie antwortet für einen Pixel nach dem anderen, sechzigmal in der Sekunde, für jeden Pixel, den es gibt. Die Regel dieser Lektion ist die kleinste, die etwas nützt: Sie macht aus einem Sprite eine Silhouette.

Nur die Imkerin wird der Regel gegeben. Jeder gezeichnete Pixel von ihr bekommt die eine Farbe; die Pixel um sie herum waren leer und bleiben leer, also scheint die Wiese durch.
Auch in dieser Lektion 3 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest
Die Idee
In Pixel Art ist ein Bild ein kleines Raster, und in diesem Kurs ist jeder Pixel darin eine ganze Zahl: 0, wo nichts gezeichnet ist, und 1 bis 25 für die Farben der Palette. Der Hut der Imkerin sind ein paar Pixel mit 8, ihre Stiefel sind 1, und alles um sie herum ist 0.
Ein Shader bekommt dieses Raster und wird Pixel für Pixel gefragt: Welche Zahl soll hier stehen? Die Regel dieser Lektion ist ein Satz. Wo nichts ist, bleibt nichts; alles andere bekommt eine einzige Farbe. Das ist eine Silhouette. Spiele brauchen sie für eine Figur hinter einer Wand, für eine Gestalt, die noch nicht verraten wird, und als ersten Schritt zu einem Schatten.
Weil die Regel mit einer Zahl der Palette antwortet, kann sie keine Farbe erfinden. Das meinen wir mit der Pixel-Art-Art, und jede weitere Lektion hält sich daran.
So funktioniert es
Die Regel sieht sich eine Sache an: die Zahl an ihrem eigenen Platz. Ist sie 0, ist die Antwort 0. Ist sie irgendetwas anderes, ist die Antwort die Farbe, die du gewählt hast, egal was vorher dort stand. Sie sieht nie zu einem Nachbarn, und die Uhrzeit ist ihr egal. Das Bild ändert sich also nur, wenn das Sprite es tut.
Im Bild oben bekommt nur die Imkerin die Regel. Die Wiese ist ein zweites Bild, das hinter der Antwort der Regel liegt. Deshalb behält sie ihre Farben: Wo die Regel 0 antwortet, siehst du auf sie hindurch.
Der Regler stellt die Farbe über ihre Nummer in der Palette ein: 1 ist die dunkle Tinte, 12 das Blau, 10 das Korallrot.
Was schiefgeht
Der erste Shader, den dir eine KI schreibt, antwortet oft für jeden Pixel mit der Farbe und fragt nie, ob dort überhaupt etwas gezeichnet war. Das Bild eines Sprites ist ein Rechteck, in dem irgendwo die Figur steht, und der größte Teil davon ist leer. Färbst du alles, läuft im Spiel ein gefüllter Kasten über die Wiese.
Schalte das Bild auf „Mit dem Fehler“. Hier ist das Bild, das die Regel bekommt, so groß wie die ganze Bühne. Deshalb füllt sich die ganze Bühne mit der Farbe, und die Wiese ist weg.
Der Test fängt das mit der kleinsten Prüfung, die es gibt: Ein leerer Pixel muss leer bleiben.
Der Fehler, Zeile für Zeile
- /* 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;
}Was der Test zum Fehler sagt
✖ 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 failedDas sagst du deiner KI
Schreib einen Shader für mein Sprite, der es als Silhouette in einer Farbe zeichnet.
- Arbeite mit ganzen Pixeln: kein Glätten und kein Mischen. Lies die Textur des Sprites ohne Filter (nearest).
- Für jeden Pixel: Hat das Sprite dort nichts (sein Alpha ist 0), zeichne nichts. Sonst zeichne genau diese Farbe: [die Farbe, als Nummer meiner Palette oder als RGB].
- Die Farbe muss eine Einstellung sein, die ich aus dem Code des Spiels ändern kann.
Bevor du den Shader schreibst, schreib dieselbe 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. Teste sie: Ein leerer Pixel bleibt leer, ein gezeichneter Pixel bekommt die Farbe, und danach sind genauso viele Pixel gezeichnet wie vorher. Zeig mir die Ausgabe des Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.
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
/* 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;
}
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 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);
}
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 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);
}
Der 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);
});
- 5 Tests bestehen mit der Regel.
- Mit dem eingebauten Fehler schlagen 3 davon fehl: "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".
- 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 10 Bildern und ergab in 324.000 von 324.000 Pixeln das Bild der Regel.
Probier es aus
- Zieh den Regler durch die Palette. Bei manchen Nummern ist sie vor der Wiese schwer zu sehen. Das sind die, die du für eine Figur nicht nimmst, die der Spieler finden muss.
- Schalte zwischen „Wie es kam“ und „Mit der Regel“, während sie geht. Ihre Beine bewegen sich weiter: Die Regel wird für jedes Bild ihres Gangs neu gefragt. Sie merkt sich keine Form.
- Schalte auf „Mit dem Fehler“ und lies dann weiter unten, was der Test dazu sagt.
Zum Nachlesen
Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.
Wie ein Sprite als Zeilen aus Buchstaben geschrieben und zu einem Bild wird: dasselbe Raster aus Zahlen, nach dem die Regel dieser Lektion gefragt wird, von der Seite gesehen, die es zeichnet.
Der übliche Einstieg in Shader, mit Bruchzahlen und weichen Farben statt ganzer Zahlen und Palette. Die ersten Kapitel zeigen, was dieser Kurs mit Absicht weglässt, und warum ein Shader Pixel für Pixel denkt.
Wo man in Godot 4 einen Shader auf ein Sprite legt und wie eine Einstellung aus dem Spiel bei ihm ankommt. Nützlich neben der Godot-Fassung der Regel dieser Lektion, die wir nicht selbst ausgeführt haben.
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.
Ein Shader ist in diesem Kurs eine einzige Regel. Wie viel vom Bild sieht diese Regel, wenn sie gefragt wird?
Nächste Lektion Wo bin ich? Streifen nach Pixelposition im Pixel-Art-Shader
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/one-rule-per-pixel
Schreib einen Shader für mein Sprite, der es als Silhouette in einer Farbe zeichnet.
- Arbeite mit ganzen Pixeln: kein Glätten und kein Mischen. Lies die Textur des Sprites ohne Filter (nearest).
- Für jeden Pixel: Hat das Sprite dort nichts (sein Alpha ist 0), zeichne nichts. Sonst zeichne genau diese Farbe: [die Farbe, als Nummer meiner Palette oder als RGB].
- Die Farbe muss eine Einstellung sein, die ich aus dem Code des Spiels ändern kann.
Bevor du den Shader schreibst, schreib dieselbe 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. Teste sie: Ein leerer Pixel bleibt leer, ein gezeichneter Pixel bekommt die Farbe, und danach sind genauso viele Pixel gezeichnet wie vorher. Zeig mir die Ausgabe des Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.
Die Regel, getestet (shader.ts):
```ts
/* 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;
}
```
Ihr 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) };
}
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);
});
```
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 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);
}
```
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 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);
}
```
Wie hilfreich war diese Seite?
Diese Seite hat noch niemand bewertet.