Fireworks
A summer night across a field, and shells rising from behind the trees to burst into 256 stars each, every star trailing sparks. Each is a particle stepped sixty times a second, flying out, slowed by the air, pulled down by gravity, cooling through the kiln's colors and glittering as it burns out. The shells burst at different distances, the far ones smaller and lower, and you hear each bang only after its sound has crossed that distance: between 1.4 and 2.3 seconds after the flash.
· Particles & Simulation · Compute shader
Teaches Bursts of particles under gravity, a bang that arrives late
Builds on Cracked Clay (integer hash, sound noise), Spiral Galaxy (fit to the picture), Lightning, Lorenz Attractor
Watch
The idea
A firework shell is a ball of stars around a charge. Fired from a mortar, it rises, slowing as it goes, and at the top a fuse sets off the charge, which throws the stars out in every direction. Each star is a pellet that burns as it flies: the air slows it at once, gravity bends its path down, and it cools and goes out a few seconds later. Every star is a particle: a few numbers on the GPU, moved on a small step at a time, sixty times a second. It is the same machinery as Lorenz Attractor: a buffer of particles, a compute kernel that steps each one, each drawn as a dot into an image that fades.
The burst reaches you as light, at once, and as sound, at 343 meters a second: about three seconds for every kilometer. So the bang comes late, as the thunder does in Lightning. Here every shell bursts at a distance of its own, and the sound waits for each one. It is built in five steps.
Stage 1 · A burst
One shell, 24,576 particles. Each is a struct Particle: its position and velocity in meters (across, and up from the ground), its age and how long it burns (life), how far it lags behind its star, and a random seed. The first 256 particles are the stars; the other 24,320 are their sparks, 95 for each star.
Every four seconds, from half a second in, update sees a new burst start and sends every particle out from the same point, 135 m up, with a direction of its star's. That direction is a point on a sphere seen flat: a height z from −1 to 1 and an angle round, both from the integer hash, so the stars crowd toward the rim of the burst as they do in the sky. A star leaves at 21 m/s; a spark at between half that and all of it, so the sparks string out behind their star into a line. Each burns for 3.6 to 4.4 seconds times burn, a spark less the more it lags. Then each step only moves it on by its velocity: nothing slows it yet, so the burst grows at the same rate until it burns out.
seenAt draws it as from 480 m away: FOCAL (3) times the meters over the distance, from a horizon 0.84 of the half height below the middle. ink colors a star by its heat, 1 at the burst and 0 when it is out, through the gold part of the kiln palette, near white to orange; a star's head is drawn bigger than a spark and at least ten times as bright, and a spark is fainter the further it lags. A particle that is burnt out goes off the picture, where nothing is drawn. fade keeps trails (90 %) of the last step's image, so each star leaves a streak.
stage-1-a-burst.metal
// Stage 1 · A burst
// A shell bursts in the night: 256 stars fly out from one point, each its
// own way, each with a trail of sparks that leave a little slower. Each
// particle is moved on sixty times a second, and nothing slows them yet.
// It bursts again every four seconds.
//
// @particles 24576
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
constant float TAU = 6.2831853;
constant float EVERY = 4.0;
// A point FAR meters away is drawn FOCAL x / FAR across and FOCAL y / FAR
// above the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
constant float FAR = 480.0;
constant float2 BURST = float2(0.0, 135.0);
constant float SPEED = 21.0;
// The first STARS particles are stars; the rest are sparks.
constant uint STARS = 256;
// A particle: its place and velocity in meters, its age and how long it
// burns, and how far it lags behind its star.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Before the first burst every particle is burnt out.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = BURST;
q.velocity = float2(0.0);
q.age = 1.0;
q.life = 0.0;
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. At a burst each particle starts again from the middle.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = u.time - 0.5;
float burst = floor(x / EVERY);
if (x >= 0.0 && burst != floor((x - dt) / EVERY)) {
// Its star's direction on a sphere, seen flat: the stars crowd
// its rim. A spark leaves slower, so it lags behind.
uint n = (id % STARS) * 3u + uint(burst) * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = id < STARS ? 0.0 : pow(q.seed, 0.8);
q.position = BURST;
q.velocity = dir * SPEED * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
}
q.position += q.velocity * dt;
q.age += dt;
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// Where a point, in meters, is on the picture.
float2 seenAt(float2 m, constant Uniforms& u) {
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / FAR;
}
// Where to draw it and how big. A burnt-out particle goes off the
// picture, where nothing is drawn. A star is bigger than its sparks.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
if (q.age > q.life) return float3(-1.0, -1.0, 0.0);
float aspect = u.resolution.x / u.resolution.y;
float2 spot = seenAt(q.position, u);
float size = q.lag == 0.0 ? 0.007 : 0.0045;
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A star's light: near white as it bursts, cooling through gold and orange
// as it burns out. A spark is fainter the further it lags. Longer trails
// keep more of each step, so each dot gives less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
float heat = 1.0 - q.age / q.life;
float3 c = kiln(mix(0.40, 0.92, pow(heat, 0.6)));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture: the light, rolled off so it never clips, on the dark.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float3 light = trails.at(uv).rgb;
float3 c = 1.0 - exp(-light * 1.4);
float2 v = uv - 0.5;
c += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(c, 1.0);
}Stage 2 · Air and gravity
Now the stars leave at 108 m/s, and two lines in update act on them each step. Gravity takes gravity × dt off the upward speed, 9.8 m/s every second by default. The air takes a part of all the speed: the velocity is multiplied by exp(−drag × dt), so with drag at 1.2 a star keeps 30 % of its speed after a second and 9 % after two. Without gravity a star would coast to a stop 108 / 1.2, 90 m, from where it started; with it, each one ends up falling at gravity / drag, about 8 m/s, so the burst flies out fast, stops growing, and droops.
This is the simplest way of stepping a motion, a form of Euler's method: the velocity is changed first, then the position moved on by the new velocity, one sixtieth of a second at a time.
stage-2-air-and-gravity.metal3 new lines since stage 1
// Stage 2 · Air and gravity
// Now the stars leave more than five times as fast, and two things act
// on them at every step. Gravity takes 9.8 meters a second off their
// upward speed each second, and the air takes a part of all their speed.
// They burst wide, slow down, and droop.
//
// @particles 24576
// @param gravity 9.8 0.0 20.0 How fast gravity pulls the stars down, in meters a second each second
// @param drag 1.2 0.3 4.0 How fast the air slows the stars, a fraction of their speed each second
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
constant float TAU = 6.2831853;
constant float EVERY = 4.0;
// A point FAR meters away is drawn FOCAL x / FAR across and FOCAL y / FAR
// above the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
constant float FAR = 480.0;
constant float2 BURST = float2(0.0, 135.0);
constant float SPEED = 108.0;
// The first STARS particles are stars; the rest are sparks.
constant uint STARS = 256;
// A particle: its place and velocity in meters, its age and how long it
// burns, and how far it lags behind its star.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Before the first burst every particle is burnt out.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = BURST;
q.velocity = float2(0.0);
q.age = 1.0;
q.life = 0.0;
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. At a burst each particle starts again from the middle.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = u.time - 0.5;
float burst = floor(x / EVERY);
if (x >= 0.0 && burst != floor((x - dt) / EVERY)) {
// Its star's direction on a sphere, seen flat: the stars crowd
// its rim. A spark leaves slower, so it lags behind.
uint n = (id % STARS) * 3u + uint(burst) * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = id < STARS ? 0.0 : pow(q.seed, 0.8);
q.position = BURST;
q.velocity = dir * SPEED * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
}
// Gravity pulls it down, the air takes a part of its speed, and it
// moves on.
q.velocity.y -= p.gravity * dt;
q.velocity *= exp(-p.drag * dt);
q.position += q.velocity * dt;
q.age += dt;
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// Where a point, in meters, is on the picture.
float2 seenAt(float2 m, constant Uniforms& u) {
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / FAR;
}
// Where to draw it and how big. A burnt-out particle goes off the
// picture, where nothing is drawn. A star is bigger than its sparks.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
if (q.age > q.life) return float3(-1.0, -1.0, 0.0);
float aspect = u.resolution.x / u.resolution.y;
float2 spot = seenAt(q.position, u);
float size = q.lag == 0.0 ? 0.007 : 0.0045;
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A star's light: near white as it bursts, cooling through gold and orange
// as it burns out. A spark is fainter the further it lags. Longer trails
// keep more of each step, so each dot gives less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
float heat = 1.0 - q.age / q.life;
float3 c = kiln(mix(0.40, 0.92, pow(heat, 0.6)));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture: the light, rolled off so it never clips, on the dark.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float3 light = trails.at(uv).rgb;
float3 c = 1.0 - exp(-light * 1.4);
float2 v = uv - 0.5;
c += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(c, 1.0);
}1 line from stage 1 that are gone
constant float SPEED = 21.0;Stage 3 · A night of shells
Now there are 196,608 particles: eight slots of 24,576, each slot one shell's worth. shellOf makes shell k from seven hashes of its number: when it is launched (k + 0.1 to k + 0.6 times every seconds, so one about every two and a half seconds, but the first at 0.1 s, so the show opens with a burst), how long it rises (1.3 to 1.8 s), how high it bursts (125 to 145 m), where across (anywhere within 0.8 of the half height of the middle), how far it leans (±4 m/s sideways), how fast its stars leave (100 to 120 m/s) and its color. Each color is its own stretch of the kiln palette (colorOf): gold, red, cream that starts white-hot, and orange.
Shell k uses slot k mod 8. In update each particle checks the shells of its slot: at a launch it becomes part of a rocket, kind 1, at the foot of the shell's place, with the shell's launch speed, which is just fast enough to be at its height when it bursts ((height + ½ gravity rise²) / rise). While rising it is moved by gravity alone, worked out exactly for the step. All 24,576 particles of the slot rise as one, and only the first is drawn: the rocket's head. At the burst each becomes a star or a spark, as in the stages before, starting from burstOf: the shell's height, as far across as it leaned on the way up, which is where the rocket has risen to.
shellCount allows (60 − 4 − 4 × burn) / every shells a minute, rounded down: 20 at the defaults, the last launched at 48.0 s, bursting at 49.8 s and out before 54.3 s. The schedule is read from the time within the minute, so the show repeats every minute, and since every shell is out a few seconds before the end, the picture at 60 s is the dark sky it starts with. Behind the shells, skyAt is near-black, a little warmer at the horizon, with a few faint stars; in front, a line of trees made of sines raised to high powers, which the rockets rise from behind, and the darker field we watch from.
stage-3-a-night-of-shells.metal112 new lines since stage 2
// Stage 3 · A night of shells
// Eight shells' worth of particles now. Each shell is a rocket that rises
// from behind the trees and bursts. When, where, how high and in which
// colors is a hash of its number, so the show is the same every minute.
// The sky and trees come in.
//
// @particles 196608
// @param every 2.5 1.5 8.0 Seconds between shells, on average
// @param gravity 9.8 0.0 20.0 How fast gravity pulls the stars down, in meters a second each second
// @param drag 1.2 0.3 4.0 How fast the air slows the stars, a fraction of their speed each second
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
constant float TAU = 6.2831853;
// The show repeats every LOOP seconds, ending in a moment of dark.
constant float LOOP = 60.0;
// A point x meters across and y up, FAR meters away, is drawn FOCAL x / FAR
// and FOCAL y / FAR from the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
constant float FAR = 560.0;
// SLOTS shells can be in the air at once, of PER particles each: STARS
// stars, each with a trail of sparks.
constant uint SLOTS = 8;
constant uint PER = 24576;
constant uint STARS = 256;
// A particle: where it is in meters (across, and up from the ground), its
// speed, its age and how long it burns, its shell, what it is doing (0
// waiting, 1 rising, 2 burning), its place in the shell, how far it lags
// behind its star, and a random number.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float shell;
float kind;
float index;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// A shell: when it is launched, how long it rises, how high it bursts,
// where across, how fast it leaves the ground and leans, how fast its
// stars fly out, and its color. All from hashes of its number,
// the same every loop.
struct Shell {
float launch;
float rise;
float height;
float across;
float2 speed;
float stars;
uint color;
};
Shell shellOf(uint k, constant Params& p) {
Shell s;
uint n = k * 8u + 5251u;
s.launch = (float(k) + 0.1 + 0.5 * hashU(n)) * p.every;
// The first goes up at once.
if (k == 0u) s.launch = 0.1;
s.rise = mix(1.3, 1.8, hashU(n + 1u));
// Across the picture's middle.
s.across = (hashU(n + 3u) * 2.0 - 1.0) * 0.8 * FAR / FOCAL;
s.height = mix(125.0, 145.0, hashU(n + 5u));
// Launched just fast enough to be at its height when it bursts.
float up = (s.height + 0.5 * p.gravity * s.rise * s.rise) / s.rise;
s.speed = float2((hashU(n + 4u) - 0.5) * 8.0, up);
s.stars = mix(100.0, 120.0, hashU(n + 6u));
s.color = uint(hashU(n + 7u) * 3.999);
return s;
}
// How many shells fit in a loop, the last burnt out before it ends.
uint shellCount(constant Params& p) {
return uint(max(floor((LOOP - 4.0 - 4.0 * p.burn) / p.every), 1.0));
}
// Where the shell bursts: at its height, as far across as it leaned.
float2 burstOf(Shell s, constant Params& p) {
return float2(s.across + s.speed.x * s.rise, s.height);
}
// Everything waits for its shell.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = float2(0.0);
q.velocity = float2(0.0);
q.age = 0.0;
q.life = 1.0;
q.shell = 0.0;
q.kind = 0.0;
q.index = float(id % PER);
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. A shell's particles are launched together as one rocket and
// all rise as it does; when it bursts each becomes a star of its own.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = fmod(u.time, LOOP);
float before = x - dt;
uint j = id % PER;
uint count = shellCount(p);
for (uint k = id / PER; k < count; k += SLOTS) {
Shell s = shellOf(k, p);
if (before < s.launch && s.launch <= x) {
q.position = float2(s.across, 0.0);
q.velocity = s.speed;
q.shell = float(k);
q.kind = 1.0;
}
float at = s.launch + s.rise;
if (q.kind == 1.0 && before < at && at <= x) {
// Its star's direction on a sphere, seen flat: the stars
// crowd its rim. A spark leaves slower, so it lags behind.
uint n = (j % STARS) * 3u + k * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = j < STARS ? 0.0 : pow(q.seed, 0.8);
float speed = s.stars * (0.92 + 0.08 * hashU(n + 2u));
q.position = burstOf(s, p);
q.velocity = dir * speed * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
q.kind = 2.0;
}
}
if (q.kind == 1.0) {
// Rising: gravity alone, worked out exactly for the step.
q.position += q.velocity * dt;
q.position.y -= 0.5 * p.gravity * dt * dt;
q.velocity.y -= p.gravity * dt;
} else if (q.kind == 2.0) {
// Burning: gravity pulls it down, the air takes a part of its
// speed, and it moves on.
q.velocity.y -= p.gravity * dt;
q.velocity *= exp(-p.drag * dt);
q.position += q.velocity * dt;
q.age += dt;
if (q.age > q.life) q.kind = 0.0;
}
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// Where a point, in meters, is on the picture.
float2 seenAt(float2 m, constant Uniforms& u) {
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / FAR;
}
// Where to draw it and how big. Waiting particles, and a rocket's but its
// first, go off the picture, where nothing is drawn. A star's head is
// bigger than its sparks.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
bool head = q.kind == 1.0 && q.index == 0.0;
if (q.kind == 0.0 || (q.kind == 1.0 && !head)) {
return float3(-1.0, -1.0, 0.0);
}
float aspect = u.resolution.x / u.resolution.y;
float size = q.lag == 0.0 ? 0.007 : 0.0045;
float2 spot = seenAt(q.position, u);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A shell's color: its own stretch of the kiln palette, from where its
// stars start (heat 1) to where they burn out (heat 0). Gold, red, cream
// and orange shells.
float3 colorOf(uint c, float heat) {
float2 span = float2(0.40, 0.92);
if (c == 1u) span = float2(0.26, 0.62);
if (c == 2u) span = float2(0.50, 1.00);
if (c == 3u) span = float2(0.32, 0.80);
return kiln(mix(span.x, span.y, heat));
}
// A star's light: hottest as it bursts, cooling through its shell's colors
// toward ember as it burns out. A spark is fainter the further it lags.
// Longer trails keep more of each step, so each dot gives less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
Shell s = shellOf(uint(q.shell), p);
if (q.kind == 1.0) return float4(kiln(0.85) * soft * 0.5, 0.0);
float heat = 1.0 - q.age / q.life;
float3 c = colorOf(s.color, pow(heat, 0.6));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The night: near-black overhead, a little warmer down at the trees, with
// a few faint stars.
float3 skyAt(float2 uv, float aspect) {
float3 low = float3(0.070, 0.042, 0.050);
float3 high = float3(0.014, 0.011, 0.020);
float3 c = mix(low, high, smoothstep(0.08, 0.8, uv.y));
float2 g = uv * float2(aspect, 1.0) * 80.0;
float2 cell = floor(g);
float h = hashU(uint(cell.x + 4000.0) * 977u + uint(cell.y) * 131u);
float star = smoothstep(0.25, 0.0, length(fract(g) - 0.5));
c += float3(0.30, 0.28, 0.26) * step(0.994, h) * star;
return c;
}
// The picture: the sky, the stars' light rolled off so it never clips,
// and in front, the trees and the field we watch from.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float3 c = skyAt(uv, aspect);
float3 light = trails.at(uv).rgb;
c += 1.0 - exp(-light * 1.4);
// The trees along the horizon, and the field nearer.
float x = s.x;
float trees = HORIZON + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
trees += 0.05 * pow(abs(sin(x * 13.0 + 1.0)), 6.0);
trees += 0.035 * pow(abs(sin(x * 29.0 + 2.0)), 10.0);
float3 dark = float3(0.010, 0.009, 0.012);
c = mix(c, dark, smoothstep(0.004, -0.004, s.y - trees));
float field = HORIZON - 0.06 - 0.05 * sin(x * 0.9 + 0.5);
float3 grass = float3(0.016, 0.013, 0.014);
c = mix(c, grass, smoothstep(0.004, -0.004, s.y - field));
float2 v = uv - 0.5;
c *= 1.0 - dot(v, v) * 0.5;
// A little noise, so the dark gradients don't show bands.
uint2 px = uint2(uv * u.resolution);
c += (hashU(px.x * 1973u + px.y * 9277u) - 0.5) / 255.0;
return float4(c, 1.0);
}19 lines from stage 2 that are gone
constant float EVERY = 4.0;
⋯
constant float FAR = 480.0;
constant float2 BURST = float2(0.0, 135.0);
constant float SPEED = 108.0;
⋯
q.position = BURST;
⋯
q.age = 1.0;
q.life = 0.0;
⋯
float x = u.time - 0.5;
float burst = floor(x / EVERY);
if (x >= 0.0 && burst != floor((x - dt) / EVERY)) {
⋯
uint n = (id % STARS) * 3u + uint(burst) * 104729u;
⋯
q.lag = id < STARS ? 0.0 : pow(q.seed, 0.8);
q.position = BURST;
q.velocity = dir * SPEED * (1.0 - 0.5 * q.lag);
⋯
if (q.age > q.life) return float3(-1.0, -1.0, 0.0);
⋯
float2 spot = seenAt(q.position, u);
⋯
float3 c = kiln(mix(0.40, 0.92, pow(heat, 0.6)));
⋯
float3 c = 1.0 - exp(-light * 1.4);
⋯
c += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);Stage 4 · Near and far
Each shell now bursts at a distance of its own, far: 0.75 to 1.4 times distance, from 413 to 770 m at the default 0.55 km. seenAt divides by it, so a far shell is drawn smaller, and lower, nearer the horizon, as it is in the sky. Where a shell bursts across is chosen in the picture's terms, so the far ones don't all gather in the middle. A star's light is multiplied by 420 / far, held between 0.3 and 1.5, so a far shell's stars are fainter too.
litBy adds the light each burst throws round it: for each shell in the air, the burst's color, rising over 0.3 s after it bursts and dying away over a few seconds, falling off with distance on the picture from where it burst and brighter for a near shell. The sky gets a tenth of it, the trees and the field less. It is kept faint, and every star's light rises over its first 0.15 s rather than at once, so no burst is a flash: the show passes Ray Kiln's flash check over its whole minute.
stage-4-near-and-far.metal32 new lines since stage 3
// Stage 4 · Near and far
// Each shell now bursts at a distance of its own, from 0.75 to 1.4 times
// distance. The farther it is, the smaller and lower it is drawn and the
// fainter its stars. Each burst also lights the sky round it, and the
// trees and the field, for a few seconds.
//
// @particles 196608
// @param every 2.5 1.5 8.0 Seconds between shells, on average
// @param distance 0.55 0.5 1.5 How far away the shells burst, in kilometers: each its own, from 0.75 to 1.4 times this
// @param gravity 9.8 0.0 20.0 How fast gravity pulls the stars down, in meters a second each second
// @param drag 1.2 0.3 4.0 How fast the air slows the stars, a fraction of their speed each second
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
constant float TAU = 6.2831853;
// The show repeats every LOOP seconds, ending in a moment of dark.
constant float LOOP = 60.0;
// A point x meters across and y up, d meters away, is drawn FOCAL x / d
// and FOCAL y / d from the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
// SLOTS shells can be in the air at once, of PER particles each: STARS
// stars, each with a trail of sparks.
constant uint SLOTS = 8;
constant uint PER = 24576;
constant uint STARS = 256;
// A particle: where it is in meters (across, and up from the ground), its
// speed, its age and how long it burns, its shell, what it is doing (0
// waiting, 1 rising, 2 burning), its place in the shell, how far it lags
// behind its star, and a random number.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float shell;
float kind;
float index;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// A shell: when it is launched, how long it rises, how high it bursts, how
// far away it is, where across, how fast it leaves the ground and leans,
// how fast its stars fly out, and its color. All from hashes of its number,
// the same every loop.
struct Shell {
float launch;
float rise;
float height;
float far;
float across;
float2 speed;
float stars;
uint color;
};
Shell shellOf(uint k, constant Params& p) {
Shell s;
uint n = k * 8u + 5251u;
s.launch = (float(k) + 0.1 + 0.5 * hashU(n)) * p.every;
// The first goes up at once.
if (k == 0u) s.launch = 0.1;
s.rise = mix(1.3, 1.8, hashU(n + 1u));
s.far = 1000.0 * p.distance * mix(0.75, 1.4, hashU(n + 2u));
// Across the picture's middle, whatever the distance.
s.across = (hashU(n + 3u) * 2.0 - 1.0) * 0.8 * s.far / FOCAL;
s.height = mix(125.0, 145.0, hashU(n + 5u));
// Launched just fast enough to be at its height when it bursts.
float up = (s.height + 0.5 * p.gravity * s.rise * s.rise) / s.rise;
s.speed = float2((hashU(n + 4u) - 0.5) * 8.0, up);
s.stars = mix(100.0, 120.0, hashU(n + 6u));
s.color = uint(hashU(n + 7u) * 3.999);
return s;
}
// How many shells fit in a loop, the last burnt out before it ends.
uint shellCount(constant Params& p) {
return uint(max(floor((LOOP - 4.0 - 4.0 * p.burn) / p.every), 1.0));
}
// Where the shell bursts: at its height, as far across as it leaned.
float2 burstOf(Shell s, constant Params& p) {
return float2(s.across + s.speed.x * s.rise, s.height);
}
// Everything waits for its shell.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = float2(0.0);
q.velocity = float2(0.0);
q.age = 0.0;
q.life = 1.0;
q.shell = 0.0;
q.kind = 0.0;
q.index = float(id % PER);
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. A shell's particles are launched together as one rocket and
// all rise as it does; when it bursts each becomes a star of its own.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = fmod(u.time, LOOP);
float before = x - dt;
uint j = id % PER;
uint count = shellCount(p);
for (uint k = id / PER; k < count; k += SLOTS) {
Shell s = shellOf(k, p);
if (before < s.launch && s.launch <= x) {
q.position = float2(s.across, 0.0);
q.velocity = s.speed;
q.shell = float(k);
q.kind = 1.0;
}
float at = s.launch + s.rise;
if (q.kind == 1.0 && before < at && at <= x) {
// Its star's direction on a sphere, seen flat: the stars
// crowd its rim. A spark leaves slower, so it lags behind.
uint n = (j % STARS) * 3u + k * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = j < STARS ? 0.0 : pow(q.seed, 0.8);
float speed = s.stars * (0.92 + 0.08 * hashU(n + 2u));
q.position = burstOf(s, p);
q.velocity = dir * speed * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
q.kind = 2.0;
}
}
if (q.kind == 1.0) {
// Rising: gravity alone, worked out exactly for the step.
q.position += q.velocity * dt;
q.position.y -= 0.5 * p.gravity * dt * dt;
q.velocity.y -= p.gravity * dt;
} else if (q.kind == 2.0) {
// Burning: gravity pulls it down, the air takes a part of its
// speed, and it moves on.
q.velocity.y -= p.gravity * dt;
q.velocity *= exp(-p.drag * dt);
q.position += q.velocity * dt;
q.age += dt;
if (q.age > q.life) q.kind = 0.0;
}
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// Where a point of a shell's, in meters, is on the picture.
float2 seenAt(float2 m, float far, constant Uniforms& u) {
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / far;
}
// Where to draw it and how big. Waiting particles, and a rocket's but its
// first, go off the picture, where nothing is drawn. A star's head is
// bigger than its sparks.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
bool head = q.kind == 1.0 && q.index == 0.0;
if (q.kind == 0.0 || (q.kind == 1.0 && !head)) {
return float3(-1.0, -1.0, 0.0);
}
float aspect = u.resolution.x / u.resolution.y;
Shell s = shellOf(uint(q.shell), p);
float size = q.lag == 0.0 ? 0.007 : 0.0045;
float2 spot = seenAt(q.position, s.far, u);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A shell's color: its own stretch of the kiln palette, from where its
// stars start (heat 1) to where they burn out (heat 0). Gold, red, cream
// and orange shells.
float3 colorOf(uint c, float heat) {
float2 span = float2(0.40, 0.92);
if (c == 1u) span = float2(0.26, 0.62);
if (c == 2u) span = float2(0.50, 1.00);
if (c == 3u) span = float2(0.32, 0.80);
return kiln(mix(span.x, span.y, heat));
}
// A star's light: hottest as it bursts, cooling through its shell's colors
// toward ember as it burns out. A near shell's stars look brighter.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
Shell s = shellOf(uint(q.shell), p);
float near = clamp(420.0 / s.far, 0.3, 1.5);
if (q.kind == 1.0) {
return float4(kiln(0.85) * soft * near * 0.5, 0.0);
}
float heat = 1.0 - q.age / q.life;
float3 c = colorOf(s.color, pow(heat, 0.6));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * near * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The night: near-black overhead, a little warmer down at the trees, with
// a few faint stars.
float3 skyAt(float2 uv, float aspect) {
float3 low = float3(0.070, 0.042, 0.050);
float3 high = float3(0.014, 0.011, 0.020);
float3 c = mix(low, high, smoothstep(0.08, 0.8, uv.y));
float2 g = uv * float2(aspect, 1.0) * 80.0;
float2 cell = floor(g);
float h = hashU(uint(cell.x + 4000.0) * 977u + uint(cell.y) * 131u);
float star = smoothstep(0.25, 0.0, length(fract(g) - 0.5));
c += float3(0.30, 0.28, 0.26) * step(0.994, h) * star;
return c;
}
// The light each shell's burst throws on the sky, the trees and the field:
// up as it bursts, gone in a few seconds, wide round where it burst.
float3 litBy(float2 s, constant Uniforms& u, constant Params& p) {
float x = fmod(u.time, LOOP);
uint count = shellCount(p);
float3 sum = float3(0.0);
for (uint k = 0; k < count; k++) {
Shell sh = shellOf(k, p);
float a = x - sh.launch - sh.rise;
if (a < 0.0 || a > 4.0 * p.burn) continue;
float env = smoothstep(0.0, 0.3, a) * exp(-a * 1.3 / p.burn);
float2 b = seenAt(burstOf(sh, p), sh.far, u);
float near = clamp(420.0 / sh.far, 0.3, 1.5);
float3 c = colorOf(sh.color, 0.8);
sum += c * env * near * exp(-length(s - b) * 1.6);
}
return sum * p.glow;
}
// The picture: the sky and its light, the stars' light rolled off so it
// never clips, and in front, the trees and the field we watch from.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float3 lit = litBy(s, u, p);
float3 c = skyAt(uv, aspect) + lit * 0.1;
float3 light = trails.at(uv).rgb;
c += 1.0 - exp(-light * 1.4);
// The trees along the horizon, and the field nearer.
float x = s.x;
float trees = HORIZON + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
trees += 0.05 * pow(abs(sin(x * 13.0 + 1.0)), 6.0);
trees += 0.035 * pow(abs(sin(x * 29.0 + 2.0)), 10.0);
float3 dark = float3(0.010, 0.009, 0.012) + lit * 0.012;
c = mix(c, dark, smoothstep(0.004, -0.004, s.y - trees));
float field = HORIZON - 0.06 - 0.05 * sin(x * 0.9 + 0.5);
float3 grass = float3(0.016, 0.013, 0.014) + lit * 0.03;
c = mix(c, grass, smoothstep(0.004, -0.004, s.y - field));
float2 v = uv - 0.5;
c *= 1.0 - dot(v, v) * 0.5;
// A little noise, so the dark gradients don't show bands.
uint2 px = uint2(uv * u.resolution);
c += (hashU(px.x * 1973u + px.y * 9277u) - 0.5) / 255.0;
return float4(c, 1.0);
}10 lines from stage 3 that are gone
constant float FAR = 560.0;
⋯
s.across = (hashU(n + 3u) * 2.0 - 1.0) * 0.8 * FAR / FOCAL;
⋯
float2 seenAt(float2 m, constant Uniforms& u) {
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / FAR;
⋯
float2 spot = seenAt(q.position, u);
⋯
if (q.kind == 1.0) return float4(kiln(0.85) * soft * 0.5, 0.0);
⋯
float k = light * p.glow * (1.0 - p.trails) * 0.3;
⋯
float3 c = skyAt(uv, aspect);
⋯
float3 dark = float3(0.010, 0.009, 0.012);
⋯
float3 grass = float3(0.016, 0.013, 0.014);Stage 5 · Fireworks
Late in its life, once its heat is under 0.4, a star glitters. Every four steps, fifteen times a second, the hash of its number and the step decides whether it is drawn 2.2 times as bright (a chance of 35 %) or 0.35 times, which averages out to its light as it was, so the picture as a whole doesn't flicker. The rocket gets a tail: the 31 particles after its head are drawn behind it along its path, 0.012 s further back each, scattered a little more the further back they are, and fainter. And in a picture taller than wide, the dots are drawn smaller along with the show, and horizonOf raises the horizon, so on a phone the show stays near the middle and looks like the wide one.
The rest is in the sound, below: each burst's bang, late. The bursts in the first twenty seconds at the defaults, with when each bang starts in the rendered sound (the start of the loudest sound in the seconds after each flash) and the straight-line distance:
- A cream shell bursts at 1.65 s, 513 m away; its bang starts at 3.14 s, 1.50 s late.
- A red shell at 4.91 s, 769 m away; its bang at 7.15 s, 2.24 s late.
- A red shell at 7.38 s, 538 m away; its bang at 8.94 s, 1.56 s late.
- An orange shell at 9.50 s, 576 m away; its bang at 11.18 s, 1.68 s late.
- A cream shell at 12.14 s, 556 m away; its bang at 13.77 s, 1.62 s late.
- A gold shell at 14.17 s, 491 m away; its bang at 15.61 s, 1.44 s late.
- A gold shell at 17.46 s, 755 m away; its bang at 19.66 s, 2.20 s late.
Each is the burst's straight-line distance over 343 m/s, to within 5 ms. Over the whole minute the delays run from 1.37 s, for a red shell 471 m away, to 2.25 s for a gold one 773 m away.
the kiln palette and shade(), from the finished shader
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// The picture: the sky and its light, the stars' light rolled off so it
// never clips, and in front, the trees and the field we watch from.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float3 lit = litBy(s, u, p);
float3 c = skyAt(uv, aspect) + lit * 0.1;
float3 light = trails.at(uv).rgb;
c += 1.0 - exp(-light * 1.4);
// The trees along the horizon, and the field nearer.
float x = s.x;
float h = horizonOf(u);
float trees = h + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
trees += 0.05 * pow(abs(sin(x * 13.0 + 1.0)), 6.0);
trees += 0.035 * pow(abs(sin(x * 29.0 + 2.0)), 10.0);
float3 dark = float3(0.010, 0.009, 0.012) + lit * 0.012;
c = mix(c, dark, smoothstep(0.004, -0.004, s.y - trees));
float field = h - 0.06 - 0.05 * sin(x * 0.9 + 0.5);
float3 grass = float3(0.016, 0.013, 0.014) + lit * 0.03;
c = mix(c, grass, smoothstep(0.004, -0.004, s.y - field));
float2 v = uv - 0.5;
c *= 1.0 - dot(v, v) * 0.5;
// A little noise, so the dark gradients don't show bands.
uint2 px = uint2(uv * u.resolution);
c += (hashU(px.x * 1973u + px.y * 9277u) - 0.5) / 255.0;
return float4(c, 1.0);
}fireworks.metal, the whole file87 new lines since stage 4
// Ray Kiln · Fireworks
//
// @episode Fireworks
// @length 60
// @variation 30
// @variations 1,2,3,5,6,7,8,11
// @sweeps every, drag, distance
// @still 15.9
// @teaches Bursts of particles under gravity, a bang that arrives late
// @category Compute
// @tags fireworks, particles, compute shaders, gravity, drag, numerical integration, speed of sound, trails, procedural sound
// @final Fireworks | Shells near and far over the trees, their stars glittering as they burn out, each bang reaching you seconds after its flash.
//
// A summer night across a field. Shells rise from behind the trees and
// burst into 256 stars each, every star trailing sparks. Each star and
// spark is a particle that flies out at the speed the burst gave it, slows
// in the air, falls under gravity and cools as it burns out. The shells
// burst at different distances, near ones bigger and higher in the
// picture, and the light of each reaches you at once, its bang only after
// it has crossed the distance at the speed of sound.
// 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
//
// @particles 196608
// @param every 2.5 1.5 8.0 Seconds between shells, on average
// @param distance 0.55 0.5 1.5 How far away the shells burst, in kilometers: each its own, from 0.75 to 1.4 times this
// @param gravity 9.8 0.0 20.0 How fast gravity pulls the stars down, in meters a second each second
// @param drag 1.2 0.3 4.0 How fast the air slows the stars, a fraction of their speed each second
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
// @param volume 0.8 0.0 1.0 Loudness of the sound
constant float TAU = 6.2831853;
// The show repeats every LOOP seconds, ending in a moment of dark.
constant float LOOP = 60.0;
// The speed of sound in air, in meters a second.
constant float SOUND = 343.0;
// A point x meters across and y up, d meters away, is drawn FOCAL x / d
// and FOCAL y / d from the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
// SLOTS shells can be in the air at once, of PER particles each: STARS
// stars, each with a trail of sparks.
constant uint SLOTS = 8;
constant uint PER = 24576;
constant uint STARS = 256;
// The rocket's head and the sparks behind it.
constant uint TAIL = 32;
// A particle: where it is in meters (across, and up from the ground), its
// speed, its age and how long it burns, its shell, what it is doing (0
// waiting, 1 rising, 2 burning), its place in the shell, how far it lags
// behind its star, and a random number.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float shell;
float kind;
float index;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// A shell: when it is launched, how long it rises, how high it bursts, how
// far away it is, where across, how fast it leaves the ground and leans,
// how fast its stars fly out, and its color. All from hashes of its number,
// so the picture and the sound agree on every one.
struct Shell {
float launch;
float rise;
float height;
float far;
float across;
float2 speed;
float stars;
uint color;
};
Shell shellOf(uint k, constant Params& p) {
Shell s;
uint n = k * 8u + 5251u;
s.launch = (float(k) + 0.1 + 0.5 * hashU(n)) * p.every;
// The first goes up at once.
if (k == 0u) s.launch = 0.1;
s.rise = mix(1.3, 1.8, hashU(n + 1u));
s.far = 1000.0 * p.distance * mix(0.75, 1.4, hashU(n + 2u));
// Across the picture's middle, whatever the distance.
s.across = (hashU(n + 3u) * 2.0 - 1.0) * 0.8 * s.far / FOCAL;
s.height = mix(125.0, 145.0, hashU(n + 5u));
// Launched just fast enough to be at its height when it bursts.
float up = (s.height + 0.5 * p.gravity * s.rise * s.rise) / s.rise;
s.speed = float2((hashU(n + 4u) - 0.5) * 8.0, up);
s.stars = mix(100.0, 120.0, hashU(n + 6u));
s.color = uint(hashU(n + 7u) * 3.999);
return s;
}
// How many shells fit in a loop, the last burnt out before it ends.
uint shellCount(constant Params& p) {
return uint(max(floor((LOOP - 4.0 - 4.0 * p.burn) / p.every), 1.0));
}
// Where the shell bursts: at its height, as far across as it leaned.
float2 burstOf(Shell s, constant Params& p) {
return float2(s.across + s.speed.x * s.rise, s.height);
}
// Everything waits for its shell.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = float2(0.0);
q.velocity = float2(0.0);
q.age = 0.0;
q.life = 1.0;
q.shell = 0.0;
q.kind = 0.0;
q.index = float(id % PER);
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. A shell's particles are launched together as one rocket and
// all rise as it does; when it bursts each becomes a star of its own.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = fmod(u.time, LOOP);
float before = x - dt;
uint j = id % PER;
uint count = shellCount(p);
for (uint k = id / PER; k < count; k += SLOTS) {
Shell s = shellOf(k, p);
if (before < s.launch && s.launch <= x) {
q.position = float2(s.across, 0.0);
q.velocity = s.speed;
q.shell = float(k);
q.kind = 1.0;
}
float at = s.launch + s.rise;
if (q.kind == 1.0 && before < at && at <= x) {
// Its star's direction on a sphere, seen flat: the stars
// crowd its rim. A spark leaves slower, so it lags behind.
uint n = (j % STARS) * 3u + k * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = j < STARS ? 0.0 : pow(q.seed, 0.8);
float speed = s.stars * (0.92 + 0.08 * hashU(n + 2u));
q.position = burstOf(s, p);
q.velocity = dir * speed * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
q.kind = 2.0;
}
}
if (q.kind == 1.0) {
// Rising: gravity alone, worked out exactly for the step.
q.position += q.velocity * dt;
q.position.y -= 0.5 * p.gravity * dt * dt;
q.velocity.y -= p.gravity * dt;
} else if (q.kind == 2.0) {
// Burning: gravity pulls it down, the air takes a part of its
// speed, and it moves on.
q.velocity.y -= p.gravity * dt;
q.velocity *= exp(-p.drag * dt);
q.position += q.velocity * dt;
q.age += dt;
if (q.age > q.life) q.kind = 0.0;
}
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// The horizon, higher in a picture taller than wide, so the show stays
// near its middle.
float horizonOf(constant Uniforms& u) {
return HORIZON + 0.5 * (1.0 - zoomOf(u));
}
// Where a point of a shell's, in meters, is on the picture.
float2 seenAt(float2 m, float far, constant Uniforms& u) {
return float2(0.0, horizonOf(u)) + m * FOCAL * zoomOf(u) / far;
}
// Where to draw it and how big. Waiting particles, and a rocket's but its
// head and tail, go off the picture, where nothing is drawn.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
uint j = uint(q.index);
bool tail = q.kind == 1.0 && j < TAIL;
if (q.kind == 0.0 || (q.kind == 1.0 && !tail)) {
return float3(-1.0, -1.0, 0.0);
}
float aspect = u.resolution.x / u.resolution.y;
Shell s = shellOf(uint(q.shell), p);
float2 m = q.position;
// A star's head is bigger than its sparks.
float size = q.lag == 0.0 ? 0.007 : 0.0045;
if (tail && j > 0u) {
// A spark of the tail: further back along the path, scattered.
float back = float(j) * 0.012;
float2 off = float2(hashU(j * 5u), hashU(j * 5u + 1u)) - 0.5;
m -= q.velocity * back - off * float(j) * 0.25;
size = 0.004;
} else if (tail) {
size = 0.007;
}
float2 spot = seenAt(m, s.far, u);
// Smaller in a picture taller than wide, as the show is.
size *= zoomOf(u);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A shell's color: its own stretch of the kiln palette, from where its
// stars start (heat 1) to where they burn out (heat 0). Gold, red, cream
// and orange shells.
float3 colorOf(uint c, float heat) {
float2 span = float2(0.40, 0.92);
if (c == 1u) span = float2(0.26, 0.62);
if (c == 2u) span = float2(0.50, 1.00);
if (c == 3u) span = float2(0.32, 0.80);
return kiln(mix(span.x, span.y, heat));
}
// A star's light: hottest as it bursts, cooling through its shell's colors
// as it burns out, brighter for a near shell. Late in its life a star
// glitters: brighter or fainter at random, fifteen times a second, the same
// on average. The rocket's tail fades behind its head.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
Shell s = shellOf(uint(q.shell), p);
float near = clamp(420.0 / s.far, 0.3, 1.5);
if (q.kind == 1.0) {
float3 c = kiln(0.85) * soft * near * 0.5;
return float4(c / (1.0 + q.index * 0.25), 0.0);
}
float heat = 1.0 - q.age / q.life;
float3 c = colorOf(s.color, pow(heat, 0.6));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
uint n = uint(q.index) + (u.frame / 4u) * 7919u;
float sparkle = hashU(n) < 0.35 ? 2.2 : 0.35;
light *= mix(sparkle, 1.0, smoothstep(0.25, 0.4, heat));
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * near * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The night: near-black overhead, a little warmer down at the trees, with
// a few faint stars.
float3 skyAt(float2 uv, float aspect) {
float3 low = float3(0.070, 0.042, 0.050);
float3 high = float3(0.014, 0.011, 0.020);
float3 c = mix(low, high, smoothstep(0.08, 0.8, uv.y));
float2 g = uv * float2(aspect, 1.0) * 80.0;
float2 cell = floor(g);
float h = hashU(uint(cell.x + 4000.0) * 977u + uint(cell.y) * 131u);
float star = smoothstep(0.25, 0.0, length(fract(g) - 0.5));
c += float3(0.30, 0.28, 0.26) * step(0.994, h) * star;
return c;
}
// The light each shell's burst throws on the sky, the trees and the field:
// up as it bursts, gone in a few seconds, wide round where it burst.
float3 litBy(float2 s, constant Uniforms& u, constant Params& p) {
float x = fmod(u.time, LOOP);
uint count = shellCount(p);
float3 sum = float3(0.0);
for (uint k = 0; k < count; k++) {
Shell sh = shellOf(k, p);
float a = x - sh.launch - sh.rise;
if (a < 0.0 || a > 4.0 * p.burn) continue;
float env = smoothstep(0.0, 0.3, a) * exp(-a * 1.3 / p.burn);
float2 b = seenAt(burstOf(sh, p), sh.far, u);
float near = clamp(420.0 / sh.far, 0.3, 1.5);
float3 c = colorOf(sh.color, 0.8);
sum += c * env * near * exp(-length(s - b) * 1.6);
}
return sum * p.glow;
}
// The picture: the sky and its light, the stars' light rolled off so it
// never clips, and in front, the trees and the field we watch from.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float3 lit = litBy(s, u, p);
float3 c = skyAt(uv, aspect) + lit * 0.1;
float3 light = trails.at(uv).rgb;
c += 1.0 - exp(-light * 1.4);
// The trees along the horizon, and the field nearer.
float x = s.x;
float h = horizonOf(u);
float trees = h + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
trees += 0.05 * pow(abs(sin(x * 13.0 + 1.0)), 6.0);
trees += 0.035 * pow(abs(sin(x * 29.0 + 2.0)), 10.0);
float3 dark = float3(0.010, 0.009, 0.012) + lit * 0.012;
c = mix(c, dark, smoothstep(0.004, -0.004, s.y - trees));
float field = h - 0.06 - 0.05 * sin(x * 0.9 + 0.5);
float3 grass = float3(0.016, 0.013, 0.014) + lit * 0.03;
c = mix(c, grass, smoothstep(0.004, -0.004, s.y - field));
float2 v = uv - 0.5;
c *= 1.0 - dot(v, v) * 0.5;
// A little noise, so the dark gradients don't show bands.
uint2 px = uint2(uv * u.resolution);
c += (hashU(px.x * 1973u + px.y * 9277u) - 0.5) / 255.0;
return float4(c, 1.0);
}
// Lightning's sound noise: random values `rate` times a second, joined
// smoothly; a lower rate sounds darker.
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;
}
// A bang `a` seconds after it reaches you, from `far` meters: a crack the
// air dulls with distance, a boom, and its echo off the hills.
float bangOf(float t, float a, float far, uint seed) {
if (a <= 0.0 || a > 5.0) return 0.0;
float clear = smoothstep(1400.0, 300.0, far);
float crack = noise1(t, 5000.0, seed) * 0.7
+ noise1(t, 1700.0, seed + 1u) * 0.6;
crack *= exp(-a * 28.0) * smoothstep(0.0, 0.0015, a) * (0.3 + 0.7 * clear);
float boom = noise1(t, 85.0, seed + 2u) * 0.7
+ noise1(t, 230.0, seed + 3u) * 0.5;
float body = smoothstep(0.0, 0.004, a) * exp(-a * 2.4);
float e = a - 0.6;
float echo = e > 0.0 ? smoothstep(0.0, 0.05, e) * exp(-e * 2.5) * 0.3 : 0.0;
return crack + boom * (body + echo);
}
// The crackle of the stars as they glitter: a click in four of every ten
// 120ths of a second, for `span` seconds.
float crackleOf(float t, float a, float span, uint seed) {
if (a <= 0.0 || a > span) return 0.0;
float cell = floor(t * 120.0);
uint n = uint(cell) * 3u + seed * 7919u;
if (hashU(n) > 0.4) return 0.0;
float d = t - (cell + 0.6 * hashU(n + 1u)) / 120.0;
float pop = d > 0.0 ? exp(-d * 2500.0) : 0.0;
float env = smoothstep(0.0, 0.2, a) * smoothstep(span, span * 0.6, a);
return pop * (hashU(n + 2u) * 2.0 - 1.0) * env;
}
// The sound can't see the particles, but it has every shell from shellOf.
// Light arrives at once; sound at 343 meters a second. So each shell's
// bang and crackle start its distance over SOUND after the picture's burst
// and glitter. Under them, a breeze.
float2 sound(float t, constant Params& p) {
float loops = floor(t / LOOP);
float x = t - loops * LOOP;
uint count = shellCount(p);
float2 sum = float2(noise1(t, 500.0, 21u) * 0.4 + noise1(t, 70.0, 22u),
noise1(t, 500.0, 23u) * 0.4 + noise1(t, 70.0, 24u));
sum *= 0.03 * (0.75 + 0.25 * noise1(t, 0.4, 25u));
// The last loop's last bangs may still be on their way.
for (int m = 0; m < 2; m++) {
if (m == 1 && loops < 1.0) break;
float xm = x + float(m) * LOOP;
for (uint k = 0; k < count; k++) {
Shell s = shellOf(k, p);
float2 b = burstOf(s, p);
float far = length(float3(b, s.far));
float near = clamp(420.0 / far, 0.25, 1.6);
float pan = 0.5 + 0.35 * clamp(b.x * FOCAL / s.far, -1.0, 1.0);
float2 side = float2(1.0 - pan, pan) * 2.0;
uint seed = k * 13u + 5u;
float a = xm - s.launch - s.rise - far / SOUND;
float v = bangOf(t, a, far, seed) * near;
float glitter = 0.6 * 4.0 * p.burn;
v += crackleOf(t, a - glitter, 1.6 * p.burn, seed) * 0.35 * near;
sum += side * v;
}
}
// Each minute fades in over a second and out over its last half.
float fadeIn = smoothstep(0.0, 1.0, x);
float fadeOut = smoothstep(LOOP, LOOP - 0.5, x);
return tanh(sum * 2.0) / 1.45 * fadeIn * fadeOut * p.volume;
}7 lines from stage 4 that are gone
return float2(0.0, HORIZON) + m * FOCAL * zoomOf(u) / far;
⋯
bool head = q.kind == 1.0 && q.index == 0.0;
if (q.kind == 0.0 || (q.kind == 1.0 && !head)) {
⋯
float2 spot = seenAt(q.position, s.far, u);
⋯
return float4(kiln(0.85) * soft * near * 0.5, 0.0);
⋯
float trees = HORIZON + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
⋯
float field = HORIZON - 0.06 - 0.05 * sin(x * 0.9 + 0.5);The sound
The sound is a function of time alone and can't see the particles, but it doesn't need to: every shell is shellOf its number, so sound knows when and where each one bursts, as the picture does. For each shell it takes the burst point from burstOf, the same as the picture's, finds the straight line from there to you, standing on the ground far meters in front of the launch site, length(float3(b, s.far)), and starts the shell's bang that distance over SOUND, 343 m/s, after the burst. Light is taken to arrive at once (it would take under three millionths of a second).
bangOf is a crack, noise joined at 5,000 and 1,700 points a second that dies away within a tenth of a second, dulled with distance (from 300 m to 1.4 km it falls to 30 %, as the air takes the highest notes first); a boom, noise at 85 and 230 points a second that rises in 4 ms and dies away over about a second; and that boom again 0.6 s later at 30 %, an echo off the hills. A bang is louder the nearer it is (420 / distance, held between 0.25 and 1.6) and placed between the ears by where the burst is across the picture. About two and a half seconds after each bang, when its stars start to glitter, comes their crackle: in four of every ten 120ths of a second, a click that dies away within a millisecond, for 1.6 × burn seconds. Under it all, a breeze: noise at 500 and 70 points a second, swelling slowly.
tanh rounds off the loudest moments. Each minute fades in over its first second and out over its last half second; the bangs of a minute's last shells, if they are still on their way when the next minute starts (only at large distance), are heard in the next. At the defaults the peak is about −5.2 dBFS and the average (RMS) about −21.0 dBFS over the minute; with volume at 1, −3.2 dBFS and −19.1 dBFS, and −16.4 dBFS RMS at the busiest settings tried (every 1.5, distance 0.5, burn 2).
New in Ray Kiln
The sound along the clock. The playground's clock is a scrubber you can drag to any moment. Now, for any shader with a sound, the sound is drawn along it: a waveform behind the scrubber, from 0 to its end, so a bang or a beat can be seen before it is heard and dragged to. There is nothing to press. The playground renders the sound ahead, the same samples its Export saves as a WAV file, a second at a time between frames, and keeps the lowest and highest sample of each hundredth of a second; the loudest moment fills the height. The part the clock has passed is drawn in ember and the rest in cream. It is drawn again a moment after the code or a slider changes, since the sound reads the same sliders, and it is in the shader's own time, so a pause, slow motion or a loop on the clock leave it as it is. A shader with no sound has none.
This episode is made to be read that way. Open it in the playground and look at the clock: each spike is a bang, with the quiet breeze between them. Press Pause, drag the clock to the foot of a spike and step back a frame at a time with ,: the sky there is already full of a burst well into its bloom, because the flash came a second and a half or more before. The first shell bursts at 1.65 s, and the first spike starts at 3.14 s. Then move distance up: the picture changes at once, and a moment later the spikes are drawn again, each further to the right of its flash.
Try this
- Set
distanceto 1.5: the shells burst small and low over the trees. The first bang comes four seconds after its flash, and the second shell's, 2.1 km away, six seconds after its own, 0.6 seconds before the third shell's. - Set
gravityto 0: the bursts no longer droop and stay round to the end. At 20 they sag into streaks that point down. - Set
dragto 4 and every burst is a tight ball about a third as wide; at 0.3 the stars keep their speed and a near shell spreads past the top of the picture. - Set
trailsto 0 to see each star as a dot, its sparks a short line behind it. - Set
everyto 1.5 for two or three shells in the sky at once.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
every | 2.5 | 1.5 to 8.0 | Seconds between shells, on average |
distance | 0.55 | 0.5 to 1.5 | How far away the shells burst, in kilometers: each its own, from 0.75 to 1.4 times this |
gravity | 9.8 | 0.0 to 20.0 | How fast gravity pulls the stars down, in meters a second each second |
drag | 1.2 | 0.3 to 4.0 | How fast the air slows the stars, a fraction of their speed each second |
burn | 1.0 | 0.4 to 2.0 | How long the stars burn, times about four seconds |
trails | 0.9 | 0.0 to 0.96 | How much of the last image is left at each step |
glow | 1.0 | 0.3 to 2.0 | How bright the stars are |
volume | 0.8 | 0.0 to 1.0 | Loudness 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.
Made from this episode
The full source
This is the whole episode: the picture, the parameters and the sound.
fireworks.metal
// Ray Kiln · Fireworks
//
// @episode Fireworks
// @length 60
// @variation 30
// @variations 1,2,3,5,6,7,8,11
// @sweeps every, drag, distance
// @still 15.9
// @teaches Bursts of particles under gravity, a bang that arrives late
// @category Compute
// @tags fireworks, particles, compute shaders, gravity, drag, numerical integration, speed of sound, trails, procedural sound
// @final Fireworks | Shells near and far over the trees, their stars glittering as they burn out, each bang reaching you seconds after its flash.
//
// A summer night across a field. Shells rise from behind the trees and
// burst into 256 stars each, every star trailing sparks. Each star and
// spark is a particle that flies out at the speed the burst gave it, slows
// in the air, falls under gravity and cools as it burns out. The shells
// burst at different distances, near ones bigger and higher in the
// picture, and the light of each reaches you at once, its bang only after
// it has crossed the distance at the speed of sound.
// 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
//
// @particles 196608
// @param every 2.5 1.5 8.0 Seconds between shells, on average
// @param distance 0.55 0.5 1.5 How far away the shells burst, in kilometers: each its own, from 0.75 to 1.4 times this
// @param gravity 9.8 0.0 20.0 How fast gravity pulls the stars down, in meters a second each second
// @param drag 1.2 0.3 4.0 How fast the air slows the stars, a fraction of their speed each second
// @param burn 1.0 0.4 2.0 How long the stars burn, times about four seconds
// @param trails 0.9 0.0 0.96 How much of the last image is left at each step
// @param glow 1.0 0.3 2.0 How bright the stars are
// @param volume 0.8 0.0 1.0 Loudness of the sound
constant float TAU = 6.2831853;
// The show repeats every LOOP seconds, ending in a moment of dark.
constant float LOOP = 60.0;
// The speed of sound in air, in meters a second.
constant float SOUND = 343.0;
// A point x meters across and y up, d meters away, is drawn FOCAL x / d
// and FOCAL y / d from the horizon, in half heights of the picture.
constant float FOCAL = 3.0;
constant float HORIZON = -0.84;
// SLOTS shells can be in the air at once, of PER particles each: STARS
// stars, each with a trail of sparks.
constant uint SLOTS = 8;
constant uint PER = 24576;
constant uint STARS = 256;
// The rocket's head and the sparks behind it.
constant uint TAIL = 32;
// A particle: where it is in meters (across, and up from the ground), its
// speed, its age and how long it burns, its shell, what it is doing (0
// waiting, 1 rising, 2 burning), its place in the shell, how far it lags
// behind its star, and a random number.
struct Particle {
float2 position;
float2 velocity;
float age;
float life;
float shell;
float kind;
float index;
float lag;
float seed;
};
// Cracked Clay's integer hash.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// A shell: when it is launched, how long it rises, how high it bursts, how
// far away it is, where across, how fast it leaves the ground and leans,
// how fast its stars fly out, and its color. All from hashes of its number,
// so the picture and the sound agree on every one.
struct Shell {
float launch;
float rise;
float height;
float far;
float across;
float2 speed;
float stars;
uint color;
};
Shell shellOf(uint k, constant Params& p) {
Shell s;
uint n = k * 8u + 5251u;
s.launch = (float(k) + 0.1 + 0.5 * hashU(n)) * p.every;
// The first goes up at once.
if (k == 0u) s.launch = 0.1;
s.rise = mix(1.3, 1.8, hashU(n + 1u));
s.far = 1000.0 * p.distance * mix(0.75, 1.4, hashU(n + 2u));
// Across the picture's middle, whatever the distance.
s.across = (hashU(n + 3u) * 2.0 - 1.0) * 0.8 * s.far / FOCAL;
s.height = mix(125.0, 145.0, hashU(n + 5u));
// Launched just fast enough to be at its height when it bursts.
float up = (s.height + 0.5 * p.gravity * s.rise * s.rise) / s.rise;
s.speed = float2((hashU(n + 4u) - 0.5) * 8.0, up);
s.stars = mix(100.0, 120.0, hashU(n + 6u));
s.color = uint(hashU(n + 7u) * 3.999);
return s;
}
// How many shells fit in a loop, the last burnt out before it ends.
uint shellCount(constant Params& p) {
return uint(max(floor((LOOP - 4.0 - 4.0 * p.burn) / p.every), 1.0));
}
// Where the shell bursts: at its height, as far across as it leaned.
float2 burstOf(Shell s, constant Params& p) {
return float2(s.across + s.speed.x * s.rise, s.height);
}
// Everything waits for its shell.
Particle spawn(uint id, constant Params& p) {
Particle q;
q.position = float2(0.0);
q.velocity = float2(0.0);
q.age = 0.0;
q.life = 1.0;
q.shell = 0.0;
q.kind = 0.0;
q.index = float(id % PER);
q.lag = 0.0;
q.seed = hashU(id * 3u + 7u);
return q;
}
// One step. A shell's particles are launched together as one rocket and
// all rise as it does; when it bursts each becomes a star of its own.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float dt = u.timeDelta;
float x = fmod(u.time, LOOP);
float before = x - dt;
uint j = id % PER;
uint count = shellCount(p);
for (uint k = id / PER; k < count; k += SLOTS) {
Shell s = shellOf(k, p);
if (before < s.launch && s.launch <= x) {
q.position = float2(s.across, 0.0);
q.velocity = s.speed;
q.shell = float(k);
q.kind = 1.0;
}
float at = s.launch + s.rise;
if (q.kind == 1.0 && before < at && at <= x) {
// Its star's direction on a sphere, seen flat: the stars
// crowd its rim. A spark leaves slower, so it lags behind.
uint n = (j % STARS) * 3u + k * 104729u;
float z = hashU(n) * 2.0 - 1.0;
float a = TAU * hashU(n + 1u);
float2 dir = sqrt(1.0 - z * z) * float2(cos(a), sin(a));
q.lag = j < STARS ? 0.0 : pow(q.seed, 0.8);
float speed = s.stars * (0.92 + 0.08 * hashU(n + 2u));
q.position = burstOf(s, p);
q.velocity = dir * speed * (1.0 - 0.5 * q.lag);
q.age = 0.0;
q.life = mix(3.6, 4.4, hashU(n + 3u)) * p.burn;
q.life *= 1.0 - 0.4 * q.lag;
q.kind = 2.0;
}
}
if (q.kind == 1.0) {
// Rising: gravity alone, worked out exactly for the step.
q.position += q.velocity * dt;
q.position.y -= 0.5 * p.gravity * dt * dt;
q.velocity.y -= p.gravity * dt;
} else if (q.kind == 2.0) {
// Burning: gravity pulls it down, the air takes a part of its
// speed, and it moves on.
q.velocity.y -= p.gravity * dt;
q.velocity *= exp(-p.drag * dt);
q.position += q.velocity * dt;
q.age += dt;
if (q.age > q.life) q.kind = 0.0;
}
}
// How much the show is drawn smaller in a picture taller than wide.
float zoomOf(constant Uniforms& u) {
return min(1.0, 0.95 * u.resolution.x / u.resolution.y);
}
// The horizon, higher in a picture taller than wide, so the show stays
// near its middle.
float horizonOf(constant Uniforms& u) {
return HORIZON + 0.5 * (1.0 - zoomOf(u));
}
// Where a point of a shell's, in meters, is on the picture.
float2 seenAt(float2 m, float far, constant Uniforms& u) {
return float2(0.0, horizonOf(u)) + m * FOCAL * zoomOf(u) / far;
}
// Where to draw it and how big. Waiting particles, and a rocket's but its
// head and tail, go off the picture, where nothing is drawn.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
uint j = uint(q.index);
bool tail = q.kind == 1.0 && j < TAIL;
if (q.kind == 0.0 || (q.kind == 1.0 && !tail)) {
return float3(-1.0, -1.0, 0.0);
}
float aspect = u.resolution.x / u.resolution.y;
Shell s = shellOf(uint(q.shell), p);
float2 m = q.position;
// A star's head is bigger than its sparks.
float size = q.lag == 0.0 ? 0.007 : 0.0045;
if (tail && j > 0u) {
// A spark of the tail: further back along the path, scattered.
float back = float(j) * 0.012;
float2 off = float2(hashU(j * 5u), hashU(j * 5u + 1u)) - 0.5;
m -= q.velocity * back - off * float(j) * 0.25;
size = 0.004;
} else if (tail) {
size = 0.007;
}
float2 spot = seenAt(m, s.far, u);
// Smaller in a picture taller than wide, as the show is.
size *= zoomOf(u);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, size);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.78, t));
c = mix(c, c4, smoothstep(0.78, 1.00, t));
return c;
}
// A shell's color: its own stretch of the kiln palette, from where its
// stars start (heat 1) to where they burn out (heat 0). Gold, red, cream
// and orange shells.
float3 colorOf(uint c, float heat) {
float2 span = float2(0.40, 0.92);
if (c == 1u) span = float2(0.26, 0.62);
if (c == 2u) span = float2(0.50, 1.00);
if (c == 3u) span = float2(0.32, 0.80);
return kiln(mix(span.x, span.y, heat));
}
// A star's light: hottest as it bursts, cooling through its shell's colors
// as it burns out, brighter for a near shell. Late in its life a star
// glitters: brighter or fainter at random, fifteen times a second, the same
// on average. The rocket's tail fades behind its head.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
Shell s = shellOf(uint(q.shell), p);
float near = clamp(420.0 / s.far, 0.3, 1.5);
if (q.kind == 1.0) {
float3 c = kiln(0.85) * soft * near * 0.5;
return float4(c / (1.0 + q.index * 0.25), 0.0);
}
float heat = 1.0 - q.age / q.life;
float3 c = colorOf(s.color, pow(heat, 0.6));
float light = smoothstep(0.0, 0.15, q.age) * smoothstep(0.0, 0.15, heat);
uint n = uint(q.index) + (u.frame / 4u) * 7919u;
float sparkle = hashU(n) < 0.35 ? 2.2 : 0.35;
light *= mix(sparkle, 1.0, smoothstep(0.25, 0.4, heat));
light *= q.lag == 0.0 ? 6.0 : 0.6 * (1.0 - q.lag);
float k = light * near * p.glow * (1.0 - p.trails) * 0.3;
return float4(c * soft * k, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The night: near-black overhead, a little warmer down at the trees, with
// a few faint stars.
float3 skyAt(float2 uv, float aspect) {
float3 low = float3(0.070, 0.042, 0.050);
float3 high = float3(0.014, 0.011, 0.020);
float3 c = mix(low, high, smoothstep(0.08, 0.8, uv.y));
float2 g = uv * float2(aspect, 1.0) * 80.0;
float2 cell = floor(g);
float h = hashU(uint(cell.x + 4000.0) * 977u + uint(cell.y) * 131u);
float star = smoothstep(0.25, 0.0, length(fract(g) - 0.5));
c += float3(0.30, 0.28, 0.26) * step(0.994, h) * star;
return c;
}
// The light each shell's burst throws on the sky, the trees and the field:
// up as it bursts, gone in a few seconds, wide round where it burst.
float3 litBy(float2 s, constant Uniforms& u, constant Params& p) {
float x = fmod(u.time, LOOP);
uint count = shellCount(p);
float3 sum = float3(0.0);
for (uint k = 0; k < count; k++) {
Shell sh = shellOf(k, p);
float a = x - sh.launch - sh.rise;
if (a < 0.0 || a > 4.0 * p.burn) continue;
float env = smoothstep(0.0, 0.3, a) * exp(-a * 1.3 / p.burn);
float2 b = seenAt(burstOf(sh, p), sh.far, u);
float near = clamp(420.0 / sh.far, 0.3, 1.5);
float3 c = colorOf(sh.color, 0.8);
sum += c * env * near * exp(-length(s - b) * 1.6);
}
return sum * p.glow;
}
// The picture: the sky and its light, the stars' light rolled off so it
// never clips, and in front, the trees and the field we watch from.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float3 lit = litBy(s, u, p);
float3 c = skyAt(uv, aspect) + lit * 0.1;
float3 light = trails.at(uv).rgb;
c += 1.0 - exp(-light * 1.4);
// The trees along the horizon, and the field nearer.
float x = s.x;
float h = horizonOf(u);
float trees = h + 0.045 + 0.02 * sin(x * 3.1) + 0.012 * sin(x * 9.3);
trees += 0.05 * pow(abs(sin(x * 13.0 + 1.0)), 6.0);
trees += 0.035 * pow(abs(sin(x * 29.0 + 2.0)), 10.0);
float3 dark = float3(0.010, 0.009, 0.012) + lit * 0.012;
c = mix(c, dark, smoothstep(0.004, -0.004, s.y - trees));
float field = h - 0.06 - 0.05 * sin(x * 0.9 + 0.5);
float3 grass = float3(0.016, 0.013, 0.014) + lit * 0.03;
c = mix(c, grass, smoothstep(0.004, -0.004, s.y - field));
float2 v = uv - 0.5;
c *= 1.0 - dot(v, v) * 0.5;
// A little noise, so the dark gradients don't show bands.
uint2 px = uint2(uv * u.resolution);
c += (hashU(px.x * 1973u + px.y * 9277u) - 0.5) / 255.0;
return float4(c, 1.0);
}
// Lightning's sound noise: random values `rate` times a second, joined
// smoothly; a lower rate sounds darker.
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;
}
// A bang `a` seconds after it reaches you, from `far` meters: a crack the
// air dulls with distance, a boom, and its echo off the hills.
float bangOf(float t, float a, float far, uint seed) {
if (a <= 0.0 || a > 5.0) return 0.0;
float clear = smoothstep(1400.0, 300.0, far);
float crack = noise1(t, 5000.0, seed) * 0.7
+ noise1(t, 1700.0, seed + 1u) * 0.6;
crack *= exp(-a * 28.0) * smoothstep(0.0, 0.0015, a) * (0.3 + 0.7 * clear);
float boom = noise1(t, 85.0, seed + 2u) * 0.7
+ noise1(t, 230.0, seed + 3u) * 0.5;
float body = smoothstep(0.0, 0.004, a) * exp(-a * 2.4);
float e = a - 0.6;
float echo = e > 0.0 ? smoothstep(0.0, 0.05, e) * exp(-e * 2.5) * 0.3 : 0.0;
return crack + boom * (body + echo);
}
// The crackle of the stars as they glitter: a click in four of every ten
// 120ths of a second, for `span` seconds.
float crackleOf(float t, float a, float span, uint seed) {
if (a <= 0.0 || a > span) return 0.0;
float cell = floor(t * 120.0);
uint n = uint(cell) * 3u + seed * 7919u;
if (hashU(n) > 0.4) return 0.0;
float d = t - (cell + 0.6 * hashU(n + 1u)) / 120.0;
float pop = d > 0.0 ? exp(-d * 2500.0) : 0.0;
float env = smoothstep(0.0, 0.2, a) * smoothstep(span, span * 0.6, a);
return pop * (hashU(n + 2u) * 2.0 - 1.0) * env;
}
// The sound can't see the particles, but it has every shell from shellOf.
// Light arrives at once; sound at 343 meters a second. So each shell's
// bang and crackle start its distance over SOUND after the picture's burst
// and glitter. Under them, a breeze.
float2 sound(float t, constant Params& p) {
float loops = floor(t / LOOP);
float x = t - loops * LOOP;
uint count = shellCount(p);
float2 sum = float2(noise1(t, 500.0, 21u) * 0.4 + noise1(t, 70.0, 22u),
noise1(t, 500.0, 23u) * 0.4 + noise1(t, 70.0, 24u));
sum *= 0.03 * (0.75 + 0.25 * noise1(t, 0.4, 25u));
// The last loop's last bangs may still be on their way.
for (int m = 0; m < 2; m++) {
if (m == 1 && loops < 1.0) break;
float xm = x + float(m) * LOOP;
for (uint k = 0; k < count; k++) {
Shell s = shellOf(k, p);
float2 b = burstOf(s, p);
float far = length(float3(b, s.far));
float near = clamp(420.0 / far, 0.25, 1.6);
float pan = 0.5 + 0.35 * clamp(b.x * FOCAL / s.far, -1.0, 1.0);
float2 side = float2(1.0 - pan, pan) * 2.0;
uint seed = k * 13u + 5u;
float a = xm - s.launch - s.rise - far / SOUND;
float v = bangOf(t, a, far, seed) * near;
float glitter = 0.6 * 4.0 * p.burn;
v += crackleOf(t, a - glitter, 1.6 * p.burn, seed) * 0.35 * near;
sum += side * v;
}
}
// Each minute fades in over a second and out over its last half.
float fadeIn = smoothstep(0.0, 1.0, x);
float fadeOut = smoothstep(LOOP, LOOP - 0.5, x);
return tanh(sum * 2.0) / 1.45 * fadeIn * fadeOut * 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] = [2.5, 0.55, 9.8, 1.2, 1.0, 0.9, 1.0, 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(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
More in Particles & Simulation
Press ? for keyboard shortcuts.


