Shader

Penrose Tiles

A floor of two tiles, a thick rhomb and a thin one, laid so that the pattern never repeats. Five grids of parallel lines, a fifth of a turn apart, decide everything. Each crossing of two lines is one tile, and the shader finds the tile under every pixel on its own. In their symmetric places the grids give the floor five-fold symmetry; slide them and tiles flip in threes, the symmetry breaks, and half a loop later it is back.

· Shapes & Symmetry · Fragment shader

Teaches An aperiodic tiling from five grids, symmetry that never repeats

Builds on Flowing Color Field, Stained Glass (a bell)

Watch

The idea

A wallpaper repeats: slide a copy of it the right distance and it lies exactly over itself. Only a few symmetries can live in a pattern like that, turns by a half, a third, a quarter and a sixth. A fifth of a turn can't. In the 1970s Roger Penrose found tiles that cover the plane and never repeat, and the best known pair is two rhombs with the same edge: a thick one with corners of 72 and 108 degrees, and a thin one with 36 and 144. Laid by his rules they make an aperiodic tiling: order everywhere, stars and suns that turn up again and again, and no shift that lays the floor over itself.

Laying tiles one at a time is no use to a fragment shader, where every pixel has to work out its own color alone. Nicolaas de Bruijn's 1981 construction gets round that. Draw five grids of parallel lines, each a fifth of a turn from the last: a pentagrid. Every crossing of two lines stands for one tile, and the lines of the other three grids say where that tile goes. So a pixel only needs to look at a handful of crossings near it to find its tile.

The grids also explain the symmetry. With all five slid by the same amount, the picture is the same after a fifth of a turn. Slide each by a different amount and that is gone, though the tiling is still a Penrose tiling. This episode's new functionality, the playground's symmetry view, shows that as it happens.

It is built in four steps, and each step is a shader you can run on its own.

Stage 1 · Five grids

gridDir(j) is the direction grid j counts along: five unit vectors 72 degrees apart, the first pointing straight up, which makes the pattern its own mirror image left to right. For a point x, grid j's count is dot(x, gridDir(j)) + gridShift(j, slide), and the grid has a line wherever that count is a whole number. So abs(fract(count + 0.5) - 0.5) is how far the nearest line is, in the grid's own spacing.

The picture is measured in tile edges from its middle (y, with tiles edges up the picture), and the grids are read at x = 0.4 × y. That 2/5 is where the tiles will be: a step along each of the five directions, weighted by the five counts, comes to 5/2 of the point you started from, so the tiling is the grids' plane stretched by 5/2. One line's distance, times 2.5, is then in tile edges, and line is its width. The five lines' coverage is added up and sent through the kiln palette from Flowing Color Field, so a line is ember and a crossing gold.

gridShift is how far each grid is slid along its direction: 0.2 + slide × share[j]. At 0.2 each, the five grids are alike and the picture has five-fold symmetry about its middle. (At 0 each, all five would send a line through the middle, and no tiling comes of that.) The shares are 1, −0.809, 0.309, 0.309 and −0.809, the cosines of 0, 144, 288, 432 and 576 degrees. They add up to nothing, so the five shifts always add up to 1. De Bruijn's condition for Penrose's tiles is exactly that: the shifts sum to a whole number. And since shares 1 and 4 are equal, and 2 and 3, the left-to-right mirror survives any slide.

slideAt moves the slide over the loop: 0.5 × slide × sin(2π × t × speed / 60). With the defaults it goes out to 0.15 at 15 s, back through 0 at 30 s, to −0.15 at 45 s and home at 60 s.

stage-1-five-grids.metal
// Stage 1 · Five grids
// Five grids of parallel lines, a fifth of a turn apart. Each grid is a
// count along its own direction, with a line wherever the count is a whole
// number. In their symmetric places the picture is the same after a fifth
// of a turn. Slide the grids and the crossings move.

// @param speed 1 0 3 How fast the grids slide
// @param tiles 13 6 30 How many tile edges fit the picture's height
// @param slide 0.3 0 1 How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
// @param line 0.05 0.015 0.15 How wide the grid lines are, in tile edges
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
constant float PHI = 1.6180340;

// The kiln palette from Flowing Color Field.
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));
}

// Grid j's direction: five, a fifth of a turn apart, the first pointing up.
float2 gridDir(int j) {
    const float2 dirs[5] = {
        float2(0.0, 1.0), float2(-0.9510565, 0.3090170),
        float2(-0.5877853, -0.8090170), float2(0.5877853, -0.8090170),
        float2(0.9510565, 0.3090170)};
    return dirs[j];
}

// How far grid j is slid. At 0.2 each the grids are alike: five-fold
// symmetry. The shares add up to nothing.
float gridShift(int j, float slide) {
    const float share[5] = {
        1.0, -0.8090170, 0.3090170, 0.3090170, -0.8090170};
    return 0.2 + slide * share[j];
}

// The slide over the loop: out, back, out the other way and back.
float slideAt(float t, constant Params& p) {
    return 0.5 * p.slide * sin(TAU * t * p.speed / 60.0);
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    // The floor in tile edges from its middle. The grids' plane is 2/5 of
    // that, so the lines lie where the tiles they make will be.
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * p.tiles;
    float2 x = 0.4 * y;
    float slide = slideAt(u.time, p);
    float pixel = p.tiles / u.resolution.y;
    float lines = 0.0;
    for (int j = 0; j < 5; j++) {
        // Grid j: a line wherever this count is a whole number.
        float count = dot(x, gridDir(j)) + gridShift(j, slide);
        // How far the nearest of its lines is, in tile edges.
        float d = abs(fract(count + 0.5) - 0.5) * 2.5;
        lines += 1.0 - smoothstep(0.5 * p.line - pixel,
                                  0.5 * p.line + pixel, d);
    }
    // One line is ember, and where two cross it burns gold.
    float3 color = kiln(0.06 + 0.36 * min(lines, 2.5));
    return float4(color, 1.0);
}

// ---- The sound ---------------------------------------------------------------

float2 sound(float t, constant Params& p) {
    float ts = t * max(p.speed, 0.05);
    float high = 0.0;
    float low = 0.0;
    // A hum on D2 and its fifth; the fifth leans out of tune as the grids
    // slide from their symmetric places, and comes back with them.
    float lean = slideAt(t, p);
    float hum = 0.5 * sin(TAU * 73.42 * t);
    hum += 0.3 * sin(TAU * 110.0 * (1.0 + 0.03 * lean) * t);
    float left = 0.35 * high + 0.12 * low + 0.30 * hum;
    float right = 0.12 * high + 0.35 * low + 0.30 * hum;
    // No click at the start or at the loop's end.
    float ends = smoothstep(0.0, 0.05, t) * smoothstep(60.0, 59.9, t);
    return float2(left, right) * p.volume * ends;
}

Stage 2 · A tile at every crossing

