Noise: Value, Perlin and fbm
Random numbers per tile (from the Patterns lesson) jump from cell to cell. Nature does not: clouds, terrain, marble, fire and water change smoothly, with detail at many scales. Noise functions give you exactly that: a random-looking value that varies continuously with position, and returns the same value for the same input every time. They are the most-used building block in procedural shaders.
The math was covered in Value and Perlin Noise on the CPU. This lesson builds the GPU versions.
Step 1: a hash
Everything starts with a hash: a function from a cell coordinate to a pseudo-random number in 0..1. The classic one-liner fract(sin(dot(c, k)) * 43758.5453) is short but its quality depends on the GPU's sin precision. An integer-free alternative that behaves the same everywhere is Dave Hoskins' "hash without sine", used below.
Step 2: value noise
Value noise puts a random value at each grid corner and interpolates between the four corners around the pixel. Plain linear interpolation shows the grid as creases, so the blend factor goes through a smooth curve first: f * f * (3.0 - 2.0 * f) (the same curve as smoothstep).
Here is the idea in plain JavaScript, in 1D, so you can see the numbers. Each integer has a fixed random value; between integers the values blend smoothly:
// Integer hash -> 0..1: same input, same output, every run
function hash(n) {
n = Math.imul(n ^ 61, 0x27d4eb2d) ^ (n >>> 15);
n = Math.imul(n ^ (n >>> 13), 0x85ebca6b);
return ((n ^ (n >>> 16)) >>> 0) / 4294967296;
}
const smooth = (f) => f * f * (3 - 2 * f);
function valueNoise(x) {
const i = Math.floor(x), f = x - i;
return hash(i) + (hash(i + 1) - hash(i)) * smooth(f);
}
for (let x = 0; x <= 2; x += 0.25) {
console.log(x.toFixed(2), valueNoise(x).toFixed(3));
}0.00 0.828
0.25 0.822
0.50 0.808
0.75 0.793
1.00 0.787
1.25 0.723
1.50 0.582
1.75 0.441
2.00 0.377
At the integers (0, 1, 2) you get the raw random values; in between, a smooth curve. The 2D shader does the same with four corners:
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float valueNoise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y * 8.0;
float n = valueNoise(p + vec2(iTime * 0.5, 0.0));
fragColor = vec4(vec3(n), 1.0);
}Look closely and you can still see a blocky, square structure: value noise has its peaks and valleys on the grid corners.
Step 3: gradient (Perlin) noise
Ken Perlin's fix: put a random gradient direction at each corner instead of a value. Each corner contributes dot(gradient, offsetFromCorner), which is zero at the corner itself and rises or falls away from it. Features no longer sit on the grid, and the result looks far more natural. The output is centered on 0 (about ±0.71 in 2D with unit-length gradients; a little wider with the cheap, not-quite-unit gradients below), so it is usually remapped with 0.5 + 0.5 * n.
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy) * 2.0 - 1.0; // -1..1
}
float gradientNoise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); // quintic fade, Perlin 2002
float a = dot(hash22(i), f);
float b = dot(hash22(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0));
float c = dot(hash22(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0));
float d = dot(hash22(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y * 8.0;
float n = 0.5 + 0.5 * gradientNoise(p + vec2(0.0, iTime * 0.5));
// Left half: noise. Right half: contour lines of the same noise.
if (fragCoord.x > iResolution.x * 0.5) n = step(0.5, fract(n * 10.0));
fragColor = vec4(vec3(n), 1.0);
}The quintic fade 6f⁵ - 15f⁴ + 10f³ (from Perlin's 2002 "Improved Noise") has zero first and second derivatives at the cell edges. The cubic one is fine for color but shows seams once you compute lighting normals from the noise, as the Water lesson does.
Note: Simplex noise (also Perlin's) uses a triangle grid instead of squares: fewer corners per sample, fewer directional artifacts, cheaper in 3D and 4D. Its patent expired in 2022. For learning, square-grid gradient noise is easier to read and looks nearly as good in 2D.
Step 4: fbm, many octaves
One layer of noise looks like smooth blobs. Real surfaces have big shapes and small details. Fractal Brownian motion (fbm) sums several layers ("octaves"), each at double the frequency (lacunarity = 2) and half the amplitude (gain = 0.5):
| Octave | Frequency | Amplitude | Adds |
|---|---|---|---|
| 1 | 1 | 0.5 | large shapes |
| 2 | 2 | 0.25 | medium bumps |
| 3 | 4 | 0.125 | small detail |
| 4 | 8 | 0.0625 | fine grain |
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash12(i), hash12(i + vec2(1, 0)), u.x),
mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
float sum = 0.0, amp = 0.5;
mat2 rot = mat2(0.8, 0.6, -0.6, 0.8); // rotate each octave to hide grid alignment
for (int i = 0; i < 6; i++) {
sum += amp * noise(p);
p = rot * p * 2.0;
amp *= 0.5;
}
return sum;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y * 3.0;
float n = fbm(p + vec2(iTime * 0.2, 0.0));
vec3 sky = mix(vec3(0.25, 0.45, 0.8), vec3(0.6, 0.75, 0.95), fragCoord.y / iResolution.y);
vec3 color = mix(sky, vec3(1.0), smoothstep(0.45, 0.75, n)); // clouds
fragColor = vec4(color, 1.0);
}Six octaves cost six noise calls per pixel. That is cheap on a desktop GPU and noticeable on a phone, so pick the octave count for the smallest detail that is actually visible.
Step 5: domain warping
The most striking noise trick costs nothing new: feed noise into the coordinates of another noise. fbm(p + fbm(p)) pushes the pattern around by itself and produces swirling, marble- and smoke-like shapes:
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash12(i), hash12(i + vec2(1, 0)), u.x),
mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
float s = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { s += a * noise(p); p = p * 2.03 + 17.0; a *= 0.5; }
return s;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y * 3.0;
float t = iTime * 0.1;
vec2 q = vec2(fbm(p + t), fbm(p + vec2(5.2, 1.3)));
vec2 r = vec2(fbm(p + 4.0 * q + vec2(1.7, 9.2) + t), fbm(p + 4.0 * q + vec2(8.3, 2.8)));
float n = fbm(p + 4.0 * r);
vec3 color = mix(vec3(0.1, 0.05, 0.2), vec3(0.9, 0.5, 0.2), n);
color = mix(color, vec3(0.2, 0.6, 0.7), length(q) * 0.6);
fragColor = vec4(color * 1.3, 1.0);
}Domain warping is expensive (here 15 noise layers per pixel), so engines often bake it into a texture. In a shader you can afford it for a hero effect, not for every surface.
Test yourself
What is the key difference between value noise and gradient (Perlin) noise?
- Value noise is 3D only
- Gradient noise stores a random direction at each corner and uses dot products, so features don't sit on the grid
- Gradient noise uses no hash
- Value noise is smoother
In a standard fbm, each new octave has...
- half the frequency and double the amplitude
- double the frequency and half the amplitude
- the same frequency and amplitude
- a random frequency
What is domain warping?
- Rotating the frame
- Offsetting the input coordinates of a noise by another noise
- Clamping noise to 0..1
- Using a texture instead of noise