Shader

Crawling Glaze

A glaze that crawls over dark clay in worms, spots and holes, grown by two chemicals on a grid of cells. One is fed in everywhere, the other eats it to make more of itself and dies away, and both spread to their neighbors at different speeds. From thirty drops, the Gray-Scott model grows a pattern nobody drew, and the feed and kill change a little across the plate and over the minute, so it keeps moving.

· Particles & Simulation · Compute shader

Teaches Reaction-diffusion, two chemicals that feed, spread and die into a growing pattern

Builds on Flowing Color Field

Watch

The idea

Some patterns aren't drawn: they grow. Put two chemicals on a surface. One, A, is fed in everywhere, slowly. The other, B, eats A where they meet and makes more of itself, and dies away at its own rate. Both spread out to their neighbors, A twice as fast as B. That is all, and from a few drops of B it grows spots that split in two, worms that lengthen and curl, mazes and holes. Alan Turing proposed something like it in 1952 for how a leopard gets its spots. This is reaction-diffusion, and the version here is the Gray-Scott model.

A fragment shader can't do this on its own: each pixel is worked out from scratch every frame, and a pattern that grows needs to remember what it was a moment ago. So this is a cells shader, // @cells 384 216: a grid of 384 by 216 cells kept on the GPU, each holding the two chemicals, struct Cell { float a; float b; }. Every step, each cell is worked out again from the grid as it was after the last step. // @steps 8 makes that 8 steps each 60th of a second, 480 a second, 28,800 in the minute. The picture then reads the grid. In a pottery kiln, a glaze that pulls back from bare clay into beads and islands is said to crawl, so here B is the glaze. It is built in four steps, and each step is a shader you can run on its own.

Stage 1 · Drops that spread

start sets every cell before the first step: all A (a = 1), apart from the drops. glaze_drop puts down 30 of them (seeds), each round, with a radius of 2 to 5 cells, a B of 0.25 to 0.75 and half its A, at places and sizes from a hash of its number (glaze_hash). The grid wraps round at its edges, so wrapped_distance measures across an edge the short way, and a drop near the right edge comes back on the left.

next is one step for one cell. It works out the Laplacian, how much the cell differs from those around it: the eight neighbors weighted 0.2 for each of the four beside it and 0.05 for each corner (the weights add up to 1), less the cell itself. cells.at(int2(at) + int2(dx, dy)) reads a neighbor as it was after the last step; the host reads one buffer and writes another, so it doesn't matter in which order the GPU works through the cells. Adding the Laplacian moves a cell toward the average of its neighbors, which is diffusion: A at rate 1, B at half that. In this stage a drop stays wet, keeping its B (the max with wet, its drop's B), so B spreads out from each one in a glow that widens over the minute. shade reads the cell under each pixel, cells.at(uv), and draws its B in the kiln's colors (kiln, from Flowing Color Field).

stage-1-drops-that-spread.metal
// Stage 1 · Drops that spread
// A grid of cells lives on the GPU, 384 by 216. Each holds two
// chemicals, A and B. Every cell starts as all A, with a few drops of B.
// Each step, every cell moves toward the average of its eight neighbors:
// that is diffusion. The drops stay wet, so B spreads out from each.
//
// @cells 384 216
// @steps 8

// @param seeds 30 3 60 How many drops of glaze start the pattern
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;

struct Cell {
    float a;   // A, fed in everywhere: 0 to 1
    float b;   // B, the glaze, which A turns into: 0 to 1
};

float glaze_hash(uint2 at) {
    uint n = at.x * 374761393u + at.y * 668265263u;
    n = (n ^ (n >> 13)) * 1274126177u;
    return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}

// The distance between two cells on a grid that wraps round.
float wrapped_distance(float2 a, float2 b, float2 size) {
    float2 d = abs(a - b);
    d = min(d, size - d);
    return length(d);
}

// The B of the drop a cell is in, 0 outside them all. The drops are
// round, as many as seeds says, each at its own place, size and strength.
float glaze_drop(uint2 at, uint2 size, constant Params& p) {
    float b = 0.0;
    int drops = int(p.seeds + 0.5);
    for (int i = 0; i < drops; i++) {
        float cx = glaze_hash(uint2(i, 7)), cy = glaze_hash(uint2(i, 19));
        float2 center = float2(cx, cy) * float2(size);
        float radius = 2.0 + 3.0 * glaze_hash(uint2(i, 31));
        if (wrapped_distance(float2(at), center, float2(size)) < radius) {
            b = 0.25 + 0.5 * glaze_hash(uint2(i, 43));
        }
    }
    return b;
}

// Every cell starts as all A, apart from the drops of B.
Cell start(uint2 at, uint2 size, constant Params& p) {
    Cell c;
    c.b = glaze_drop(at, size, p);
    c.a = c.b > 0.0 ? 0.5 : 1.0;
    return c;
}

// One step of diffusion. The Laplacian, how much a cell differs from
// those around it, is a weighted sum of the eight neighbors (0.2 for
// each beside it, 0.05 for each corner) less the cell itself.
// A spreads at rate 1, B at half that. A drop stays wet: it keeps its B.
Cell next(Cell me, uint2 at, Cells cells,
          constant Uniforms& u, constant Params& p) {
    float2 lap = -float2(me.a, me.b);
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            Cell n = cells.at(int2(at) + int2(dx, dy));
            float w = (dx == 0 || dy == 0) ? 0.2 : 0.05;
            lap += w * float2(n.a, n.b);
        }
    }
    Cell c;
    c.a = clamp(me.a + 1.0 * lap.x, 0.0, 1.0);
    float wet = glaze_drop(at, cells.size, p);
    c.b = max(clamp(me.b + 0.5 * lap.y, 0.0, 1.0), wet);
    return c;
}

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));
}

