shader_legends · KenPerlinNoisePure
Improved Noise — Ken Perlin (SIGGRAPH 2002, faithful reimplementation)
Ken Perlin's Improved Noise (SIGGRAPH 2002) ported faithfully to numpy. The algorithm wraps (x, y) into a 256-entry permutation table, computes the eight cube-corner gradient dot products with one of 12+4 gradient directions (cube-edge vectors padded to 16 to avoid division by 12), and trilinearly interpolates them using Perlin's quintic fade curve 6t^5 - 15t^4 + 10t^3. The implementation is a direct translation of the reference Java code at https://mrl.cs.nyu.edu/~perlin/noise/. fBM stacking is layered on top for natural-looking texture.
Original: https://mrl.cs.nyu.edu/~perlin/noise/
▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.
Math essence
For query (x, y): X = floor(x) & 255, Y = floor(y) & 255, xf = x - floor(x), yf = y - floor(y), u = fade(xf), v = fade(yf). A = p[X] + Y, B = p[X+1] + Y, AA = p[A], AB = p[A+1], BA = p[B], BB = p[B+1]. n_{ij} = grad(hash, xf-i, yf-j) where grad selects one of 16 cube-edge directions from h & 15. Result = lerp(v, lerp(u, n00, n10), lerp(u, n01, n11)). For fBM we sum amp*lac^k * perlin(lac^k * x) over k octaves.
Parameters
| Name | Type | Default | Range | Description |
|---|---|---|---|---|
dim | int | 360 | 120–800 | Square output size in pixels. |
scale | float | 0.04 | 0.005–0.4 | Frequency scale (input coordinate per pixel). |
octaves | int | 4 | 1–8 | Number of stacked fBM octaves. |
persistence | float | 0.55 | 0.2–0.9 | Amplitude falloff per octave (G in Quilez's fbm). |
lacunarity | float | 2 | 1.4–3.5 | Frequency multiplier per octave. |
shuffle | bool | true | null–null | If true, shuffle the original Perlin permutation table by seed. |
Reference
First 60 lines of reference.py. Full source:
library_external/shader_legends/KenPerlinNoisePure/reference.py
from __future__ import annotations
from typing import ClassVar
import numpy as np
_PERMUTATION = (
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54,
65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200,
196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52,
217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207,
206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119,
248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129,
22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218,
246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241,
81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
)
def _make_perm(seed: int) -> np.ndarray:
base = np.array(_PERMUTATION, dtype=np.int32)
rng = np.random.default_rng(seed)
perm = base.copy()
rng.shuffle(perm)
full = np.zeros(512, dtype=np.int32)
full[:256] = perm
full[256:] = perm
return full
def _fade(t: np.ndarray) -> np.ndarray:
"""Ken Perlin's improved fade curve: 6t^5 - 15t^4 + 10t^3."""
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
def _lerp(t: np.ndarray, a: np.ndarray, b: np.ndarray) -> np.ndarray:
return a + t * (b - a)
def _grad(hash_idx: np.ndarray, x: np.ndarray, y: np.ndarray) -> np.ndarray:
"""Perlin's grad for 2D noise (z=0), derived from the lo 4 bits.
The original 3D grad uses the lo 4 bits of the hash to pick one
of 16 gradient directions. For z=0, this collapses to:
h & 1 → sign of u
h & 2 → sign of v
h & 4 → u = x or y
h & 8 → v = x or y
"""
h = hash_idx & 15
u_x = (h & 1) == 0
u_y = ~u_x Try running it locally
The reference implementation is self-contained Python with numpy. Install the package and run:
python library_external/shader_legends/KenPerlinNoisePure/reference.py Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.
More from shader legends
Domain Repetition — Inigo Quilez tile-with-warp
shader_legends · IQDomainRepetition
Inigo Quilez's 'Domain Repetition' technique from his 2008 article.
Hash without Sine — Dave Hoskins hash art
shader_legends · TaterHashArt
Dave Hoskins's celebrated 'Hash without Sine' family of cross-platform-stable pseudo-random hash functions, presented as art.
Pixel City — top-down procedural city
shader_legends · PolygoncityPixelCity
A top-down 50x50 procedural city built from a deterministic hash of grid cells.
Shane Flow Field — particles in an fBM flow
shader_legends · ShaneFlowFieldFBM
Inspired by Shane's Noisey Image Flow Lines (Shadertoy 7sSSzw).
Shane Organic Noise — layered Perlin with rotation and twist
shader_legends · ShaneOrganicNoise
Shane Olsson's 'organic noise' Shadertoy technique: a base Perlin-style gradient noise is sampled in multiple octaves, with each successive layer rotated by an angle proportional to the layer index and twisted by sinusoidal warping about the canvas centre.