tileAt(y, slide) finds the tile that holds the point y. A tile belongs to one crossing: line ki of grid i and line kj of grid j. They cross at q, found by solving the two lines' equations (cross, the cross product of the two directions, is the determinant). The tile's corner is a sum of steps: ki steps along grid i's direction, kj along grid j's, and for each of the other three grids as many steps as the count of the next line beyond q, ceil(dot(q, em) + shift). Its two edges are the two grids' directions, each one tile edge long.

Whether y is in that tile is one more pair of equations: at is y measured from the corner along the two edges, and the tile holds it when both parts are from 0 to 1. Grids next to each other (j − i of 1 or 4) cross at 72 degrees and make a thick rhomb; grids two apart cross at 36 and make a thin one.

Which crossings to try? y's own place among the lines is near x = 0.4 × (y − moved), where moved is how far the shifts move the whole floor. For each of the ten pairs of grids the function takes the line just below x in each grid and the one above: four crossings a pair, forty in all. Exactly one of those forty tiles holds y. (That was checked outside the shader, on 200,000 points for seven settings of the shifts: every point was in one tile and never in two.)

edgeDistance is how far the point is from its tile's nearest edge: the smaller of at and 1 − at, times the sine of the tile's corner angle, in tile edges. Within half of grout of an edge the color goes to dark, softened over one pixel. Thick tiles are ember, kiln(0.42), and thin ones gold, kiln(0.66).

Watch the tiles as the grids slide. Nothing moves smoothly: every so often three lines pass through one point, and the three tiles around it, a hexagon of two of one kind and one of the other, flip over.

stage-2-a-tile-at-every-crossing.metal94 new lines since stage 1
// Stage 2 · A tile at every crossing
// Every crossing of two lines becomes one tile: a rhomb whose edges are
// the two grids' directions, thick where they cross at 72 degrees, thin
// at 36. The other three grids say where it goes. As the grids slide,
// three lines pass through one point and three tiles flip.

// @param speed 1 0 3 How fast the grids slide
// @param tiles 13 6 30 How many tile edges fit the picture's height
// @param slide 0.3 0 1 How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
// @param grout 0.05 0.015 0.15 How wide the dark lines between the tiles are, in tile edges
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
constant float PHI = 1.6180340;

// The kiln palette from Flowing Color Field.
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));
}

// Grid j's direction: five, a fifth of a turn apart, the first pointing up.
float2 gridDir(int j) {
    const float2 dirs[5] = {
        float2(0.0, 1.0), float2(-0.9510565, 0.3090170),
        float2(-0.5877853, -0.8090170), float2(0.5877853, -0.8090170),
        float2(0.9510565, 0.3090170)};
    return dirs[j];
}

// How far grid j is slid. At 0.2 each the grids are alike: five-fold
// symmetry. The shares add up to nothing.
float gridShift(int j, float slide) {
    const float share[5] = {
        1.0, -0.8090170, 0.3090170, 0.3090170, -0.8090170};
    return 0.2 + slide * share[j];
}

// The slide over the loop: out, back, out the other way and back.
float slideAt(float t, constant Params& p) {
    return 0.5 * p.slide * sin(TAU * t * p.speed / 60.0);
}

struct Tile {
    float2 at;      // where the point is in its tile, 0...1 along each edge
    float thin;     // 0 for a thick rhomb, 1 for a thin one
    float sine;     // the sine of the tile's corner angle
};

// The tile that holds y. Every crossing of two grids' lines is one tile;
// the four crossings nearest y are tried for each of the ten pairs of
// grids, and one of those tiles holds y.
Tile tileAt(float2 y, float slide) {
    Tile t;
    t.at = float2(0.5);
    t.thin = 0.0;
    t.sine = 1.0;
    // The tiles are the grids' plane stretched by 5/2, so y lies near
    // 2/5 of it.
    float2 moved = float2(0.0);
    for (int m = 0; m < 5; m++) {
        moved += gridShift(m, slide) * gridDir(m);
    }
    float2 x = 0.4 * (y - moved);
    for (int i = 0; i < 4; i++) {
        float2 ei = gridDir(i);
        float gi = gridShift(i, slide);
        float ni = floor(dot(x, ei) + gi);
        for (int j = i + 1; j < 5; j++) {
            float2 ej = gridDir(j);
            float gj = gridShift(j, slide);
            float nj = floor(dot(x, ej) + gj);
            float cross = ei.x * ej.y - ei.y * ej.x;
            for (int a = 0; a < 2; a++) {
                for (int b = 0; b < 2; b++) {
                    // Lines ki and kj cross at q.
                    float ki = ni + float(a);
                    float kj = nj + float(b);
                    float si = ki - gi;
                    float sj = kj - gj;
                    float2 q = float2(si * ej.y - sj * ei.y,
                                      sj * ei.x - si * ej.x) / cross;
                    // The tile's corner: a step along a grid's direction
                    // for each line passed, the next line beyond q in the
                    // other three grids.
                    float2 corner = ki * ei + kj * ej;
                    for (int m = 0; m < 5; m++) {
                        if (m == i || m == j) { continue; }
                        float2 em = gridDir(m);
                        float km = ceil(dot(q, em) + gridShift(m, slide));
                        corner += km * em;
                    }
                    // y from that corner, along the tile's two edges.
                    float2 d = y - corner;
                    float2 at = float2(d.x * ej.y - d.y * ej.x,
                                       ei.x * d.y - ei.y * d.x) / cross;
                    bool inside = at.x >= 0.0 && at.x < 1.0
                               && at.y >= 0.0 && at.y < 1.0;
                    if (inside) {
                        // Neighboring grids cross at 72 degrees: thick.
                        // Grids two apart, at 36: thin.
                        bool thick = (j - i == 1) || (j - i == 4);
                        t.at = at;
                        t.thin = thick ? 0.0 : 1.0;
                        t.sine = abs(cross);
                    }
                }
            }
        }
    }
    return t;
}

// How far the point is from its tile's nearest edge, in tile edges.
float edgeDistance(Tile t) {
    float2 d = min(t.at, 1.0 - t.at);
    return min(d.x, d.y) * t.sine;
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * p.tiles;
    Tile tile = tileAt(y, slideAt(u.time, p));
    // Thick tiles ember, thin tiles gold.
    float3 color = kiln(mix(0.42, 0.66, tile.thin));
    // Dark grout where two tiles meet, softened over a pixel.
    float d = edgeDistance(tile);
    float pixel = p.tiles / u.resolution.y;
    float line = smoothstep(0.5 * p.grout - pixel,
                            0.5 * p.grout + pixel, d);
    color = mix(kiln(0.06), color, line);
    return float4(color, 1.0);
}

// ---- The sound ---------------------------------------------------------------

// A bell: a tone and two partials that are not whole multiples of it, each
// dying away at its own rate. since is the time since it was struck.
float bell(float f, float since) {
    if (since < 0.0) { return 0.0; }
    float attack = smoothstep(0.0, 0.004, since);
    float tone = sin(TAU * f * since) * exp(-since * 3.0);
    tone += 0.45 * sin(TAU * f * 2.76 * since) * exp(-since * 6.0);
    tone += 0.25 * sin(TAU * f * 5.40 * since) * exp(-since * 11.0);
    return tone * attack;
}