// B in the kiln's colors, the cell under each pixel.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
             Cells cells) {
    Cell c = cells.at(uv);
    return float4(kiln(c.b * 1.5), 1.0);
}

// A low hum: two tones a fifth apart.
float2 sound(float t, constant Params& p) {
    float fade = smoothstep(0.0, 1.5, t);
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * 0.5 * t))
              + 0.5 * sin(TAU * 110.0 * t + 0.5 * sin(TAU * 0.07 * t));
    return float2(hum * 0.19 * fade * p.volume);
}

Stage 2 · Feed and kill

Now the chemicals react, and the drops don't stay wet. Each step, B eats A where both are: react = a × b × b, taken from A and added to B. A is fed in toward 1 at the feed rate (feed × (1 − a)), and B dies away at the kill rate plus the feed rate, times what there is of it. The two sliders are in thousandths, because the rates are small and the pattern is sensitive to them: feed at 55 is a feed rate of 0.055. With feed 0.055 and kill 0.062, each drop grows into a ring, the ring breaks into worms, and the worms grow and fold until they fill the grid. A spot of B can't grow on its own without running out of A at its middle, so it splits; where two worms come close, the A between them runs out and they keep apart. That is why the pattern keeps an even spacing.

stage-2-feed-and-kill.metal35 new lines since stage 1
// Stage 2 · Feed and kill
// Now the two react. A is fed in everywhere, toward 1. Where a cell has
// both, B eats A to make more of itself: A times B times B. B dies away
// at its own rate. Spots grow, split and turn to worms. This is the
// Gray-Scott model. Its sliders are in thousandths: feed 55 is 0.055.
//
// @cells 384 216
// @steps 8

// @param feed 55 35 70 How fast A is fed in, in thousandths: low gives spots, high gives worms and holes
// @param kill 62 61 66 How fast B dies away, in thousandths: low floods the plate, high leaves a few slow worms
// @param seeds 30 3 60 How many drops of glaze start the pattern
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;

struct Cell {
    float a;   // A, fed in everywhere: 0 to 1
    float b;   // B, the glaze, which A turns into: 0 to 1
};

float glaze_hash(uint2 at) {
    uint n = at.x * 374761393u + at.y * 668265263u;
    n = (n ^ (n >> 13)) * 1274126177u;
    return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}

// The distance between two cells on a grid that wraps round.
float wrapped_distance(float2 a, float2 b, float2 size) {
    float2 d = abs(a - b);
    d = min(d, size - d);
    return length(d);
}

// The B of the drop a cell is in, 0 outside them all. The drops are
// round, as many as seeds says, each at its own place, size and strength.
float glaze_drop(uint2 at, uint2 size, constant Params& p) {
    float b = 0.0;
    int drops = int(p.seeds + 0.5);
    for (int i = 0; i < drops; i++) {
        float cx = glaze_hash(uint2(i, 7)), cy = glaze_hash(uint2(i, 19));
        float2 center = float2(cx, cy) * float2(size);
        float radius = 2.0 + 3.0 * glaze_hash(uint2(i, 31));
        if (wrapped_distance(float2(at), center, float2(size)) < radius) {
            b = 0.25 + 0.5 * glaze_hash(uint2(i, 43));
        }
    }
    return b;
}

// Every cell starts as all A, apart from the drops of B.
Cell start(uint2 at, uint2 size, constant Params& p) {
    Cell c;
    c.b = glaze_drop(at, size, p);
    c.a = c.b > 0.0 ? 0.5 : 1.0;
    return c;
}

// One step of Gray-Scott: spread, react, feed A, kill B.
Cell next(Cell me, uint2 at, Cells cells,
          constant Uniforms& u, constant Params& p) {
    float feed = 0.001 * p.feed, kill = 0.001 * p.kill;
    float2 lap = -float2(me.a, me.b);
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            Cell n = cells.at(int2(at) + int2(dx, dy));
            float w = (dx == 0 || dy == 0) ? 0.2 : 0.05;
            lap += w * float2(n.a, n.b);
        }
    }
    float react = me.a * me.b * me.b;
    Cell c;
    c.a = clamp(me.a + 1.0 * lap.x - react + feed * (1.0 - me.a), 0.0, 1.0);
    float dies = (kill + feed) * me.b;
    c.b = clamp(me.b + 0.5 * lap.y + react - dies, 0.0, 1.0);
    return c;
}

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) {
    Cell c = cells.at(uv);
    return float4(kiln(c.b * 2.5), 1.0);
}

float sound_hash(float n) {
    return fract(sin(n * 12.9898) * 43758.5453);
}

// The sound follows the picture's ideas. A low hum is the feed: two
// tones a fifth apart whose beating speeds up with feed. The glaze as it
// spreads is a ticking, like glaze crazing as it cools: short pings at
// random moments, more of them as the pattern grows over the first 20
// seconds, higher when the kill is high and lower when it is low.
float2 sound(float t, constant Params& p) {
    float fade = smoothstep(0.0, 1.5, t);

    float beat = 0.3 + 0.03 * p.feed;
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * beat * 0.25 * t))
              + 0.5 * sin(TAU * 110.0 * t + 0.5 * sin(TAU * 0.07 * t));
    hum *= 0.19;

    // 17 chances a second for a ping; at first 3 of them ring, all 17
    // once the pattern has grown.
    float grown = smoothstep(0.0, 20.0, t);
    float rate = 17.0;
    float2 pings = float2(0.0);
    for (int k = 0; k < 5; k++) {
        float slot = floor(t * rate) - float(k);
        if (sound_hash(slot + 0.9) > 0.18 + 0.82 * grown) continue;
        float when = (slot + sound_hash(slot)) / rate;
        float age = t - when;
        if (age < 0.0) continue;
        float step5 = floor(sound_hash(slot + 0.5) * 5.0);
        // A pentatonic scale: 0, 2, 4, 7 or 9 semitones up.
        float semis = step5 < 1.0 ? 0.0 : step5 < 2.0 ? 2.0 : step5 < 3.0 ? 4.0
                    : step5 < 4.0 ? 7.0 : 9.0;
        float shift = (p.kill - 62.0) * 0.4;
        float pitch = 880.0 * pow(2.0, (semis + shift) / 12.0);
        float env = exp(-age * 28.0) * smoothstep(0.0, 0.002, age);
        float loud = 0.5 + 0.5 * sound_hash(slot + 0.25);
        float v = sin(TAU * pitch * age) * env * loud;
        float pan = sound_hash(slot + 0.75);
        pings += v * float2(1.0 - pan, pan);
    }
    pings *= 0.33;

    float2 s = (hum + pings) * fade * p.volume;
    return s;
}
6 lines from stage 1 that are gone
    c.a = clamp(me.a + 1.0 * lap.x, 0.0, 1.0);
    float wet = glaze_drop(at, cells.size, p);
    c.b = max(clamp(me.b + 0.5 * lap.y, 0.0, 1.0), wet);
