Shader

Jellyfish

A jellyfish swimming up through dark water, made from one flat grid. A vertex shader wraps the top of the grid into a bell and cuts the rest into strips, each narrowed to a ribbon that trails and turns; the squares left between two ribbons are discarded per pixel. A squeeze starts at the crown every three seconds and runs to the rim and down the tentacles with a wave of light, and the soft body is shaded by how much of it the eye looks through.

· Geometry · Vertex shader

Teaches A pulsing bell and trailing tentacles from one grid, light through a soft body

Builds on Cracked Clay (integer hash, sound noise), Rippling Water (the camera's axes, perspective projection), Flag in the Wind, Spinning Planet (the eye, moved by the pointer)

Watch

The idea

A vertex episode gets one grid from the host, and corner decides where each of its corners goes. Spinning Planet wrapped the whole grid round a ball. Here one grid has to become an animal with two very different parts: a closed bell, and a dozen separate tentacles hanging from it.

The top 0.4 of the grid is the bell: across the grid is the way round, and down the grid is the way from the crown to the rim. The rest of the grid hangs from the rim. It is cut into strips across, one for each tentacle, and each strip is narrowed into a ribbon. A grid can't be cut, though: the squares between one strip and the next are still there, stretched from one ribbon to its neighbor. shade throws those pixels away with discard_fragment(), and what is left is twelve ribbons.

A jellyfish swims by squeezing its bell. Every part of this one does what the part above it did a moment before, so one function of time, read late, makes the bell squeeze from the top down, the tentacles follow, and a wave of light run down the whole body. It is built in five steps.

Stage 1 · A bell from a grid

The host lays down 48 by 48 squares and calls corner for every corner, with grid running 0 to 1 across and down. bellAt wraps it into a bell: grid.x times a whole turn is the angle round, taken clockwise seen from above (which side of the grid that puts outside is this episode's New in Ray Kiln, below), and grid.y is how far along the wall the corner is, from the crown (0) to the rim (1). The wall is an arc. At a = s × wall radians along it, the corner is 0.62 sin(a) out from the middle and 0.5 cos(a) up, so the bell is 1.24 wide and half as tall above its middle.

wall is how far round the arc goes, and it is a slider in this stage. A quarter turn (1.571) would end the wall at its widest, like an umbrella. At 1.95 it goes on past that, so the rim tucks back in under the widest part, as a jellyfish's does. shade only paints twelve stripes the way round and four rings the way down, so you can see how the grid lies on the bell. The eye is a little below the bell, looking up into its opening, and stands farther back in a picture taller than wide, so the animal fits a phone held upright; project is the camera, as in Flag in the Wind.

stage-1-bell.metal
// Stage 1 · A bell from a grid
// The host lays down a grid of 48 by 48 squares and calls corner for every
// corner. Across the grid becomes the way round the bell, and down the grid
// the way from the crown to the rim. Stripes and rings show how it lies.
//
// @grid 48

// @param wall 1.95 0.8 2.6 How far round the wall turns from crown to rim, in radians

constant float TAU = 6.2831853;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);

// What each corner carries to the pixels: where it lands on screen, where it
// is in the world, and its place on the grid.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 body;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A point of the bell: `s` runs 0 at the crown to 1 at the rim, `angle` is
// the way round. The wall is an arc.
float3 bellAt(float s, float angle, float wall) {
    float a = s * wall;
    float r = WIDE * sin(a);
    float y = TALL * cos(a);
    return float3(r * cos(angle), y, r * sin(angle));
}

// Once for every corner: its place on the bell.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float3 eye = eyeAt(u);
    Vertex o;
    // Clockwise seen from above, so the grid's front is outside.
    float3 bell = bellAt(grid.y, -grid.x * TAU, p.wall);
    o.world = bell + float3(0.0, 0.2, 0.0);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.body = grid;
    return o;
}

// Once for every pixel of the grid: twelve stripes the way round and four
// rings the way down.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float stripes = 0.5 + 0.5 * cos(12.0 * v.body.x * TAU);
    float rings = 0.5 + 0.5 * cos(4.0 * v.body.y * TAU);
    return float4(kiln(0.3 + 0.2 * stripes + 0.2 * rings), 1.0);
}

Stage 2 · The squeeze

squeeze is the shape of one beat: it closes over the first quarter of the beat and opens again slowly over the rest, 0 when open and 1 when shut. A beat is 3 seconds at speed 1. bellAt takes how squeezed the bell is at each place, c, and pulls the wall in by up to 0.42 c s² of its radius, so the rim moves most and the crown not at all, and makes the bell up to 22% taller. pulse scales it.

The squeeze doesn't happen everywhere at once. bodyAt asks for squeeze(beats − 0.18 × grid.y): the rim does what the crown did 0.18 of a beat (about half a second) before. That delay is the whole trick of the motion. lean then moves the whole body: it rises 0.07 with each squeeze, and sways slowly, up to 0.22 radians one way and 0.16 the other.

For the light, each corner needs a normal. As in Flag in the Wind, corner asks bodyAt where the corner's neighbors are, 0.002 of the grid to each side across and down, and crosses the two directions. At the crown every corner of the top row is the same point and the direction across has no length, so the normal is asked for a little below it. From inside the bell the normal points away from the eye, so shade turns it round before lighting it from above.

stage-2-squeeze.metal42 new lines since stage 1
// Stage 2 · The squeeze
// Every three seconds the bell squeezes: quick to close, slow to open. The
// squeeze starts at the crown and reaches the rim a moment later. Each
// corner now has a normal, from where its two neighbors are, for the light.
//
// @grid 96

// @param speed 1.0 0.0 3.0 How fast it swims: a beat every 3 s at 1
// @param pulse 0.8 0.0 1.0 How hard the bell squeezes on each beat

constant float TAU = 6.2831853;
// Seconds a beat at speed 1.
constant float BEAT = 3.0;
// Radians the wall turns, crown to rim: past a quarter turn.
constant float WALL = 1.95;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// Beats the squeeze takes to reach the rim.
constant float RIM_LATE = 0.18;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);
// How far apart a corner's neighbors are.
constant float NEAR = 0.002;

// What a corner carries to the pixels.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float3 normal;
    float2 body;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A beat's squeeze: quick to close, slow to open; 0 open, 1 shut.
float squeeze(float beats) {
    float ph = fract(beats);
    return smoothstep(0.0, 0.25, ph) * (1.0 - smoothstep(0.25, 1.0, ph));
}

// The bell at `s` from crown to rim, squeezed by `c`.
float3 bellAt(float s, float angle, float c) {
    float a = s * WALL;
    // A squeeze pulls the wall in and makes it taller.
    float r = WIDE * sin(a) * (1.0 - 0.42 * c * s * s);
    float y = TALL * cos(a) * (1.0 + 0.22 * c * s);
    return float3(r * cos(angle), y, r * sin(angle));
}

// The body rises with each squeeze and sways.
float3 lean(float3 q, float beats) {
    float rise = 0.07 * squeeze(beats - 0.12);
    float az = 0.22 * sin(beats * 0.31 + 0.6), ax = 0.16 * sin(beats * 0.23);
    float cz = cos(az), sz = sin(az);
    q = float3(cz * q.x - sz * q.y, sz * q.x + cz * q.y, q.z);
    float cx = cos(ax), sx = sin(ax);
    q = float3(q.x, cx * q.y - sx * q.z, sx * q.y + cx * q.z);
    return q + float3(0.0, 0.2 + rise, 0.0);
}

float3 bodyAt(float2 g, float beats, constant Params& p) {
    // Clockwise seen from above, so the grid's front is outside.
    float angle = -g.x * TAU;
    // The rim does what the crown did 0.18 of a beat before.
    float c = squeeze(beats - RIM_LATE * g.y) * p.pulse;
    return lean(bellAt(g.y, angle, c), beats);
}

// Once for every corner: its place on the animal, and its normal from where
// its two neighbors are.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float beats = u.time * p.speed / BEAT;
    // At the crown every corner is the same point: ask a little below it.
    float2 g = float2(grid.x, max(grid.y, 2.0 * NEAR));
    float2 dx = float2(NEAR, 0.0), dy = float2(0.0, NEAR);
    float3 along = bodyAt(g + dx, beats, p) - bodyAt(g - dx, beats, p);
    float3 down = bodyAt(g + dy, beats, p) - bodyAt(g - dy, beats, p);
    float3 eye = eyeAt(u);
    Vertex o;
    o.world = bodyAt(grid, beats, p);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.normal = normalize(cross(down, along));
    o.body = grid;
    return o;
}