// When beat n falls. Penrose's tiles hide rows of parallel lines (Ammann's
// bars) whose gaps come in two lengths, PHI to 1, in the order of the
// Fibonacci word, which never repeats. Beat n comes after n gaps,
// floor(n / PHI) of them long.
float beatTime(float n, float gap) {
    return gap * (n + (PHI - 1.0) * floor(n / PHI));
}

// One rhythm of bells: the last beats before t ring on. The notes are a
// pentatonic scale on D, five like the grids, and which one a beat plays
// comes from the same count of long gaps, so the tune never repeats.
float rhythm(float t, float gap, float base, float shift) {
    // The mean gap is gap * (2 - 1 / PHI), so the beat at t is near this.
    float near = floor(t / (gap * (2.0 - 1.0 / PHI)));
    float sum = 0.0;
    for (int k = -4; k <= 1; k++) {
        float n = near + float(k);
        if (n < 0.0) { continue; }
        float longs = floor(n / PHI);
        const float scale[5] = {1.0, 1.125, 1.25, 1.5, 1.6666667};
        int note = int(fmod(n + 2.0 * longs + shift, 5.0));
        // A beat before a long gap is struck harder.
        float hard = (floor((n + 1.0) / PHI) - longs) > 0.5 ? 1.0 : 0.6;
        sum += hard * bell(base * scale[note], t - beatTime(n, gap));
    }
    return sum;
}

float2 sound(float t, constant Params& p) {
    float ts = t * max(p.speed, 0.05);
    float high = rhythm(ts, 0.42, 293.66, 0.0);
    float low = 0.0;
    // A hum on D2 and its fifth; the fifth leans out of tune as the grids
    // slide from their symmetric places, and comes back with them.
    float lean = slideAt(t, p);
    float hum = 0.5 * sin(TAU * 73.42 * t);
    hum += 0.3 * sin(TAU * 110.0 * (1.0 + 0.03 * lean) * t);
    float left = 0.35 * high + 0.12 * low + 0.17 * hum;
    float right = 0.12 * high + 0.35 * low + 0.17 * hum;
    // No click at the start or at the loop's end.
    float ends = smoothstep(0.0, 0.05, t) * smoothstep(60.0, 59.9, t);
    return float2(left, right) * p.volume * ends;
}
13 lines from stage 1 that are gone
    float2 x = 0.4 * y;
    float slide = slideAt(u.time, p);
⋯
    float lines = 0.0;
    for (int j = 0; j < 5; j++) {
⋯
        float count = dot(x, gridDir(j)) + gridShift(j, slide);
⋯
        float d = abs(fract(count + 0.5) - 0.5) * 2.5;
        lines += 1.0 - smoothstep(0.5 * p.line - pixel,
                                  0.5 * p.line + pixel, d);
    }
⋯
    float3 color = kiln(0.06 + 0.36 * min(lines, 2.5));
⋯
    float high = 0.0;
⋯
    float left = 0.35 * high + 0.12 * low + 0.30 * hum;
    float right = 0.12 * high + 0.35 * low + 0.30 * hum;

Stage 3 · Color that keeps the symmetry

The tiles have five-fold symmetry at the symmetric moments, but a picture only has the symmetry its colors have. So a tile's color is made from things that a turn of the floor about its middle, or its mirror, leaves alone. Tile gains two fields for that: middle, the tile's middle (its corner plus half of each edge), and along, the direction of its long diagonal (the sum of the two edge directions for a thick tile, the difference for a thin one).

The first is distance. r is how far the tile's middle is from the floor's, and ring = 0.5 + 0.5 × sin(2π × (r / rings − t / 6)) is a wave of heat running outwards, a new ring every 6 seconds and rings tile edges apart. A tile takes the heat at its own middle, so it stays one flat color and whole tiles light as a ring passes. glow is how much it adds.

The second is points, the absolute value of the dot product of along with the direction out from the floor's middle: 1 for a tile whose long diagonal points at the middle, 0 for one lying across. It moves the tile 0.22 along the palette. The absolute value matters: a diagonal has no front or back, and without it two tiles a half turn apart in direction would differ.

stage-3-color-that-keeps-the-symmetry.metal14 new lines since stage 2
// Stage 3 · Color that keeps the symmetry
// A tile's color comes from things a turn of the floor leaves alone: how
// far its middle is from the floor's, and how its long diagonal lies to
// the line out from the middle. Heat runs outwards in rings.

// @param speed 1 0 3 How fast the grids slide and the heat runs outwards
// @param tiles 13 6 30 How many tile edges fit the picture's height
// @param slide 0.3 0 1 How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
// @param grout 0.05 0.015 0.15 How wide the dark lines between the tiles are, in tile edges
// @param rings 5 2 12 How many tile edges from one ring of heat to the next
// @param glow 0.6 0 1 How much the rings of heat change a tile
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
constant float PHI = 1.6180340;

// The kiln palette from Flowing Color Field.
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));
}

// Grid j's direction: five, a fifth of a turn apart, the first pointing up.
float2 gridDir(int j) {
    const float2 dirs[5] = {
        float2(0.0, 1.0), float2(-0.9510565, 0.3090170),
        float2(-0.5877853, -0.8090170), float2(0.5877853, -0.8090170),
        float2(0.9510565, 0.3090170)};
    return dirs[j];
}

// How far grid j is slid. At 0.2 each the grids are alike: five-fold
// symmetry. The shares add up to nothing.
float gridShift(int j, float slide) {
    const float share[5] = {
        1.0, -0.8090170, 0.3090170, 0.3090170, -0.8090170};
    return 0.2 + slide * share[j];
}

// The slide over the loop: out, back, out the other way and back.
float slideAt(float t, constant Params& p) {
    return 0.5 * p.slide * sin(TAU * t * p.speed / 60.0);
}

struct Tile {
    float2 at;      // where the point is in its tile, 0...1 along each edge
    float thin;     // 0 for a thick rhomb, 1 for a thin one
    float sine;     // the sine of the tile's corner angle
    float2 middle;  // the tile's middle, in tile edges from the floor's
    float2 along;   // its long diagonal, a unit vector
};

