Shader

Dunes

A sea of sand at sunset, seen from a little above it. Every corner of a 512 by 512 grid is lifted to the height of three sets of ridges, each with a long gentle side the wind climbs and a short steep slip face, their crests bent so they snake. Each pixel walks from the sand toward the low sun to find the dunes that shade it, the wind combs fine ripples across the gentle slopes, and in the gusts veils of sand stream off the brinks. The wind moves the whole field one repeat a minute, so it loops, and the roar, the hiss of the grains and a low hum rise and fall with the same gusts.

· Geometry · Vertex shader

Teaches Sand dunes on a grid, ripples from the wind, low sun and long shadows

Builds on Flowing Color Field (hash21, value noise), Morphing Shapes (smooth minimum), Cracked Clay (integer hash, sound noise), Endless Terrain, Rippling Water (the camera's axes, perspective projection), Ocean Waves, Plucked Strings (time in the loop)

Watch

The idea

A vertex episode starts from a grid the host lays down, as Ocean Waves and Rippling Water do, and corner moves every corner of it. Here the grid becomes a fan of sand in front of the eye, and each corner is lifted to the height of the dunes there. The eye is 4.6 units up, a little above the tallest dune, looking slightly down toward a low sun ahead and to the left.

A real dune is not a smooth wave. The wind pushes sand up its long windward side, and at the top, the brink, the sand tumbles down a short steep slip face on the far side. So the height here is a profile with two sides that meet at a corner, repeated across the wind, three sets of them at different angles and sizes. The sun is low, so the shadows are long and much of the sand lies in them: each pixel walks toward the sun over the same height function, and if a dune stands in the way, it is in the dark. The wind moves the whole field along one of its repeats a minute, so the piece loops at 60 seconds. It is built in five steps, then finished.

Stage 1 · The sheet

The host lays down a grid of 512 by 512 squares, and corner turns its 0 to 1 into flat sand. Ahead, z = 172 grid.y² − 2, so the sheet runs from right under the eye out to 170 units, with small squares near the eye and long ones far off, where each covers less of the screen. Across, it is wide to either side, which grows with the distance so the sheet is as wide as the picture everywhere. project is the camera, as in Ocean Waves: x and y on screen times the depth, the depth for the depth test, and the depth itself, which the host divides by.

shade draws a line every 2 units each way so the perspective shows, fading them out between 15 and 50 units before they get finer than a pixel. Far off the sand sinks into the haze, 1 − exp(−0.012 dist), and from 110 to 165 units it becomes all haze, so the sheet's far edge at 170 never shows. The haze is horizonHaze, the sky just over the horizon in that direction. background draws the sky behind everything: desertSky is gold and hot at the horizon and darker overhead, with a wide glow around the sun and its white-hot disk. sun sets how high the sun stands, in degrees, and heat slides everything from deep ember to white-hot.

stage-1-the-sheet.metal
// Stage 1 · The sheet
// The host lays down a grid of 512 by 512 squares. corner spreads it into
// a fan of flat sand from the eye's feet to 170 units out, as wide as the
// picture at every distance. Behind it, a sunset sky, and haze where the
// sand meets it.
//
// @grid 512

// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot

constant float TAU = 6.2831853;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen
// and where it is in the world.
struct Vertex {
    float4 position [[position]];
    float3 world;
};

// Flowing Color Field's 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));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Once for every corner: the grid becomes flat sand out to 170 units, as
// wide as the picture at each distance, small squares near, long far off.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    Vertex o;
    o.world = float3(x, 0.0, z);
    o.position = project(o.world, aspect);
    return o;
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Once for every pixel of sand: a line every 2 units each way, so the
// perspective shows, fading out before the lines get finer than a pixel.
// Far off the sand sinks into the haze, the sky's color at the horizon.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 d = (0.5 - abs(fract(v.world.xz * 0.5) - 0.5)) * 2.0;
    float line = 1.0 - smoothstep(0.02, 0.03 + 0.004 * dist, min(d.x, d.y));
    line *= 1.0 - smoothstep(15.0, 50.0, dist);
    float level = 0.42 + 0.2 * line + (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);
    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}

Stage 2 · One dune

duneProfile is one dune across, its phase running 0 to 1 from trough to trough. For the first 70% (BRINK) the sand climbs the windward side, u (1.5 − 0.5 u), steep at the start and easing as it nears the top; for the last 30% it falls down the slip face, (1 − v)². The two sides meet at the brink, and smin, the smooth minimum, rounds that corner by k. duneHeight lays the profile across the wind, one repeat every 16 units (PERIOD), and slides it downwind by 16 units a minute, so after a minute the field is as it began. wind turns the wind's heading, and the ridges with it; height scales them.

Far off the grid's squares are long, and a sharp brink falls between corners and flickers, so k grows with the distance from the eye, 0.02 plus 0.003 a unit: far brinks are rounder and the squares can follow them. corner also measures the sand's slope, from the height a little to either side of the corner in x and in z, and hands it on in the Vertex. The slope blends between corners as the position does, and shade builds the normal from it, normalize(−slope.x, 1, −slope.y). The light from the sun on whatever faces it is the brightness; the slip faces, turned toward the eye and away from the sun, sink into dark.

stage-2-one-dune.metal46 new lines since stage 1
// Stage 2 · One dune
// Each corner is lifted to a dune's height: a long gentle side the wind
// climbs, then a short steep slip face, repeating every 16 units along
// the wind. The wind moves them one repeat a minute, so the piece loops.
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// The sand: straight dunes across the wind. Far off the brinks are
// rounder, for the long squares.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = duneProfile(dot(at, w) / PERIOD, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Once for every pixel of sand: the normal from the slope, and light from
// the low sun on whatever faces it. The slip faces turn away, into dark.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));
    float3 s = towardSun(p);
    float sun = max(dot(n, s), 0.0);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);
    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}
5 lines from stage 1 that are gone
    o.world = float3(x, 0.0, z);
⋯
    float2 d = (0.5 - abs(fract(v.world.xz * 0.5) - 0.5)) * 2.0;
    float line = 1.0 - smoothstep(0.02, 0.03 + 0.004 * dist, min(d.x, d.y));
    line *= 1.0 - smoothstep(15.0, 50.0, dist);
    float level = 0.42 + 0.2 * line + (p.heat - 0.5) * 0.25;

Stage 3 · A dune field

Straight ridges look plowed. crestBend bends each big crest downwind by two slow waves along it, 2.4 and 1.1 units deep, so the crests snake, and tall makes their height rise and fall along them, between 44% and 100% (0.72 + 0.28 sin(…)). Two smaller sets of ridges cross the big ones: one turned 0.7 radians from the wind, 0.3 as tall as a big dune at its tallest, its repeat 16 cos(0.7) / 2, about 6.1 units; the other turned 0.95 radians the other way, 0.35 as tall, its repeat 16 cos(0.95), about 9.3. Those spacings are chosen so that when the field slides 16 units along the wind, each smaller set moves exactly two repeats or one along its own heading: the whole field still loops at a minute. Each smaller set also bends with the big crests, 0.6 and −0.5 times as much.

