Shader

Pine Cone

A pine cone made by mesh shaders, in 145 pieces: a core with its stem, and 144 scales, each one a piece of its own. Scale n sits n golden angles round the cone, about 137.5° from one to the next, so the scales pack into crossing spirals, as a sunflower's seeds do. Once every 40 seconds the cone shuts, tip first, and opens again, base first, each scale turning about its root away from a core that glows like a kiln through the gaps, and each one ticks as it moves.

· Geometry · Mesh shader

Teaches Scales placed by the golden angle, each a piece of geometry made on the GPU

Builds on Kaleidoscope (a note of the scale), Cracked Clay (integer hash), Rippling Water, Crystal Bloom

Watch

The idea

Crystal Bloom made its geometry with mesh shaders: the host asks each piece how many corners and triangles it wants, then asks for every corner and every triangle by number, and a group of 64 GPU threads builds the piece, up to 64 corners and 64 triangles. Here the pieces are the scales of a pine cone, and the question is where to put them. A plant that grows its leaves, seeds or scales one at a time from a point turns each new one about 137.5° round from the last, the golden angle, the share of a full turn that is least like any simple fraction. No scale lands in line with the ones just before it, and they pack into crossing spirals with no gaps. That pattern is phyllotaxis.

There are 145 pieces, // @mesh 145. Piece 0 is the core and its stem, and the other 144, a Fibonacci number, are scales. The cone is an egg about 1.8 tall and 1.1 across at its widest, a little below the middle, and the camera goes round it once in 40 seconds, 3 away, rising and dipping twice. It is built in four steps.

Stage 1 · One scale

One piece, // @mesh 1, one scale. size asks for 31 corners and 54 triangles, under the limit of 64 of each. The corners are five rings of six and a boss: coneRing says how far out along the scale each ring sits and how wide and thick it is, as parts of the end's size, so the scale is a thin stalk at its root (0.3 of the end's width and 0.1 of its thickness) that swells into a diamond end. Each ring is a hexagon with its four slanting corners pulled in, so the end reads as a diamond, and the last corner, the boss, stands out of the middle of the end. triangle joins the rings as Crystal Bloom joins its crystal's: two triangles for each face of each band between two rings, 48, and a fan of 6 up to the boss.

Scale holds everything about the scale: its root, the way it points (dir), its across (side) and its thickness (thick), its length and the size of its end. A corner is the root plus so far along dir, so far along side and so far along thick, so the same corners work for a scale pointing any way. The size slider scales the end. shade lights each face flat, from the normal of the triangle under the pixel (the cross product of dfdx and dfdy, as in Crystal Bloom), turned to face the eye, with a light high above it. The wood is dark near the root and paler gold on the end.

stage-1-one-scale.metal
// Stage 1 · One scale
// A mesh shader makes its own geometry, a piece at a time. Here there is
// one piece, one scale of a pine cone: five rings of six corners, thin at
// the root and thick at the end, and a boss standing out of the end's
// middle. 31 corners and 54 triangles, under the limit of 64 of each.
//
// @mesh 1

// @param size 1 0.6 1.4 How big the scale's end is

constant float TAU = 6.2831853;
constant float CYCLE = 40.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and how far out along its scale it is.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
};

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round in 40 seconds, rising and dipping twice.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, and the size of its end.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
};

// The one scale, pointing out and a little up.
Scale coneScale(uint n, constant Params& p) {
    Scale s;
    s.wide = 0.8 * p.size;
    s.tall = 0.58 * p.size;
    s.dir = normalize(float3(1.0, 0.45, 0.0));
    s.side = float3(0.0, 0.0, -1.0);
    s.thick = cross(s.side, s.dir);
    s.len = 2.1;
    s.root = float3(-1.15, -0.85, 0.0);
    return s;
}

// Every scale is 31 corners, six round each of five rings and the boss,
// and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    return uint2(31, 54);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;

    Scale s = coneScale(piece, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = 6u;
    uint bands = 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5) * 0.7, end);
    return float4(wood * lit, 1.0);
}

// Behind it: the dark of the kiln, with a glow round the middle.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12;
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}

Stage 2 · By the golden angle

144 pieces, seen from high above. Scale n goes n turns of turn round, and out from the middle as the square root of n, so each has the same share of the disk, and every scale points up, leaning out. At 137.5°, the golden angle, they fill the disk evenly in crossing spirals, as a sunflower's seeds do. Move turn off it and the pattern falls apart into spokes: at 135°, three eighths of a turn, every eighth scale lands on the same line and there are 8 spokes; at 120°, a third of a turn, there are 3.

stage-2-golden-angle.metal13 new lines since stage 1
// Stage 2 · By the golden angle
// 144 pieces, one scale each, seen from above. Scale n sits n turns of
// the angle round and the square root of n out, so each has the same
// room. At 137.5 degrees, the golden angle, they pack with no gaps into
// spirals of 8, 13 and 21, as a sunflower's seeds do. Move turn off it.
//
// @mesh 144

// @param turn 137.5 120 150 Degrees round from one scale to the next: 137.5 is the golden angle
// @param size 1 0.6 1.4 How big each scale is

constant float TAU = 6.2831853;
// 144 scales, a Fibonacci number.
constant uint SCALES = 144;
constant float CYCLE = 40.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and how far out along its scale it is.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
};

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round in 40 seconds, high above, rising and dipping.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 2.4 + 0.3 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, and the size of its end.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
};

Scale coneScale(uint n, constant Params& p) {
    Scale s;
    // Scale n goes n turns of the angle round, and out as the square
    // root of n.
    float u = (float(n) + 0.5) / float(SCALES);
    float a = float(n) * p.turn * TAU / 360.0;
    float3 out = float3(cos(a), 0.0, sin(a));
    float3 at = out * 1.5 * sqrt(u) + float3(0.0, -0.3, 0.0);

    // The room each has: the disk's area shared out among them.
    float room = 1.5 * sqrt(TAU * 0.5 / float(SCALES));
    s.wide = room * 0.72 * p.size;
    s.tall = room * 0.58 * p.size;

    // It points up, leaning out, from a root below the disk.
    s.dir = normalize(out * 0.4 + float3(0.0, 1.0, 0.0));
    s.side = float3(sin(a), 0.0, -cos(a));
    s.thick = cross(s.side, s.dir);
    s.len = 0.35;
    s.root = at - s.dir * s.len;
    return s;
}

