A WebGPU desert for three.js: dune fields baked from wind physics in about 15 to 20 seconds (once: the result is cached), drawn as a walkable sand sea with kilometre-long dune shadows, ripples, glints, footprints, blowing sand, rocks, grass, shrubs and trees. The dune bake has no dependencies; everything else needs only three@0.187.x (three/webgpu + three/tsl). No textures or models are shipped: every dune, stone and plant is made in code from a seed.
Live Demo: directedgames.com/packages/directed-desert/ (requires a WebGPU-capable browser)
For AI agents: putting the desert into a game? Read INTEGRATION.md (shipped in the package at node_modules/directed-desert/INTEGRATION.md). It has every entry point with its options, the frame loop, and the rules that are easy to break. Changing the library itself? CLAUDE.md records how the code works, what was tried and dropped, and the constraints the size check and the tests enforce.
The package is not published on npm. Install it from GitHub and pin a commit:
git ls-remote https://github.com/directed-games/directed-desert HEAD # prints "<sha> HEAD"
npm install github:directed-games/directed-desert#<sha> three@0.187.0three is an optional peer dependency: the bake (directed-desert) runs without it. The directed lighting kit also needs directed-lighting, installed the same way; the basic kit needs nothing more.
Bake a field of big dunes and a tiling layer of small ones, draw them, and light them with the plain three.js kit:
import * as THREE from 'three/webgpu';
import { bakeDunes } from 'directed-desert';
import { createDuneTerrain } from 'directed-desert/three';
import { createRenderer, createLighting } from 'directed-desert/lighting/basic';
const renderer = await createRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 30000);
// Bake once at load, on the renderer's own device: big dunes, and small ones that ride on their windward slopes.
const device = renderer.backend.device;
const macro = await bakeDunes({ preset: 'transverse', N: 1024, cellSize: 4, wavelength: 480, smooth: 3, device, cache: true });
const detail = await bakeDunes({ preset: 'transverse', N: 512, cellSize: 1, wavelength: 40, smooth: 2, device, cache: true });
const terrain = createDuneTerrain({ renderer, macro, detail, options: { baseCell: 0.5, levels: 8, grid: 256, detailGain: 1.5 } });
scene.add(terrain.mesh);
const lighting = createLighting({ renderer, scene, camera, options: { hour: 16.5 } });
const timer = new THREE.Timer();
renderer.setAnimationLoop(() => {
timer.update();
const x = 1000, z = 1000;
camera.position.set(x, terrain.heightAt(x, z) + 30, z); // heightAt is the surface the GPU draws, to within a centimetre
camera.rotation.set(-0.05, 2.1, 0, 'YXZ');
terrain.update(camera);
terrain.setSun(lighting.sunDirection, lighting.sunColor, lighting.dark ?? 0);
lighting.update(timer.getDelta(), camera.position);
lighting.render();
});The two bakes take about 15 to 20 seconds with no cache (measured on an Apple M1 Max), and cache: true keeps them in IndexedDB for the next load. After that the per-frame cost is drawing only. For the sky and haze in the picture, and for dusk, night and sandstorms, swap the import for directed-desert/lighting/directed. Wind, plants, rocks, the walker's footprints and your own scanned textures are a few lines each: see INTEGRATION.md.
- Dune bake (
directed-desert), no dependencies, WebGPU compute:bakeDunes(options)— an FFT spectral seed sets crest spacing and orientation, then a continuum dune model (the Kroy, Sauermann and Herrmann family) grows the dunes: wind shear in Fourier space over the flow-separation envelope, saturated sand flux, mass-conserving transport and 34° avalanching. Presetstransverse,barchan,linearandstar;mode: 'physics', or'fast'(about 55 ms at 1024²).- Masks and bedrock (
supplyMask,stabilityMask,flatMask,bedrock,sandDepth) for roads, towns, bare pans and bounded desert patches between other biomes. evolveDunes(field, options)andrebakeRegion(field, region, options)— carry the physics on from an existing bake, everywhere or inside a feathered circle (a road buried by a storm).field.heightAt,normalAt,maskAt,sampleChunk— CPU queries for walking and colliders. The same seed and options give bit-identical fields on the same machine;cachestores bakes in IndexedDB or your own store.directed-desert/analyze—analyzeSpectrum, a windowed 2D spectrum (dominant wavelength and orientation).
- Three.js terrain (
directed-desert/three):createDuneTerrain({ renderer, macro, detail, options })— a geometry clipmap from centimetre cells underfoot to a view of many kilometres, with nothing discarded and no cracks between rings; small dunes weighted off the big crests; shading normals from gradient textures.- Dune shadows from the heightfield, the same at every distance: one lookup in a horizon map for the big dunes and a short march for the small ones.
shadowNodeandapplyDuneShadow(material, factor)put your own objects in the same shadow. - Sand: three octaves of wind ripples, grain glints that hold still on the sand and are cut by every shadow, slip-face streaks, old footprints along winding trails, and a floor of cracked clay where the sand runs out.
- A walker:
walk(x, z)presses a trail of prints into the sand that fades and fills in with the wind;setWalkershades the ground at its feet. - Your own scans, optional:
detailMapsfor the sand andfloorMapsfor the floor, hex-tiled so the repeat does not show and packed into two array textures however many layers you pass. setColors,updateField,setMorphTargetandsetMorphAlpha— live colours, and blending between two bakes with no compute.
- Scatter add-ons, instanced, grown in code, placed from a position hash so the same thing always stands in the same place:
directed-desert/plants—createDuneGrass: seven kinds of clump, green to cured straw.directed-desert/rocks—createDuneRocks: pebbles to boulders on the bare floor, with sand banked against them.directed-desert/flora—createDuneBushesandcreateDuneTrees: eight shrub shapes, dead trees and a twin-trunked tamarisk, with optional bark scans.- All of them: per-tile culling, three levels of detail by size on screen,
amountandsetAmount, and shadow budgets from the lighting kit.
- Wind (
directed-desert/wind) —createDuneWind: a plume of sand off every brink, drawn as a small volume that glows against the sun; sand streaming along the ground in gusts; a puff of dust at each of the walker's steps. OnesetStrength, from calm to gale. - Horizon and decals —
directed-desert/horizon(createDuneHorizon: a hazy escarpment 11 km out) anddirected-desert/tracks(createFootprints: footprint decals along a path). - Shader warm-up —
directed-desert/warmup(warmRender): draws the first frames with every add-on mesh shown, so shaders compile behind the loading screen and not as stalls mid-walk. - Fidelity tiers —
directed-desert/quality(fidelity,pixelRatioFor,terrainFor): five steps fromlowtosuper-duperthat trade look for frame rate (canvas pixels, FSR render scale, shadows, ground mesh). The demo's Fidelity slider uses them (?fidelity=high). - Lighting kits (
directed-desert/lighting/*) — lighting is a plugin with a small contract, and the terrain never imports one:lighting/directedwraps directed-lighting: physical sky, a day that runs through dusk into a starlit night, heat haze, fog and god rays, sandstorms, six tiers of shadow quality.lighting/basicis plain three.js lights and fog.
- Kept small and tested: the core is 10.4 KB and the terrain adapter 15.4 KB minified and gzipped, each under a 16 KB budget that
npm run sizeenforces along with the import rules; 14 acceptance tests run in headless Chrome on a real WebGPU adapter (npm run test:webgpu).
We are looking for collaborators! If you're interested in contributing to procedural terrain, WebGPU, TSL shaders or demo scenes for Three.js, reach out on X at x.com/jgrebski.
Released under the MIT License: free to use, change, and ship in commercial or non-commercial projects, as long as the copyright and license notice stay with the code. Contributions are welcome: run npm run size and npm run test:webgpu before sending a change, and read the hard constraints at the top of CLAUDE.md.
The grain glints, the walker's trail and the shade at the walker's feet follow techniques from mrdoob's Codevember 2026, day 19 ("Erosion", MIT), re-implemented here; the glints also owe to John Edwards' "Sand Rendering in Journey" (GDC 2013). The dune physics follows Kroy, Sauermann & Herrmann (2002) and the later work cited in INTEGRATION.md.
Walk the desert live at directedgames.com/packages/directed-desert/, or run it locally:
npm install
npm run assets # fetch the demo's Poly Haven scans (optional: the demo runs without them)
npm run serve # demos and the test page on http://127.0.0.1:8788
npm run size # gzip budgets and import/asset guards
npm run test:webgpu # the 14 acceptance tests, headless Chrome or Edge with WebGPU/demo/index.html— the walkable desert in the picture, with a control window (F2 hides it) for the time of day, wind, shadows, and the amounts of rocks, grass, shrubs and trees.?lighting=basicruns it without directed-lighting./demo/gallery.html— the four presets and the masks, top-down./demo/heightmap.html— a 2D heightmap and mask inspector./test/index.html— the acceptance tests in a page.
If Chrome or Edge is not at a standard path, set CHROME_PATH. The tests need a real WebGPU adapter.
Demo assets. The demo layers CC0 scans from Poly Haven over the procedural sand, floor, rocks and bark (credited although CC0 does not require it). They are not in the repository or the package: npm run assets downloads them, checked against pinned checksums, into the gitignored demo/assets/.