stage-3-a-dune-field.metal16 new lines since stage 2
// Stage 3 · A dune field
// The crests snake: two slow waves bend them downwind, and their height
// rises and falls along them. Two smaller sets of ridges cross at angles,
// spaced so they move a whole number of repeats when the big ones move one.
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// How far a big dune's crest is bent downwind at b across the wind: two
// slow waves, so the crests snake instead of running straight.
float crestBend(float b) {
    return 2.4 * sin(0.13 * b + 1.0) + 1.1 * sin(0.29 * b + 4.0);
}

// The sand's height. Three sets of ridges: the big dunes across the wind,
// their crests bent by two slow waves, and two smaller sets at an angle,
// spaced so each moves a whole number of repeats when the field moves one.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float2 c = float2(-w.y, w.x);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float b = dot(xz, c);
    float bend = crestBend(b);
    float tall = 0.72 + 0.28 * sin(0.07 * b + 2.0);
    // Far off, where the squares are long, the brinks are rounder, so the
    // squares can follow them.
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = tall * duneProfile((dot(at, w) + bend) / PERIOD, k);
    float2 d2 = float2(w.x * cos(0.7) - w.y * sin(0.7),
                       w.x * sin(0.7) + w.y * cos(0.7));
    float l2 = PERIOD * cos(0.7) / 2.0;
    h += 0.3 * duneProfile((dot(at, d2) + 0.6 * bend) / l2, k);
    float2 d3 = float2(w.x * cos(0.95) + w.y * sin(0.95),
                       -w.x * sin(0.95) + w.y * cos(0.95));
    float l3 = PERIOD * cos(0.95);
    h += 0.35 * duneProfile((dot(at, d3) - 0.5 * bend) / l3, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Once for every pixel of sand: the normal from the slope, and light from
// the low sun on whatever faces it. The slip faces turn away, into dark.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));
    float3 s = towardSun(p);
    float sun = max(dot(n, s), 0.0);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);
    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}
1 line from stage 2 that are gone
    float h = duneProfile(dot(at, w) / PERIOD, k);

Stage 4 · Long shadows

sandShadow walks from the sand toward the sun. At each step it compares the walk's height with duneHeight under it: the gap. Where the gap goes below zero, a dune stands between this point and the sun, and the point is in shadow. Passing just over a crest keeps a little light, 12 gap / d, the gap over the distance walked, so the shadow's edge is soft, as in Endless Terrain. Each step is half the gap, at least 0.05 and at most 0.4 + 0.02 d, so the walk goes quickly over open sand and carefully near it, and it stops when it is higher than any dune can be (TOP, 2.7 times height) or after 128 steps.

The walk starts 0.02 over the sand, or over the grid's surface where a flat square cuts under a rounded brink, so the brink shades it as the eye sees it. The lower the sun, the longer the shadows run: at 8° most of the slip faces and the troughs in front of them are dark.

stage-4-long-shadows.metal20 new lines since stage 3
// Stage 4 · Long shadows
// For each pixel, a walk toward the sun over the same height function.
// Where the walk passes under the sand, a dune stands between this point
// and the sun: shadow. The lower the sun, the longer the shadows run.
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The tallest the sand can be at height 1, under the eye.
constant float TOP = 2.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// How far a big dune's crest is bent downwind at b across the wind: two
// slow waves, so the crests snake instead of running straight.
float crestBend(float b) {
    return 2.4 * sin(0.13 * b + 1.0) + 1.1 * sin(0.29 * b + 4.0);
}

// The sand's height. Three sets of ridges: the big dunes across the wind,
// their crests bent by two slow waves, and two smaller sets at an angle,
// spaced so each moves a whole number of repeats when the field moves one.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float2 c = float2(-w.y, w.x);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float b = dot(xz, c);
    float bend = crestBend(b);
    float tall = 0.72 + 0.28 * sin(0.07 * b + 2.0);
    // Far off, where the squares are long, the brinks are rounder, so the
    // squares can follow them.
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = tall * duneProfile((dot(at, w) + bend) / PERIOD, k);
    float2 d2 = float2(w.x * cos(0.7) - w.y * sin(0.7),
                       w.x * sin(0.7) + w.y * cos(0.7));
    float l2 = PERIOD * cos(0.7) / 2.0;
    h += 0.3 * duneProfile((dot(at, d2) + 0.6 * bend) / l2, k);
    float2 d3 = float2(w.x * cos(0.95) + w.y * sin(0.95),
                       -w.x * sin(0.95) + w.y * cos(0.95));
    float l3 = PERIOD * cos(0.95);
    h += 0.35 * duneProfile((dot(at, d3) - 0.5 * bend) / l3, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// How much sun reaches a point: walk toward the sun, and if the walk
// passes under the sand, the point is in shadow. Just clearing a crest
// keeps a little light: a soft edge. The walk ends above the tallest dune.
float sandShadow(float3 at, float3 s, float t, constant Params& p) {
    float lit = 1.0;
    float d = 0.05;
    float top = TOP * p.height;
    for (int i = 0; i < 128; i++) {
        float3 q = at + s * d;
        if (q.y > top) break;
        float gap = q.y - duneHeight(q.xz, t, p);
        lit = min(lit, 12.0 * gap / d);
        if (lit < 0.0) break;
        d += clamp(gap * 0.5, 0.05, 0.4 + 0.02 * d);
    }
    return smoothstep(0.0, 1.0, lit);
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Once for every pixel of sand: the normal from the slope, the light of
// the low sun on whatever faces it, unless a dune between here and the
// sun is in the way. Then the haze.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float t = u.time;
    float2 xz = v.world.xz;
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));

    float3 s = towardSun(p);
    // The walk starts just over the sand, or over the square where a square
    // cuts under a rounded brink, so the brink shades it as the eye sees it.
    float lift = min(duneHeight(xz, t, p), v.world.y) + 0.02;
    float3 at = float3(xz.x, lift, xz.y);
    float sun = max(dot(n, s), 0.0) * sandShadow(at, s, t, p);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);

    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}
1 line from stage 3 that are gone
    float sun = max(dot(n, s), 0.0);

Stage 5 · Ripples

The wind combs the sand into ripples 0.32 units apart (RIPPLE), across the wind like the dunes and with the same steep downwind side (duneProfile again), bent a little along their length and creeping downwind, 12 of them past a point each minute. At 0.01 units tall they are far too fine for the grid, so shade measures their slope itself, from rippleHeight a little to either side of the pixel, and adds it to the dunes' slope from the corners.