// Every scale is 31 corners, six round each of five rings and the boss,
// and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    return uint2(31, 54);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;

    Scale s = coneScale(piece, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = 6u;
    uint bands = 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5) * 0.7, end);
    return float4(wood * lit, 1.0);
}

// Behind it: the dark of the kiln, with a glow round the middle.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12;
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}
7 lines from stage 1 that are gone
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
⋯
    s.wide = 0.8 * p.size;
    s.tall = 0.58 * p.size;
    s.dir = normalize(float3(1.0, 0.45, 0.0));
    s.side = float3(0.0, 0.0, -1.0);
⋯
    s.len = 2.1;
    s.root = float3(-1.15, -0.85, 0.0);

Stage 3 · On the cone

The disk wraps onto the egg, its middle at the tip. coneBody is the egg: for a height h from 1 at the tip to −1 at the base, it gives the radius there and the height in the world, wider below the middle. Scale n goes n turns of the angle round and down the egg so that each has about the same share of it, a little less near the tip, down to near the stem. Each is sized to its share: its width from the room round it, its end's height from how much the egg stretches there. It points out of the egg's surface and a little up, from a root deep inside, and it is longer where the egg is wider, so the scales near the middle are longest.

Piece 0 is the core and stem, a fixed 34 corners and 64 triangles: four rings of eight down the axis, from the stem up, and a point at each end, joined in bands of 16 triangles and a fan of 8 at each end. It is dark wood. The whole cone is 7,840 triangles.

stage-3-on-the-cone.metal42 new lines since stage 2
// Stage 3 · On the cone
// The disk wraps onto an egg, its middle at the tip. Scale n goes n golden
// angles round and down the egg so each has the same share of it, a
// little less near the tip, sized to its share and pointing out. Piece 0
// is the core and stem: rings of eight down the axis, 64 triangles.
//
// @mesh 145

// @param turn 137.5 120 150 Degrees round from one scale to the next: 137.5 is the golden angle
// @param size 1 0.6 1.4 How big each scale is

constant float TAU = 6.2831853;
// 144 scales, a Fibonacci number, from the tip of the egg down to near
// the stem; and how long the eye takes to go round, in seconds.
constant uint SCALES = 144;
constant float COVER = 0.94;
constant float CYCLE = 40.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, how far out along its scale it is, and 1 on
// the core.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
    float core;
};

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round the cone in a cycle, rising and dipping twice.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// The cone's body, an egg wider below the middle. h runs from 1 at the
// tip to -1 at the base; out comes the egg's radius and height there.
float2 coneBody(float h) {
    float r = sqrt(max(1.0 - h * h, 0.0)) * 0.52 * (1.0 - 0.2 * h);
    return float2(r, 0.92 * h);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, and the size of its end.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
};

Scale coneScale(uint n, constant Params& p) {
    Scale s;
    // Scale n goes n turns of the angle round, and down the egg so that
    // each has the same share of it, a little less near the tip.
    float u = (float(n) + 0.5) / float(SCALES);
    float h = 1.0 - 2.0 * COVER * pow(u, 1.25);
    float a = float(n) * p.turn * TAU / 360.0;
    float3 out = float3(cos(a), 0.0, sin(a));
    float2 b = coneBody(h);
    float3 at = out * b.x + float3(0.0, b.y, 0.0);

    // Its share of the egg sets its size: the room round it on a ball,
    // stretched as the egg stretches the ball there.
    float th = acos(h);
    float2 m = coneBody(cos(th + 0.01)) - coneBody(cos(th - 0.01));
    float room = sqrt(5.0 * TAU * COVER * pow(u, 0.25) / float(SCALES));
    s.wide = room * b.x / sqrt(1.0 - h * h) * 0.62 * p.size;
    s.tall = room * length(m) / 0.02 * 0.5 * p.size;

    // It points out of the egg and a little up, from a root deep inside.
    float2 nrm = normalize(float2(-m.y, m.x));
    s.dir = normalize(out * nrm.x + float3(0.0, nrm.y + 0.45, 0.0));
    s.side = float3(sin(a), 0.0, -cos(a));
    s.thick = cross(s.side, s.dir);
    s.len = 0.1 + 0.9 * b.x;
    s.root = at - s.dir * s.len;
    return s;
}

// The core is a fixed 34 corners and 64 triangles; every scale is 31
// corners, six round each of five rings and the boss, and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    if (piece == 0u) return uint2(34, 64);
    return uint2(31, 54);
}

// A core corner: four rings of eight down the axis, from the stem up, then
// its two ends.
float3 coneCore(uint i) {
    const float ys[4] = {-1.24, -0.80, 0.05, 0.66};
    const float rs[4] = {0.045, 0.13, 0.25, 0.1};
    if (i >= 32u) return float3(0.0, i == 32u ? -1.27 : 0.76, 0.0);
    int ring = int(i / 8u);
    float a = float(i % 8u) * TAU / 8.0;
    return float3(cos(a) * rs[ring], ys[ring], sin(a) * rs[ring]);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;
    if (piece == 0u) {
        o.world = coneCore(i);
        o.position = coneScreen(o.world, u.time, aspect);
        o.along = 0.0; o.core = 1.0;
        return o;
    }

    Scale s = coneScale(piece - 1u, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    o.core = 0.0;
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss. The core's are bands between its rings and fans to its
// two ends.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = piece == 0u ? 8u : 6u;
    uint bands = piece == 0u ? 3u : 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    if (piece == 0u) {
        if (i - 2u * s * bands < s) return uint3(a, b, 32u);
        return uint3(24u + a, 24u + b, 33u);
    }
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    // The core is dark wood.
    if (v.core > 0.5) return float4(kiln(0.1) * lit, 1.0);

    // A scale: dark near the root and pale on its end.
    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5) * 0.7, end);
    return float4(wood * lit, 1.0);
}

// Behind it: the dark of the kiln, with a glow round the middle.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12;
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}
10 lines from stage 2 that are gone
    float h = 2.4 + 0.3 * sin(2.0 * TAU * t / CYCLE);
⋯
    float3 at = out * 1.5 * sqrt(u) + float3(0.0, -0.3, 0.0);
⋯
    float room = 1.5 * sqrt(TAU * 0.5 / float(SCALES));
    s.wide = room * 0.72 * p.size;
    s.tall = room * 0.58 * p.size;
