worley: Cellular Distance Patterns
Grid-based cellular texture generation
Worley noise creates cellular patterns by calculating distances to randomly distributed points within a grid structure. The algorithm generates organic cell-like formations by evaluating proximity to nearest neighbors.
The function operates through nearest-neighbor analysis across a 3×3 (2D) or 3×3×3 (3D) grid:
where:
- represents input coordinates
- denotes neighboring grid cells
- are grid cell positions
- are random offsets within each cell
- calculates distance (default: Euclidean)
The algorithm maintains two distances: (nearest) and (second nearest), enabling various cellular effects through their combination.
Function Variations
Function | Purpose | Output |
---|---|---|
worley2Vec2(p) | 2D distances | vec2(F1, F2) nearest distances |
worleyVec2(p) | 2D cellular | float inverted F1 distance |
worley2Vec3(p) | 3D distances | vec2(F1, F2) nearest distances |
worleyVec3(p) | 3D cellular | float inverted F1 distance |
Distance Metrics
The implementation uses Euclidean distance by default but can accommodate other metrics:
Euclidean: Manhattan: Chebyshev:
const fragment = () => { const coord = uv.mul(8) const cells = worleyVec2(coord) const edges = worley2Vec2(coord) const outline = edges.y.sub(edges.x).mul(8) return vec4(cells.mul(vec3(1, 0.8, 0.6)).add(outline.mul(0.3)), 1) }
const fragment = () => { const scale = uv.mul(6) const w1 = worley2Vec2(scale) const w2 = worley2Vec2(scale.add(vec2(0.5))) const interference = w1.x.sub(w2.x).abs().mul(4) const gradient = vec3(interference, interference.mul(0.7), interference.mul(0.4)) return vec4(gradient, 1) }
Algorithm Structure
The Worley noise algorithm follows this computational pattern:
- Grid Cell Identification: ,
- Neighbor Evaluation: Iterate through adjacent cells
- Random Offset Generation:
- Distance Calculation:
- Nearest Point Tracking: Update F1, F2 distances
- Result Inversion: Return for cellular appearance
Jitter Control
The random offset multiplier (jitter) controls cell regularity:
- jitter = 0: Perfect grid arrangement
- jitter = 1: Full random displacement (default)
- jitter > 1: Exaggerated irregularity
Mathematical Properties
Worley noise exhibits several characteristics:
Bounded Output: Values range for standard configurations Cell Continuity: Adjacent cells share boundary conditions Scale Invariance: Pattern maintains structure across scales Grid Alignment: Base structure follows integer grid spacing
Distance Combinations
Different F1/F2 combinations create varied effects:
Expression | Visual Effect |
---|---|
1 - F1 | Solid cells |
F2 - F1 | Cell boundaries |
F1 + F2 | Inverted cells |
F2 / F1 | Edge emphasis |
Implementation Details
The TSL implementation uses nested loops for neighbor evaluation:
- 2D: 9 cells (3×3 grid)
- 3D: 27 cells (3×3×3 cube)
Distance tracking maintains two variables:
distF1
: Nearest point distancedistF2
: Second nearest point distance
Random offsets employ consistent hashing for reproducible patterns while maintaining spatial coherence across grid boundaries.