Noise from scratch
glint · Examples · Tutorials · Gallery
Where randomness comes from: hashes, value and gradient noise, fractal noise, ridges, domain warping, Voronoi cells, marble and wood, and a landscape map.
A free interactive course of 10 steps in glint, a live shader editor in the browser: each step changes a running shader, and ticks itself when you've done its task.
1. Random numbers from a hash
Shaders have no random(): every pixel runs the same code at the same moment. What they use instead is a hash, a function that scrambles its input so thoroughly that the output looks random, but the same input always gives the same output.
fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453) does it in one line: dot turns the point into one number, sin of a big number jumps around wildly, multiplying by 43758.5 magnifies the tiny digits, and fract keeps just the part after the point. Neighbouring pixels get completely unrelated values.
Hashing every pixel gives TV static. Hash floor(p * 10.0) instead and every pixel in a 0.1-wide cell gets the same input, so the same value: a grid of random tiles. That's the first ingredient of all noise.
Your turn: Give each cell one random value: float v = hash(floor(p * 10.0));
2. Value noise: smooth randomness
Random tiles have hard edges. Noise is randomness that changes smoothly: put a random value at each grid corner, and blend between the four corners around a point.
floor(p) is the cell's corner, fract(p) how far across the cell we are (0 to 1 in x and y). Blend the bottom two corners by f.x, the top two by f.x, then those two results by f.y.
Blending straight by f leaves visible creases along the grid lines: the slope jumps at every edge. Easing f first with f * f * (3 - 2f) (smoothstep's curve) makes the slope zero at the edges, so the cells join seamlessly. Compare before and after.
Your turn: Ease the blend: vec2 u = f * f * (3.0 - 2.0 * f);
3. Frequency, amplitude and motion
Two numbers shape any noise. Frequency: how much you scale p before asking. noise(p * 2.0) gives big, slow blobs; noise(p * 20.0) fine grain. Amplitude: what you multiply the answer by, how strong it is.
To animate, slide where you read from: noise(p * 6.0 + vec2(time, 0.0)) drifts the pattern sideways. Adding time to a third input (3D noise) would make it boil in place instead; sliding is the cheap version.
The left half reads at frequency 3, the right at 12. Same noise, very different character.
Your turn: Make the right half drift: float v = noise(p * 12.0 + vec2(time, 0.0));
4. Gradient noise
Value noise has a tell: its bright and dark spots sit on the grid corners, and you can see the grid if you look. Gradient noise (the idea behind Perlin noise) fixes it: instead of a random value, each corner gets a random direction, a gradient.
Each corner's contribution is dot(gradient, offset from the corner): 0 exactly at the corner, rising on one side and falling on the other. Blending those slopes puts the hills and valleys between the corners, so the grid disappears and the shapes look more natural.
It returns roughly −1 to 1 (0 at every corner); * 0.5 + 0.5 maps it to 0..1 for display. Left: value noise. Right: gradient noise, once you switch it.
Your turn: Use gradient noise on the right: inside the if, change the line to v = gnoise(p * 8.0) * 0.5 + 0.5;
5. Fractal noise (fBm)
Nature has detail at every scale: a coastline has bays, the bays have coves, the coves have rocks. Fractal noise (fBm, fractal Brownian motion) copies that by adding several layers of noise, each with twice the detail and half the strength of the one before. Each layer is an octave, like in music.
The sum: 0.5 × noise(p) + 0.25 × noise(2p) + 0.125 × noise(4p) + … The first layer sets the big shapes, the rest add finer and finer detail on top. With 6 octaves the strengths add up to just under 1, so the result stays in 0..1.
The mat2 turns each layer a little as well as scaling it, so the grids of the layers don't line up (lined-up grids show as faint stripes).
Your turn: Add detail at every scale: const int OCTAVES = 6;
6. Ridges and turbulence
Shaping each layer before adding it changes the whole character. Centre the noise on zero (noise * 2 − 1, so −1..1) and take abs: every place it crossed zero becomes a sharp crease. That's turbulence: billowy, like smoke or cauliflower.
Flip it with 1 − abs(…) and the creases become sharp ridges at the top: mountain ranges, lightning, veins.
Worked example: noise of 0.5 is 0 after centring, so 1 − abs(0) = 1: a ridge top. Noise of 0.9 is 0.8 after centring: 1 − 0.8 = 0.2, low ground.
Your turn: Make ridges: change noise(p) in the fBm loop to (1.0 - abs(noise(p) * 2.0 - 1.0)).
7. Domain warping
Here's the trick behind most organic-looking shaders: before reading the noise at p, push p around by another noise. fbm(p + 2.0 * warp) where warp is itself made of fBm. Straight bands of noise get dragged into swirls, like marble, smoke or paint stirred in water.
The + 5.2 makes the second warp component read a different part of the noise, so x and y are pushed independently (otherwise everything slides diagonally). Adding time to one of them makes it flow.
Warp the warp (p + fbm(p + fbm(p))) and it gets even more liquid, at the cost of more noise per pixel.
Your turn: Warp the noise: vec2 warp = vec2(fbm(p + 0.15 * time), fbm(p + 5.2));
8. Cells: Voronoi
A different kind of noise: scatter one point in every grid cell (at a hashed position), then colour each pixel by its distance to the nearest point. You get cells, like cracked mud, giraffe skin or foam. It's called Voronoi or cellular noise.
The nearest point is always in this cell or one of its 8 neighbours, so the loop checks those 9 and keeps the smallest distance (F1) and the second smallest (F2).
F1 alone is bright in the middle of each cell and dark at the points. F2 − F1 is 0 exactly where two points are equally near: the borders between cells. Small values of it draw the cracks.
Your turn: Show the cell borders: float v = f2 - f1;
9. Marble and wood
Many natural materials are a simple regular pattern pushed around by noise. Marble is stripes, sin(p.x * k), with fBm added to x before the sine: the straight veins wobble and fold.
Wood is rings, fract(length(p) * k), with a little noise added to the radius so the rings wander like real grain.
The amount of noise added is everything: 0 is a perfect pattern, too much is just noise. Somewhere in between it looks like stone.
Your turn: Turn the stripes into marble: float turbulence = 4.0;
10. A landscape from noise
Treat fBm as a height map and colour by height, and it becomes a map: below 0.45 is water, then a thin beach, grass, rock, and snow on the peaks. Each band is a mix with a smoothstep, so the edges are soft.
Shading makes it read as relief: compare the height here with the height a tiny step up-left. Where it rises towards the light it's brighter, where it falls away darker.
Everything from this course is in here: a hash, value noise, octaves, and shaping the result.
Your turn: Put snow on the peaks: col = mix(col, vec3(1.0), smoothstep(0.72, 0.78, h));
Other courses: Basics · Medium · Advanced · Maths for shaders · Raymarching, step by step · Matrices, from scratch · Sound you can see · Colour and light · Passes and feedback · Skies and reflections · Vertex shaders, step by step · Raymarching 2: glow, fog and light · Raymarching 3: worlds · Raymarching 4: modelling, materials and motion · Fractals, step by step · Vertex shaders 2: lines, particles and moving meshes · Fractals 2: further in · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment · Blur and glow