Lava Flow
A river of lava running down a bending channel. Fractal noise is carried along the flow, two copies half a cycle apart so it never tears, and every pixel gets a temperature, hot in the middle and at the top, cooler downstream and at the banks. A cosine palette turns that number into a color, and where it falls low enough the lava sets into crust that breaks into plates over glowing cracks.
· Noise & Texture · Fragment shader
Teaches Flow noise, a cooling crust, color from temperature
Builds on Flowing Color Field (hash21, value noise, fractal noise), Wood & Marble (hash11)
Watch
The idea
Scrolling noise down the screen looks like a sliding picture, not a liquid. Lava is faster in the middle of its channel than at the banks, and it follows the channel's bends, so the pattern on it has to move with the flow at every point. That is flow noise: each pixel looks back along the flow for where its noise came from. The trouble is that a pattern carried by uneven speeds stretches more and more, until it tears. The fix is to carry two copies, half a cycle apart, and fade each one out before it gets too stretched and jumps back to where it started.
The rest of the episode is one number per pixel, a temperature, and two ways to read it. As a color, through a cosine palette, a whole ramp from dull red to nearly white made of four float3s. And as a state: under about 0.45 the lava has set into crust, which breaks into plates over cracks where the hotter lava shows. The noise here is the fractal noise from Flowing Color Field, bent by a second noise with domain warping.
It is built in four steps, and each step is a shader you can run on its own.
Stage 1 · Noise carried by the flow
The channel's middle is two slow sine waves of the height (channelMid), and across is how far a point is from that middle, 0 there and 1 at a bank (width sets how far that is). flowAt is the flow field: it points down the slope along the bend of the middle line, at a speed of 0.12 × (0.25 + 0.75 × (1 − a²)), so the lava in the middle runs four times as fast as at the banks.
carried is one copy of the noise. A cycle lasts CYCLE, 3 seconds, and phase is how far through it the copy is. The pixel reads the noise at q - flowAt(q) × phase × CYCLE × speed: back up the flow by as far as it has come since the cycle began. The noise is fractal noise, its coordinates first moved by two more fractal noises (domain warping, 1.6 times as far), so the shapes come out as streaks and folds rather than blobs. Each cycle starts from a new place in the noise, picked by the cycle's number, so the pattern doesn't come back each time it jumps; there are 20 of those places, and after 60 seconds (LOOP) the shader is back where it began, pixel for pixel.
flowNoise mixes two copies, the second half a cycle behind the first. The weight w = |1 − 2 × fract(t / CYCLE)| is 0 in the middle of the first copy's cycle and 1 at its ends, where it jumps, so each copy is only seen while it is fresh. Mixing two noises makes them flatter, so the result is stretched back out by 1 / √(w² + (1 − w)²). The picture is that noise, as gray.
stage-1-noise-carried-by-the-flow.metal
// Stage 1 · Noise carried by the flow
// The lava runs down a channel that bends. Fractal noise is carried along
// it: each pixel looks back up the flow for where its noise came from. A
// copy stretched too far would tear, so there are two, half a cycle apart,
// and each fades out before it jumps back. That is flow noise.
// @param speed 1 0 3 How fast the lava runs
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
// The noise itself, as gray.
float3 col = float3(n);
// Past the channel's edge, plain dark rock.
col = mix(col, float3(0.04, 0.034, 0.034), smoothstep(0.95, 1.05, a));
return float4(col, 1.0);
}
// The sound: a low rumble, noise with nothing above about 90 Hz, louder
// the faster the lava runs.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(low * 1.3) * p.volume * fade;
}Stage 2 · A temperature
temperature gives each pixel one number. It starts at heat × (1 − 0.45 a²), hottest in the middle. It loses cooling × (0.12 + 0.42 × down + 0.22 × a), where down goes from 0 at the top of the picture to 1 at the bottom: the lava cools as it runs and towards the banks. The noise adds or takes away up to about 0.2 (0.45 × (n − 0.5)), so some patches are hotter than their neighbors.
Here it is shown as mix(float3(0.250, 0.020, 0.010), float3(1.000, 0.850, 0.400), temp), dark red to pale gold. In the playground that mix has a strip in front of it showing the gradient it makes (see New in Ray Kiln below). The strip also shows the problem: two colors only make a straight line between them, and hot things don't glow along a straight line.
stage-2-a-temperature.metal18 new lines since stage 1
// Stage 2 · A temperature
// Every pixel gets one number, its temperature: hottest in the middle of
// the channel and at the top, cooler as it runs down and towards the
// banks, and the noise makes some of it hotter, some cooler. Here it is
// a mix of two colors, dark red for cold and pale gold for hot.
// @param speed 1 0 3 How fast the lava runs
// @param heat 1 0.4 1.6 How hot it comes out at the top
// @param cooling 1 0 2 How quickly it cools and grows a crust
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
// The temperature at a point, from 0 (cold) to 1 (white-hot).
float temperature(float2 q, float n, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
// How far it has run: q.y goes from 1 at the top to -1 at the bottom.
float down = 0.5 - 0.5 * q.y;
float hot = p.heat * (1.0 - 0.45 * a * a);
// It cools as it runs and towards the banks.
float cool = p.cooling * (0.12 + 0.42 * down + 0.22 * a);
// The noise makes some of it hotter, some cooler.
return hot - cool + 0.45 * (n - 0.5) - 0.1;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
float3 col = mix(float3(0.250, 0.020, 0.010),
float3(1.000, 0.850, 0.400), clamp(temp, 0.0, 1.0));
// Past the channel's edge, plain dark rock.
col = mix(col, float3(0.04, 0.034, 0.034), smoothstep(0.95, 1.05, a));
return float4(col, 1.0);
}
// The sound: the rumble, and a drone on 55 Hz whose high partials come in
// as the heat goes up.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// The drone: 55 Hz and its partials, the high ones only when hot.
float drone = 0.0;
for (int k = 1; k <= 6; k++) {
float level = exp(-float(k - 1) / (0.6 + 2.0 * p.heat)) / float(k);
drone += sin(TAU * 55.0 * float(k) * t + float(k)) * level;
}
float breath = 0.75 + 0.25 * sin(TAU * t / 12.0);
float2 hum = float2(drone) * 0.12 * breath;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh((low + hum) * 1.3) * p.volume * fade;
}2 lines from stage 1 that are gone
float3 col = float3(n);
⋯
return tanh(low * 1.3) * p.volume * fade;Stage 3 · Color from temperature
Something heated glows dull red first, then orange, gold and white. heatColor gets that ramp from a cosine palette, a + b × cos(2π(c × t + d)), computed for red, green and blue apiece. a is the middle of each channel, b how far it swings, c how many times it swings as t goes from 0 to 1, and d where in the swing it starts. The twelve numbers here were fitted to the Flowing Color Field kiln palette, so at 0.1 it gives about (0.12, 0.01, 0.02), at 0.5 (0.79, 0.29, 0.10) and at 0.9 (1.01, 0.86, 0.53). Blue's c is small, 0.199, so blue stays low until near the top, as it does in a fire. Past 0.9 the fit turns greenish, so the shader stops the temperature there: the hottest lava is gold-white, and heat at 1.6 makes more of it that color rather than a new one.
stage-3-color-from-temperature.metal8 new lines since stage 2
// Stage 3 · Color from temperature
// Two colors make a straight line between them. Hot things glow dull red,
// then orange, gold and nearly white, so the colors here come from a
// cosine palette: a + b * cos(2π(c * t + d)), four float3s, fitted to
// the kiln palette. Past 0.9 it would turn green, so it stops there.
// @param speed 1 0 3 How fast the lava runs
// @param heat 1 0.4 1.6 How hot it comes out at the top
// @param cooling 1 0 2 How quickly it cools and grows a crust
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
// Temperature to color: a cosine palette, a + b * cos(2π(c * t + d)).
// Four float3s give a ramp close to the kiln palette: dull red, orange,
// gold, nearly white.
float3 heatColor(float t) {
float3 a = float3(0.527, 0.645, 1.500);
float3 b = float3(0.518, 0.631, 1.496);
float3 c = float3(0.564, 0.378, 0.199);
float3 d = float3(0.551, 0.465, 0.458);
return max(a + b * cos(6.28318 * (c * t + d)), 0.0);
}
// The temperature at a point, from 0 (cold) to 1 (white-hot).
float temperature(float2 q, float n, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
// How far it has run: q.y goes from 1 at the top to -1 at the bottom.
float down = 0.5 - 0.5 * q.y;
float hot = p.heat * (1.0 - 0.45 * a * a);
// It cools as it runs and towards the banks.
float cool = p.cooling * (0.12 + 0.42 * down + 0.22 * a);
// The noise makes some of it hotter, some cooler.
return hot - cool + 0.45 * (n - 0.5) - 0.1;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
float3 col = heatColor(clamp(temp, 0.0, 0.9));
// Past the channel's edge, plain dark rock.
col = mix(col, float3(0.04, 0.034, 0.034), smoothstep(0.95, 1.05, a));
return float4(col, 1.0);
}
// The sound: the rumble and the drone, as before.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// The drone: 55 Hz and its partials, the high ones only when hot.
float drone = 0.0;
for (int k = 1; k <= 6; k++) {
float level = exp(-float(k - 1) / (0.6 + 2.0 * p.heat)) / float(k);
drone += sin(TAU * 55.0 * float(k) * t + float(k)) * level;
}
float breath = 0.75 + 0.25 * sin(TAU * t / 12.0);
float2 hum = float2(drone) * 0.12 * breath;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh((low + hum) * 1.3) * p.volume * fade;
}2 lines from stage 2 that are gone
float3 col = mix(float3(0.250, 0.020, 0.010),
float3(1.000, 0.850, 0.400), clamp(temp, 0.0, 1.0));Stage 4 · Crust and cracks
Where the temperature falls under about 0.45 the lava sets: crust goes from 0 at 0.52 to 1 at 0.38. The crust is rock, a mix of two dark grays by fractal noise (squared, so most of it is dark). It breaks into plates along the lines where the flow noise crosses 1/8, 3/8, 5/8 and 7/8. d is how far n is from the nearest of those, and within about 0.01 of one there is a crack. In a crack the lava underneath shows, at the temperature plus 0.2, since under the crust it hasn't lost as much heat. A plate's rim is lit by the lava under it, half as bright. Where the temperature is low all the way through, the cracks fade out too, so the coldest crust is solid.
Since the cracks are lines in the same noise that the flow carries, they move with the lava, and the plates ride downstream on them.
stage-4-crust-and-cracks.metal27 new lines since stage 3
// Stage 4 · Crust and cracks
// Where the lava cools under about 0.45 it sets into crust: dark rock, a
// mix of two grays. It breaks into plates along the lines where the
// noise crosses a level, and in those cracks the hotter lava underneath
// shows, so the plates ride on glowing seams.
// @param speed 1 0 3 How fast the lava runs
// @param heat 1 0.4 1.6 How hot it comes out at the top
// @param cooling 1 0 2 How quickly it cools and grows a crust
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
// Temperature to color: a cosine palette, a + b * cos(2π(c * t + d)).
// Four float3s give a ramp close to the kiln palette: dull red, orange,
// gold, nearly white.
float3 heatColor(float t) {
float3 a = float3(0.527, 0.645, 1.500);
float3 b = float3(0.518, 0.631, 1.496);
float3 c = float3(0.564, 0.378, 0.199);
float3 d = float3(0.551, 0.465, 0.458);
return max(a + b * cos(6.28318 * (c * t + d)), 0.0);
}
// The temperature at a point, from 0 (cold) to 1 (white-hot).
float temperature(float2 q, float n, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
// How far it has run: q.y goes from 1 at the top to -1 at the bottom.
float down = 0.5 - 0.5 * q.y;
float hot = p.heat * (1.0 - 0.45 * a * a);
// It cools as it runs and towards the banks.
float cool = p.cooling * (0.12 + 0.42 * down + 0.22 * a);
// The noise makes some of it hotter, some cooler.
return hot - cool + 0.45 * (n - 0.5) - 0.1;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
// Past 0.9 the fitted palette turns green.
float3 lava = heatColor(clamp(temp, 0.0, 0.9));
// Under about 0.45 the lava sets into crust.
float crust = 1.0 - smoothstep(0.38, 0.52, temp);
// Cracks where the noise crosses 1/8, 3/8, 5/8, 7/8.
float d = abs(fract(n * 4.0) - 0.5) / 4.0;
float crack = 1.0 - smoothstep(0.004, 0.016, d);
// Cold all through: no glow left.
crack *= smoothstep(0.05, 0.25, temp + 0.2);
// Rock: two grays.
float rough = fbm(q * p.scale * 6.0 + float2(n * 3.0, 0.0));
float3 rock = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115), rough * rough);
float rim = 1.0 - smoothstep(0.0, 0.05, d);
float3 plate = rock + lava * 0.5 * rim;
// In a crack, the hotter lava underneath.
float3 seam = heatColor(clamp(temp + 0.2, 0.0, 0.9));
float3 col = mix(lava, mix(plate, seam, crack), crust);
// Past the channel's edge, plain dark rock.
col = mix(col, float3(0.04, 0.034, 0.034), smoothstep(0.95, 1.05, a));
return float4(col, 1.0);
}
// The sound: crackles join in, the crust breaking: short bursts of noise,
// each a few milliseconds, more of them the faster it cools.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// One crackle channel: in each 1/60 s slot, maybe a burst.
float crackle(float t, float chance, float seed) {
float slot = floor(t * 60.0);
float h = hash11(slot * 1.37 + seed);
if (h > chance) return 0.0;
// Where in the slot it starts, in seconds after that; it dies in ~4 ms.
float at = (t * 60.0 - slot - hash11(slot + seed * 3.1) * 0.5) / 60.0;
if (at < 0.0) return 0.0;
float env = (1.0 - exp(-at * 4000.0)) * exp(-at * 250.0);
float grain = hash11(floor(t * 24000.0) + seed) * 2.0 - 1.0;
return grain * env * (0.5 + 0.5 * h / chance);
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// The drone: 55 Hz and its partials, the high ones only when hot.
float drone = 0.0;
for (int k = 1; k <= 6; k++) {
float level = exp(-float(k - 1) / (0.6 + 2.0 * p.heat)) / float(k);
drone += sin(TAU * 55.0 * float(k) * t + float(k)) * level;
}
float breath = 0.75 + 0.25 * sin(TAU * t / 12.0);
float2 hum = float2(drone) * 0.12 * breath;
// Crust breaks more when it cools faster.
float chance = 0.04 + 0.14 * min(p.cooling, 2.0) / 2.0;
float2 crack = float2(crackle(t, chance, 1.0),
crackle(t + 0.013, chance, 7.0)) * 0.4;
float2 all = low + hum + crack;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 1.3) * p.volume * fade;
}2 lines from stage 3 that are gone
float3 col = heatColor(clamp(temp, 0.0, 0.9));
⋯
return tanh((low + hum) * 1.3) * p.volume * fade;Stage 5 · Lava Flow
The finished shader adds the banks. Past the channel's edge (a over 1) the ground is cold rock from two fractal noises, warmed by the lava's glow: heatColor(0.45) falling off as exp(−6 (a − 1)), squared, and stronger with heat. Where lava meets bank there is a dark line, 1 − 0.5 × exp(−30 |a − 1|), since the lava is coolest there.
shade(), from the finished shader
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
// Past 0.9 the fitted palette turns green.
float3 lava = heatColor(clamp(temp, 0.0, 0.9));
// Under about 0.45 the lava sets into crust.
float crust = 1.0 - smoothstep(0.38, 0.52, temp);
// Cracks where the noise crosses 1/8, 3/8, 5/8, 7/8.
float d = abs(fract(n * 4.0) - 0.5) / 4.0;
float crack = 1.0 - smoothstep(0.004, 0.016, d);
// Cold all through: no glow left.
crack *= smoothstep(0.05, 0.25, temp + 0.2);
// Rock: two grays.
float rough = fbm(q * p.scale * 6.0 + float2(n * 3.0, 0.0));
float3 rock = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115), rough * rough);
float rim = 1.0 - smoothstep(0.0, 0.05, d);
float3 plate = rock + lava * 0.5 * rim;
// In a crack, the hotter lava underneath.
float3 seam = heatColor(clamp(temp + 0.2, 0.0, 0.9));
float3 col = mix(lava, mix(plate, seam, crack), crust);
// The banks: cold rock past the channel's edge, warmed by its glow.
float bank = smoothstep(0.95, 1.05, a);
float near = exp(-max(a - 1.0, 0.0) * 6.0);
float3 stone = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115),
fbm(q * 7.0) * fbm(q * 2.5 + 4.0));
stone += heatColor(0.45) * 0.35 * near * near * p.heat;
col = mix(col, stone, bank);
// A dark edge where the lava meets the bank: it is coolest there.
col *= 1.0 - 0.5 * exp(-abs(a - 1.0) * 30.0);
return float4(col, 1.0);
}lava-flow.metal, the whole file8 new lines since stage 4
// Ray Kiln · Lava Flow
//
// @episode Lava Flow
// @length 60
// @variation 30
// @variations 1,2,4,5,6,8,9,12
// @still 14
// @teaches Flow noise, a cooling crust, color from temperature
// @category Fragment
// @tags flow noise, fractal noise, domain warping, cosine palette, procedural sound
// @final Lava Flow | The crust breaks into plates, the cracks glow, and the banks catch the light.
//
// A river of lava running down a slope. The noise doesn't scroll: it is
// carried along a flow field, two copies half a cycle apart, each faded
// out as it stretches too far, so it moves with the river and never tears.
// Every pixel gets a temperature: hottest in the middle of the channel and
// at the top, cooler downstream and towards the banks, and lower where the
// noise says the crust is thick. That one number is the color: a cosine
// palette, four float3s, from dull red to white-hot. Where it falls low
// enough the lava is crust, dark rock that cracks into plates, and the
// cracks between them stay hot.
// 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
// @param speed 1 0 3 How fast the lava runs
// @param heat 1 0.4 1.6 How hot it comes out at the top
// @param cooling 1 0 2 How quickly it cools and grows a crust
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
// Temperature to color: a cosine palette, a + b * cos(2π(c * t + d)).
// Four float3s give a ramp close to the kiln palette: dull red, orange,
// gold, nearly white.
float3 heatColor(float t) {
float3 a = float3(0.527, 0.645, 1.500);
float3 b = float3(0.518, 0.631, 1.496);
float3 c = float3(0.564, 0.378, 0.199);
float3 d = float3(0.551, 0.465, 0.458);
return max(a + b * cos(6.28318 * (c * t + d)), 0.0);
}
// The temperature at a point, from 0 (cold) to 1 (white-hot).
float temperature(float2 q, float n, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
// How far it has run: q.y goes from 1 at the top to -1 at the bottom.
float down = 0.5 - 0.5 * q.y;
float hot = p.heat * (1.0 - 0.45 * a * a);
// It cools as it runs and towards the banks.
float cool = p.cooling * (0.12 + 0.42 * down + 0.22 * a);
// The noise makes some of it hotter, some cooler.
return hot - cool + 0.45 * (n - 0.5) - 0.1;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
// Past 0.9 the fitted palette turns green.
float3 lava = heatColor(clamp(temp, 0.0, 0.9));
// Under about 0.45 the lava sets into crust.
float crust = 1.0 - smoothstep(0.38, 0.52, temp);
// Cracks where the noise crosses 1/8, 3/8, 5/8, 7/8.
float d = abs(fract(n * 4.0) - 0.5) / 4.0;
float crack = 1.0 - smoothstep(0.004, 0.016, d);
// Cold all through: no glow left.
crack *= smoothstep(0.05, 0.25, temp + 0.2);
// Rock: two grays.
float rough = fbm(q * p.scale * 6.0 + float2(n * 3.0, 0.0));
float3 rock = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115), rough * rough);
float rim = 1.0 - smoothstep(0.0, 0.05, d);
float3 plate = rock + lava * 0.5 * rim;
// In a crack, the hotter lava underneath.
float3 seam = heatColor(clamp(temp + 0.2, 0.0, 0.9));
float3 col = mix(lava, mix(plate, seam, crack), crust);
// The banks: cold rock past the channel's edge, warmed by its glow.
float bank = smoothstep(0.95, 1.05, a);
float near = exp(-max(a - 1.0, 0.0) * 6.0);
float3 stone = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115),
fbm(q * 7.0) * fbm(q * 2.5 + 4.0));
stone += heatColor(0.45) * 0.35 * near * near * p.heat;
col = mix(col, stone, bank);
// A dark edge where the lava meets the bank: it is coolest there.
col *= 1.0 - 0.5 * exp(-abs(a - 1.0) * 30.0);
return float4(col, 1.0);
}
// The lava, heard. A low rumble is the flow: noise with nothing above
// about 90 Hz, louder with speed. Under it a drone on 55 Hz brightens
// with the heat. Crackles are the crust breaking: short bursts of noise,
// each a few milliseconds, more of them the more crust there is.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// One crackle channel: in each 1/60 s slot, maybe a burst.
float crackle(float t, float chance, float seed) {
float slot = floor(t * 60.0);
float h = hash11(slot * 1.37 + seed);
if (h > chance) return 0.0;
// Where in the slot it starts, in seconds after that; it dies in ~4 ms.
float at = (t * 60.0 - slot - hash11(slot + seed * 3.1) * 0.5) / 60.0;
if (at < 0.0) return 0.0;
float env = (1.0 - exp(-at * 4000.0)) * exp(-at * 250.0);
float grain = hash11(floor(t * 24000.0) + seed) * 2.0 - 1.0;
return grain * env * (0.5 + 0.5 * h / chance);
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// The drone: 55 Hz and its partials, the high ones only when hot.
float drone = 0.0;
for (int k = 1; k <= 6; k++) {
float level = exp(-float(k - 1) / (0.6 + 2.0 * p.heat)) / float(k);
drone += sin(TAU * 55.0 * float(k) * t + float(k)) * level;
}
float breath = 0.75 + 0.25 * sin(TAU * t / 12.0);
float2 hum = float2(drone) * 0.12 * breath;
// Crust breaks more when it cools faster.
float chance = 0.04 + 0.14 * min(p.cooling, 2.0) / 2.0;
float2 crack = float2(crackle(t, chance, 1.0),
crackle(t + 0.013, chance, 7.0)) * 0.4;
float2 all = low + hum + crack;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 1.3) * p.volume * fade;
}1 line from stage 4 that are gone
col = mix(col, float3(0.04, 0.034, 0.034), smoothstep(0.95, 1.05, a));The sound
The sound is built from the same sliders as the picture. The flow is a low rumble: smooth noise from hashes of the time at 40 and 90 steps a second, so it is nearly all under 100 Hz, a different pair in each ear, and louder with speed. Under it a drone plays 55 Hz and its first six partials, the high ones only coming in with heat (each partial k is at exp(−(k − 1) / (0.6 + 2 × heat)) / k), swelling over 12 seconds. The crust breaking is crackles: time is cut into slots of 1/60 second, and in each slot a hash decides whether a burst of noise starts, about 4 ms long. The chance is 0.04 plus up to 0.14 more with cooling, about 7 bursts a second in each ear at the defaults. tanh keeps the sum under full scale.
At the defaults the peak is about −6.2 dBFS and the average (RMS) about −18.9 dBFS over the minute. With speed at 3, cooling at 2 and heat at 1.6 the peak is about −5.5 dBFS and the RMS −16.5 dBFS; with speed and cooling at 0 it is quieter, about −7.5 and −21.4 dBFS. The sound fades out and back in around the point where the minute comes round (a time constant of about 33 ms), so the loop joins without a click.
New in Ray Kiln
Gradient swatches in the code. The playground's editor already put a small square of color before a float3 of three plain numbers. Now a gradient written with those literals gets a strip in front of it showing the colors it makes from t = 0 on the left to t = 1 on the right. A mix whose first two arguments are color literals gets one before mix, and clicking the strip's left or right half opens the color picker on that end; the strip and the picture follow as you pick. A cosine palette gets one too, written inline as a + b * cos(k * (c * t + d)) with the four literals in it, or as the palette maker writes it, with four names each set from a literal above the return. Its four vectors aren't colors, so its strip only shows the result: change a, b, c or d through their own squares and watch the strip move. Both are found across lines, and the strip shows what the canvas would, each channel stopping at 1.
This episode has both. Open Lava Flow in the playground: heatColor has a strip before its return, the whole temperature ramp at a glance, and the crust's rock and the banks' stone each have one before their mix. Pick a redder dark end for the rock, or open stage 2 and drag its gold end towards white, and watch the strip and the lava change together.
Try this
- Set
coolingto 0: no crust forms, and the whole channel is open lava. At 2 most of it sets, and only cracks glow. - Set
heatto 1.6 for a channel of gold-white lava with crust only at its edges, or to 0.4 for a flow that has nearly stopped glowing. - Set
scaleto 8 for small plates and fine cracks, or to 1 for a few big slabs. - Set
speedto 0: the noise stops moving, but the two copies still fade in turn every 1.5 seconds. That fade is the only thing flow noise adds to a still pattern. At 3 the copies stretch further before they fade, and the streaks get long. - In
heatColor, change blue'sc(the last number ofc) from 0.199 to 1.0: blue swings five times as often, and the lava turns violet and magenta with blue cracks. - In
flowNoise, replacemix(a, b, w)witha: one copy only. Watch the pattern jump back every 3 seconds. - In
flowAt, change0.75 * (1.0 - a * a)to0.0: the whole channel runs at one speed, and the noise slides like a picture instead of shearing like a liquid.
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 | 1 | 0 to 3 | How fast the lava runs |
heat | 1 | 0.4 to 1.6 | How hot it comes out at the top |
cooling | 1 | 0 to 2 | How quickly it cools and grows a crust |
scale | 3 | 1 to 8 | How big the crust's plates are: higher means smaller |
width | 0.75 | 0.3 to 1.2 | How wide the channel is |
volume | 0.8 | 0 to 1 | 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.
lava-flow.metal
// Ray Kiln · Lava Flow
//
// @episode Lava Flow
// @length 60
// @variation 30
// @variations 1,2,4,5,6,8,9,12
// @still 14
// @teaches Flow noise, a cooling crust, color from temperature
// @category Fragment
// @tags flow noise, fractal noise, domain warping, cosine palette, procedural sound
// @final Lava Flow | The crust breaks into plates, the cracks glow, and the banks catch the light.
//
// A river of lava running down a slope. The noise doesn't scroll: it is
// carried along a flow field, two copies half a cycle apart, each faded
// out as it stretches too far, so it moves with the river and never tears.
// Every pixel gets a temperature: hottest in the middle of the channel and
// at the top, cooler downstream and towards the banks, and lower where the
// noise says the crust is thick. That one number is the color: a cosine
// palette, four float3s, from dull red to white-hot. Where it falls low
// enough the lava is crust, dark rock that cracks into plates, and the
// cracks between them stay hot.
// 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
// @param speed 1 0 3 How fast the lava runs
// @param heat 1 0.4 1.6 How hot it comes out at the top
// @param cooling 1 0 2 How quickly it cools and grows a crust
// @param scale 3 1 8 How big the crust's plates are: higher means smaller
// @param width 0.75 0.3 1.2 How wide the channel is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The loop: everything comes back to where it started in this many seconds.
constant float LOOP = 60.0;
// Each copy of the noise is carried this many seconds before it fades.
constant float CYCLE = 3.0;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: five octaves, each finer and fainter than the last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// Where the channel's middle is at height y: it bends gently down the slope.
float channelMid(float y) {
return 0.22 * sin(y * 1.6 + 0.7) + 0.08 * sin(y * 3.7 + 2.0);
}
// How far across the channel a point is: 0 in the middle, 1 at a bank.
float across(float2 q, constant Params& p) {
return abs(q.x - channelMid(q.y)) / p.width;
}
// The flow: down the slope along the bend, slow at the banks.
float2 flowAt(float2 q, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
float pace = 0.12 * (0.25 + 0.75 * (1.0 - a * a));
float bend = (channelMid(q.y + 0.01) - channelMid(q.y - 0.01)) / 0.02;
return normalize(float2(-bend, -1.0)) * pace;
}
// One copy of the noise, carried back up the flow. Each cycle starts
// from a new place in the noise.
float carried(float2 q, float t, float shift, constant Params& p) {
float k = (t + shift) / CYCLE;
float phase = fract(k);
float jump = fmod(floor(k), LOOP / CYCLE);
float2 start = float2(jump * 3.17, jump * 5.71);
float2 from = q - flowAt(q, p) * phase * CYCLE * p.speed;
float2 s = from * p.scale + start;
// Domain warping: a second noise bends the first.
float2 bendBy = float2(fbm(s * 0.7 + 3.1), fbm(s * 0.7 + 8.4));
return fbm(s + 1.6 * bendBy);
}
// Flow noise: two copies half a cycle apart, each unseen as it jumps.
float flowNoise(float2 q, float t, constant Params& p) {
float w = abs(1.0 - 2.0 * fract(t / CYCLE));
float a = carried(q, t, 0.0, p);
float b = carried(q, t, 0.5 * CYCLE, p);
float n = mix(a, b, w);
// Mixing flattens them: put the contrast back.
return 0.5 + (n - 0.5) / sqrt(w * w + (1.0 - w) * (1.0 - w));
}
// Temperature to color: a cosine palette, a + b * cos(2π(c * t + d)).
// Four float3s give a ramp close to the kiln palette: dull red, orange,
// gold, nearly white.
float3 heatColor(float t) {
float3 a = float3(0.527, 0.645, 1.500);
float3 b = float3(0.518, 0.631, 1.496);
float3 c = float3(0.564, 0.378, 0.199);
float3 d = float3(0.551, 0.465, 0.458);
return max(a + b * cos(6.28318 * (c * t + d)), 0.0);
}
// The temperature at a point, from 0 (cold) to 1 (white-hot).
float temperature(float2 q, float n, constant Params& p) {
float a = clamp(across(q, p), 0.0, 1.0);
// How far it has run: q.y goes from 1 at the top to -1 at the bottom.
float down = 0.5 - 0.5 * q.y;
float hot = p.heat * (1.0 - 0.45 * a * a);
// It cools as it runs and towards the banks.
float cool = p.cooling * (0.12 + 0.42 * down + 0.22 * a);
// The noise makes some of it hotter, some cooler.
return hot - cool + 0.45 * (n - 0.5) - 0.1;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 q = (uv - 0.5) * 2.0 * float2(u.resolution.x / u.resolution.y, 1.0);
// The lava enters at the top, q.y = 1.
float t = fmod(u.time, LOOP);
float a = across(q, p);
float n = flowNoise(q, t, p);
float temp = temperature(q, n, p);
// Past 0.9 the fitted palette turns green.
float3 lava = heatColor(clamp(temp, 0.0, 0.9));
// Under about 0.45 the lava sets into crust.
float crust = 1.0 - smoothstep(0.38, 0.52, temp);
// Cracks where the noise crosses 1/8, 3/8, 5/8, 7/8.
float d = abs(fract(n * 4.0) - 0.5) / 4.0;
float crack = 1.0 - smoothstep(0.004, 0.016, d);
// Cold all through: no glow left.
crack *= smoothstep(0.05, 0.25, temp + 0.2);
// Rock: two grays.
float rough = fbm(q * p.scale * 6.0 + float2(n * 3.0, 0.0));
float3 rock = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115), rough * rough);
float rim = 1.0 - smoothstep(0.0, 0.05, d);
float3 plate = rock + lava * 0.5 * rim;
// In a crack, the hotter lava underneath.
float3 seam = heatColor(clamp(temp + 0.2, 0.0, 0.9));
float3 col = mix(lava, mix(plate, seam, crack), crust);
// The banks: cold rock past the channel's edge, warmed by its glow.
float bank = smoothstep(0.95, 1.05, a);
float near = exp(-max(a - 1.0, 0.0) * 6.0);
float3 stone = mix(float3(0.030, 0.026, 0.026),
float3(0.150, 0.125, 0.115),
fbm(q * 7.0) * fbm(q * 2.5 + 4.0));
stone += heatColor(0.45) * 0.35 * near * near * p.heat;
col = mix(col, stone, bank);
// A dark edge where the lava meets the bank: it is coolest there.
col *= 1.0 - 0.5 * exp(-abs(a - 1.0) * 30.0);
return float4(col, 1.0);
}
// The lava, heard. A low rumble is the flow: noise with nothing above
// about 90 Hz, louder with speed. Under it a drone on 55 Hz brightens
// with the heat. Crackles are the crust breaking: short bursts of noise,
// each a few milliseconds, more of them the more crust there is.
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float rumble(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// One crackle channel: in each 1/60 s slot, maybe a burst.
float crackle(float t, float chance, float seed) {
float slot = floor(t * 60.0);
float h = hash11(slot * 1.37 + seed);
if (h > chance) return 0.0;
// Where in the slot it starts, in seconds after that; it dies in ~4 ms.
float at = (t * 60.0 - slot - hash11(slot + seed * 3.1) * 0.5) / 60.0;
if (at < 0.0) return 0.0;
float env = (1.0 - exp(-at * 4000.0)) * exp(-at * 250.0);
float grain = hash11(floor(t * 24000.0) + seed) * 2.0 - 1.0;
return grain * env * (0.5 + 0.5 * h / chance);
}
float2 sound(float t, constant Params& p) {
float flow = min(p.speed, 2.0);
float lowL = 0.6 * rumble(t, 40.0) + 0.4 * rumble(t + 9.1, 90.0);
float lowR = 0.6 * rumble(t + 3.3, 40.0) + 0.4 * rumble(t + 5.7, 90.0);
float2 low = float2(lowL, lowR) * 0.35 * (0.3 + 0.7 * flow / 2.0);
// The drone: 55 Hz and its partials, the high ones only when hot.
float drone = 0.0;
for (int k = 1; k <= 6; k++) {
float level = exp(-float(k - 1) / (0.6 + 2.0 * p.heat)) / float(k);
drone += sin(TAU * 55.0 * float(k) * t + float(k)) * level;
}
float breath = 0.75 + 0.25 * sin(TAU * t / 12.0);
float2 hum = float2(drone) * 0.12 * breath;
// Crust breaks more when it cools faster.
float chance = 0.04 + 0.14 * min(p.cooling, 2.0) / 2.0;
float2 crack = float2(crackle(t, chance, 1.0),
crackle(t + 0.013, chance, 7.0)) * 0.4;
float2 all = low + hum + crack;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 1.3) * p.volume * fade;
}Running it in your own project
Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.
To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:
Drawing it, in Swift
struct Uniforms {
var resolution: SIMD2<Float>
var time: Float
var timeDelta: Float
var mouse: SIMD2<Float>
var frame: UInt32
var pad: UInt32 = 0
}
let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [1, 1, 1, 3, 0.75, 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 Noise & Texture
Press ? for keyboard shortcuts.