// Once for every pixel of the grid: the bell, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // Seen from inside the bell, the normal points away: turn it round.
    if (dot(n, toEye) < 0.0) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));
    float lit = max(dot(n, sun), 0.0);
    float stripes = 0.5 + 0.5 * cos(12.0 * v.body.x * TAU);
    float3 flesh = kiln(0.45 + 0.1 * stripes * v.body.y);
    return float4(flesh * (0.4 + 0.9 * lit), 1.0);
}
8 lines from stage 1 that are gone
float3 bellAt(float s, float angle, float wall) {
    float a = s * wall;
    float r = WIDE * sin(a);
    float y = TALL * cos(a);
⋯
    float3 bell = bellAt(grid.y, -grid.x * TAU, p.wall);
    o.world = bell + float3(0.0, 0.2, 0.0);
⋯
    float rings = 0.5 + 0.5 * cos(4.0 * v.body.y * TAU);
    return float4(kiln(0.3 + 0.2 * stripes + 0.2 * rings), 1.0);

Stage 3 · Tentacles

Now only the top 0.4 of the grid (BELL) is the bell, and the grid is finer, 192 squares a side. Below the rim, bodyAt works out two numbers: v, how far down its tentacle a corner is (0 at the rim, 1 at the tip), and l, where it is across its strip (−0.5 to 0.5). The strip is band, floor(grid.x × arms): a whole number, the same for every corner of one tentacle.

Each tentacle has a middle line. It hangs from the middle of its strip's stretch of the rim, a notch in the scalloped edge bellAt now makes, and goes down by trail times v (times 0.65 to 1, a random share for each tentacle from hashU). It stays as far out as the rim was when the squeeze passed that place, lateAt giving the delay: 0.18 of a beat at the rim, and 0.7 more at the tips. Two sine waves, each with its own speed and a different start for each tentacle, make the line trail and wave. The ribbon lies across that line, l times its width (0.064 under the rim, 0.014 at the tip), and the direction across turns twist times round on the way down, so a ribbon shows one side and then the other. Over the first tenth of the way down (SKIRT) the strip blends from the rim itself into the ribbon, which makes the skirt under the rim.

That leaves the squares between two ribbons. A corner carries its band to the pixels, and inside a ribbon every corner has the same one, so the pixels get a whole number. In a square that joins two ribbons, one side's corners have one band and the other side's the next, and the pixels get something in between. shade measures how far the band is from a whole number, and where that is more than 0.0005 and the pixel is below an arch under the rim, it calls discard_fragment(): the pixel is not drawn, and neither is its depth. The arch (2% of the way down in the middle of a gap, 9% beside a ribbon) leaves the skirt's scallops.

stage-3-tentacles.metal53 new lines since stage 2
// Stage 3 · Tentacles
// The top 0.4 of the grid is still the bell. The rest hangs from the rim,
// cut into one strip for each tentacle; each strip narrows to a ribbon that
// trails and turns. The squares left between two ribbons are thrown away.
//
// @grid 192

// @param speed 1.0 0.0 3.0 How fast it swims: a beat every 3 s at 1
// @param pulse 0.8 0.0 1.0 How hard the bell squeezes on each beat
// @param arms 12 4 20 How many tentacles hang from the rim
// @param trail 1.5 0.5 2.4 How long the tentacles are
// @param twist 1.2 0.0 4.0 Turns each ribbon makes along its length

constant float TAU = 6.2831853;
// Seconds a beat at speed 1.
constant float BEAT = 3.0;
// The share of the grid, from the top, that is the bell; the rest is tentacles.
constant float BELL = 0.4;
// Radians the wall turns, crown to rim: past a quarter turn.
constant float WALL = 1.95;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// Beats the squeeze takes to reach the rim, and then the tips.
constant float RIM_LATE = 0.18;
constant float TIP_LATE = 0.7;
// The share of a tentacle's length over which the skirt narrows to a ribbon.
constant float SKIRT = 0.1;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);
// How far apart a corner's neighbors are.
constant float NEAR = 0.002;

// What a corner carries to the pixels, now with its tentacle's strip.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float3 normal;
    float2 body;
    float band;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A beat's squeeze: quick to close, slow to open; 0 open, 1 shut.
float squeeze(float beats) {
    float ph = fract(beats);
    return smoothstep(0.0, 0.25, ph) * (1.0 - smoothstep(0.25, 1.0, ph));
}

// How late, in beats, the squeeze reaches a place down the grid: the rim
// 0.18 of a beat after the crown, a tentacle's tip 0.7 after the rim.
float lateAt(float down) {
    if (down < BELL) return RIM_LATE * down / BELL;
    return RIM_LATE + TIP_LATE * (down - BELL) / (1.0 - BELL);
}

// The bell at `s` from crown to rim, squeezed by `c`.
float3 bellAt(float s, float angle, float c, float arms) {
    float a = s * WALL;
    float r = WIDE * sin(a) * (1.0 - 0.42 * c * s * s);
    // The rim is scalloped: a notch where each tentacle hangs.
    r *= 1.0 + 0.05 * pow(s, 6.0) * cos(arms * angle);
    float y = TALL * cos(a) * (1.0 + 0.22 * c * s);
    return float3(r * cos(angle), y, r * sin(angle));
}

// The body rises with each squeeze and sways.
float3 lean(float3 q, float beats) {
    float rise = 0.07 * squeeze(beats - 0.12);
    float az = 0.22 * sin(beats * 0.31 + 0.6), ax = 0.16 * sin(beats * 0.23);
    float cz = cos(az), sz = sin(az);
    q = float3(cz * q.x - sz * q.y, sz * q.x + cz * q.y, q.z);
    float cx = cos(ax), sx = sin(ax);
    q = float3(q.x, cx * q.y - sx * q.z, sx * q.y + cx * q.z);
    return q + float3(0.0, 0.2 + rise, 0.0);
}

// A point of the animal: `band` is its tentacle's strip of the grid.
float3 bodyAt(float2 g, float band, float beats, constant Params& p) {
    float arms = floor(p.arms);
    // Clockwise seen from above, so the grid's front is outside.
    float angle = -g.x * TAU;
    float c = squeeze(beats - lateAt(g.y)) * p.pulse;
    if (g.y <= BELL) return lean(bellAt(g.y / BELL, angle, c, arms), beats);
    // Below the rim: how far down, and where across the strip.
    float v = (g.y - BELL) / (1.0 - BELL);
    float l = g.x * arms - band - 0.5;
    float rimC = squeeze(beats - RIM_LATE) * p.pulse;
    float3 rim = bellAt(1.0, angle, rimC, arms);
    // The middle line hangs from its notch and waves as it trails.
    float mid = -(band + 0.5) / arms * TAU;
    uint id = uint(band) * 7u + 3u;
    float len = p.trail * (0.65 + 0.35 * hashU(id));
    float out = WIDE * sin(WALL) * (1.0 - 0.42 * c) * (1.0 - 0.3 * v);
    out += 0.11 * v * sin(TAU * (1.4 * v - beats) + 2.4 * band);
    float side = 0.09 * v * sin(TAU * (1.1 * v - 0.7 * beats) + 4.1 * band);
    float3 away = float3(cos(mid), 0.0, sin(mid));
    float3 round = float3(sin(mid), 0.0, -cos(mid));
    float3 line = away * out + round * side;
    line.y = rim.y - len * v * (1.0 - 0.1 * c);
    // The ribbon lies across that line, turning as it goes down.
    float turn = TAU * p.twist * v + 1.7 * band;
    float3 across = cos(turn) * round + sin(turn) * away;
    float wide = 0.014 + 0.05 * (1.0 - v);
    float3 ribbon = line + across * l * wide;
    // Just under the rim the strip is still the bell's skirt.
    float3 q = mix(rim, ribbon, smoothstep(0.0, SKIRT, v));
    return lean(q, beats);
}

// Once for every corner: its place on the animal, and its normal from where
// its two neighbors are.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float beats = u.time * p.speed / BEAT;
    // The strip of the grid this corner is in: one for each tentacle.
    float band = floor(grid.x * floor(p.arms));
    // At the crown every corner is the same point: ask a little below it.
    float2 g = float2(grid.x, max(grid.y, 2.0 * NEAR));
    float2 dx = float2(NEAR, 0.0), dy = float2(0.0, NEAR);
    float3 along = bodyAt(g + dx, band, beats, p);
    along -= bodyAt(g - dx, band, beats, p);
    float3 down = bodyAt(g + dy, band, beats, p);
    down -= bodyAt(g - dy, band, beats, p);
    float3 eye = eyeAt(u);
    Vertex o;
    o.world = bodyAt(grid, band, beats, p);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.normal = normalize(cross(down, along));
    o.body = grid;
    o.band = band;
    return o;
}