⋯
    s.dir = normalize(out * 0.4 + float3(0.0, 1.0, 0.0));
⋯
    s.len = 0.35;
⋯
    Scale s = coneScale(piece, p);
⋯
    uint s = 6u;
    uint bands = 4u;

Stage 4 · Opening

A dry cone opens. coneOpening goes from shut to open and back once a cycle, 40 seconds at speed 1, and the episode starts with the cone open, a while before it shuts. Each scale follows it with a lag: the tip's scales are the last to open and the first to shut, so the cone shuts from the tip down and opens from the base up. Opening turns a scale about its root, round its across, until it points out and a little down, and draws it a little out of the core, so gaps open between the rows. open sets how far: at 0 the cone stays shut, and at 1.3 the scales turn further down. The scales are in shadow deep inside the cone, where their neighbors hide the light. Every corner is worked out fresh every frame, as a mesh shader's always are.

stage-4-opening.metal19 new lines since stage 3
// Stage 4 · Opening
// A dry cone opens. Every 40 seconds it shuts, tip first, and opens
// again, base first, each scale turning about its root until it points
// out and a little down, and drawn a little out of the core, so gaps open
// between the rows. The whole picture is made fresh every frame.
//
// @mesh 145

// @param speed 1 0 3 How fast the cone opens and shuts
// @param open 1 0 1.3 How far the scales turn away from the core
// @param turn 137.5 120 150 Degrees round from one scale to the next: 137.5 is the golden angle
// @param size 1 0.6 1.4 How big each scale is

constant float TAU = 6.2831853;
// 144 scales, a Fibonacci number, from the tip of the egg down to near
// the stem; and how long the cone takes to shut and open, in seconds.
constant uint SCALES = 144;
constant float COVER = 0.94;
constant float CYCLE = 40.0;
// Where in its cycle the cone starts: open, a while before it shuts.
constant float START = 17.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, how far out along its scale it is, and 1 on
// the core.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
    float core;
};

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round the cone in a cycle, rising and dipping twice.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// The cone's body, an egg wider below the middle. h runs from 1 at the
// tip to -1 at the base; out comes the egg's radius and height there.
float2 coneBody(float h) {
    float r = sqrt(max(1.0 - h * h, 0.0)) * 0.52 * (1.0 - 0.2 * h);
    return float2(r, 0.92 * h);
}

// How open the whole cone is at time T: it opens and shuts once a cycle.
float coneOpening(float T) {
    return 0.5 - 0.5 * cos(TAU * (T + START) / CYCLE);
}

// How far scale n has opened, 0 to 1: the ones at the base go first.
float coneOpen(uint n, float T) {
    float lag = 1.0 - float(n) / float(SCALES - 1u);
    return smoothstep(0.0, 1.0, (coneOpening(T) * 1.5 - lag) / 0.5);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, the size of its end, and how open it is.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
    float open;
};

Scale coneScale(uint n, float T, constant Params& p) {
    Scale s;
    // Scale n goes n turns of the angle round, and down the egg so that
    // each has the same share of it, a little less near the tip.
    float u = (float(n) + 0.5) / float(SCALES);
    float h = 1.0 - 2.0 * COVER * pow(u, 1.25);
    float a = float(n) * p.turn * TAU / 360.0;
    float3 out = float3(cos(a), 0.0, sin(a));
    float2 b = coneBody(h);
    float3 at = out * b.x + float3(0.0, b.y, 0.0);

    // Its share of the egg sets its size: the room round it on a ball,
    // stretched as the egg stretches the ball there.
    float th = acos(h);
    float2 m = coneBody(cos(th + 0.01)) - coneBody(cos(th - 0.01));
    float room = sqrt(5.0 * TAU * COVER * pow(u, 0.25) / float(SCALES));
    s.wide = room * b.x / sqrt(1.0 - h * h) * 0.62 * p.size;
    s.tall = room * length(m) / 0.02 * 0.5 * p.size;

    // It points out of the egg and a little up, from a root deep inside.
    float2 nrm = normalize(float2(-m.y, m.x));
    s.dir = normalize(out * nrm.x + float3(0.0, nrm.y + 0.45, 0.0));
    s.side = float3(sin(a), 0.0, -cos(a));
    s.thick = cross(s.side, s.dir);
    s.len = 0.1 + 0.9 * b.x;
    s.root = at - s.dir * s.len;

    // Opening turns it about its root, round its across, until it points
    // out and a little down, and draws it out of the core.
    s.open = coneOpen(n, T);
    s.len += s.open * p.open * 0.28;
    float turn = s.open * p.open * (asin(s.dir.y) + 0.45);
    float3 d = s.dir * cos(turn) + s.thick * sin(turn);
    s.thick = s.thick * cos(turn) - s.dir * sin(turn);
    s.dir = d;
    return s;
}

// The core is a fixed 34 corners and 64 triangles; every scale is 31
// corners, six round each of five rings and the boss, and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    if (piece == 0u) return uint2(34, 64);
    return uint2(31, 54);
}

// A core corner: four rings of eight down the axis, from the stem up, then
// its two ends.
float3 coneCore(uint i) {
    const float ys[4] = {-1.24, -0.80, 0.05, 0.66};
    const float rs[4] = {0.045, 0.13, 0.25, 0.1};
    if (i >= 32u) return float3(0.0, i == 32u ? -1.27 : 0.76, 0.0);
    int ring = int(i / 8u);
    float a = float(i % 8u) * TAU / 8.0;
    return float3(cos(a) * rs[ring], ys[ring], sin(a) * rs[ring]);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;
    float T = u.time * p.speed;
    if (piece == 0u) {
        o.world = coneCore(i);
        o.position = coneScreen(o.world, u.time, aspect);
        o.along = 0.0; o.core = 1.0;
        return o;
    }

    Scale s = coneScale(piece - 1u, T, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    o.core = 0.0;
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss. The core's are bands between its rings and fans to its
// two ends.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = piece == 0u ? 8u : 6u;
    uint bands = piece == 0u ? 3u : 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    if (piece == 0u) {
        if (i - 2u * s * bands < s) return uint3(a, b, 32u);
        return uint3(24u + a, 24u + b, 33u);
    }
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    // The core is dark wood.
    if (v.core > 0.5) return float4(kiln(0.1) * lit, 1.0);

    // A scale: dark near the root and pale on its end, and in shadow deep
    // inside the cone, where its neighbors hide the light.
    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5) * 0.7, end);
    lit *= 0.3 + 0.7 * smoothstep(0.2, 0.9, v.along);
    return float4(wood * lit, 1.0);
}

