Library

Shared functions

The helper functions the shaders share, each with what it does, the episode that introduced it, and the code to copy. Most are a few lines long; the ones that need another function say which.

Noise and randomness

hash21

Turns a 2D point into a pseudo-random number from 0 to 1. The same point always gives the same number, and neighboring points give unrelated ones.

Introduced in Flowing Color Field; also in Kaleidoscope, Cracked Clay, Endless Terrain, Kiln Floor Tiles, Aurora, Rain on Glass, Lightning, Heat Haze (changed), Ocean Waves, Stained Glass, Clockwork, Lava Flow, Frosted Glass (changed), Lighthouse, Topographic Map

hash21 · from Flowing Color Field
float hash21(float2 p) {
    p = fract(p * float2(123.34, 456.21));
    p += dot(p, p + 45.32);
    return fract(p.x * p.y);
}

hash11

Turns one number into a pseudo-random number from 0 to 1. The same number always gives the same result, so smoothing between the results at whole numbers gives noise along a line.

Introduced in Wood & Marble; also in Sunset, Lava Flow, Topographic Map, Copper Kettle

hash11 · from Wood & Marble
float hash11(float n) {
    return fract(sin(n * 12.9898) * 43758.5453);
}

hash22

Like hash21, but gives two random numbers for a point, such as a random position inside a grid square.

Introduced in Cracked Clay; also in Stained Glass

hash22 · from Cracked Clay
float2 hash22(float2 p) {
    float3 q = fract(float3(p.xyx) * float3(0.1031, 0.1030, 0.0973));
    q += dot(q, q.yzx + 33.33);
    return fract((q.xx + q.yz) * q.zy);
}

hashU integer hash

A whole number in, a random number from 0 to 1 out. It works on integers, so noise built on it stays exact for minutes of sound, where a float hash would lose precision.

Introduced in Cracked Clay; also in Molten Metaballs, Kiln Fire, Ember Tunnel, Particle Swarm, Endless Terrain, Glass Orb, Curl-Noise Flow, Rippling Water, Pottery Wheel, Kiln Floor Tiles, Smoke Stack, Murmuration, Flag in the Wind, Neon Sign, Aurora, Rain on Glass, Spinning Planet, Spiral Galaxy, Lightning, Black Hole, Crystal Bloom, Ocean Waves, Stained Glass, Fireflies, Lorenz Attractor, Lighthouse, Jellyfish, Hall of Mirrors, Pine Cone, Fireworks, Copper Kettle

hashU · from Cracked Clay
float hashU(uint n) {
    n = (n << 13u) ^ n;
    n = n * (n * n * 15731u + 789221u) + 1376312589u;
    return float(n & 0x7fffffffu) / float(0x7fffffff);
}

hashBits whole-number hash

Scrambles the bits of a whole number with shifts, XORs and two multiplies, the same on every GPU: a step number, a pattern seed or a sample's number in, 32 random-looking bits out.

Introduced in A Shader That Keeps the Beat; also in Pulse Synth, Wind Chimes

hashBits · from A Shader That Keeps the Beat
uint hashBits(uint x) {
    x ^= x >> 16;
    x *= 0x7feb352du;
    x ^= x >> 15;
    x *= 0x846ca68bu;
    x ^= x >> 16;
    return x;
}

vnoise value noise

Value noise: a random value at each grid point, blended smoothly between the points.

Introduced in Flowing Color Field; also in Kaleidoscope, Cracked Clay, Endless Terrain, Aurora, Rain on Glass, Spiral Galaxy (changed), Lightning, Heat Haze, Ocean Waves, Stained Glass, Clockwork, Lava Flow, Frosted Glass, Topographic Map

Calls hash21

vnoise · from Flowing Color Field
float vnoise(float2 p) {
    float2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash21(i), b = hash21(i + float2(1, 0));
    float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

fbm fractal noise

Fractal noise: several octaves of value noise added together, each about twice as fine and half as strong as the one before.

Introduced in Flowing Color Field; also in Kaleidoscope (changed), Cracked Clay (changed), Aurora, Lightning, Stained Glass (changed), Clockwork (changed), Lava Flow, Topographic Map

Calls vnoise

fbm · from Flowing Color Field
float fbm(float2 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) {
        v += a * vnoise(p);
        p = p * 2.03 + float2(17.1, 9.2);
        a *= 0.5;
    }
    return v;
}

voronoi Voronoi cells

Voronoi cells around a random point in each grid square. Returns the distance to the nearest edge between two cells, the distance to the nearest point, and a random number for the cell.