Ripples finer than a pixel would sparkle, so foot, how wide a pixel is on the sand (wider far off, and wider again where the sand is seen edge-on), sets both how far apart the two heights are taken and where they fade: as a pixel grows from 0.08 of a ripple's spacing to 0.18, they fade out, so only the near sand has them. They are also gone from the steep slip faces, where the slope is over 0.35 to 0.6: sand that steep slides instead of holding ripples. ripples sets how deep they are.

stage-5-ripples.metal19 new lines since stage 4
// Stage 5 · Ripples
// The wind combs the sand into ripples a third of a unit apart, across the
// wind like the dunes and creeping with it. They are too fine for the grid,
// so shade adds their slope to the normal, fading them out far away.
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param ripples 1.0 0.0 2.0 How deep the ripples the wind combs into the sand are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The ripples' spacing, and how many of them pass a point each minute.
constant float RIPPLE = 0.32;
constant float RIPPLE_RUN = 12.0;
// The tallest the sand can be at height 1, under the eye.
constant float TOP = 2.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// How far a big dune's crest is bent downwind at b across the wind: two
// slow waves, so the crests snake instead of running straight.
float crestBend(float b) {
    return 2.4 * sin(0.13 * b + 1.0) + 1.1 * sin(0.29 * b + 4.0);
}

// The sand's height. Three sets of ridges: the big dunes across the wind,
// their crests bent by two slow waves, and two smaller sets at an angle,
// spaced so each moves a whole number of repeats when the field moves one.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float2 c = float2(-w.y, w.x);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float b = dot(xz, c);
    float bend = crestBend(b);
    float tall = 0.72 + 0.28 * sin(0.07 * b + 2.0);
    // Far off, where the squares are long, the brinks are rounder, so the
    // squares can follow them.
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = tall * duneProfile((dot(at, w) + bend) / PERIOD, k);
    float2 d2 = float2(w.x * cos(0.7) - w.y * sin(0.7),
                       w.x * sin(0.7) + w.y * cos(0.7));
    float l2 = PERIOD * cos(0.7) / 2.0;
    h += 0.3 * duneProfile((dot(at, d2) + 0.6 * bend) / l2, k);
    float2 d3 = float2(w.x * cos(0.95) + w.y * sin(0.95),
                       -w.x * sin(0.95) + w.y * cos(0.95));
    float l3 = PERIOD * cos(0.95);
    h += 0.35 * duneProfile((dot(at, d3) - 0.5 * bend) / l3, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// Ripples: low ridges a third of a unit apart, across the wind like the
// dunes, with the same steep downwind side, creeping with the wind.
float rippleHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float a = dot(xz, w), b = dot(xz, float2(-w.y, w.x));
    float bend = 0.1 * sin(2.1 * b) + 0.25 * sin(0.6 * b + 0.3 * a);
    float creep = RIPPLE * RIPPLE_RUN * loopTime(t) / LOOP;
    return 0.01 * p.ripples * duneProfile((a + bend - creep) / RIPPLE, 0.1);
}

// How much sun reaches a point: walk toward the sun, and if the walk
// passes under the sand, the point is in shadow. Just clearing a crest
// keeps a little light: a soft edge. The walk ends above the tallest dune.
float sandShadow(float3 at, float3 s, float t, constant Params& p) {
    float lit = 1.0;
    float d = 0.05;
    float top = TOP * p.height;
    for (int i = 0; i < 128; i++) {
        float3 q = at + s * d;
        if (q.y > top) break;
        float gap = q.y - duneHeight(q.xz, t, p);
        lit = min(lit, 12.0 * gap / d);
        if (lit < 0.0) break;
        d += clamp(gap * 0.5, 0.05, 0.4 + 0.02 * d);
    }
    return smoothstep(0.0, 1.0, lit);
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Once for every pixel of sand. The normal comes from the slope of the
// dunes, from the corners, and the ripples' slopes, measured here, are
// added where the sand lies gently enough to hold them. Then the sun, its
// shadows, and the haze.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float t = u.time;
    float2 xz = v.world.xz;
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    // How wide a pixel is on the sand: wider far off, and wider again where
    // the sand is seen edge-on. Ripples fade out as it grows from a twelfth
    // of one to a sixth, and there are none on the steep slip faces.
    float3 flat = normalize(float3(-slope.x, 1.0, -slope.y));
    float facing = max(abs(dot(look, flat)), 0.05);
    float foot = dist * 2.0 / (1.5 * u.resolution.y) / facing;
    float hold = 1.0 - smoothstep(0.35, 0.6, length(slope));
    hold *= 1.0 - smoothstep(0.08, 0.18, foot / RIPPLE);
    float r = max(0.01, 0.5 * foot);
    float2 rx = float2(r, 0.0), rz = float2(0.0, r);
    float rgx = rippleHeight(xz + rx, t, p) - rippleHeight(xz - rx, t, p);
    float rgz = rippleHeight(xz + rz, t, p) - rippleHeight(xz - rz, t, p);
    slope += hold * float2(rgx, rgz) / (2.0 * r);
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));

    float3 s = towardSun(p);
    // The walk starts just over the sand, or over the square where a square
    // cuts under a rounded brink, so the brink shades it as the eye sees it.
    float lift = min(duneHeight(xz, t, p), v.world.y) + 0.02;
    float3 at = float3(xz.x, lift, xz.y);
    float sun = max(dot(n, s), 0.0) * sandShadow(at, s, t, p);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);


    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}

Stage 6 · Dunes

Blown sand. In a gust, veils of grains stream off the big dunes' brinks and down the slip faces. For each pixel, shade finds how far past the big dune's brink it is, in the same phase duneHeight used, and the veil rises just before the brink and dies away over the 1.2 units beyond it. Two layers of value noise, stretched along the wind and running downwind, tear it into streaks. Its strength is windGust, two slow swells that go round 3 and 7 times a minute, scaled by gusts, and it is brighter looking toward the sun, as sand lit from behind glows. It fades out between 20 and 45 units from the eye.

the kiln palette and shade(), from the finished shader
// Flowing Color Field's 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));
}