// Once for every pixel of the grid: the bell and the ribbons, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float tv = (v.body.y - BELL) / (1.0 - BELL);
    // Between two tentacles the band is part-way from one whole number to
    // the next: throw those pixels away, in an arch under the rim.
    float between = abs(v.band - floor(v.band + 0.5));
    float arch = SKIRT * (0.9 - 0.7 * 4.0 * between * (1.0 - between));
    if (between > 0.0005 && tv > arch) discard_fragment();

    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // Seen from inside the bell, the normal points away: turn it round.
    if (dot(n, toEye) < 0.0) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));
    float lit = max(dot(n, sun), 0.0);
    float stripes = 0.5 + 0.5 * cos(floor(p.arms) * v.body.x * TAU);
    float s = clamp(v.body.y / BELL, 0.0, 1.0);
    float3 flesh = kiln(0.45 + 0.1 * stripes * s);
    return float4(flesh * (0.4 + 0.9 * lit), 1.0);
}
9 lines from stage 2 that are gone
float3 bellAt(float s, float angle, float c) {
⋯
float3 bodyAt(float2 g, float beats, constant Params& p) {
⋯
    float c = squeeze(beats - RIM_LATE * g.y) * p.pulse;
    return lean(bellAt(g.y, angle, c), beats);
⋯
    float3 along = bodyAt(g + dx, beats, p) - bodyAt(g - dx, beats, p);
    float3 down = bodyAt(g + dy, beats, p) - bodyAt(g - dy, beats, p);
⋯
    o.world = bodyAt(grid, beats, p);
⋯
    float stripes = 0.5 + 0.5 * cos(12.0 * v.body.x * TAU);
    float3 flesh = kiln(0.45 + 0.1 * stripes * v.body.y);

Stage 4 · A soft body

A jellyfish is mostly water, and it has no hard surface for light to bounce off. A vertex episode draws without blending, so nothing is really see-through here; shade fakes it from three things.

  • How much of it the eye looks through. edge is 1 − |n · toEye|: 0 where the surface faces the eye and 1 where the eye looks along it. Looking along a thin wall, the eye passes through much more of it, so the body is given more of its own color there (thick), and a gold rim is added by edge cubed. The wall is also thicker at the crown than at the rim.
  • Light through it. The light comes from above. lit is the usual light on the side that faces it, and through is the same for the side that faces away, which a hard surface would leave dark. Flag in the Wind did the same for cloth.
  • The water behind. abyss is the color of the sea in a direction: near-black below, deep blue above, with a soft light coming down. Where the body is thin, shade mixes toward the water in the direction the eye is looking, and background draws the same water behind everything, so the thin parts seem to let it show.

The inside of the bell, which you see through its opening, is a little darker than the outside.

stage-4-soft-body.metal33 new lines since stage 3
// Stage 4 · A soft body
// A jellyfish has no hard surface to light. It shows most where the eye
// looks along it, the light from above comes through it, and where it is
// thin the water behind shows. Behind it all is the water itself.
//
// @grid 192

// @param speed 1.0 0.0 3.0 How fast it swims: a beat every 3 s at 1
// @param pulse 0.8 0.0 1.0 How hard the bell squeezes on each beat
// @param arms 12 4 20 How many tentacles hang from the rim
// @param trail 1.5 0.5 2.4 How long the tentacles are
// @param twist 1.2 0.0 4.0 Turns each ribbon makes along its length

constant float TAU = 6.2831853;
// Seconds a beat at speed 1.
constant float BEAT = 3.0;
// The share of the grid, from the top, that is the bell; the rest is tentacles.
constant float BELL = 0.4;
// Radians the wall turns, crown to rim: past a quarter turn.
constant float WALL = 1.95;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// Beats the squeeze takes to reach the rim, and then the tips.
constant float RIM_LATE = 0.18;
constant float TIP_LATE = 0.7;
// The share of a tentacle's length over which the skirt narrows to a ribbon.
constant float SKIRT = 0.1;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);
// How far apart a corner's neighbors are.
constant float NEAR = 0.002;

// What a corner carries to the pixels, now with its tentacle's strip.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float3 normal;
    float2 body;
    float band;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A beat's squeeze: quick to close, slow to open; 0 open, 1 shut.
float squeeze(float beats) {
    float ph = fract(beats);
    return smoothstep(0.0, 0.25, ph) * (1.0 - smoothstep(0.25, 1.0, ph));
}

// How late, in beats, the squeeze reaches a place down the grid: the rim
// 0.18 of a beat after the crown, a tentacle's tip 0.7 after the rim.
float lateAt(float down) {
    if (down < BELL) return RIM_LATE * down / BELL;
    return RIM_LATE + TIP_LATE * (down - BELL) / (1.0 - BELL);
}

// The bell at `s` from crown to rim, squeezed by `c`.
float3 bellAt(float s, float angle, float c, float arms) {
    float a = s * WALL;
    float r = WIDE * sin(a) * (1.0 - 0.42 * c * s * s);
    // The rim is scalloped: a notch where each tentacle hangs.
    r *= 1.0 + 0.05 * pow(s, 6.0) * cos(arms * angle);
    float y = TALL * cos(a) * (1.0 + 0.22 * c * s);
    return float3(r * cos(angle), y, r * sin(angle));
}

// The body rises with each squeeze and sways.
float3 lean(float3 q, float beats) {
    float rise = 0.07 * squeeze(beats - 0.12);
    float az = 0.22 * sin(beats * 0.31 + 0.6), ax = 0.16 * sin(beats * 0.23);
    float cz = cos(az), sz = sin(az);
    q = float3(cz * q.x - sz * q.y, sz * q.x + cz * q.y, q.z);
    float cx = cos(ax), sx = sin(ax);
    q = float3(q.x, cx * q.y - sx * q.z, sx * q.y + cx * q.z);
    return q + float3(0.0, 0.2 + rise, 0.0);
}

// A point of the animal: `band` is its tentacle's strip of the grid.
float3 bodyAt(float2 g, float band, float beats, constant Params& p) {
    float arms = floor(p.arms);
    // Clockwise seen from above, so the grid's front is outside.
    float angle = -g.x * TAU;
    float c = squeeze(beats - lateAt(g.y)) * p.pulse;
    if (g.y <= BELL) return lean(bellAt(g.y / BELL, angle, c, arms), beats);
    // Below the rim: how far down, and where across the strip.
    float v = (g.y - BELL) / (1.0 - BELL);
    float l = g.x * arms - band - 0.5;
    float rimC = squeeze(beats - RIM_LATE) * p.pulse;
    float3 rim = bellAt(1.0, angle, rimC, arms);
    // The middle line hangs from its notch and waves as it trails.
    float mid = -(band + 0.5) / arms * TAU;
    uint id = uint(band) * 7u + 3u;
    float len = p.trail * (0.65 + 0.35 * hashU(id));
    float out = WIDE * sin(WALL) * (1.0 - 0.42 * c) * (1.0 - 0.3 * v);
    out += 0.11 * v * sin(TAU * (1.4 * v - beats) + 2.4 * band);
    float side = 0.09 * v * sin(TAU * (1.1 * v - 0.7 * beats) + 4.1 * band);
    float3 away = float3(cos(mid), 0.0, sin(mid));
    float3 round = float3(sin(mid), 0.0, -cos(mid));
    float3 line = away * out + round * side;
    line.y = rim.y - len * v * (1.0 - 0.1 * c);
    // The ribbon lies across that line, turning as it goes down.
    float turn = TAU * p.twist * v + 1.7 * band;
    float3 across = cos(turn) * round + sin(turn) * away;
    float wide = 0.014 + 0.05 * (1.0 - v);
    float3 ribbon = line + across * l * wide;
    // Just under the rim the strip is still the bell's skirt.
    float3 q = mix(rim, ribbon, smoothstep(0.0, SKIRT, v));
    return lean(q, beats);
}

// Once for every corner: its place on the animal, and its normal from where
// its two neighbors are.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float beats = u.time * p.speed / BEAT;
    // The strip of the grid this corner is in: one for each tentacle.
    float band = floor(grid.x * floor(p.arms));
    // At the crown every corner is the same point: ask a little below it.
    float2 g = float2(grid.x, max(grid.y, 2.0 * NEAR));
    float2 dx = float2(NEAR, 0.0), dy = float2(0.0, NEAR);
    float3 along = bodyAt(g + dx, band, beats, p);
    along -= bodyAt(g - dx, band, beats, p);
    float3 down = bodyAt(g + dy, band, beats, p);
    down -= bodyAt(g - dy, band, beats, p);
    float3 eye = eyeAt(u);
    Vertex o;
    o.world = bodyAt(grid, band, beats, p);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.normal = normalize(cross(down, along));
    o.body = grid;
    o.band = band;
    return o;
}