Introduced in Cracked Clay; also in Stained Glass

Calls cellPoint, hash21

voronoi · from Cracked Clay
float3 voronoi(float2 x, float t) {
    float2 n = floor(x), f = fract(x);
    float2 mg = 0.0, mr = 0.0;
    float md = 8.0;
    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            float2 g = float2(i, j);
            float2 r = g + cellPoint(n + g, t) - f;
            float d = dot(r, r);
            if (d < md) { md = d; mr = r; mg = g; }
        }
    }
    float near = sqrt(md);
    md = 8.0;
    for (int j = -2; j <= 2; j++) {
        for (int i = -2; i <= 2; i++) {
            float2 g = mg + float2(i, j);
            float2 r = g + cellPoint(n + g, t) - f;
            // The nearest cell itself is left out.
            if (i != 0 || j != 0) {
                md = min(md, dot(0.5 * (mr + r), normalize(r - mr)));
            }
        }
    }
    return float3(md, near, hash21(n + mg));
}

cellPoint Voronoi cell point

The point voronoi puts in grid square c: a random place from hash22, swinging back and forth in time t. With t fixed, the points stay still.

Introduced in Cracked Clay; also in Stained Glass

Calls hash22

cellPoint · from Cracked Clay
float2 cellPoint(float2 c, float t) {
    float2 h = hash22(c);
    return 0.5 + 0.4 * sin(t * 0.3 + TAU * h);
}

snoise 1D noise

Smooth noise along one line, from -1 to 1: random values at whole numbers, blended between them.

Introduced in Flowing Color Field

Calls sh11 (the episode’s own)

snoise · from Flowing Color Field
float snoise(float x) {
    float i = floor(x), f = fract(x);
    f = f * f * (3.0 - 2.0 * f);
    return mix(sh11(i), sh11(i + 1.0), f) * 2.0 - 1.0;
}

noise1 sound noise

Noise for sound: random values joined rate times a second. A lower rate sounds darker.

Introduced in Cracked Clay; also in Kiln Fire, Ember Tunnel, Particle Swarm (changed), Endless Terrain, Curl-Noise Flow (changed), Rippling Water (changed), Pottery Wheel, Kiln Floor Tiles, Smoke Stack, Murmuration (changed), Flag in the Wind (changed), Aurora, Rain on Glass, Spinning Planet (changed), Spiral Galaxy (changed), Lightning, Black Hole (changed), Ocean Waves (changed), Fireflies (changed), Lighthouse, Jellyfish (changed), Fireworks, Copper Kettle

Calls hashU

noise1 · from Cracked Clay
float noise1(float t, float rate, uint seed) {
    float x = t * rate;
    float i = floor(x), f = fract(x);
    f = f * f * (3.0 - 2.0 * f);
    uint k = uint(i) + seed * 7919u;
    return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}

gnoise gradient noise