// Behind it: the dark of the kiln, with a glow round the middle.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12;
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}
2 lines from stage 3 that are gone
Scale coneScale(uint n, constant Params& p) {
⋯
    Scale s = coneScale(piece - 1u, p);

Stage 5 · Pine cone

The core glows like a kiln inside. Each corner of the core opens with the scales round it, base first, and the stem never does; shade adds the kiln palette to the core, from orange toward gold as it opens, times glow. A scale glows too where it is deep and open, near its root, so the light seems to come up from inside through the gaps. Each scale gets a random tint from its number (Cracked Clay's hashU), a gold rim where a face is seen edge on, and the background warms round the cone as it opens. At glow 0 the gaps stay dark, as in stage 4. The picture loops at speed 1: the cycle and the camera's turn are both 40 seconds, and the camera always turns at its own pace, whatever the speed.

the kiln palette and shade(), from the finished shader
// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    // The core glows from inside as the scales round it open.
    float heat = p.glow * v.open;
    if (v.core > 0.5) {
        float3 c = kiln(0.1) * lit + kiln(0.42 + 0.35 * heat) * heat;
        return float4(c, 1.0);
    }

    // A scale: dark near the root, pale on its end, lit by the core where
    // it is deep and open, with a rim where it is seen edge on.
    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3 + 0.08 * v.tint) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5 + 0.1 * v.tint) * 0.7, end);
    lit *= 0.3 + 0.7 * smoothstep(0.2, 0.9, v.along);
    float deep = 1.0 - smoothstep(0.1, 0.85, v.along);
    float3 burn = kiln(0.42 + 0.35 * heat * deep) * heat * deep;
    float rim = pow(1.0 - max(dot(n, toEye), 0.0), 3.0);
    float3 c = wood * lit + burn + kiln(0.62) * rim * 0.35;
    return float4(c, 1.0);
}
pine-cone.metal, the whole file68 new lines since stage 4
// Ray Kiln · Pine Cone
//
// @episode Pine Cone
// @length 40
// @variation 30
// @variations 2,3,6,7,8,9,10,11
// @still 36
// @teaches Scales placed by the golden angle, each a piece of geometry made on the GPU
// @category Mesh
// @tags mesh shaders, golden angle, phyllotaxis, flat shading, emission, procedural sound
// @final Pine cone | The cone shuts from the tip down and opens again from the base up, each scale lifting away from a core that glows like a kiln inside, every scale ticking as it moves.
//
// A pine cone made by mesh shaders, in 145 pieces. Piece 0 is the core and
// its stem. Every other piece is one scale: a thin root that thickens to a
// diamond end with a boss in its middle, 31 corners and 54 triangles. Scale
// n sits n golden angles round the cone, so the scales fall into the 8 and
// 13 spirals a real cone shows. Once a cycle the cone shuts, tip first,
// and opens again, base first, each scale turning about its root away from
// the core, which glows through the gaps. The cycle and the camera's turn
// are both 40 seconds, so the shader loops.
// 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
//
// @mesh 145

// @param speed 1 0 3 How fast the cone opens and shuts
// @param open 1 0 1.3 How far the scales turn away from the core
// @param turn 137.5 120 150 Degrees round from one scale to the next: 137.5 is the golden angle
// @param size 1 0.6 1.4 How big each scale is
// @param glow 1 0 2 How hot the core burns through the open scales
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
// 144 scales, a Fibonacci number, from the tip of the egg down to near
// the stem; and how long the cone takes to shut and open, in seconds.
constant uint SCALES = 144;
constant float COVER = 0.94;
constant float CYCLE = 40.0;
// Where in its cycle the cone starts: open, a while before it shuts.
constant float START = 17.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, how far out along its scale it is, how far
// open it is, its own tint, and 1 on the core.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
    float open;
    float tint;
    float core;
};

// Cracked Clay's hash: a whole number in, a random 0 to 1 out.
float hashU(uint n) {
    n = (n << 13u) ^ n;
    n = n * (n * n * 15731u + 789221u) + 1376312589u;
    return float(n & 0x7fffffffu) / float(0x7fffffff);
}

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round the cone in a cycle, rising and dipping twice.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// The cone's body, an egg wider below the middle. h runs from 1 at the
// tip to -1 at the base; out comes the egg's radius and height there.
float2 coneBody(float h) {
    float r = sqrt(max(1.0 - h * h, 0.0)) * 0.52 * (1.0 - 0.2 * h);
    return float2(r, 0.92 * h);
}

// How open the whole cone is at time T: it opens and shuts once a cycle.
float coneOpening(float T) {
    return 0.5 - 0.5 * cos(TAU * (T + START) / CYCLE);
}

// How far scale n has opened, 0 to 1: the ones at the base go first.
float coneOpen(uint n, float T) {
    float lag = 1.0 - float(n) / float(SCALES - 1u);
    return smoothstep(0.0, 1.0, (coneOpening(T) * 1.5 - lag) / 0.5);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, the size of its end, and how open it is.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
    float open;
};

Scale coneScale(uint n, float T, constant Params& p) {
    Scale s;
    // Scale n goes n turns of the angle round, and down the egg so that
    // each has the same share of it, a little less near the tip.
    float u = (float(n) + 0.5) / float(SCALES);
    float h = 1.0 - 2.0 * COVER * pow(u, 1.25);
    float a = float(n) * p.turn * TAU / 360.0;
    float3 out = float3(cos(a), 0.0, sin(a));
    float2 b = coneBody(h);
    float3 at = out * b.x + float3(0.0, b.y, 0.0);

    // Its share of the egg sets its size: the room round it on a ball,
    // stretched as the egg stretches the ball there.
    float th = acos(h);
    float2 m = coneBody(cos(th + 0.01)) - coneBody(cos(th - 0.01));
    float room = sqrt(5.0 * TAU * COVER * pow(u, 0.25) / float(SCALES));
    s.wide = room * b.x / sqrt(1.0 - h * h) * 0.62 * p.size;
    s.tall = room * length(m) / 0.02 * 0.5 * p.size;

    // It points out of the egg and a little up, from a root deep inside.
    float2 nrm = normalize(float2(-m.y, m.x));
    s.dir = normalize(out * nrm.x + float3(0.0, nrm.y + 0.45, 0.0));
    s.side = float3(sin(a), 0.0, -cos(a));
    s.thick = cross(s.side, s.dir);
    s.len = 0.1 + 0.9 * b.x;
    s.root = at - s.dir * s.len;

    // Opening turns it about its root, round its across, until it points
    // out and a little down, and draws it out of the core.
    s.open = coneOpen(n, T);
    s.len += s.open * p.open * 0.28;
    float turn = s.open * p.open * (asin(s.dir.y) + 0.45);
    float3 d = s.dir * cos(turn) + s.thick * sin(turn);
    s.thick = s.thick * cos(turn) - s.dir * sin(turn);
    s.dir = d;
    return s;
}