⋯
    return float4(kiln(c.b * 1.5), 1.0);
⋯
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * 0.5 * t))
⋯
    return float2(hum * 0.19 * fade * p.volume);

Stage 3 · Glaze on clay

rates gives each cell its own feed and kill. The feed rate is feed plus up to 4 (times drift), in thousandths, from two sine waves multiplied together: one running across the plate at a slant and moving once a minute, the other running up it and moving the other way once every 90 seconds. The kill rate is kill plus up to 1.5 (times drift), changing once a minute and a little across the plate. They are small changes, but the Gray-Scott model is sensitive to them, so worms, spots and floods of glaze with holes in them meet, and the borders between them move as the minute goes on.

The picture is now glaze on clay. grid_point finds the point of the grid under a pixel, keeping the cells square whatever shape the picture is: the grid covers the picture and is cropped at the sides or at the top and bottom, which works because it wraps. glaze_at reads B there smoothly between the four nearest cells, so the squares of the grid don't show. The glaze begins where B passes 0.12 and is whole by 0.35 (thick). The clay under it is a dark brown with a little grain, and the glaze runs through the kiln's colors from ember at its thin edge to gold where B is highest.

stage-3-glaze-on-clay.metal33 new lines since stage 2
// Stage 3 · Glaze on clay
// The feed and kill change a little across the plate and slowly over
// the minute, so spots, worms and holes meet. B is drawn as glaze on
// dark clay, read smoothly between the four nearest cells, so the grid's
// squares don't show.
//
// @cells 384 216
// @steps 8

// @param feed 55 35 70 How fast A is fed in, in thousandths: low gives spots, high gives worms and holes
// @param kill 62 61 66 How fast B dies away, in thousandths: low floods the plate, high leaves a few slow worms
// @param drift 1 0 1.2 How much the feed and kill change across the plate and over time
// @param seeds 30 3 60 How many drops of glaze start the pattern
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;

struct Cell {
    float a;   // A, fed in everywhere: 0 to 1
    float b;   // B, the glaze, which A turns into: 0 to 1
};

float glaze_hash(uint2 at) {
    uint n = at.x * 374761393u + at.y * 668265263u;
    n = (n ^ (n >> 13)) * 1274126177u;
    return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}

// The distance between two cells on a grid that wraps round.
float wrapped_distance(float2 a, float2 b, float2 size) {
    float2 d = abs(a - b);
    d = min(d, size - d);
    return length(d);
}

// The B of the drop a cell is in, 0 outside them all. The drops are
// round, as many as seeds says, each at its own place, size and strength.
float glaze_drop(uint2 at, uint2 size, constant Params& p) {
    float b = 0.0;
    int drops = int(p.seeds + 0.5);
    for (int i = 0; i < drops; i++) {
        float cx = glaze_hash(uint2(i, 7)), cy = glaze_hash(uint2(i, 19));
        float2 center = float2(cx, cy) * float2(size);
        float radius = 2.0 + 3.0 * glaze_hash(uint2(i, 31));
        if (wrapped_distance(float2(at), center, float2(size)) < radius) {
            b = 0.25 + 0.5 * glaze_hash(uint2(i, 43));
        }
    }
    return b;
}

// Every cell starts as all A, apart from the drops of B.
Cell start(uint2 at, uint2 size, constant Params& p) {
    Cell c;
    c.b = glaze_drop(at, size, p);
    c.a = c.b > 0.0 ? 0.5 : 1.0;
    return c;
}

// The feed and kill here and now: a slow wave across the plate and
// over the minute.
float2 rates(float2 at, float2 size, float time, constant Params& p) {
    float2 q = at / size.y;
    float wave = sin(TAU * (q.x * 0.6 + q.y * 0.3 + time / 60.0))
               * sin(TAU * (q.y * 0.45 - time / 90.0) + 1.3);
    float swing = sin(TAU * time / 60.0 + q.x * 2.0);
    // The sliders are in thousandths: feed 55 is 0.055.
    float feed = 0.001 * (p.feed + 4.0 * p.drift * wave);
    float kill = 0.001 * (p.kill + 1.5 * p.drift * swing);
    return float2(feed, kill);
}

// One step of Gray-Scott, with the feed and kill of this cell.
Cell next(Cell me, uint2 at, Cells cells,
          constant Uniforms& u, constant Params& p) {
    float2 lap = -float2(me.a, me.b);
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            Cell n = cells.at(int2(at) + int2(dx, dy));
            float w = (dx == 0 || dy == 0) ? 0.2 : 0.05;
            lap += w * float2(n.a, n.b);
        }
    }
    float2 fk = rates(float2(at), float2(cells.size), u.time, p);
    float react = me.a * me.b * me.b;
    Cell c;
    c.a = clamp(me.a + 1.0 * lap.x - react + fk.x * (1.0 - me.a), 0.0, 1.0);
    c.b = clamp(me.b + 0.5 * lap.y + react - (fk.y + fk.x) * me.b, 0.0, 1.0);
    return c;
}