Gradient noise in 3D, roughly -1 to 1: a random slope at each corner of a grid of cubes (a direction on a sphere, from the corner's hash), blended with a smooth curve. Zero at every corner, so its grid hardly shows; the third coordinate can be time.

Introduced in Curl-Noise Flow

Calls cornerPart (the episode’s own)

gnoise · from Curl-Noise Flow
float gnoise(float3 p) {
    float3 i = floor(p), f = p - i;
    float3 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
    float n000 = cornerPart(i, f, float3(0, 0, 0));
    float n100 = cornerPart(i, f, float3(1, 0, 0));
    float n010 = cornerPart(i, f, float3(0, 1, 0));
    float n110 = cornerPart(i, f, float3(1, 1, 0));
    float n001 = cornerPart(i, f, float3(0, 0, 1));
    float n101 = cornerPart(i, f, float3(1, 0, 1));
    float n011 = cornerPart(i, f, float3(0, 1, 1));
    float n111 = cornerPart(i, f, float3(1, 1, 1));
    float lo = mix(mix(n000, n100, w.x), mix(n010, n110, w.x), w.y);
    float hi = mix(mix(n001, n101, w.x), mix(n011, n111, w.x), w.y);
    return mix(lo, hi, w.z);
}

curl curl of noise

A flow from a landscape: its slope, measured with four samples a small step apart, turned a quarter, so the flow follows the contour lines and never gathers anywhere.

Introduced in Curl-Noise Flow

Calls potential (the episode’s own)

curl · from Curl-Noise Flow
float2 curl(float2 x, float t, constant Params& p) {
    float2 a = float2(0.002, 0.0), b = float2(0.0, 0.002);
    float dx = potential(x + a, t, p) - potential(x - a, t, p);
    float dy = potential(x + b, t, p) - potential(x - b, t, p);
    return float2(dy, -dx) / 0.004;
}

hash31 3D hash

Turns a 3D point into a pseudo-random number from 0 to 1, the same for every point in the same whole-numbered cube. It mixes the cube's three whole numbers with integer arithmetic, so neighboring cubes give unrelated numbers.

Introduced in Kiln Fire; also in Smoke Stack, Wood & Marble, Soap Bubble, Lighthouse, Copper Kettle

hash31 · from Kiln Fire
float hash31(float3 p) {
    uint3 v = uint3(int3(floor(p))) * 1664525u + 1013904223u;
    v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
    v ^= v >> 16u;
    v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
    return float(v.x) * (1.0 / 4294967295.0);
}

hash3 3D hash

A random 0 to 1 for each whole-numbered point in space: the three whole numbers become unsigned integers, each multiplied by a large prime, combined with XOR and put through hashU, so neighboring cubes give unrelated numbers and the result is exact however far out.

Introduced in Spinning Planet; also in Black Hole

Calls hashU

hash3 · from Spinning Planet
float hash3(float3 c) {
    uint x = uint(int(c.x) + 4096);
    uint y = uint(int(c.y) + 4096);
    uint z = uint(int(c.z) + 4096);
    return hashU(x * 73856093u ^ y * 19349663u ^ z * 83492791u);
}

vnoise3 3D value noise

Value noise one dimension up: a random value at each corner of a grid of cubes (from hash31), blended smoothly across each cube, eight corners instead of four.

Introduced in Kiln Fire; also in Smoke Stack, Soap Bubble, Lighthouse, Copper Kettle

Calls hash31

vnoise3 · from Kiln Fire
float vnoise3(float3 p) {
    float3 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash31(i),                     b = hash31(i + float3(1, 0, 0));
    float c = hash31(i + float3(0, 1, 0)),   d = hash31(i + float3(1, 1, 0));
    float e = hash31(i + float3(0, 0, 1)),   g = hash31(i + float3(1, 0, 1));
    float h = hash31(i + float3(0, 1, 1)),   k = hash31(i + float3(1, 1, 1));
    float lo = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
    float hi = mix(mix(e, g, f.x), mix(h, k, f.x), f.y);
    return mix(lo, hi, f.z);
}

valueNoise3 3D value noise

Value noise in three dimensions from hash3: a random value at every whole-numbered point, blended smoothly across the cube between eight of them. Spinning Planet reads it on the ball so its noise closes without a seam; Black Hole reads it on a cylinder round the disk.

Introduced in Spinning Planet; also in Black Hole

Calls hash3

valueNoise3 · from Spinning Planet
float valueNoise3(float3 x) {
    float3 i = floor(x);
    float3 f = x - i;
    f = f * f * (3.0 - 2.0 * f);
    float a = mix(hash3(i), hash3(i + float3(1, 0, 0)), f.x);
    float b = mix(hash3(i + float3(0, 1, 0)), hash3(i + float3(1, 1, 0)), f.x);
    float c = mix(hash3(i + float3(0, 0, 1)), hash3(i + float3(1, 0, 1)), f.x);
    float d = mix(hash3(i + float3(0, 1, 1)), hash3(i + float3(1, 1, 1)), f.x);
    return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
}

fbm3 3D fractal noise

Octaves of 3D value noise, each about twice as fine and half as strong as the one before, around 0.5 on average. Smoke Stack's takes the number of octaves, so a light march can get by with fewer.

Introduced in Kiln Fire; also in Smoke Stack (changed), Spinning Planet (changed), Black Hole (changed), Soap Bubble (changed), Lighthouse (changed)

Calls vnoise3

fbm3 · from Kiln Fire
float fbm3(float3 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) {
        v += a * vnoise3(p);
        p = p * 2.02 + float3(17.1, 9.2, 4.7);
        a *= 0.5;
    }
    return v;
}

wrap wrap into a period

x wrapped into 0 to m, below 0 as well as above (x minus m times floor(x / m)). Wrapping a lattice point's number before hashing it makes noise repeat every m cells.

Introduced in Kiln Floor Tiles; also in Wood & Marble

wrap · from Kiln Floor Tiles
float wrap(float x, float m) {
    return x - m * floor(x / m);
}

Shapes in 2D

sdCircle circle distance

The distance to a circle of radius r: negative inside, zero on the edge, positive outside.