// The water in a direction from the eye: near-black below, a deep blue
// above, and a soft light coming down from the surface.
float3 abyss(float3 d) {
    float up = smoothstep(-0.6, 0.9, d.y);
    float3 deep = float3(0.006, 0.008, 0.014);
    float3 col = mix(deep, float3(0.02, 0.055, 0.1), up);
    float3 sun = normalize(float3(0.25, 1.0, 0.1));
    float shaft = pow(max(dot(d, sun), 0.0), 6.0);
    return col + float3(0.05, 0.1, 0.13) * shaft;
}

// Once for every pixel of the grid: the soft body, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float tv = (v.body.y - BELL) / (1.0 - BELL);
    // Between two tentacles the band is part-way from one whole number to
    // the next: throw those pixels away, in an arch under the rim.
    float between = abs(v.band - floor(v.band + 0.5));
    float arch = SKIRT * (0.9 - 0.7 * 4.0 * between * (1.0 - between));
    if (between > 0.0005 && tv > arch) discard_fragment();

    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // The side that faces the eye: the outside, or the inside of the bell.
    float inside = dot(n, toEye) < 0.0 ? 1.0 : 0.0;
    if (inside > 0.5) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));

    // A soft body shows most where the eye looks along its surface.
    float edge = 1.0 - abs(dot(n, toEye));
    float thick = 0.35 + 0.65 * pow(edge, 1.6);
    // Thicker at the crown, thin at the rim; a ribbon is seen flat on.
    float s = clamp(v.body.y / BELL, 0.0, 1.0);
    thick *= mix(1.0, 0.55, s);
    if (tv > 0.0) thick = 0.7 + 0.3 * edge;

    // Its own color, the inside of the bell darker.
    float heat = 0.5;
    if (tv > 0.0) heat = 0.6 - 0.16 * tv;
    heat -= 0.12 * inside;
    float3 flesh = kiln(heat);

    // The light from above: on the surface, and through it from behind.
    float lit = max(dot(n, sun), 0.0);
    float through = max(-dot(n, sun), 0.0);
    float3 col = flesh * (0.5 + 0.9 * lit + 0.7 * through);
    col += kiln(0.72) * pow(edge, 3.0) * 0.4;
    // Where the body is thin, the water behind shows through it.
    float3 behind = abyss(-toEye);
    col = mix(behind, col + behind * 0.6, clamp(thick, 0.0, 1.0));
    return float4(col, 1.0);
}

// What is behind the animal: the water, in the direction of each pixel.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3 eye = eyeAt(u);
    float3x3 axes = cameraAxes(eye);
    float2 sc = (uv * 2.0 - 1.0) / ZOOM;
    float3 d = normalize(axes[2] + axes[0] * sc.x * aspect + axes[1] * sc.y);
    return float4(abyss(d), 1.0);
}
5 lines from stage 3 that are gone
    if (dot(n, toEye) < 0.0) n = -n;
⋯
    float lit = max(dot(n, sun), 0.0);
    float stripes = 0.5 + 0.5 * cos(floor(p.arms) * v.body.x * TAU);
⋯
    float3 flesh = kiln(0.45 + 0.1 * stripes * s);
    return float4(flesh * (0.4 + 0.9 * lit), 1.0);

Stage 5 · Jellyfish

The last step makes it alive.

  • The wave of light. lightWave is brightest where the squeeze is closing: a Gaussian bump 0.12 of a beat after the squeeze reaches a place, read with the same lateAt as the motion. So light leaves the crown on the beat, reaches the rim half a second later, and runs to the tips of the tentacles over the next two seconds. glow sets how bright it is.
  • Markings. Canals run from the crown to the rim, one for each tentacle, and a ring of four warm lobes sits near the top of the bell.
  • Snow. background adds specks sinking past, in three layers of cells laid over the direction of each pixel, the near layer larger and faster. They sink a little faster just after each squeeze, as the body surges up.
  • The pointer moves the eye round the animal, up to 1.2 radians left and right and 0.6 up and down; with no pointer it looks from the front, 0.3 radians below.
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 of the grid: the soft body, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float tv = (v.body.y - BELL) / (1.0 - BELL);
    // Between two tentacles the band is part-way from one whole number to
    // the next: throw those pixels away, in an arch under the rim.
    float between = abs(v.band - floor(v.band + 0.5));
    float arch = SKIRT * (0.9 - 0.7 * 4.0 * between * (1.0 - between));
    if (between > 0.0005 && tv > arch) discard_fragment();

    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // The side that faces the eye: the outside, or the inside of the bell.
    float inside = dot(n, toEye) < 0.0 ? 1.0 : 0.0;
    if (inside > 0.5) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));

    // A soft body shows most where the eye looks along its surface.
    float edge = 1.0 - abs(dot(n, toEye));
    float thick = 0.35 + 0.65 * pow(edge, 1.6);
    // Thicker at the crown, thin at the rim; a ribbon is seen flat on.
    float s = clamp(v.body.y / BELL, 0.0, 1.0);
    thick *= mix(1.0, 0.55, s);
    if (tv > 0.0) thick = 0.7 + 0.3 * edge;

    // Canals from crown to rim, and a ring of four warm lobes.
    float arms = floor(p.arms);
    float heat = 0.5 + 0.1 * cos(arms * v.body.x * TAU) * s;
    float lobes = 0.5 + 0.5 * cos(4.0 * v.body.x * TAU);
    float ring = smoothstep(0.2, 0.34, s) * (1.0 - smoothstep(0.4, 0.56, s));
    heat += 0.22 * lobes * ring;
    if (tv > 0.0) heat = 0.6 - 0.16 * tv;
    heat -= 0.12 * inside;
    float3 flesh = kiln(heat);

    // The light from above: on the surface, and through it from behind.
    float lit = max(dot(n, sun), 0.0);
    float through = max(-dot(n, sun), 0.0);
    float3 col = flesh * (0.5 + 0.9 * lit + 0.7 * through);
    col += kiln(0.72) * pow(edge, 3.0) * 0.4;
    // The light runs down the body with the squeeze.
    float beats = u.time * p.speed / BEAT;
    float wave = lightWave(v.body.y, beats) * p.glow;
    col += kiln(0.78 + 0.15 * wave) * wave * (0.5 + 0.9 * edge);
    // Where the body is thin, the water behind shows through it.
    float3 behind = abyss(-toEye);
    col = mix(behind, col + behind * 0.6, clamp(thick, 0.0, 1.0));
    return float4(col, 1.0);
}
jellyfish.metal, the whole file88 new lines since stage 4
// Ray Kiln · Jellyfish
//
// @episode Jellyfish
// @length 60
// @variation 30
// @variations 1,2,3,4,6,9,10,11
// @still 13.0
// @teaches A pulsing bell and trailing tentacles from one grid, light through a soft body
// @category Vertex
// @tags vertex shaders, grids, displacement, normals, finite differences, two-sided lighting, translucency, discard, procedural sound
// @final Jellyfish | A wave of light runs from the crown to the tips with every beat, the body sways, and snow drifts down past it.
//
// A jellyfish swimming up through dark water. The host lays down one grid of squares, and `corner` makes the whole
// animal from it: the top of the grid is wrapped into a bell, across becoming the way round and down the way from
// the crown to the rim, and the rest of it hangs from the rim as tentacles, each a narrow ribbon cut from its own
// strip of the grid. The squares left between two ribbons are thrown away in `shade`. Every three seconds a squeeze
// starts at the crown and runs to the rim and on down the tentacles, each part doing what the part above it did a
// moment before. A soft body has no hard surface to light, so `shade` shows more of it where the eye looks along it,
// lets the light from above through it, and mixes in the water behind where it is thin.
// 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
//
// @grid 192

// @param speed 1.0 0.0 3.0 How fast it swims: a beat every 3 s at 1
// @param pulse 0.8 0.0 1.0 How hard the bell squeezes on each beat
// @param arms 12 4 20 How many tentacles hang from the rim
// @param trail 1.5 0.5 2.4 How long the tentacles are
// @param twist 1.2 0.0 4.0 Turns each ribbon makes along its length
// @param glow 0.7 0.0 1.0 How bright the wave of light is that runs down the body
// @param volume 0.8 0.0 1.0 Loudness of the sound

constant float TAU = 6.2831853;
// Seconds a beat at speed 1.
constant float BEAT = 3.0;
// The share of the grid, from the top, that is the bell; the rest is tentacles.
constant float BELL = 0.4;
// Radians the wall turns, crown to rim: past a quarter turn.
constant float WALL = 1.95;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// Beats the squeeze takes to reach the rim, and then the tips.
constant float RIM_LATE = 0.18;
constant float TIP_LATE = 0.7;
// The share of a tentacle's length over which the skirt narrows to a ribbon.
constant float SKIRT = 0.1;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);
// How far apart a corner's neighbors are.
constant float NEAR = 0.002;