// Once for every pixel of sand. The normal comes from the slope of the
// dunes, from the corners, and the ripples' slopes, measured here, are
// added where the sand lies gently enough to hold them. Then the sun, its
// shadows, and the haze.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float t = u.time;
    float2 xz = v.world.xz;
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    // How wide a pixel is on the sand: wider far off, and wider again where
    // the sand is seen edge-on. Ripples fade out as it grows from a twelfth
    // of one to a sixth, and there are none on the steep slip faces.
    float3 flat = normalize(float3(-slope.x, 1.0, -slope.y));
    float facing = max(abs(dot(look, flat)), 0.05);
    float foot = dist * 2.0 / (1.5 * u.resolution.y) / facing;
    float hold = 1.0 - smoothstep(0.35, 0.6, length(slope));
    hold *= 1.0 - smoothstep(0.08, 0.18, foot / RIPPLE);
    float r = max(0.01, 0.5 * foot);
    float2 rx = float2(r, 0.0), rz = float2(0.0, r);
    float rgx = rippleHeight(xz + rx, t, p) - rippleHeight(xz - rx, t, p);
    float rgz = rippleHeight(xz + rz, t, p) - rippleHeight(xz - rz, t, p);
    slope += hold * float2(rgx, rgz) / (2.0 * r);
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));

    float3 s = towardSun(p);
    // The walk starts just over the sand, or over the square where a square
    // cuts under a rounded brink, so the brink shades it as the eye sees it.
    float lift = min(duneHeight(xz, t, p), v.world.y) + 0.02;
    float3 at = float3(xz.x, lift, xz.y);
    float sun = max(dot(n, s), 0.0) * sandShadow(at, s, t, p);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);

    // Blown sand: in a gust, veils of grains stream off the big dunes'
    // brinks and over the slip faces below, glowing with the sun behind.
    float2 w = windDir(p);
    float lt = loopTime(t);
    float drift = PERIOD * lt / LOOP;
    float b = dot(xz, float2(-w.y, w.x));
    float f = fract((dot(xz, w) - drift + crestBend(b)) / PERIOD);
    float past = (f - BRINK) * PERIOD;
    float veil = smoothstep(-0.4, 0.1, past) * exp(-max(past, 0.0) / 1.2);
    float2 q = float2(dot(xz, w) * 0.35 - lt * 3.0, b * 0.9);
    float streak = vnoise(q) * 0.6 + vnoise(q * 2.7) * 0.4;
    veil *= smoothstep(0.3, 0.8, streak) * windGust(t, p);
    veil *= 1.0 - smoothstep(20.0, 45.0, dist);
    float behind = pow(max(dot(look, s), 0.0), 2.0);
    col += kiln(0.85) * veil * (0.2 + 0.6 * behind);

    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}
dunes.metal, the whole file71 new lines since stage 5
// Ray Kiln · Dunes
//
// @episode Dunes
// @length 60
// @variation 30
// @variations 1,2,3,5,6,9,11,12
// @still 24.0
// @teaches Sand dunes on a grid, ripples from the wind, low sun and long shadows
// @category Vertex
// @tags vertex shaders, grids, displacement, heightfield, dunes, normals, finite differences, soft shadows, shadows, ripples, haze, procedural sound
// @final Dunes | Gusts lift sand off the brinks in glowing veils, and the wind, the hiss of the grains and a low boom rise and fall with them.
//
// A sea of sand at sunset, seen from a little above it. The host lays down
// a grid of 512 by 512 squares, and `corner` lifts every corner to the
// height of the dunes there: three sets of ridges, each with a long gentle
// side the wind climbs and a short steep face where the sand slips down.
// The wind moves the whole field along by one of its repeats a minute, so
// it loops at 60 s. `shade` lights each pixel from the low sun: it walks
// from the sand toward the sun, and where a dune stands in the way the
// sand is in its shadow. Fine ripples from the wind cross the slopes.
// 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
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param ripples 1.0 0.0 2.0 How deep the ripples the wind combs into the sand are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param gusts 0.7 0.0 1.0 How hard the wind gusts: sand off the brinks, and its sound
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot
// @param volume 0.8 0.0 1.0 Loudness of the sound

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The ripples' spacing, and how many of them pass a point each minute.
constant float RIPPLE = 0.32;
constant float RIPPLE_RUN = 12.0;
// The tallest the sand can be at height 1, under the eye.
constant float TOP = 2.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// How far a big dune's crest is bent downwind at b across the wind: two
// slow waves, so the crests snake instead of running straight.
float crestBend(float b) {
    return 2.4 * sin(0.13 * b + 1.0) + 1.1 * sin(0.29 * b + 4.0);
}

// The sand's height. Three sets of ridges: the big dunes across the wind,
// their crests bent by two slow waves, and two smaller sets at an angle,
// spaced so each moves a whole number of repeats when the field moves one.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float2 c = float2(-w.y, w.x);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float b = dot(xz, c);
    float bend = crestBend(b);
    float tall = 0.72 + 0.28 * sin(0.07 * b + 2.0);
    // Far off, where the squares are long, the brinks are rounder, so the
    // squares can follow them.
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = tall * duneProfile((dot(at, w) + bend) / PERIOD, k);
    float2 d2 = float2(w.x * cos(0.7) - w.y * sin(0.7),
                       w.x * sin(0.7) + w.y * cos(0.7));
    float l2 = PERIOD * cos(0.7) / 2.0;
    h += 0.3 * duneProfile((dot(at, d2) + 0.6 * bend) / l2, k);
    float2 d3 = float2(w.x * cos(0.95) + w.y * sin(0.95),
                       -w.x * sin(0.95) + w.y * cos(0.95));
    float l3 = PERIOD * cos(0.95);
    h += 0.35 * duneProfile((dot(at, d3) - 0.5 * bend) / l3, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// Ripples: low ridges a third of a unit apart, across the wind like the
// dunes, with the same steep downwind side, creeping with the wind.
float rippleHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float a = dot(xz, w), b = dot(xz, float2(-w.y, w.x));
    float bend = 0.1 * sin(2.1 * b) + 0.25 * sin(0.6 * b + 0.3 * a);
    float creep = RIPPLE * RIPPLE_RUN * loopTime(t) / LOOP;
    return 0.01 * p.ripples * duneProfile((a + bend - creep) / RIPPLE, 0.1);
}

// How much sun reaches a point: walk toward the sun, and if the walk
// passes under the sand, the point is in shadow. Just clearing a crest
// keeps a little light: a soft edge. The walk ends above the tallest dune.
float sandShadow(float3 at, float3 s, float t, constant Params& p) {
    float lit = 1.0;
    float d = 0.05;
    float top = TOP * p.height;
    for (int i = 0; i < 128; i++) {
        float3 q = at + s * d;
        if (q.y > top) break;
        float gap = q.y - duneHeight(q.xz, t, p);
        lit = min(lit, 12.0 * gap / d);
        if (lit < 0.0) break;
        d += clamp(gap * 0.5, 0.05, 0.4 + 0.02 * d);
    }
    return smoothstep(0.0, 1.0, lit);
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Flowing Color Field's hash: a 2D point in, a random 0 to 1 out.
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.
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);
}

// How hard the wind blows, 0 to 1: two swells, 3 and 7 times a minute,
// times the gusts slider. The blown sand and the sound both read it.
float windGust(float t, constant Params& p) {
    float x = TAU * loopTime(t) / LOOP;
    float g = 0.55 + 0.25 * sin(3.0 * x + 0.6) + 0.2 * sin(7.0 * x + 2.1);
    return p.gusts * g;
}

