Copper Kettle
A copper kettle on a dark table in the kiln's glow, drawn the way a metal works. It has no diffuse color at all, only the room mirrored and tinted by copper's own Fresnel, and a microfacet highlight from GGX. Brushed round its axis, the copper is smooth along its grooves and rough across them, so the lamp's highlight stretches into a streak, several times brighter than white. It comes to the boil and cools once a minute, and whistles at the top.
· Light & Materials · Fragment shader
Teaches Metals, microfacet highlights, brushed and anisotropic shine
Builds on Flowing Color Field, Morphing Shapes (smooth minimum), Floating 3D Scene (rotation, surface normals), Cracked Clay (integer hash, sound noise), Molten Metaballs (soft shadows), Kiln Fire (3D hash, 3D value noise), Pottery Wheel, Wood & Marble (hash11)
Watch
The idea
Paint, clay and wood have a color of their own: light goes a little way in, bounces around, and comes back out tinted by what it met. That light leaves in every direction, so it looks the same from wherever you stand. It is diffuse light. On top of it, the surface mirrors about 4% of the light back, in white.
A metal works differently. Its free electrons send light back from the very surface, and whatever gets in is absorbed within a few tens of nanometers. Nothing comes back out from inside, so a metal has no diffuse color at all. Everything it shows is a reflection, and the reflection itself carries the color. Copper sends back 95.5% of red light head on, 63.8% of green and 53.8% of blue (in linear light), so whatever it mirrors turns copper. At a glancing angle every metal mirrors nearly all of every color, and its edges turn pale. That is Fresnel, here in Schlick's form with a color instead of a single number.
A real surface is not a perfect mirror. It is rough at a scale far too small to see, so it is drawn as countless tiny mirrors, microfacets, each facing a slightly different way. GGX says what share of them face exactly halfway between the eye and a light, and so throw that light at the eye. Smooth copper has nearly all its facets facing one way, and a small, very bright highlight. Rough copper scatters them, and the highlight spreads and dims. Brush the copper and its scratches all run one way: across them it is rough, along them it is smooth, so the highlight stretches into a streak across the brushing. That is anisotropic reflection.
The kettle is drawn by raymarching, as Pottery Wheel draws its pot. It comes to the boil and cools once a minute, swings a little each way, and the lamp sways twice in that time, so the whole picture loops at 60 seconds. It is built in five steps, each a shader you can run on its own.
Stage 1 · A kettle on the table
The body is a solid of revolution: a point's distance from the upright axis less bodyRadius at its height, which runs from 0.70 at the foot out to 0.78 low down and in to 0.42 at the shoulder, cut off at the top at 0.88. The profile slopes, so that difference is more than the true distance to the surface, and the march takes 80% of it each step. The lid is a low disc with rounded edges and a squashed sphere (ellipsoid, radii 0.36 across and 0.12 up), with a ball for the knob. The spout is taper, a tube from low on the right up to its tip, its radius 0.13 at the body and 0.045 at the end, opened by cutting a small ball out of its tip. The handle is an arch, a tube round a circle of radius 0.47, kept above 0.85, its ends sitting in two round lugs, with a wooden grip along its top. smin joins the parts with a small fillet. kettle also says what a point is: 1 for the body and lid, 2 for the spout and handle, 3 for the wood. The whole kettle turns by a, which swings 0.35 ± 0.45 radians once a minute.
The ray first finds where it enters a ball of radius 1.5 round the kettle and starts marching there. A ray that misses the kettle and heads down meets the table, a dark plane at height 0, lit in a pool by the lamp, with the kettle's soft shadow on it (softShadow, as in Molten Metaballs). A ray that heads up sees roomSeen: walls from the kiln palette (kiln, from Flowing Color Field), the table past the horizon, and the kiln's mouth glowing low on the right, ember, brighter as the kettle comes to the boil (boilAt, from 0 at the start of the minute to 1 at 30 seconds and back). The lamp is a panel of four panes up on the left, behind the camera, so it shows only in reflections.
For now the copper is painted like a matte wall: its color times the lamp's light by n · l (in shadow, none), plus a little of the room. It is a fine pot of clay. It doesn't look like metal at all.
stage-1-a-kettle-on-the-table.metal
// Stage 1 · A kettle on the table
// A kettle, raymarched: a body spun round its axis, a lid,
// a spout and an arched handle. For now it is matte, like
// paint, lit by a lamp and by the kiln's glow.
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
col = diffuse;
} else {
// For now the copper is matte, like paint: lit, not mirrored.
col = float3(0.1, 0.04, 0.025)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard: it simmers, low noise that rises as it heats.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
float2 all = simmer;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * p.volume * fade;
}Stage 2 · Copper mirrors the room
The paint is gone. Now the copper shows only what it mirrors: the room in the direction reflect(rd, n), times schlick(COPPER, n · v). Head on, that is copper's own color; toward the rim, where the surface turns away from the eye, every channel rises toward 1. The wood keeps its diffuse color, which is right for wood.
Most of the room is dark, so most of the kettle is dark too, a deep red where it mirrors the walls. The kiln's glow lights up its right side, and the lamp's four panes show as small sharp windows on the body and the lid. A perfect mirror shows only what is there to mirror: this is why a metal needs a room.
stage-2-copper-mirrors-the-room.metal24 new lines since stage 1
// Stage 2 · Copper mirrors the room
// A metal has no diffuse color: all it shows is the room,
// mirrored and tinted. Copper sends back 95% of red head on,
// less green and blue, and every color more at a glance.
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// Copper's reflectance head on, in linear red, green and blue.
constant float3 COPPER = float3(0.955, 0.638, 0.538);
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
// Schlick's Fresnel with a color: f0 head on, white at a glance.
float3 schlick(float3 f0, float c) {
return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
col = diffuse;
} else {
float3 f0 = COPPER;
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, 0.0, t, p);
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard. It simmers, low noise that rises as it heats,
// with bubbles that come quicker.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
// Bubbles: forty chances a second, more of them taken when hot.
// This slot's and the last's, so none is cut off as the next begins.
float2 pops = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 40.0) - float(j);
uint id = uint(max(slot, 0.0));
// Each bubble takes the heat of its own moment, so none is
// switched on or off halfway.
float hot = boilAt(slot / 40.0);
if (hashU(id * 3u + 11u) < 0.05 + 0.6 * hot * hot) {
float since = t - slot / 40.0;
float hz = 220.0 + 500.0 * hashU(id * 3u + 12u) + 300.0 * hot;
float env = since * 150.0 * exp(1.0 - since * 150.0);
float pan = hashU(id * 3u + 13u);
pops += float2(1.0 - pan, pan) * env
* sin(TAU * hz * since * (1.0 + 4.0 * since));
}
}
float2 all = simmer + pops * 0.12;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * p.volume * fade;
}4 lines from stage 1 that are gone
col = float3(0.1, 0.04, 0.025)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
⋯
float2 all = simmer;Stage 3 · A microfacet highlight
Now the copper is rough, by rough, and GGX takes its square, alpha. For each light, microfacet works out h, the direction halfway between the eye and the light, and multiplies three things. ggx is the share of facets facing h, alpha² / (π ((n·h)² (alpha² − 1) + 1)²), high and narrow for small alpha. smithV takes out the facets hidden behind their neighbors from the eye or from the light, already divided by the 4 (n·l)(n·v) that the formula needs. And schlick is copper's Fresnel again, this time for the angle between the eye and h. Times n · l and the light's own brightness, that is the highlight. There are two lights: the lamp, cream, and the kiln, ember, weaker but growing with the heat.
The room's reflection is blurred to match: roomSeen softens the table's edge, the lamp's panes and the kiln's mouth by alpha, and dims the lamp as it spreads, so a rough mirror doesn't get brighter than a sharp one.
The lamp's highlight is far brighter than white. Head on to a highlight, GGX's peak, 1 / (π alpha²), is about 39 at the default rough of 0.3, and with the lamp's light at 5 (CREAM * 5.0 times lamp) the value shade returns at the hot spot is about 6, where white is 1. Nothing clamps it: the last line raises the color to 1/2.2 for the screen and stops, so the screen clips it at white, and everything about its shape inside is there in the numbers but lost on screen. The wood gets a faint highlight too, white at 4% head on, as anything that isn't a metal does.
stage-3-a-microfacet-highlight.metal45 new lines since stage 2
// Stage 3 · A microfacet highlight
// The surface is countless tiny mirrors. GGX counts those
// facing halfway between eye and light: rougher is wider
// and dimmer. The lamp's highlight is far brighter than white.
// @param rough 0.3 0.12 0.7 How rough the copper is: mirror to satin
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// Copper's reflectance head on, in linear red, green and blue.
constant float3 COPPER = float3(0.955, 0.638, 0.538);
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
// Schlick's Fresnel with a color: f0 head on, white at a glance.
float3 schlick(float3 f0, float c) {
return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0);
}
// GGX: the share of tiny mirrors that face along h.
float ggx(float nh, float a) {
float a2 = a * a;
float k = nh * nh * (a2 - 1.0) + 1.0;
return a2 / (PI * k * k);
}
// Smith: mirrors that hide each other, over 4 (n.l)(n.v).
float smithV(float nl, float nv, float a) {
float a2 = a * a;
float gv = nl * sqrt(nv * nv * (1.0 - a2) + a2);
float gl = nv * sqrt(nl * nl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// One light's microfacet highlight: Fresnel times GGX times Smith.
float3 microfacet(float3 n, float3 v, float3 l, float3 f0, float a) {
float3 h = normalize(v + l);
float nl = max(dot(n, l), 0.0), nv = max(dot(n, v), 1e-3);
float d = ggx(max(dot(n, h), 0.0), a);
float vis = smithV(nl, nv, a);
return schlick(f0, dot(v, h)) * d * vis * nl;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
// And a faint highlight, white, 4% head on.
col = diffuse + microfacet(n, v, l, float3(0.04), 0.3)
* lampLight * lit;
} else {
// Copper's roughness: GGX takes its square.
float alpha = p.rough * p.rough;
float3 f0 = COPPER;
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, alpha, t, p);
// Two microfacet highlights: the lamp and the kiln.
col += microfacet(n, v, l, f0, alpha) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, alpha) * kilnLight;
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard. It simmers, low noise that rises as it heats,
// with bubbles that come quicker; the copper ticks as it swells and
// shrinks, duller the rougher it is.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
// A tick of thin copper `since` seconds ago: three partials out of tune
// with each other, as a metal sheet's are, dying faster when rough.
float tink(float since, float hz, float rough) {
if (since < 0.0) return 0.0;
float die = 25.0 + 90.0 * rough;
float rise = 1.0 - exp(-since * 2000.0);
return rise * (sin(TAU * hz * since) * exp(-since * die)
+ 0.5 * sin(TAU * hz * 2.31 * since) * exp(-since * die * 1.7)
+ 0.25 * sin(TAU * hz * 3.92 * since) * exp(-since * die * 2.6));
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
// Bubbles: forty chances a second, more of them taken when hot.
// This slot's and the last's, so none is cut off as the next begins.
float2 pops = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 40.0) - float(j);
uint id = uint(max(slot, 0.0));
// Each bubble takes the heat of its own moment, so none is
// switched on or off halfway.
float hot = boilAt(slot / 40.0);
if (hashU(id * 3u + 11u) < 0.05 + 0.6 * hot * hot) {
float since = t - slot / 40.0;
float hz = 220.0 + 500.0 * hashU(id * 3u + 12u) + 300.0 * hot;
float env = since * 150.0 * exp(1.0 - since * 150.0);
float pan = hashU(id * 3u + 13u);
pops += float2(1.0 - pan, pan) * env
* sin(TAU * hz * since * (1.0 + 4.0 * since));
}
}
// Ticks: the copper swells as it heats and shrinks as it cools,
// ticking most while the heat changes fastest.
float2 ticks = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 3.0) - float(j);
uint id = uint(max(slot, 0.0));
float change = abs(sin(TAU * slot / 3.0 / LOOP));
if (hashU(id + 500u) < 0.7 * change) {
float hz = 1900.0 + 1400.0 * hashU(id + 600u);
float at = slot / 3.0 + 0.3 * hashU(id + 700u);
float pan = hashU(id + 800u);
ticks += float2(1.0 - pan, pan) * tink(t - at, hz, p.rough);
}
}
float2 all = simmer + pops * 0.12 + ticks * 0.3;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * p.volume * fade;
}3 lines from stage 2 that are gone
col = diffuse;
⋯
col = schlick(f0, nv) * roomSeen(r, 0.0, t, p);
⋯
float2 all = simmer + pops * 0.12;Stage 4 · Brushed copper
The kettle's body and lid are brushed round the axis, as copper spun on a lathe is. The brushing's direction at a point is tg, round the axis, float3(pos.z, 0, -pos.x) made to lie in the surface, and bt is across it, up and down the profile. ggxAniso is GGX with two roughnesses: at along the brushing, smaller, and ab across it, larger, 1 / (π at ab k²) with k = (h·tg / at)² + (h·bt / ab)² + (h·n)². With brush at its default of 0.7, at is 0.475 times alpha and ab 2.75 times, nearly six times as rough across as along. Rough across the grooves means the facets tilt up and down a lot, so the highlight spreads up and down: across the brushing, which runs round. It becomes a tall streak. Near the axis (the middle of the lid) the brushing has no direction, so it fades out there, and the spout and handle stay unbrushed.
Each ring of grooves is brushed a little finer or coarser. grooves is a smooth random number for each of the rings, 70 to a unit of height and radius, plus a slower one, and at is scaled by exp(1.4 g × brushed), so some rings hold a brighter, narrower streak and some a dimmer, wider one. The streak breaks into fine bands across, and away from the highlight the body shows the rings as a faint striping. The room's reflection is blurred by sqrt(at · ab).
Inside the streak the bands are mostly brighter than white, so on a normal screen the middle of the streak is a flat white bar, and the bands show only along its edges.
stage-4-brushed-copper.metal45 new lines since stage 3
// Stage 4 · Brushed copper
// Brushed round its axis, the copper is smooth along its
// grooves and rough across them, so the highlight stretches
// into a streak across the brushing.
// @param rough 0.3 0.12 0.7 How rough the copper is: mirror to satin
// @param brush 0.7 0 1 How strongly it is brushed: round spot to streak
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// Copper's reflectance head on, in linear red, green and blue.
constant float3 COPPER = float3(0.955, 0.638, 0.538);
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A random number from 0 to 1 for a number.
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
// Schlick's Fresnel with a color: f0 head on, white at a glance.
float3 schlick(float3 f0, float c) {
return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0);
}
// GGX stretched: roughness at along the brushing, ab across it.
float ggxAniso(float3 h, float3 n, float3 tg, float3 bt,
float at, float ab) {
float x = dot(h, tg) / at, y = dot(h, bt) / ab, z = dot(h, n);
float k = x * x + y * y + z * z;
return 1.0 / (PI * at * ab * k * k);
}
// Smith: mirrors that hide each other, over 4 (n.l)(n.v).
float smithV(float nl, float nv, float a) {
float a2 = a * a;
float gv = nl * sqrt(nv * nv * (1.0 - a2) + a2);
float gl = nv * sqrt(nl * nl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// One light's microfacet highlight: Fresnel times GGX times Smith.
float3 microfacet(float3 n, float3 v, float3 l, float3 f0,
float3 tg, float3 bt, float at, float ab) {
float3 h = normalize(v + l);
float nl = max(dot(n, l), 0.0), nv = max(dot(n, v), 1e-3);
float d = ggxAniso(h, n, tg, bt, at, ab);
float vis = smithV(nl, nv, sqrt(at * ab));
return schlick(f0, dot(v, h)) * d * vis * nl;
}
// The brushing's grooves: a smooth random number for each ring.
float grooves(float s) {
float i = floor(s), f = fract(s);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) - 0.5;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
// And a faint highlight, white, 4% head on.
float3 tg = normalize(cross(n, float3(0.0, 0.0, 1.0)));
float3 bt = cross(n, tg);
col = diffuse + microfacet(n, v, l, float3(0.04), tg, bt,
0.3, 0.3) * lampLight * lit;
} else {
// Copper's roughness: GGX takes its square.
float alpha = p.rough * p.rough;
// Brushed round the axis, as a lathe spins it.
float3 local = pos;
local.xz = rot(local.xz, a);
float3 tg = float3(pos.z, 0.0, -pos.x);
tg = normalize(tg - n * dot(n, tg) + float3(1e-4, 0.0, 0.0));
float3 bt = cross(n, tg);
float brushed = p.brush * smoothstep(0.03, 0.12, length(pos.xz))
* (id == 1.0 ? 1.0 : 0.0);
// Each ring of grooves is brushed a little finer or coarser.
float ring = (local.y + length(local.xz)) * 70.0;
float g = grooves(ring) + 0.7 * grooves(ring * 0.23 + 4.0);
float3 f0 = COPPER;
// Smooth along the grooves, rough across them.
float at = alpha * (1.0 - 0.75 * brushed) * exp(1.4 * g * brushed);
at = max(at, 0.004);
float ab = min(alpha * (1.0 + 2.5 * brushed), 0.9);
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, sqrt(at * ab), t, p);
// Two microfacet highlights: the lamp and the kiln.
col += microfacet(n, v, l, f0, tg, bt, at, ab) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, tg, bt, at, ab) * kilnLight;
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard. It simmers, low noise that rises as it heats,
// with bubbles that come quicker; the copper ticks as it swells and
// shrinks, duller the rougher it is; and at the boil it whistles, a
// tone that climbs with the heat and falls away as it cools.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
// A tick of thin copper `since` seconds ago: three partials out of tune
// with each other, as a metal sheet's are, dying faster when rough.
float tink(float since, float hz, float rough) {
if (since < 0.0) return 0.0;
float die = 25.0 + 90.0 * rough;
float rise = 1.0 - exp(-since * 2000.0);
return rise * (sin(TAU * hz * since) * exp(-since * die)
+ 0.5 * sin(TAU * hz * 2.31 * since) * exp(-since * die * 1.7)
+ 0.25 * sin(TAU * hz * 3.92 * since) * exp(-since * die * 2.6));
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
// Bubbles: forty chances a second, more of them taken when hot.
// This slot's and the last's, so none is cut off as the next begins.
float2 pops = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 40.0) - float(j);
uint id = uint(max(slot, 0.0));
// Each bubble takes the heat of its own moment, so none is
// switched on or off halfway.
float hot = boilAt(slot / 40.0);
if (hashU(id * 3u + 11u) < 0.05 + 0.6 * hot * hot) {
float since = t - slot / 40.0;
float hz = 220.0 + 500.0 * hashU(id * 3u + 12u) + 300.0 * hot;
float env = since * 150.0 * exp(1.0 - since * 150.0);
float pan = hashU(id * 3u + 13u);
pops += float2(1.0 - pan, pan) * env
* sin(TAU * hz * since * (1.0 + 4.0 * since));
}
}
// Ticks: the copper swells as it heats and shrinks as it cools,
// ticking most while the heat changes fastest.
float2 ticks = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 3.0) - float(j);
uint id = uint(max(slot, 0.0));
float change = abs(sin(TAU * slot / 3.0 / LOOP));
if (hashU(id + 500u) < 0.7 * change) {
float hz = 1900.0 + 1400.0 * hashU(id + 600u);
float at = slot / 3.0 + 0.3 * hashU(id + 700u);
float pan = hashU(id + 800u);
ticks += float2(1.0 - pan, pan) * tink(t - at, hz, p.rough);
}
}
// The whistle: its pitch follows the heat, 900 Hz cold to 1400
// at the boil. The phase is that pitch added up over time.
float cycles = 1150.0 * t - 250.0 * LOOP / TAU * sin(TAU * t / LOOP);
float phase = TAU * fract(cycles) + 1.6 * sin(TAU * 5.0 * t);
float blow = smoothstep(0.55, 0.95, heat);
float whistle = sin(phase) + 0.3 * sin(2.0 * phase + 0.4);
whistle = whistle * (0.8 + 0.2 * noise1(t, 40.0, 5u))
+ 0.3 * noise1(t, 3000.0, 6u);
whistle *= 0.22 * blow * blow;
float2 all = simmer + pops * 0.12 + ticks * 0.3
+ float2(0.4, 0.6) * whistle;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * p.volume * fade;
}13 lines from stage 3 that are gone
float ggx(float nh, float a) {
float a2 = a * a;
float k = nh * nh * (a2 - 1.0) + 1.0;
return a2 / (PI * k * k);
⋯
float3 microfacet(float3 n, float3 v, float3 l, float3 f0, float a) {
⋯
float d = ggx(max(dot(n, h), 0.0), a);
float vis = smithV(nl, nv, a);
⋯
col = diffuse + microfacet(n, v, l, float3(0.04), 0.3)
* lampLight * lit;
⋯
col = schlick(f0, nv) * roomSeen(r, alpha, t, p);
⋯
col += microfacet(n, v, l, f0, alpha) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, alpha) * kilnLight;
⋯
float2 all = simmer + pops * 0.12 + ticks * 0.3;Stage 5 · Copper Kettle
Old copper tarnishes. Where hands don't polish it, it grows a dark oxide. tarnishAt is fractal noise (vnoise3 at two sizes) plus a share that grows toward the foot, through a smoothstep, times tarnish. Where there is oxide, the head-on reflectance turns from copper's to a dark brown, float3(0.2, 0.07, 0.05), alpha rises to at least 0.3, and the brushing fades out. The kettle's lower half goes duller and darker in patches, and its highlight goes dim and wide there.
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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
// And a faint highlight, white, 4% head on.
float3 tg = normalize(cross(n, float3(0.0, 0.0, 1.0)));
float3 bt = cross(n, tg);
col = diffuse + microfacet(n, v, l, float3(0.04), tg, bt,
0.3, 0.3) * lampLight * lit;
} else {
// Copper's roughness: GGX takes its square.
float alpha = p.rough * p.rough;
// Brushed round the axis, as a lathe spins it.
float3 local = pos;
local.xz = rot(local.xz, a);
float3 tg = float3(pos.z, 0.0, -pos.x);
tg = normalize(tg - n * dot(n, tg) + float3(1e-4, 0.0, 0.0));
float3 bt = cross(n, tg);
float brushed = p.brush * smoothstep(0.03, 0.12, length(pos.xz))
* (id == 1.0 ? 1.0 : 0.0);
// Each ring of grooves is brushed a little finer or coarser.
float ring = (local.y + length(local.xz)) * 70.0;
float g = grooves(ring) + 0.7 * grooves(ring * 0.23 + 4.0);
// Oxide: darker, rougher and no longer brushed.
float ox = tarnishAt(local, p.tarnish);
float3 f0 = mix(COPPER, float3(0.2, 0.07, 0.05), ox);
alpha = mix(alpha, max(alpha, 0.3), ox);
brushed *= 1.0 - ox;
// Smooth along the grooves, rough across them.
float at = alpha * (1.0 - 0.75 * brushed) * exp(1.4 * g * brushed);
at = max(at, 0.004);
float ab = min(alpha * (1.0 + 2.5 * brushed), 0.9);
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, sqrt(at * ab), t, p);
// Two microfacet highlights: the lamp and the kiln.
col += microfacet(n, v, l, f0, tg, bt, at, ab) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, tg, bt, at, ab) * kilnLight;
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}copper-kettle.metal, the whole file27 new lines since stage 4
// Ray Kiln · Copper Kettle
//
// @episode Copper Kettle
// @length 60
// @variation 30
// @variations 1,3,4,5,6,8,9,11
// @still 30
// @teaches Metals, microfacet highlights, brushed and anisotropic shine
// @category Fragment
// @tags raymarching, signed distance fields, solids of revolution, metals, fresnel, microfacets, anisotropic reflection, high dynamic range, procedural sound
// @final Copper Kettle | Old copper tarnishes: dark oxide low down, where hands never polish it, duller and rougher.
//
// A copper kettle on a dark table, lit by a lamp and by the kiln's
// glow. A metal has no diffuse color: everything it shows is a
// reflection, tinted by the metal, and stronger at a glance (Schlick's
// Fresnel with copper's own color head on). Its shine is a microfacet
// highlight: the surface is taken as countless tiny mirrors, and GGX
// says how many of them face halfway between the eye and the light. A
// brushed surface is rough across its grooves and smooth along them, so
// the highlight stretches into a streak across the brushing. The
// highlight is far brighter than white: nothing here clips it.
// 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 rough 0.3 0.12 0.7 How rough the copper is: mirror to satin
// @param brush 0.7 0 1 How strongly it is brushed: round spot to streak
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param tarnish 0.45 0 1 How much dark oxide old copper has grown
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// Copper's reflectance head on, in linear red, green and blue.
constant float3 COPPER = float3(0.955, 0.638, 0.538);
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A random number from 0 to 1 for a number.
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// A random number for each whole-numbered cube of space.
float hash31(float3 p) {
uint3 v = uint3(int3(floor(p))) * 1664525u + 1013904223u;
v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
v ^= v >> 16u;
v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
return float(v.x) * (1.0 / 4294967295.0);
}
// Value noise in 3D: the cubes' numbers, blended smoothly.
float vnoise3(float3 p) {
float3 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash31(i), b = hash31(i + float3(1, 0, 0));
float c = hash31(i + float3(0, 1, 0)), d = hash31(i + float3(1, 1, 0));
float e = hash31(i + float3(0, 0, 1)), g = hash31(i + float3(1, 0, 1));
float h = hash31(i + float3(0, 1, 1)), k = hash31(i + float3(1, 1, 1));
float lo = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
float hi = mix(mix(e, g, f.x), mix(h, k, f.x), f.y);
return mix(lo, hi, f.z);
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
// Schlick's Fresnel with a color: f0 head on, white at a glance.
float3 schlick(float3 f0, float c) {
return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0);
}
// GGX stretched: roughness at along the brushing, ab across it.
float ggxAniso(float3 h, float3 n, float3 tg, float3 bt,
float at, float ab) {
float x = dot(h, tg) / at, y = dot(h, bt) / ab, z = dot(h, n);
float k = x * x + y * y + z * z;
return 1.0 / (PI * at * ab * k * k);
}
// Smith: mirrors that hide each other, over 4 (n.l)(n.v).
float smithV(float nl, float nv, float a) {
float a2 = a * a;
float gv = nl * sqrt(nv * nv * (1.0 - a2) + a2);
float gl = nv * sqrt(nl * nl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// One light's microfacet highlight: Fresnel times GGX times Smith.
float3 microfacet(float3 n, float3 v, float3 l, float3 f0,
float3 tg, float3 bt, float at, float ab) {
float3 h = normalize(v + l);
float nl = max(dot(n, l), 0.0), nv = max(dot(n, v), 1e-3);
float d = ggxAniso(h, n, tg, bt, at, ab);
float vis = smithV(nl, nv, sqrt(at * ab));
return schlick(f0, dot(v, h)) * d * vis * nl;
}
// The brushing's grooves: a smooth random number for each ring.
float grooves(float s) {
float i = floor(s), f = fract(s);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) - 0.5;
}
// Oxide, 0 to 1: low down and in patches, where hands don't polish.
float tarnishAt(float3 p, float amount) {
float n = 0.65 * vnoise3(p * 4.0) + 0.35 * vnoise3(p * 15.0 + 7.0);
float low = 1.0 - smoothstep(0.0, 0.7, p.y);
return smoothstep(0.25, 0.85, 0.6 * n + 0.7 * low) * amount;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
// And a faint highlight, white, 4% head on.
float3 tg = normalize(cross(n, float3(0.0, 0.0, 1.0)));
float3 bt = cross(n, tg);
col = diffuse + microfacet(n, v, l, float3(0.04), tg, bt,
0.3, 0.3) * lampLight * lit;
} else {
// Copper's roughness: GGX takes its square.
float alpha = p.rough * p.rough;
// Brushed round the axis, as a lathe spins it.
float3 local = pos;
local.xz = rot(local.xz, a);
float3 tg = float3(pos.z, 0.0, -pos.x);
tg = normalize(tg - n * dot(n, tg) + float3(1e-4, 0.0, 0.0));
float3 bt = cross(n, tg);
float brushed = p.brush * smoothstep(0.03, 0.12, length(pos.xz))
* (id == 1.0 ? 1.0 : 0.0);
// Each ring of grooves is brushed a little finer or coarser.
float ring = (local.y + length(local.xz)) * 70.0;
float g = grooves(ring) + 0.7 * grooves(ring * 0.23 + 4.0);
// Oxide: darker, rougher and no longer brushed.
float ox = tarnishAt(local, p.tarnish);
float3 f0 = mix(COPPER, float3(0.2, 0.07, 0.05), ox);
alpha = mix(alpha, max(alpha, 0.3), ox);
brushed *= 1.0 - ox;
// Smooth along the grooves, rough across them.
float at = alpha * (1.0 - 0.75 * brushed) * exp(1.4 * g * brushed);
at = max(at, 0.004);
float ab = min(alpha * (1.0 + 2.5 * brushed), 0.9);
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, sqrt(at * ab), t, p);
// Two microfacet highlights: the lamp and the kiln.
col += microfacet(n, v, l, f0, tg, bt, at, ab) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, tg, bt, at, ab) * kilnLight;
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard. It simmers, low noise that rises as it heats,
// with bubbles that come quicker; the copper ticks as it swells and
// shrinks, duller the rougher it is; and at the boil it whistles, a
// tone that climbs with the heat and falls away as it cools.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
// A tick of thin copper `since` seconds ago: three partials out of tune
// with each other, as a metal sheet's are, dying faster when rough.
float tink(float since, float hz, float rough) {
if (since < 0.0) return 0.0;
float die = 25.0 + 90.0 * rough;
float rise = 1.0 - exp(-since * 2000.0);
return rise * (sin(TAU * hz * since) * exp(-since * die)
+ 0.5 * sin(TAU * hz * 2.31 * since) * exp(-since * die * 1.7)
+ 0.25 * sin(TAU * hz * 3.92 * since) * exp(-since * die * 2.6));
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
// Bubbles: forty chances a second, more of them taken when hot.
// This slot's and the last's, so none is cut off as the next begins.
float2 pops = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 40.0) - float(j);
uint id = uint(max(slot, 0.0));
// Each bubble takes the heat of its own moment, so none is
// switched on or off halfway.
float hot = boilAt(slot / 40.0);
if (hashU(id * 3u + 11u) < 0.05 + 0.6 * hot * hot) {
float since = t - slot / 40.0;
float hz = 220.0 + 500.0 * hashU(id * 3u + 12u) + 300.0 * hot;
float env = since * 150.0 * exp(1.0 - since * 150.0);
float pan = hashU(id * 3u + 13u);
pops += float2(1.0 - pan, pan) * env
* sin(TAU * hz * since * (1.0 + 4.0 * since));
}
}
// Ticks: the copper swells as it heats and shrinks as it cools,
// ticking most while the heat changes fastest.
float2 ticks = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 3.0) - float(j);
uint id = uint(max(slot, 0.0));
float change = abs(sin(TAU * slot / 3.0 / LOOP));
if (hashU(id + 500u) < 0.7 * change) {
float hz = 1900.0 + 1400.0 * hashU(id + 600u);
float at = slot / 3.0 + 0.3 * hashU(id + 700u);
float pan = hashU(id + 800u);
ticks += float2(1.0 - pan, pan) * tink(t - at, hz, p.rough);
}
}
// The whistle: its pitch follows the heat, 900 Hz cold to 1400
// at the boil. The phase is that pitch added up over time.
float cycles = 1150.0 * t - 250.0 * LOOP / TAU * sin(TAU * t / LOOP);
float phase = TAU * fract(cycles) + 1.6 * sin(TAU * 5.0 * t);
float blow = smoothstep(0.55, 0.95, heat);
float whistle = sin(phase) + 0.3 * sin(2.0 * phase + 0.4);
whistle = whistle * (0.8 + 0.2 * noise1(t, 40.0, 5u))
+ 0.3 * noise1(t, 3000.0, 6u);
whistle *= 0.22 * blow * blow;
float2 all = simmer + pops * 0.12 + ticks * 0.3
+ float2(0.4, 0.6) * whistle;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * p.volume * fade;
}1 line from stage 4 that are gone
float3 f0 = COPPER;The sound
The kettle, heard, following the same heat as the kiln's glow, boilAt, 0 at the start of the minute and 1 at 30 seconds. The simmer is two bands of noise (noise1, joined 260 and 900 times a second on the left, 280 and 950 on the right), rising to five times as loud at the boil. Bubbles pop on top: forty chances a second, of which 5% are taken when cold and 65% at the boil (each by the heat at its own moment, so none is switched on or off halfway), each a short blip from 220 to 1,020 Hz that slides up as it fades, placed at random between left and right.
The copper ticks as it swells and shrinks with the heat. Three times a second there is a chance of a tick, taken most where the heat changes fastest, at 15 and 45 seconds, and never at the boil or when cold. A tick (tink) is three partials at 1, 2.31 and 3.92 times a pitch between 1,900 and 3,300 Hz, out of tune with one another as a thin metal sheet's are, and they die away at 25 + 90 × rough a second: the rougher the copper, the duller the tick.
From about 16 seconds to 44 the kettle whistles. Its pitch follows the heat, 1150 − 250 cos(2πt / 60) Hz, 900 cold and 1,400 at the boil, and its phase is that pitch added up over time (1150 t − 250 × 60 / 2π × sin(2πt / 60) cycles), so it glides without a jump. It wobbles 5 times a second by about 8 Hz, carries a little breath of noise and an octave, and sits a little to the right, the spout's side. Everything goes through a soft limit, tanh, and fades to silence for a few milliseconds where the minute comes round, so it loops without a click.
Over the minute at the defaults the peak is −5.2 dBFS and the RMS −19.3 dBFS: −29 in the first and last five seconds, where only the simmer and a few bubbles play, and −15.7 around the boil. rough at 0.12 or 0.7 changes the ticks but hardly the level (−5.1 and −5.4 dBFS peak, −19.3 and −19.4 RMS), and volume at 1 gives −3.2 and −17.4.
New in Ray Kiln
Exposure and zebra. A screen stops at white, and a shader doesn't have to. In the playground, press Exposure under the picture (or the E key) and a bar opens with a slider in stops, from −8 to +4 in half stops: each stop down halves the picture and each stop up doubles it, starting from the values the shader returned, before the screen clips them. So a highlight that was a flat white patch shows what is inside it. Zebra, on when the bar opens, draws still dark diagonal stripes over every pixel the screen shows clipped at white at the exposure you chose. Reset goes back to 0 stops, and Stop closes the bar. It is on screen only: the loupe, Scopes, a Save and an Export keep the shader's own picture. It works for a fragment shader drawn from uv alone, not for feedback, particles, cells, vertex or mesh episodes, or a SwiftUI effect.
This episode's highlight is made for it. Open it in the playground, drag the clock to about 33 seconds, press Pause and then Exposure. At 0 stops the zebra covers two large patches, the lamp's streak down the body, left of center, and the kiln's glow on the kettle's right side by the spout, and thin slivers on the handle and along the spout's edge. Drag the slider down to −3 stops: the stripes are gone, the room is nearly black, and the streak that was a white bar is a soft, graded highlight, brightest in a narrow core, with the brushing's bands across it.
Try this
- Set
brushto 0: the streak gathers back into round spots, one on the body and one on the lid, and the rings vanish. At 1 the streak gets thinner and taller. - Set
roughto 0.12: the streak narrows to a bright line, and near its top the lamp's frame shows in it as a dark cross. At 0.7 the copper is satin: the highlight spreads pale over most of the body. - Set
tarnishto 1: the lower half of the kettle goes dark brown in patches, and the highlight there goes dim and wide. - Change
COPPERto silver,float3(0.972, 0.960, 0.915): the kettle turns silver-gray, and only its right side, mirroring the kiln's glow, stays orange. - Give the copper a diffuse color it shouldn't have: in
shade, addcol += f0 * 0.1 * lampLight * max(dot(n, l), 0.0) * lit;before the two highlights. The body goes pale and milky, like pink plastic, and stops looking like metal.
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 |
|---|---|---|---|
rough | 0.3 | 0.12 to 0.7 | How rough the copper is: mirror to satin |
brush | 0.7 | 0 to 1 | How strongly it is brushed: round spot to streak |
lamp | 1 | 0.3 to 2.5 | How bright the lamp is |
tarnish | 0.45 | 0 to 1 | How much dark oxide old copper has grown |
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.
copper-kettle.metal
// Ray Kiln · Copper Kettle
//
// @episode Copper Kettle
// @length 60
// @variation 30
// @variations 1,3,4,5,6,8,9,11
// @still 30
// @teaches Metals, microfacet highlights, brushed and anisotropic shine
// @category Fragment
// @tags raymarching, signed distance fields, solids of revolution, metals, fresnel, microfacets, anisotropic reflection, high dynamic range, procedural sound
// @final Copper Kettle | Old copper tarnishes: dark oxide low down, where hands never polish it, duller and rougher.
//
// A copper kettle on a dark table, lit by a lamp and by the kiln's
// glow. A metal has no diffuse color: everything it shows is a
// reflection, tinted by the metal, and stronger at a glance (Schlick's
// Fresnel with copper's own color head on). Its shine is a microfacet
// highlight: the surface is taken as countless tiny mirrors, and GGX
// says how many of them face halfway between the eye and the light. A
// brushed surface is rough across its grooves and smooth along them, so
// the highlight stretches into a streak across the brushing. The
// highlight is far brighter than white: nothing here clips it.
// 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 rough 0.3 0.12 0.7 How rough the copper is: mirror to satin
// @param brush 0.7 0 1 How strongly it is brushed: round spot to streak
// @param lamp 1 0.3 2.5 How bright the lamp is
// @param tarnish 0.45 0 1 How much dark oxide old copper has grown
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float PI = 3.14159265;
// Everything comes round in this many seconds.
constant float LOOP = 60.0;
// Copper's reflectance head on, in linear red, green and blue.
constant float3 COPPER = float3(0.955, 0.638, 0.538);
// The lamp's cream and the kiln's ember, in linear light.
constant float3 CREAM = float3(1.0, 0.90, 0.73);
constant float3 EMBER = float3(1.0, 0.30, 0.03);
// The kiln's mouth: low, to the right and behind the kettle.
constant float3 KILN_AT = float3(0.78, 0.12, -0.61);
// Turns a pair of coordinates round by the angle a.
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);
}
// 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.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// A random number from 0 to 1 for a number.
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// A random number for each whole-numbered cube of space.
float hash31(float3 p) {
uint3 v = uint3(int3(floor(p))) * 1664525u + 1013904223u;
v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
v ^= v >> 16u;
v.x += v.y * v.z; v.y += v.z * v.x; v.z += v.x * v.y;
return float(v.x) * (1.0 / 4294967295.0);
}
// Value noise in 3D: the cubes' numbers, blended smoothly.
float vnoise3(float3 p) {
float3 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash31(i), b = hash31(i + float3(1, 0, 0));
float c = hash31(i + float3(0, 1, 0)), d = hash31(i + float3(1, 1, 0));
float e = hash31(i + float3(0, 0, 1)), g = hash31(i + float3(1, 0, 1));
float h = hash31(i + float3(0, 1, 1)), k = hash31(i + float3(1, 1, 1));
float lo = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
float hi = mix(mix(e, g, f.x), mix(h, k, f.x), f.y);
return mix(lo, hi, f.z);
}
// The smooth minimum: two shapes joined with a fillet.
float smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
// The kettle comes to the boil and cools once a minute: 0 to 1.
float boilAt(float t) {
float s = sin(PI * t / LOOP);
return s * s;
}
// Where the lamp shines from: high on the left, swaying slowly.
float3 lampDir(float t) {
float a = -0.75 + 0.35 * sin(TAU * 2.0 * t / LOOP);
return normalize(float3(sin(a), 0.9, cos(a)));
}
// The body's radius at height y: wide low down, a shoulder above.
float bodyRadius(float y) {
float low = 1.0 - smoothstep(0.0, 0.3, y);
return mix(0.78, 0.42, smoothstep(0.3, 0.9, y)) - 0.08 * low * low;
}
// A squashed sphere, its radii r.
float ellipsoid(float3 p, float3 r) {
float k0 = length(p / r);
float k1 = length(p / (r * r));
return k0 * (k0 - 1.0) / k1;
}
// A tube from a to b whose radius tapers from ra to rb.
float taper(float3 p, float3 a, float3 b, float ra, float rb) {
float3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// The kettle turned by a: distance, and 1 body, 2 spout, 3 wood.
float2 kettle(float3 p, float a) {
p.xz = rot(p.xz, a);
// A solid of revolution, cut below the lid, marched at 80%.
float r = length(p.xz);
float body = max(r - bodyRadius(p.y), max(-p.y, p.y - 0.88)) * 0.8;
// The lid: a rim and a low dome, with a wooden knob.
float2 rim = float2(r - 0.39, abs(p.y - 0.9) - 0.025);
float lid = length(max(rim, 0.0)) + min(max(rim.x, rim.y), 0.0) - 0.01;
lid = min(lid, ellipsoid(p - float3(0.0, 0.9, 0.0),
float3(0.36, 0.12, 0.36)));
float knob = length(p - float3(0.0, 1.07, 0.0)) - 0.065;
// The spout: a tapered tube, open at its tip.
float3 tip = float3(1.12, 0.8, 0.0);
float spout = taper(p, float3(0.5, 0.28, 0.0), tip, 0.13, 0.045);
spout = max(spout, 0.03 - length(p - tip));
// The handle: an arch on two lugs, a wooden grip on top.
float2 q = float2(length(p.xy - float2(0.0, 0.78)) - 0.47, p.z);
float arch = max(length(q) - 0.035, 0.85 - p.y);
float3 lug = float3(abs(p.x) - 0.45, p.y - 0.86, p.z);
arch = min(arch, length(lug) - 0.05);
float grip = taper(p, float3(-0.2, 1.25, 0.0), float3(0.2, 1.25, 0.0),
0.07, 0.07);
float copper = smin(body, lid, 0.02);
float2 d = float2(copper, 1.0);
float rest = smin(smin(spout, arch, 0.03), copper, 0.06);
if (rest < d.x - 0.001) d = float2(rest, 2.0);
float wood = min(knob, grip);
if (wood < d.x) d = float2(wood, 3.0);
return d;
}
// The way the surface faces: the slope of the distance.
float3 normalAt(float3 p, float a) {
const float2 e = float2(0.0015, 0.0);
return normalize(float3(
kettle(p + e.xyy, a).x - kettle(p - e.xyy, a).x,
kettle(p + e.yxy, a).x - kettle(p - e.yxy, a).x,
kettle(p + e.yyx, a).x - kettle(p - e.yyx, a).x));
}
// Darker where a ray to the light passes near the kettle.
float softShadow(float3 ro, float3 rd, float w, float a) {
float lit = 1.0;
float s = 0.02;
for (int i = 0; i < 40; i++) {
float h = kettle(ro + rd * s, a).x;
lit = min(lit, h / (w * s));
if (lit < 0.001 || s > 4.0) break;
s += clamp(h, 0.01, 0.25);
}
lit = clamp(lit, 0.0, 1.0);
return lit * lit * (3.0 - 2.0 * lit);
}
// The room seen along rd; blur softens it as a rough mirror does.
float3 roomSeen(float3 rd, float blur, float t, constant Params& p) {
float3 col = pow(kiln(0.1 + 0.12 * exp(-6.0 * abs(rd.y))), 2.2);
// The table: near-black, past the horizon.
float table = smoothstep(0.02 + blur, -0.02 - blur, rd.y);
col = mix(col, float3(0.006, 0.004, 0.004), table * 0.85);
// The lamp: a panel of four panes, out of sight behind us.
float3 l = lampDir(t);
float3 side = normalize(cross(float3(0.0, 1.0, 0.0), l));
float3 up = cross(l, side);
float2 f = float2(dot(rd, side), dot(rd, up)) / max(dot(rd, l), 0.05);
float w = 0.01 + blur;
float panel = smoothstep(0.28 + w, 0.28 - w, abs(f.x))
* smoothstep(0.2 + w, 0.2 - w, abs(f.y));
panel *= smoothstep(0.01, 0.01 + w, abs(f.x))
* smoothstep(0.01, 0.01 + w, abs(f.y));
panel *= step(0.0, dot(rd, l)) / (1.0 + 12.0 * blur);
col += CREAM * 4.0 * p.lamp * panel;
// The kiln's mouth glows hotter as the kettle nears the boil.
float spread = 1.0 + 4.0 * blur;
float glow = exp(-14.0 / spread * (1.0 - dot(rd, KILN_AT))) / spread;
col += EMBER * glow * (0.4 + 2.0 * boilAt(t));
return col;
}
// Schlick's Fresnel with a color: f0 head on, white at a glance.
float3 schlick(float3 f0, float c) {
return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0);
}
// GGX stretched: roughness at along the brushing, ab across it.
float ggxAniso(float3 h, float3 n, float3 tg, float3 bt,
float at, float ab) {
float x = dot(h, tg) / at, y = dot(h, bt) / ab, z = dot(h, n);
float k = x * x + y * y + z * z;
return 1.0 / (PI * at * ab * k * k);
}
// Smith: mirrors that hide each other, over 4 (n.l)(n.v).
float smithV(float nl, float nv, float a) {
float a2 = a * a;
float gv = nl * sqrt(nv * nv * (1.0 - a2) + a2);
float gl = nv * sqrt(nl * nl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// One light's microfacet highlight: Fresnel times GGX times Smith.
float3 microfacet(float3 n, float3 v, float3 l, float3 f0,
float3 tg, float3 bt, float at, float ab) {
float3 h = normalize(v + l);
float nl = max(dot(n, l), 0.0), nv = max(dot(n, v), 1e-3);
float d = ggxAniso(h, n, tg, bt, at, ab);
float vis = smithV(nl, nv, sqrt(at * ab));
return schlick(f0, dot(v, h)) * d * vis * nl;
}
// The brushing's grooves: a smooth random number for each ring.
float grooves(float s) {
float i = floor(s), f = fract(s);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) - 0.5;
}
// Oxide, 0 to 1: low down and in patches, where hands don't polish.
float tarnishAt(float3 p, float amount) {
float n = 0.65 * vnoise3(p * 4.0) + 0.35 * vnoise3(p * 15.0 + 7.0);
float low = 1.0 - smoothstep(0.0, 0.7, p.y);
return smoothstep(0.25, 0.85, 0.6 * n + 0.7 * low) * amount;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution * 2.0 / u.resolution.y;
float t = fmod(u.time, LOOP);
// The kettle swings a little each way and back once a minute.
float a = 0.35 + 0.45 * sin(TAU * t / LOOP);
// The camera looks down at it; a tall picture moves it back.
float fit = min(1.0, u.resolution.x / u.resolution.y * 1.1);
float3 eye = float3(0.0, 1.6, 4.4);
float3 fw = normalize(float3(0.25, 0.55, 0.0) - eye);
float3 right = normalize(cross(fw, float3(0.0, 1.0, 0.0)));
float3 up = cross(right, fw);
float3 rd = normalize(fw * 3.8 * fit + right * s.x + up * s.y);
// March to the kettle, starting where the ray enters a ball round it.
float dist = 0.0, id = 0.0;
float3 oc = eye - float3(0.2, 0.62, 0.0);
float bb = dot(oc, rd), cc = dot(oc, oc) - 1.5 * 1.5;
if (bb * bb - cc > 0.0) {
dist = max(-bb - sqrt(bb * bb - cc), 0.0);
for (int i = 0; i < 128; i++) {
float2 k = kettle(eye + rd * dist, a);
if (k.x < 0.0008) { id = k.y; break; }
dist += k.x;
if (dist > 7.0) break;
}
}
float floorAt = rd.y < 0.0 ? -eye.y / rd.y : 100.0;
// Two lights: the lamp, and the kiln, hotter at the boil.
float3 l = lampDir(t);
float3 lampLight = CREAM * 5.0 * p.lamp;
float3 kilnLight = EMBER * (0.25 + 1.1 * boilAt(t));
float3 col;
if (id == 0.0 && floorAt < 100.0) {
// The table: dark, lit by the lamp, the kettle's shadow on it.
float3 q = eye + rd * floorAt;
float lit = softShadow(q + float3(0.0, 0.002, 0.0), l, 0.08, a);
float pool = exp(-0.3 * dot(q.xz, q.xz));
float3 light = lampLight * l.y * lit * pool + kilnLight * KILN_AT.y;
col = float3(0.01, 0.007, 0.006) * light;
} else if (id == 0.0) {
col = roomSeen(rd, 0.0, t, p);
} else {
float3 pos = eye + rd * dist;
float3 n = normalAt(pos, a);
float3 v = -rd;
float lit = softShadow(pos + n * 0.004, l, 0.06, a);
if (id == 3.0) {
// Wood is no metal: a dark diffuse color of its own.
float3 diffuse = float3(0.03, 0.012, 0.007)
* (lampLight * max(dot(n, l), 0.0) * lit
+ roomSeen(n, 1.0, t, p));
// And a faint highlight, white, 4% head on.
float3 tg = normalize(cross(n, float3(0.0, 0.0, 1.0)));
float3 bt = cross(n, tg);
col = diffuse + microfacet(n, v, l, float3(0.04), tg, bt,
0.3, 0.3) * lampLight * lit;
} else {
// Copper's roughness: GGX takes its square.
float alpha = p.rough * p.rough;
// Brushed round the axis, as a lathe spins it.
float3 local = pos;
local.xz = rot(local.xz, a);
float3 tg = float3(pos.z, 0.0, -pos.x);
tg = normalize(tg - n * dot(n, tg) + float3(1e-4, 0.0, 0.0));
float3 bt = cross(n, tg);
float brushed = p.brush * smoothstep(0.03, 0.12, length(pos.xz))
* (id == 1.0 ? 1.0 : 0.0);
// Each ring of grooves is brushed a little finer or coarser.
float ring = (local.y + length(local.xz)) * 70.0;
float g = grooves(ring) + 0.7 * grooves(ring * 0.23 + 4.0);
// Oxide: darker, rougher and no longer brushed.
float ox = tarnishAt(local, p.tarnish);
float3 f0 = mix(COPPER, float3(0.2, 0.07, 0.05), ox);
alpha = mix(alpha, max(alpha, 0.3), ox);
brushed *= 1.0 - ox;
// Smooth along the grooves, rough across them.
float at = alpha * (1.0 - 0.75 * brushed) * exp(1.4 * g * brushed);
at = max(at, 0.004);
float ab = min(alpha * (1.0 + 2.5 * brushed), 0.9);
// No diffuse: the room, mirrored, tinted by Fresnel.
float3 r = reflect(rd, n);
float nv = max(dot(n, v), 0.0);
col = schlick(f0, nv) * roomSeen(r, sqrt(at * ab), t, p);
// Two microfacet highlights: the lamp and the kiln.
col += microfacet(n, v, l, f0, tg, bt, at, ab) * lampLight * lit;
col += microfacet(n, v, KILN_AT, f0, tg, bt, at, ab) * kilnLight;
}
}
// The screen's curve, no clamp: highlights stay over white.
return float4(pow(max(col, 0.0), float3(1.0 / 2.2)), 1.0);
}
// ---- The sound ---------------------------------------------------------
// The kettle, heard. It simmers, low noise that rises as it heats,
// with bubbles that come quicker; the copper ticks as it swells and
// shrinks, duller the rougher it is; and at the boil it whistles, a
// tone that climbs with the heat and falls away as it cools.
// Integer hash, so noise stays exact for minutes of sound.
float hashU(uint n) {
n = (n << 13u) ^ n;
n = n * (n * n * 15731u + 789221u) + 1376312589u;
return float(n & 0x7fffffffu) / float(0x7fffffff);
}
// Cracked Clay's sound noise: random values joined `rate` times a second.
float noise1(float t, float rate, uint seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
uint k = uint(i) + seed * 7919u;
return mix(hashU(k), hashU(k + 1u), f) * 2.0 - 1.0;
}
// A tick of thin copper `since` seconds ago: three partials out of tune
// with each other, as a metal sheet's are, dying faster when rough.
float tink(float since, float hz, float rough) {
if (since < 0.0) return 0.0;
float die = 25.0 + 90.0 * rough;
float rise = 1.0 - exp(-since * 2000.0);
return rise * (sin(TAU * hz * since) * exp(-since * die)
+ 0.5 * sin(TAU * hz * 2.31 * since) * exp(-since * die * 1.7)
+ 0.25 * sin(TAU * hz * 3.92 * since) * exp(-since * die * 2.6));
}
float2 sound(float t, constant Params& p) {
float heat = boilAt(t);
// The simmer: two bands of noise, louder as the water heats.
float2 simmer = float2(noise1(t, 260.0, 1u) + 0.4 * noise1(t, 900.0, 2u),
noise1(t, 280.0, 3u) + 0.4 * noise1(t, 950.0, 4u));
simmer *= 0.05 + 0.2 * heat;
// Bubbles: forty chances a second, more of them taken when hot.
// This slot's and the last's, so none is cut off as the next begins.
float2 pops = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 40.0) - float(j);
uint id = uint(max(slot, 0.0));
// Each bubble takes the heat of its own moment, so none is
// switched on or off halfway.
float hot = boilAt(slot / 40.0);
if (hashU(id * 3u + 11u) < 0.05 + 0.6 * hot * hot) {
float since = t - slot / 40.0;
float hz = 220.0 + 500.0 * hashU(id * 3u + 12u) + 300.0 * hot;
float env = since * 150.0 * exp(1.0 - since * 150.0);
float pan = hashU(id * 3u + 13u);
pops += float2(1.0 - pan, pan) * env
* sin(TAU * hz * since * (1.0 + 4.0 * since));
}
}
// Ticks: the copper swells as it heats and shrinks as it cools,
// ticking most while the heat changes fastest.
float2 ticks = float2(0.0);
for (int j = 0; j < 2; j++) {
float slot = floor(t * 3.0) - float(j);
uint id = uint(max(slot, 0.0));
float change = abs(sin(TAU * slot / 3.0 / LOOP));
if (hashU(id + 500u) < 0.7 * change) {
float hz = 1900.0 + 1400.0 * hashU(id + 600u);
float at = slot / 3.0 + 0.3 * hashU(id + 700u);
float pan = hashU(id + 800u);
ticks += float2(1.0 - pan, pan) * tink(t - at, hz, p.rough);
}
}
// The whistle: its pitch follows the heat, 900 Hz cold to 1400
// at the boil. The phase is that pitch added up over time.
float cycles = 1150.0 * t - 250.0 * LOOP / TAU * sin(TAU * t / LOOP);
float phase = TAU * fract(cycles) + 1.6 * sin(TAU * 5.0 * t);
float blow = smoothstep(0.55, 0.95, heat);
float whistle = sin(phase) + 0.3 * sin(2.0 * phase + 0.4);
whistle = whistle * (0.8 + 0.2 * noise1(t, 40.0, 5u))
+ 0.3 * noise1(t, 3000.0, 6u);
whistle *= 0.22 * blow * blow;
float2 all = simmer + pops * 0.12 + ticks * 0.3
+ float2(0.4, 0.6) * whistle;
// Silence where the minute comes round, so it loops without a click.
float round = fract(t / LOOP);
float fade = 1.0 - exp(-min(round, 1.0 - round) * LOOP * 30.0);
return tanh(all * 1.8) * 0.8 * 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] = [0.3, 0.7, 1, 0.45, 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 Light & Materials
Press ? for keyboard shortcuts.


