A Pixel Art Fire Shader: Flames That Rise and Thin Out
A fire in pixel art is a handful of colours that climb, flicker and break off at the top, and a shader can draw it without a single drawn frame. This lesson's rule finds the coals below each pixel, takes a scrambled number for its place, slides that number upward as time goes on, and lets the height wear it away. What is left picks a colour from a short ramp, coral at the edge and white at the heart.

The whole room is given to the rule. Above the coral coals each pixel takes a scrambled number that slides upward with time, and loses heat the higher it is. Watch the tongues climb: the same shapes move up, row by row.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
A hearth in a cottage, a torch on a cellar wall, a roof that caught fire: a player reads all of them as tongues of flame that rise and come apart. Two things make a fire look like fire and not like an orange blob. Its shapes travel upward, and they get smaller and redder the higher they go.
Drawn by hand, that is a short loop the player learns to spot. A rule makes the flames out of numbers, so there is no loop, and it burns on a small campfire as well as along a whole wall.
The numbers come from the scramble of the lesson on noise you can trust: a whole number goes in, another comes out, the same on every machine. Raw, a scramble is salt and pepper. Fire needs neighbours that are alike, so this rule smooths it first.
How it works
The whole room is given to the rule. For each pixel it asks four things in turn.
Are there coals below? It looks down its own column, at most reach pixels, for the coals' colour (number 10, coral, in the picture). If there are none, the pixel comes back as it came. That is why the wall, the jars and the beekeeper are untouched.
What is the flicker here? The rule scrambles only the corners of a coarse grid of cells, 4 pixels wide and 8 high, and mixes the four corners around a pixel by how near each one is, with whole-number weights. Tall cells give tall tongues.
Which way is up? Every pace ticks the rule reads its scramble one row further down. What was below a pixel comes up to it, so the shapes climb one row at a time. In this picture rows are counted from the top, so a row further down is a larger number.
How much heat is left? The flicker is scaled down by the height: all of it just above the coals, none of it at reach. It is halved where the coals do not reach half the height to one side, and gone where they reach to neither, which narrows the fire to a point. The heat is then cut into bands of 16: the lowest band burns nothing, the next ones are coral, amber, honey and white.
The sliders are reach, how many pixels above the coals the flames can climb (30 when nobody touches it; the picture swings it from 12 to 40 and back), and pace, the ticks a flame takes to climb one pixel (2; 1 is a fierce fire, 6 a lazy one).
What goes wrong
The flames fall. Many shader examples count rows from the bottom of the screen, and there you take the time away from the row to make something rise. An AI has seen that line many times and writes it into a game whose rows are counted from the top, as in this picture and in many 2D engines. The tongues then slide down into the coals.
One frame of it looks fine, so it is easy to miss. Switch the picture to "With the mistake" and watch for a moment. Every tongue is born at the top of the fire and pours down into the logs, like a waterfall that happens to be orange.
Two tests catch it, and neither looks at a single frame. "The flicker climbs" checks that the same numbers stand one row higher pace ticks later. "In the picture the flames go up, not down" compares two frames: the later one must match the earlier one moved up by a row more often than moved down.
The mistake, line by line
- /* 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);
}
What the test says about the mistake
✔ 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 failedTell your AI
Write a fire shader for my pixel art game: flames that rise from [the coals in my hearth / the head of my torch sprite] and thin out as they climb.
- Whole pixels only: no smoothing of the picture and no blending. The fire draws only these colours, from its edge to its heart: [my darkest flame colour, two middle ones, my brightest].
- For the flicker, use a hash of whole numbers, not sin and not a noise texture. Smooth it on a coarse grid of cells 4 pixels wide and 8 high by mixing the four corners of a cell with whole-number weights.
- Time is a whole number of ticks that my game counts. The flicker climbs one pixel every [2] ticks.
- The heat is the smoothed number scaled down by the height above the source: all of it at the source, none of it [30] pixels above. Cut the heat into bands; the lowest band burns nothing.
- First find out whether my engine counts rows from the top or from the bottom, and tell me. The flames must move up on my screen.
Before you write the shader, write the same rule as a small plain function that takes a place and a tick and returns the colour's number. Test it: no fire where there is nothing to burn below, only the colours I listed, the same flicker [2] ticks later one row higher and not one row lower, and fewer pixels burning the higher you go. Show me the tests' output. Then write the shader so that it does exactly what the function does.
The code
Three times the same rule. The first is the one we test; the two shaders are that rule in a shader language, which is what your game runs.
The rule, tested: shader.ts
/* 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)];
}
The same rule as a shader: GLSL
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int 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);
}
The same rule for Godot 4
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int 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);
}
The 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 pass on the rule.
- With the mistake put in, 2 of them fail: "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".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 12 pictures and gave the rule's picture in 388,800 of 388,800 pixels.
Try it
- Pull
reachdown to 6. The fire sinks into a low bed of embers that hardly rises. Push it to 44 and it climbs past the middle of the opening, still narrowing to a point. - Set
paceto 6 and follow one dark gap in the fire. It climbs a row every 6 ticks, ten rows a second, and keeps its shape on the way. Only the fading eats it. - Switch to "With the mistake" and look at the fire's top edge. With the rule, scraps break off it and float up; with the mistake, they appear out of nothing above the fire and drop into it.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Smooth noise for the game's own code, with layers of finer detail and a third axis for time, for a torch's glow that wobbles outside the shader. It works in fractions, not in whole numbers.
Embers as things the game moves, each with its own speed, life and fall. The recipe counts 90 degrees as down, because a screen's rows run from the top: the trap this lesson's flames fall into.
The classic fire of old demos: a buffer of heat kept from frame to frame, each pixel the sum of four below it divided by a little more than four, so the flames die out as they rise. The way this rule does without.
Check yourself: three questions
Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.
What makes a fire in pixel art read as fire to a player, and not as an orange blob?
Next lesson Pixel Art Rain Shader: Streaks That Never Fall in Step
Give the whole lesson to your AI
The button copies what to tell it together with the tested rule, its test and both shaders, so your AI has everything to build this in your game and to show you that it works.
This is a lesson from the course "Shaders for Pixel Art" on spielerando.com: https://spielerando.com/en/courses/pixel-shaders/fire
Write a fire shader for my pixel art game: flames that rise from [the coals in my hearth / the head of my torch sprite] and thin out as they climb.
- Whole pixels only: no smoothing of the picture and no blending. The fire draws only these colours, from its edge to its heart: [my darkest flame colour, two middle ones, my brightest].
- For the flicker, use a hash of whole numbers, not sin and not a noise texture. Smooth it on a coarse grid of cells 4 pixels wide and 8 high by mixing the four corners of a cell with whole-number weights.
- Time is a whole number of ticks that my game counts. The flicker climbs one pixel every [2] ticks.
- The heat is the smoothed number scaled down by the height above the source: all of it at the source, none of it [30] pixels above. Cut the heat into bands; the lowest band burns nothing.
- First find out whether my engine counts rows from the top or from the bottom, and tell me. The flames must move up on my screen.
Before you write the shader, write the same rule as a small plain function that takes a place and a tick and returns the colour's number. Test it: no fire where there is nothing to burn below, only the colours I listed, the same flicker [2] ticks later one row higher and not one row lower, and fewer pixels burning the higher you go. Show me the tests' output. Then write the shader so that it does exactly what the function does.
The rule, tested (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)];
}
```
Its 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));
}
});
```
The same rule as a GLSL shader (WebGL 2, OpenGL ES 3.0), checked against the rule pixel for pixel:
```glsl
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int 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);
}
```
The same rule for Godot 4 (not run by the authors):
```gdshader
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int 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);
}
```
How useful was this page?
Nobody has rated this page yet.