// Once for every pixel of sand. The normal comes from the slope of the
// dunes, from the corners, and the ripples' slopes, measured here, are
// added where the sand lies gently enough to hold them. Then the sun, its
// shadows, and the haze.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float t = u.time;
    float2 xz = v.world.xz;
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    // How wide a pixel is on the sand: wider far off, and wider again where
    // the sand is seen edge-on. Ripples fade out as it grows from a twelfth
    // of one to a sixth, and there are none on the steep slip faces.
    float3 flat = normalize(float3(-slope.x, 1.0, -slope.y));
    float facing = max(abs(dot(look, flat)), 0.05);
    float foot = dist * 2.0 / (1.5 * u.resolution.y) / facing;
    float hold = 1.0 - smoothstep(0.35, 0.6, length(slope));
    hold *= 1.0 - smoothstep(0.08, 0.18, foot / RIPPLE);
    float r = max(0.01, 0.5 * foot);
    float2 rx = float2(r, 0.0), rz = float2(0.0, r);
    float rgx = rippleHeight(xz + rx, t, p) - rippleHeight(xz - rx, t, p);
    float rgz = rippleHeight(xz + rz, t, p) - rippleHeight(xz - rz, t, p);
    slope += hold * float2(rgx, rgz) / (2.0 * r);
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));

    float3 s = towardSun(p);
    // The walk starts just over the sand, or over the square where a square
    // cuts under a rounded brink, so the brink shades it as the eye sees it.
    float lift = min(duneHeight(xz, t, p), v.world.y) + 0.02;
    float3 at = float3(xz.x, lift, xz.y);
    float sun = max(dot(n, s), 0.0) * sandShadow(at, s, t, p);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);

    // Blown sand: in a gust, veils of grains stream off the big dunes'
    // brinks and over the slip faces below, glowing with the sun behind.
    float2 w = windDir(p);
    float lt = loopTime(t);
    float drift = PERIOD * lt / LOOP;
    float b = dot(xz, float2(-w.y, w.x));
    float f = fract((dot(xz, w) - drift + crestBend(b)) / PERIOD);
    float past = (f - BRINK) * PERIOD;
    float veil = smoothstep(-0.4, 0.1, past) * exp(-max(past, 0.0) / 1.2);
    float2 q = float2(dot(xz, w) * 0.35 - lt * 3.0, b * 0.9);
    float streak = vnoise(q) * 0.6 + vnoise(q * 2.7) * 0.4;
    veil *= smoothstep(0.3, 0.8, streak) * windGust(t, p);
    veil *= 1.0 - smoothstep(20.0, 45.0, dist);
    float behind = pow(max(dot(look, s), 0.0), 2.0);
    col += kiln(0.85) * veil * (0.2 + 0.6 * behind);

    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}

// ---- Sound -----------------------------------------------------------------------------------------------------
// The sound is part of the shader: a function of time, run on the GPU 48,000 times a second and put in the video.

// Cracked Clay's hash: a whole number in, a random 0 to 1 out.
float hashU(uint n) {
    n = (n << 13u) ^ n;
    n = n * (n * n * 15731u + 789221u) + 1376312589u;
    return float(n & 0x7fffffffu) / float(0x7fffffff);
}

// Noise from random values joined `rate` times a second: lower is darker.
float noise1(float t, float rate, uint salt) {
    float x = t * rate;
    float i = floor(x);
    float f = x - i;
    f = f * f * (3.0 - 2.0 * f);
    uint n = uint(int(i) + 1000000);
    return mix(hashU(n * 31u + salt), hashU((n + 1u) * 31u + salt), f);
}

// Wind over sand: a dark roar that swells with each gust, the hiss of
// grains in the strong ones, toward the side the wind blows to, a singing
// dune's low hum near 90 Hz at the top of the strongest. It fades out as
// the minute ends and in as it starts, so it loops.
float2 sound(float t, constant Params& p) {
    float g = windGust(t, p);
    float roar = noise1(t, 180.0, 3u) - 0.5;
    roar += 0.7 * (noise1(t, 420.0, 7u) - 0.5);
    roar += 0.4 * (noise1(t, 950.0, 13u) - 0.5);
    float rl = noise1(t, 260.0, 17u) - 0.5;
    float2 v = float2(roar, 0.8 * roar + 0.6 * rl) * (0.25 + 1.1 * g);

    float grains = smoothstep(0.25, 0.75, g);
    float2 w = windDir(p);
    for (int i = 0; i < 2; i++) {
        float side = float(i) * 2.0 - 1.0;
        float h = noise1(t, 9000.0, 29u + uint(i)) - 0.5;
        h += 0.6 * (noise1(t, 5200.0, 41u + uint(i)) - 0.5);
        float flicker = 0.6 + 0.8 * noise1(t, 13.0, 53u + uint(i));
        float pan = clamp(0.5 + 0.3 * w.x + side * 0.15, 0.0, 1.0);
        v += float2(1.0 - pan, pan) * h * grains * flicker * 0.8;
    }

    float hz = 110.0 - 18.0 * p.height;
    float wob = 0.6 * (noise1(t, 7.0, 61u) - 0.5);
    float ph = TAU * hz * t + wob;
    float hum = sin(ph) + 0.45 * sin(2.0 * ph) + 0.2 * sin(3.0 * ph);
    hum *= 0.7 + 0.6 * noise1(t, 23.0, 67u);
    float boom = smoothstep(0.62, 0.92, g) * min(p.height, 1.2);
    v += float2(hum * boom * 0.18);

    float lt = loopTime(t);
    float fade = smoothstep(0.0, 2.5, min(lt, LOOP - lt));
    return tanh(v * 1.3) * 0.5 * p.volume * fade;
}

The sound

The sound is the same wind. windGust is read by sound too: a dark roar of three layers of noise, at 180, 420 and 950 random values a second, swells with each gust (the right channel mixes in a fourth at 260, so the two sides differ). In the stronger gusts, a hiss of grains comes in, two layers at 9000 and 5200 values a second that flicker 13 times a second, panned toward the side the wind blows to, and wind moves them. At the top of the strongest gusts a singing dune's low hum joins them: a tone at 110 − 18 height Hz, 92 Hz at the default height, so taller dunes hum lower, with its second and third harmonics and a slow wobble. At the default gusts of 0.7 it is only a murmur; at 1 it booms. tanh rounds off the loudest moments, and it fades over 2.5 seconds at each end of the minute, so it loops.

At the defaults the peak is about −8.4 dBFS and the average (RMS) −19.7 dBFS over the minute; with gusts, height and volume at their highest, −6.1 dBFS and −15.6 dBFS.

New in Ray Kiln