// The tile that holds y. Every crossing of two grids' lines is one tile;
// the four crossings nearest y are tried for each of the ten pairs of
// grids, and one of those tiles holds y.
Tile tileAt(float2 y, float slide) {
    Tile t;
    t.at = float2(0.5);
    t.thin = 0.0;
    t.sine = 1.0;
    t.middle = y;
    t.along = float2(0.0, 1.0);
    // The tiles are the grids' plane stretched by 5/2, so y lies near
    // 2/5 of it.
    float2 moved = float2(0.0);
    for (int m = 0; m < 5; m++) {
        moved += gridShift(m, slide) * gridDir(m);
    }
    float2 x = 0.4 * (y - moved);
    for (int i = 0; i < 4; i++) {
        float2 ei = gridDir(i);
        float gi = gridShift(i, slide);
        float ni = floor(dot(x, ei) + gi);
        for (int j = i + 1; j < 5; j++) {
            float2 ej = gridDir(j);
            float gj = gridShift(j, slide);
            float nj = floor(dot(x, ej) + gj);
            float cross = ei.x * ej.y - ei.y * ej.x;
            for (int a = 0; a < 2; a++) {
                for (int b = 0; b < 2; b++) {
                    // Lines ki and kj cross at q.
                    float ki = ni + float(a);
                    float kj = nj + float(b);
                    float si = ki - gi;
                    float sj = kj - gj;
                    float2 q = float2(si * ej.y - sj * ei.y,
                                      sj * ei.x - si * ej.x) / cross;
                    // The tile's corner: a step along a grid's direction
                    // for each line passed, the next line beyond q in the
                    // other three grids.
                    float2 corner = ki * ei + kj * ej;
                    for (int m = 0; m < 5; m++) {
                        if (m == i || m == j) { continue; }
                        float2 em = gridDir(m);
                        float km = ceil(dot(q, em) + gridShift(m, slide));
                        corner += km * em;
                    }
                    // y from that corner, along the tile's two edges.
                    float2 d = y - corner;
                    float2 at = float2(d.x * ej.y - d.y * ej.x,
                                       ei.x * d.y - ei.y * d.x) / cross;
                    bool inside = at.x >= 0.0 && at.x < 1.0
                               && at.y >= 0.0 && at.y < 1.0;
                    if (inside) {
                        // Neighboring grids cross at 72 degrees: thick.
                        // Grids two apart, at 36: thin.
                        bool thick = (j - i == 1) || (j - i == 4);
                        t.at = at;
                        t.thin = thick ? 0.0 : 1.0;
                        t.sine = abs(cross);
                        t.middle = corner + 0.5 * (ei + ej);
                        t.along = normalize(thick ? ei + ej : ei - ej);
                    }
                }
            }
        }
    }
    return t;
}

// How far the point is from its tile's nearest edge, in tile edges.
float edgeDistance(Tile t) {
    float2 d = min(t.at, 1.0 - t.at);
    return min(d.x, d.y) * t.sine;
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float t = u.time * p.speed;
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * p.tiles;
    Tile tile = tileAt(y, slideAt(u.time, p));
    // Heat runs outwards in rings, and each tile takes the heat at its
    // own middle, so it is one flat color.
    float r = length(tile.middle);
    float ring = 0.5 + 0.5 * sin(TAU * (r / p.rings - t / 6.0));
    // A tile whose long diagonal points at the floor's middle burns
    // brighter than one that lies across. Both are the same after a
    // fifth of a turn, so the color keeps the symmetry the tiles have.
    float2 out = tile.middle / max(r, 0.001);
    float points = abs(dot(tile.along, out));
    float fire = mix(0.42, 0.66, tile.thin) + 0.22 * (points - 0.5);
    fire += p.glow * 0.36 * (ring - 0.5);
    float3 color = kiln(fire);
    // Dark grout where two tiles meet, softened over a pixel.
    float d = edgeDistance(tile);
    float pixel = p.tiles / u.resolution.y;
    float line = smoothstep(0.5 * p.grout - pixel,
                            0.5 * p.grout + pixel, d);
    color = mix(kiln(0.06), color, line);
    return float4(color, 1.0);
}

// ---- The sound ---------------------------------------------------------------

// A bell: a tone and two partials that are not whole multiples of it, each
// dying away at its own rate. since is the time since it was struck.
float bell(float f, float since) {
    if (since < 0.0) { return 0.0; }
    float attack = smoothstep(0.0, 0.004, since);
    float tone = sin(TAU * f * since) * exp(-since * 3.0);
    tone += 0.45 * sin(TAU * f * 2.76 * since) * exp(-since * 6.0);
    tone += 0.25 * sin(TAU * f * 5.40 * since) * exp(-since * 11.0);
    return tone * attack;
}

// When beat n falls. Penrose's tiles hide rows of parallel lines (Ammann's
// bars) whose gaps come in two lengths, PHI to 1, in the order of the
// Fibonacci word, which never repeats. Beat n comes after n gaps,
// floor(n / PHI) of them long.
float beatTime(float n, float gap) {
    return gap * (n + (PHI - 1.0) * floor(n / PHI));
}

// One rhythm of bells: the last beats before t ring on. The notes are a
// pentatonic scale on D, five like the grids, and which one a beat plays
// comes from the same count of long gaps, so the tune never repeats.
float rhythm(float t, float gap, float base, float shift) {
    // The mean gap is gap * (2 - 1 / PHI), so the beat at t is near this.
    float near = floor(t / (gap * (2.0 - 1.0 / PHI)));
    float sum = 0.0;
    for (int k = -4; k <= 1; k++) {
        float n = near + float(k);
        if (n < 0.0) { continue; }
        float longs = floor(n / PHI);
        const float scale[5] = {1.0, 1.125, 1.25, 1.5, 1.6666667};
        int note = int(fmod(n + 2.0 * longs + shift, 5.0));
        // A beat before a long gap is struck harder.
        float hard = (floor((n + 1.0) / PHI) - longs) > 0.5 ? 1.0 : 0.6;
        sum += hard * bell(base * scale[note], t - beatTime(n, gap));
    }
    return sum;
}

float2 sound(float t, constant Params& p) {
    float ts = t * max(p.speed, 0.05);
    float high = rhythm(ts, 0.42, 293.66, 0.0);
    float low = 0.0;
    // A hum on D2 and its fifth; the fifth leans out of tune as the grids
    // slide from their symmetric places, and comes back with them.
    float lean = slideAt(t, p);
    float hum = 0.5 * sin(TAU * 73.42 * t);
    hum += 0.3 * sin(TAU * 110.0 * (1.0 + 0.03 * lean) * t);
    float left = 0.35 * high + 0.12 * low + 0.17 * hum;
    float right = 0.12 * high + 0.35 * low + 0.17 * hum;
    // No click at the start or at the loop's end.
    float ends = smoothstep(0.0, 0.05, t) * smoothstep(60.0, 59.9, t);
    return float2(left, right) * p.volume * ends;
}
1 line from stage 2 that are gone
    float3 color = kiln(mix(0.42, 0.66, tile.thin));

Stage 4 · Penrose Tiles

The finished shader makes it a fired floor. Glaze pools in the middle of each tile and thins at its edges: pool rises from 0 at the edge to 1 at 0.02 + 0.3 × bevel tile edges in, and the color is multiplied by 0.55 + 0.45 × pool, with a little white-hot added where a ring of heat is passing. The floor is cooler towards its rim (fire loses up to 0.1), and it breathes: the number of tiles across swells and shrinks by 6% twice a loop. All of that is still made of distances from the middle, so none of it breaks the symmetry.

The whole minute loops: the frame at 60 s is the first frame.

