Medium 3DAnimated
Moon
A grey cratered-looking moon with a sharp day/night terminator.
About the Moon shader
A grey cratered-looking moon with a sharp day/night terminator. Three-dimensional scenes drawn entirely by a fragment shader: lighting, shadows and shapes from signed distance functions.
How this challenge works
You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 1048 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
What it is built from
- Sphere
R = ?, n as in the globe- Surface noise
tex = fbm((n.xy * ? + float2(time * ?, 0.0)) * 2.0)- Base color
lerp(float3(0.35,0.35,0.38), float3(0.8,0.8,0.78), tex)- Light
normalize(float3(-?, ?, ?)), brightness ? + ? * diffuse- Space
float3(0.02, 0.02, 0.05)
The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.
How to make a moon shader
- Use the sphere normal's xy as 2D coordinates into fbm.
- A tiny ambient (0.04) keeps the dark side almost black.
Techniques used
- Fractal noise (fBm): layers of noise at doubling frequency give natural-looking detail.
- Diffuse lighting: brightness from the angle between the surface and the light.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Moon shader code, explained step by step
Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.
Part 1: Helper: hash()
float hash(float2 p)
{
return frac(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
} - A hash function. Shaders have no random(), so this turns a position into a pseudo-random number between 0 and 1. The same input always gives the same output, which is what makes noise repeatable.
Part 2: Helper: vnoise()
float vnoise(float2 p)
{
float2 i = floor(p);
float2 f = frac(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + float2(1.0, 0.0));
float c = hash(i + float2(0.0, 1.0));
float d = hash(i + float2(1.0, 1.0));
return lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y);
} - Value noise. It takes the four (or eight) corners of the grid cell the point is in, hashes each one to a random value, and blends them with a smoothed fraction, so the result changes gently across the picture.
Part 3: Helper: fbm()
float fbm(float2 p)
{
float v = 0.0;
float amp = 0.5;
for (int i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + float2(13.7, 7.1);
amp *= 0.5;
}
return v;
} - Fractal noise (fBm). It adds the noise together several times, each time at twice the frequency and half the strength. Big shapes come from the first layers and fine detail from the last ones.
Part 4: Set up the coordinates
Inside float4 frag(float2 uv)
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p); - Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
- Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.
Part 5: Choose the colours
Still inside frag()
float mask = step(r2, R * R);
float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
float tex = fbm((n.xy * 3.0 + float2(time * 0.05, 0.0)) * 2.0); - fbm adds layers of noise at doubling frequency: large shapes plus finer and finer detail, as in clouds, fire and terrain.
- step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- time is the number of seconds since the shader started, so anything that uses it moves.
- normalize() scales a direction to length 1, so only its direction matters.
Part 6: Choose the colours (continued)
Still inside frag()
float3 base = lerp(float3(0.35, 0.35, 0.38), float3(0.8, 0.8, 0.78), tex);
float diff = max(dot(n, normalize(float3(-0.6, 0.3, 0.7))), 0.0);
float3 col = base * (0.04 + 0.96 * diff); - lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
- normalize() scales a direction to length 1, so only its direction matters.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 7: Colour the pixel and return it
Still inside frag()
return float4(lerp(float3(0.02, 0.02, 0.05), col, mask), 1.0); - lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
- The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float hash(float2 p)
{
return frac(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}
float vnoise(float2 p)
{
float2 i = floor(p);
float2 f = frac(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + float2(1.0, 0.0));
float c = hash(i + float2(0.0, 1.0));
float d = hash(i + float2(1.0, 1.0));
return lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y);
}
float fbm(float2 p)
{
float v = 0.0;
float amp = 0.5;
for (int i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + float2(13.7, 7.1);
amp *= 0.5;
}
return v;
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p);
float mask = step(r2, R * R);
float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
float tex = fbm((n.xy * 3.0 + float2(time * 0.05, 0.0)) * 2.0);
float3 base = lerp(float3(0.35, 0.35, 0.38), float3(0.8, 0.8, 0.78), tex);
float diff = max(dot(n, normalize(float3(-0.6, 0.3, 0.7))), 0.0);
float3 col = base * (0.04 + 0.96 * diff);
return float4(lerp(float3(0.02, 0.02, 0.05), col, mask), 1.0);
} GLSL Shadertoy, three.js, OpenGL
float hash(vec2 p)
{
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
float vnoise(vec2 p)
{
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float fbm(vec2 p)
{
float v = 0.0;
float amp = 0.5;
for (int i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + vec2(13.7, 7.1);
amp *= 0.5;
}
return v;
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p);
float mask = step(r2, R * R);
vec3 n = normalize(vec3(p, sqrt(max(R * R - r2, 0.0))));
float tex = fbm((n.xy * 3.0 + vec2(time * 0.05, 0.0)) * 2.0);
vec3 base = mix(vec3(0.35, 0.35, 0.38), vec3(0.8, 0.8, 0.78), tex);
float diff = max(dot(n, normalize(vec3(-0.6, 0.3, 0.7))), 0.0);
vec3 col = base * (0.04 + 0.96 * diff);
return vec4(mix(vec3(0.02, 0.02, 0.05), col, mask), 1.0);
} WGSL WebGPU, Bevy
fn hash(p: vec2f) -> f32
{
return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453);
}
fn vnoise(p: vec2f) -> f32
{
let i = floor(p);
var f = fract(p);
f = f * f * (3.0 - 2.0 * f);
let a = hash(i);
let b = hash(i + vec2f(1.0, 0.0));
let c = hash(i + vec2f(0.0, 1.0));
let d = hash(i + vec2f(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
fn fbm(pIn: vec2f) -> f32
{
var p = pIn;
var v = 0.0;
var amp = 0.5;
for (var i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + vec2f(13.7, 7.1);
amp *= 0.5;
}
return v;
}
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0) * 2.0;
let R = 0.8;
let r2 = dot(p, p);
let mask = step(r2, R * R);
let n = normalize(vec3f(p, sqrt(max(R * R - r2, 0.0))));
let tex = fbm((n.xy * 3.0 + vec2f(time * 0.05, 0.0)) * 2.0);
let base = mix(vec3f(0.35, 0.35, 0.38), vec3f(0.8, 0.8, 0.78), tex);
let diff = max(dot(n, normalize(vec3f(-0.6, 0.3, 0.7))), 0.0);
let col = base * (0.04 + 0.96 * diff);
return vec4f(mix(vec3f(0.02, 0.02, 0.05), col, mask), 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float hash(vec2 p)
{
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
float vnoise(vec2 p)
{
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float fbm(vec2 p)
{
float v = 0.0;
float amp = 0.5;
for (int i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + vec2(13.7, 7.1);
amp *= 0.5;
}
return v;
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p);
float mask = step(r2, R * R);
vec3 n = normalize(vec3(p, sqrt(max(R * R - r2, 0.0))));
float tex = fbm((n.xy * 3.0 + vec2(TIME * 0.05, 0.0)) * 2.0);
vec3 base = mix(vec3(0.35, 0.35, 0.38), vec3(0.8, 0.8, 0.78), tex);
float diff = max(dot(n, normalize(vec3(-0.6, 0.3, 0.7))), 0.0);
vec3 col = base * (0.04 + 0.96 * diff);
return vec4(mix(vec3(0.02, 0.02, 0.05), col, mask), 1.0);
}
void fragment()
{
// Godot's UV starts at the top-left, the lessons use bottom-left
COLOR = frag(vec2(UV.x, 1.0 - UV.y));
} Metal iOS, macOS
float hash(float2 p)
{
return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}
float vnoise(float2 p)
{
float2 i = floor(p);
float2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + float2(1.0, 0.0));
float c = hash(i + float2(0.0, 1.0));
float d = hash(i + float2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float fbm(float2 p)
{
float v = 0.0;
float amp = 0.5;
for (int i = 0; i < 5; i++)
{
v += amp * vnoise(p);
p = p * 2.0 + float2(13.7, 7.1);
amp *= 0.5;
}
return v;
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p);
float mask = step(r2, R * R);
float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
float tex = fbm((n.xy * 3.0 + float2(time * 0.05, 0.0)) * 2.0);
float3 base = mix(float3(0.35, 0.35, 0.38), float3(0.8, 0.8, 0.78), tex);
float diff = max(dot(n, normalize(float3(-0.6, 0.3, 0.7))), 0.0);
float3 col = base * (0.04 + 0.96 * diff);
return float4(mix(float3(0.02, 0.02, 0.05), col, mask), 1.0);
} Helper functions provided
The toolbox has: hash(), vnoise(), fbm(). Add them above your frag function with one click.
Solve it in your language
Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start codingQuestions about this shader
How do I make a moon shader?
A grey cratered-looking moon with a sharp day/night terminator. A good first step: use the sphere normal's xy as 2D coordinates into fbm. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.
What techniques does the moon shader use?
It is built from fractal noise (fbm), diffuse lighting, step, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this moon shader work in Unity, Unreal, Godot and Shadertoy?
Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.