// The core is a fixed 34 corners and 64 triangles; every scale is 31
// corners, six round each of five rings and the boss, and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    if (piece == 0u) return uint2(34, 64);
    return uint2(31, 54);
}

// A core corner: four rings of eight down the axis, from the stem up, then
// its two ends.
float3 coneCore(uint i) {
    const float ys[4] = {-1.24, -0.80, 0.05, 0.66};
    const float rs[4] = {0.045, 0.13, 0.25, 0.1};
    if (i >= 32u) return float3(0.0, i == 32u ? -1.27 : 0.76, 0.0);
    int ring = int(i / 8u);
    float a = float(i % 8u) * TAU / 8.0;
    return float3(cos(a) * rs[ring], ys[ring], sin(a) * rs[ring]);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;
    float T = u.time * p.speed;
    if (piece == 0u) {
        o.world = coneCore(i);
        o.position = coneScreen(o.world, u.time, aspect);
        o.along = 0.0; o.core = 1.0;
        // The core opens with the scales round it, base first; the stem
        // never does.
        float up = clamp((o.world.y + 0.8) / 1.6, 0.0, 1.0);
        o.open = smoothstep(0.0, 1.0, coneOpening(T) * 1.5 - up);
        o.open *= smoothstep(-0.8, -0.5, o.world.y) * min(p.open, 1.0);
        o.tint = 0.0;
        return o;
    }

    Scale s = coneScale(piece - 1u, T, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    o.open = s.open;
    o.tint = hashU(piece + 300u);
    o.core = 0.0;
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss. The core's are bands between its rings and fans to its
// two ends.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = piece == 0u ? 8u : 6u;
    uint bands = piece == 0u ? 3u : 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    if (piece == 0u) {
        if (i - 2u * s * bands < s) return uint3(a, b, 32u);
        return uint3(24u + a, 24u + b, 33u);
    }
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    // The core glows from inside as the scales round it open.
    float heat = p.glow * v.open;
    if (v.core > 0.5) {
        float3 c = kiln(0.1) * lit + kiln(0.42 + 0.35 * heat) * heat;
        return float4(c, 1.0);
    }

    // A scale: dark near the root, pale on its end, lit by the core where
    // it is deep and open, with a rim where it is seen edge on.
    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3 + 0.08 * v.tint) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5 + 0.1 * v.tint) * 0.7, end);
    lit *= 0.3 + 0.7 * smoothstep(0.2, 0.9, v.along);
    float deep = 1.0 - smoothstep(0.1, 0.85, v.along);
    float3 burn = kiln(0.42 + 0.35 * heat * deep) * heat * deep;
    float rim = pow(1.0 - max(dot(n, toEye), 0.0), 3.0);
    float3 c = wood * lit + burn + kiln(0.62) * rim * 0.35;
    return float4(c, 1.0);
}

// Behind it: the dark of the kiln, warming round the cone as it opens.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12 + 0.1 * p.glow * coneOpening(u.time * p.speed);
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}

// The sound: each scale ticks as it opens, and more softly an octave down
// as it shuts, its note from its place on the cone, low at the base and
// high at the tip, over a warm drone that swells as the cone opens.

// Kaleidoscope's scale: D minor pentatonic over three octaves.
float scaleNote(int i) {
    const float notes[16] = {
        293.66, 349.23, 392.00, 440.00, 523.25, 587.33, 698.46, 783.99,
        880.00, 1046.50, 1174.66, 1396.91, 1567.98, 1760.00, 2093.00, 2349.32};
    return notes[clamp(i, 0, 15)];
}

// A dry tick `since` seconds after the scale moved: a sine and a partial
// 2.76 times it, as a struck piece of wood has, both gone in moments.
float coneTick(float f, float since) {
    float on = 1.0 - exp(-since * 900.0);
    float s = sin(TAU * f * since) * exp(-since * 14.0)
            + 0.5 * sin(TAU * f * 2.76 * since) * exp(-since * 30.0);
    return s * on;
}

float2 sound(float t, constant Params& p) {
    float T = t * p.speed;
    float rate = max(p.speed, 0.01);
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));

    float2 out = float2(0.0);
    for (uint n = 0u; n < SCALES; n++) {
        // When this scale is half open, going and coming back: coneOpen
        // turned round, from the place it waits for.
        float lag = 1.0 - float(n) / float(SCALES - 1u);
        float g = (lag + 0.25) / 1.5;
        float when = acos(1.0 - 2.0 * g) * CYCLE / TAU;
        float opened = fract((T + START - when) / CYCLE) * CYCLE / rate;
        float shut = fract((T + START + when) / CYCLE) * CYCLE / rate;
        if (opened > 1.0 && shut > 1.0) continue;

        float note = scaleNote(int(lag * 11.0 + hashU(n + 77u) * 2.0));
        float s = 0.0;
        if (opened < 1.0) s += coneTick(note, opened);
        if (shut < 1.0) s += 0.5 * coneTick(note * 0.5, shut);
        float a = float(n) * p.turn * TAU / 360.0;
        float pan = clamp(dot(float3(cos(a), 0.0, sin(a)), r), -1.0, 1.0);
        pan = 0.5 + 0.4 * pan;
        out += s * 0.22 * float2(sqrt(1.0 - pan), sqrt(pan));
    }

    // The drone: a low D and the A above it, a little apart in each ear.
    float swell = 0.07 + 0.1 * coneOpening(T);
    float hum = sin(TAU * 73.4 * t) + 0.5 * sin(TAU * 110.0 * t);
    float hum2 = sin(TAU * 73.5 * t) + 0.5 * sin(TAU * 110.1 * t);
    out += swell * float2(hum, hum2);

    out = tanh(out * 1.2) / 1.2;
    return out * p.volume * smoothstep(0.0, 0.5, t);
}
5 lines from stage 4 that are gone
    if (v.core > 0.5) return float4(kiln(0.1) * lit, 1.0);