Introduced in Morphing Shapes; also in Kaleidoscope

sdCircle · from Morphing Shapes
float sdCircle(float2 p, float r) {
    return length(p) - r;
}

sdBox box distance

The distance to a rectangle centered on the origin, with b its half-width and half-height. It is exact inside and outside, corners included.

Introduced in Morphing Shapes; also in Kaleidoscope

sdBox · from Morphing Shapes
float sdBox(float2 p, float2 b) {
    float2 d = abs(p) - b;
    return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
}

segment segment distance

The distance from a point (the first argument) to the line segment between the other two: projected onto the segment, clamped to its ends, and measured from there.

Introduced in Murmuration; also in Pulse Synth (changed), Lightning (changed)

segment · from Murmuration
float segment(float2 p, float2 a, float2 b) {
    float2 pa = p - a, ba = b - a;
    return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0));
}

lineAt line from a distance

Turns a distance into ink: 1 on the shape, falling smoothly to 0 by w away (one minus a smoothstep), so a distance function becomes a line or a solid shape with an antialiased edge a pixel or two wide.

Introduced in Pulse Synth; also in Wind Chimes

lineAt · from Pulse Synth
float lineAt(float d, float w) {
    return 1.0 - smoothstep(0.0, w, d);
}

sdTriangle triangle distance

The distance to an equilateral triangle centered on the origin, r setting its size.

Introduced in Morphing Shapes

sdTriangle · from Morphing Shapes
float sdTriangle(float2 p, float r) {
    const float k = 1.7320508;
    p.x = abs(p.x) - r;
    p.y = p.y + r / k;
    if (p.x + k * p.y > 0.0) p = float2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
    p.x -= clamp(p.x, -2.0 * r, 0.0);
    return -length(p) * sign(p.y);
}

Shapes in 3D

sdSphere sphere distance

The distance to a sphere of radius r: the circle's formula, in three dimensions.

Introduced in Floating 3D Scene; also in Molten Metaballs

sdSphere · from Floating 3D Scene
float sdSphere(float3 p, float r) {
    return length(p) - r;
}

sdBox 3D box distance

The distance to a box centered on the origin, with b its half-size along each axis.

Introduced in Floating 3D Scene; also in Menger Sponge

sdBox · from Floating 3D Scene
float sdBox(float3 p, float3 b) {
    float3 d = abs(p) - b;
    return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}

sdTorus torus distance

The distance to a ring lying flat: R is the radius of the ring, r the thickness of its tube.

Introduced in Floating 3D Scene; also in Glass Orb

sdTorus · from Floating 3D Scene
float sdTorus(float3 p, float R, float r) {
    float2 q = float2(length(p.xz) - R, p.y);
    return length(q) - r;
}

Combining shapes

smin smooth minimum

The smooth minimum. Like min, but when two distances are within k of each other it pulls the result below both, so two shapes melt into one with a smooth neck between them.

Introduced in Morphing Shapes; also in Floating 3D Scene, Kaleidoscope, Molten Metaballs, Copper Kettle