// How much glaze lies at a point in cell units, read smoothly between
// the four nearest cells.
float glaze_at(Cells cells, float2 g) {
    float2 f = g - 0.5;
    int2 i = int2(floor(f));
    float2 w = f - floor(f);
    float b00 = cells.at(i).b;
    float b10 = cells.at(i + int2(1, 0)).b;
    float b01 = cells.at(i + int2(0, 1)).b;
    float b11 = cells.at(i + int2(1, 1)).b;
    return mix(mix(b00, b10, w.x), mix(b01, b11, w.x), w.y);
}

// 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 grid_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));
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
             Cells cells) {
    float2 g = grid_point(uv, u.resolution, float2(cells.size));
    float b = glaze_at(cells, g);
    // The glaze starts where b passes 0.12 and is whole by 0.35.
    float thick = smoothstep(0.12, 0.35, b);
    float grain = glaze_hash(uint2(g * 3.0));
    float3 clay = float3(0.075, 0.050, 0.045) * (0.85 + 0.3 * grain);
    float3 glaze = kiln(0.32 + 0.52 * smoothstep(0.12, 0.4, b));
    return float4(mix(clay, glaze, thick), 1.0);
}

float sound_hash(float n) {
    return fract(sin(n * 12.9898) * 43758.5453);
}

// The sound follows the picture's ideas. A low hum is the feed: two
// tones a fifth apart whose beating speeds up with feed. The glaze as it
// spreads is a ticking, like glaze crazing as it cools: short pings at
// random moments, more of them as the pattern grows over the first 20
// seconds, higher when the kill is high and lower when it is low.
float2 sound(float t, constant Params& p) {
    float fade = smoothstep(0.0, 1.5, t);

    float beat = 0.3 + 0.03 * p.feed;
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * beat * 0.25 * t))
              + 0.5 * sin(TAU * 110.0 * t + 0.5 * sin(TAU * 0.07 * t));
    hum *= 0.19;

    // 17 chances a second for a ping; at first 3 of them ring, all 17
    // once the pattern has grown.
    float grown = smoothstep(0.0, 20.0, t);
    float rate = 17.0;
    float2 pings = float2(0.0);
    for (int k = 0; k < 5; k++) {
        float slot = floor(t * rate) - float(k);
        if (sound_hash(slot + 0.9) > 0.18 + 0.82 * grown) continue;
        float when = (slot + sound_hash(slot)) / rate;
        float age = t - when;
        if (age < 0.0) continue;
        float step5 = floor(sound_hash(slot + 0.5) * 5.0);
        // A pentatonic scale: 0, 2, 4, 7 or 9 semitones up.
        float semis = step5 < 1.0 ? 0.0 : step5 < 2.0 ? 2.0 : step5 < 3.0 ? 4.0
                    : step5 < 4.0 ? 7.0 : 9.0;
        float shift = (p.kill - 62.0) * 0.4;
        float pitch = 880.0 * pow(2.0, (semis + shift) / 12.0);
        float env = exp(-age * 28.0) * smoothstep(0.0, 0.002, age);
        float loud = 0.5 + 0.5 * sound_hash(slot + 0.25);
        float v = sin(TAU * pitch * age) * env * loud;
        float pan = sound_hash(slot + 0.75);
        pings += v * float2(1.0 - pan, pan);
    }
    pings *= 0.33;

    float2 s = (hum + pings) * fade * p.volume;
    return s;
}
6 lines from stage 2 that are gone
    float feed = 0.001 * p.feed, kill = 0.001 * p.kill;
⋯
    c.a = clamp(me.a + 1.0 * lap.x - react + feed * (1.0 - me.a), 0.0, 1.0);
    float dies = (kill + feed) * me.b;
    c.b = clamp(me.b + 0.5 * lap.y + react - dies, 0.0, 1.0);
⋯
    Cell c = cells.at(uv);
    return float4(kiln(c.b * 2.5), 1.0);

Stage 4 · Crawling Glaze