The depth buffer view is new in the playground. Under a vertex or mesh episode's picture, press Depth buffer, or Z, and instead of the picture you see its depth buffer, the depth each pixel's surface was tested at, so you can see what hides what. A pixel's depth is the nearest of its four samples. It is drawn in gray, stretched between the frame's nearest surface, white, and its farthest, black, either linearly or, with the bar's Scale button, on a log scale, where each step of gray is the same ratio of depth; where no surface was drawn, as in Dunes' sky, the pixel is dim blue. The bar under the picture gives the frame's range, the share of pixels with a surface, and the depth under the pointer with how far it lies between the nearest and the farthest on the scale in use. It is on screen only, so exports and stills keep the shader's own picture, and it and Motion, Symmetry, Wireframe and Facing turn each other off. It is free.

The link opens Dunes paused at 24 seconds. Turn the view on and the bar reads white 0.0134 and black 0.8522, with a surface on 60.3% of the pixels; above the horizon it is dim blue. project makes the depth linear in the distance ahead of the eye, 0 at 0.1 units and 1 at 200, and the near dunes that fill most of the screen lie in the first few percent of that, so on the linear scale they are one white: the sand under the eye reads depth 0.0158, 0% of the way from the nearest to the farthest, and the dune behind the near crest 0.0832, 8%. Press Scale: linear in the bar and it becomes Scale: log. Now the sand under the eye is white (4% of the way), the near slope grays as it climbs to its crest (0.0495, 32%), the dune behind that crest is mid-gray (0.0832, 44%), a far dune just under the horizon is dark (0.3805, 81%), and the sky still reads no surface. The dunes stand as separate bands, each darker than the one in front, and where a ridge hides the sand behind it the gray steps down: at the left of the picture a crest at 0.0702, 40% of the way, stands a few pixels from the slope behind it at 0.1234, 53%.

Try this

  • In One dune, set wind to 1: the ridges swing round to run almost straight away from the eye, toward the horizon. Set height to 0.2: they sink into low swells, and the dark slip faces are nearly gone.
  • In Long shadows, set sun to 3: the sun sits just over the horizon, and shadow covers most of the sand. At 30 the sun climbs out of the picture, nearly every shadow is gone, and only the steepest slip faces stay dark.
  • In the finished shader, set height to 0.2: low swells with almost no shadow, and ripples across a wide stretch of the near sand at the lower right. At 1.6 the near crest stands tall against a deep shadow.
  • Set gusts to 0: the pale veils over the dark slip faces in the middle distance are gone, and the sound falls to a quiet roar.
  • Set ripples to 0: the near slopes are smooth. At 2 the ripples are deeper, with darker troughs.
  • Set heat to 0: deep ember sand under a sky that goes nearly black overhead. At 1 the sand and the glow around the sun turn gold.
  • In sandShadow, change 12.0 * gap / d to 3.0 * gap / d: the shadows' edges go soft and wide.

Parameters

ParameterDefaultRangeWhat it does
wind0.0-1.0 to 1.0Turns the wind, and the dunes with it, left or right
height1.00.2 to 1.6How tall the dunes are
ripples1.00.0 to 2.0How deep the ripples the wind combs into the sand are
sun8.03.0 to 30.0The sun's height over the horizon, in degrees
gusts0.70.0 to 1.0How hard the wind gusts: sand off the brinks, and its sound
heat0.50.0 to 1.0Slides the light from deep ember to white-hot
volume0.80.0 to 1.0Loudness 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.

The full source

This is the whole episode: the picture, the parameters and the sound.

dunes.metal
// Ray Kiln · Dunes
//
// @episode Dunes
// @length 60
// @variation 30
// @variations 1,2,3,5,6,9,11,12
// @still 24.0
// @teaches Sand dunes on a grid, ripples from the wind, low sun and long shadows
// @category Vertex
// @tags vertex shaders, grids, displacement, heightfield, dunes, normals, finite differences, soft shadows, shadows, ripples, haze, procedural sound
// @final Dunes | Gusts lift sand off the brinks in glowing veils, and the wind, the hiss of the grains and a low boom rise and fall with them.
//
// A sea of sand at sunset, seen from a little above it. The host lays down
// a grid of 512 by 512 squares, and `corner` lifts every corner to the
// height of the dunes there: three sets of ridges, each with a long gentle
// side the wind climbs and a short steep face where the sand slips down.
// The wind moves the whole field along by one of its repeats a minute, so
// it loops at 60 s. `shade` lights each pixel from the low sun: it walks
// from the sand toward the sun, and where a dune stands in the way the
// sand is in its shadow. Fine ripples from the wind cross the slopes.
// 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
//
// @grid 512

// @param wind 0.0 -1.0 1.0 Turns the wind, and the dunes with it, left or right
// @param height 1.0 0.2 1.6 How tall the dunes are
// @param ripples 1.0 0.0 2.0 How deep the ripples the wind combs into the sand are
// @param sun 8.0 3.0 30.0 The sun's height over the horizon, in degrees
// @param gusts 0.7 0.0 1.0 How hard the wind gusts: sand off the brinks, and its sound
// @param heat 0.5 0.0 1.0 Slides the light from deep ember to white-hot
// @param volume 0.8 0.0 1.0 Loudness of the sound

constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The field repeats every 16 units along the wind, and moves that far a
// minute.
constant float PERIOD = 16.0;
// The share of a dune that is windward side: the rest is slip face.
constant float BRINK = 0.7;
// The ripples' spacing, and how many of them pass a point each minute.
constant float RIPPLE = 0.32;
constant float RIPPLE_RUN = 12.0;
// The tallest the sand can be at height 1, under the eye.
constant float TOP = 2.7;
// The eye, a little above the highest dune, and the point it looks at.
constant float3 EYE = float3(0.0, 4.6, -2.0);
constant float3 AIM = float3(0.0, 1.2, 20.0);

// What each corner carries to the pixels between: where it lands on screen,
// where it is in the world, and the slope of the sand there.
struct Vertex {
    float4 position [[position]];
    float3 world;
    float2 slope;
};

// Flowing Color Field's 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 time within the minute that repeats.
float loopTime(float t) {
    return t - LOOP * floor(t / LOOP);
}

// Which way the wind blows: toward the eye and right, turned by the wind
// slider.
float2 windDir(constant Params& p) {
    float a = 0.65 + p.wind * 0.5;
    return float2(sin(a), -cos(a));
}

// Toward the sun: low, ahead and to the left, from its height in degrees.
float3 towardSun(constant Params& p) {
    float e = p.sun * TAU / 360.0;
    return normalize(float3(-0.64 * cos(e), sin(e), 0.77 * cos(e)));
}

// The camera's three directions: right, up and forward.
float3x3 cameraAxes() {
    float3 f = normalize(AIM - EYE);
    float3 r = normalize(cross(float3(0, 1, 0), f));
    return float3x3(r, cross(f, r), f);
}