⋯
    float3 bark = kiln(0.3) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5) * 0.7, end);
⋯
    return float4(wood * lit, 1.0);
⋯
    float warm = 0.12;

The sound

Each scale ticks as it moves. sound goes through all 144 and works out, from the same lag the picture uses, the moments in the cycle when that scale is half open, going and coming back: it runs coneOpen backward. A tick is a struck piece of wood: a sine and a partial 2.76 times its pitch, the higher one dying about twice as fast, both gone in moments. A scale ticks at its note as it opens, and half as loud an octave down as it shuts. The note is from Kaleidoscope's D minor pentatonic scale, low at the base and high at the tip, up to two steps higher at random for each scale, so opening from the base up runs up the scale, and shutting from the tip down runs back down it. Each tick is placed between the ears by which way its scale faces, as the camera sees it now.

Under the ticks is a drone, a low D and the A above it, a little apart in each ear so they beat slowly, which swells as the cone opens. A tanh rounds off the loudest moments, and it fades in over the first half second.

New in Ray Kiln

The wireframe in mesh episodes. The wireframe draws a shader's triangles as cream lines over its surface. It first appeared with Rippling Water and until now worked only for vertex (@grid) episodes. Now it draws a // @mesh N episode's triangles too, as each piece makes them that frame, so a piece that changes its shape or its counts from one frame to the next shows it. In the playground, press Wireframe under the picture; raykiln render … --wireframe writes a still with the lines; and the Mac app's Shader > Show Wireframe draws them for mesh episodes with the app's next release. A mesh has no seams to mark, unlike a grid's gold border. It is live only: exports and videos are always the surface alone, and Run it live on this page has no Wireframe button, so open the playground for it.

Turn it on here and every scale shows how it is made: its 54 triangles, the bands between its five rings of six corners and the fan of six up to its boss, 31 corners in all. The core is its 64 triangles: when the cone is shut, the stem shows its lines under the scales, and as it opens, the core's long bands show between the rows. Compare it with Crystal Bloom, the first mesh episode, where the lines show each crystal's rings stacked up its length.

Try this

  • Set turn to 120: a third of a turn, so every third scale lands on the same line and the scales stack into 3 fins. At 150 they line up in 12 columns. In stage 2, 135 gives 8 spokes.
  • Set open to 0: the cone stays shut all the way round the cycle. At 1.3 the open scales turn further and the gaps between them widen.
  • Set glow to 0: the gaps between the open scales stay dark. At 2 the core burns bright gold through them.
  • Set size to 0.6: thin stalks with small ends, and the glowing core shows between them. At 1.4 the ends crowd each other and cover most of the gaps.
  • Set speed to 0: the cone holds still, nearly open, while the camera keeps going round it.
  • In coneOpen, change float lag = 1.0 - float(n) / float(SCALES - 1u); to float lag = float(n) / float(SCALES - 1u);: the cone opens from the tip down and its base stays shut longest, the stem showing under it. The ticks keep the old order, because sound works out its own lag.

Parameters

ParameterDefaultRangeWhat it does
speed10 to 3How fast the cone opens and shuts
open10 to 1.3How far the scales turn away from the core
turn137.5120 to 150Degrees round from one scale to the next: 137.5 is the golden angle
size10.6 to 1.4How big each scale is
glow10 to 2How hot the core burns through the open scales
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.

pine-cone.metal
// Ray Kiln · Pine Cone
//
// @episode Pine Cone
// @length 40
// @variation 30
// @variations 2,3,6,7,8,9,10,11
// @still 36
// @teaches Scales placed by the golden angle, each a piece of geometry made on the GPU
// @category Mesh
// @tags mesh shaders, golden angle, phyllotaxis, flat shading, emission, procedural sound
// @final Pine cone | The cone shuts from the tip down and opens again from the base up, each scale lifting away from a core that glows like a kiln inside, every scale ticking as it moves.
//
// A pine cone made by mesh shaders, in 145 pieces. Piece 0 is the core and
// its stem. Every other piece is one scale: a thin root that thickens to a
// diamond end with a boss in its middle, 31 corners and 54 triangles. Scale
// n sits n golden angles round the cone, so the scales fall into the 8 and
// 13 spirals a real cone shows. Once a cycle the cone shuts, tip first,
// and opens again, base first, each scale turning about its root away from
// the core, which glows through the gaps. The cycle and the camera's turn
// are both 40 seconds, so the shader loops.
// 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
//
// @mesh 145

// @param speed 1 0 3 How fast the cone opens and shuts
// @param open 1 0 1.3 How far the scales turn away from the core
// @param turn 137.5 120 150 Degrees round from one scale to the next: 137.5 is the golden angle
// @param size 1 0.6 1.4 How big each scale is
// @param glow 1 0 2 How hot the core burns through the open scales
// @param volume 0.8 0 1 Loudness of the sound

constant float TAU = 6.2831853;
// 144 scales, a Fibonacci number, from the tip of the egg down to near
// the stem; and how long the cone takes to shut and open, in seconds.
constant uint SCALES = 144;
constant float COVER = 0.94;
constant float CYCLE = 40.0;
// Where in its cycle the cone starts: open, a while before it shuts.
constant float START = 17.0;

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, how far out along its scale it is, how far
// open it is, its own tint, and 1 on the core.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float along;
    float open;
    float tint;
    float core;
};

// Cracked Clay's hash: a whole number in, a random 0 to 1 out.
float hashU(uint n) {
    n = (n << 13u) ^ n;
    n = n * (n * n * 15731u + 789221u) + 1376312589u;
    return float(n & 0x7fffffffu) / float(0x7fffffff);
}

// Flowing Color Field's palette: near-black, ember, orange, gold, white-hot.
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));
}