smin · from Morphing Shapes
float smin(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

Moving space

kaleido kaleidoscope fold

Folds the plane into n slices around the center, each mirrored down its middle, so whatever is drawn in one half-slice appears in all of them. spin turns the folds and twist turns them more the further out they are.

Introduced in Kaleidoscope

kaleido · from Kaleidoscope
float2 kaleido(float2 q, float n, float spin, float twist) {
    float r = length(q);
    float a = atan2(q.y, q.x) + spin + twist * r;
    float slice = TAU / n;
    // Angular repetition: every slice lands on the first.
    a -= slice * floor(a / slice);
    // Mirror fold: the two halves of a slice reflect each other.
    a = abs(a - slice * 0.5);
    return r * float2(cos(a), sin(a));
}

travel camera travel

How far along its path the camera has flown at time t: the time times a fixed speed. Everything else about the camera (where it is, where it looks, how it leans) is worked out from that distance.

Introduced in Ember Tunnel; also in Endless Terrain (changed)

travel · from Ember Tunnel
float travel(float t) {
    return t * 3.0;
}

orbitRate orbit rate

How fast something goes round at a distance from the center, in radians a second, so an angle can be turned by rate × time. Each episode has its own law: Spiral Galaxy's is a rotation curve that rises through the core and then holds, so outer stars lag; Black Hole's is Kepler's, falling as r to the three halves, so the inner gas shears past the outer.

Introduced in Spiral Galaxy; also in Black Hole (changed)

orbitRate · from Spiral Galaxy
float orbitRate(float a, constant Params& p) {
    float v = 0.11 * a / sqrt(a * a + CORE * CORE);
    return p.speed * v / max(a, 0.01);
}

viewOf the particles' view

Where a point of a particle episode's own space is on the picture, one unit the half height, with no perspective: turned about the scene's axis with rot (the pointer turns it too) and leaned toward the eye. Spiral Galaxy takes a place in the disk and a height out of it; Lorenz Attractor a point of Lorenz's three numbers, with z up.

Introduced in Spiral Galaxy; also in Lorenz Attractor (changed)

Calls viewAngles (the episode’s own), rot

viewOf · from Spiral Galaxy
float2 viewOf(float2 position, float height,
              constant Uniforms& u, constant Params& p) {
    float2 v = viewAngles(u, p);
    float2 r = rot(position, v.x);
    return float2(r.x, r.y * cos(v.y) + height * sin(v.y));
}

zoomOf fit to the picture

How large the subject is drawn: its usual size, or smaller, 0.95 of the picture's width over its height, in a picture taller than wide, so it fits a phone held upright.

Introduced in Spiral Galaxy; also in Lorenz Attractor (changed), Fireworks (changed)

zoomOf · from Spiral Galaxy
float zoomOf(constant Uniforms& u) {
    return min(ZOOM, 0.95 * u.resolution.x / u.resolution.y);
}

Raymarching and light

normalAt surface normals

The direction a surface faces at a point: how the scene's distance changes for a small step along each axis. Its arguments are whatever the episode's map takes.

Introduced in Floating 3D Scene; also in Molten Metaballs (changed), Ember Tunnel (changed), Copper Kettle (changed)

Calls map (the episode’s own)

normalAt · from Floating 3D Scene
float3 normalAt(float3 p, float t, float lift, float k) {
    const float2 e = float2(0.001, 0.0);
    return normalize(float3(
        map(p + e.xyy, t, lift, k) - map(p - e.xyy, t, lift, k),
        map(p + e.yxy, t, lift, k) - map(p - e.yxy, t, lift, k),
        map(p + e.yyx, t, lift, k) - map(p - e.yyx, t, lift, k)));
}

softShadow soft shadows

Marches from a surface point towards the light. The nearer the ray passes to something, compared with how far it has gone, the darker the shadow, so its edge fades over a band; w sets how wide.

Introduced in Molten Metaballs; also in Endless Terrain (changed), Copper Kettle (changed)

Calls map (the episode’s own)

softShadow · from Molten Metaballs
float softShadow(float3 ro, float3 rd, float w,
                 float t, int n, float rise, float k) {
    float res = 1.0;
    float s = 0.02;
    for (int i = 0; i < 48; i++) {
        float h = map(ro + rd * s, t, n, rise, k);
        res = min(res, h / (w * s));
        if (res < 0.001 || s > 6.0) break;
        s += clamp(h, 0.01, 0.3);
    }
    res = clamp(res, 0.0, 1.0);
    return res * res * (3.0 - 2.0 * res);
}

ambientOcclusion ambient occlusion

Takes a few short steps out along the normal. Where the scene is nearer than the step, something blocks light from that side, so creases and corners come out darker.

Introduced in Molten Metaballs

Calls map (the episode’s own)

ambientOcclusion · from Molten Metaballs
float ambientOcclusion(float3 pos, float3 nrm,
                       float t, int n, float rise, float k) {
    float occ = 0.0;
    float weight = 1.0;
    for (int i = 0; i < 5; i++) {
        float h = 0.02 + 0.12 * float(i);
        occ += (h - map(pos + nrm * h, t, n, rise, k)) * weight;
        weight *= 0.8;
    }
    return clamp(1.0 - 2.0 * occ, 0.0, 1.0);
}

fresnel Fresnel

Schlick's Fresnel: how much of the light a surface mirrors at an angle. f0 is the share mirrored head on, worked out from the index of refraction, and toward a grazing angle the share climbs to all of it by (1 - cos)⁵.

Introduced in Glass Orb; also in Pottery Wheel

fresnel · from Glass Orb
float fresnel(float cosi, float ior) {
    float f0 = (ior - 1.0) / (ior + 1.0);
    f0 *= f0;
    return f0 + (1.0 - f0) * pow(1.0 - clamp(cosi, 0.0, 1.0), 5.0);
}

lightDir the light's direction

Where the kiln's light comes from, as a unit direction: high and to one side. Pottery Wheel's goes round with the camera, so the subject is always lit from the viewer's side.

Introduced in Glass Orb; also in Pottery Wheel (changed)

lightDir · from Glass Orb
float3 lightDir(float t) {
    return normalize(float3(-0.6, 1.0, -0.4));
}

sky the kiln's dark

What a ray sees that meets nothing: the dark of the kiln, a little warmer toward the horizon, and glowing where it points toward the light, its hot mouth.

Introduced in Glass Orb; also in Pottery Wheel, Murmuration (changed), Flag in the Wind (changed), Fireflies (changed)

sky · from Glass Orb
float sky(float3 rd, float3 l) {
    float up = abs(rd.y);
    float mouth = pow(max(dot(rd, l), 0.0), 6.0);
    return 0.08 + 0.14 * exp(-up * 6.0) + 0.55 * mouth;
}

floorLevel the tiled floor

The brightness of the kiln's floor at a point: tiles of fired clay, each a slightly different shade from a hash of its number, with dark grout between them, and the shadow of whatever stands on it cast along the light.

Introduced in Glass Orb; also in Pottery Wheel (changed)

floorLevel · from Glass Orb
float floorLevel(float3 p, float3 l, float glow) {
    float2 g = p.xz * 1.6;
    float2 f = abs(fract(g) - 0.5);
    float grout = smoothstep(0.47, 0.44, max(f.x, f.y));
    float2 id = floor(g);
    float tone = 0.28 + 0.08 * fract(sin(dot(id, float2(12.9, 78.2))) * 437.5);
    // The orb's shadow, cast along the light onto the floor.
    float3 toOrb = -p;
    float along = dot(toOrb, l);
    float miss = length(toOrb - l * along);
    float shadow = along > 0.0 ? smoothstep(0.55, 1.15, miss) : 1.0;
    // The caustic: the orb works as a lens, so the light it bends gathers in
    // a spot inside the shadow.
    float focus = exp(-miss * miss * 18.0) * step(0.0, along);
    float level = tone * grout * (0.35 + 0.65 * shadow) + 0.9 * focus;
    // The ember's own glow reaches the floor nearby.
    level += glow * 0.35 * exp(-length(p.xz) * 1.5);
    return level;
}

env the room

Everything a free ray sees of the room: the floor if it points down, found without marching since the floor is flat, fading into the kiln's haze with distance; otherwise the kiln's dark. Reflections ask it what they see.

Introduced in Glass Orb; also in Pottery Wheel (changed)

Calls lightDir, sky, floorLevel

env · from Glass Orb
float env(float3 ro, float3 rd, float t, float glow) {
    float3 l = lightDir(t);
    float back = sky(rd, l);
    if (rd.y < -0.001) {
        float d = (FLOOR - ro.y) / rd.y;
        float3 p = ro + rd * d;
        if (d > 0.0) return mix(back, floorLevel(p, l, glow), exp(-d * 0.12));
    }
    return back;
}

kilnSky kiln sky

The sky in a direction, for water to mirror: gold at the horizon fading to near-black overhead, with the kiln's glow low ahead and a haze around it, all through the kiln palette; heat slides it hotter or cooler.

Introduced in Rippling Water; also in Ocean Waves

Calls kiln

kilnSky · from Rippling Water
float3 kilnSky(float3 d, constant Params& p) {
    float up = pow(max(d.y, 0.0), 0.6);
    float g = max(dot(d, GLOW), 0.0);
    float glow = 0.5 * pow(g, 500.0) + 0.2 * pow(g, 30.0) + 0.07 * pow(g, 3.0);
    float t = 0.6 - 0.75 * up + glow + (p.heat - 0.5) * 0.4;
    return kiln(min(t, 0.92));
}

nightSky night sky

The night sky in a direction: dark blue overhead and paler toward the horizon. Smoke Stack's adds a warm haze low down, stars and a moon with a glow round it; Lighthouse's is the two blues alone, with its stars in a function of their own, so the sea can mirror the sky without them.

Introduced in Smoke Stack; also in Lighthouse (changed)

Calls hash31

nightSky · from Smoke Stack
float3 nightSky(float3 rd, float t) {
    float up = clamp(rd.y, 0.0, 1.0);
    float3 col = mix(float3(0.10, 0.045, 0.040), float3(0.012, 0.016, 0.040),
                     smoothstep(-0.05, 0.45, rd.y));
    float2 a = float2(atan2(rd.x, rd.z), rd.y) * 90.0;
    float star = hash31(float3(floor(a), 1.0));
    float2 at = fract(a) - 0.5;
    col += step(0.985, star) * up * smoothstep(0.12, 0.0, length(at)) * 0.7
           * (0.75 + 0.25 * sin(t * 3.0 + star * 60.0));
    float m = dot(rd, MOON);
    col += MOONLIGHT * smoothstep(0.9993, 0.9995, m) * 1.4;
    m = max(m, 0.0);
    col += MOONLIGHT * (pow(m, 300.0) * 0.25 + pow(m, 12.0) * 0.05);
    return col;
}

mirror Fresnel mirror

How much water mirrors, by Schlick's Fresnel: 2% seen from straight above, rising to nearly all of it edge-on. It takes how squarely the eye faces the surface, from 0 to 1.

Introduced in Rippling Water; also in Ocean Waves

mirror · from Rippling Water
float mirror(float facing) {
    return 0.02 + 0.98 * pow(1.0 - clamp(facing, 0.0, 1.0), 5.0);
}

through light through colored glass

The share of each color a pane of stained glass lets through, by Beer-Lambert: the glass's color raised to its thickness, which wanders with noise, and nothing where the lead is.

Introduced in Stained Glass

Calls fbm

through · from Stained Glass
float3 through(Pane g, float2 w, constant Params& p) {
    float wave = fbm(w * 30.0 + g.id * 40.0);
    float thick = p.thickness * (0.6 + 0.8 * wave);
    return pow(max(g.colour, 0.02), thick) * (1.0 - g.lead);
}

through a view through rough glass

A SwiftUI layer seen through rough glass at one point: the average of sixteen samples on a spiral around it, each the golden angle on from the last, out to a radius in points; the last argument turns the spiral.

Introduced in Frosted Glass

through · from Frosted Glass
half4 through(SwiftUI::Layer layer, float2 at, float radius, float turn) {
    half4 sum = half4(0.0h);
    for (int i = 0; i < 16; i++) {
        float r = radius * sqrt((float(i) + 0.5) / 16.0);
        float a = float(i) * 2.39996 + turn;
        sum += layer.sample(at + r * float2(cos(a), sin(a)));
    }
    return sum / 16.0h;
}

Vertex cameras

cameraAxes the camera's axes

The camera's three directions as the columns of a matrix: forward from the eye to the point it looks at, right across it (crossed with straight up), and up as the cross of those two. The background turns each pixel's ray with the same three.

Introduced in Rippling Water; also in Flag in the Wind, Spinning Planet (changed), Ocean Waves, Jellyfish (changed)

cameraAxes · from Rippling Water
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

project perspective projection

A point in the world as a vertex episode's host wants it: measured along the camera's three axes from the eye, x and y scaled by the zoom (x also by the aspect), the depth from near to far mapped to 0 to 1 for the depth test, and the depth itself as w, which the host divides by so far things come out smaller.

Introduced in Rippling Water; also in Flag in the Wind (changed), Spinning Planet (changed), Ocean Waves (changed), Jellyfish (changed)

Calls cameraAxes

project · from Rippling Water
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.7, 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);
}