// What a corner carries to the pixels, now with its tentacle's strip.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float3 normal;
    float2 body;
    float band;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A beat's squeeze: quick to close, slow to open; 0 open, 1 shut.
float squeeze(float beats) {
    float ph = fract(beats);
    return smoothstep(0.0, 0.25, ph) * (1.0 - smoothstep(0.25, 1.0, ph));
}

// How late, in beats, the squeeze reaches a place down the grid: the rim
// 0.18 of a beat after the crown, a tentacle's tip 0.7 after the rim.
float lateAt(float down) {
    if (down < BELL) return RIM_LATE * down / BELL;
    return RIM_LATE + TIP_LATE * (down - BELL) / (1.0 - BELL);
}

// The bell at `s` from crown to rim, squeezed by `c`.
float3 bellAt(float s, float angle, float c, float arms) {
    float a = s * WALL;
    float r = WIDE * sin(a) * (1.0 - 0.42 * c * s * s);
    // The rim is scalloped: a notch where each tentacle hangs.
    r *= 1.0 + 0.05 * pow(s, 6.0) * cos(arms * angle);
    float y = TALL * cos(a) * (1.0 + 0.22 * c * s);
    return float3(r * cos(angle), y, r * sin(angle));
}

// The body rises with each squeeze and sways.
float3 lean(float3 q, float beats) {
    float rise = 0.07 * squeeze(beats - 0.12);
    float az = 0.22 * sin(beats * 0.31 + 0.6), ax = 0.16 * sin(beats * 0.23);
    float cz = cos(az), sz = sin(az);
    q = float3(cz * q.x - sz * q.y, sz * q.x + cz * q.y, q.z);
    float cx = cos(ax), sx = sin(ax);
    q = float3(q.x, cx * q.y - sx * q.z, sx * q.y + cx * q.z);
    return q + float3(0.0, 0.2 + rise, 0.0);
}

// A point of the animal: `band` is its tentacle's strip of the grid.
float3 bodyAt(float2 g, float band, float beats, constant Params& p) {
    float arms = floor(p.arms);
    // Clockwise seen from above, so the grid's front is outside.
    float angle = -g.x * TAU;
    float c = squeeze(beats - lateAt(g.y)) * p.pulse;
    if (g.y <= BELL) return lean(bellAt(g.y / BELL, angle, c, arms), beats);
    // Below the rim: how far down, and where across the strip.
    float v = (g.y - BELL) / (1.0 - BELL);
    float l = g.x * arms - band - 0.5;
    float rimC = squeeze(beats - RIM_LATE) * p.pulse;
    float3 rim = bellAt(1.0, angle, rimC, arms);
    // The middle line hangs from its notch and waves as it trails.
    float mid = -(band + 0.5) / arms * TAU;
    uint id = uint(band) * 7u + 3u;
    float len = p.trail * (0.65 + 0.35 * hashU(id));
    float out = WIDE * sin(WALL) * (1.0 - 0.42 * c) * (1.0 - 0.3 * v);
    out += 0.11 * v * sin(TAU * (1.4 * v - beats) + 2.4 * band);
    float side = 0.09 * v * sin(TAU * (1.1 * v - 0.7 * beats) + 4.1 * band);
    float3 away = float3(cos(mid), 0.0, sin(mid));
    float3 round = float3(sin(mid), 0.0, -cos(mid));
    float3 line = away * out + round * side;
    line.y = rim.y - len * v * (1.0 - 0.1 * c);
    // The ribbon lies across that line, turning as it goes down.
    float turn = TAU * p.twist * v + 1.7 * band;
    float3 across = cos(turn) * round + sin(turn) * away;
    float wide = 0.014 + 0.05 * (1.0 - v);
    float3 ribbon = line + across * l * wide;
    // Just under the rim the strip is still the bell's skirt.
    float3 q = mix(rim, ribbon, smoothstep(0.0, SKIRT, v));
    return lean(q, beats);
}

// Once for every corner: its place on the animal, and its normal from where
// its two neighbors are.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float beats = u.time * p.speed / BEAT;
    // The strip of the grid this corner is in: one for each tentacle.
    float band = floor(grid.x * floor(p.arms));
    // At the crown every corner is the same point: ask a little below it.
    float2 g = float2(grid.x, max(grid.y, 2.0 * NEAR));
    float2 dx = float2(NEAR, 0.0), dy = float2(0.0, NEAR);
    float3 along = bodyAt(g + dx, band, beats, p);
    along -= bodyAt(g - dx, band, beats, p);
    float3 down = bodyAt(g + dy, band, beats, p);
    down -= bodyAt(g - dy, band, beats, p);
    float3 eye = eyeAt(u);
    Vertex o;
    o.world = bodyAt(grid, band, beats, p);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.normal = normalize(cross(down, along));
    o.body = grid;
    o.band = band;
    return o;
}

// The water in a direction from the eye: near-black below, a deep blue
// above, and a soft light coming down from the surface.
float3 abyss(float3 d) {
    float up = smoothstep(-0.6, 0.9, d.y);
    float3 deep = float3(0.006, 0.008, 0.014);
    float3 col = mix(deep, float3(0.02, 0.055, 0.1), up);
    float3 sun = normalize(float3(0.25, 1.0, 0.1));
    float shaft = pow(max(dot(d, sun), 0.0), 6.0);
    return col + float3(0.05, 0.1, 0.13) * shaft;
}

// The wave of light: brightest where the squeeze is closing.
float lightWave(float down, float beats) {
    float ph = fract(beats - lateAt(down));
    float x = (ph - 0.12) / 0.09;
    return exp(-x * x);
}

// Once for every pixel of the grid: the soft body, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float tv = (v.body.y - BELL) / (1.0 - BELL);
    // Between two tentacles the band is part-way from one whole number to
    // the next: throw those pixels away, in an arch under the rim.
    float between = abs(v.band - floor(v.band + 0.5));
    float arch = SKIRT * (0.9 - 0.7 * 4.0 * between * (1.0 - between));
    if (between > 0.0005 && tv > arch) discard_fragment();

    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // The side that faces the eye: the outside, or the inside of the bell.
    float inside = dot(n, toEye) < 0.0 ? 1.0 : 0.0;
    if (inside > 0.5) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));

    // A soft body shows most where the eye looks along its surface.
    float edge = 1.0 - abs(dot(n, toEye));
    float thick = 0.35 + 0.65 * pow(edge, 1.6);
    // Thicker at the crown, thin at the rim; a ribbon is seen flat on.
    float s = clamp(v.body.y / BELL, 0.0, 1.0);
    thick *= mix(1.0, 0.55, s);
    if (tv > 0.0) thick = 0.7 + 0.3 * edge;

    // Canals from crown to rim, and a ring of four warm lobes.
    float arms = floor(p.arms);
    float heat = 0.5 + 0.1 * cos(arms * v.body.x * TAU) * s;
    float lobes = 0.5 + 0.5 * cos(4.0 * v.body.x * TAU);
    float ring = smoothstep(0.2, 0.34, s) * (1.0 - smoothstep(0.4, 0.56, s));
    heat += 0.22 * lobes * ring;
    if (tv > 0.0) heat = 0.6 - 0.16 * tv;
    heat -= 0.12 * inside;
    float3 flesh = kiln(heat);

    // The light from above: on the surface, and through it from behind.
    float lit = max(dot(n, sun), 0.0);
    float through = max(-dot(n, sun), 0.0);
    float3 col = flesh * (0.5 + 0.9 * lit + 0.7 * through);
    col += kiln(0.72) * pow(edge, 3.0) * 0.4;
    // The light runs down the body with the squeeze.
    float beats = u.time * p.speed / BEAT;
    float wave = lightWave(v.body.y, beats) * p.glow;
    col += kiln(0.78 + 0.15 * wave) * wave * (0.5 + 0.9 * edge);
    // Where the body is thin, the water behind shows through it.
    float3 behind = abyss(-toEye);
    col = mix(behind, col + behind * 0.6, clamp(thick, 0.0, 1.0));
    return float4(col, 1.0);
}

// A random 0 to 1 for a cell of the snow.
float snowHash(float2 cell, uint salt) {
    uint x = uint(int(cell.x) + 4096), y = uint(int(cell.y) + 4096);
    return hashU(x * 7349u + y * 911u + salt);
}