the kiln palette and shade(), from the finished shader
// The kiln palette from Flowing Color Field.
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) {
    float aspect = u.resolution.x / u.resolution.y;
    float t = u.time * p.speed;
    // The floor breathes in and out a little, twice a loop.
    float across = p.tiles * (1.0 + 0.06 * sin(TAU * t / 30.0));
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * across;
    float pixel = across / u.resolution.y;
    Tile tile = tileAt(y, slideAt(u.time, p));
    // Heat runs outwards in rings, and each tile takes the heat at its
    // own middle, so it is one flat color.
    float r = length(tile.middle);
    float ring = 0.5 + 0.5 * sin(TAU * (r / p.rings - t / 6.0));
    // A tile whose long diagonal points at the floor's middle burns
    // brighter than one that lies across. Both are the same after a
    // fifth of a turn, so the color keeps the symmetry the tiles have.
    float2 out = tile.middle / max(r, 0.001);
    float points = abs(dot(tile.along, out));
    float fire = mix(0.42, 0.66, tile.thin) + 0.22 * (points - 0.5);
    fire += p.glow * 0.36 * (ring - 0.5);
    // The floor is cooler towards its rim.
    fire -= 0.10 * smoothstep(0.0, 1.2, r / across);
    float3 color = kiln(fire);
    float d = edgeDistance(tile);
    // Glaze pools in the middle of a tile and thins towards its edges,
    // and the pool catches the heat as a ring passes.
    float pool = smoothstep(0.0, 0.02 + 0.3 * p.bevel, d);
    color *= mix(1.0, 0.55 + 0.45 * pool, step(0.001, p.bevel));
    color += 0.10 * kiln(0.9) * pool * pool * ring * p.glow;
    // Dark grout where two tiles meet, softened over a pixel.
    float line = smoothstep(0.5 * p.grout - pixel,
                            0.5 * p.grout + pixel, d);
    color = mix(kiln(0.06), color, line);
    return float4(color, 1.0);
}
penrose-tiles.metal, the whole file9 new lines since stage 3
// Ray Kiln · Penrose Tiles
//
// @episode Penrose Tiles
// @length 60
// @variation 30
// @variations 1,2,4,5,6,7,10,11
// @still 30.0
// @teaches An aperiodic tiling from five grids, symmetry that never repeats
// @category Fragment
// @tags penrose tiling, aperiodic tiling, pentagrid, quasicrystal, symmetry, procedural sound
// @final Penrose Tiles | Glaze pools in each tile and thins at its edges, the floor breathes, and bells follow the long and short gaps.
//
// Two tiles, a thick rhomb and a thin one, cover the plane without the
// pattern ever repeating. De Bruijn's way to lay them: five grids of
// parallel lines, a fifth of a turn apart. Every crossing of two lines
// is one tile, thick where the lines cross at 72 degrees, thin at 36,
// and the lines a point lies beyond in the other three grids say where
// the tile goes. With the grids in their symmetric places the whole
// floor has five-fold symmetry about its middle. Slide them and tiles
// flip in threes: the five-fold symmetry goes at once, a mirror stays,
// and at half the loop everything is back.
// 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

// @param speed 1 0 3 How fast the grids slide and the heat runs outwards
// @param tiles 13 6 30 How many tile edges fit the picture's height
// @param slide 0.3 0 1 How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
// @param grout 0.05 0.015 0.15 How wide the dark lines between the tiles are, in tile edges
// @param rings 5 2 12 How many tile edges from one ring of heat to the next
// @param glow 0.6 0 1 How much the rings of heat change a tile
// @param bevel 0.5 0 1 How far the shade at a tile's edge reaches in
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
constant float PHI = 1.6180340;

// The kiln palette from Flowing Color Field.
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));
}

// Grid j's direction: five, a fifth of a turn apart, the first pointing up.
float2 gridDir(int j) {
    const float2 dirs[5] = {
        float2(0.0, 1.0), float2(-0.9510565, 0.3090170),
        float2(-0.5877853, -0.8090170), float2(0.5877853, -0.8090170),
        float2(0.9510565, 0.3090170)};
    return dirs[j];
}

// How far grid j is slid. At 0.2 each the grids are alike: five-fold
// symmetry. The shares add up to nothing.
float gridShift(int j, float slide) {
    const float share[5] = {
        1.0, -0.8090170, 0.3090170, 0.3090170, -0.8090170};
    return 0.2 + slide * share[j];
}

// The slide over the loop: out, back, out the other way and back.
float slideAt(float t, constant Params& p) {
    return 0.5 * p.slide * sin(TAU * t * p.speed / 60.0);
}

struct Tile {
    float2 at;      // where the point is in its tile, 0...1 along each edge
    float thin;     // 0 for a thick rhomb, 1 for a thin one
    float sine;     // the sine of the tile's corner angle
    float2 middle;  // the tile's middle, in tile edges from the floor's
    float2 along;   // its long diagonal, a unit vector
};

// The tile that holds y. Every crossing of two grids' lines is one tile;
// the four crossings nearest y are tried for each of the ten pairs of
// grids, and one of those tiles holds y.
Tile tileAt(float2 y, float slide) {
    Tile t;
    t.at = float2(0.5);
    t.thin = 0.0;
    t.sine = 1.0;
    t.middle = y;
    t.along = float2(0.0, 1.0);
    // The tiles are the grids' plane stretched by 5/2, so y lies near
    // 2/5 of it.
    float2 moved = float2(0.0);
    for (int m = 0; m < 5; m++) {
        moved += gridShift(m, slide) * gridDir(m);
    }
    float2 x = 0.4 * (y - moved);
    for (int i = 0; i < 4; i++) {
        float2 ei = gridDir(i);
        float gi = gridShift(i, slide);
        float ni = floor(dot(x, ei) + gi);
        for (int j = i + 1; j < 5; j++) {
            float2 ej = gridDir(j);
            float gj = gridShift(j, slide);
            float nj = floor(dot(x, ej) + gj);
            float cross = ei.x * ej.y - ei.y * ej.x;
            for (int a = 0; a < 2; a++) {
                for (int b = 0; b < 2; b++) {
                    // Lines ki and kj cross at q.
                    float ki = ni + float(a);
                    float kj = nj + float(b);
                    float si = ki - gi;
                    float sj = kj - gj;
                    float2 q = float2(si * ej.y - sj * ei.y,
                                      sj * ei.x - si * ej.x) / cross;
                    // The tile's corner: a step along a grid's direction
                    // for each line passed, the next line beyond q in the
                    // other three grids.
                    float2 corner = ki * ei + kj * ej;
                    for (int m = 0; m < 5; m++) {
                        if (m == i || m == j) { continue; }
                        float2 em = gridDir(m);
                        float km = ceil(dot(q, em) + gridShift(m, slide));
                        corner += km * em;
                    }
                    // y from that corner, along the tile's two edges.
                    float2 d = y - corner;
                    float2 at = float2(d.x * ej.y - d.y * ej.x,
                                       ei.x * d.y - ei.y * d.x) / cross;
                    bool inside = at.x >= 0.0 && at.x < 1.0
                               && at.y >= 0.0 && at.y < 1.0;
                    if (inside) {
                        // Neighboring grids cross at 72 degrees: thick.
                        // Grids two apart, at 36: thin.
                        bool thick = (j - i == 1) || (j - i == 4);
                        t.at = at;
                        t.thin = thick ? 0.0 : 1.0;
                        t.sine = abs(cross);
                        t.middle = corner + 0.5 * (ei + ej);
                        t.along = normalize(thick ? ei + ej : ei - ej);
                    }
                }
            }
        }
    }
    return t;
}