// A point in the world as the host wants it: x and y on screen (times
// the depth), the depth from 0 to 1 for the depth test, and the depth itself.
float4 project(float3 world, float aspect) {
    float3x3 axes = cameraAxes();
    float3 v = world - EYE;
    float3 view = float3(dot(v, axes[0]), dot(v, axes[1]), dot(v, axes[2]));
    float zoom = 1.5, near = 0.1, far = 200.0;
    float depth = (view.z - near) / (far - near) * view.z;
    return float4(view.x * zoom / aspect, view.y * zoom, depth, view.z);
}

// Like min, but rounded where a and b are within k.
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;
}

// One dune, s from 0 to 1 trough to trough: up the long windward side,
// down the steep slip face, the brink rounded by k.
float duneProfile(float s, float k) {
    float f = fract(s);
    float u = f / BRINK;
    float up = u < 1.0 ? u * (1.5 - 0.5 * u) : 0.5 + 0.5 * u;
    float v = (f - BRINK) / (1.0 - BRINK);
    float down = (1.0 - v) * (1.0 - v);
    return smin(up, down, k);
}

// How far a big dune's crest is bent downwind at b across the wind: two
// slow waves, so the crests snake instead of running straight.
float crestBend(float b) {
    return 2.4 * sin(0.13 * b + 1.0) + 1.1 * sin(0.29 * b + 4.0);
}

// The sand's height. Three sets of ridges: the big dunes across the wind,
// their crests bent by two slow waves, and two smaller sets at an angle,
// spaced so each moves a whole number of repeats when the field moves one.
float duneHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float2 c = float2(-w.y, w.x);
    float drift = PERIOD * loopTime(t) / LOOP;
    float2 at = xz - w * drift;
    float b = dot(xz, c);
    float bend = crestBend(b);
    float tall = 0.72 + 0.28 * sin(0.07 * b + 2.0);
    // Far off, where the squares are long, the brinks are rounder, so the
    // squares can follow them.
    float k = 0.02 + 0.003 * length(xz - EYE.xz);
    float h = tall * duneProfile((dot(at, w) + bend) / PERIOD, k);
    float2 d2 = float2(w.x * cos(0.7) - w.y * sin(0.7),
                       w.x * sin(0.7) + w.y * cos(0.7));
    float l2 = PERIOD * cos(0.7) / 2.0;
    h += 0.3 * duneProfile((dot(at, d2) + 0.6 * bend) / l2, k);
    float2 d3 = float2(w.x * cos(0.95) + w.y * sin(0.95),
                       -w.x * sin(0.95) + w.y * cos(0.95));
    float l3 = PERIOD * cos(0.95);
    h += 0.35 * duneProfile((dot(at, d3) - 0.5 * bend) / l3, k);
    return 1.6 * p.height * h;
}

// Once for every corner: the grid becomes a fan of sand in front of the
// eye, as wide as the picture at each distance, and each corner is lifted
// to the dunes. Its slope, measured about a square to each side, blends
// between corners, so crests far off shade smoothly.
Vertex corner(float2 grid, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float z = 172.0 * grid.y * grid.y - 2.0;
    float wide = max(z, 0.0) * aspect / 1.5 * 1.2 + 4.0;
    float x = (grid.x - 0.5) * 2.0 * wide;
    float2 xz = float2(x, z);
    float t = u.time;
    float e = max(0.6 * grid.y, 0.02);
    float2 ex = float2(e, 0.0), ez = float2(0.0, e);
    float gx = duneHeight(xz + ex, t, p) - duneHeight(xz - ex, t, p);
    float gz = duneHeight(xz + ez, t, p) - duneHeight(xz - ez, t, p);
    Vertex o;
    o.world = float3(x, duneHeight(xz, t, p), z);
    o.position = project(o.world, aspect);
    o.slope = float2(gx, gz) / (2.0 * e);
    return o;
}

// Ripples: low ridges a third of a unit apart, across the wind like the
// dunes, with the same steep downwind side, creeping with the wind.
float rippleHeight(float2 xz, float t, constant Params& p) {
    float2 w = windDir(p);
    float a = dot(xz, w), b = dot(xz, float2(-w.y, w.x));
    float bend = 0.1 * sin(2.1 * b) + 0.25 * sin(0.6 * b + 0.3 * a);
    float creep = RIPPLE * RIPPLE_RUN * loopTime(t) / LOOP;
    return 0.01 * p.ripples * duneProfile((a + bend - creep) / RIPPLE, 0.1);
}

// How much sun reaches a point: walk toward the sun, and if the walk
// passes under the sand, the point is in shadow. Just clearing a crest
// keeps a little light: a soft edge. The walk ends above the tallest dune.
float sandShadow(float3 at, float3 s, float t, constant Params& p) {
    float lit = 1.0;
    float d = 0.05;
    float top = TOP * p.height;
    for (int i = 0; i < 128; i++) {
        float3 q = at + s * d;
        if (q.y > top) break;
        float gap = q.y - duneHeight(q.xz, t, p);
        lit = min(lit, 12.0 * gap / d);
        if (lit < 0.0) break;
        d += clamp(gap * 0.5, 0.05, 0.4 + 0.02 * d);
    }
    return smoothstep(0.0, 1.0, lit);
}

// The sky in a direction: gold and hot at the horizon, darker overhead,
// a wide glow around the sun and its disk, white-hot.
float3 desertSky(float3 d, constant Params& p) {
    float3 s = towardSun(p);
    float up = pow(max(d.y, 0.0), 0.5);
    float g = max(dot(d, s), 0.0);
    float t = 0.6 - 0.8 * up + 0.12 * pow(g, 4.0) + 0.1 * pow(g, 40.0);
    float3 col = kiln(min(t + (p.heat - 0.5) * 0.3, 0.8));
    col += kiln(0.85) * 0.2 * pow(g, 400.0);
    return col + kiln(0.97) * smoothstep(0.99988, 0.99993, g);
}

// The haze in a direction: the sky just over the horizon that way.
float3 horizonHaze(float3 d, constant Params& p) {
    return desertSky(normalize(float3(d.x, 0.02, d.z)), p);
}

// Flowing Color Field's hash: a 2D point in, a random 0 to 1 out.
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.
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);
}

// How hard the wind blows, 0 to 1: two swells, 3 and 7 times a minute,
// times the gusts slider. The blown sand and the sound both read it.
float windGust(float t, constant Params& p) {
    float x = TAU * loopTime(t) / LOOP;
    float g = 0.55 + 0.25 * sin(3.0 * x + 0.6) + 0.2 * sin(7.0 * x + 2.1);
    return p.gusts * g;
}

