Feuer-Shader für Pixel Art: Flammen, die steigen und vergehen
Ein Feuer in Pixel Art ist eine Handvoll Farben, die steigen, flackern und oben abreißen, und ein Shader kann es zeichnen, ohne dass du ein einziges Bild malst. Die Regel dieser Lektion sucht unter jedem Pixel die Glut, nimmt für seinen Platz eine verwürfelte Zahl, schiebt diese Zahl mit der Zeit nach oben und lässt die Höhe an ihr zehren. Was übrig bleibt, wählt eine Farbe aus einer kurzen Reihe: Korallrot am Rand, Weiß im Kern.

Das ganze Zimmer geht an die Regel. Über den korallroten Kohlen nimmt jeder Pixel eine verwürfelte Zahl, die mit der Zeit nach oben rutscht, und verliert Hitze, je höher er steht. Sieh den Zungen beim Steigen zu: Dieselben Formen wandern Reihe für Reihe nach oben.
Auch in dieser Lektion 3 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest
Die Idee
Ein Kamin in einer Hütte, eine Fackel an einer Kellerwand, ein Dach, das Feuer gefangen hat: Ein Spieler liest das alles als Flammenzungen, die aufsteigen und zerfallen. Zwei Dinge machen ein Feuer zum Feuer und nicht zu einem orangen Klecks. Seine Formen wandern nach oben, und sie werden kleiner und röter, je höher sie kommen.
Von Hand gezeichnet ist das eine kurze Schleife, die der Spieler bald erkennt. Eine Regel macht die Flammen aus Zahlen, also gibt es keine Schleife, und sie brennt auf einem kleinen Lagerfeuer genauso wie an einer ganzen Wand.
Die Zahlen kommen aus dem Verwürfeln der Lektion über Rauschen, dem du trauen kannst: Eine ganze Zahl geht hinein, eine andere kommt heraus, auf jedem Rechner dieselbe. Roh ist so ein Verwürfeln Salz und Pfeffer. Feuer braucht Nachbarn, die sich ähneln, also glättet die Regel es zuerst.
So funktioniert es
Das ganze Zimmer geht an die Regel. Für jeden Pixel fragt sie vier Dinge nacheinander.
Liegt Glut darunter? Sie schaut in ihrer eigenen Spalte nach unten, höchstens reach Pixel weit, nach der Farbe der Glut (im Bild Nummer 10, Korallrot). Findet sie keine, kommt der Pixel zurück, wie er kam. Deshalb bleiben die Wand, die Gläser und die Imkerin unberührt.
Wie flackert es hier? Die Regel verwürfelt nur die Ecken eines groben Rasters aus Zellen, 4 Pixel breit und 8 hoch, und mischt die vier Ecken um einen Pixel danach, wie nah jede ist, mit ganzzahligen Gewichten. Hohe Zellen geben hohe Zungen.
Wo ist oben? Alle pace Ticks liest die Regel ihr Verwürfeltes eine Reihe weiter unten. Was unter einem Pixel war, kommt zu ihm hoch, also klettern die Formen Reihe für Reihe. In diesem Bild werden die Reihen von oben gezählt; eine Reihe weiter unten hat also eine größere Nummer.
Wie viel Hitze bleibt? Das Flackern wird mit der Höhe kleiner gerechnet: direkt über der Glut ganz, bei reach gar nicht mehr. Es wird halbiert, wo die Glut zu einer Seite nicht die halbe Höhe weit reicht, und fällt weg, wo sie zu keiner Seite so weit reicht. Das spitzt das Feuer nach oben zu. Dann wird die Hitze in Stufen von 16 geteilt: Die unterste Stufe brennt nicht, die nächsten sind Korallrot, Bernstein, Honiggelb und Weiß.
Die Regler sind reach, wie viele Pixel über der Glut die Flammen klettern können (30, solange niemand ihn anfasst; das Bild schwenkt ihn von 12 auf 40 und zurück), und pace, wie viele Ticks eine Flamme für einen Pixel nach oben braucht (2; bei 1 tobt das Feuer, bei 6 ist es träge).
Was schiefgeht
Die Flammen fallen. Viele Shader-Beispiele zählen die Reihen vom unteren Bildrand aus, und dort zieht man die Zeit von der Reihe ab, damit etwas steigt. Eine KI hat diese Zeile oft gesehen und schreibt sie in ein Spiel, dessen Reihen von oben gezählt werden, wie in diesem Bild und in vielen 2D-Engines. Dann rutschen die Zungen nach unten in die Glut.
Ein einzelnes Bild davon sieht gut aus, deshalb übersieht man es leicht. Schalte das Bild auf „Mit dem Fehler“ und schau einen Moment zu. Jede Zunge entsteht oben am Feuer und fließt hinunter in die Scheite, wie ein Wasserfall, der zufällig orange ist.
Zwei Tests fangen das, und keiner schaut auf ein einzelnes Bild. „The flicker climbs“ prüft, dass dieselben Zahlen pace Ticks später eine Reihe höher stehen. „In the picture the flames go up, not down“ vergleicht zwei Bilder: Das spätere muss öfter zum früheren passen, wenn man dieses eine Reihe nach oben schiebt, als wenn man es nach unten schiebt.
Der Fehler, Zeile für Zeile
- /* Fire, with the mistake: the scramble is slid the wrong way. Many shader examples count rows from
- the bottom of the screen, and there the time is taken away from the row to make a pattern rise.
- Copied into a picture whose rows are counted from the top, as here and in many 2D engines, the
- same line reads the scramble further up as time goes on, and the flames pour down into the coals
- instead of rising. Everything else is the same as in shader.ts.
+ /* Fire: flames that rise from the coals, flicker, and thin out as they climb. Every pixel looks down
+ its own column for the coals. Above them it takes a scrambled number for its place, smoothed so
+ that neighbours are alike and slid upward as time goes on, and lowers it the higher it stands. What
+ heat is left picks a colour from a short ramp: coral at the edge, white at the heart.
+ 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. Rows are counted
+ from the top, so "up" is a smaller row.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
}
- /** How the fire flickers at a place and a time, wrong: the scramble slid down one pixel every `pace` ticks. */
+ /** How the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks. */
export function flicker(x: number, y: number, tick: number, p: Params = DEFAULTS): number {
const climb = Math.floor(tick / p.pace);
- // the mistake: the time is taken away, as if rows were counted from the bottom, so what was above comes down
- return soft(x, y - climb);
+ // the scramble is read further down as time goes on, so what was below comes up
+ return soft(x, y + climb);
}
Was der Test zum Fehler sagt
✔ a pixel with no coals below it stays as it came
✔ the coals stay as they are, and nothing burns at the reach or above it
✔ every pixel that burns is a colour of the ramp: coral, amber, honey or white
✖ the flicker climbs: pace ticks later the same numbers stand one row higher
at 0, 1, tick 0
got 3, expected 2
✖ in the picture the flames go up, not down: a frame pace ticks later matches one row higher
one row higher 2396 times, one row lower 3472 times
✔ the fire thins out upward: fewer pixels burn in each higher third of the reach
✔ between two steps of the pace the fire stands still
7 tests: 5 passed, 2 failedDas sagst du deiner KI
Schreib einen Feuer-Shader für mein Pixel-Art-Spiel: Flammen, die aus [der Glut in meinem Kamin / dem Kopf meines Fackel-Sprites] aufsteigen und nach oben hin vergehen.
- Nur ganze Pixel: kein Glätten des Bildes und kein Mischen. Das Feuer zeichnet nur diese Farben, vom Rand zum Kern: [meine dunkelste Flammenfarbe, zwei mittlere, meine hellste].
- Nimm für das Flackern einen Hash aus ganzen Zahlen, kein sin und keine Rauschtextur. Glätte ihn auf einem groben Raster aus Zellen, 4 Pixel breit und 8 hoch, indem du die vier Ecken einer Zelle mit ganzzahligen Gewichten mischst.
- Die Zeit ist eine ganze Zahl von Ticks, die mein Spiel zählt. Das Flackern steigt alle [2] Ticks um einen Pixel.
- Die Hitze ist die geglättete Zahl, kleiner gerechnet mit der Höhe über der Quelle: an der Quelle ganz, [30] Pixel darüber gar nicht mehr. Teil die Hitze in Stufen; die unterste Stufe brennt nicht.
- Finde zuerst heraus, ob meine Engine die Reihen von oben oder von unten zählt, und sag es mir. Die Flammen müssen sich auf meinem Bildschirm nach oben bewegen.
Bevor du den Shader schreibst, schreib dieselbe Regel als kleine, einfache Funktion, die einen Platz und einen Tick bekommt und die Nummer der Farbe zurückgibt. Teste sie: kein Feuer, wo darunter nichts brennt, nur die Farben, die ich genannt habe, dasselbe Flackern [2] Ticks später eine Reihe höher und nicht eine Reihe tiefer, und weniger brennende Pixel, je höher man kommt. Zeig mir die Ausgabe der 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
/* Fire: flames that rise from the coals, flicker, and thin out as they climb. Every pixel looks down
its own column for the coals. Above them it takes a scrambled number for its place, smoothed so
that neighbours are alike and slid upward as time goes on, and lowers it the higher it stands. What
heat is left picks a colour from a short ramp: coral at the edge, white at the heart.
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. Rows are counted
from the top, so "up" is a smaller row.
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 above the coals the flames can reach. */
reach: 30,
/** Ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one. */
pace: 2,
/** The number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral. */
coal: 10,
};
export type Params = typeof DEFAULTS;
/** The colours of the fire from its edge to its heart: coral, amber, honey, white. */
const RAMP = [10, 9, 8, 5];
/** How much heat each colour of the ramp covers. */
const BAND = 16;
/** The coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall. */
const CW = 4;
const CH = 8;
/** 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 whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** The scrambled number of one cell of the coarse grid: 0 to 99. */
function cell(cx: number, cy: number): number {
return hash(cx + cy * 4099 + 3 * 65537) % 100;
}
/** The scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99. */
function soft(x: number, y: number): number {
const cx = Math.floor(x / CW);
const cy = Math.floor(y / CH);
const fx = x - cx * CW;
const fy = y - cy * CH;
const top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
const bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return Math.floor((top * (CH - fy) + bottom * fy) / (CW * CH));
}
/** How the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks. */
export function flicker(x: number, y: number, tick: number, p: Params = DEFAULTS): number {
const climb = Math.floor(tick / p.pace);
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
/** The rule: above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
if (here === p.coal) return here;
// how far below are the coals? 0 if they are not within reach
let up = 0;
for (let d = 1; d <= p.reach; d++) {
if (at(src, x, y + d) === p.coal) {
up = d;
break;
}
}
if (up === 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
const side = Math.floor(up / 2);
const wide = (at(src, x - side, y + up) === p.coal ? 1 : 0) + (at(src, x + side, y + up) === p.coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
const heat = Math.floor((flicker(x, y, tick, p) * (p.reach - up) * wide) / (p.reach * 2));
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
const level = Math.floor(heat / BAND);
if (level === 0) return here;
return RAMP[Math.min(3, level - 1)];
}
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 reach; // how many pixels above the coals the flames can reach
uniform int pace; // ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one
uniform int coal; // the number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral
// the colours of the fire from its edge to its heart: coral, amber, honey, white
const int RAMP[4] = int[4](10, 9, 8, 5);
// how much heat each colour of the ramp covers
const int BAND = 16;
// the coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall
const int CW = 4;
const int CH = 8;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the scrambled number of one cell of the coarse grid: 0 to 99
int cell(int cx, int cy) {
return int(hash(uint(cx) + uint(cy) * 4099u + 3u * 65537u) % 100u);
}
// the scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99
int soft(int x, int y) {
int cx = x / CW;
int cy = y / CH;
int fx = x - cx * CW;
int fy = y - cy * CH;
int top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
int bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return (top * (CH - fy) + bottom * fy) / (CW * CH);
}
// how the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks
int flicker(int x, int y, int tick) {
int climb = tick / pace;
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
// above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp
int shade(ivec2 p) {
int here = at(p);
if (here == coal) return here;
// how far below are the coals? 0 if they are not within reach
int up = 0;
for (int d = 1; d <= reach; d++) {
if (at(ivec2(p.x, p.y + d)) == coal) {
up = d;
break;
}
}
if (up == 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
int side = up / 2;
int wide = (at(ivec2(p.x - side, p.y + up)) == coal ? 1 : 0) + (at(ivec2(p.x + side, p.y + up)) == coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
int heat = (flicker(p.x, p.y, TICK) * (reach - up) * wide) / (reach * 2);
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
int level = heat / BAND;
if (level == 0) return here;
return RAMP[min(3, level - 1)];
}
// ---- 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 reach; // how many pixels above the coals the flames can reach
uniform int pace; // ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one
uniform int coal; // the number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral
// the colours of the fire from its edge to its heart: coral, amber, honey, white
const int RAMP[4] = int[4](10, 9, 8, 5);
// how much heat each colour of the ramp covers
const int BAND = 16;
// the coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall
const int CW = 4;
const int CH = 8;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the scrambled number of one cell of the coarse grid: 0 to 99
int cell(int cx, int cy) {
return int(hash(uint(cx) + uint(cy) * 4099u + 3u * 65537u) % 100u);
}
// the scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99
int soft(int x, int y) {
int cx = x / CW;
int cy = y / CH;
int fx = x - cx * CW;
int fy = y - cy * CH;
int top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
int bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return (top * (CH - fy) + bottom * fy) / (CW * CH);
}
// how the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks
int flicker(int x, int y, int tick) {
int climb = tick / pace;
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
// above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp
int shade(ivec2 p) {
int here = at(p);
if (here == coal) return here;
// how far below are the coals? 0 if they are not within reach
int up = 0;
for (int d = 1; d <= reach; d++) {
if (at(ivec2(p.x, p.y + d)) == coal) {
up = d;
break;
}
}
if (up == 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
int side = up / 2;
int wide = (at(ivec2(p.x - side, p.y + up)) == coal ? 1 : 0) + (at(ivec2(p.x + side, p.y + up)) == coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
int heat = (flicker(p.x, p.y, TICK) * (reach - up) * wide) / (reach * 2);
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
int level = heat / BAND;
if (level == 0) return here;
return RAMP[min(3, level - 1)];
}
// ---- 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 { DEFAULTS, type Frame, flicker, 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) };
}
/** In these pictures the coals are colour 7 and the wall behind the fire is 2. */
const P = { ...DEFAULTS, coal: 7, reach: 16 };
const EMPTY = "2222222222222222222222222222";
const HEARTH = picture([
...Array.from({ length: 20 }, () => EMPTY),
"2222777777777777777777772222",
"1111111111111111111111111111",
]);
const COALS = 20;
const RAMP = new Set([10, 9, 8, 5]);
test("a pixel with no coals below it stays as it came", () => {
for (let tick = 0; tick < 60; tick += 7) {
for (let y = 0; y < HEARTH.height; y++) {
assert.equal(shade(1, y, tick, HEARTH, P), HEARTH.index[y * HEARTH.width + 1]);
assert.equal(shade(26, y, tick, HEARTH, P), HEARTH.index[y * HEARTH.width + 26]);
}
}
// below the coals, too: the fire burns upward only
assert.equal(shade(14, 21, 0, HEARTH, P), 1);
});
test("the coals stay as they are, and nothing burns at the reach or above it", () => {
for (let tick = 0; tick < 60; tick += 5) {
for (let x = 4; x < 24; x++) {
assert.equal(shade(x, COALS, tick, HEARTH, P), 7);
for (let y = 0; y <= COALS - P.reach; y++) assert.equal(shade(x, y, tick, HEARTH, P), 2);
}
}
});
test("every pixel that burns is a colour of the ramp: coral, amber, honey or white", () => {
let burning = 0;
for (let tick = 0; tick < 120; tick += 3) {
for (let y = 0; y < COALS; y++) {
for (let x = 0; x < HEARTH.width; x++) {
const n = shade(x, y, tick, HEARTH, P);
if (n !== 2) {
assert.ok(RAMP.has(n), `${n} at ${x}, ${y} is not a colour of the ramp`);
burning++;
}
}
}
}
assert.ok(burning > 0, "nothing burned at all");
});
test("the flicker climbs: pace ticks later the same numbers stand one row higher", () => {
for (const pace of [1, 2, 5]) {
const p = { ...P, pace };
for (let tick = 0; tick < 200; tick += pace * 3) {
for (let y = 1; y < 40; y++) {
for (let x = 0; x < 30; x++) assert.equal(flicker(x, y - 1, tick + pace, p), flicker(x, y, tick, p), `at ${x}, ${y}, tick ${tick}`);
}
}
}
});
test("in the picture the flames go up, not down: a frame pace ticks later matches one row higher", () => {
// the middle of the fire, with a reach far above this tiny picture, so that the fading hardly
// changes from one row to the next
const p = { ...P, reach: 200 };
let higher = 0;
let lower = 0;
for (let tick = 0; tick < 240; tick += 6) {
for (let y = 10; y < COALS - 1; y++) {
for (let x = 9; x < 19; x++) {
const now = shade(x, y, tick, HEARTH, p);
if (now === 2) continue;
if (shade(x, y - 1, tick + p.pace, HEARTH, p) === now) higher++;
if (shade(x, y + 1, tick + p.pace, HEARTH, p) === now) lower++;
}
}
}
assert.ok(higher > lower, `one row higher ${higher} times, one row lower ${lower} times`);
});
test("the fire thins out upward: fewer pixels burn in each higher third of the reach", () => {
const thirds = [0, 0, 0];
for (let tick = 0; tick < 240; tick += 3) {
for (let up = 1; up < P.reach; up++) {
for (let x = 4; x < 24; x++) if (shade(x, COALS - up, tick, HEARTH, P) !== 2) thirds[Math.floor(((up - 1) * 3) / (P.reach - 1))]++;
}
}
assert.ok(thirds[0] > thirds[1] && thirds[1] > thirds[2], `burning from the bottom third up: ${thirds.join(", ")}`);
});
test("between two steps of the pace the fire stands still", () => {
const p = { ...P, pace: 4 };
for (let y = 0; y < COALS; y++) {
for (let x = 0; x < HEARTH.width; x++) assert.equal(shade(x, y, 9, HEARTH, p), shade(x, y, 8, HEARTH, p));
}
});
- 7 Tests bestehen mit der Regel.
- Mit dem eingebauten Fehler schlagen 2 davon fehl: "the flicker climbs: pace ticks later the same numbers stand one row higher", "in the picture the flames go up, not down: a frame pace ticks later matches one row higher".
- 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
- Zieh
reachauf 6 herunter. Das Feuer sinkt zu einem flachen Glutbett zusammen, das kaum noch steigt. Schieb ihn auf 44, und es klettert über die Mitte der Öffnung, immer noch spitz zulaufend. - Stell
paceauf 6 und folge einer dunklen Lücke im Feuer. Sie steigt alle 6 Ticks eine Reihe, zehn Reihen in der Sekunde, und behält dabei ihre Form. Nur das Schwinden nagt an ihr. - Schalte auf „Mit dem Fehler“ und schau auf den oberen Rand des Feuers. Mit der Regel reißen dort Fetzen ab und schweben nach oben; mit dem Fehler tauchen sie aus dem Nichts über dem Feuer auf und fallen hinein.
Zum Nachlesen
Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.
Weiches Rauschen für den Code des Spiels selbst, mit Schichten feinerer Details und einer dritten Achse für die Zeit, etwa für den Schein einer Fackel außerhalb des Shaders. Es rechnet mit Bruchzahlen, nicht mit ganzen.
Funken als Dinge, die das Spiel bewegt, jeder mit eigenem Tempo, eigener Lebensdauer und eigenem Fall. Das Rezept zählt 90 Grad als unten, weil die Zeilen eines Bildschirms oben beginnen: die Falle der Flammen dieser Lektion.
Das klassische Feuer alter Demos: ein Puffer aus Hitze, der von Bild zu Bild bleibt, jeder Pixel die Summe von vier darunter, geteilt durch etwas mehr als vier, damit die Flammen beim Steigen vergehen. Diese Regel kommt ohne Puffer aus.
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.
Woran erkennt ein Spieler ein Feuer in Pixel Art als Feuer und nicht als orangen Klecks?
Nächste Lektion Regen-Shader für Pixel Art: Streifen, die nie im Gleichschritt fallen
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/fire
Schreib einen Feuer-Shader für mein Pixel-Art-Spiel: Flammen, die aus [der Glut in meinem Kamin / dem Kopf meines Fackel-Sprites] aufsteigen und nach oben hin vergehen.
- Nur ganze Pixel: kein Glätten des Bildes und kein Mischen. Das Feuer zeichnet nur diese Farben, vom Rand zum Kern: [meine dunkelste Flammenfarbe, zwei mittlere, meine hellste].
- Nimm für das Flackern einen Hash aus ganzen Zahlen, kein sin und keine Rauschtextur. Glätte ihn auf einem groben Raster aus Zellen, 4 Pixel breit und 8 hoch, indem du die vier Ecken einer Zelle mit ganzzahligen Gewichten mischst.
- Die Zeit ist eine ganze Zahl von Ticks, die mein Spiel zählt. Das Flackern steigt alle [2] Ticks um einen Pixel.
- Die Hitze ist die geglättete Zahl, kleiner gerechnet mit der Höhe über der Quelle: an der Quelle ganz, [30] Pixel darüber gar nicht mehr. Teil die Hitze in Stufen; die unterste Stufe brennt nicht.
- Finde zuerst heraus, ob meine Engine die Reihen von oben oder von unten zählt, und sag es mir. Die Flammen müssen sich auf meinem Bildschirm nach oben bewegen.
Bevor du den Shader schreibst, schreib dieselbe Regel als kleine, einfache Funktion, die einen Platz und einen Tick bekommt und die Nummer der Farbe zurückgibt. Teste sie: kein Feuer, wo darunter nichts brennt, nur die Farben, die ich genannt habe, dasselbe Flackern [2] Ticks später eine Reihe höher und nicht eine Reihe tiefer, und weniger brennende Pixel, je höher man kommt. Zeig mir die Ausgabe der Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.
Die Regel, getestet (shader.ts):
```ts
/* Fire: flames that rise from the coals, flicker, and thin out as they climb. Every pixel looks down
its own column for the coals. Above them it takes a scrambled number for its place, smoothed so
that neighbours are alike and slid upward as time goes on, and lowers it the higher it stands. What
heat is left picks a colour from a short ramp: coral at the edge, white at the heart.
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. Rows are counted
from the top, so "up" is a smaller row.
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 above the coals the flames can reach. */
reach: 30,
/** Ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one. */
pace: 2,
/** The number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral. */
coal: 10,
};
export type Params = typeof DEFAULTS;
/** The colours of the fire from its edge to its heart: coral, amber, honey, white. */
const RAMP = [10, 9, 8, 5];
/** How much heat each colour of the ramp covers. */
const BAND = 16;
/** The coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall. */
const CW = 4;
const CH = 8;
/** 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 whole number scrambled into another, the same on every machine. */
function hash(n: number): number {
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
return (n ^ (n >>> 16)) >>> 0;
}
/** The scrambled number of one cell of the coarse grid: 0 to 99. */
function cell(cx: number, cy: number): number {
return hash(cx + cy * 4099 + 3 * 65537) % 100;
}
/** The scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99. */
function soft(x: number, y: number): number {
const cx = Math.floor(x / CW);
const cy = Math.floor(y / CH);
const fx = x - cx * CW;
const fy = y - cy * CH;
const top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
const bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return Math.floor((top * (CH - fy) + bottom * fy) / (CW * CH));
}
/** How the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks. */
export function flicker(x: number, y: number, tick: number, p: Params = DEFAULTS): number {
const climb = Math.floor(tick / p.pace);
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
/** The rule: above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp. */
export function shade(x: number, y: number, tick: number, src: Frame, p: Params = DEFAULTS): number {
const here = at(src, x, y);
if (here === p.coal) return here;
// how far below are the coals? 0 if they are not within reach
let up = 0;
for (let d = 1; d <= p.reach; d++) {
if (at(src, x, y + d) === p.coal) {
up = d;
break;
}
}
if (up === 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
const side = Math.floor(up / 2);
const wide = (at(src, x - side, y + up) === p.coal ? 1 : 0) + (at(src, x + side, y + up) === p.coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
const heat = Math.floor((flicker(x, y, tick, p) * (p.reach - up) * wide) / (p.reach * 2));
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
const level = Math.floor(heat / BAND);
if (level === 0) return here;
return RAMP[Math.min(3, level - 1)];
}
```
Ihr Test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { DEFAULTS, type Frame, flicker, 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) };
}
/** In these pictures the coals are colour 7 and the wall behind the fire is 2. */
const P = { ...DEFAULTS, coal: 7, reach: 16 };
const EMPTY = "2222222222222222222222222222";
const HEARTH = picture([
...Array.from({ length: 20 }, () => EMPTY),
"2222777777777777777777772222",
"1111111111111111111111111111",
]);
const COALS = 20;
const RAMP = new Set([10, 9, 8, 5]);
test("a pixel with no coals below it stays as it came", () => {
for (let tick = 0; tick < 60; tick += 7) {
for (let y = 0; y < HEARTH.height; y++) {
assert.equal(shade(1, y, tick, HEARTH, P), HEARTH.index[y * HEARTH.width + 1]);
assert.equal(shade(26, y, tick, HEARTH, P), HEARTH.index[y * HEARTH.width + 26]);
}
}
// below the coals, too: the fire burns upward only
assert.equal(shade(14, 21, 0, HEARTH, P), 1);
});
test("the coals stay as they are, and nothing burns at the reach or above it", () => {
for (let tick = 0; tick < 60; tick += 5) {
for (let x = 4; x < 24; x++) {
assert.equal(shade(x, COALS, tick, HEARTH, P), 7);
for (let y = 0; y <= COALS - P.reach; y++) assert.equal(shade(x, y, tick, HEARTH, P), 2);
}
}
});
test("every pixel that burns is a colour of the ramp: coral, amber, honey or white", () => {
let burning = 0;
for (let tick = 0; tick < 120; tick += 3) {
for (let y = 0; y < COALS; y++) {
for (let x = 0; x < HEARTH.width; x++) {
const n = shade(x, y, tick, HEARTH, P);
if (n !== 2) {
assert.ok(RAMP.has(n), `${n} at ${x}, ${y} is not a colour of the ramp`);
burning++;
}
}
}
}
assert.ok(burning > 0, "nothing burned at all");
});
test("the flicker climbs: pace ticks later the same numbers stand one row higher", () => {
for (const pace of [1, 2, 5]) {
const p = { ...P, pace };
for (let tick = 0; tick < 200; tick += pace * 3) {
for (let y = 1; y < 40; y++) {
for (let x = 0; x < 30; x++) assert.equal(flicker(x, y - 1, tick + pace, p), flicker(x, y, tick, p), `at ${x}, ${y}, tick ${tick}`);
}
}
}
});
test("in the picture the flames go up, not down: a frame pace ticks later matches one row higher", () => {
// the middle of the fire, with a reach far above this tiny picture, so that the fading hardly
// changes from one row to the next
const p = { ...P, reach: 200 };
let higher = 0;
let lower = 0;
for (let tick = 0; tick < 240; tick += 6) {
for (let y = 10; y < COALS - 1; y++) {
for (let x = 9; x < 19; x++) {
const now = shade(x, y, tick, HEARTH, p);
if (now === 2) continue;
if (shade(x, y - 1, tick + p.pace, HEARTH, p) === now) higher++;
if (shade(x, y + 1, tick + p.pace, HEARTH, p) === now) lower++;
}
}
}
assert.ok(higher > lower, `one row higher ${higher} times, one row lower ${lower} times`);
});
test("the fire thins out upward: fewer pixels burn in each higher third of the reach", () => {
const thirds = [0, 0, 0];
for (let tick = 0; tick < 240; tick += 3) {
for (let up = 1; up < P.reach; up++) {
for (let x = 4; x < 24; x++) if (shade(x, COALS - up, tick, HEARTH, P) !== 2) thirds[Math.floor(((up - 1) * 3) / (P.reach - 1))]++;
}
}
assert.ok(thirds[0] > thirds[1] && thirds[1] > thirds[2], `burning from the bottom third up: ${thirds.join(", ")}`);
});
test("between two steps of the pace the fire stands still", () => {
const p = { ...P, pace: 4 };
for (let y = 0; y < COALS; y++) {
for (let x = 0; x < HEARTH.width; x++) assert.equal(shade(x, y, 9, HEARTH, p), shade(x, y, 8, HEARTH, p));
}
});
```
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 reach; // how many pixels above the coals the flames can reach
uniform int pace; // ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one
uniform int coal; // the number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral
// the colours of the fire from its edge to its heart: coral, amber, honey, white
const int RAMP[4] = int[4](10, 9, 8, 5);
// how much heat each colour of the ramp covers
const int BAND = 16;
// the coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall
const int CW = 4;
const int CH = 8;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the scrambled number of one cell of the coarse grid: 0 to 99
int cell(int cx, int cy) {
return int(hash(uint(cx) + uint(cy) * 4099u + 3u * 65537u) % 100u);
}
// the scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99
int soft(int x, int y) {
int cx = x / CW;
int cy = y / CH;
int fx = x - cx * CW;
int fy = y - cy * CH;
int top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
int bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return (top * (CH - fy) + bottom * fy) / (CW * CH);
}
// how the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks
int flicker(int x, int y, int tick) {
int climb = tick / pace;
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
// above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp
int shade(ivec2 p) {
int here = at(p);
if (here == coal) return here;
// how far below are the coals? 0 if they are not within reach
int up = 0;
for (int d = 1; d <= reach; d++) {
if (at(ivec2(p.x, p.y + d)) == coal) {
up = d;
break;
}
}
if (up == 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
int side = up / 2;
int wide = (at(ivec2(p.x - side, p.y + up)) == coal ? 1 : 0) + (at(ivec2(p.x + side, p.y + up)) == coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
int heat = (flicker(p.x, p.y, TICK) * (reach - up) * wide) / (reach * 2);
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
int level = heat / BAND;
if (level == 0) return here;
return RAMP[min(3, level - 1)];
}
// ---- 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 reach; // how many pixels above the coals the flames can reach
uniform int pace; // ticks for the flames to climb one pixel: 1 is a fierce fire, 6 a slow one
uniform int coal; // the number of the coals' colour in the palette: the fire burns above every pixel of it. 10 is coral
// the colours of the fire from its edge to its heart: coral, amber, honey, white
const int RAMP[4] = int[4](10, 9, 8, 5);
// how much heat each colour of the ramp covers
const int BAND = 16;
// the coarse grid the scramble is smoothed on: cells 4 pixels wide and 8 high, so tongues come out tall
const int CW = 4;
const int CH = 8;
// a whole number scrambled into another, the same on every machine
uint hash(uint n) {
n ^= n >> 16; n *= 0x7feb352du; n ^= n >> 15; n *= 0x846ca68bu; n ^= n >> 16;
return n;
}
// the scrambled number of one cell of the coarse grid: 0 to 99
int cell(int cx, int cy) {
return int(hash(uint(cx) + uint(cy) * 4099u + 3u * 65537u) % 100u);
}
// the scramble smoothed: the four cells around a place, each weighted by how near it is. 0 to 99
int soft(int x, int y) {
int cx = x / CW;
int cy = y / CH;
int fx = x - cx * CW;
int fy = y - cy * CH;
int top = cell(cx, cy) * (CW - fx) + cell(cx + 1, cy) * fx;
int bottom = cell(cx, cy + 1) * (CW - fx) + cell(cx + 1, cy + 1) * fx;
return (top * (CH - fy) + bottom * fy) / (CW * CH);
}
// how the fire flickers at a place and a time: the smoothed scramble, slid up one pixel every `pace` ticks
int flicker(int x, int y, int tick) {
int climb = tick / pace;
// the scramble is read further down as time goes on, so what was below comes up
return soft(x, y + climb);
}
// above the coals, heat from the flicker, less the higher up; the heat picks a colour of the ramp
int shade(ivec2 p) {
int here = at(p);
if (here == coal) return here;
// how far below are the coals? 0 if they are not within reach
int up = 0;
for (int d = 1; d <= reach; d++) {
if (at(ivec2(p.x, p.y + d)) == coal) {
up = d;
break;
}
}
if (up == 0) return here;
// the higher, the wider the coals under a flame must be: are there coals up / 2 to the left, and to the right?
int side = up / 2;
int wide = (at(ivec2(p.x - side, p.y + up)) == coal ? 1 : 0) + (at(ivec2(p.x + side, p.y + up)) == coal ? 1 : 0);
// the flicker, scaled down by the height (all of it at the coals, none at the reach) and by the coals around
int heat = (flicker(p.x, p.y, TICK) * (reach - up) * wide) / (reach * 2);
// which colour of the ramp: below one band of heat nothing burns, and the heart is white
int level = heat / BAND;
if (level == 0) return here;
return RAMP[min(3, level - 1)];
}
// ---- 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.