// The eye: once round the cone in a cycle, rising and dipping twice.
float3 coneEye(float t) {
    float a = TAU * t / CYCLE + 0.6;
    float h = 0.3 + 0.5 * sin(2.0 * TAU * t / CYCLE);
    return float3(sin(a) * 3.0, h, cos(a) * 3.0);
}

// A point in the world as the host wants it: x and y on screen (times the
// depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 coneScreen(float3 world, float t, float aspect) {
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    float3 up = cross(f, r);
    float3 v = world - eye;
    float3 view = float3(dot(v, r), dot(v, up), dot(v, f));
    float zoom = 2.05, near = 0.1, far = 20.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Ring j of a scale: how far out along it, then its half width and half
// thickness as parts of the end's: thin at the root, thick at the end.
float3 coneRing(uint j, float len, float tall) {
    if (j == 0u) return float3(0.0, 0.30, 0.10);
    if (j == 1u) return float3(len * 0.5, 0.62, 0.16);
    if (j == 2u) return float3(len - 0.9 * tall, 0.88, 0.36);
    if (j == 3u) return float3(len - 0.25 * tall, 1.0, 0.85);
    return float3(len, 0.82, 0.6);
}

// The cone's body, an egg wider below the middle. h runs from 1 at the
// tip to -1 at the base; out comes the egg's radius and height there.
float2 coneBody(float h) {
    float r = sqrt(max(1.0 - h * h, 0.0)) * 0.52 * (1.0 - 0.2 * h);
    return float2(r, 0.92 * h);
}

// How open the whole cone is at time T: it opens and shuts once a cycle.
float coneOpening(float T) {
    return 0.5 - 0.5 * cos(TAU * (T + START) / CYCLE);
}

// How far scale n has opened, 0 to 1: the ones at the base go first.
float coneOpen(uint n, float T) {
    float lag = 1.0 - float(n) / float(SCALES - 1u);
    return smoothstep(0.0, 1.0, (coneOpening(T) * 1.5 - lag) / 0.5);
}

// Everything about a scale: its root, the way it points, its across and
// thickness, its length, the size of its end, and how open it is.
struct Scale {
    float3 root;
    float3 dir;
    float3 side;
    float3 thick;
    float len;
    float wide;
    float tall;
    float open;
};

Scale coneScale(uint n, float T, constant Params& p) {
    Scale s;
    // Scale n goes n turns of the angle round, and down the egg so that
    // each has the same share of it, a little less near the tip.
    float u = (float(n) + 0.5) / float(SCALES);
    float h = 1.0 - 2.0 * COVER * pow(u, 1.25);
    float a = float(n) * p.turn * TAU / 360.0;
    float3 out = float3(cos(a), 0.0, sin(a));
    float2 b = coneBody(h);
    float3 at = out * b.x + float3(0.0, b.y, 0.0);

    // Its share of the egg sets its size: the room round it on a ball,
    // stretched as the egg stretches the ball there.
    float th = acos(h);
    float2 m = coneBody(cos(th + 0.01)) - coneBody(cos(th - 0.01));
    float room = sqrt(5.0 * TAU * COVER * pow(u, 0.25) / float(SCALES));
    s.wide = room * b.x / sqrt(1.0 - h * h) * 0.62 * p.size;
    s.tall = room * length(m) / 0.02 * 0.5 * p.size;

    // It points out of the egg and a little up, from a root deep inside.
    float2 nrm = normalize(float2(-m.y, m.x));
    s.dir = normalize(out * nrm.x + float3(0.0, nrm.y + 0.45, 0.0));
    s.side = float3(sin(a), 0.0, -cos(a));
    s.thick = cross(s.side, s.dir);
    s.len = 0.1 + 0.9 * b.x;
    s.root = at - s.dir * s.len;

    // Opening turns it about its root, round its across, until it points
    // out and a little down, and draws it out of the core.
    s.open = coneOpen(n, T);
    s.len += s.open * p.open * 0.28;
    float turn = s.open * p.open * (asin(s.dir.y) + 0.45);
    float3 d = s.dir * cos(turn) + s.thick * sin(turn);
    s.thick = s.thick * cos(turn) - s.dir * sin(turn);
    s.dir = d;
    return s;
}

// The core is a fixed 34 corners and 64 triangles; every scale is 31
// corners, six round each of five rings and the boss, and 54 triangles.
uint2 size(uint piece, constant Uniforms& u, constant Params& p) {
    if (piece == 0u) return uint2(34, 64);
    return uint2(31, 54);
}

// A core corner: four rings of eight down the axis, from the stem up, then
// its two ends.
float3 coneCore(uint i) {
    const float ys[4] = {-1.24, -0.80, 0.05, 0.66};
    const float rs[4] = {0.045, 0.13, 0.25, 0.1};
    if (i >= 32u) return float3(0.0, i == 32u ? -1.27 : 0.76, 0.0);
    int ring = int(i / 8u);
    float a = float(i % 8u) * TAU / 8.0;
    return float3(cos(a) * rs[ring], ys[ring], sin(a) * rs[ring]);
}

Vertex corner(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    Vertex o;
    float aspect = u.resolution.x / u.resolution.y;
    float T = u.time * p.speed;
    if (piece == 0u) {
        o.world = coneCore(i);
        o.position = coneScreen(o.world, u.time, aspect);
        o.along = 0.0; o.core = 1.0;
        // The core opens with the scales round it, base first; the stem
        // never does.
        float up = clamp((o.world.y + 0.8) / 1.6, 0.0, 1.0);
        o.open = smoothstep(0.0, 1.0, coneOpening(T) * 1.5 - up);
        o.open *= smoothstep(-0.8, -0.5, o.world.y) * min(p.open, 1.0);
        o.tint = 0.0;
        return o;
    }

    Scale s = coneScale(piece - 1u, T, p);
    // Corner k of ring j goes round the ring's flattened hexagon; the
    // last corner is the boss, standing out of the middle of the end.
    float out = s.len + 0.2 * s.tall;
    float3 local = float3(out, 0.0, 0.0);
    if (i < 30u) {
        float3 r = coneRing(i / 6u, s.len, s.tall);
        float a = float(i % 6u) * TAU / 6.0;
        float x = cos(a) * (0.55 + 0.45 * abs(cos(a)));
        local = float3(r.x, x * r.y * s.wide, sin(a) * 1.15 * r.z * s.tall);
    }
    o.world = s.root + s.dir * local.x + s.side * local.y
            + s.thick * local.z;
    o.position = coneScreen(o.world, u.time, aspect);
    o.along = clamp(local.x / s.len, 0.0, 1.0);
    o.open = s.open;
    o.tint = hashU(piece + 300u);
    o.core = 0.0;
    return o;
}