eyeAt the eye, moved by the pointer

Where the eye is: a fixed distance from what it looks at, moved round it left and right and up and down by where the pointer is in the picture. The middle of the picture, or no pointer, is the episode's own view from the front.

Introduced in Spinning Planet; also in Jellyfish (changed)

eyeAt · from Spinning Planet
float3 eyeAt(constant Uniforms& u) {
    float2 m = u.mouse / max(u.resolution, float2(1.0)) - 0.5;
    float yaw = -m.x * 1.4, pitch = -m.y * 0.9;
    float c = cos(pitch);
    return DISTANCE * float3(sin(yaw) * c, sin(pitch), -cos(yaw) * c);
}

Color

kiln the kiln palette

The Ray Kiln palette: a number from 0 to 1 becomes near-black, then ember, orange, gold and white-hot.

Introduced in Flowing Color Field; also in Morphing Shapes, Floating 3D Scene, Kaleidoscope, Cracked Clay, Molten Metaballs, Fractal Glow, Kiln Fire, Ember Tunnel, Particle Swarm (changed), Endless Terrain, Glass Orb, A Shader That Keeps the Beat, Curl-Noise Flow (changed), Rippling Water, Pottery Wheel, Kiln Floor Tiles, Smoke Stack, Murmuration (changed), Mandelbulb, Flag in the Wind, Aurora, Rain on Glass, Spinning Planet, Pulse Synth, Lightning, Black Hole, Crystal Bloom, Ocean Waves, Stained Glass, Fireflies (changed), Clockwork, Menger Sponge, Wind Chimes, Soap Bubble, Crawling Glaze, Lorenz Attractor (changed), Penrose Tiles, Jellyfish, Hall of Mirrors, Pine Cone, Topographic Map, Fireworks (changed), Copper Kettle