// What is behind the animal: the water, and specks of snow sinking past
// as it swims up, the near ones larger and faster.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3 eye = eyeAt(u);
    float3x3 axes = cameraAxes(eye);
    float2 sc = (uv * 2.0 - 1.0) / ZOOM;
    float3 d = normalize(axes[2] + axes[0] * sc.x * aspect + axes[1] * sc.y);
    float3 col = abyss(d);
    float beats = u.time * p.speed / BEAT;
    // The snow sinks faster just after each squeeze, as the body surges.
    float sunk = beats * 0.5 + 0.1 * squeeze(beats - 0.2);
    float2 q = float2(atan2(d.x, d.z), d.y);
    for (int k = 0; k < 3; k++) {
        float size = 9.0 + 7.0 * float(k);
        float fall = sunk * (2.6 - 0.7 * float(k));
        float2 at = q * size + float2(float(k) * 3.7, fall);
        float2 cell = floor(at);
        uint salt = uint(k) * 13u;
        float2 spot = float2(snowHash(cell, salt), snowHash(cell, salt + 5u));
        float far = length(at - cell - 0.2 - 0.6 * spot);
        float bright = snowHash(cell, salt + 9u);
        float speck = smoothstep(0.09 - 0.02 * float(k), 0.0, far);
        col += float3(0.3, 0.42, 0.5) * speck * bright * bright * 0.5;
    }
    return float4(col, 1.0);
}

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

// Rippling Water's noise from joined random values, `rate` times a second.
float noise1(float t, float rate, uint salt) {
    float x = t * rate;
    float i = floor(x);
    float f = x - i;
    f = f * f * (3.0 - 2.0 * f);
    uint n = uint(int(i) + 1000000);
    return mix(hashU(n * 31u + salt), hashU((n + 1u) * 31u + salt), f);
}

// One of five notes of a D minor pentatonic scale, as a frequency.
float noteOf(uint n) {
    uint k = n % 5u;
    float semis = k == 0u ? 0.0 : k == 1u ? 3.0 : k == 2u ? 5.0 : 7.0;
    if (k == 4u) semis = 10.0;
    return 293.66 * pow(2.0, semis / 12.0);
}

// The sound: deep water, a wash with every squeeze, a soft note on each beat,
// and six pings as the wave of light runs down the tentacles.
float2 sound(float t, constant Params& p) {
    float beats = t * p.speed / BEAT;
    float n = floor(beats);
    float ph = beats - n;
    float since = ph * BEAT / max(p.speed, 0.05);
    // The deep: D1 and A1, a few cents apart, slowly beating.
    float deep = sin(TAU * 36.71 * t) + 0.7 * sin(TAU * 55.2 * t);
    deep += 0.5 * sin(TAU * 73.6 * t + 0.6 * sin(TAU * 0.11 * t));
    float2 v = float2(deep) * 0.11;
    // The wash: the water the bell pushes out, loudest as it closes.
    float push = squeeze(beats) * p.pulse;
    float dark = noise1(t, 300.0, 3u) - 0.5;
    dark += 0.7 * (noise1(t, 110.0, 7u) - 0.5);
    float bright = noise1(t, 1400.0, 11u) - 0.5;
    float wash = dark + 0.5 * push * bright;
    v += float2(1.0, 0.9) * wash * (0.05 + 0.5 * push * push);
    // The beat's note: it swells as the bell closes and dies as it opens.
    uint id = uint(int(n) + 4000);
    float f = noteOf(uint(hashU(id * 23u) * 5.0)) * 0.5;
    float swell = (1.0 - exp(-since / 0.12)) * exp(-since / 1.1) * p.pulse;
    swell *= 1.0 - smoothstep(0.85, 1.0, ph);
    float wobble = 0.8 * sin(TAU * f * 2.0 * since) * exp(-since / 0.5);
    float tone = sin(TAU * f * since + wobble);
    v += float2(0.5, 0.6) * tone * swell * 0.3;
    // The light going down the tentacles: six pings a beat, one as it passes
    // each sixth of the way down, the last at the tips, each from a
    // tentacle of its own, left or right as it hangs round the rim.
    float arms = floor(p.arms);
    for (int k = 0; k < 6; k++) {
        float which = floor(float(k) * arms / 6.0);
        float late = RIM_LATE + TIP_LATE * float(k + 1) / 6.0 + 0.12;
        float at = since - late * BEAT / max(p.speed, 0.05);
        // Before its moment, a tip is still ringing from the beat before.
        uint beat = id;
        if (at < 0.0) {
            at += BEAT / max(p.speed, 0.05);
            beat = id - 1u;
        }
        float pf = noteOf(beat + uint(k) * 3u) * 2.0;
        float ping = sin(TAU * pf * at) * exp(-at / 0.35);
        ping *= 1.0 - exp(-at / 0.004);
        float pan = 0.5 + 0.4 * cos((which + 0.5) / arms * TAU);
        v += float2(1.0 - pan, pan) * ping * 0.09 * p.glow;
    }
    float fadeIn = smoothstep(0.0, 2.0, t);
    return tanh(v * 1.6) / 1.6 * fadeIn * p.volume;
}
2 lines from stage 4 that are gone
    float heat = 0.5;
⋯
    return float4(abyss(d), 1.0);

The sound

The sound is built on the same beat as the picture, squeeze and all. Under everything is deep water: three low sines near D1, A1 and D2 (36.71, 55.2 and 73.6 Hz), the top one drifting slowly in phase. With each squeeze comes a wash, the water the bell pushes out: smooth noise from noise1, joined 300 and 110 times a second, growing with the square of the squeeze, with a brighter layer (1,400 a second) only while the bell is shut. pulse scales it as it scales the bell.

Each beat also has a note, one of five from a D minor pentatonic scale an octave below D4, picked by hashU from the beat's number. It swells over the first tenth of a second and dies away over the next second or so. Then come six pings, two octaves above the beat's notes (from D5), as the wave of light goes down the tentacles: one as it passes each sixth of the way down, the last as it reaches the tips, 3 seconds after the beat began, so that one sounds together with the next beat. Each ping comes from a tentacle of its own, left or right by where it hangs round the rim, and glow sets how loud the pings are, as it sets how bright the light is.

It all goes through tanh and fades in over the first two seconds. At the defaults the peak is about -8.9 dBFS and the average (RMS) about -20.4 dBFS.

New in Ray Kiln

The facing view. A triangle has two sides, and which one you see depends on where the eye is. The playground's new Facing button, under the picture of any vertex shader, draws the grid's triangles in gold where one is seen from its front and in blue where it is seen from its back. Each pixel keeps the brightness the shader gave it, so the shape still reads. The front is the side the grid shows as the host lays it down, with across running to the right and down running up; a flat sheet drawn that way is all gold, and its mirror image is all blue. It is on screen only: exports keep the shader's own colors.

Open Jellyfish in the playground and turn on Facing. The outside of the bell is gold: corner takes the way round clockwise seen from above, which puts the grid's front outside. Through the opening you see blue, the inside of the far wall, which no light model told you was the other side of the same sheet. The skirt under the rim is gold, and each ribbon changes between gold and blue along its length as it turns, once for each half turn of twist. The squares shade discards are gone here too, since the view runs the shader's own shade for every pixel.

Try this

  • With Facing on, set twist to 0: no ribbon turns any more, and each keeps one color from under the skirt to its tip, gold for some and blue for others, by which way it happens to lie. Set it to 4 and they stripe.
  • In shade, delete the line with discard_fragment(): the squares between the ribbons come back, and the tentacles become one crumpled bag hanging from the rim.
  • Set WALL (at the top) to 1.2: the wall stops before its widest part, and the bell opens into a shallow umbrella you can see well into.
  • Set arms to 4: four ribbons far apart, with four deep arches of skirt between them. Set it to 20 for a curtain.
  • Set pulse to 0: the bell stops squeezing and the tentacles keep their distance from the middle, but the light still runs down and the body still bobs.
  • Set trail to 0.5: short tentacles that curl under the bell; 2.4 lets them run off the bottom of the picture.
  • Move the pointer to the top of the picture: the eye drops below the animal and looks up into the bell.

Parameters

ParameterDefaultRangeWhat it does
speed1.00.0 to 3.0How fast it swims: a beat every 3 s at 1
pulse0.80.0 to 1.0How hard the bell squeezes on each beat
arms124 to 20How many tentacles hang from the rim
trail1.50.5 to 2.4How long the tentacles are
twist1.20.0 to 4.0Turns each ribbon makes along its length
glow0.70.0 to 1.0How bright the wave of light is that runs down the body
volume0.80.0 to 1.0Loudness 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.