The finished shader lights the glaze. Its slope comes from the glaze one cell to each side, left and right, below and above, and that gives a normal n. A light up and to the left shades it (between 55% and 100%), and a highlight, Blinn-Phong with a power of 60, gives the glaze its shine where it faces halfway between the light and the eye, only on the glaze (times thick) and as strong as gloss. The picture is a little darker toward its edges.

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 g = grid_point(uv, u.resolution, float2(cells.size));
    float b = glaze_at(cells, g);
    // The glaze starts where b passes 0.12 and is whole by 0.35.
    float thick = smoothstep(0.12, 0.35, b);

    // Its slope, from the glaze a cell to each side, gives a normal.
    float bx = glaze_at(cells, g + float2(1.0, 0.0))
             - glaze_at(cells, g - float2(1.0, 0.0));
    float by = glaze_at(cells, g + float2(0.0, 1.0))
             - glaze_at(cells, g - float2(0.0, 1.0));
    float3 n = normalize(float3(-bx * 4.0, -by * 4.0, 1.0));

    // The clay: dark and dry, with a little grain.
    float grain = glaze_hash(uint2(g * 3.0));
    float3 clay = float3(0.075, 0.050, 0.045) * (0.85 + 0.3 * grain);

    // The glaze: thin at its edge, deep where it pools, in the kiln's colors.
    float3 glaze = kiln(0.32 + 0.52 * smoothstep(0.12, 0.4, b));

    // A light up and to the left, and a highlight where the glaze faces
    // halfway between it and the eye, as shiny as gloss.
    float3 light = normalize(float3(-0.5, 0.6, 0.65));
    float diffuse = 0.55 + 0.45 * max(dot(n, light), 0.0);
    float3 h = normalize(light + float3(0.0, 0.0, 1.0));
    float spec = pow(max(dot(n, h), 0.0), 60.0) * 1.5 * p.gloss * thick;

    float3 color = mix(clay, glaze * diffuse, thick)
                 + spec * float3(1.0, 0.9, 0.75);
    // A little darker toward the picture's edge.
    float2 c = uv - 0.5;
    color *= 1.0 - 0.6 * dot(c, c);
    return float4(color, 1.0);
}
crawling-glaze.metal, the whole file14 new lines since stage 3
// Ray Kiln · Crawling Glaze
//
// @episode Crawling Glaze
// @length 60
// @variation 30
// @variations 1,2,3,5,7,8,9,11
// @sweeps feed, kill, drift
// @still 40
// @teaches Reaction-diffusion: two chemicals that feed, spread and die, a pattern that grows itself
// @category Compute
// @tags reaction-diffusion, gray-scott, laplacian, diffusion, compute shaders, procedural sound
// @final Crawling Glaze | The glaze is lit: its slope from the cells around it, a gloss where it faces the light.
//
// Two chemicals share a grid of cells. A is fed in everywhere at a
// steady rate. B eats A to make more of itself, and B dies away at its
// own rate. Both spread to their neighbors, A twice as fast as B. From a
// few drops of B, that is all it takes: spots split, worms grow and
// curl, and a pattern spreads across the grid that nobody drew. This is
// the Gray-Scott model. Here B is a glaze crawling over fired clay: the
// feed and the kill change a little across the plate and slowly over
// the minute, so spots, worms and holes meet and keep moving.
// 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 384 216
// @steps 8

// @param feed 55 35 70 How fast A is fed in, in thousandths: low gives spots, high gives worms and holes
// @param kill 62 61 66 How fast B dies away, in thousandths: low floods the plate, high leaves a few slow worms
// @param drift 1 0 1.2 How much the feed and kill change across the plate and over time
// @param seeds 30 3 60 How many drops of glaze start the pattern
// @param gloss 1 0 2 How shiny the glaze is
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;

struct Cell {
    float a;   // A, fed in everywhere: 0 to 1
    float b;   // B, the glaze, which A turns into: 0 to 1
};

float glaze_hash(uint2 at) {
    uint n = at.x * 374761393u + at.y * 668265263u;
    n = (n ^ (n >> 13)) * 1274126177u;
    return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}

// The distance between two cells on a grid that wraps round.
float wrapped_distance(float2 a, float2 b, float2 size) {
    float2 d = abs(a - b);
    d = min(d, size - d);
    return length(d);
}

// The B of the drop a cell is in, 0 outside them all. The drops are
// round, as many as seeds says, each at its own place, size and strength.
float glaze_drop(uint2 at, uint2 size, constant Params& p) {
    float b = 0.0;
    int drops = int(p.seeds + 0.5);
    for (int i = 0; i < drops; i++) {
        float cx = glaze_hash(uint2(i, 7)), cy = glaze_hash(uint2(i, 19));
        float2 center = float2(cx, cy) * float2(size);
        float radius = 2.0 + 3.0 * glaze_hash(uint2(i, 31));
        if (wrapped_distance(float2(at), center, float2(size)) < radius) {
            b = 0.25 + 0.5 * glaze_hash(uint2(i, 43));
        }
    }
    return b;
}

// Every cell starts as all A, apart from the drops of B.
Cell start(uint2 at, uint2 size, constant Params& p) {
    Cell c;
    c.b = glaze_drop(at, size, p);
    c.a = c.b > 0.0 ? 0.5 : 1.0;
    return c;
}

// The feed and kill here and now: a slow wave across the plate and
// over the minute.
float2 rates(float2 at, float2 size, float time, constant Params& p) {
    float2 q = at / size.y;
    float wave = sin(TAU * (q.x * 0.6 + q.y * 0.3 + time / 60.0))
               * sin(TAU * (q.y * 0.45 - time / 90.0) + 1.3);
    float swing = sin(TAU * time / 60.0 + q.x * 2.0);
    // The sliders are in thousandths: feed 55 is 0.055.
    float feed = 0.001 * (p.feed + 4.0 * p.drift * wave);
    float kill = 0.001 * (p.kill + 1.5 * p.drift * swing);
    return float2(feed, kill);
}

// One step of Gray-Scott. The Laplacian, how much a cell differs from
// those around it, is a weighted sum of the eight neighbors (0.2 for
// each beside it, 0.05 for each corner) less the cell itself.
Cell next(Cell me, uint2 at, Cells cells,
          constant Uniforms& u, constant Params& p) {
    float2 lap = -float2(me.a, me.b);
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            Cell n = cells.at(int2(at) + int2(dx, dy));
            float w = (dx == 0 || dy == 0) ? 0.2 : 0.05;
            lap += w * float2(n.a, n.b);
        }
    }
    float2 fk = rates(float2(at), float2(cells.size), u.time, p);
    float react = me.a * me.b * me.b;
    Cell c;
    c.a = clamp(me.a + 1.0 * lap.x - react + fk.x * (1.0 - me.a), 0.0, 1.0);
    c.b = clamp(me.b + 0.5 * lap.y + react - (fk.y + fk.x) * me.b, 0.0, 1.0);
    return c;
}

// How much glaze lies at a point in cell units, read smoothly between
// the four nearest cells.
float glaze_at(Cells cells, float2 g) {
    float2 f = g - 0.5;
    int2 i = int2(floor(f));
    float2 w = f - floor(f);
    float b00 = cells.at(i).b;
    float b10 = cells.at(i + int2(1, 0)).b;
    float b01 = cells.at(i + int2(0, 1)).b;
    float b11 = cells.at(i + int2(1, 1)).b;
    return mix(mix(b00, b10, w.x), mix(b01, b11, w.x), w.y);
}