kiln · from Flowing Color Field
float3 kiln(float t) {
    float3 c0 = float3(0.020, 0.012, 0.020);
    float3 c1 = float3(0.280, 0.040, 0.030);
    float3 c2 = float3(0.880, 0.260, 0.050);
    float3 c3 = float3(1.000, 0.680, 0.200);
    float3 c4 = float3(1.000, 0.970, 0.840);
    t = clamp(t, 0.0, 1.0);
    float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
    c = mix(c, c2, smoothstep(0.22, 0.50, t));
    c = mix(c, c3, smoothstep(0.50, 0.76, t));
    return mix(c, c4, smoothstep(0.76, 1.00, t));
}

Sound

voice a voice with overtones

A tone with its next three overtones. ph is the phase in cycles; bright goes from 0, a pure sine, to 1, a fuller and reedier sound.

Introduced in Floating 3D Scene; also in Molten Metaballs, Aurora (changed)

voice · from Floating 3D Scene
float voice(float ph, float bright) {
    const float tau = 6.2831853;
    return sin(tau * ph) + bright * (0.45 * sin(2.0 * tau * ph) + 0.25 * sin(3.0 * tau * ph) + 0.12 * sin(4.0 * tau * ph));
}

morphWave a morphing wave

One wave blending a sine, a square and a triangle, by the three weights in w. The square and triangle are built from their first four odd harmonics, so they stay soft.

