Topographic Map
A map of land that isn't there, seen from above. Its height is fractal noise, cut into even steps by contour lines, every fifth one heavier, each line the same width on screen however steep the ground, because the distance to a line in height is divided by how fast the height changes from one pixel to the next. The same slope lights the hills from the upper left, a sea fills the low ground behind a cream shore, and the map slides round a slow circle once a minute. A walker at the middle hears the map: a note for each step of height and a tick at each line crossed.
· Noise & Texture · Fragment shader
Teaches Contour lines of a height field, shading a map by its slope
Builds on Flowing Color Field (hash21, value noise, fractal noise), Floating 3D Scene, Endless Terrain, Wood & Marble (hash11)
Watch
The idea
A map can't show height as height, so it draws lines. A contour line joins the points of one height: follow it and you never climb or fall. Cut the height into even steps and draw a line at each, and the land's shape is in where the lines run and how close they crowd: far apart on gentle ground, packed tight on a cliff.
A shader can draw those lines for any height it can work out at a point. The height here is a height field like the land of Endless Terrain, seen from straight above instead of marched over from the side: one layer of broad value noise for the hills and valleys, and fractal noise for the detail on them, the vnoise and fbm of Flowing Color Field. The hard part is the width. A pixel knows how far its height is from the nearest step, but that is a distance in height, not on the screen. Divided by how much the height changes from one pixel to the next, it becomes a distance in pixels, and every line is the same width wherever it runs. That rate of change is the slope, found by trying the height a pixel to each side, and the same slope, taken over a wider step, lights the hills. It is built in four steps.
Stage 1 · A height field
Each pixel is a point of the map. s is the pixel's place with the picture's height running from −0.5 to 0.5, and q = s * scale + topoCenter(time) is where it is on the map, so the picture is scale map units tall: 2.2 at the default, and more land as it grows.
topoHeight is the land. broad is value noise at 0.45 times the map's scale, hills and valleys a couple of map units across. On it is fbm, five octaves of value noise, each 2.03 times finer and half as strong as the one before. The two are added, 0.45 of the broad and 0.9 of the detail, and moved and scaled so the height runs from about 0 in the valleys to about 1 on the tops (a little past both here and there). For now the height is the color, through the kiln palette: kiln(0.08 + 0.8 * height), low ground near-black and the tops white-hot.
topoCenter moves the map: its middle goes round a circle 1.4 map units in radius about the point (4.7, 2.3), once every 60 seconds at drift 1, so the minute ends where it began.
stage-1-a-height-field.metal
// Stage 1 · A height field
// The land is a height at every point: broad hills from one layer of
// value noise, and fractal noise for the detail on them. Each pixel is a
// point of the map seen from above, colored by its height through the
// kiln palette. The map slides round a circle.
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// Low ground dark, the tops white-hot.
float3 col = kiln(0.08 + 0.8 * height);
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The middle of the screen, heard: a drone that swells and brightens as
// the land under it rises.
float2 sound(float t, constant Params& p) {
float h = topoHeight(topoCenter(t, p));
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
float hum = sin(TAU * 220.0 * t) + 0.3 * sin(TAU * 330.0 * t);
hum *= 0.35 * clamp(h, 0.0, 1.2);
float2 all = float2(drone + 0.8 * hum, drone + hum);
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 0.8) * p.volume * fade;
}Stage 2 · Contour lines
gap is one step of height, 1 / lines: 1/12 at the default. level = height / gap counts the steps, the nearest whole one is k = floor(level + 0.5), and away = |level − k| · gap is how far the pixel's height is from that step's height. The lines are at 0 and every gap either side.
away is a height, and a line must be a width on the screen. topoSlope finds how fast the height climbs, by finite differences: the height a little to the right minus a little to the left, and above minus below, each divided by twice the step. Here the step is px, one pixel in map units (scale / resolution.y), and climb = length(slope) · px is how much the height changes from this pixel to the next. So away / climb is the distance to the line in pixels. topoLine turns that into ink: full within half the line's width, fading out over one pixel. The width is width pixels of a 720-line picture, scaled with the picture's height so a larger render draws the same lines. The lines are cream, kiln(0.95), over the height's colors.
stage-2-contour-lines.metal47 new lines since stage 1
// Stage 2 · Contour lines
// A contour joins the points of one height. Cut the height into even
// steps; how far a pixel is from the nearest step, divided by how much
// the height changes from one pixel to the next, is its distance from
// the line in pixels, so every line is the same width.
// @param lines 12 4 30 How many contour steps the height is cut into
// @param width 1.4 0.5 4 How wide a contour line is, in pixels of a 720-line picture
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
// The height's slope, sampled `e` to each side.
float2 topoSlope(float2 q, float e) {
float dx = topoHeight(q + float2(e, 0.0)) - topoHeight(q - float2(e, 0.0));
float dy = topoHeight(q + float2(0.0, e)) - topoHeight(q - float2(0.0, e));
return float2(dx, dy) / (2.0 * e);
}
// How much a line `width` pixels wide covers a pixel `away` from it in height.
float topoLine(float away, float climb, float width) {
float dist = away / max(climb, 1e-6);
return 1.0 - smoothstep(width * 0.5 - 0.5, width * 0.5 + 0.5, dist);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// Low ground dark, the tops white-hot.
float3 col = kiln(0.08 + 0.8 * height);
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
float w = p.width * u.resolution.y / 720.0;
float ink = topoLine(away, climb, w);
col = mix(col, kiln(0.95), ink * 0.9);
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The middle of the screen, heard: the drone of the land, and a tick
// each time the walker there crosses a contour line.
// The walker's contour step at time t. Their path is read on a fixed grid
// of times, LOOK apart, so a line is crossed at a grid time, the same one
// however the sound is cut up.
constant float LOOK = 0.0125;
constant int LOOKS = 24;
constant float WINDOW = LOOK * float(LOOKS);
float topoStepAt(float n, constant Params& p) {
return floor(topoHeight(topoCenter(n * LOOK, p)) * p.lines);
}
// How long ago (dir -1) or how soon (dir 1) the walker crosses a contour,
// up to WINDOW seconds; `crossed` is the higher of the two steps.
float topoCrossing(float t, float dir, constant Params& p,
thread float& crossed) {
float n0 = floor(t / LOOK);
crossed = -1.0;
for (int i = 0; i < LOOKS; i++) {
// Two neighboring grid times, going back from now or on from it;
// the step changes at the later one, b.
float a = dir < 0.0 ? n0 - float(i) - 1.0 : n0 + float(i);
float b = a + 1.0;
float ka = topoStepAt(a, p), kb = topoStepAt(b, p);
if (ka != kb) {
crossed = max(ka, kb);
return dir < 0.0 ? t - b * LOOK : b * LOOK - t;
}
}
return WINDOW;
}
float2 sound(float t, constant Params& p) {
float h = topoHeight(topoCenter(t, p));
// Time since the last line and until the next.
float lastLine = 0.0, nextLine = 0.0;
float since = topoCrossing(t, -1.0, p, lastLine);
float until = topoCrossing(t, 1.0, p, nextLine);
// The tick of a line crossed.
float tick = sin(TAU * 2600.0 * since) * exp(-since * 70.0);
tick *= smoothstep(0.0, 0.002, since);
// It dies away before the next line, so a new tick never cuts it off.
tick *= smoothstep(0.0, 0.01, until);
tick *= since < WINDOW ? 0.25 : 0.0;
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
float hum = sin(TAU * 220.0 * t) + 0.3 * sin(TAU * 330.0 * t);
hum *= 0.35 * clamp(h, 0.0, 1.2);
float2 all = float2(drone + 0.8 * hum, drone + hum) + tick;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 0.8) * p.volume * fade;
}1 line from stage 1 that are gone
float2 all = float2(drone + 0.8 * hum, drone + hum);Stage 3 · Index lines and tints
On a real map every fifth contour is an index line, drawn heavier so the eye can count the climb. Here k is an index line when it is a whole multiple of 5: at the default 12 lines, the heights 0, 5/12 and 10/12. Index lines are twice as wide, a pale gold (kiln(0.86)), and laid on at 0.95 against 0.8 for the others, whose color goes from orange-gold low down to pale gold on the tops.
On a steep slope the lines crowd, and lines closer together than their own width smear into a blur. spacing is how many pixels apart the lines are there, gap / climb (five times that for the index lines, which are five steps apart), and the ink fades out as the spacing falls from 3.5 widths to 1.5. The index lines last longest on a cliff, as they do on a printed map.
The land itself is darker now, kiln(0.14 + 0.5 · height) · 0.85, dark ember low down and gold on the tops, so that the lines glow on it.
stage-3-index-lines-and-tints.metal24 new lines since stage 2
// Stage 3 · Index lines and tints
// Every fifth contour, an index line, is drawn twice as wide and
// brighter, so the eye can count the climb. Where lines crowd on a steep
// slope they fade instead of smearing. The land darkens so the lines
// glow, and each step of height sounds a note.
// @param lines 12 4 30 How many contour steps the height is cut into
// @param width 1.4 0.5 4 How wide a contour line is, in pixels of a 720-line picture
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
// The height's slope, sampled `e` to each side.
float2 topoSlope(float2 q, float e) {
float dx = topoHeight(q + float2(e, 0.0)) - topoHeight(q - float2(e, 0.0));
float dy = topoHeight(q + float2(0.0, e)) - topoHeight(q - float2(0.0, e));
return float2(dx, dy) / (2.0 * e);
}
// How much a line `width` pixels wide covers a pixel `away` from it in height.
float topoLine(float away, float climb, float width) {
float dist = away / max(climb, 1e-6);
return 1.0 - smoothstep(width * 0.5 - 0.5, width * 0.5 + 0.5, dist);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// The land's tint: dark ember low down, gold on the tops.
float land = height;
float3 col = kiln(0.14 + 0.5 * land) * 0.85;
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
// Every fifth line, an index contour, is drawn twice as wide.
bool index = k - 5.0 * floor(k / 5.0) < 0.5;
float w = p.width * u.resolution.y / 720.0 * (index ? 2.0 : 1.0);
float ink = topoLine(away, climb, w);
// Where the lines crowd closer than a few widths, they fade out,
// except the index lines, which are five times as far apart.
float spacing = (index ? 5.0 : 1.0) * gap / max(climb, 1e-6);
ink *= smoothstep(w * 1.5, w * 3.5, spacing);
float3 inkColor = kiln(index ? 0.86 : 0.62 + 0.25 * land);
col = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The middle of the screen, heard: the walker's note is the contour step
// they stand on, a pentatonic scale climbing with the land, and each line
// crossed ticks; an index line ticks lower and louder.
// The walker's contour step at time t. Their path is read on a fixed grid
// of times, LOOK apart, so a line is crossed at a grid time, the same one
// however the sound is cut up.
constant float LOOK = 0.0125;
constant int LOOKS = 24;
constant float WINDOW = LOOK * float(LOOKS);
float topoStepAt(float n, constant Params& p) {
return floor(topoHeight(topoCenter(n * LOOK, p)) * p.lines);
}
// How long ago (dir -1) or how soon (dir 1) the walker crosses a contour,
// up to WINDOW seconds; `crossed` is the higher of the two steps.
float topoCrossing(float t, float dir, constant Params& p,
thread float& crossed) {
float n0 = floor(t / LOOK);
crossed = -1.0;
for (int i = 0; i < LOOKS; i++) {
// Two neighboring grid times, going back from now or on from it;
// the step changes at the later one, b.
float a = dir < 0.0 ? n0 - float(i) - 1.0 : n0 + float(i);
float b = a + 1.0;
float ka = topoStepAt(a, p), kb = topoStepAt(b, p);
if (ka != kb) {
crossed = max(ka, kb);
return dir < 0.0 ? t - b * LOOK : b * LOOK - t;
}
}
return WINDOW;
}
float2 sound(float t, constant Params& p) {
float2 c = topoCenter(t, p);
float h = topoHeight(c);
// The step the walker is on, read at the grid time just passed.
float k = topoStepAt(floor(t / LOOK), p);
// Time since the last line and until the next.
float lastLine = 0.0, nextLine = 0.0;
float since = topoCrossing(t, -1.0, p, lastLine);
float until = topoCrossing(t, 1.0, p, nextLine);
// The note of this step: a pentatonic scale over three octaves.
float step5 = k - 5.0 * floor(k / 5.0);
float semis = step5 == 0.0 ? 0.0 : (step5 == 1.0 ? 2.0
: (step5 == 2.0 ? 4.0 : (step5 == 3.0 ? 7.0 : 9.0)));
float octave = floor(k / 5.0);
octave -= 3.0 * floor(octave / 3.0);
float f = 110.0 * exp2((semis + 12.0 * octave) / 12.0);
// It sounds between lines and falls silent across each, so a new
// pitch never starts mid-wave.
float env = smoothstep(0.0, 0.04, since) * smoothstep(0.0, 0.04, until);
float tone = sin(TAU * f * t) * env * 0.22;
// The tick of a line crossed: an index line lower and louder.
bool index = lastLine - 5.0 * floor(lastLine / 5.0) < 0.5;
float tickF = index ? 1400.0 : 2600.0;
float tick = sin(TAU * tickF * since) * exp(-since * 70.0);
tick *= smoothstep(0.0, 0.002, since);
// It dies away before the next line, so a new tick never cuts it off.
tick *= smoothstep(0.0, 0.01, until);
tick *= since < WINDOW ? (index ? 0.3 : 0.16) : 0.0;
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
float2 all = float2(tone, tone) + tick + drone;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 0.8) * p.volume * fade;
}9 lines from stage 2 that are gone
float3 col = kiln(0.08 + 0.8 * height);
⋯
float w = p.width * u.resolution.y / 720.0;
⋯
col = mix(col, kiln(0.95), ink * 0.9);
⋯
float h = topoHeight(topoCenter(t, p));
⋯
float tick = sin(TAU * 2600.0 * since) * exp(-since * 70.0);
⋯
tick *= since < WINDOW ? 0.25 : 0.0;
⋯
float hum = sin(TAU * 220.0 * t) + 0.3 * sin(TAU * 330.0 * t);
hum *= 0.35 * clamp(h, 0.0, 1.2);
float2 all = float2(drone + 0.8 * hum, drone + hum) + tick;Stage 4 · Hill shading
The slope also says which way the ground faces. n = normalize(−hill · relief · 0.25, 1) is a surface tilted by the slope, its tilt grown by relief, and it is lit by a lamp at the upper left, normalize(−1, 1, 1.3). Faces turned toward the lamp brighten, faces turned away darken, and the flat map stands up in relief. lit is divided by the lamp's own height, so flat ground keeps its color and only the slopes change. The color is multiplied by lit², mixed in by relief up to 1; above 1, only the tilt keeps growing.
The light takes its slope from topoSlope(q, 0.03): 0.03 map units to each side, about ten pixels of a 720-line picture at the default scale, where the lines' slope is taken one pixel to each side. The wider step averages away the smallest bumps of the noise, so the shading shows the hills and not the grain on them.
stage-4-hill-shading.metal14 new lines since stage 3
// Stage 4 · Hill shading
// The slope also tilts a surface, lit by a lamp at the upper left: faces
// turned to it brighten, faces turned away darken, and the flat map
// stands up in relief. The light takes the slope over a wider step than
// the lines, so the smallest bumps don't speckle it.
// @param lines 12 4 30 How many contour steps the height is cut into
// @param width 1.4 0.5 4 How wide a contour line is, in pixels of a 720-line picture
// @param relief 1.2 0 3 How strongly the slopes are lit and shaded
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
// The height's slope, sampled `e` to each side.
float2 topoSlope(float2 q, float e) {
float dx = topoHeight(q + float2(e, 0.0)) - topoHeight(q - float2(e, 0.0));
float dy = topoHeight(q + float2(0.0, e)) - topoHeight(q - float2(0.0, e));
return float2(dx, dy) / (2.0 * e);
}
// How much a line `width` pixels wide covers a pixel `away` from it in height.
float topoLine(float away, float climb, float width) {
float dist = away / max(climb, 1e-6);
return 1.0 - smoothstep(width * 0.5 - 0.5, width * 0.5 + 0.5, dist);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// The light takes the slope over a wider step, which smooths out
// the smallest bumps.
float2 hill = topoSlope(q, 0.03);
// The land's tint: dark ember low down, gold on the tops.
float land = height;
float3 col = kiln(0.14 + 0.5 * land) * 0.85;
// Hill shading: a surface tilted by the slope, lit from the upper left.
float3 n = normalize(float3(-hill * p.relief * 0.25, 1.0));
float3 lamp = normalize(float3(-1.0, 1.0, 1.3));
float lit = max(dot(n, lamp), 0.0) / lamp.z;
col *= mix(1.0, lit * lit, min(p.relief, 1.0));
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
// Every fifth line, an index contour, is drawn twice as wide.
bool index = k - 5.0 * floor(k / 5.0) < 0.5;
float w = p.width * u.resolution.y / 720.0 * (index ? 2.0 : 1.0);
float ink = topoLine(away, climb, w);
// Where the lines crowd closer than a few widths, they fade out,
// except the index lines, which are five times as far apart.
float spacing = (index ? 5.0 : 1.0) * gap / max(climb, 1e-6);
ink *= smoothstep(w * 1.5, w * 3.5, spacing);
float3 inkColor = kiln(index ? 0.86 : 0.62 + 0.25 * land);
col = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The middle of the screen, heard: the note of the contour step, a tick at
// each line, and now the slope: the steeper the ground, the brighter the
// note, and it leans toward the side the ground falls away to.
// The walker's contour step at time t. Their path is read on a fixed grid
// of times, LOOK apart, so a line is crossed at a grid time, the same one
// however the sound is cut up.
constant float LOOK = 0.0125;
constant int LOOKS = 24;
constant float WINDOW = LOOK * float(LOOKS);
float topoStepAt(float n, constant Params& p) {
return floor(topoHeight(topoCenter(n * LOOK, p)) * p.lines);
}
// How long ago (dir -1) or how soon (dir 1) the walker crosses a contour,
// up to WINDOW seconds; `crossed` is the higher of the two steps.
float topoCrossing(float t, float dir, constant Params& p,
thread float& crossed) {
float n0 = floor(t / LOOK);
crossed = -1.0;
for (int i = 0; i < LOOKS; i++) {
// Two neighboring grid times, going back from now or on from it;
// the step changes at the later one, b.
float a = dir < 0.0 ? n0 - float(i) - 1.0 : n0 + float(i);
float b = a + 1.0;
float ka = topoStepAt(a, p), kb = topoStepAt(b, p);
if (ka != kb) {
crossed = max(ka, kb);
return dir < 0.0 ? t - b * LOOK : b * LOOK - t;
}
}
return WINDOW;
}
float2 sound(float t, constant Params& p) {
float2 c = topoCenter(t, p);
float h = topoHeight(c);
float2 g = topoSlope(c, 0.01);
// The step the walker is on, read at the grid time just passed.
float k = topoStepAt(floor(t / LOOK), p);
// Time since the last line and until the next.
float lastLine = 0.0, nextLine = 0.0;
float since = topoCrossing(t, -1.0, p, lastLine);
float until = topoCrossing(t, 1.0, p, nextLine);
// The note of this step: a pentatonic scale over three octaves.
float step5 = k - 5.0 * floor(k / 5.0);
float semis = step5 == 0.0 ? 0.0 : (step5 == 1.0 ? 2.0
: (step5 == 2.0 ? 4.0 : (step5 == 3.0 ? 7.0 : 9.0)));
float octave = floor(k / 5.0);
octave -= 3.0 * floor(octave / 3.0);
float f = 110.0 * exp2((semis + 12.0 * octave) / 12.0);
// It sounds between lines and falls silent across each, so a new
// pitch never starts mid-wave.
float env = smoothstep(0.0, 0.04, since) * smoothstep(0.0, 0.04, until);
float bright = clamp(length(g) * 0.4, 0.0, 1.0);
float tone = sin(TAU * f * t) + 0.35 * bright * sin(TAU * 2.0 * f * t)
+ 0.2 * bright * sin(TAU * 3.0 * f * t);
tone *= env * 0.22;
// The tick of a line crossed: an index line lower and louder.
bool index = lastLine - 5.0 * floor(lastLine / 5.0) < 0.5;
float tickF = index ? 1400.0 : 2600.0;
float tick = sin(TAU * tickF * since) * exp(-since * 70.0);
tick *= smoothstep(0.0, 0.002, since);
// It dies away before the next line, so a new tick never cuts it off.
tick *= smoothstep(0.0, 0.01, until);
tick *= since < WINDOW ? (index ? 0.3 : 0.16) : 0.0;
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
// The note leans toward the side the ground falls away to.
float lean = 0.5 + 0.35 * clamp(-g.x * 0.5, -1.0, 1.0);
float2 all = float2(tone * (1.0 - lean), tone * lean) * 2.0
+ float2(tick * lean, tick * (1.0 - lean)) * 2.0
+ drone;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 0.8) * p.volume * fade;
}2 lines from stage 3 that are gone
float tone = sin(TAU * f * t) * env * 0.22;
⋯
float2 all = float2(tone, tone) + tick + drone;Stage 5 · Topographic Map
The finished map has a sea. Everything lower than sea is under water: a flat, dark color, a little warmer toward the shore (depth, the height as a share of the sea level), with the contour lines showing through faintly, at 0.3 of their ink. below is how far under the shore a pixel is, in pixels, (sea − height) / climb, smoothed over one pixel, so the coast is as sharp at any zoom as the lines are. The shore is a cream line where the height equals sea, drawn by topoLine like a contour, a little wider. The land's tints now start at the shore: land runs from 0 at the sea's level to 1 at the top, so the dark ember is along the coast and the gold is still on the peaks. A sea level of 0 means no sea at all.
Last, the paper: each pixel is darkened or brightened a little by a hash of where it is on the screen, from 0.94 to 1.02 of its color. The grain is fixed to the sheet, not the land, so the map slides under it.
the kiln palette and shade(), from the finished shader
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// The light takes the slope over a wider step, which smooths out
// the smallest bumps.
float2 hill = topoSlope(q, 0.03);
// The land's tint now runs from the sea level up.
float land = (height - p.sea) / max(1.0 - p.sea, 0.05);
float3 col = kiln(0.14 + 0.5 * land) * 0.85;
// Hill shading: a surface tilted by the slope, lit from the upper left.
float3 n = normalize(float3(-hill * p.relief * 0.25, 1.0));
float3 lamp = normalize(float3(-1.0, 1.0, 1.3));
float lit = max(dot(n, lamp), 0.0) / lamp.z;
col *= mix(1.0, lit * lit, min(p.relief, 1.0));
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
// Every fifth line, an index contour, is drawn twice as wide.
bool index = k - 5.0 * floor(k / 5.0) < 0.5;
float w = p.width * u.resolution.y / 720.0 * (index ? 2.0 : 1.0);
float ink = topoLine(away, climb, w);
// Where the lines crowd closer than a few widths, they fade out,
// except the index lines, which are five times as far apart.
float spacing = (index ? 5.0 : 1.0) * gap / max(climb, 1e-6);
ink *= smoothstep(w * 1.5, w * 3.5, spacing);
float3 inkColor = kiln(index ? 0.86 : 0.62 + 0.25 * land);
float3 lined = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
// The sea: dark water over the low ground, its depth lines faint.
float depth = clamp(height / max(p.sea, 0.01), 0.0, 1.0);
float3 water = float3(0.02, 0.016, 0.026)
+ float3(0.07, 0.03, 0.03) * depth;
water = mix(water, kiln(0.3), ink * 0.3);
// How far below the shore, in pixels.
float below = clamp((p.sea - height) / max(climb, 1e-6) + 0.5, 0.0, 1.0);
// A sea level of 0 means no sea.
below *= step(0.001, p.sea);
col = mix(lined, water, below);
// The shore, in cream.
float shore = topoLine(abs(height - p.sea), climb, w * 1.3);
col = mix(col, kiln(0.95), shore * step(0.001, p.sea));
// Paper grain: a faint, fixed speckle.
float grain = hash21(floor(uv * u.resolution));
col *= 0.94 + 0.08 * grain;
col = pow(col, float3(0.9));
return float4(col, 1.0);
}topographic-map.metal, the whole file29 new lines since stage 4
// Ray Kiln · Topographic Map
//
// @episode Topographic Map
// @length 60
// @variation 30
// @variations 1,2,4,6,9,10,11,12
// @sweeps lines, sea, relief
// @still 30
// @teaches Contour lines of a height field, shading a map by its slope
// @category Fragment
// @tags contour lines, height field, fractal noise, finite differences, hill shading, procedural sound
// @final Topographic Map | A sea fills the low ground, the shore is drawn in cream, and the map is printed on grained paper.
//
// A map of land that isn't there, seen from above. Its height is fractal
// noise. A contour line joins the points of one height, at even steps,
// and every fifth is drawn heavier. Each line is the same width on the
// screen wherever it runs: the distance to the nearest level, in height,
// is divided by how fast the height changes there, which comes from
// sampling the height one pixel to each side. The slope, sampled over a
// wider step, also lights the hills from the upper left. The map slides
// round a slow circle once a minute and comes back where it began.
// 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 lines 12 4 30 How many contour steps the height is cut into
// @param width 1.4 0.5 4 How wide a contour line is, in pixels of a 720-line picture
// @param relief 1.2 0 3 How strongly the slopes are lit and shaded
// @param sea 0.32 0 0.7 The sea's level, as a share of the height
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
// The height's slope, sampled `e` to each side.
float2 topoSlope(float2 q, float e) {
float dx = topoHeight(q + float2(e, 0.0)) - topoHeight(q - float2(e, 0.0));
float dy = topoHeight(q + float2(0.0, e)) - topoHeight(q - float2(0.0, e));
return float2(dx, dy) / (2.0 * e);
}
// How much a line `width` pixels wide covers a pixel `away` from it in height.
float topoLine(float away, float climb, float width) {
float dist = away / max(climb, 1e-6);
return 1.0 - smoothstep(width * 0.5 - 0.5, width * 0.5 + 0.5, dist);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// The light takes the slope over a wider step, which smooths out
// the smallest bumps.
float2 hill = topoSlope(q, 0.03);
// The land's tint now runs from the sea level up.
float land = (height - p.sea) / max(1.0 - p.sea, 0.05);
float3 col = kiln(0.14 + 0.5 * land) * 0.85;
// Hill shading: a surface tilted by the slope, lit from the upper left.
float3 n = normalize(float3(-hill * p.relief * 0.25, 1.0));
float3 lamp = normalize(float3(-1.0, 1.0, 1.3));
float lit = max(dot(n, lamp), 0.0) / lamp.z;
col *= mix(1.0, lit * lit, min(p.relief, 1.0));
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
// Every fifth line, an index contour, is drawn twice as wide.
bool index = k - 5.0 * floor(k / 5.0) < 0.5;
float w = p.width * u.resolution.y / 720.0 * (index ? 2.0 : 1.0);
float ink = topoLine(away, climb, w);
// Where the lines crowd closer than a few widths, they fade out,
// except the index lines, which are five times as far apart.
float spacing = (index ? 5.0 : 1.0) * gap / max(climb, 1e-6);
ink *= smoothstep(w * 1.5, w * 3.5, spacing);
float3 inkColor = kiln(index ? 0.86 : 0.62 + 0.25 * land);
float3 lined = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
// The sea: dark water over the low ground, its depth lines faint.
float depth = clamp(height / max(p.sea, 0.01), 0.0, 1.0);
float3 water = float3(0.02, 0.016, 0.026)
+ float3(0.07, 0.03, 0.03) * depth;
water = mix(water, kiln(0.3), ink * 0.3);
// How far below the shore, in pixels.
float below = clamp((p.sea - height) / max(climb, 1e-6) + 0.5, 0.0, 1.0);
// A sea level of 0 means no sea.
below *= step(0.001, p.sea);
col = mix(lined, water, below);
// The shore, in cream.
float shore = topoLine(abs(height - p.sea), climb, w * 1.3);
col = mix(col, kiln(0.95), shore * step(0.001, p.sea));
// Paper grain: a faint, fixed speckle.
float grain = hash21(floor(uv * u.resolution));
col *= 0.94 + 0.08 * grain;
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The same map, heard. A walker stands at the middle of the screen and
// the map slides under them. Their note is the contour step they are on,
// a pentatonic scale climbing with the land, and each time they cross a
// line it ticks; an index line ticks lower and louder. The steeper the
// ground, the brighter the note. Under the sea, the note gives way to surf.
// The walker's contour step at time t. Their path is read on a fixed grid
// of times, LOOK apart, so a line is crossed at a grid time, the same one
// however the sound is cut up.
constant float LOOK = 0.0125;
constant int LOOKS = 24;
constant float WINDOW = LOOK * float(LOOKS);
float topoStepAt(float n, constant Params& p) {
return floor(topoHeight(topoCenter(n * LOOK, p)) * p.lines);
}
// How long ago (dir -1) or how soon (dir 1) the walker crosses a contour,
// up to WINDOW seconds; `crossed` is the higher of the two steps.
float topoCrossing(float t, float dir, constant Params& p,
thread float& crossed) {
float n0 = floor(t / LOOK);
crossed = -1.0;
for (int i = 0; i < LOOKS; i++) {
// Two neighboring grid times, going back from now or on from it;
// the step changes at the later one, b.
float a = dir < 0.0 ? n0 - float(i) - 1.0 : n0 + float(i);
float b = a + 1.0;
float ka = topoStepAt(a, p), kb = topoStepAt(b, p);
if (ka != kb) {
crossed = max(ka, kb);
return dir < 0.0 ? t - b * LOOK : b * LOOK - t;
}
}
return WINDOW;
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float topoSurf(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float2 c = topoCenter(t, p);
float h = topoHeight(c);
float2 g = topoSlope(c, 0.01);
// The step the walker is on, read at the grid time just passed.
float k = topoStepAt(floor(t / LOOK), p);
// Time since the last line and until the next.
float lastLine = 0.0, nextLine = 0.0;
float since = topoCrossing(t, -1.0, p, lastLine);
float until = topoCrossing(t, 1.0, p, nextLine);
// The note of this step: a pentatonic scale over three octaves.
float step5 = k - 5.0 * floor(k / 5.0);
float semis = step5 == 0.0 ? 0.0 : (step5 == 1.0 ? 2.0
: (step5 == 2.0 ? 4.0 : (step5 == 3.0 ? 7.0 : 9.0)));
float octave = floor(k / 5.0);
octave -= 3.0 * floor(octave / 3.0);
float f = 110.0 * exp2((semis + 12.0 * octave) / 12.0);
// It sounds between lines and falls silent across each, so a new
// pitch never starts mid-wave.
float env = smoothstep(0.0, 0.04, since) * smoothstep(0.0, 0.04, until);
float bright = clamp(length(g) * 0.4, 0.0, 1.0);
float tone = sin(TAU * f * t) + 0.35 * bright * sin(TAU * 2.0 * f * t)
+ 0.2 * bright * sin(TAU * 3.0 * f * t);
tone *= env * 0.22;
// The tick of a line crossed: an index line lower and louder.
bool index = lastLine - 5.0 * floor(lastLine / 5.0) < 0.5;
float tickF = index ? 1400.0 : 2600.0;
float tick = sin(TAU * tickF * since) * exp(-since * 70.0);
tick *= smoothstep(0.0, 0.002, since);
// It dies away before the next line, so a new tick never cuts it off.
tick *= smoothstep(0.0, 0.01, until);
tick *= since < WINDOW ? (index ? 0.3 : 0.16) : 0.0;
// Under the sea the note sinks into surf.
float wet = 1.0 - smoothstep(p.sea - 0.03, p.sea + 0.01, h);
wet *= step(0.001, p.sea);
float swell = 0.6 + 0.4 * sin(TAU * t / 7.0);
float surf = 0.6 * topoSurf(t, 500.0) + 0.4 * topoSurf(t + 3.7, 1300.0);
tone *= 1.0 - 0.85 * wet;
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
// The note leans toward the side the ground falls away to.
float lean = 0.5 + 0.35 * clamp(-g.x * 0.5, -1.0, 1.0);
float2 all = float2(tone * (1.0 - lean), tone * lean) * 2.0
+ float2(tick * lean, tick * (1.0 - lean)) * 2.0
+ drone + float2(surf, topoSurf(t + 9.1, 600.0))
* 0.12 * wet * swell;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 0.8) * p.volume * fade;
}3 lines from stage 4 that are gone
float land = height;
⋯
col = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
⋯
+ drone;The sound
The same map, heard. A walker stands at the middle of the screen, and the map slides under them. Their note is the contour step they are on, floor(height · lines), played on a pentatonic scale climbing with the land: A, B, C♯, E and F♯ from A2 (110 Hz), the next five steps an octave up, wrapping round after three octaves. The note falls silent for 0.04 s either side of each line, so a new pitch never starts in the middle of a wave. The steeper the ground under the walker, the brighter the note: its second and third harmonics come in with the slope.
Each line crossed ticks, a short ping at 2.6 kHz, and an index line ticks lower and louder, at 1.4 kHz. The walker's path is read 80 times a second, on a fixed grid of times, and a crossing is looked for up to 0.3 s before and after the present, so each tick falls at the same moment however the sound is cut up. The note leans toward the side the ground falls away to, and the tick toward the side it rises to. Under it a low drone on A, 55 Hz with the E above it at 82.5 Hz, swells as the land under the walker rises.
When the walker is under the sea, the note sinks to 15% of its level and gives way to surf: noise made by hashing the time and smoothing between the hashes, swelling and falling over 7 seconds. The sound falls to silence for a moment where the minute comes round, so the loop joins without a click.
At the defaults the peak is −11.1 dBFS and the average (RMS) −19.8 dBFS over the minute. With volume 1, lines 30 and drift 3 it reaches a peak of −9.5 dBFS and an RMS of −19.0 dBFS.
New in Ray Kiln
Show a value as contours. Show a value puts a variable of shade on the picture as gray. Now it can draw it as a map. Press Contours in the Show a value bar and the picture stays the shader's own, with the value drawn over it as lines at even steps, at 0 and every step either side, every fifth heavier, about the same width in pixels however steep the value is. Lines every sets the step: its slider offers round steps from a fiftieth of the range to half of it, and the number beside it takes any step. Until you choose one it follows the range, about ten lines across it. A vector's x, y and z get lines of their own, red, green and blue. It works for a line inside an if or a loop, in a fragment shader's shade, and only live in the playground: it isn't saved in a Share link, a Save or an Export.
This episode draws the same thing in Metal, so the two can be laid on each other. In the playground, put the cursor on height in the line float height = topoHeight(q); in shade, press Show a value (or Alt-click height), then press Contours. The range is 0 to 1, so the step is 0.1. Set the lines slider to 10, and the playground's lines lie on the shader's own: both are drawn where the height is a whole number of steps from 0, and both make every fifth heavier, so the heavy ones meet at 0 and 0.5. At the default 12 lines, type 0.08333 in the number to do the same. They part in three places: on the steepest ground, where each thins its crowded lines in its own way; under the sea, where the shader's lines are faint; and at the shore, a line of the shader's own at the height sea, which at the default 0.32 falls on no step. The sea covers the low ground but doesn't move a line. Then press Stop, put the cursor on climb in the line float climb = length(slope) * px;, and press Show a value, Auto, then Contours, to see another field drawn the same way: how steep the ground is, as a map of its own.
Try this
- Set
seato 0: the water and the cream shore are gone, and the low ground is the darkest ember with its contour lines at full strength. Set it to 0.7 and only the highest ground stands out of the sea, as islands ringed in cream. - Set
reliefto 0: the hills lie flat, every color the plain tint of its height, with only the lines to show the shape. At 3 the slopes facing the upper left shine and the slopes facing away go much darker. - Set
linesto 10 andseato 0.5: the shore runs exactly along an index line, the heavy line at height 0.5, and the two are drawn as one. - Set
driftto 1.5: the map goes round its circle one and a half times a minute, and at 60 s it is on the far side of the circle, showing other land, so the minute no longer loops; it comes back to its start at 40 s instead. Set it to 0 and the map stands still: the note holds on one step and nothing ticks. - Set
scaleto 6: the picture shows land nearly three times as far across, and on the steepest slopes, crowded closer together on the screen, the thin lines fade out and the index lines carry the shape.
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 |
|---|---|---|---|
lines | 12 | 4 to 30 | How many contour steps the height is cut into |
width | 1.4 | 0.5 to 4 | How wide a contour line is, in pixels of a 720-line picture |
relief | 1.2 | 0 to 3 | How strongly the slopes are lit and shaded |
sea | 0.32 | 0 to 0.7 | The sea's level, as a share of the height |
scale | 2.2 | 1 to 6 | How much land the map shows: higher shows more |
drift | 1 | 0 to 3 | How fast the map slides round its circle |
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.
topographic-map.metal
// Ray Kiln · Topographic Map
//
// @episode Topographic Map
// @length 60
// @variation 30
// @variations 1,2,4,6,9,10,11,12
// @sweeps lines, sea, relief
// @still 30
// @teaches Contour lines of a height field, shading a map by its slope
// @category Fragment
// @tags contour lines, height field, fractal noise, finite differences, hill shading, procedural sound
// @final Topographic Map | A sea fills the low ground, the shore is drawn in cream, and the map is printed on grained paper.
//
// A map of land that isn't there, seen from above. Its height is fractal
// noise. A contour line joins the points of one height, at even steps,
// and every fifth is drawn heavier. Each line is the same width on the
// screen wherever it runs: the distance to the nearest level, in height,
// is divided by how fast the height changes there, which comes from
// sampling the height one pixel to each side. The slope, sampled over a
// wider step, also lights the hills from the upper left. The map slides
// round a slow circle once a minute and comes back where it began.
// 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 lines 12 4 30 How many contour steps the height is cut into
// @param width 1.4 0.5 4 How wide a contour line is, in pixels of a 720-line picture
// @param relief 1.2 0 3 How strongly the slopes are lit and shaded
// @param sea 0.32 0 0.7 The sea's level, as a share of the height
// @param scale 2.2 1 6 How much land the map shows: higher shows more
// @param drift 1 0 3 How fast the map slides round its circle
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The map comes back where it started after this many seconds.
constant float LOOP = 60.0;
// How far the middle of the map travels from the circle's center.
constant float ROAM = 1.4;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Value noise: random values on a grid, blended smoothly between them.
float vnoise(float2 p) {
float2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i), b = hash21(i + float2(1, 0));
float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
// Fractal noise: octaves of value noise, each finer and fainter than the
// last.
float fbm(float2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * vnoise(p);
p = p * 2.03 + float2(17.1, 9.2);
a *= 0.5;
}
return v;
}
// The Ray Kiln palette: near-black, ember, orange, gold, white-hot.
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 land's height at a point of the map, about 0 low down to 1 high.
float topoHeight(float2 q) {
// Broad hills and valleys, with the fractal noise's detail on them.
float broad = vnoise(q * 0.45 + float2(3.1, 7.7));
return (0.45 * broad + 0.9 * fbm(q) - 0.35) / 0.65;
}
// Where the middle of the map is at time t: round a circle, once a loop.
float2 topoCenter(float t, constant Params& p) {
float a = TAU * t * p.drift / LOOP;
return float2(4.7, 2.3) + ROAM * float2(cos(a), sin(a));
}
// The height's slope, sampled `e` to each side.
float2 topoSlope(float2 q, float e) {
float dx = topoHeight(q + float2(e, 0.0)) - topoHeight(q - float2(e, 0.0));
float dy = topoHeight(q + float2(0.0, e)) - topoHeight(q - float2(0.0, e));
return float2(dx, dy) / (2.0 * e);
}
// How much a line `width` pixels wide covers a pixel `away` from it in height.
float topoLine(float away, float climb, float width) {
float dist = away / max(climb, 1e-6);
return 1.0 - smoothstep(width * 0.5 - 0.5, width * 0.5 + 0.5, dist);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y;
// One pixel, measured in map units.
float px = p.scale / u.resolution.y;
float2 q = s * p.scale + topoCenter(u.time, p);
float height = topoHeight(q);
// The slope, one pixel to each side.
float2 slope = topoSlope(q, px);
// The height's change from one pixel to the next.
float climb = length(slope) * px;
// The light takes the slope over a wider step, which smooths out
// the smallest bumps.
float2 hill = topoSlope(q, 0.03);
// The land's tint now runs from the sea level up.
float land = (height - p.sea) / max(1.0 - p.sea, 0.05);
float3 col = kiln(0.14 + 0.5 * land) * 0.85;
// Hill shading: a surface tilted by the slope, lit from the upper left.
float3 n = normalize(float3(-hill * p.relief * 0.25, 1.0));
float3 lamp = normalize(float3(-1.0, 1.0, 1.3));
float lit = max(dot(n, lamp), 0.0) / lamp.z;
col *= mix(1.0, lit * lit, min(p.relief, 1.0));
// The contour lines: `k` is the nearest step.
float gap = 1.0 / p.lines;
float level = height / gap;
float k = floor(level + 0.5);
float away = abs(level - k) * gap;
// Every fifth line, an index contour, is drawn twice as wide.
bool index = k - 5.0 * floor(k / 5.0) < 0.5;
float w = p.width * u.resolution.y / 720.0 * (index ? 2.0 : 1.0);
float ink = topoLine(away, climb, w);
// Where the lines crowd closer than a few widths, they fade out,
// except the index lines, which are five times as far apart.
float spacing = (index ? 5.0 : 1.0) * gap / max(climb, 1e-6);
ink *= smoothstep(w * 1.5, w * 3.5, spacing);
float3 inkColor = kiln(index ? 0.86 : 0.62 + 0.25 * land);
float3 lined = mix(col, inkColor, ink * (index ? 0.95 : 0.8));
// The sea: dark water over the low ground, its depth lines faint.
float depth = clamp(height / max(p.sea, 0.01), 0.0, 1.0);
float3 water = float3(0.02, 0.016, 0.026)
+ float3(0.07, 0.03, 0.03) * depth;
water = mix(water, kiln(0.3), ink * 0.3);
// How far below the shore, in pixels.
float below = clamp((p.sea - height) / max(climb, 1e-6) + 0.5, 0.0, 1.0);
// A sea level of 0 means no sea.
below *= step(0.001, p.sea);
col = mix(lined, water, below);
// The shore, in cream.
float shore = topoLine(abs(height - p.sea), climb, w * 1.3);
col = mix(col, kiln(0.95), shore * step(0.001, p.sea));
// Paper grain: a faint, fixed speckle.
float grain = hash21(floor(uv * u.resolution));
col *= 0.94 + 0.08 * grain;
col = pow(col, float3(0.9));
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// The same map, heard. A walker stands at the middle of the screen and
// the map slides under them. Their note is the contour step they are on,
// a pentatonic scale climbing with the land, and each time they cross a
// line it ticks; an index line ticks lower and louder. The steeper the
// ground, the brighter the note. Under the sea, the note gives way to surf.
// The walker's contour step at time t. Their path is read on a fixed grid
// of times, LOOK apart, so a line is crossed at a grid time, the same one
// however the sound is cut up.
constant float LOOK = 0.0125;
constant int LOOKS = 24;
constant float WINDOW = LOOK * float(LOOKS);
float topoStepAt(float n, constant Params& p) {
return floor(topoHeight(topoCenter(n * LOOK, p)) * p.lines);
}
// How long ago (dir -1) or how soon (dir 1) the walker crosses a contour,
// up to WINDOW seconds; `crossed` is the higher of the two steps.
float topoCrossing(float t, float dir, constant Params& p,
thread float& crossed) {
float n0 = floor(t / LOOK);
crossed = -1.0;
for (int i = 0; i < LOOKS; i++) {
// Two neighboring grid times, going back from now or on from it;
// the step changes at the later one, b.
float a = dir < 0.0 ? n0 - float(i) - 1.0 : n0 + float(i);
float b = a + 1.0;
float ka = topoStepAt(a, p), kb = topoStepAt(b, p);
if (ka != kb) {
crossed = max(ka, kb);
return dir < 0.0 ? t - b * LOOK : b * LOOK - t;
}
}
return WINDOW;
}
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float topoSurf(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float2 c = topoCenter(t, p);
float h = topoHeight(c);
float2 g = topoSlope(c, 0.01);
// The step the walker is on, read at the grid time just passed.
float k = topoStepAt(floor(t / LOOK), p);
// Time since the last line and until the next.
float lastLine = 0.0, nextLine = 0.0;
float since = topoCrossing(t, -1.0, p, lastLine);
float until = topoCrossing(t, 1.0, p, nextLine);
// The note of this step: a pentatonic scale over three octaves.
float step5 = k - 5.0 * floor(k / 5.0);
float semis = step5 == 0.0 ? 0.0 : (step5 == 1.0 ? 2.0
: (step5 == 2.0 ? 4.0 : (step5 == 3.0 ? 7.0 : 9.0)));
float octave = floor(k / 5.0);
octave -= 3.0 * floor(octave / 3.0);
float f = 110.0 * exp2((semis + 12.0 * octave) / 12.0);
// It sounds between lines and falls silent across each, so a new
// pitch never starts mid-wave.
float env = smoothstep(0.0, 0.04, since) * smoothstep(0.0, 0.04, until);
float bright = clamp(length(g) * 0.4, 0.0, 1.0);
float tone = sin(TAU * f * t) + 0.35 * bright * sin(TAU * 2.0 * f * t)
+ 0.2 * bright * sin(TAU * 3.0 * f * t);
tone *= env * 0.22;
// The tick of a line crossed: an index line lower and louder.
bool index = lastLine - 5.0 * floor(lastLine / 5.0) < 0.5;
float tickF = index ? 1400.0 : 2600.0;
float tick = sin(TAU * tickF * since) * exp(-since * 70.0);
tick *= smoothstep(0.0, 0.002, since);
// It dies away before the next line, so a new tick never cuts it off.
tick *= smoothstep(0.0, 0.01, until);
tick *= since < WINDOW ? (index ? 0.3 : 0.16) : 0.0;
// Under the sea the note sinks into surf.
float wet = 1.0 - smoothstep(p.sea - 0.03, p.sea + 0.01, h);
wet *= step(0.001, p.sea);
float swell = 0.6 + 0.4 * sin(TAU * t / 7.0);
float surf = 0.6 * topoSurf(t, 500.0) + 0.4 * topoSurf(t + 3.7, 1300.0);
tone *= 1.0 - 0.85 * wet;
// A low drone on A, swelling as the land rises.
float drone = sin(TAU * 55.0 * t) + 0.5 * sin(TAU * 82.5 * t);
drone *= 0.08 * (0.5 + 0.5 * h);
// The note leans toward the side the ground falls away to.
float lean = 0.5 + 0.35 * clamp(-g.x * 0.5, -1.0, 1.0);
float2 all = float2(tone * (1.0 - lean), tone * lean) * 2.0
+ float2(tick * lean, tick * (1.0 - lean)) * 2.0
+ drone + float2(surf, topoSurf(t + 9.1, 600.0))
* 0.12 * wet * swell;
// Silence where the minute comes round, so it joins without a click.
float turn = fract(t / LOOP);
float fade = 1.0 - exp(-min(turn, 1.0 - turn) * LOOP * 30.0);
return tanh(all * 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] = [12, 1.4, 1.2, 0.32, 2.2, 1, 0.8] // the @param defaults, in the order they are declared
encoder.setRenderPipelineState(pipeline)
encoder.setFragmentBytes(&uniforms, length: MemoryLayout<Uniforms>.stride, index: 0)
encoder.setFragmentBytes(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
More in Noise & Texture
Press ? for keyboard shortcuts.