// 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 grid_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));
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
             Cells cells) {
    float2 g = grid_point(uv, u.resolution, float2(cells.size));
    float b = glaze_at(cells, g);
    // The glaze starts where b passes 0.12 and is whole by 0.35.
    float thick = smoothstep(0.12, 0.35, b);

    // Its slope, from the glaze a cell to each side, gives a normal.
    float bx = glaze_at(cells, g + float2(1.0, 0.0))
             - glaze_at(cells, g - float2(1.0, 0.0));
    float by = glaze_at(cells, g + float2(0.0, 1.0))
             - glaze_at(cells, g - float2(0.0, 1.0));
    float3 n = normalize(float3(-bx * 4.0, -by * 4.0, 1.0));

    // The clay: dark and dry, with a little grain.
    float grain = glaze_hash(uint2(g * 3.0));
    float3 clay = float3(0.075, 0.050, 0.045) * (0.85 + 0.3 * grain);

    // The glaze: thin at its edge, deep where it pools, in the kiln's colors.
    float3 glaze = kiln(0.32 + 0.52 * smoothstep(0.12, 0.4, b));

    // A light up and to the left, and a highlight where the glaze faces
    // halfway between it and the eye, as shiny as gloss.
    float3 light = normalize(float3(-0.5, 0.6, 0.65));
    float diffuse = 0.55 + 0.45 * max(dot(n, light), 0.0);
    float3 h = normalize(light + float3(0.0, 0.0, 1.0));
    float spec = pow(max(dot(n, h), 0.0), 60.0) * 1.5 * p.gloss * thick;

    float3 color = mix(clay, glaze * diffuse, thick)
                 + spec * float3(1.0, 0.9, 0.75);
    // A little darker toward the picture's edge.
    float2 c = uv - 0.5;
    color *= 1.0 - 0.6 * dot(c, c);
    return float4(color, 1.0);
}

float sound_hash(float n) {
    return fract(sin(n * 12.9898) * 43758.5453);
}

// The sound follows the picture's ideas. A low hum is the feed: two
// tones a fifth apart whose beating speeds up with feed. The glaze as it
// spreads is a ticking, like glaze crazing as it cools: short pings at
// random moments, more of them as the pattern grows over the first 20
// seconds, higher when the kill is high and lower when it is low.
float2 sound(float t, constant Params& p) {
    float fade = smoothstep(0.0, 1.5, t);

    float beat = 0.3 + 0.03 * p.feed;
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * beat * 0.25 * t))
              + 0.5 * sin(TAU * 110.0 * t + 0.5 * sin(TAU * 0.07 * t));
    hum *= 0.19;

    // 17 chances a second for a ping; at first 3 of them ring, all 17
    // once the pattern has grown.
    float grown = smoothstep(0.0, 20.0, t);
    float rate = 17.0;
    float2 pings = float2(0.0);
    for (int k = 0; k < 5; k++) {
        float slot = floor(t * rate) - float(k);
        if (sound_hash(slot + 0.9) > 0.18 + 0.82 * grown) continue;
        float when = (slot + sound_hash(slot)) / rate;
        float age = t - when;
        if (age < 0.0) continue;
        float step5 = floor(sound_hash(slot + 0.5) * 5.0);
        // A pentatonic scale: 0, 2, 4, 7 or 9 semitones up.
        float semis = step5 < 1.0 ? 0.0 : step5 < 2.0 ? 2.0 : step5 < 3.0 ? 4.0
                    : step5 < 4.0 ? 7.0 : 9.0;
        float shift = (p.kill - 62.0) * 0.4;
        float pitch = 880.0 * pow(2.0, (semis + shift) / 12.0);
        float env = exp(-age * 28.0) * smoothstep(0.0, 0.002, age);
        float loud = 0.5 + 0.5 * sound_hash(slot + 0.25);
        float v = sin(TAU * pitch * age) * env * loud;
        float pan = sound_hash(slot + 0.75);
        pings += v * float2(1.0 - pan, pan);
    }
    pings *= 0.33;

    float2 s = (hum + pings) * fade * p.volume;
    return s;
}
1 line from stage 3 that are gone
    return float4(mix(clay, glaze, thick), 1.0);

The sound

The sound can't read the cells: it is worked out from the time alone. So it follows the same ideas instead. A low hum is the feed: D2 (73.42 Hz) and A2 (110 Hz), a fifth apart. The D swells and fades at (0.3 + 0.03 × feed) / 4 times a second, about twice in four seconds at the default, faster with more feed, and the A wavers slowly in pitch. Over it, the glaze ticks as it spreads, as glaze can ping when it crazes in a cooling kiln: 17 chances a second for a short ping, of which about 3 a second ring at first and all of them once the pattern has grown, over the first 20 seconds. Each ping is a sine at 880 Hz or 2, 4, 7 or 9 semitones above it (a pentatonic scale), dying away in a few hundredths of a second, at a random loudness and place between left and right. kill tunes the pings, up to 1.6 semitones higher when it is high and lower when it is low. The sound fades in over the first 1.5 seconds.

At the defaults the peak is −4.9 dBFS and the RMS −20.3 dBFS over the minute. With volume at 1 and feed at 70 it is −2.9 and −18.3 dBFS.

New in Ray Kiln

Cells shaders. Crawling Glaze is the first episode on a grid of cells that steps, and the whole path was built for it ahead of time. A // @cells W H line gives a shader a grid of W by H cells kept on the GPU, each a struct Cell of the shader's own, and three functions: start for each cell before the first step, next for a cell after one more step, worked out from the grid as it was after the last one, and shade, which draws the picture from the grid. cells.at(int2(x, y)) reads a cell and wraps round at every edge, cells.at(uv) reads the cell under a point, and // @steps N steps the grid N times each 60th of a second. The grid steps at a fixed rate from start, whatever the frame rate, so a still, the loop and the videos agree. It works in the browser and in raykiln's renders now, and in the Mac app with its next release.