// How far the point is from its tile's nearest edge, in tile edges.
float edgeDistance(Tile t) {
    float2 d = min(t.at, 1.0 - t.at);
    return min(d.x, d.y) * t.sine;
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float t = u.time * p.speed;
    // The floor breathes in and out a little, twice a loop.
    float across = p.tiles * (1.0 + 0.06 * sin(TAU * t / 30.0));
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * across;
    float pixel = across / u.resolution.y;
    Tile tile = tileAt(y, slideAt(u.time, p));
    // Heat runs outwards in rings, and each tile takes the heat at its
    // own middle, so it is one flat color.
    float r = length(tile.middle);
    float ring = 0.5 + 0.5 * sin(TAU * (r / p.rings - t / 6.0));
    // A tile whose long diagonal points at the floor's middle burns
    // brighter than one that lies across. Both are the same after a
    // fifth of a turn, so the color keeps the symmetry the tiles have.
    float2 out = tile.middle / max(r, 0.001);
    float points = abs(dot(tile.along, out));
    float fire = mix(0.42, 0.66, tile.thin) + 0.22 * (points - 0.5);
    fire += p.glow * 0.36 * (ring - 0.5);
    // The floor is cooler towards its rim.
    fire -= 0.10 * smoothstep(0.0, 1.2, r / across);
    float3 color = kiln(fire);
    float d = edgeDistance(tile);
    // Glaze pools in the middle of a tile and thins towards its edges,
    // and the pool catches the heat as a ring passes.
    float pool = smoothstep(0.0, 0.02 + 0.3 * p.bevel, d);
    color *= mix(1.0, 0.55 + 0.45 * pool, step(0.001, p.bevel));
    color += 0.10 * kiln(0.9) * pool * pool * ring * p.glow;
    // Dark grout where two tiles meet, softened over a pixel.
    float line = smoothstep(0.5 * p.grout - pixel,
                            0.5 * p.grout + pixel, d);
    color = mix(kiln(0.06), color, line);
    return float4(color, 1.0);
}

// ---- The sound ---------------------------------------------------------------

// A bell: a tone and two partials that are not whole multiples of it, each
// dying away at its own rate. since is the time since it was struck.
float bell(float f, float since) {
    if (since < 0.0) { return 0.0; }
    float attack = smoothstep(0.0, 0.004, since);
    float tone = sin(TAU * f * since) * exp(-since * 3.0);
    tone += 0.45 * sin(TAU * f * 2.76 * since) * exp(-since * 6.0);
    tone += 0.25 * sin(TAU * f * 5.40 * since) * exp(-since * 11.0);
    return tone * attack;
}

// When beat n falls. Penrose's tiles hide rows of parallel lines (Ammann's
// bars) whose gaps come in two lengths, PHI to 1, in the order of the
// Fibonacci word, which never repeats. Beat n comes after n gaps,
// floor(n / PHI) of them long.
float beatTime(float n, float gap) {
    return gap * (n + (PHI - 1.0) * floor(n / PHI));
}

// One rhythm of bells: the last beats before t ring on. The notes are a
// pentatonic scale on D, five like the grids, and which one a beat plays
// comes from the same count of long gaps, so the tune never repeats.
float rhythm(float t, float gap, float base, float shift) {
    // The mean gap is gap * (2 - 1 / PHI), so the beat at t is near this.
    float near = floor(t / (gap * (2.0 - 1.0 / PHI)));
    float sum = 0.0;
    for (int k = -4; k <= 1; k++) {
        float n = near + float(k);
        if (n < 0.0) { continue; }
        float longs = floor(n / PHI);
        const float scale[5] = {1.0, 1.125, 1.25, 1.5, 1.6666667};
        int note = int(fmod(n + 2.0 * longs + shift, 5.0));
        // A beat before a long gap is struck harder.
        float hard = (floor((n + 1.0) / PHI) - longs) > 0.5 ? 1.0 : 0.6;
        sum += hard * bell(base * scale[note], t - beatTime(n, gap));
    }
    return sum;
}

float2 sound(float t, constant Params& p) {
    float ts = t * max(p.speed, 0.05);
    // Two rhythms, one PHI times slower than the other and an octave
    // below, one to each side.
    float high = rhythm(ts, 0.42, 293.66, 0.0);
    float low = rhythm(ts, 0.42 * PHI, 146.83, 2.0);
    // A hum on D2 and its fifth; the fifth leans out of tune as the grids
    // slide from their symmetric places, and comes back with them.
    float lean = slideAt(t, p);
    float hum = 0.5 * sin(TAU * 73.42 * t);
    hum += 0.3 * sin(TAU * 110.0 * (1.0 + 0.03 * lean) * t);
    // The hum swells as each ring of heat leaves the middle.
    hum *= 0.5 + 0.5 * p.glow * sin(TAU * ts / 6.0);
    float left = 0.35 * high + 0.12 * low + 0.17 * hum;
    float right = 0.12 * high + 0.35 * low + 0.17 * hum;
    // No click at the start or at the loop's end.
    float ends = smoothstep(0.0, 0.05, t) * smoothstep(60.0, 59.9, t);
    return float2(left, right) * p.volume * ends;
}
3 lines from stage 3 that are gone
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * p.tiles;
⋯
    float pixel = p.tiles / u.resolution.y;
⋯
    float low = 0.0;

The sound

Penrose's tiles hide rows of parallel lines, Ammann's bars, and the gaps between them come in two lengths, one φ times the other (φ is the golden ratio), in an order that never repeats: the Fibonacci word. The sound plays that. beatTime(n, gap) is gap × (n + (φ − 1) × floor(n / φ)), so every beat follows the last by a short gap or by one φ times as long, and long and short alternate in the Fibonacci word's order. rhythm finds the beats near t and rings a bell for each (bell, as in Stained Glass: a tone with partials at 2.76 and 5.4 times its pitch, dying away at their own rates), the beat before a long gap struck harder. The notes are a five-note scale on D, one for each grid, and which one a beat plays comes from the same count of long gaps, so the tune never repeats either.

Two of those rhythms play at once: one with a short gap of 0.42 s from D4, mostly in the left ear, and one φ times slower and an octave below, mostly in the right. Under them is a hum on D2 and the fifth above it. The fifth leans out of tune by up to 0.45% as the grids slide from their symmetric places and comes back as they do, and the hum swells with each ring of heat that leaves the middle.

At the defaults the peak is about −4.7 dBFS and the average (RMS) about −20.3 dBFS over the minute. With speed at 3, slide and glow at 1 it is −4.4 and −20.2 dBFS; with speed and glow at 0, −5.9 and −20.2 dBFS. The sound fades in over the first 50 ms and out over the last 100 ms, so the loop joins without a click.

New in Ray Kiln

