Medium NatureAnimated
Twinkling starfield
A night sky of twinkling stars.
About the Twinkling starfield shader
A night sky of twinkling stars. Natural phenomena such as fire, clouds, lightning and snow. Most are noise functions shaped by a few well-chosen curves.
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 534 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
- Grid
uv * float2(?, ?)- Star in cell
only where hash(cell) > ?- Star position
jittered by float2(hash(cell + ?), hash(cell + ?)) - 0.5, times ?- Star shape
1.0 - smoothstep(0.0, ?, distance from jittered center)- Twinkle
0.5 + 0.5 * sin(time * ? + hash(cell) * ?)- Sky
lerp(float3(0.02,0.02,0.08), float3(0.1,0.05,0.2), uv.y)
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 twinkling starfield shader
- Use the hash() from the lessons (already in the starter).
- f = frac(g) - 0.5 is the pixel position inside its cell, centered. Subtract the jitter offset, take length.
- Multiply everything together: star mask × shape × twinkle, then add white to the sky.
Techniques used
- A hash function: a cheap way to get a repeatable random number from a position.
- smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
- Tiling with floor and frac: repeat a pattern by splitting space into cells.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Twinkling starfield 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: Set up the coordinates
Inside float4 frag(float2 uv)
float2 g = uv * float2(40.0, 30.0);
float2 cell = floor(g);
float2 f = frac(g) - 0.5; - floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
Part 3: Build the pattern
Still inside frag()
float h = hash(cell);
float2 o = (float2(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
float d = length(f - o); - A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
Part 4: Choose the colours
Still inside frag()
float tw = 0.5 + 0.5 * sin(time * 3.0 + h * 40.0);
float s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
float3 sky = lerp(float3(0.02, 0.02, 0.08), float3(0.1, 0.05, 0.2), uv.y); - uv runs from 0 to 1 across the picture, so uv.x and uv.y say how far along the screen this pixel is.
- smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
- step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
Part 5: Return the result
Still inside frag()
return float4(sky + s, 1.0); - 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);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(40.0, 30.0);
float2 cell = floor(g);
float2 f = frac(g) - 0.5;
float h = hash(cell);
float2 o = (float2(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
float d = length(f - o);
float tw = 0.5 + 0.5 * sin(time * 3.0 + h * 40.0);
float s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
float3 sky = lerp(float3(0.02, 0.02, 0.08), float3(0.1, 0.05, 0.2), uv.y);
return float4(sky + s, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float hash(vec2 p)
{
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(40.0, 30.0);
vec2 cell = floor(g);
vec2 f = fract(g) - 0.5;
float h = hash(cell);
vec2 o = (vec2(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
float d = length(f - o);
float tw = 0.5 + 0.5 * sin(time * 3.0 + h * 40.0);
float s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
vec3 sky = mix(vec3(0.02, 0.02, 0.08), vec3(0.1, 0.05, 0.2), uv.y);
return vec4(sky + s, 1.0);
} WGSL WebGPU, Bevy
fn hash(p: vec2f) -> f32
{
return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453);
}
fn frag(uv: vec2f) -> vec4f
{
let g = uv * vec2f(40.0, 30.0);
let cell = floor(g);
let f = fract(g) - 0.5;
let h = hash(cell);
let o = (vec2f(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
let d = length(f - o);
let tw = 0.5 + 0.5 * sin(time * 3.0 + h * 40.0);
let s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
let sky = mix(vec3f(0.02, 0.02, 0.08), vec3f(0.1, 0.05, 0.2), uv.y);
return vec4f(sky + s, 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);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(40.0, 30.0);
vec2 cell = floor(g);
vec2 f = fract(g) - 0.5;
float h = hash(cell);
vec2 o = (vec2(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
float d = length(f - o);
float tw = 0.5 + 0.5 * sin(TIME * 3.0 + h * 40.0);
float s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
vec3 sky = mix(vec3(0.02, 0.02, 0.08), vec3(0.1, 0.05, 0.2), uv.y);
return vec4(sky + s, 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);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(40.0, 30.0);
float2 cell = floor(g);
float2 f = fract(g) - 0.5;
float h = hash(cell);
float2 o = (float2(hash(cell + 7.0), hash(cell + 13.0)) - 0.5) * 0.6;
float d = length(f - o);
float tw = 0.5 + 0.5 * sin(time * 3.0 + h * 40.0);
float s = step(0.8, h) * (1.0 - smoothstep(0.0, 0.25, d)) * tw;
float3 sky = mix(float3(0.02, 0.02, 0.08), float3(0.1, 0.05, 0.2), uv.y);
return float4(sky + s, 1.0);
} Helper functions provided
The toolbox has: hash(). 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 twinkling starfield shader?
A night sky of twinkling stars. A good first step: use the hash() from the lessons (already in the starter). 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 twinkling starfield shader use?
It is built from hash function, smoothstep, tiling with floor and frac, sine waves, step. Each one is a small idea you can reuse in other effects.
Does this twinkling starfield 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.