Lorenz Attractor
Three numbers and three rules for how fast each one changes, followed a small step at a time by 120,000 particles. They start as one ember, closer together than a pixel, and the rules draw it out into a ribbon, fold it and spread it over the whole two-winged butterfly, where no particle ever settles or repeats. Every thirty seconds the cloud is gathered onto one particle and comes apart again. Eight voices start as one line and go their own ways.
· Particles & Simulation · Compute shader
Teaches A chaotic system integrated step by step, particles that never settle
Builds on Floating 3D Scene (rotation), Kaleidoscope, Cracked Clay (integer hash), Particle Swarm, Murmuration, Spiral Galaxy (the particles' view, fit to the picture)
Watch
The idea
In 1963 the meteorologist Edward Lorenz cut a model of air rising and falling over a warm floor down to three numbers, x, y and z, and three rules for how fast each one changes: x changes at 10 (y − x), y at x (ρ − z) − y, and z at x y − 8/3 z, with ρ at 28. Nothing in them is random. Yet a point that follows them never comes to rest and never repeats: it circles one wing of a butterfly a few times, crosses to the other, and comes back, with no pattern to how many turns each visit lasts. And two points that start a hair apart are, a little later, nowhere near each other. That is chaos, and the butterfly is its best-known picture, a strange attractor.
There is no formula for where a point is after a given time. The only way to know is to go there a small step at a time, which is numerical integration, and a GPU can do it for a great many points at once. The particles are the same machinery as Particle Swarm: a buffer of them, a compute kernel that moves each one sixty times a second, each drawn as a dot of light into an image that fades. It is built in four steps.
Stage 1 · A step at a time
Each of the 120,000 particles is a position, a point (x, y, z). lorenz is the three rules: it takes a point and gives the rate of each number there, with σ = 10 and β = 8/3 as constants and ρ from the rho slider. advance moves a point on by a time h with Runge and Kutta's fourth-order method: the rate where the point is (k1), the rate half a step on along k1 (k2), half a step on along k2 (k3), and a whole step on along k3 (k4), and then the point moves by h times their average, the middle two counted twice. The simplest method, Euler's, would use k1 alone; four rates a step follow a curving path far more closely for the same step. update takes two such steps of 0.5 × speed / 60 each, so at the default speed of 0.5 a second on the clock is half a unit of the system's own time, in steps of 1/240.
spawn scatters the particles through a box round the shape, and place shows them from the front: x across and z up, each less rho − 3 (about the middle of the shape) and divided by rho, so the picture stays the same size as rho changes. Wherever a particle starts, within a second or two it is on the butterfly. Every dot is the same gold, 0.36 % of the picture's height, and fade keeps trails (90 %) of the last step's image.
stage-1-a-step-at-a-time.metal
// Stage 1 · A step at a time
// Three numbers, x, y and z, and Lorenz's three rules for how fast each
// changes. Every particle starts somewhere in a box and takes the rules a
// small step at a time. Seen from the front, x across and z up, they all
// fall onto the same two-winged shape and go round it without end.
//
// @particles 120000
// @param speed 0.5 0.0 2.0 How fast the system's own time runs
// @param rho 28.0 14.0 45.0 Lorenz's third number: under about 24 every orbit settles, over it none does
// @param trails 0.9 0.0 0.97 How much of the last image is left at each step
// Lorenz's other two numbers, as he had them.
constant float SIGMA = 10.0;
constant float BETA = 8.0 / 3.0;
// A particle: where it is in Lorenz's space of three numbers.
struct Particle {
float3 position;
};
// 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);
}
// Lorenz's three rules: how fast x, y and z change at the point q.
float3 lorenz(float3 q, float rho) {
return float3(SIGMA * (q.y - q.x),
q.x * (rho - q.z) - q.y,
q.x * q.y - BETA * q.z);
}
// One step of h, by Runge and Kutta's fourth-order method: the rate where
// the point is, two more half a step on, one a whole step on, averaged with
// the middle two counted twice.
float3 advance(float3 q, float h, float rho) {
float3 k1 = lorenz(q, rho);
float3 k2 = lorenz(q + 0.5 * h * k1, rho);
float3 k3 = lorenz(q + 0.5 * h * k2, rho);
float3 k4 = lorenz(q + h * k3, rho);
return q + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
// Each starts anywhere in a box round the butterfly.
Particle spawn(uint id, constant Params& p) {
Particle q;
float3 h = float3(hashU(id * 3u), hashU(id * 3u + 1u), hashU(id * 3u + 2u));
q.position = (h - float3(0.5, 0.5, 0.0)) * float3(2.0, 2.0, 1.6) * p.rho;
return q;
}
// One step: two Runge-Kutta steps of half the time each.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float h = 0.5 * p.speed * u.timeDelta;
q.position = advance(advance(q.position, h, p.rho), h, p.rho);
}
// How much the butterfly 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);
}
// Seen from the front: x across, z up, one unit the half height.
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
float aspect = u.resolution.x / u.resolution.y;
float3 c = (q.position - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float2 spot = c.xz * zoomOf(u);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, 0.0036);
}
// 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 dot of gold. Longer trails keep more of each step, so each gives less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float soft = smoothstep(1.0, 0.2, length(at));
return float4(kiln(0.7) * soft * (1.0 - p.trails) * 0.25, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture: the dots' light, rolled off so it never clips, on the dark
// of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float3 light = trails.at(uv).rgb;
float3 col = 1.0 - exp(-light * 1.5);
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}Stage 2 · Turning it round
The shape is not flat. viewOf turns x and y about the z axis with rot, Kaleidoscope's rotation, by an angle that grows with time (0.12 radians a second times turn, and the pointer turns it too), then leans the whole thing 0.3 radians toward the eye, so each wing shows as a loop with a hole in it. Nothing is nearer or further in this view: there is no perspective, and the dots add up whichever is in front.
Each particle also gets a heat: the length of its three rates, over 0.22 × rho², held between 0 and 1. ink colors the dot with it through the kiln palette, from 0.38 to 0.98: ember near the middle of each wing, where a particle idles, and gold to white-hot round the outside and on the dive between the wings, where it races. The two dark eyes are the middles of the wings, where all three rates are zero: a particle there would stay there, but none ever arrives.
stage-2-turning-it-round.metal21 new lines since stage 1
// Stage 2 · Turning it round
// The shape is not flat. The view now goes round it, leaning a little, and
// each particle is colored by how fast it is going: ember where it idles
// near the middle of a wing, gold to white-hot where it races round the
// outside and dives between the wings.
//
// @particles 120000
// @param speed 0.5 0.0 2.0 How fast the system's own time runs
// @param rho 28.0 14.0 45.0 Lorenz's third number: under about 24 every orbit settles, over it none does
// @param turn 1.0 -3.0 3.0 How fast the view goes round the butterfly
// @param trails 0.9 0.0 0.97 How much of the last image is left at each step
constant float TAU = 6.2831853;
// Lorenz's other two numbers, as he had them.
constant float SIGMA = 10.0;
constant float BETA = 8.0 / 3.0;
// How far the butterfly leans toward the eye, in radians.
constant float LEAN = 0.3;
// A particle: where it is in Lorenz's space of three numbers, and how fast
// it is going there, from 0 to 1.
struct Particle {
float3 position;
float heat;
};
// 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);
}
// Lorenz's three rules: how fast x, y and z change at the point q.
float3 lorenz(float3 q, float rho) {
return float3(SIGMA * (q.y - q.x),
q.x * (rho - q.z) - q.y,
q.x * q.y - BETA * q.z);
}
// One step of h, by Runge and Kutta's fourth-order method: the rate where
// the point is, two more half a step on, one a whole step on, averaged with
// the middle two counted twice.
float3 advance(float3 q, float h, float rho) {
float3 k1 = lorenz(q, rho);
float3 k2 = lorenz(q + 0.5 * h * k1, rho);
float3 k3 = lorenz(q + 0.5 * h * k2, rho);
float3 k4 = lorenz(q + h * k3, rho);
return q + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
// How fast a point is going, 0 to 1: the rates grow with rho squared.
float heatAt(float3 q, float rho) {
return clamp(length(lorenz(q, rho)) / (0.22 * rho * rho), 0.0, 1.0);
}
// Each starts anywhere in a box round the butterfly.
Particle spawn(uint id, constant Params& p) {
Particle q;
float3 h = float3(hashU(id * 3u), hashU(id * 3u + 1u), hashU(id * 3u + 2u));
q.position = (h - float3(0.5, 0.5, 0.0)) * float3(2.0, 2.0, 1.6) * p.rho;
q.heat = heatAt(q.position, p.rho);
return q;
}
// One step: two Runge-Kutta steps of half the time each.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float h = 0.5 * p.speed * u.timeDelta;
q.position = advance(advance(q.position, h, p.rho), h, p.rho);
q.heat = heatAt(q.position, p.rho);
}
// Kaleidoscope's rotation: a point turned by angle a about the middle.
float2 rot(float2 p, float a) {
float c = cos(a), s = sin(a);
return float2(c * p.x - s * p.y, s * p.x + c * p.y);
}
// How much the butterfly 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 Lorenz's space is on the picture, one unit the half
// height: z is up, the view goes round the z axis (the pointer turns it
// too) and leans a little, so the wings show as loops, not lines.
float2 viewOf(float3 q, constant Uniforms& u, constant Params& p) {
float3 c = (q - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float mouse = u.mouse.x / max(u.resolution.x, 1.0) - 0.5;
float2 r = rot(c.xy, 0.6 + 0.12 * p.turn * u.time + mouse * TAU);
return float2(r.x, c.z * cos(LEAN) + r.y * sin(LEAN)) * zoomOf(u);
}
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
float aspect = u.resolution.x / u.resolution.y;
float2 spot = viewOf(q.position, u, p);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, 0.0036);
}
// 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 particle's light: ember where it idles round a wing, gold to white-hot
// where it races. Longer trails keep more of each step, so each dot gives
// less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float3 c = kiln(0.38 + 0.6 * q.heat);
float soft = smoothstep(1.0, 0.2, length(at));
return float4(c * soft * (1.0 - p.trails) * 0.25, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture: the dots' light, rolled off so it never clips, on the dark
// of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float3 light = trails.at(uv).rgb;
float3 col = 1.0 - exp(-light * 1.5);
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}3 lines from stage 1 that are gone
float3 c = (q.position - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float2 spot = c.xz * zoomOf(u);
⋯
return float4(kiln(0.7) * soft * (1.0 - p.trails) * 0.25, 0.0);Stage 3 · One ember
Now spawn puts every particle within spread (0.05 units, when the butterfly is about 40 across) of one point: inBall gives each a place of its own inside a ball, from four hashes of its number, and particle 0 sits at the very middle. On the picture that ball is far smaller than a pixel. For the first seconds the particles travel as one bright dot. But the rules pull any two nearby points apart, doubling the gap about every 0.8 units of the system's time, so the ember is drawn out into a short line, then a ribbon that goes right round a wing. Where the ribbon reaches the dive between the wings, one part of it goes left and the other right, and each part is stretched and split again, until the ribbon is sheet on sheet over the whole shape. From 0.05 to the size of the butterfly takes about 6 units of time, twelve seconds at the default speed. A start a thousand times closer would only take a little over twice as long.
One particle in 24 is drawn about eight times brighter than the rest (bright, set once in spawn), so single lines stand out of the cloud.
stage-3-one-ember.metal10 new lines since stage 2
// Stage 3 · One ember
// Now every particle starts within a hair of one point: an ember smaller
// than a pixel. For a few seconds they travel as one. But the rules double
// the gap between any two of them about every 0.8 of the system's time,
// and the ember is drawn out into a ribbon, folded, and spread over the
// whole butterfly. That is chaos: no start is close enough.
//
// @particles 120000
// @param speed 0.5 0.0 2.0 How fast the system's own time runs
// @param rho 28.0 14.0 45.0 Lorenz's third number: under about 24 every orbit settles, over it none does
// @param spread 0.05 0.001 2.0 How far apart the particles start, in the system's own units
// @param turn 1.0 -3.0 3.0 How fast the view goes round the butterfly
// @param trails 0.9 0.0 0.97 How much of the last image is left at each step
constant float TAU = 6.2831853;
// Lorenz's other two numbers, as he had them.
constant float SIGMA = 10.0;
constant float BETA = 8.0 / 3.0;
// How far the butterfly leans toward the eye, in radians.
constant float LEAN = 0.3;
// A particle: where it is in Lorenz's space of three numbers, how fast it
// is going there, from 0 to 1, and how bright it is drawn.
struct Particle {
float3 position;
float heat;
float bright;
};
// 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);
}
// Lorenz's three rules: how fast x, y and z change at the point q.
float3 lorenz(float3 q, float rho) {
return float3(SIGMA * (q.y - q.x),
q.x * (rho - q.z) - q.y,
q.x * q.y - BETA * q.z);
}
// One step of h, by Runge and Kutta's fourth-order method: the rate where
// the point is, two more half a step on, one a whole step on, averaged with
// the middle two counted twice.
float3 advance(float3 q, float h, float rho) {
float3 k1 = lorenz(q, rho);
float3 k2 = lorenz(q + 0.5 * h * k1, rho);
float3 k3 = lorenz(q + 0.5 * h * k2, rho);
float3 k4 = lorenz(q + h * k3, rho);
return q + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
// How fast a point is going, 0 to 1: the rates grow with rho squared.
float heatAt(float3 q, float rho) {
return clamp(length(lorenz(q, rho)) / (0.22 * rho * rho), 0.0, 1.0);
}
// A point inside a ball of radius 1, particle id's own; salt gives another.
float3 inBall(uint id, uint salt) {
uint n = id * 4u + salt * 7919u;
float3 d = float3(hashU(n), hashU(n + 1u), hashU(n + 2u)) - 0.5;
return normalize(d + 1e-4) * pow(hashU(n + 3u), 1.0 / 3.0);
}
// The ember: a point near the butterfly, and every particle within spread
// of it. Particle 0 is the ember's very middle.
Particle spawn(uint id, constant Params& p) {
Particle q;
float3 ember = float3(-0.2, -0.3, 0.8) * p.rho;
q.position = ember + p.spread * inBall(id, 0u) * (id == 0u ? 0.0 : 1.0);
q.heat = heatAt(q.position, p.rho);
// One in 24 is drawn bright, so single orbits show in the cloud.
q.bright = hashU(id * 4u + 3571u) < 1.0 / 24.0 ? 1.0 : 0.12;
return q;
}
// One step: two Runge-Kutta steps of half the time each.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p) {
float h = 0.5 * p.speed * u.timeDelta;
q.position = advance(advance(q.position, h, p.rho), h, p.rho);
q.heat = heatAt(q.position, p.rho);
}
// Kaleidoscope's rotation: a point turned by angle a about the middle.
float2 rot(float2 p, float a) {
float c = cos(a), s = sin(a);
return float2(c * p.x - s * p.y, s * p.x + c * p.y);
}
// How much the butterfly 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 Lorenz's space is on the picture, one unit the half
// height: z is up, the view goes round the z axis (the pointer turns it
// too) and leans a little, so the wings show as loops, not lines.
float2 viewOf(float3 q, constant Uniforms& u, constant Params& p) {
float3 c = (q - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float mouse = u.mouse.x / max(u.resolution.x, 1.0) - 0.5;
float2 r = rot(c.xy, 0.6 + 0.12 * p.turn * u.time + mouse * TAU);
return float2(r.x, c.z * cos(LEAN) + r.y * sin(LEAN)) * zoomOf(u);
}
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
float aspect = u.resolution.x / u.resolution.y;
float2 spot = viewOf(q.position, u, p);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, 0.0036);
}
// 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 particle's light: ember where it idles round a wing, gold to white-hot
// where it races, times its own brightness. Longer trails keep more of
// each step, so each dot gives less.
float4 ink(Particle q, float2 at, constant Uniforms& u, constant Params& p) {
float3 c = kiln(0.38 + 0.6 * q.heat);
float soft = smoothstep(1.0, 0.2, length(at));
return float4(c * soft * q.bright * (1.0 - p.trails) * 1.6, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture: the dots' light, rolled off so it never clips, on the dark
// of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float3 light = trails.at(uv).rgb;
float3 col = 1.0 - exp(-light * 1.5);
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}3 lines from stage 2 that are gone
float3 h = float3(hashU(id * 3u), hashU(id * 3u + 1u), hashU(id * 3u + 2u));
q.position = (h - float3(0.5, 0.5, 0.0)) * float3(2.0, 2.0, 1.6) * p.rho;
⋯
return float4(c * soft * (1.0 - p.trails) * 0.25, 0.0);Stage 4 · Lorenz Attractor
Every thirty seconds the cloud is gathered. update now takes Swarm swarm, as Murmuration's does, but reads only one particle from it: at the step a new cycle starts, every particle but particle 0 is put back within spread of where particle 0 is (swarm.at(0), its place after the last step), at a new place in the ball for each cycle. Particle 0 itself is never moved, so its path is unbroken for the whole minute, and the ember comes apart from a different point of the butterfly each time, and so in a different way.
shade adds a soft glow, the light at eight points 1.2 % of the picture's height away, a twentieth of each, and marks the middles of the two wings with a small ember dot each: the points (±√(β(ρ − 1)), ±√(β(ρ − 1)), ρ − 1), put through the same viewOf as the particles. glow scales the light before it is rolled off with 1 − exp(−x), so nothing clips.
With rho under about 24.7 those two middles pull orbits in, and under about 24 every particle spirals into one of them and stops: the picture goes dark but for two dots. Over it, none ever settles.
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 dots' light is rolled off so it never clips, with a
// soft copy of itself round it; behind, the two points each wing goes
// round and never reaches, and the dark of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float3 light = trails.at(uv).rgb;
// A soft glow: the light a little way off on eight sides.
float3 near = float3(0.0);
for (int i = 0; i < 8; i++) {
float a = TAU * float(i) / 8.0;
float2 off = float2(cos(a), sin(a)) * 0.012;
near += trails.at(uv + off / float2(aspect, 1.0)).rgb;
}
light += near * 0.05;
float3 col = 1.0 - exp(-light * 1.5 * p.glow);
// The middles of the wings: where the three rates are all zero.
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float w = sqrt(BETA * max(p.rho - 1.0, 0.0));
for (int i = 0; i < 2; i++) {
float side = i == 0 ? 1.0 : -1.0;
float3 middle = float3(side * w, side * w, p.rho - 1.0);
float r = length(s - viewOf(middle, u, p));
col += kiln(0.5) * smoothstep(0.012, 0.004, r) * 0.6;
col += kiln(0.42) * exp(-r * 9.0) * 0.05;
}
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}lorenz-attractor.metal, the whole file57 new lines since stage 3
// Ray Kiln · Lorenz Attractor
//
// @episode Lorenz Attractor
// @length 60
// @variation 30
// @variations 1,2,4,5,7,9,11,12
// @still 20.0
// @teaches A chaotic system integrated step by step, particles that never settle
// @category Compute
// @tags lorenz attractor, chaos, strange attractor, numerical integration, runge-kutta, particles, compute shaders, procedural sound
// @final Lorenz Attractor | Every thirty seconds the cloud is gathered onto one particle again, and chaos pulls it apart again.
//
// In 1963 Edward Lorenz boiled the weather down to three numbers and three
// rules for how fast each one changes. Followed step by step, a point never
// comes to rest and never repeats: it circles one wing of a butterfly, then
// the other, with no pattern to which. And two points that start a hair
// apart end up nowhere near each other. Here 120,000 particles start as
// one ember, closer together than a pixel, and the rules pull them into the
// whole butterfly. Every thirty seconds they are gathered again.
// 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 120000
// @param speed 0.5 0.0 2.0 How fast the system's own time runs
// @param rho 28.0 14.0 45.0 Lorenz's third number: under about 24 every orbit settles, over it none does
// @param spread 0.05 0.001 2.0 How far apart the particles start, in the system's own units
// @param turn 1.0 -3.0 3.0 How fast the view goes round the butterfly
// @param trails 0.9 0.0 0.97 How much of the last image is left at each step
// @param glow 1.0 0.3 2.5 How bright the particles are
// @param volume 0.8 0.0 1.0 Loudness of the sound
constant float TAU = 6.2831853;
// Lorenz's other two numbers, as he had them.
constant float SIGMA = 10.0;
constant float BETA = 8.0 / 3.0;
// The cloud is gathered every CYCLE seconds.
constant float CYCLE = 30.0;
// How far the butterfly leans toward the eye, in radians.
constant float LEAN = 0.3;
// A particle: where it is in Lorenz's space of three numbers, how fast it
// is going there, from 0 to 1, and how bright it is drawn.
struct Particle {
float3 position;
float heat;
float bright;
};
// 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);
}
// Lorenz's three rules: how fast x, y and z change at the point q.
float3 lorenz(float3 q, float rho) {
return float3(SIGMA * (q.y - q.x),
q.x * (rho - q.z) - q.y,
q.x * q.y - BETA * q.z);
}
// One step of h, by Runge and Kutta's fourth-order method: the rate where
// the point is, two more half a step on, one a whole step on, averaged with
// the middle two counted twice.
float3 advance(float3 q, float h, float rho) {
float3 k1 = lorenz(q, rho);
float3 k2 = lorenz(q + 0.5 * h * k1, rho);
float3 k3 = lorenz(q + 0.5 * h * k2, rho);
float3 k4 = lorenz(q + h * k3, rho);
return q + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
// How fast a point is going, 0 to 1: the rates grow with rho squared.
float heatAt(float3 q, float rho) {
return clamp(length(lorenz(q, rho)) / (0.22 * rho * rho), 0.0, 1.0);
}
// A point inside a ball of radius 1, particle id's own; salt gives another.
float3 inBall(uint id, uint salt) {
uint n = id * 4u + salt * 7919u;
float3 d = float3(hashU(n), hashU(n + 1u), hashU(n + 2u)) - 0.5;
return normalize(d + 1e-4) * pow(hashU(n + 3u), 1.0 / 3.0);
}
// The ember: a point near the butterfly, and every particle within spread
// of it. Particle 0 is the ember's very middle.
Particle spawn(uint id, constant Params& p) {
Particle q;
float3 ember = float3(-0.2, -0.3, 0.8) * p.rho;
q.position = ember + p.spread * inBall(id, 0u) * (id == 0u ? 0.0 : 1.0);
q.heat = heatAt(q.position, p.rho);
// One in 24 is drawn bright, so single orbits show in the cloud.
q.bright = hashU(id * 4u + 3571u) < 1.0 / 24.0 ? 1.0 : 0.12;
return q;
}
// One step: two Runge-Kutta steps of half the time each.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p, Swarm swarm) {
// As a new cycle starts, every particle is put back within spread of
// particle 0, wherever it has got to.
float cycle = floor(u.time / CYCLE);
if (id != 0u && cycle != floor((u.time - u.timeDelta) / CYCLE)) {
float3 lead = swarm.at(0u).position;
q.position = lead + p.spread * inBall(id, uint(cycle));
}
float h = 0.5 * p.speed * u.timeDelta;
q.position = advance(advance(q.position, h, p.rho), h, p.rho);
q.heat = heatAt(q.position, p.rho);
}
// Kaleidoscope's rotation: a point turned by angle a about the middle.
float2 rot(float2 p, float a) {
float c = cos(a), s = sin(a);
return float2(c * p.x - s * p.y, s * p.x + c * p.y);
}
// How much the butterfly 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 Lorenz's space is on the picture, one unit the half
// height: z is up, the view goes round the z axis (the pointer turns it
// too) and leans a little, so the wings show as loops, not lines.
float2 viewOf(float3 q, constant Uniforms& u, constant Params& p) {
float3 c = (q - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float mouse = u.mouse.x / max(u.resolution.x, 1.0) - 0.5;
float2 r = rot(c.xy, 0.6 + 0.12 * p.turn * u.time + mouse * TAU);
return float2(r.x, c.z * cos(LEAN) + r.y * sin(LEAN)) * zoomOf(u);
}
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
float aspect = u.resolution.x / u.resolution.y;
float2 spot = viewOf(q.position, u, p);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, 0.0036);
}
// 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 particle's light: ember where it idles round a wing, gold to white-hot
// where it races, times its own brightness. 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 d = length(at);
float3 c = kiln(0.38 + 0.6 * q.heat);
float soft = smoothstep(1.0, 0.2, d);
return float4(c * soft * q.bright * (1.0 - p.trails) * 1.6, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture. The dots' light is rolled off so it never clips, with a
// soft copy of itself round it; behind, the two points each wing goes
// round and never reaches, and the dark of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float3 light = trails.at(uv).rgb;
// A soft glow: the light a little way off on eight sides.
float3 near = float3(0.0);
for (int i = 0; i < 8; i++) {
float a = TAU * float(i) / 8.0;
float2 off = float2(cos(a), sin(a)) * 0.012;
near += trails.at(uv + off / float2(aspect, 1.0)).rgb;
}
light += near * 0.05;
float3 col = 1.0 - exp(-light * 1.5 * p.glow);
// The middles of the wings: where the three rates are all zero.
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float w = sqrt(BETA * max(p.rho - 1.0, 0.0));
for (int i = 0; i < 2; i++) {
float side = i == 0 ? 1.0 : -1.0;
float3 middle = float3(side * w, side * w, p.rho - 1.0);
float r = length(s - viewOf(middle, u, p));
col += kiln(0.5) * smoothstep(0.012, 0.004, r) * 0.6;
col += kiln(0.42) * exp(-r * 9.0) * 0.05;
}
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}
// The sound can't see the particles, so it tells the same story from the
// clock. Eight voices each go round a wing in turn: a low note in the left
// ear for one wing, a note a fifth above in the right ear for the other,
// one note a loop. Which wing comes next is a hashed coin. Every voice
// tosses the same coin until its own moment, a few seconds after the cloud
// is gathered, and its own coin after that: one line comes apart into
// eight. Under them, a drone on D.
float2 sound(float t, constant Params& p) {
float x = fmod(t, CYCLE);
uint cycle = uint(floor(t / CYCLE));
// A loop of a wing takes about 0.75 of the system's time.
float loop = 0.75 / max(p.speed, 0.25);
// When two points spread apart come to be a wing apart: their distance
// grows by e about every 1.1 of the system's time.
float apart = log(10.0 / p.spread) * 1.1 / max(p.speed, 0.25);
float n = floor(x / loop);
float age = x - n * loop;
float notes[4] = { 1.0, 2.0, 3.0, 4.0 };
float2 voices = float2(0.0);
for (uint i = 0; i < 8; i++) {
float own = apart * (0.7 + 0.6 * hashU(i * 17u + cycle * 5u));
uint coin = uint(n) * 31u + cycle * 1009u;
if (n * loop > own) coin += (i + 1u) * 977u;
float wing = step(0.5, hashU(coin));
float f = mix(146.83, 220.0, wing) * notes[i % 4] * (i < 4 ? 1.0 : 0.5);
float env = smoothstep(0.0, 0.02, age) * exp(-age * 3.5 / loop);
env *= smoothstep(loop, loop - 0.03, age);
float ring = sin(TAU * f * age) / (1.0 + 0.6 * float(i % 4));
float pan = mix(0.25, 0.75, wing);
voices += float2(1.0 - pan, pan) * ring * env;
}
// Silent as the cloud is gathered, so no voice's jump back is heard.
voices *= smoothstep(CYCLE, CYCLE - 0.3, x) * 0.27;
float drone = sin(TAU * 73.42 * t) + 0.4 * sin(TAU * 110.13 * t);
drone *= 0.14 * (0.8 + 0.2 * sin(TAU * 0.1 * t));
float2 v = voices + float2(drone);
// Each minute fades in over a second and out over its last half.
float minute = fmod(t, 60.0);
float fadeIn = smoothstep(0.0, 1.0, minute);
float fadeOut = smoothstep(60.0, 59.5, minute);
return tanh(v * 1.5) / 1.4 * fadeIn * fadeOut * p.volume;
}3 lines from stage 3 that are gone
constant Uniforms& u, constant Params& p) {
⋯
float soft = smoothstep(1.0, 0.2, length(at));
⋯
float3 col = 1.0 - exp(-light * 1.5);The sound
The sound is a function of time alone and can't see the particles, so it tells the same story from the clock. Eight voices each play one note for every loop of a wing, a loop taking 0.75 / speed seconds (with speed never counted as less than 0.25): a note on D3 (146.83 Hz) to the left for one wing, or on A3 (220 Hz), a fifth above, to the right for the other. The voices are at one, two, three and four times that note, quieter the higher, and the same again an octave down. Which wing comes next is a hashed coin, tossed once a loop. Every voice reads the same coin until a moment of its own, and its own coin from then on. Those moments are scattered round the time two points spread apart would take to be a wing apart, 1.1 × ln(10 / spread) units of the system's time, about twelve seconds at the defaults: so after each gathering there is one line of notes, and then it comes apart into eight, as the ember does. The picture and the sound share that timing and nothing else: the notes are not the wings the particles are on.
Each note rises in 20 ms, dies away over its loop and is closed over its last 30 ms, and the voices fade out over the last 0.3 s of each cycle, so the jump back to one line is not heard. Under them a drone on D2 (73.42 Hz) with a quieter A2 swells and sinks once every ten seconds. tanh rounds off the loudest moments; each minute of the sound fades in over a second and out over its last half second.
At the defaults the peak is about −6.5 dBFS and the average (RMS) about −20.1 dBFS over the minute; with volume at its highest, about −4.4 dBFS and −18.2 dBFS at the loudest settings tried (speed at 0 and spread at 0.001).
New in Ray Kiln
The inspector draws a path. The playground's particle inspector follows one particle: press Inspect (or I) under a particle shader's picture and click near a dot, and that particle's fields are read back from the GPU as it moves, with a ring on its dot. Now the ring leaves a line behind it: where the particle has been over the last four seconds of the shader's clock, brightest at the ring and fading to nothing at its far end. Each frame's reading of the particle's place is kept with the clock's time and the line is drawn through them, over the picture.
Because the time is the shader's clock and not the wall's, a pause holds the path where it is, slow motion draws the same line more slowly, and Restart or dragging the clock back starts it again. Two readings more than 0.3 of the picture apart, or a quarter of a second apart, are not joined, and neither is a piece more than four times as long as the pieces on both sides of it: a particle moves much as it did a frame before, so one that wraps round an edge, or is put somewhere new, leaves no streak across the picture. It is drawn in the playground only, never in an export.
Open Lorenz Attractor in the playground, wait until the cloud has spread, press Inspect and click anywhere on the butterfly. Among 120,000 dots, the line shows what one of them does: a loop or two round one wing, each a little wider than the last, then across to the other. Click another dot beside where the first one was and its line soon goes another way. At the thirty-second mark the particle you follow is put back beside particle 0: unless it was already close by, its line breaks there and starts again from the new place. Under the picture, position is its x, y and z, and heat rises on each dive between the wings.
Try this
- Set
spreadto 0.001 and restart: the ember, fifty times smaller, holds together less than twice as long. Then 2: the cloud fills the butterfly almost at once. - Lower
rhoto 20 and restart: every particle spirals into the middle of one wing and stays. Raise it slowly while it runs and find where they stop settling. - Set
trailsto 0 to see the particles alone, one dot each a step; 0.97 smooths them into the sheets they lie on. - Set
turnto 0 and move the pointer across the picture to look round the butterfly yourself: from the side the two wings are nearly flat sheets, at an angle to each other. - In the playground, press Inspect and click a dot, then set the speed under the picture to ¼: the path draws slowly enough to count the loops before each crossing.
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 |
|---|---|---|---|
speed | 0.5 | 0.0 to 2.0 | How fast the system's own time runs |
rho | 28.0 | 14.0 to 45.0 | Lorenz's third number: under about 24 every orbit settles, over it none does |
spread | 0.05 | 0.001 to 2.0 | How far apart the particles start, in the system's own units |
turn | 1.0 | -3.0 to 3.0 | How fast the view goes round the butterfly |
trails | 0.9 | 0.0 to 0.97 | How much of the last image is left at each step |
glow | 1.0 | 0.3 to 2.5 | How bright the particles 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.
lorenz-attractor.metal
// Ray Kiln · Lorenz Attractor
//
// @episode Lorenz Attractor
// @length 60
// @variation 30
// @variations 1,2,4,5,7,9,11,12
// @still 20.0
// @teaches A chaotic system integrated step by step, particles that never settle
// @category Compute
// @tags lorenz attractor, chaos, strange attractor, numerical integration, runge-kutta, particles, compute shaders, procedural sound
// @final Lorenz Attractor | Every thirty seconds the cloud is gathered onto one particle again, and chaos pulls it apart again.
//
// In 1963 Edward Lorenz boiled the weather down to three numbers and three
// rules for how fast each one changes. Followed step by step, a point never
// comes to rest and never repeats: it circles one wing of a butterfly, then
// the other, with no pattern to which. And two points that start a hair
// apart end up nowhere near each other. Here 120,000 particles start as
// one ember, closer together than a pixel, and the rules pull them into the
// whole butterfly. Every thirty seconds they are gathered again.
// 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 120000
// @param speed 0.5 0.0 2.0 How fast the system's own time runs
// @param rho 28.0 14.0 45.0 Lorenz's third number: under about 24 every orbit settles, over it none does
// @param spread 0.05 0.001 2.0 How far apart the particles start, in the system's own units
// @param turn 1.0 -3.0 3.0 How fast the view goes round the butterfly
// @param trails 0.9 0.0 0.97 How much of the last image is left at each step
// @param glow 1.0 0.3 2.5 How bright the particles are
// @param volume 0.8 0.0 1.0 Loudness of the sound
constant float TAU = 6.2831853;
// Lorenz's other two numbers, as he had them.
constant float SIGMA = 10.0;
constant float BETA = 8.0 / 3.0;
// The cloud is gathered every CYCLE seconds.
constant float CYCLE = 30.0;
// How far the butterfly leans toward the eye, in radians.
constant float LEAN = 0.3;
// A particle: where it is in Lorenz's space of three numbers, how fast it
// is going there, from 0 to 1, and how bright it is drawn.
struct Particle {
float3 position;
float heat;
float bright;
};
// 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);
}
// Lorenz's three rules: how fast x, y and z change at the point q.
float3 lorenz(float3 q, float rho) {
return float3(SIGMA * (q.y - q.x),
q.x * (rho - q.z) - q.y,
q.x * q.y - BETA * q.z);
}
// One step of h, by Runge and Kutta's fourth-order method: the rate where
// the point is, two more half a step on, one a whole step on, averaged with
// the middle two counted twice.
float3 advance(float3 q, float h, float rho) {
float3 k1 = lorenz(q, rho);
float3 k2 = lorenz(q + 0.5 * h * k1, rho);
float3 k3 = lorenz(q + 0.5 * h * k2, rho);
float3 k4 = lorenz(q + h * k3, rho);
return q + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
// How fast a point is going, 0 to 1: the rates grow with rho squared.
float heatAt(float3 q, float rho) {
return clamp(length(lorenz(q, rho)) / (0.22 * rho * rho), 0.0, 1.0);
}
// A point inside a ball of radius 1, particle id's own; salt gives another.
float3 inBall(uint id, uint salt) {
uint n = id * 4u + salt * 7919u;
float3 d = float3(hashU(n), hashU(n + 1u), hashU(n + 2u)) - 0.5;
return normalize(d + 1e-4) * pow(hashU(n + 3u), 1.0 / 3.0);
}
// The ember: a point near the butterfly, and every particle within spread
// of it. Particle 0 is the ember's very middle.
Particle spawn(uint id, constant Params& p) {
Particle q;
float3 ember = float3(-0.2, -0.3, 0.8) * p.rho;
q.position = ember + p.spread * inBall(id, 0u) * (id == 0u ? 0.0 : 1.0);
q.heat = heatAt(q.position, p.rho);
// One in 24 is drawn bright, so single orbits show in the cloud.
q.bright = hashU(id * 4u + 3571u) < 1.0 / 24.0 ? 1.0 : 0.12;
return q;
}
// One step: two Runge-Kutta steps of half the time each.
void update(thread Particle& q, uint id,
constant Uniforms& u, constant Params& p, Swarm swarm) {
// As a new cycle starts, every particle is put back within spread of
// particle 0, wherever it has got to.
float cycle = floor(u.time / CYCLE);
if (id != 0u && cycle != floor((u.time - u.timeDelta) / CYCLE)) {
float3 lead = swarm.at(0u).position;
q.position = lead + p.spread * inBall(id, uint(cycle));
}
float h = 0.5 * p.speed * u.timeDelta;
q.position = advance(advance(q.position, h, p.rho), h, p.rho);
q.heat = heatAt(q.position, p.rho);
}
// Kaleidoscope's rotation: a point turned by angle a about the middle.
float2 rot(float2 p, float a) {
float c = cos(a), s = sin(a);
return float2(c * p.x - s * p.y, s * p.x + c * p.y);
}
// How much the butterfly 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 Lorenz's space is on the picture, one unit the half
// height: z is up, the view goes round the z axis (the pointer turns it
// too) and leans a little, so the wings show as loops, not lines.
float2 viewOf(float3 q, constant Uniforms& u, constant Params& p) {
float3 c = (q - float3(0.0, 0.0, p.rho - 3.0)) / p.rho;
float mouse = u.mouse.x / max(u.resolution.x, 1.0) - 0.5;
float2 r = rot(c.xy, 0.6 + 0.12 * p.turn * u.time + mouse * TAU);
return float2(r.x, c.z * cos(LEAN) + r.y * sin(LEAN)) * zoomOf(u);
}
float3 place(Particle q, constant Uniforms& u, constant Params& p) {
float aspect = u.resolution.x / u.resolution.y;
float2 spot = viewOf(q.position, u, p);
return float3(spot / float2(aspect, 1.0) * 0.5 + 0.5, 0.0036);
}
// 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 particle's light: ember where it idles round a wing, gold to white-hot
// where it races, times its own brightness. 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 d = length(at);
float3 c = kiln(0.38 + 0.6 * q.heat);
float soft = smoothstep(1.0, 0.2, d);
return float4(c * soft * q.bright * (1.0 - p.trails) * 1.6, 0.0);
}
float4 fade(float2 uv, Trails previous,
constant Uniforms& u, constant Params& p) {
return previous.at(uv) * p.trails;
}
// The picture. The dots' light is rolled off so it never clips, with a
// soft copy of itself round it; behind, the two points each wing goes
// round and never reaches, and the dark of the kiln.
float4 shade(float2 uv, constant Uniforms& u, constant Params& p,
Trails trails) {
float aspect = u.resolution.x / u.resolution.y;
float3 light = trails.at(uv).rgb;
// A soft glow: the light a little way off on eight sides.
float3 near = float3(0.0);
for (int i = 0; i < 8; i++) {
float a = TAU * float(i) / 8.0;
float2 off = float2(cos(a), sin(a)) * 0.012;
near += trails.at(uv + off / float2(aspect, 1.0)).rgb;
}
light += near * 0.05;
float3 col = 1.0 - exp(-light * 1.5 * p.glow);
// The middles of the wings: where the three rates are all zero.
float2 s = (uv - 0.5) * 2.0 * float2(aspect, 1.0);
float w = sqrt(BETA * max(p.rho - 1.0, 0.0));
for (int i = 0; i < 2; i++) {
float side = i == 0 ? 1.0 : -1.0;
float3 middle = float3(side * w, side * w, p.rho - 1.0);
float r = length(s - viewOf(middle, u, p));
col += kiln(0.5) * smoothstep(0.012, 0.004, r) * 0.6;
col += kiln(0.42) * exp(-r * 9.0) * 0.05;
}
float2 v = uv - 0.5;
col += float3(0.030, 0.019, 0.022) * (1.0 - dot(v, v) * 1.8);
return float4(col, 1.0);
}
// The sound can't see the particles, so it tells the same story from the
// clock. Eight voices each go round a wing in turn: a low note in the left
// ear for one wing, a note a fifth above in the right ear for the other,
// one note a loop. Which wing comes next is a hashed coin. Every voice
// tosses the same coin until its own moment, a few seconds after the cloud
// is gathered, and its own coin after that: one line comes apart into
// eight. Under them, a drone on D.
float2 sound(float t, constant Params& p) {
float x = fmod(t, CYCLE);
uint cycle = uint(floor(t / CYCLE));
// A loop of a wing takes about 0.75 of the system's time.
float loop = 0.75 / max(p.speed, 0.25);
// When two points spread apart come to be a wing apart: their distance
// grows by e about every 1.1 of the system's time.
float apart = log(10.0 / p.spread) * 1.1 / max(p.speed, 0.25);
float n = floor(x / loop);
float age = x - n * loop;
float notes[4] = { 1.0, 2.0, 3.0, 4.0 };
float2 voices = float2(0.0);
for (uint i = 0; i < 8; i++) {
float own = apart * (0.7 + 0.6 * hashU(i * 17u + cycle * 5u));
uint coin = uint(n) * 31u + cycle * 1009u;
if (n * loop > own) coin += (i + 1u) * 977u;
float wing = step(0.5, hashU(coin));
float f = mix(146.83, 220.0, wing) * notes[i % 4] * (i < 4 ? 1.0 : 0.5);
float env = smoothstep(0.0, 0.02, age) * exp(-age * 3.5 / loop);
env *= smoothstep(loop, loop - 0.03, age);
float ring = sin(TAU * f * age) / (1.0 + 0.6 * float(i % 4));
float pan = mix(0.25, 0.75, wing);
voices += float2(1.0 - pan, pan) * ring * env;
}
// Silent as the cloud is gathered, so no voice's jump back is heard.
voices *= smoothstep(CYCLE, CYCLE - 0.3, x) * 0.27;
float drone = sin(TAU * 73.42 * t) + 0.4 * sin(TAU * 110.13 * t);
drone *= 0.14 * (0.8 + 0.2 * sin(TAU * 0.1 * t));
float2 v = voices + float2(drone);
// Each minute fades in over a second and out over its last half.
float minute = fmod(t, 60.0);
float fadeIn = smoothstep(0.0, 1.0, minute);
float fadeOut = smoothstep(60.0, 59.5, minute);
return tanh(v * 1.5) / 1.4 * 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] = [0.5, 28.0, 0.05, 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.
Built on later
More in Particles & Simulation
Press ? for keyboard shortcuts.