The symmetry view. The playground has a new menu under the picture, beside color vision: Symmetry. Pick a number from 2 to 12 and the live picture is compared with itself turned by 360/N degrees about its middle; pick a mirror and it is compared with itself with left and right swapped, or top and bottom. The picture is drawn dim and gray where it matches, as heat where it doesn't (dark red for a small difference, through gold to cream for a large one), and dim blue where the turned picture has nothing to offer, since a turn carries the corners of a wide picture outside it. A line under the tools says how much of what can be compared matches.

A turn other than a half or a quarter lands between pixels, so each pixel is compared with the one its place lands on and the eight around it, and it matches when its color lies between the smallest and largest of those nine. That forgives an edge sitting a pixel off after the turn, and nothing wider. It works on any shader, on the frame as it is on screen, and it is free; exports, stills and the other tools still see the shader's own colors.

This episode is made for it. Open Penrose Tiles in the playground, choose 5-fold and let it play. At the start, at 30 s and at 60 s the view is dark: nearly all of the picture, about 99%, matches itself a fifth of a turn round (the rest is specks along the tiles' edges). As the grids slide, heat spreads in from the rim, tile by flipped tile, until at 15 s and 45 s only about a third still matches. Now choose Mirror, left and right: it stays at 100% the whole minute, because the slide moves the grids in mirrored pairs. Try 10-fold at a symmetric moment too: the tiles look as if they might have it, and they don't.

Try this

  • Set slide to 0: the floor keeps its five-fold symmetry the whole time, and nothing flips. At 1 the grids go more than three times as far and the floor is rearranged almost everywhere.
  • Set tiles to 30 and look for the same star in different places. Every patch you can find turns up again, but never in a regular row.
  • Set bevel to 0 and glow to 0: stage 2's flat tiles come back, with stage 3's shading by direction.
  • In gridShift, change 0.2 to 0.4: another floor with five-fold symmetry, with a different middle. Change it to 0.3 and the five shifts no longer add up to a whole number: still rhombs, still no gaps, but no longer Penrose's tiling. Corners turn up that Penrose's rules never allow.
  • In gridShift, change the last share from -0.8090170 to 0.8090170: the shares no longer mirror each other, and the symmetry view's Mirror, left and right lights up as soon as the grids slide.
  • In shade, remove the abs from points: the symmetry view shows where the color broke a symmetry the tiles still have.
  • In tileAt, change for (int b = 0; b < 2; b++) to b < 1: only half the crossings are tried, and some pixels find no tile (they fall back to a smooth color with no grout).
  • In the sound, change PHI to 1.5 in beatTime and rhythm: the gaps now repeat every three beats, and the rhythm turns into a loop you can tap along to.

Parameters

ParameterDefaultRangeWhat it does
speed10 to 3How fast the grids slide and the heat runs outwards
tiles136 to 30How many tile edges fit the picture's height
slide0.30 to 1How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
grout0.050.015 to 0.15How wide the dark lines between the tiles are, in tile edges
rings52 to 12How many tile edges from one ring of heat to the next
glow0.60 to 1How much the rings of heat change a tile
bevel0.50 to 1How far the shade at a tile's edge reaches in
volume0.80 to 1Loudness 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.

The full source

This is the whole episode: the picture, the parameters and the sound.

penrose-tiles.metal
// Ray Kiln · Penrose Tiles
//
// @episode Penrose Tiles
// @length 60
// @variation 30
// @variations 1,2,4,5,6,7,10,11
// @still 30.0
// @teaches An aperiodic tiling from five grids, symmetry that never repeats
// @category Fragment
// @tags penrose tiling, aperiodic tiling, pentagrid, quasicrystal, symmetry, procedural sound
// @final Penrose Tiles | Glaze pools in each tile and thins at its edges, the floor breathes, and bells follow the long and short gaps.
//
// Two tiles, a thick rhomb and a thin one, cover the plane without the
// pattern ever repeating. De Bruijn's way to lay them: five grids of
// parallel lines, a fifth of a turn apart. Every crossing of two lines
// is one tile, thick where the lines cross at 72 degrees, thin at 36,
// and the lines a point lies beyond in the other three grids say where
// the tile goes. With the grids in their symmetric places the whole
// floor has five-fold symmetry about its middle. Slide them and tiles
// flip in threes: the five-fold symmetry goes at once, a mirror stays,
// and at half the loop everything is back.
// 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

// @param speed 1 0 3 How fast the grids slide and the heat runs outwards
// @param tiles 13 6 30 How many tile edges fit the picture's height
// @param slide 0.3 0 1 How far the grids slide from their symmetric places (0 keeps the five-fold symmetry)
// @param grout 0.05 0.015 0.15 How wide the dark lines between the tiles are, in tile edges
// @param rings 5 2 12 How many tile edges from one ring of heat to the next
// @param glow 0.6 0 1 How much the rings of heat change a tile
// @param bevel 0.5 0 1 How far the shade at a tile's edge reaches in
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
constant float PHI = 1.6180340;

// The kiln palette from Flowing Color Field.
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));
}

// Grid j's direction: five, a fifth of a turn apart, the first pointing up.
float2 gridDir(int j) {
    const float2 dirs[5] = {
        float2(0.0, 1.0), float2(-0.9510565, 0.3090170),
        float2(-0.5877853, -0.8090170), float2(0.5877853, -0.8090170),
        float2(0.9510565, 0.3090170)};
    return dirs[j];
}

// How far grid j is slid. At 0.2 each the grids are alike: five-fold
// symmetry. The shares add up to nothing.
float gridShift(int j, float slide) {
    const float share[5] = {
        1.0, -0.8090170, 0.3090170, 0.3090170, -0.8090170};
    return 0.2 + slide * share[j];
}

// The slide over the loop: out, back, out the other way and back.
float slideAt(float t, constant Params& p) {
    return 0.5 * p.slide * sin(TAU * t * p.speed / 60.0);
}

struct Tile {
    float2 at;      // where the point is in its tile, 0...1 along each edge
    float thin;     // 0 for a thick rhomb, 1 for a thin one
    float sine;     // the sine of the tile's corner angle
    float2 middle;  // the tile's middle, in tile edges from the floor's
    float2 along;   // its long diagonal, a unit vector
};