It is how the Game of Life, a reaction like this one, falling sand or a wave on a drum are made. Open Crawling Glaze in the playground, change the rule in next, and watch what it grows instead. Here, every stage is a cells shader, and the WebGPU picture at 40 seconds, after 19,200 steps, matched the Metal one.

Try this

  • Set kill to 61: the glaze floods much of the plate, and the rest is a net of glaze with round holes. At 66 only a few slow worms grow.
  • Set feed to 35: the pattern breaks into spots between the worms. At 70 the worms are thicker and run into each other.
  • Set drift to 0: the feed and kill are the same everywhere, and the worms settle into one even maze.
  • Set seeds to 3: the pattern grows from three places, and still nearly covers the plate by 20 seconds. Each drop is enough to start the whole thing.
  • In next, change B's 0.5 * lap.y to 1.0 * lap.y: B spreads as fast as A, and no pattern forms, which is Turing's point: the two have to spread at different speeds.
  • In next, set the corner weight 0.05 to 0.0 and the side weight 0.2 to 0.25: a Laplacian from the four neighbors beside each cell. Both chemicals now spread a sixth slower, and that is enough to change everything: the glaze floods the plate, its edges ragged along the grid.

Parameters

ParameterDefaultRangeWhat it does
feed5535 to 70How fast A is fed in, in thousandths: low gives spots, high gives worms and holes
kill6261 to 66How fast B dies away, in thousandths: low floods the plate, high leaves a few slow worms
drift10 to 1.2How much the feed and kill change across the plate and over time
seeds303 to 60How many drops of glaze start the pattern
gloss10 to 2How shiny the glaze is
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.

crawling-glaze.metal
// Ray Kiln · Crawling Glaze
//
// @episode Crawling Glaze
// @length 60
// @variation 30
// @variations 1,2,3,5,7,8,9,11
// @sweeps feed, kill, drift
// @still 40
// @teaches Reaction-diffusion: two chemicals that feed, spread and die, a pattern that grows itself
// @category Compute
// @tags reaction-diffusion, gray-scott, laplacian, diffusion, compute shaders, procedural sound
// @final Crawling Glaze | The glaze is lit: its slope from the cells around it, a gloss where it faces the light.
//
// Two chemicals share a grid of cells. A is fed in everywhere at a
// steady rate. B eats A to make more of itself, and B dies away at its
// own rate. Both spread to their neighbors, A twice as fast as B. From a
// few drops of B, that is all it takes: spots split, worms grow and
// curl, and a pattern spreads across the grid that nobody drew. This is
// the Gray-Scott model. Here B is a glaze crawling over fired clay: the
// feed and the kill change a little across the plate and slowly over
// the minute, so spots, worms and holes meet and keep moving.
// 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 384 216
// @steps 8

// @param feed 55 35 70 How fast A is fed in, in thousandths: low gives spots, high gives worms and holes
// @param kill 62 61 66 How fast B dies away, in thousandths: low floods the plate, high leaves a few slow worms
// @param drift 1 0 1.2 How much the feed and kill change across the plate and over time
// @param seeds 30 3 60 How many drops of glaze start the pattern
// @param gloss 1 0 2 How shiny the glaze is
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;

struct Cell {
    float a;   // A, fed in everywhere: 0 to 1
    float b;   // B, the glaze, which A turns into: 0 to 1
};

float glaze_hash(uint2 at) {
    uint n = at.x * 374761393u + at.y * 668265263u;
    n = (n ^ (n >> 13)) * 1274126177u;
    return float((n ^ (n >> 16)) & 0xffffu) / 65535.0;
}

// The distance between two cells on a grid that wraps round.
float wrapped_distance(float2 a, float2 b, float2 size) {
    float2 d = abs(a - b);
    d = min(d, size - d);
    return length(d);
}

// The B of the drop a cell is in, 0 outside them all. The drops are
// round, as many as seeds says, each at its own place, size and strength.
float glaze_drop(uint2 at, uint2 size, constant Params& p) {
    float b = 0.0;
    int drops = int(p.seeds + 0.5);
    for (int i = 0; i < drops; i++) {
        float cx = glaze_hash(uint2(i, 7)), cy = glaze_hash(uint2(i, 19));
        float2 center = float2(cx, cy) * float2(size);
        float radius = 2.0 + 3.0 * glaze_hash(uint2(i, 31));
        if (wrapped_distance(float2(at), center, float2(size)) < radius) {
            b = 0.25 + 0.5 * glaze_hash(uint2(i, 43));
        }
    }
    return b;
}

// Every cell starts as all A, apart from the drops of B.
Cell start(uint2 at, uint2 size, constant Params& p) {
    Cell c;
    c.b = glaze_drop(at, size, p);
    c.a = c.b > 0.0 ? 0.5 : 1.0;
    return c;
}

// The feed and kill here and now: a slow wave across the plate and
// over the minute.
float2 rates(float2 at, float2 size, float time, constant Params& p) {
    float2 q = at / size.y;
    float wave = sin(TAU * (q.x * 0.6 + q.y * 0.3 + time / 60.0))
               * sin(TAU * (q.y * 0.45 - time / 90.0) + 1.3);
    float swing = sin(TAU * time / 60.0 + q.x * 2.0);
    // The sliders are in thousandths: feed 55 is 0.055.
    float feed = 0.001 * (p.feed + 4.0 * p.drift * wave);
    float kill = 0.001 * (p.kill + 1.5 * p.drift * swing);
    return float2(feed, kill);
}

