GLSL Moon Shader
Shader not running? It needs a browser with WebGL2. If it's slow, open Controls and set Quality to Low.
Source moon.fragView and edit the GLSL, live
This moon started as a GLSL material in TouchDesigner, and I ported it to WebGL2 so it runs live right here in the page. There are no texture maps or height maps. Every crater, mountain and dark sea is made from noise, worked out per pixel, every frame.
The shader is about 450 lines. Here's how it's put together.

Sticking to the surface
In TouchDesigner the shader is a material on a sphere. In the browser there's no sphere mesh: each pixel fires a ray from the camera and works out where it hits a radius-1 sphere, which also gives a clean, anti-aliased edge.
Every noise lookup uses the point's position on the sphere before rotation, not its position in the world. That pins the whole surface to the moon, so craters travel with it as it spins, while the light stays where it is.
Maria and highlands
The first layer is the big picture: dark seas of basalt (the maria) and bright highlands. One six-octave noise field is pushed through a narrow smoothstep, which turns soft clouds into blobs with fairly crisp, ragged coastlines. That mask blends between two colours: a dark grey for the maria and near-white for the highlands.
On top of that sit a few more layers of colour:
- A second dark layer, from a separate noise field, dims parts of the highlands by up to about 30%, so they aren't one flat white.
- Sparkle: fine ridged noise brightens the highlands a little, like light catching rough, broken rock.
- Bright spots: a Voronoi pattern scatters small bright dots, standing in for fresh, tiny impacts. They only ever brighten, so they show up most on the maria.

Craters
Craters come from Voronoi noise: space is split into cells, each with one randomly placed point, and every pixel finds its nearest point. That point becomes a crater centre, and the distance to it says where the pixel sits in the crater.
There are five of these layers at different scales, from a few large craters to many small ones. Before each lookup, the position is nudged by a low-frequency noise so the cells don't line up in a visible grid. Each crater also gets its own random values from a hash of its cell:
- Whether it exists at all: each layer has a density, the fraction of cells that hold a crater. That's what the Large and Small craters sliders change.
- Size: the random radius is squared, so most craters are small and only a few are big.
- Age: fresh craters are deep, with sharp rims and a bright halo of thrown-out material. Old ones are shallow, faded and more irregular.
- An uneven outline: two octaves of noise wobble the radius, so craters aren't perfect circles.
The shape of each crater is a profile across its radius: a shallow, rounded bowl, a raised rim just inside the edge and a fade to zero at the boundary, so it never leaves a step. Mid-sized craters are dug deeper, and the biggest get lower rims, as on the real moon. Near the edge of a cell the shader blends the nearest crater with the second nearest, which hides the seams between cells.

Two rules make the layers work together:
- Big craters avoid the maria. The three largest layers are scaled by the maria mask, so they mostly appear in the highlands, and the seas stay smoother, with only small craters. On the moon, the maria are younger lava plains that have had less time to collect big impacts.
- Newer craters cut through older ones. Wherever a small crater's bowl overlaps a larger crater's rim, the rim is flattened, so the small crater looks as if it landed later and punched through.
Craters also change the colour. Their floors darken a little, and in the maria a crater brightens toward white, as if the impact dug down through the dark basalt to the brighter rock underneath.

Mountains and fine detail
Underneath the craters, three octaves of ridged noise build mountains. Ridged noise flips the valleys of ordinary noise into sharp crests, which reads as ranges and ridges rather than soft hills. A separate noise mask flattens the mountains away over large areas, so they cluster rather than covering everything.
Then come five more octaves of fine noise, from broad swells down to grain you only see close up, plus a layer of small ridged wrinkles. They're turned down inside crater bowls, so the floors stay smoother than the ground around them.
Ray systems
A few young craters throw out long, bright streaks, like Tycho and Copernicus on the real moon. The shader picks crater centres from a coarse grid around the sphere and keeps only the freshest. Around each one it measures the angle to the pixel and runs it through a sharpened cosine, which gives 7 to 16 thin spokes. A second, softer set at half the count fills in between. The streaks fade with distance along the surface and reach about a quarter of the way around the moon.
Each pixel checks 125 neighbouring cells, because a long ray can reach far from its crater. Most cells are thrown out after two quick random tests.

Lighting the bumps
All the height (craters, mountains and fine detail) is never actually built as geometry. It only changes the lighting. The shader adds the layers into one height value, then samples that height twice more, a tiny step away in two directions along the surface. The differences give the slope, and the slope tilts the lighting normal.
Those two extra samples are the expensive part of the shader, since each one runs all five crater layers again. The first sample reuses the crater results from the colour pass, which saves a third set.

The lighting itself is simple: one point light with a matte diffuse term and a very broad, faint highlight. One fix matters a lot. Bump mapping is a cheat, so a crater rim facing the light can catch light even on the night side, where the sphere itself is in shadow. The shader limits the bumped lighting to twice the lighting of the smooth sphere. Rims can still glint in daylight, but the dark side stays dark.
Low light is where all of this shows best. Close to the terminator, the line between day and night, the light skims the surface, rims cast bright edges and bowls fill with shadow.

Controls
Drag on the moon to rotate it, scroll or pinch to zoom, and double-click to reset. Open Controls in the top-right corner for:
- Quality: Low, Medium (the default) and High.
- View: the lit surface, or the Bump normals, Crater albedo and Crater cells views shown above.
- Sliders for rotation speed, light angle, bump strength, mountain height, crater bowl depth and rim height, large and small craters, surface wrinkles, bright spots, contrast, brightness and bloom.
- Pause and Reset.
Under the hood
The browser version rebuilds the rest of my TouchDesigner network around the shader:
- Camera and light: their positions and rotations are copied from the TouchDesigner scene, including the camera's roll that tilts the moon in the frame.
- Spin: in TouchDesigner the sphere turns 10° a second with absolute time; here a small loop does the same, and dragging adds a rotation on top.
- Lighting functions: TouchDesigner gives a material its lights through built-in functions. The port supplies a small stand-in with the same name and inputs, so the shader code doesn't change.
- Bloom: the brightest parts are pulled out at quarter resolution, blurred in two passes and added back, matching the Bloom TOP.
The GLSL source sits below the shader on this page. You can edit it and the moon recompiles live. Try changing the crater colours or the smoothstep on the maria mask and see what happens.
— CGDavidB