// The tile that holds y. Every crossing of two grids' lines is one tile;
// the four crossings nearest y are tried for each of the ten pairs of
// grids, and one of those tiles holds y.
Tile tileAt(float2 y, float slide) {
    Tile t;
    t.at = float2(0.5);
    t.thin = 0.0;
    t.sine = 1.0;
    t.middle = y;
    t.along = float2(0.0, 1.0);
    // The tiles are the grids' plane stretched by 5/2, so y lies near
    // 2/5 of it.
    float2 moved = float2(0.0);
    for (int m = 0; m < 5; m++) {
        moved += gridShift(m, slide) * gridDir(m);
    }
    float2 x = 0.4 * (y - moved);
    for (int i = 0; i < 4; i++) {
        float2 ei = gridDir(i);
        float gi = gridShift(i, slide);
        float ni = floor(dot(x, ei) + gi);
        for (int j = i + 1; j < 5; j++) {
            float2 ej = gridDir(j);
            float gj = gridShift(j, slide);
            float nj = floor(dot(x, ej) + gj);
            float cross = ei.x * ej.y - ei.y * ej.x;
            for (int a = 0; a < 2; a++) {
                for (int b = 0; b < 2; b++) {
                    // Lines ki and kj cross at q.
                    float ki = ni + float(a);
                    float kj = nj + float(b);
                    float si = ki - gi;
                    float sj = kj - gj;
                    float2 q = float2(si * ej.y - sj * ei.y,
                                      sj * ei.x - si * ej.x) / cross;
                    // The tile's corner: a step along a grid's direction
                    // for each line passed, the next line beyond q in the
                    // other three grids.
                    float2 corner = ki * ei + kj * ej;
                    for (int m = 0; m < 5; m++) {
                        if (m == i || m == j) { continue; }
                        float2 em = gridDir(m);
                        float km = ceil(dot(q, em) + gridShift(m, slide));
                        corner += km * em;
                    }
                    // y from that corner, along the tile's two edges.
                    float2 d = y - corner;
                    float2 at = float2(d.x * ej.y - d.y * ej.x,
                                       ei.x * d.y - ei.y * d.x) / cross;
                    bool inside = at.x >= 0.0 && at.x < 1.0
                               && at.y >= 0.0 && at.y < 1.0;
                    if (inside) {
                        // Neighboring grids cross at 72 degrees: thick.
                        // Grids two apart, at 36: thin.
                        bool thick = (j - i == 1) || (j - i == 4);
                        t.at = at;
                        t.thin = thick ? 0.0 : 1.0;
                        t.sine = abs(cross);
                        t.middle = corner + 0.5 * (ei + ej);
                        t.along = normalize(thick ? ei + ej : ei - ej);
                    }
                }
            }
        }
    }
    return t;
}

// How far the point is from its tile's nearest edge, in tile edges.
float edgeDistance(Tile t) {
    float2 d = min(t.at, 1.0 - t.at);
    return min(d.x, d.y) * t.sine;
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float t = u.time * p.speed;
    // The floor breathes in and out a little, twice a loop.
    float across = p.tiles * (1.0 + 0.06 * sin(TAU * t / 30.0));
    float2 y = (uv - 0.5) * float2(aspect, 1.0) * across;
    float pixel = across / u.resolution.y;
    Tile tile = tileAt(y, slideAt(u.time, p));
    // Heat runs outwards in rings, and each tile takes the heat at its
    // own middle, so it is one flat color.
    float r = length(tile.middle);
    float ring = 0.5 + 0.5 * sin(TAU * (r / p.rings - t / 6.0));
    // A tile whose long diagonal points at the floor's middle burns
    // brighter than one that lies across. Both are the same after a
    // fifth of a turn, so the color keeps the symmetry the tiles have.
    float2 out = tile.middle / max(r, 0.001);
    float points = abs(dot(tile.along, out));
    float fire = mix(0.42, 0.66, tile.thin) + 0.22 * (points - 0.5);
    fire += p.glow * 0.36 * (ring - 0.5);
    // The floor is cooler towards its rim.
    fire -= 0.10 * smoothstep(0.0, 1.2, r / across);
    float3 color = kiln(fire);
    float d = edgeDistance(tile);
    // Glaze pools in the middle of a tile and thins towards its edges,
    // and the pool catches the heat as a ring passes.
    float pool = smoothstep(0.0, 0.02 + 0.3 * p.bevel, d);
    color *= mix(1.0, 0.55 + 0.45 * pool, step(0.001, p.bevel));
    color += 0.10 * kiln(0.9) * pool * pool * ring * p.glow;
    // Dark grout where two tiles meet, softened over a pixel.
    float line = smoothstep(0.5 * p.grout - pixel,
                            0.5 * p.grout + pixel, d);
    color = mix(kiln(0.06), color, line);
    return float4(color, 1.0);
}

// ---- The sound ---------------------------------------------------------------

// A bell: a tone and two partials that are not whole multiples of it, each
// dying away at its own rate. since is the time since it was struck.
float bell(float f, float since) {
    if (since < 0.0) { return 0.0; }
    float attack = smoothstep(0.0, 0.004, since);
    float tone = sin(TAU * f * since) * exp(-since * 3.0);
    tone += 0.45 * sin(TAU * f * 2.76 * since) * exp(-since * 6.0);
    tone += 0.25 * sin(TAU * f * 5.40 * since) * exp(-since * 11.0);
    return tone * attack;
}

// When beat n falls. Penrose's tiles hide rows of parallel lines (Ammann's
// bars) whose gaps come in two lengths, PHI to 1, in the order of the
// Fibonacci word, which never repeats. Beat n comes after n gaps,
// floor(n / PHI) of them long.
float beatTime(float n, float gap) {
    return gap * (n + (PHI - 1.0) * floor(n / PHI));
}

// One rhythm of bells: the last beats before t ring on. The notes are a
// pentatonic scale on D, five like the grids, and which one a beat plays
// comes from the same count of long gaps, so the tune never repeats.
float rhythm(float t, float gap, float base, float shift) {
    // The mean gap is gap * (2 - 1 / PHI), so the beat at t is near this.
    float near = floor(t / (gap * (2.0 - 1.0 / PHI)));
    float sum = 0.0;
    for (int k = -4; k <= 1; k++) {
        float n = near + float(k);
        if (n < 0.0) { continue; }
        float longs = floor(n / PHI);
        const float scale[5] = {1.0, 1.125, 1.25, 1.5, 1.6666667};
        int note = int(fmod(n + 2.0 * longs + shift, 5.0));
        // A beat before a long gap is struck harder.
        float hard = (floor((n + 1.0) / PHI) - longs) > 0.5 ? 1.0 : 0.6;
        sum += hard * bell(base * scale[note], t - beatTime(n, gap));
    }
    return sum;
}

float2 sound(float t, constant Params& p) {
    float ts = t * max(p.speed, 0.05);
    // Two rhythms, one PHI times slower than the other and an octave
    // below, one to each side.
    float high = rhythm(ts, 0.42, 293.66, 0.0);
    float low = rhythm(ts, 0.42 * PHI, 146.83, 2.0);
    // A hum on D2 and its fifth; the fifth leans out of tune as the grids
    // slide from their symmetric places, and comes back with them.
    float lean = slideAt(t, p);
    float hum = 0.5 * sin(TAU * 73.42 * t);
    hum += 0.3 * sin(TAU * 110.0 * (1.0 + 0.03 * lean) * t);
    // The hum swells as each ring of heat leaves the middle.
    hum *= 0.5 + 0.5 * p.glow * sin(TAU * ts / 6.0);
    float left = 0.35 * high + 0.12 * low + 0.17 * hum;
    float right = 0.12 * high + 0.35 * low + 0.17 * hum;
    // No click at the start or at the loop's end.
    float ends = smoothstep(0.0, 0.05, t) * smoothstep(60.0, 59.9, t);
    return float2(left, right) * p.volume * ends;
}

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] = [1, 13, 0.3, 0.05, 5, 0.6, 0.5, 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(&params, 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 Shapes & Symmetry