// One step of Gray-Scott. The Laplacian, how much a cell differs from
// those around it, is a weighted sum of the eight neighbors (0.2 for
// each beside it, 0.05 for each corner) less the cell itself.
Cell next(Cell me, uint2 at, Cells cells,
          constant Uniforms& u, constant Params& p) {
    float2 lap = -float2(me.a, me.b);
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            Cell n = cells.at(int2(at) + int2(dx, dy));
            float w = (dx == 0 || dy == 0) ? 0.2 : 0.05;
            lap += w * float2(n.a, n.b);
        }
    }
    float2 fk = rates(float2(at), float2(cells.size), u.time, p);
    float react = me.a * me.b * me.b;
    Cell c;
    c.a = clamp(me.a + 1.0 * lap.x - react + fk.x * (1.0 - me.a), 0.0, 1.0);
    c.b = clamp(me.b + 0.5 * lap.y + react - (fk.y + fk.x) * me.b, 0.0, 1.0);
    return c;
}

// How much glaze lies at a point in cell units, read smoothly between
// the four nearest cells.
float glaze_at(Cells cells, float2 g) {
    float2 f = g - 0.5;
    int2 i = int2(floor(f));
    float2 w = f - floor(f);
    float b00 = cells.at(i).b;
    float b10 = cells.at(i + int2(1, 0)).b;
    float b01 = cells.at(i + int2(0, 1)).b;
    float b11 = cells.at(i + int2(1, 1)).b;
    return mix(mix(b00, b10, w.x), mix(b01, b11, w.x), w.y);
}

// 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 grid_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));
}

float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
             Cells cells) {
    float2 g = grid_point(uv, u.resolution, float2(cells.size));
    float b = glaze_at(cells, g);
    // The glaze starts where b passes 0.12 and is whole by 0.35.
    float thick = smoothstep(0.12, 0.35, b);

    // Its slope, from the glaze a cell to each side, gives a normal.
    float bx = glaze_at(cells, g + float2(1.0, 0.0))
             - glaze_at(cells, g - float2(1.0, 0.0));
    float by = glaze_at(cells, g + float2(0.0, 1.0))
             - glaze_at(cells, g - float2(0.0, 1.0));
    float3 n = normalize(float3(-bx * 4.0, -by * 4.0, 1.0));

    // The clay: dark and dry, with a little grain.
    float grain = glaze_hash(uint2(g * 3.0));
    float3 clay = float3(0.075, 0.050, 0.045) * (0.85 + 0.3 * grain);

    // The glaze: thin at its edge, deep where it pools, in the kiln's colors.
    float3 glaze = kiln(0.32 + 0.52 * smoothstep(0.12, 0.4, b));

    // A light up and to the left, and a highlight where the glaze faces
    // halfway between it and the eye, as shiny as gloss.
    float3 light = normalize(float3(-0.5, 0.6, 0.65));
    float diffuse = 0.55 + 0.45 * max(dot(n, light), 0.0);
    float3 h = normalize(light + float3(0.0, 0.0, 1.0));
    float spec = pow(max(dot(n, h), 0.0), 60.0) * 1.5 * p.gloss * thick;

    float3 color = mix(clay, glaze * diffuse, thick)
                 + spec * float3(1.0, 0.9, 0.75);
    // A little darker toward the picture's edge.
    float2 c = uv - 0.5;
    color *= 1.0 - 0.6 * dot(c, c);
    return float4(color, 1.0);
}

float sound_hash(float n) {
    return fract(sin(n * 12.9898) * 43758.5453);
}

// The sound follows the picture's ideas. A low hum is the feed: two
// tones a fifth apart whose beating speeds up with feed. The glaze as it
// spreads is a ticking, like glaze crazing as it cools: short pings at
// random moments, more of them as the pattern grows over the first 20
// seconds, higher when the kill is high and lower when it is low.
float2 sound(float t, constant Params& p) {
    float fade = smoothstep(0.0, 1.5, t);

    float beat = 0.3 + 0.03 * p.feed;
    float hum = sin(TAU * 73.42 * t) * (0.6 + 0.4 * sin(TAU * beat * 0.25 * t))
              + 0.5 * sin(TAU * 110.0 * t + 0.5 * sin(TAU * 0.07 * t));
    hum *= 0.19;

    // 17 chances a second for a ping; at first 3 of them ring, all 17
    // once the pattern has grown.
    float grown = smoothstep(0.0, 20.0, t);
    float rate = 17.0;
    float2 pings = float2(0.0);
    for (int k = 0; k < 5; k++) {
        float slot = floor(t * rate) - float(k);
        if (sound_hash(slot + 0.9) > 0.18 + 0.82 * grown) continue;
        float when = (slot + sound_hash(slot)) / rate;
        float age = t - when;
        if (age < 0.0) continue;
        float step5 = floor(sound_hash(slot + 0.5) * 5.0);
        // A pentatonic scale: 0, 2, 4, 7 or 9 semitones up.
        float semis = step5 < 1.0 ? 0.0 : step5 < 2.0 ? 2.0 : step5 < 3.0 ? 4.0
                    : step5 < 4.0 ? 7.0 : 9.0;
        float shift = (p.kill - 62.0) * 0.4;
        float pitch = 880.0 * pow(2.0, (semis + shift) / 12.0);
        float env = exp(-age * 28.0) * smoothstep(0.0, 0.002, age);
        float loud = 0.5 + 0.5 * sound_hash(slot + 0.25);
        float v = sin(TAU * pitch * age) * env * loud;
        float pan = sound_hash(slot + 0.75);
        pings += v * float2(1.0 - pan, pan);
    }
    pings *= 0.33;

    float2 s = (hum + pings) * fade * p.volume;
    return s;
}

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] = [55, 62, 1, 30, 1, 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 Particles & Simulation