// The triangles: two for each face of each band between rings, then a fan
// up to the boss. The core's are bands between its rings and fans to its
// two ends.
uint3 triangle(uint piece, uint i, constant Uniforms& u, constant Params& p) {
    uint s = piece == 0u ? 8u : 6u;
    uint bands = piece == 0u ? 3u : 4u;
    if (i < 2u * s * bands) {
        uint j = i / (2u * s), q = i % (2u * s), a = q / 2u;
        uint b = (a + 1u) % s;
        uint c00 = j * s + a, c01 = j * s + b;
        uint c10 = c00 + s, c11 = c01 + s;
        return q % 2u == 0u ? uint3(c00, c01, c11) : uint3(c00, c11, c10);
    }
    uint a = (i - 2u * s * bands) % s, b = (a + 1u) % s;
    if (piece == 0u) {
        if (i - 2u * s * bands < s) return uint3(a, b, 32u);
        return uint3(24u + a, 24u + b, 33u);
    }
    uint top = bands * s;
    return uint3(top + a, top + b, top + s);
}

// Once for every pixel. Each face is flat and lit by a warm light above
// the eye. The wood is dark near the root and pale gold on the end.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 n = normalize(cross(dfdx(v.world), dfdy(v.world)));
    float3 eye = coneEye(u.time);
    float3 toEye = normalize(eye - v.world);
    if (dot(n, toEye) < 0.0) n = -n;
    float3 light = normalize(float3(eye.x + eye.z * 0.6, 4.0,
                                    eye.z - eye.x * 0.6));
    float diffuse = max(dot(n, light), 0.0);
    float lit = 0.25 + 0.75 * diffuse;

    // The core glows from inside as the scales round it open.
    float heat = p.glow * v.open;
    if (v.core > 0.5) {
        float3 c = kiln(0.1) * lit + kiln(0.42 + 0.35 * heat) * heat;
        return float4(c, 1.0);
    }

    // A scale: dark near the root, pale on its end, lit by the core where
    // it is deep and open, with a rim where it is seen edge on.
    float end = smoothstep(0.75, 1.0, v.along);
    float3 bark = kiln(0.3 + 0.08 * v.tint) * 0.5 + 0.06;
    float3 wood = mix(bark, kiln(0.5 + 0.1 * v.tint) * 0.7, end);
    lit *= 0.3 + 0.7 * smoothstep(0.2, 0.9, v.along);
    float deep = 1.0 - smoothstep(0.1, 0.85, v.along);
    float3 burn = kiln(0.42 + 0.35 * heat * deep) * heat * deep;
    float rim = pow(1.0 - max(dot(n, toEye), 0.0), 3.0);
    float3 c = wood * lit + burn + kiln(0.62) * rim * 0.35;
    return float4(c, 1.0);
}

// Behind it: the dark of the kiln, warming round the cone as it opens.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float2 q = (uv - float2(0.5, 0.5)) * float2(aspect, 1.0);
    float warm = 0.12 + 0.1 * p.glow * coneOpening(u.time * p.speed);
    return float4(kiln(0.03 + warm * exp(-length(q) * 2.4)), 1.0);
}

// The sound: each scale ticks as it opens, and more softly an octave down
// as it shuts, its note from its place on the cone, low at the base and
// high at the tip, over a warm drone that swells as the cone opens.

// Kaleidoscope's scale: D minor pentatonic over three octaves.
float scaleNote(int i) {
    const float notes[16] = {
        293.66, 349.23, 392.00, 440.00, 523.25, 587.33, 698.46, 783.99,
        880.00, 1046.50, 1174.66, 1396.91, 1567.98, 1760.00, 2093.00, 2349.32};
    return notes[clamp(i, 0, 15)];
}

// A dry tick `since` seconds after the scale moved: a sine and a partial
// 2.76 times it, as a struck piece of wood has, both gone in moments.
float coneTick(float f, float since) {
    float on = 1.0 - exp(-since * 900.0);
    float s = sin(TAU * f * since) * exp(-since * 14.0)
            + 0.5 * sin(TAU * f * 2.76 * since) * exp(-since * 30.0);
    return s * on;
}

float2 sound(float t, constant Params& p) {
    float T = t * p.speed;
    float rate = max(p.speed, 0.01);
    float3 eye = coneEye(t);
    float3 f = normalize(float3(0.0, -0.2, 0.0) - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));

    float2 out = float2(0.0);
    for (uint n = 0u; n < SCALES; n++) {
        // When this scale is half open, going and coming back: coneOpen
        // turned round, from the place it waits for.
        float lag = 1.0 - float(n) / float(SCALES - 1u);
        float g = (lag + 0.25) / 1.5;
        float when = acos(1.0 - 2.0 * g) * CYCLE / TAU;
        float opened = fract((T + START - when) / CYCLE) * CYCLE / rate;
        float shut = fract((T + START + when) / CYCLE) * CYCLE / rate;
        if (opened > 1.0 && shut > 1.0) continue;

        float note = scaleNote(int(lag * 11.0 + hashU(n + 77u) * 2.0));
        float s = 0.0;
        if (opened < 1.0) s += coneTick(note, opened);
        if (shut < 1.0) s += 0.5 * coneTick(note * 0.5, shut);
        float a = float(n) * p.turn * TAU / 360.0;
        float pan = clamp(dot(float3(cos(a), 0.0, sin(a)), r), -1.0, 1.0);
        pan = 0.5 + 0.4 * pan;
        out += s * 0.22 * float2(sqrt(1.0 - pan), sqrt(pan));
    }

    // The drone: a low D and the A above it, a little apart in each ear.
    float swell = 0.07 + 0.1 * coneOpening(T);
    float hum = sin(TAU * 73.4 * t) + 0.5 * sin(TAU * 110.0 * t);
    float hum2 = sin(TAU * 73.5 * t) + 0.5 * sin(TAU * 110.1 * t);
    out += swell * float2(hum, hum2);

    out = tanh(out * 1.2) / 1.2;
    return out * p.volume * smoothstep(0.0, 0.5, t);
}

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, 1, 137.5, 1, 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 Geometry