// Once for every pixel of sand. The normal comes from the slope of the
// dunes, from the corners, and the ripples' slopes, measured here, are
// added where the sand lies gently enough to hold them. Then the sun, its
// shadows, and the haze.
float4 shade(Vertex v, constant Uniforms& u, constant Params& p) {
    float t = u.time;
    float2 xz = v.world.xz;
    float3 look = normalize(v.world - EYE);
    float dist = length(v.world - EYE);
    float2 slope = v.slope;
    // How wide a pixel is on the sand: wider far off, and wider again where
    // the sand is seen edge-on. Ripples fade out as it grows from a twelfth
    // of one to a sixth, and there are none on the steep slip faces.
    float3 flat = normalize(float3(-slope.x, 1.0, -slope.y));
    float facing = max(abs(dot(look, flat)), 0.05);
    float foot = dist * 2.0 / (1.5 * u.resolution.y) / facing;
    float hold = 1.0 - smoothstep(0.35, 0.6, length(slope));
    hold *= 1.0 - smoothstep(0.08, 0.18, foot / RIPPLE);
    float r = max(0.01, 0.5 * foot);
    float2 rx = float2(r, 0.0), rz = float2(0.0, r);
    float rgx = rippleHeight(xz + rx, t, p) - rippleHeight(xz - rx, t, p);
    float rgz = rippleHeight(xz + rz, t, p) - rippleHeight(xz - rz, t, p);
    slope += hold * float2(rgx, rgz) / (2.0 * r);
    float3 n = normalize(float3(-slope.x, 1.0, -slope.y));

    float3 s = towardSun(p);
    // The walk starts just over the sand, or over the square where a square
    // cuts under a rounded brink, so the brink shades it as the eye sees it.
    float lift = min(duneHeight(xz, t, p), v.world.y) + 0.02;
    float3 at = float3(xz.x, lift, xz.y);
    float sun = max(dot(n, s), 0.0) * sandShadow(at, s, t, p);
    float sky = 0.5 + 0.5 * n.y;
    float level = 0.18 + 0.1 * sky + 0.6 * sqrt(sun);
    level += (p.heat - 0.5) * 0.25;
    float3 col = kiln(level);

    // Blown sand: in a gust, veils of grains stream off the big dunes'
    // brinks and over the slip faces below, glowing with the sun behind.
    float2 w = windDir(p);
    float lt = loopTime(t);
    float drift = PERIOD * lt / LOOP;
    float b = dot(xz, float2(-w.y, w.x));
    float f = fract((dot(xz, w) - drift + crestBend(b)) / PERIOD);
    float past = (f - BRINK) * PERIOD;
    float veil = smoothstep(-0.4, 0.1, past) * exp(-max(past, 0.0) / 1.2);
    float2 q = float2(dot(xz, w) * 0.35 - lt * 3.0, b * 0.9);
    float streak = vnoise(q) * 0.6 + vnoise(q * 2.7) * 0.4;
    veil *= smoothstep(0.3, 0.8, streak) * windGust(t, p);
    veil *= 1.0 - smoothstep(20.0, 45.0, dist);
    float behind = pow(max(dot(look, s), 0.0), 2.0);
    col += kiln(0.85) * veil * (0.2 + 0.6 * behind);

    float fog = 1.0 - exp(-dist * 0.012);
    fog = max(fog, smoothstep(110.0, 165.0, dist));
    return float4(mix(col, horizonHaze(look, p), fog), 1.0);
}

// Behind the sand: the sky above the horizon, and haze below it, past the
// sheet's far edge, so the edge never shows.
float4 background(float2 uv, constant Uniforms& u, constant Params& p) {
    float aspect = u.resolution.x / u.resolution.y;
    float3x3 axes = cameraAxes();
    float2 s = (uv * 2.0 - 1.0) / 1.5;
    float3 d = normalize(axes[2] + axes[0] * s.x * aspect + axes[1] * s.y);
    return float4(d.y > 0.0 ? desertSky(d, p) : horizonHaze(d, p), 1.0);
}

// ---- Sound -----------------------------------------------------------------------------------------------------
// The sound is part of the shader: a function of time, run on the GPU 48,000 times a second and put in the video.

// Cracked Clay's hash: a whole number in, a random 0 to 1 out.
float hashU(uint n) {
    n = (n << 13u) ^ n;
    n = n * (n * n * 15731u + 789221u) + 1376312589u;
    return float(n & 0x7fffffffu) / float(0x7fffffff);
}

// Noise from random values joined `rate` times a second: lower is darker.
float noise1(float t, float rate, uint salt) {
    float x = t * rate;
    float i = floor(x);
    float f = x - i;
    f = f * f * (3.0 - 2.0 * f);
    uint n = uint(int(i) + 1000000);
    return mix(hashU(n * 31u + salt), hashU((n + 1u) * 31u + salt), f);
}

// Wind over sand: a dark roar that swells with each gust, the hiss of
// grains in the strong ones, toward the side the wind blows to, a singing
// dune's low hum near 90 Hz at the top of the strongest. It fades out as
// the minute ends and in as it starts, so it loops.
float2 sound(float t, constant Params& p) {
    float g = windGust(t, p);
    float roar = noise1(t, 180.0, 3u) - 0.5;
    roar += 0.7 * (noise1(t, 420.0, 7u) - 0.5);
    roar += 0.4 * (noise1(t, 950.0, 13u) - 0.5);
    float rl = noise1(t, 260.0, 17u) - 0.5;
    float2 v = float2(roar, 0.8 * roar + 0.6 * rl) * (0.25 + 1.1 * g);

    float grains = smoothstep(0.25, 0.75, g);
    float2 w = windDir(p);
    for (int i = 0; i < 2; i++) {
        float side = float(i) * 2.0 - 1.0;
        float h = noise1(t, 9000.0, 29u + uint(i)) - 0.5;
        h += 0.6 * (noise1(t, 5200.0, 41u + uint(i)) - 0.5);
        float flicker = 0.6 + 0.8 * noise1(t, 13.0, 53u + uint(i));
        float pan = clamp(0.5 + 0.3 * w.x + side * 0.15, 0.0, 1.0);
        v += float2(1.0 - pan, pan) * h * grains * flicker * 0.8;
    }

    float hz = 110.0 - 18.0 * p.height;
    float wob = 0.6 * (noise1(t, 7.0, 61u) - 0.5);
    float ph = TAU * hz * t + wob;
    float hum = sin(ph) + 0.45 * sin(2.0 * ph) + 0.2 * sin(3.0 * ph);
    hum *= 0.7 + 0.6 * noise1(t, 23.0, 67u);
    float boom = smoothstep(0.62, 0.92, g) * min(p.height, 1.2);
    v += float2(hum * boom * 0.18);

    float lt = loopTime(t);
    float fade = smoothstep(0.0, 2.5, min(lt, LOOP - lt));
    return tanh(v * 1.3) * 0.5 * 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.0, 1.0, 1.0, 8.0, 0.7, 0.5, 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(&params, 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 Geometry