Game of Life
John Conway's Game of Life on a grid of 192 by 108 cells, stepped on the GPU. One rule for every cell at once, and from it come blinkers, a pulsar, spaceships that wrap round the grid, and Bill Gosper's glider gun firing a glider every 30 generations down to an eater that swallows it. Cells are born white-hot and cool to ember as they age, and a cell that dies leaves an afterglow.
· Particles & Simulation · Compute shader
Teaches Conway's rule: a cell lives or dies by its eight neighbors; gliders, blinkers, guns
Builds on Flowing Color Field, Crawling Glaze
Watch
The idea
Take a grid of cells, each alive or dead. At each generation, every cell looks at its eight neighbors and follows one rule, all at the same moment: a live cell with two or three live neighbors lives on, a dead cell with exactly three is born, and every other cell is dead next time. That is all of John Conway's Game of Life, from 1970. Nobody draws what happens next. Three cells in a row flip to three in a column and back, forever: a blinker. Five cells in the right shape walk across the grid diagonally: a glider. And Bill Gosper found a pattern of 36 cells that stays where it is and makes a new glider every 30 generations.
Like Crawling Glaze, this is a cells shader, // @cells 192 108: a grid of 192 by 108 cells, 20,736 of them, kept on the GPU, each a struct Cell of the shader's own. start sets every cell, next works out a cell one step later from the grid as it was after the last step, and shade draws the picture from the grid. The grid steps 60 times a second, and at the default speed a generation comes 8 times a second, 480 in the minute. It is built in six steps, and each step is a shader you can run on its own. It does not loop: the gun keeps firing, and the minute ends wherever the patterns are.
Stage 1 · A grid of cells
A cell holds one number, alive, 1 or 0. start asks life_placed whether each cell is part of a pattern, and each pattern is a small function written as on paper: life_from turns the cell into a place seen from the pattern's top-left corner, x to the right and y down, since the grid's row 0 is at the bottom. Three blinkers at the left and three at the right (life_blinker), two toads, two rows of three that breathe in and out (life_toad), two beacons, two blocks whose inner corners blink (life_beacon), a pulsar of 13 by 13 that repeats every 3 generations (life_pulsar), a pentadecathlon, ten cells with bent ends that comes back every 15 (life_penta), two gliders (life_glider, five cells from a table) and two lightweight spaceships, one heading right and one left, the same ship mirrored (life_ship).
next returns the cell as it was, so nothing moves yet. shade finds the point of the grid under each pixel with life_point: the cells stay square whatever the picture's shape, and at 16:9 each is exactly 10 pixels at 1080p. The ground is near-black with a faint dot at each cell's middle, and a live cell is a cream square with rounded corners (life_square, 0.76 of a cell across). The picture is a little darker toward its edges.
stage-1-a-grid-of-cells.metal
// Stage 1 · A grid of cells
// A grid of 192 by 108 cells lives on the GPU, and a cell holds one
// number: alive, 1 or 0. To start, some are alive in shapes drawn on
// purpose: blinkers, a toad, a beacon, a pulsar, two gliders and two
// spaceships. Each live cell is drawn as a square.
//
// @cells 192 108
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
};
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// Two gliders, heading down and to the right.
on = on || life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)));
return on;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at));
Cell c;
c.alive = on ? 1.0 : 0.0;
return c;
}
// Nothing happens yet: each step, a cell stays as it was.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
return me;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point: a cream square if it is alive.
Cell c = cells.at(home);
float2 d = g - (float2(home) + 0.5);
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, float3(1.0, 0.95, 0.87), body * c.alive);
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
return float4(color, 1.0);
}
// A soft hum on two notes, G and D, slowly breathing.
float2 sound(float t, constant Params& p) {
float2 s = float2(0.0);
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}Stage 2 · Eight neighbors
A cell now also holds around, how many of its eight neighbors are alive. life_count reads the eight cells around it with cells.at(at + int2(dx, dy)) and adds up their alive. cells.at wraps round at every edge, so a cell at the edge of the grid has eight neighbors too, from the other side. Each step next keeps the count and nothing else, and shade lights a dead cell in ember by its count, brighter the more live neighbors it has. The glow round each pattern is the rule's raw material: the dead cells with exactly three are where the next cells will be born.
stage-2-eight-neighbors.metal16 new lines since stage 1
// Stage 2 · Eight neighbors
// Every cell has eight neighbors: beside it, above, below and at the
// four corners. The grid wraps round, so edge cells have them too. Each
// cell counts its live neighbors and keeps the count. Dead cells glow
// ember by it: the more live neighbors, the brighter.
//
// @cells 192 108
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float around; // It holds how many neighbors are alive.
};
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// Two gliders, heading down and to the right.
on = on || life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)));
return on;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at));
Cell c;
c.alive = on ? 1.0 : 0.0;
c.around = 0.0;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// Each step, a cell counts its live neighbors. It doesn't change yet.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point: a cream square if it is alive.
Cell c = cells.at(home);
float2 d = g - (float2(home) + 0.5);
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, float3(1.0, 0.95, 0.87), body * c.alive);
// A dead cell glows ember, brighter the more live neighbors it has.
color += (1.0 - c.alive) * c.around * 0.1 * float3(1.0, 0.45, 0.15);
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
return float4(color, 1.0);
}
// A soft hum on two notes, G and D, slowly breathing.
float2 sound(float t, constant Params& p) {
float2 s = float2(0.0);
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}1 line from stage 1 that are gone
return me;Stage 3 · Conway's rule
Now the rule. The grid still steps 60 times a second, but a generation only comes speed times a second: life_tick counts steps, and a generation comes on the step where steps × speed ÷ 60 reaches a new whole number. At speed 8 that is every 7 or 8 steps, and counting steps rather than seconds keeps every generation to exactly one step. On that step, a live cell with an around of 2 or 3 lives on, a dead cell with 3 is born, and every other cell is dead. Every cell reads the grid as it was after the last step, so they all change at once, as the rule needs. Between generations a cell keeps what it is, but around is counted again every step, so it always says what the next generation will do. The blinkers flip, the pulsar pulses, the gliders walk down and to the right, one cell each way every 4 generations, and the spaceships head across and wrap round the grid. The dead cells' ember is turned down to a faint warmth.
stage-3-conways-rule.metal32 new lines since stage 2
// Stage 3 · Conway's rule
// Now the rule. A live cell with two or three live neighbors lives on.
// A dead cell with exactly three is born. Every other cell is dead next
// time. Speed sets the generations a second. Blinkers flip, the pulsar
// pulses, and the gliders and spaceships walk.
//
// @cells 192 108
// @param speed 8 1 20 Generations a second
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float around; // It holds how many neighbors are alive.
};
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// Two gliders, heading down and to the right.
on = on || life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)));
return on;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at));
Cell c;
c.alive = on ? 1.0 : 0.0;
c.around = 0.0;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// The grid steps 60 times a second, and a generation comes speed times
// a second, each on a step of its own.
bool life_tick(uint frame, float dt, float speed) {
float rate = round(1.0 / dt);
float n = float(frame);
return floor(n * speed / rate) > floor((n - 1.0) * speed / rate);
}
// Conway's rule, for every cell at once. A cell that doesn't live on
// and isn't born is dead next time.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
if (life_tick(u.frame, u.timeDelta, p.speed)) {
// Alive with two or three live neighbors, it lives on; dead with
// exactly three, it is born.
bool was = me.alive > 0.5;
bool lives = was ? (c.around == 2.0 || c.around == 3.0)
: c.around == 3.0;
c.alive = lives ? 1.0 : 0.0;
}
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point: a cream square if it is alive.
Cell c = cells.at(home);
float2 d = g - (float2(home) + 0.5);
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, float3(1.0, 0.95, 0.87), body * c.alive);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006 * float3(1.0, 0.45, 0.15);
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
return float4(color, 1.0);
}
float life_noise(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Each generation is a pluck, its note picked at random from a
// pentatonic scale by the generation's number, over a soft hum.
float2 sound(float t, constant Params& p) {
float speed = max(p.speed, 1.0);
float gen = floor(t * speed);
float2 s = float2(0.0);
// The last four generations' plucks, each dying away.
for (int k = 0; k < 4; k++) {
float g = gen - float(k);
if (g < 0.0) continue;
float age = t - g / speed;
float pick = floor(life_noise(g + 0.31) * 5.0);
float semis = pick < 1.0 ? 0.0 : pick < 2.0 ? 2.0 : pick < 3.0 ? 4.0
: pick < 4.0 ? 7.0 : 9.0;
semis += life_noise(g + 0.57) > 0.7 ? 12.0 : 0.0;
float pitch = 392.0 * pow(2.0, semis / 12.0);
float env = exp(-age * 32.0) * smoothstep(0.0, 0.003, age);
float v = (sin(TAU * pitch * age) + 0.3 * sin(TAU * 2.0 * pitch * age))
* env;
float pan = 0.5 + 0.35 * (2.0 * life_noise(g + 0.77) - 1.0);
s += 0.26 * v * float2(1.0 - pan, pan);
}
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}1 line from stage 2 that are gone
color += (1.0 - c.alive) * c.around * 0.1 * float3(1.0, 0.45, 0.15);Stage 4 · A glider gun
Bill Gosper's glider gun goes in at the top, left of the middle (life_gun, 36 cells in a box of 36 by 9, from a table). Two blocks at its ends hold two shuttles that bounce between them, and each time the shuttles meet, a glider flies out, down and to the right: one every 30 generations, so 7.5 cells apart along the stream. Three gliders are placed already on their way down the stream. Left alone, the gliders would travel off the edge of the grid, come round the wrapped edges and run into the gun. So at the end of the stream, at the lower right, sits an eater (life_eater), a still life of seven cells shaped like a fishhook: a glider that runs into it is gone a few generations later, and the eater is whole again.
stage-4-a-glider-gun.metal39 new lines since stage 3
// Stage 4 · A glider gun
// Bill Gosper found a gun: a pattern that fires a glider every 30
// generations, forever. Its stream crosses the grid to an eater, a still
// life that swallows each glider and mends itself, so the gliders never
// come round the wrapped grid to break the gun.
//
// @cells 192 108
// @param speed 8 1 20 Generations a second
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float around; // It holds how many neighbors are alive.
};
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Bill Gosper's glider gun, 36 by 9: two blocks hold two shuttles that
// bounce between them, and each time they meet a glider flies out, down
// and to the right, once every 30 generations.
constant int2 LIFE_GUN[36] = {
int2(24, 0), int2(22, 1), int2(24, 1), int2(12, 2), int2(13, 2),
int2(20, 2), int2(21, 2), int2(34, 2), int2(35, 2), int2(11, 3),
int2(15, 3), int2(20, 3), int2(21, 3), int2(34, 3), int2(35, 3),
int2(0, 4), int2(1, 4), int2(10, 4), int2(16, 4), int2(20, 4),
int2(21, 4), int2(0, 5), int2(1, 5), int2(10, 5), int2(14, 5),
int2(16, 5), int2(17, 5), int2(22, 5), int2(24, 5), int2(10, 6),
int2(16, 6), int2(24, 6), int2(11, 7), int2(15, 7), int2(12, 8),
int2(13, 8)
};
bool life_gun(int2 d) {
if (!life_box(d, int2(36, 9))) return false;
for (int i = 0; i < 36; i++) {
if (all(d == LIFE_GUN[i])) return true;
}
return false;
}
// An eater: a still life shaped like a fishhook. A glider that runs
// into it is gone a few generations later, and the eater is whole again.
bool life_eater(int2 d) {
return all(d == int2(0, 0)) || all(d == int2(1, 0))
|| all(d == int2(0, 1)) || all(d == int2(2, 1))
|| all(d == int2(2, 2)) || all(d == int2(2, 3))
|| all(d == int2(3, 3));
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// The gun, the eater at the end of its stream, and three gliders
// already on their way down the stream.
on = on || life_gun(life_from(at, int2(60, 100)))
|| life_eater(life_from(at, int2(144, 30)))
|| life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)))
|| life_glider(life_from(at, int2(128, 46)));
return on;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at));
Cell c;
c.alive = on ? 1.0 : 0.0;
c.around = 0.0;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// The grid steps 60 times a second, and a generation comes speed times
// a second, each on a step of its own.
bool life_tick(uint frame, float dt, float speed) {
float rate = round(1.0 / dt);
float n = float(frame);
return floor(n * speed / rate) > floor((n - 1.0) * speed / rate);
}
// Conway's rule, for every cell at once. A cell that doesn't live on
// and isn't born is dead next time.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
if (life_tick(u.frame, u.timeDelta, p.speed)) {
// Alive with two or three live neighbors, it lives on; dead with
// exactly three, it is born.
bool was = me.alive > 0.5;
bool lives = was ? (c.around == 2.0 || c.around == 3.0)
: c.around == 3.0;
c.alive = lives ? 1.0 : 0.0;
}
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point: a cream square if it is alive.
Cell c = cells.at(home);
float2 d = g - (float2(home) + 0.5);
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, float3(1.0, 0.95, 0.87), body * c.alive);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006 * float3(1.0, 0.45, 0.15);
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
return float4(color, 1.0);
}
float life_noise(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Each generation is a pluck, its note picked at random from a
// pentatonic scale by the generation's number. Every 30 generations,
// the gun's period, a low bell rings. Under it, a soft hum.
float2 sound(float t, constant Params& p) {
float speed = max(p.speed, 1.0);
float gen = floor(t * speed);
float2 s = float2(0.0);
// The last four generations' plucks, each dying away.
for (int k = 0; k < 4; k++) {
float g = gen - float(k);
if (g < 0.0) continue;
float age = t - g / speed;
float pick = floor(life_noise(g + 0.31) * 5.0);
float semis = pick < 1.0 ? 0.0 : pick < 2.0 ? 2.0 : pick < 3.0 ? 4.0
: pick < 4.0 ? 7.0 : 9.0;
semis += life_noise(g + 0.57) > 0.7 ? 12.0 : 0.0;
float pitch = 392.0 * pow(2.0, semis / 12.0);
float env = exp(-age * 32.0) * smoothstep(0.0, 0.003, age);
float v = (sin(TAU * pitch * age) + 0.3 * sin(TAU * 2.0 * pitch * age))
* env;
float pan = 0.5 + 0.35 * (2.0 * life_noise(g + 0.77) - 1.0);
s += 0.26 * v * float2(1.0 - pan, pan);
}
// The bell, once every 30 generations, and the one before it.
float bar = floor(gen / 30.0);
for (int k = 0; k < 2; k++) {
float b = bar - float(k);
if (b < 0.0) continue;
float age = t - b * 30.0 / speed;
float env = exp(-age * 1.6) * smoothstep(0.0, 0.004, age);
float bell = sin(TAU * 98.0 * age)
+ 0.5 * exp(-age * 2.0) * sin(TAU * 98.0 * 2.76 * age)
+ 0.25 * exp(-age * 4.0) * sin(TAU * 98.0 * 5.40 * age);
s += 0.21 * bell * env;
}
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}2 lines from stage 3 that are gone
on = on || life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)));Stage 5 · Age and afterglow
A cell now keeps two more numbers. age counts the generations it has lived, 0 when it is born. glow is its light: a live cell's rises to 1 in a thirtieth of a second, and a dead cell's falls by exp(−3 × dt ÷ fade) each step, to a twentieth in fade seconds. life_color makes a live cell white-hot when it is born and cools it toward ember over cool generations, through the kiln's colors (kiln, from Flowing Color Field); a dead cell is a dim ember, as bright as its glow. So the parts that move stay bright, because their cells are always new, and the parts that stay put cool: a blinker's middle cell never dies, so it turns ember, while its two ends are born again every other generation and stay white-hot. The cells a glider leaves behind glow for a moment, so it draws a short trail.
stage-5-age-and-afterglow.metal31 new lines since stage 4
// Stage 5 · Age and afterglow
// A cell now keeps its age, the generations it has lived, and its glow.
// It is born white-hot and cools to ember, so gliders stay bright and
// still lifes dim. A cell that dies leaves an afterglow that fades, so
// you can see where the gliders have been.
//
// @cells 192 108
// @param speed 8 1 20 Generations a second
// @param cool 12 2 60 Generations a cell takes to cool from white-hot to ember
// @param fade 0.6 0 3 Seconds a dead cell's afterglow takes to die away
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float age; // It counts the generations it has lived.
float around; // It holds how many neighbors are alive.
float glow; // Its light is 1 alive and fades once it dies.
};
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Bill Gosper's glider gun, 36 by 9: two blocks hold two shuttles that
// bounce between them, and each time they meet a glider flies out, down
// and to the right, once every 30 generations.
constant int2 LIFE_GUN[36] = {
int2(24, 0), int2(22, 1), int2(24, 1), int2(12, 2), int2(13, 2),
int2(20, 2), int2(21, 2), int2(34, 2), int2(35, 2), int2(11, 3),
int2(15, 3), int2(20, 3), int2(21, 3), int2(34, 3), int2(35, 3),
int2(0, 4), int2(1, 4), int2(10, 4), int2(16, 4), int2(20, 4),
int2(21, 4), int2(0, 5), int2(1, 5), int2(10, 5), int2(14, 5),
int2(16, 5), int2(17, 5), int2(22, 5), int2(24, 5), int2(10, 6),
int2(16, 6), int2(24, 6), int2(11, 7), int2(15, 7), int2(12, 8),
int2(13, 8)
};
bool life_gun(int2 d) {
if (!life_box(d, int2(36, 9))) return false;
for (int i = 0; i < 36; i++) {
if (all(d == LIFE_GUN[i])) return true;
}
return false;
}
// An eater: a still life shaped like a fishhook. A glider that runs
// into it is gone a few generations later, and the eater is whole again.
bool life_eater(int2 d) {
return all(d == int2(0, 0)) || all(d == int2(1, 0))
|| all(d == int2(0, 1)) || all(d == int2(2, 1))
|| all(d == int2(2, 2)) || all(d == int2(2, 3))
|| all(d == int2(3, 3));
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// The gun, the eater at the end of its stream, and three gliders
// already on their way down the stream.
on = on || life_gun(life_from(at, int2(60, 100)))
|| life_eater(life_from(at, int2(144, 30)))
|| life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)))
|| life_glider(life_from(at, int2(128, 46)));
return on;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at));
Cell c;
c.alive = on ? 1.0 : 0.0;
c.age = 0.0;
c.around = 0.0;
c.glow = c.alive;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// The grid steps 60 times a second, and a generation comes speed times
// a second, each on a step of its own.
bool life_tick(uint frame, float dt, float speed) {
float rate = round(1.0 / dt);
float n = float(frame);
return floor(n * speed / rate) > floor((n - 1.0) * speed / rate);
}
// Conway's rule, for every cell at once. Between generations a cell
// keeps what it is, and only its light changes.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
if (life_tick(u.frame, u.timeDelta, p.speed)) {
// Alive with two or three live neighbors, it lives on; dead with
// exactly three, it is born.
bool was = me.alive > 0.5;
bool lives = was ? (c.around == 2.0 || c.around == 3.0)
: c.around == 3.0;
c.alive = lives ? 1.0 : 0.0;
c.age = lives && was ? me.age + 1.0 : 0.0;
}
// A live cell lights up in a thirtieth of a second. A dead one's light
// falls to a twentieth in fade seconds.
float dt = u.timeDelta;
if (c.alive > 0.5) {
c.glow = min(1.0, me.glow + 30.0 * dt);
} else {
c.glow = p.fade > 0.0 ? me.glow * exp(-3.0 * dt / p.fade) : 0.0;
}
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
// A live cell's color: white-hot when it is born, cooling to ember over
// cool generations; while it lights up it heats from orange. A dead
// cell's afterglow is a dim ember.
float3 life_color(Cell c, constant Params& p) {
float cooled = smoothstep(0.0, 1.0, c.age / p.cool);
float3 hot = kiln((1.0 - 0.52 * cooled) * (0.5 + 0.5 * c.glow));
float3 ash = kiln(0.42) * 0.45 * c.glow;
return c.alive > 0.5 ? hot : ash;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point, in its color and as bright as its glow.
Cell c = cells.at(home);
float3 light = life_color(c, p);
float2 d = g - (float2(home) + 0.5);
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, light, body * c.glow);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006 * float3(1.0, 0.45, 0.15);
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
return float4(color, 1.0);
}
float life_noise(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Each generation is a pluck, its note picked at random from a
// pentatonic scale by the generation's number. Every 30 generations,
// the gun's period, a low bell rings. Under it, a soft hum.
float2 sound(float t, constant Params& p) {
float speed = max(p.speed, 1.0);
float gen = floor(t * speed);
float2 s = float2(0.0);
// The last four generations' plucks, each dying away.
for (int k = 0; k < 4; k++) {
float g = gen - float(k);
if (g < 0.0) continue;
float age = t - g / speed;
float pick = floor(life_noise(g + 0.31) * 5.0);
float semis = pick < 1.0 ? 0.0 : pick < 2.0 ? 2.0 : pick < 3.0 ? 4.0
: pick < 4.0 ? 7.0 : 9.0;
semis += life_noise(g + 0.57) > 0.7 ? 12.0 : 0.0;
float pitch = 392.0 * pow(2.0, semis / 12.0);
float env = exp(-age * 32.0) * smoothstep(0.0, 0.003, age);
float v = (sin(TAU * pitch * age) + 0.3 * sin(TAU * 2.0 * pitch * age))
* env;
float pan = 0.5 + 0.35 * (2.0 * life_noise(g + 0.77) - 1.0);
s += 0.26 * v * float2(1.0 - pan, pan);
}
// The bell, once every 30 generations, and the one before it.
float bar = floor(gen / 30.0);
for (int k = 0; k < 2; k++) {
float b = bar - float(k);
if (b < 0.0) continue;
float age = t - b * 30.0 / speed;
float env = exp(-age * 1.6) * smoothstep(0.0, 0.004, age);
float bell = sin(TAU * 98.0 * age)
+ 0.5 * exp(-age * 2.0) * sin(TAU * 98.0 * 2.76 * age)
+ 0.25 * exp(-age * 4.0) * sin(TAU * 98.0 * 5.40 * age);
s += 0.21 * bell * env;
}
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}1 line from stage 4 that are gone
color = mix(color, float3(1.0, 0.95, 0.87), body * c.alive);Stage 6 · Game of Life
In the finished shader each cell spills a little light on the pixels round it: shade looks at the cell under the pixel and its eight neighbors, and each adds its color times 0.35 × exp(−2.6 × d²), where d is the distance from its middle in cells. The square is drawn only for the cell under the pixel. A speck of noise, less than one step of the screen's brightness, keeps the dark ground from showing rings.
soup fills a square of 50 by 50 cells at the top right with random live cells as well, that percentage of them, chosen by a hash of each cell's place (life_hash), so the same soup comes every time and more soup adds to it. A soup burns down into still lifes, blinkers and stray gliders, and a stray glider that reaches the gun or its stream can break it. At the default of 0 there is none.
the kiln palette and shade(), from the finished shader
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point is drawn as a square. It and its eight
// neighbors each spill a little light here, fading with distance.
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int2 at = home + int2(dx, dy);
Cell c = cells.at(at);
float3 light = life_color(c, p);
float2 d = g - (float2(at) + 0.5);
color += light * 0.35 * exp(-2.6 * dot(d, d));
if (dx == 0 && dy == 0) {
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, light, body * c.glow);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006
* float3(1.0, 0.45, 0.15);
}
}
}
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
// A speck of noise, under one step of the screen's brightness, so the
// dark ground shades smoothly instead of in rings.
color += (life_hash(uint2(uv * u.resolution)) - 0.5) / 255.0;
return float4(color, 1.0);
}game-of-life.metal, the whole file23 new lines since stage 5
// Ray Kiln · Game of Life
//
// @episode Game of Life
// @length 60
// @variation 30
// @variations 2,3,4,7,8,10,11,12
// @sweeps soup, fade
// @still 32
// @teaches Conway's rule: a cell lives or dies by its eight neighbors; gliders, blinkers, guns
// @category Compute
// @tags game of life, cellular automata, gliders, compute shaders, procedural sound
// @final Game of Life | Each cell spills a little light on its neighbors, and the soup slider fills the empty corner with random cells.
//
// A grid of cells, each alive or dead. One rule, the same for every cell
// at once: a live cell with two or three live neighbors lives on; a dead
// cell with exactly three comes alive; every other cell is dead next
// time. That is John Conway's Game of Life. From the rule alone come
// blinkers that flip back and forth, gliders that walk across the grid,
// and a gun that makes a new glider every 30 generations, forever. Here
// a cell is born white-hot and cools to ember as it ages, and a cell
// that dies leaves an afterglow, so you can see where things have been.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
//
// @cells 192 108
// @param speed 8 1 20 Generations a second
// @param cool 12 2 60 Generations a cell takes to cool from white-hot to ember
// @param fade 0.6 0 3 Seconds a dead cell's afterglow takes to die away
// @param soup 0 0 50 Percent of the empty corner, top right, that starts alive at random
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float age; // It counts the generations it has lived.
float around; // It holds how many neighbors are alive.
float glow; // Its light is 1 alive and fades once it dies.
};
float life_hash(uint2 at) {
uint n = at.x * 374761393u + at.y * 668265263u;
n = (n ^ (n >> 13)) * 1274126177u;
return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Bill Gosper's glider gun, 36 by 9: two blocks hold two shuttles that
// bounce between them, and each time they meet a glider flies out, down
// and to the right, once every 30 generations.
constant int2 LIFE_GUN[36] = {
int2(24, 0), int2(22, 1), int2(24, 1), int2(12, 2), int2(13, 2),
int2(20, 2), int2(21, 2), int2(34, 2), int2(35, 2), int2(11, 3),
int2(15, 3), int2(20, 3), int2(21, 3), int2(34, 3), int2(35, 3),
int2(0, 4), int2(1, 4), int2(10, 4), int2(16, 4), int2(20, 4),
int2(21, 4), int2(0, 5), int2(1, 5), int2(10, 5), int2(14, 5),
int2(16, 5), int2(17, 5), int2(22, 5), int2(24, 5), int2(10, 6),
int2(16, 6), int2(24, 6), int2(11, 7), int2(15, 7), int2(12, 8),
int2(13, 8)
};
bool life_gun(int2 d) {
if (!life_box(d, int2(36, 9))) return false;
for (int i = 0; i < 36; i++) {
if (all(d == LIFE_GUN[i])) return true;
}
return false;
}
// An eater: a still life shaped like a fishhook. A glider that runs
// into it is gone a few generations later, and the eater is whole again.
bool life_eater(int2 d) {
return all(d == int2(0, 0)) || all(d == int2(1, 0))
|| all(d == int2(0, 1)) || all(d == int2(2, 1))
|| all(d == int2(2, 2)) || all(d == int2(2, 3))
|| all(d == int2(3, 3));
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// The gun, the eater at the end of its stream, and three gliders
// already on their way down the stream.
on = on || life_gun(life_from(at, int2(60, 100)))
|| life_eater(life_from(at, int2(144, 30)))
|| life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)))
|| life_glider(life_from(at, int2(128, 46)));
return on;
}
// Soup: cells alive at random in the empty corner at the top right. They
// burn out into still lifes, blinkers and stray gliders, and a stray
// glider may reach the gun and break it.
bool life_soup(int2 at, constant Params& p) {
bool corner = at.x >= 130 && at.x < 180 && at.y >= 50 && at.y < 100;
return corner && life_hash(uint2(at)) < p.soup * 0.01;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at)) || life_soup(int2(at), p);
Cell c;
c.alive = on ? 1.0 : 0.0;
c.age = 0.0;
c.around = 0.0;
c.glow = c.alive;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// The grid steps 60 times a second, and a generation comes speed times
// a second, each on a step of its own.
bool life_tick(uint frame, float dt, float speed) {
float rate = round(1.0 / dt);
float n = float(frame);
return floor(n * speed / rate) > floor((n - 1.0) * speed / rate);
}
// Conway's rule, for every cell at once. Between generations a cell
// keeps what it is, and only its light changes.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
if (life_tick(u.frame, u.timeDelta, p.speed)) {
// Alive with two or three live neighbors, it lives on; dead with
// exactly three, it is born.
bool was = me.alive > 0.5;
bool lives = was ? (c.around == 2.0 || c.around == 3.0)
: c.around == 3.0;
c.alive = lives ? 1.0 : 0.0;
c.age = lives && was ? me.age + 1.0 : 0.0;
}
// A live cell lights up in a thirtieth of a second. A dead one's light
// falls to a twentieth in fade seconds.
float dt = u.timeDelta;
if (c.alive > 0.5) {
c.glow = min(1.0, me.glow + 30.0 * dt);
} else {
c.glow = p.fade > 0.0 ? me.glow * exp(-3.0 * dt / p.fade) : 0.0;
}
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
// A live cell's color: white-hot when it is born, cooling to ember over
// cool generations; while it lights up it heats from orange. A dead
// cell's afterglow is a dim ember.
float3 life_color(Cell c, constant Params& p) {
float cooled = smoothstep(0.0, 1.0, c.age / p.cool);
float3 hot = kiln((1.0 - 0.52 * cooled) * (0.5 + 0.5 * c.glow));
float3 ash = kiln(0.42) * 0.45 * c.glow;
return c.alive > 0.5 ? hot : ash;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point is drawn as a square. It and its eight
// neighbors each spill a little light here, fading with distance.
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int2 at = home + int2(dx, dy);
Cell c = cells.at(at);
float3 light = life_color(c, p);
float2 d = g - (float2(at) + 0.5);
color += light * 0.35 * exp(-2.6 * dot(d, d));
if (dx == 0 && dy == 0) {
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, light, body * c.glow);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006
* float3(1.0, 0.45, 0.15);
}
}
}
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
// A speck of noise, under one step of the screen's brightness, so the
// dark ground shades smoothly instead of in rings.
color += (life_hash(uint2(uv * u.resolution)) - 0.5) / 255.0;
return float4(color, 1.0);
}
float life_noise(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// The sound keeps the picture's clock: it can't see the cells. Each
// generation is a pluck, its note picked at random from a pentatonic
// scale by the generation's number. Every 30 generations, the gun's
// period, a low bell rings. Under it, a soft hum on two notes.
float2 sound(float t, constant Params& p) {
float speed = max(p.speed, 1.0);
float gen = floor(t * speed);
float2 s = float2(0.0);
// The last four generations' plucks, each dying away.
for (int k = 0; k < 4; k++) {
float g = gen - float(k);
if (g < 0.0) continue;
float age = t - g / speed;
float pick = floor(life_noise(g + 0.31) * 5.0);
float semis = pick < 1.0 ? 0.0 : pick < 2.0 ? 2.0 : pick < 3.0 ? 4.0
: pick < 4.0 ? 7.0 : 9.0;
semis += life_noise(g + 0.57) > 0.7 ? 12.0 : 0.0;
float pitch = 392.0 * pow(2.0, semis / 12.0);
float env = exp(-age * 32.0) * smoothstep(0.0, 0.003, age);
float v = (sin(TAU * pitch * age) + 0.3 * sin(TAU * 2.0 * pitch * age))
* env;
float pan = 0.5 + 0.35 * (2.0 * life_noise(g + 0.77) - 1.0);
s += 0.26 * v * float2(1.0 - pan, pan);
}
// The bell, once every 30 generations, and the one before it.
float bar = floor(gen / 30.0);
for (int k = 0; k < 2; k++) {
float b = bar - float(k);
if (b < 0.0) continue;
float age = t - b * 30.0 / speed;
float env = exp(-age * 1.6) * smoothstep(0.0, 0.004, age);
float bell = sin(TAU * 98.0 * age)
+ 0.5 * exp(-age * 2.0) * sin(TAU * 98.0 * 2.76 * age)
+ 0.25 * exp(-age * 4.0) * sin(TAU * 98.0 * 5.40 * age);
s += 0.21 * bell * env;
}
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}4 lines from stage 5 that are gone
bool on = life_placed(int2(at));
⋯
Cell c = cells.at(home);
⋯
float2 d = g - (float2(home) + 0.5);
⋯
color += (1.0 - c.alive) * c.around * 0.006 * float3(1.0, 0.45, 0.15);The sound
The sound can't see the cells: it is worked out from the time alone, so it keeps time with the generations rather than following them. Each generation, floor(t × speed), is a pluck: a note from a pentatonic scale on G4 (392 Hz: G, A, B, D or E), an octave higher about three times in ten, picked by a hash of the generation's number, with a little of its octave above, dying away in about a tenth of a second and placed at random between left and right. The last four generations' plucks are summed, so they overlap a little. Every 30 generations, the gun's period, a low bell rings on G2 (98 Hz) with two higher partials that die away faster, and the bell dies away over about two seconds. Under it a soft hum on G1 and D2 (49 and 73.4 Hz) swells and falls every 8 seconds. speed sets the pace of both plucks and bells, and the sound fades in over the first 1.5 seconds.
At the defaults the peak is −5.8 dBFS and the RMS −21.5 dBFS, over 20 seconds and over the minute.
New in Ray Kiln
The cell inspector. In the playground, a cells episode now has the Inspect button under the picture (or the I key), as particle episodes do. Press it, then click the picture: the cell under the pointer is picked, found from the click the same way cells.at(uv) finds it, and a cream box marks it, never smaller than 14 pixels a side so a tiny cell is still marked. Under the picture its struct Cell is shown field by field, by name, with its column and row (counted from the bottom left) and the step the grid is at. The reading is copied back from the grid once a frame as it steps. Pause and it holds; Restart shows the cell as start made it; new code keeps the same column and row while the grid still has them. It is live only, so stills and exports never see it, and it is free.
This episode is made for it. Open it in the playground, press Inspect and click the middle cell of a blinker: alive stays 1, around stays 2, and age climbs by one every generation, because that cell never dies. Click one of its ends instead: it is alive with an around of 1, so it dies, then dead with 3, so it is born, every other generation, and its glow falls and rises with it. Then click a dead cell just ahead of a glider in the stream and watch around: when it reads 3, the cell is born at the next generation.
Try this
- Set
fadeto 3: every glider and spaceship draws a long ember trail behind it, and the stream from the gun to the eater becomes a line. - Set
fadeto 0: a dead cell goes dark at once. A blinker is then a bar of three, where at the default the two cells it has just lost still glow, so it looks like a plus. - Set
coolto 2: everything but the newborn cells turns ember, and a glider is ember with only a cream cell or two. - Set
soupto 30: the top right corner burns down into still lifes, blinkers and stray gliders, and the debris reaches the stream and then the gun, which is broken before the minute ends. - Set
speedto 20: the minute runs 1,200 generations, and the gun is still firing at the end of it.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
speed | 8 | 1 to 20 | Generations a second |
cool | 12 | 2 to 60 | Generations a cell takes to cool from white-hot to ember |
fade | 0.6 | 0 to 3 | Seconds a dead cell's afterglow takes to die away |
soup | 0 | 0 to 50 | Percent of the empty corner, top right, that starts alive at random |
volume | 0.8 | 0 to 1 | Loudness of the sound |
More from this shader
The same shader 8 more ways: each from a different moment, with different settings, one after another. Each chapter below says which settings moved most, then lists them all.
Wallpapers
A still from the shader, rendered at full size for a desktop or a phone screen. Free to use as your wallpaper.
Clips to post
Fifteen seconds of the shader with its sound, sized for posting: square for feeds, vertical for stories and short videos. Free to post, ideally with a link to this page.
Made from this episode
The full source
This is the whole episode: the picture, the parameters and the sound.
game-of-life.metal
// Ray Kiln · Game of Life
//
// @episode Game of Life
// @length 60
// @variation 30
// @variations 2,3,4,7,8,10,11,12
// @sweeps soup, fade
// @still 32
// @teaches Conway's rule: a cell lives or dies by its eight neighbors; gliders, blinkers, guns
// @category Compute
// @tags game of life, cellular automata, gliders, compute shaders, procedural sound
// @final Game of Life | Each cell spills a little light on its neighbors, and the soup slider fills the empty corner with random cells.
//
// A grid of cells, each alive or dead. One rule, the same for every cell
// at once: a live cell with two or three live neighbors lives on; a dead
// cell with exactly three comes alive; every other cell is dead next
// time. That is John Conway's Game of Life. From the rule alone come
// blinkers that flip back and forth, gliders that walk across the grid,
// and a gun that makes a new glider every 30 generations, forever. Here
// a cell is born white-hot and cools to ember as it ages, and a cell
// that dies leaves an afterglow, so you can see where things have been.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
//
// @cells 192 108
// @param speed 8 1 20 Generations a second
// @param cool 12 2 60 Generations a cell takes to cool from white-hot to ember
// @param fade 0.6 0 3 Seconds a dead cell's afterglow takes to die away
// @param soup 0 0 50 Percent of the empty corner, top right, that starts alive at random
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
struct Cell {
float alive; // A live cell holds 1, a dead one 0.
float age; // It counts the generations it has lived.
float around; // It holds how many neighbors are alive.
float glow; // Its light is 1 alive and fades once it dies.
};
float life_hash(uint2 at) {
uint n = at.x * 374761393u + at.y * 668265263u;
n = (n ^ (n >> 13)) * 1274126177u;
return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}
// Patterns are drawn as on paper, from their top-left corner, y down.
int2 life_from(int2 at, int2 corner) {
return int2(at.x - corner.x, corner.y - at.y);
}
bool life_box(int2 d, int2 size) {
return d.x >= 0 && d.y >= 0 && d.x < size.x && d.y < size.y;
}
// A blinker: three in a row. Next time it is three in a column.
bool life_blinker(int2 d) {
return d.y == 0 && d.x >= 0 && d.x <= 2;
}
// A toad: two rows of three, one shifted along. It breathes in and out.
bool life_toad(int2 d) {
return (d.y == 0 && d.x >= 1 && d.x <= 3)
|| (d.y == 1 && d.x >= 0 && d.x <= 2);
}
// A beacon: two blocks whose inner corners blink.
bool life_beacon(int2 d) {
return life_box(d, int2(2, 2)) || life_box(d - int2(2, 2), int2(2, 2));
}
// A pulsar, 13 by 13, repeats every 3 generations.
bool life_pulsar(int2 d) {
if (!life_box(d, int2(13, 13))) return false;
int a = abs(d.x - 6), b = abs(d.y - 6);
return ((b == 1 || b == 6) && a >= 2 && a <= 4)
|| ((a == 1 || a == 6) && b >= 2 && b <= 4);
}
// A glider: every 4 generations the same shape, one cell down and right.
constant int2 LIFE_GLIDER[5] = {
int2(0, 0), int2(1, 1), int2(2, 1), int2(0, 2), int2(1, 2)
};
// A lightweight spaceship: 2 cells left every 4 generations.
constant int2 LIFE_SHIP[9] = {
int2(1, 0), int2(4, 0), int2(0, 1), int2(0, 2), int2(4, 2),
int2(0, 3), int2(1, 3), int2(2, 3), int2(3, 3)
};
bool life_glider(int2 d) {
for (int i = 0; i < 5; i++) {
if (all(d == LIFE_GLIDER[i])) return true;
}
return false;
}
// The ship heading right is the same ship turned over, x mirrored.
bool life_ship(int2 d, bool right) {
int2 e = right ? int2(4 - d.x, d.y) : d;
for (int i = 0; i < 9; i++) {
if (all(e == LIFE_SHIP[i])) return true;
}
return false;
}
// A pentadecathlon: ten cells in a row with its ends bent. It takes 15
// generations to come back to this.
bool life_penta(int2 d) {
if (d.y == 1) return d.x >= 0 && d.x <= 9 && d.x != 2 && d.x != 7;
return (d.y == 0 || d.y == 2) && (d.x == 2 || d.x == 7);
}
// Bill Gosper's glider gun, 36 by 9: two blocks hold two shuttles that
// bounce between them, and each time they meet a glider flies out, down
// and to the right, once every 30 generations.
constant int2 LIFE_GUN[36] = {
int2(24, 0), int2(22, 1), int2(24, 1), int2(12, 2), int2(13, 2),
int2(20, 2), int2(21, 2), int2(34, 2), int2(35, 2), int2(11, 3),
int2(15, 3), int2(20, 3), int2(21, 3), int2(34, 3), int2(35, 3),
int2(0, 4), int2(1, 4), int2(10, 4), int2(16, 4), int2(20, 4),
int2(21, 4), int2(0, 5), int2(1, 5), int2(10, 5), int2(14, 5),
int2(16, 5), int2(17, 5), int2(22, 5), int2(24, 5), int2(10, 6),
int2(16, 6), int2(24, 6), int2(11, 7), int2(15, 7), int2(12, 8),
int2(13, 8)
};
bool life_gun(int2 d) {
if (!life_box(d, int2(36, 9))) return false;
for (int i = 0; i < 36; i++) {
if (all(d == LIFE_GUN[i])) return true;
}
return false;
}
// An eater: a still life shaped like a fishhook. A glider that runs
// into it is gone a few generations later, and the eater is whole again.
bool life_eater(int2 d) {
return all(d == int2(0, 0)) || all(d == int2(1, 0))
|| all(d == int2(0, 1)) || all(d == int2(2, 1))
|| all(d == int2(2, 2)) || all(d == int2(2, 3))
|| all(d == int2(3, 3));
}
// Every pattern, each at its own corner of the 192 by 108 grid.
bool life_placed(int2 at) {
bool on = life_pulsar(life_from(at, int2(80, 36)))
|| life_toad(life_from(at, int2(44, 66)))
|| life_beacon(life_from(at, int2(50, 68)))
|| life_toad(life_from(at, int2(150, 60)))
|| life_beacon(life_from(at, int2(176, 60)))
|| life_penta(life_from(at, int2(150, 88)))
|| life_ship(life_from(at, int2(10, 10)), true)
|| life_ship(life_from(at, int2(170, 18)), false);
for (int i = 0; i < 3; i++) {
on = on || life_blinker(life_from(at, int2(16 + 8 * i, 66)))
|| life_blinker(life_from(at, int2(140 + 8 * i, 46)));
}
// The gun, the eater at the end of its stream, and three gliders
// already on their way down the stream.
on = on || life_gun(life_from(at, int2(60, 100)))
|| life_eater(life_from(at, int2(144, 30)))
|| life_glider(life_from(at, int2(98, 76)))
|| life_glider(life_from(at, int2(113, 61)))
|| life_glider(life_from(at, int2(128, 46)));
return on;
}
// Soup: cells alive at random in the empty corner at the top right. They
// burn out into still lifes, blinkers and stray gliders, and a stray
// glider may reach the gun and break it.
bool life_soup(int2 at, constant Params& p) {
bool corner = at.x >= 130 && at.x < 180 && at.y >= 50 && at.y < 100;
return corner && life_hash(uint2(at)) < p.soup * 0.01;
}
Cell start(uint2 at, uint2 size, constant Params& p) {
bool on = life_placed(int2(at)) || life_soup(int2(at), p);
Cell c;
c.alive = on ? 1.0 : 0.0;
c.age = 0.0;
c.around = 0.0;
c.glow = c.alive;
return c;
}
// Add up the eight cells around this one, 1 for each that is alive.
float life_count(Cells cells, int2 at) {
float around = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
around += cells.at(at + int2(dx, dy)).alive;
}
}
return around;
}
// The grid steps 60 times a second, and a generation comes speed times
// a second, each on a step of its own.
bool life_tick(uint frame, float dt, float speed) {
float rate = round(1.0 / dt);
float n = float(frame);
return floor(n * speed / rate) > floor((n - 1.0) * speed / rate);
}
// Conway's rule, for every cell at once. Between generations a cell
// keeps what it is, and only its light changes.
Cell next(Cell me, uint2 at, Cells cells,
constant Uniforms& u, constant Params& p) {
Cell c = me;
c.around = life_count(cells, int2(at));
if (life_tick(u.frame, u.timeDelta, p.speed)) {
// Alive with two or three live neighbors, it lives on; dead with
// exactly three, it is born.
bool was = me.alive > 0.5;
bool lives = was ? (c.around == 2.0 || c.around == 3.0)
: c.around == 3.0;
c.alive = lives ? 1.0 : 0.0;
c.age = lives && was ? me.age + 1.0 : 0.0;
}
// A live cell lights up in a thirtieth of a second. A dead one's light
// falls to a twentieth in fade seconds.
float dt = u.timeDelta;
if (c.alive > 0.5) {
c.glow = min(1.0, me.glow + 30.0 * dt);
} else {
c.glow = p.fade > 0.0 ? me.glow * exp(-3.0 * dt / p.fade) : 0.0;
}
return c;
}
// The point of the grid under uv, in cells. The cells stay square
// whatever the picture's shape: the grid covers the picture, cropped
// at the sides or at the top and bottom.
float2 life_point(float2 uv, float2 res, float2 size) {
float perCell = max(res.x / size.x, res.y / size.y);
return size * 0.5 + (uv - 0.5) * res / perCell;
}
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A rounded square, 0.76 of a cell across: how far a point is outside it.
float life_square(float2 d) {
float2 q = abs(d) - (0.38 - 0.14);
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.14;
}
// A live cell's color: white-hot when it is born, cooling to ember over
// cool generations; while it lights up it heats from orange. A dead
// cell's afterglow is a dim ember.
float3 life_color(Cell c, constant Params& p) {
float cooled = smoothstep(0.0, 1.0, c.age / p.cool);
float3 hot = kiln((1.0 - 0.52 * cooled) * (0.5 + 0.5 * c.glow));
float3 ash = kiln(0.42) * 0.45 * c.glow;
return c.alive > 0.5 ? hot : ash;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Cells cells) {
float2 size = float2(cells.size);
float perCell = max(u.resolution.x / size.x, u.resolution.y / size.y);
float2 g = life_point(uv, u.resolution, size);
int2 home = int2(floor(g));
// The ground: near-black, with a faint dot at each cell's middle.
float2 f = g - floor(g) - 0.5;
float mark = 1.0 - smoothstep(0.03, 0.09, length(f));
float3 color = float3(0.035, 0.026, 0.030) + 0.025 * mark;
// The cell under this point is drawn as a square. It and its eight
// neighbors each spill a little light here, fading with distance.
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int2 at = home + int2(dx, dy);
Cell c = cells.at(at);
float3 light = life_color(c, p);
float2 d = g - (float2(at) + 0.5);
color += light * 0.35 * exp(-2.6 * dot(d, d));
if (dx == 0 && dy == 0) {
float body = clamp(0.5 - life_square(d) * perCell, 0.0, 1.0);
color = mix(color, light, body * c.glow);
// A dead cell warms a little with each live neighbor.
color += (1.0 - c.alive) * c.around * 0.006
* float3(1.0, 0.45, 0.15);
}
}
}
// A little darker toward the picture's edge.
float2 e = uv - 0.5;
color *= 1.0 - 0.5 * dot(e, e);
// A speck of noise, under one step of the screen's brightness, so the
// dark ground shades smoothly instead of in rings.
color += (life_hash(uint2(uv * u.resolution)) - 0.5) / 255.0;
return float4(color, 1.0);
}
float life_noise(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// The sound keeps the picture's clock: it can't see the cells. Each
// generation is a pluck, its note picked at random from a pentatonic
// scale by the generation's number. Every 30 generations, the gun's
// period, a low bell rings. Under it, a soft hum on two notes.
float2 sound(float t, constant Params& p) {
float speed = max(p.speed, 1.0);
float gen = floor(t * speed);
float2 s = float2(0.0);
// The last four generations' plucks, each dying away.
for (int k = 0; k < 4; k++) {
float g = gen - float(k);
if (g < 0.0) continue;
float age = t - g / speed;
float pick = floor(life_noise(g + 0.31) * 5.0);
float semis = pick < 1.0 ? 0.0 : pick < 2.0 ? 2.0 : pick < 3.0 ? 4.0
: pick < 4.0 ? 7.0 : 9.0;
semis += life_noise(g + 0.57) > 0.7 ? 12.0 : 0.0;
float pitch = 392.0 * pow(2.0, semis / 12.0);
float env = exp(-age * 32.0) * smoothstep(0.0, 0.003, age);
float v = (sin(TAU * pitch * age) + 0.3 * sin(TAU * 2.0 * pitch * age))
* env;
float pan = 0.5 + 0.35 * (2.0 * life_noise(g + 0.77) - 1.0);
s += 0.26 * v * float2(1.0 - pan, pan);
}
// The bell, once every 30 generations, and the one before it.
float bar = floor(gen / 30.0);
for (int k = 0; k < 2; k++) {
float b = bar - float(k);
if (b < 0.0) continue;
float age = t - b * 30.0 / speed;
float env = exp(-age * 1.6) * smoothstep(0.0, 0.004, age);
float bell = sin(TAU * 98.0 * age)
+ 0.5 * exp(-age * 2.0) * sin(TAU * 98.0 * 2.76 * age)
+ 0.25 * exp(-age * 4.0) * sin(TAU * 98.0 * 5.40 * age);
s += 0.21 * bell * env;
}
// The hum: G and D, slowly breathing.
float breath = 0.75 + 0.25 * sin(TAU * t / 8.0);
float hum = sin(TAU * 49.0 * t) + 0.6 * sin(TAU * 73.4 * t);
s += 0.14 * hum * breath;
float fade = smoothstep(0.0, 1.5, t);
return s * fade * p.volume;
}Running it in your own project
Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.
To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:
Drawing it, in Swift
struct Uniforms {
var resolution: SIMD2<Float>
var time: Float
var timeDelta: Float
var mouse: SIMD2<Float>
var frame: UInt32
var pad: UInt32 = 0
}
let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [8, 12, 0.6, 0, 0.8] // the @param defaults, in the order they are declared
encoder.setRenderPipelineState(pipeline)
encoder.setFragmentBytes(&uniforms, length: MemoryLayout<Uniforms>.stride, index: 0)
encoder.setFragmentBytes(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
More in Particles & Simulation
Press ? for keyboard shortcuts.