jellyfish.metal
// Ray Kiln · Jellyfish
//
// @episode Jellyfish
// @length 60
// @variation 30
// @variations 1,2,3,4,6,9,10,11
// @still 13.0
// @teaches A pulsing bell and trailing tentacles from one grid, light through a soft body
// @category Vertex
// @tags vertex shaders, grids, displacement, normals, finite differences, two-sided lighting, translucency, discard, procedural sound
// @final Jellyfish | A wave of light runs from the crown to the tips with every beat, the body sways, and snow drifts down past it.
//
// A jellyfish swimming up through dark water. The host lays down one grid of squares, and `corner` makes the whole
// animal from it: the top of the grid is wrapped into a bell, across becoming the way round and down the way from
// the crown to the rim, and the rest of it hangs from the rim as tentacles, each a narrow ribbon cut from its own
// strip of the grid. The squares left between two ribbons are thrown away in `shade`. Every three seconds a squeeze
// starts at the crown and runs to the rim and on down the tentacles, each part doing what the part above it did a
// moment before. A soft body has no hard surface to light, so `shade` shows more of it where the eye looks along it,
// lets the light from above through it, and mixes in the water behind where it is thin.
// 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
//
// @grid 192

// @param speed 1.0 0.0 3.0 How fast it swims: a beat every 3 s at 1
// @param pulse 0.8 0.0 1.0 How hard the bell squeezes on each beat
// @param arms 12 4 20 How many tentacles hang from the rim
// @param trail 1.5 0.5 2.4 How long the tentacles are
// @param twist 1.2 0.0 4.0 Turns each ribbon makes along its length
// @param glow 0.7 0.0 1.0 How bright the wave of light is that runs down the body
// @param volume 0.8 0.0 1.0 Loudness of the sound

constant float TAU = 6.2831853;
// Seconds a beat at speed 1.
constant float BEAT = 3.0;
// The share of the grid, from the top, that is the bell; the rest is tentacles.
constant float BELL = 0.4;
// Radians the wall turns, crown to rim: past a quarter turn.
constant float WALL = 1.95;
// The bell's widest radius and its height above its middle.
constant float WIDE = 0.62;
constant float TALL = 0.5;
// Beats the squeeze takes to reach the rim, and then the tips.
constant float RIM_LATE = 0.18;
constant float TIP_LATE = 0.7;
// The share of a tentacle's length over which the skirt narrows to a ribbon.
constant float SKIRT = 0.1;
// The eye's distance from the point it looks at, and how much the camera
// magnifies.
constant float DISTANCE = 3.6;
constant float ZOOM = 2.8;
constant float3 AIM = float3(0.0, -0.3, 0.0);
// How far apart a corner's neighbors are.
constant float NEAR = 0.002;

// What a corner carries to the pixels, now with its tentacle's strip.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float3 normal;
    float2 body;
    float band;
};

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

// Where the eye is: in front of the jellyfish and a little below it, moved
// round it by the pointer (the middle of the picture looks straight on).
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    if (u.mouse.x <= 0.0 && u.mouse.y <= 0.0) m = float2(0.0);
    float yaw = -m.x * 2.4, pitch = -0.3 - m.y * 1.2;
    float c = cos(pitch);
    float3 back = float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
    // In a picture taller than wide the eye stands farther back.
    float fit = min(1.0, 1.4 * u.resolution.x / u.resolution.y);
    return AIM + DISTANCE / fit * back;
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes(float3 eye) {
    float3 f = normalize(AIM - eye);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A world point for the host: x and y on screen times the depth, the depth
// as 0 to 1 for the depth test, and the depth.
float4 project(float3 world, float3 eye, float aspect) {
    float3x3 axes = cameraAxes(eye);
    float3 v = world - eye;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float near = 0.1, far = 30.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * ZOOM / aspect, view.y * ZOOM, depth, view.z);
}

// A beat's squeeze: quick to close, slow to open; 0 open, 1 shut.
float squeeze(float beats) {
    float ph = fract(beats);
    return smoothstep(0.0, 0.25, ph) * (1.0 - smoothstep(0.25, 1.0, ph));
}

// How late, in beats, the squeeze reaches a place down the grid: the rim
// 0.18 of a beat after the crown, a tentacle's tip 0.7 after the rim.
float lateAt(float down) {
    if (down < BELL) return RIM_LATE * down / BELL;
    return RIM_LATE + TIP_LATE * (down - BELL) / (1.0 - BELL);
}

// The bell at `s` from crown to rim, squeezed by `c`.
float3 bellAt(float s, float angle, float c, float arms) {
    float a = s * WALL;
    float r = WIDE * sin(a) * (1.0 - 0.42 * c * s * s);
    // The rim is scalloped: a notch where each tentacle hangs.
    r *= 1.0 + 0.05 * pow(s, 6.0) * cos(arms * angle);
    float y = TALL * cos(a) * (1.0 + 0.22 * c * s);
    return float3(r * cos(angle), y, r * sin(angle));
}

// The body rises with each squeeze and sways.
float3 lean(float3 q, float beats) {
    float rise = 0.07 * squeeze(beats - 0.12);
    float az = 0.22 * sin(beats * 0.31 + 0.6), ax = 0.16 * sin(beats * 0.23);
    float cz = cos(az), sz = sin(az);
    q = float3(cz * q.x - sz * q.y, sz * q.x + cz * q.y, q.z);
    float cx = cos(ax), sx = sin(ax);
    q = float3(q.x, cx * q.y - sx * q.z, sx * q.y + cx * q.z);
    return q + float3(0.0, 0.2 + rise, 0.0);
}

// A point of the animal: `band` is its tentacle's strip of the grid.
float3 bodyAt(float2 g, float band, float beats, constant Params& p) {
    float arms = floor(p.arms);
    // Clockwise seen from above, so the grid's front is outside.
    float angle = -g.x * TAU;
    float c = squeeze(beats - lateAt(g.y)) * p.pulse;
    if (g.y <= BELL) return lean(bellAt(g.y / BELL, angle, c, arms), beats);
    // Below the rim: how far down, and where across the strip.
    float v = (g.y - BELL) / (1.0 - BELL);
    float l = g.x * arms - band - 0.5;
    float rimC = squeeze(beats - RIM_LATE) * p.pulse;
    float3 rim = bellAt(1.0, angle, rimC, arms);
    // The middle line hangs from its notch and waves as it trails.
    float mid = -(band + 0.5) / arms * TAU;
    uint id = uint(band) * 7u + 3u;
    float len = p.trail * (0.65 + 0.35 * hashU(id));
    float out = WIDE * sin(WALL) * (1.0 - 0.42 * c) * (1.0 - 0.3 * v);
    out += 0.11 * v * sin(TAU * (1.4 * v - beats) + 2.4 * band);
    float side = 0.09 * v * sin(TAU * (1.1 * v - 0.7 * beats) + 4.1 * band);
    float3 away = float3(cos(mid), 0.0, sin(mid));
    float3 round = float3(sin(mid), 0.0, -cos(mid));
    float3 line = away * out + round * side;
    line.y = rim.y - len * v * (1.0 - 0.1 * c);
    // The ribbon lies across that line, turning as it goes down.
    float turn = TAU * p.twist * v + 1.7 * band;
    float3 across = cos(turn) * round + sin(turn) * away;
    float wide = 0.014 + 0.05 * (1.0 - v);
    float3 ribbon = line + across * l * wide;
    // Just under the rim the strip is still the bell's skirt.
    float3 q = mix(rim, ribbon, smoothstep(0.0, SKIRT, v));
    return lean(q, beats);
}

// Once for every corner: its place on the animal, and its normal from where
// its two neighbors are.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float beats = u.time * p.speed / BEAT;
    // The strip of the grid this corner is in: one for each tentacle.
    float band = floor(grid.x * floor(p.arms));
    // At the crown every corner is the same point: ask a little below it.
    float2 g = float2(grid.x, max(grid.y, 2.0 * NEAR));
    float2 dx = float2(NEAR, 0.0), dy = float2(0.0, NEAR);
    float3 along = bodyAt(g + dx, band, beats, p);
    along -= bodyAt(g - dx, band, beats, p);
    float3 down = bodyAt(g + dy, band, beats, p);
    down -= bodyAt(g - dy, band, beats, p);
    float3 eye = eyeAt(u);
    Vertex o;
    o.world = bodyAt(grid, band, beats, p);
    o.position = project(o.world, eye, u.resolution.x / u.resolution.y);
    o.normal = normalize(cross(down, along));
    o.body = grid;
    o.band = band;
    return o;
}

// The water in a direction from the eye: near-black below, a deep blue
// above, and a soft light coming down from the surface.
float3 abyss(float3 d) {
    float up = smoothstep(-0.6, 0.9, d.y);
    float3 deep = float3(0.006, 0.008, 0.014);
    float3 col = mix(deep, float3(0.02, 0.055, 0.1), up);
    float3 sun = normalize(float3(0.25, 1.0, 0.1));
    float shaft = pow(max(dot(d, sun), 0.0), 6.0);
    return col + float3(0.05, 0.1, 0.13) * shaft;
}