Introduced in Morphing Shapes

morphWave · from Morphing Shapes
float morphWave(float ph, float3 w) {
    const float tau = 6.2831853;
    float sq = 0.0, tri = 0.0;
    for (int n = 1; n <= 7; n += 2) {
        float s = sin(tau * float(n) * ph);
        sq += s / float(n);
        tri += s * ((n / 2) % 2 == 0 ? 1.0 : -1.0) / float(n * n);
    }
    return w.x * sin(tau * ph) + w.y * sq * 0.75 + w.z * tri * 0.81;
}

scaleNote a note of the scale

The frequency of note i of a pentatonic scale, read from a table of hertz and clamped to its ends, so a melody is a list of whole numbers.

Introduced in Kaleidoscope; also in Pulse Synth (changed), Crystal Bloom, Stained Glass, Pine Cone

scaleNote · from Kaleidoscope
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)];
}

pluck a plucked note

A plucked note: a sine and its octave that start fast and die away. since is the time since the note began.

Introduced in Kaleidoscope; also in Fractal Glow (changed)

pluck · from Kaleidoscope
float pluck(float f, float since, float t) {
    float env = (1.0 - exp(-since * 250.0)) * exp(-since * 5.0);
    return (sin(TAU * f * t) + 0.3 * sin(TAU * 2.0 * f * t)) * env;
}

thunder

Thunder after a lightning strike: low noise from noise1 that swells in and rolls away over a few seconds. x is the time since the strike (Rain on Glass) or since its sound arrived (Lightning, where a near strike cracks first and a far one rumbles lower and longer).

Introduced in Rain on Glass; also in Lightning (changed)

Calls noise1

thunder · from Rain on Glass
float thunder(float t, float x) {
    float y = x - 1.2;
    if (y <= 0.0) return 0.0;
    float env = (1.0 - exp(-y * 4.0)) * exp(-y * 0.9);
    float rumble = noise1(t, 70.0, 11u) * 0.6 + noise1(t, 140.0, 12u) * 0.3
                 + noise1(t, 35.0, 13u) * 0.5;
    return rumble * env;
}

bell a bell

A struck bell: a tone and two partials at 2.76 and 5.4 times its pitch, which are not whole multiples of it, the higher ones dying away sooner. since is the time since it was struck.

Introduced in Stained Glass; also in Penrose Tiles (changed)

bell · from Stained Glass
float bell(float f, float since) {
    float a = 1.0 - exp(-since * 400.0);
    float s = sin(TAU * f * since) * exp(-since * 2.5);
    s += 0.5 * sin(TAU * f * 2.76 * since) * exp(-since * 4.0);
    s += 0.25 * sin(TAU * f * 5.40 * since) * exp(-since * 7.0);
    return a * s;
}

The words used here, such as distance field or octave, are explained in the glossary.