// The wave of light: brightest where the squeeze is closing.
float lightWave(float down, float beats) {
    float ph = fract(beats - lateAt(down));
    float x = (ph - 0.12) / 0.09;
    return exp(-x * x);
}

// Once for every pixel of the grid: the soft body, lit from above.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float tv = (v.body.y - BELL) / (1.0 - BELL);
    // Between two tentacles the band is part-way from one whole number to
    // the next: throw those pixels away, in an arch under the rim.
    float between = abs(v.band - floor(v.band + 0.5));
    float arch = SKIRT * (0.9 - 0.7 * 4.0 * between * (1.0 - between));
    if (between > 0.0005 && tv > arch) discard_fragment();

    float3 eye = eyeAt(u);
    float3 toEye = normalize(eye - v.world);
    float3 n = normalize(v.normal);
    // The side that faces the eye: the outside, or the inside of the bell.
    float inside = dot(n, toEye) < 0.0 ? 1.0 : 0.0;
    if (inside > 0.5) n = -n;
    float3 sun = normalize(float3(0.25, 1.0, 0.1));

    // A soft body shows most where the eye looks along its surface.
    float edge = 1.0 - abs(dot(n, toEye));
    float thick = 0.35 + 0.65 * pow(edge, 1.6);
    // Thicker at the crown, thin at the rim; a ribbon is seen flat on.
    float s = clamp(v.body.y / BELL, 0.0, 1.0);
    thick *= mix(1.0, 0.55, s);
    if (tv > 0.0) thick = 0.7 + 0.3 * edge;

    // Canals from crown to rim, and a ring of four warm lobes.
    float arms = floor(p.arms);
    float heat = 0.5 + 0.1 * cos(arms * v.body.x * TAU) * s;
    float lobes = 0.5 + 0.5 * cos(4.0 * v.body.x * TAU);
    float ring = smoothstep(0.2, 0.34, s) * (1.0 - smoothstep(0.4, 0.56, s));
    heat += 0.22 * lobes * ring;
    if (tv > 0.0) heat = 0.6 - 0.16 * tv;
    heat -= 0.12 * inside;
    float3 flesh = kiln(heat);

    // The light from above: on the surface, and through it from behind.
    float lit = max(dot(n, sun), 0.0);
    float through = max(-dot(n, sun), 0.0);
    float3 col = flesh * (0.5 + 0.9 * lit + 0.7 * through);
    col += kiln(0.72) * pow(edge, 3.0) * 0.4;
    // The light runs down the body with the squeeze.
    float beats = u.time * p.speed / BEAT;
    float wave = lightWave(v.body.y, beats) * p.glow;
    col += kiln(0.78 + 0.15 * wave) * wave * (0.5 + 0.9 * edge);
    // Where the body is thin, the water behind shows through it.
    float3 behind = abyss(-toEye);
    col = mix(behind, col + behind * 0.6, clamp(thick, 0.0, 1.0));
    return float4(col, 1.0);
}

// A random 0 to 1 for a cell of the snow.
float snowHash(float2 cell, uint salt) {
    uint x = uint(int(cell.x) + 4096), y = uint(int(cell.y) + 4096);
    return hashU(x * 7349u + y * 911u + salt);
}

// What is behind the animal: the water, and specks of snow sinking past
// as it swims up, the near ones larger and faster.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3 eye = eyeAt(u);
    float3x3 axes = cameraAxes(eye);
    float2 sc = (uv * 2.0 - 1.0) / ZOOM;
    float3 d = normalize(axes[2] + axes[0] * sc.x * aspect + axes[1] * sc.y);
    float3 col = abyss(d);
    float beats = u.time * p.speed / BEAT;
    // The snow sinks faster just after each squeeze, as the body surges.
    float sunk = beats * 0.5 + 0.1 * squeeze(beats - 0.2);
    float2 q = float2(atan2(d.x, d.z), d.y);
    for (int k = 0; k < 3; k++) {
        float size = 9.0 + 7.0 * float(k);
        float fall = sunk * (2.6 - 0.7 * float(k));
        float2 at = q * size + float2(float(k) * 3.7, fall);
        float2 cell = floor(at);
        uint salt = uint(k) * 13u;
        float2 spot = float2(snowHash(cell, salt), snowHash(cell, salt + 5u));
        float far = length(at - cell - 0.2 - 0.6 * spot);
        float bright = snowHash(cell, salt + 9u);
        float speck = smoothstep(0.09 - 0.02 * float(k), 0.0, far);
        col += float3(0.3, 0.42, 0.5) * speck * bright * bright * 0.5;
    }
    return float4(col, 1.0);
}

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

// Rippling Water's noise from joined random values, `rate` times a second.
float noise1(float t, float rate, uint salt) {
    float x = t * rate;
    float i = floor(x);
    float f = x - i;
    f = f * f * (3.0 - 2.0 * f);
    uint n = uint(int(i) + 1000000);
    return mix(hashU(n * 31u + salt), hashU((n + 1u) * 31u + salt), f);
}

// One of five notes of a D minor pentatonic scale, as a frequency.
float noteOf(uint n) {
    uint k = n % 5u;
    float semis = k == 0u ? 0.0 : k == 1u ? 3.0 : k == 2u ? 5.0 : 7.0;
    if (k == 4u) semis = 10.0;
    return 293.66 * pow(2.0, semis / 12.0);
}

// The sound: deep water, a wash with every squeeze, a soft note on each beat,
// and six pings as the wave of light runs down the tentacles.
float2 sound(float t, constant Params& p) {
    float beats = t * p.speed / BEAT;
    float n = floor(beats);
    float ph = beats - n;
    float since = ph * BEAT / max(p.speed, 0.05);
    // The deep: D1 and A1, a few cents apart, slowly beating.
    float deep = sin(TAU * 36.71 * t) + 0.7 * sin(TAU * 55.2 * t);
    deep += 0.5 * sin(TAU * 73.6 * t + 0.6 * sin(TAU * 0.11 * t));
    float2 v = float2(deep) * 0.11;
    // The wash: the water the bell pushes out, loudest as it closes.
    float push = squeeze(beats) * p.pulse;
    float dark = noise1(t, 300.0, 3u) - 0.5;
    dark += 0.7 * (noise1(t, 110.0, 7u) - 0.5);
    float bright = noise1(t, 1400.0, 11u) - 0.5;
    float wash = dark + 0.5 * push * bright;
    v += float2(1.0, 0.9) * wash * (0.05 + 0.5 * push * push);
    // The beat's note: it swells as the bell closes and dies as it opens.
    uint id = uint(int(n) + 4000);
    float f = noteOf(uint(hashU(id * 23u) * 5.0)) * 0.5;
    float swell = (1.0 - exp(-since / 0.12)) * exp(-since / 1.1) * p.pulse;
    swell *= 1.0 - smoothstep(0.85, 1.0, ph);
    float wobble = 0.8 * sin(TAU * f * 2.0 * since) * exp(-since / 0.5);
    float tone = sin(TAU * f * since + wobble);
    v += float2(0.5, 0.6) * tone * swell * 0.3;
    // The light going down the tentacles: six pings a beat, one as it passes
    // each sixth of the way down, the last at the tips, each from a
    // tentacle of its own, left or right as it hangs round the rim.
    float arms = floor(p.arms);
    for (int k = 0; k < 6; k++) {
        float which = floor(float(k) * arms / 6.0);
        float late = RIM_LATE + TIP_LATE * float(k + 1) / 6.0 + 0.12;
        float at = since - late * BEAT / max(p.speed, 0.05);
        // Before its moment, a tip is still ringing from the beat before.
        uint beat = id;
        if (at < 0.0) {
            at += BEAT / max(p.speed, 0.05);
            beat = id - 1u;
        }
        float pf = noteOf(beat + uint(k) * 3u) * 2.0;
        float ping = sin(TAU * pf * at) * exp(-at / 0.35);
        ping *= 1.0 - exp(-at / 0.004);
        float pan = 0.5 + 0.4 * cos((which + 0.5) / arms * TAU);
        v += float2(1.0 - pan, pan) * ping * 0.09 * p.glow;
    }
    float fadeIn = smoothstep(0.0, 2.0, t);
    return tanh(v * 1.6) / 1.6 * fadeIn * p.volume;
}

Running it in your own project

Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.

To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:

Drawing it, in Swift
struct Uniforms {
    var resolution: SIMD2<Float>
    var time: Float
    var timeDelta: Float
    var mouse: SIMD2<Float>
    var frame: UInt32
    var pad: UInt32 = 0
}

let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)

// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [1.0, 0.8, 12, 1.5, 1.2, 0.7, 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