Skip to content
Sample of Worley / Cellular Noise

hodgin_iquilezles · WorleyNoise

Worley / Cellular Noise

Quilez's classic 2D Worley noise: a unit grid of cells, each containing one pseudo-random feature point placed by a 2D integer hash. At every pixel, the value is the distance to the closest feature point in the 3x3 neighbourhood (F1), the F2-F1 cell-edge distance, or a colour from the integer cell id. Multi-octave summation produces tileable organic cellular textures.

Original: https://iquilezles.org/articles/smoothvoronoi/

▶ 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.

Family
hodgin_iquilezles
Source
hodgin × iquilezles
Evolution
noise_field
Render
scalar_field
Complexity
low
Agents
0
Year
2013
Author
Inigo Quilez (after Steven Worley, 1996)

Math essence

For a query point x with cell index p = floor(x) and fractional part f, scan the 3x3 neighbour cells (p+offset). Each cell hosts one feature point at hash(cell). Compute squared distances to these 9 points; keep F1 = min and F2 = second-min. Render via sqrt(F1) or sqrt(F2-F1) or by hashing F1's owner cell id. Multi-octave stacking scales frequency by 2 and amplitude by 0.55 per octave.

Parameters

Name Type Default Range Description
dim int 360 120–800 Square output size in pixels.
scale float 12 2–80 Number of Worley cells across the canvas.
octaves int 1 1–5 Number of stacked octaves.
contrast float 1 0.25–4 Post-field contrast multiplier.
mode string f1 null–null Output mode: f1, f2_minus_f1, or id (Voronoi cell id colour).
hue_shift float 0 0–1 Hue rotation applied when mode='id'.

Reference

First 60 lines of reference.py. Full source: library_external/hodgin_iquilezles/WorleyNoise/reference.py

from __future__ import annotations

from typing import ClassVar

import numpy as np


def _hash2(ix: np.ndarray, iy: np.ndarray, seed: int) -> tuple[np.ndarray, np.ndarray]:
    ix = ix.astype(np.uint64, copy=False)
    iy = iy.astype(np.uint64, copy=False)
    s = np.uint64(np.int64(seed) & 0xFFFFFFFF) * np.uint64(0x9E3779B97F4A7C15)
    s &= np.uint64(0xFFFFFFFFFFFFFFFF)
    h = (ix * np.uint64(0x27D4EB2F165667C5)) ^ (
        iy * np.uint64(0x165667B19E3779F9)
    ) ^ s
    h &= np.uint64(0xFFFFFFFFFFFFFFFF)
    h ^= h >> np.uint64(33)
    h *= np.uint64(0xFF51AFD7ED558CCD)
    h &= np.uint64(0xFFFFFFFFFFFFFFFF)
    h ^= h >> np.uint64(33)
    h *= np.uint64(0xC4CEB9FE1A85EC53)
    h &= np.uint64(0xFFFFFFFFFFFFFFFF)
    h ^= h >> np.uint64(33)
    a = (h & np.uint64(0xFFFFFFFF)).astype(np.float64) / 4294967296.0
    h ^= h >> np.uint64(27)
    b = (h & np.uint64(0xFFFFFFFF)).astype(np.float64) / 4294967296.0
    return a.astype(np.float32), b.astype(np.float32)


class WorleyNoise:
    """Worley / Cellular Noise — Inigo Quilez (iquilezles.org).

    Quilez's classic 2D Worley noise (a.k.a. cellular noise, Voronoi noise)
    partitions the plane into a regular grid of unit cells, places a single
    pseudo-random feature point inside each cell using his compact 2D hash,
    and at every query point ``x`` returns the distance to the closest
    feature point in the 3×3 cell neighborhood.

    The image is rendered by sampling the scalar field at every pixel and
    colouring by the F1 distance, the F2-F1 difference (cell edges), or a
    hash of the nearest cell id (Voronoi diagram).

    Reference code is from the canonical Quilez snippet:

        float voronoi( in vec2 x ) {
            ivec2 p = ivec2( floor( x ) );
            vec2  f = fract( x );
            float res = 8.0;
            for ( int j=-1; j<=1; j++ )
            for ( int i=-1; i<=1; i++ ) {
                ivec2 b = ivec2( i, j );
                vec2  r = vec2( b ) - f + hash2( p + b );
                float d = dot( r, r );
                res = min( res, d );
            }
            return sqrt( res );
        }

    (See also https://iquilezles.org/articles/voronoise/ for IQ's "voro-noise"
    extension.)

Try running it locally

The reference implementation is self-contained Python with numpy. Install the package and run:

python library_external/hodgin_iquilezles/WorleyNoise/reference.py

Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.

More from hodgin × iquilezles

Sample of Boids Flock (Hodgin Murmuration)

Boids Flock (Hodgin Murmuration)

hodgin_iquilezles · BoidsFlock

A 2D flock of boids steered by Craig Reynolds' three classic rules (separation, alignment, cohesion) over a coarse spatial-hash neighbor query.

View →
Sample of Fractional Brownian Motion noise

Fractional Brownian Motion noise

hodgin_iquilezles · FBMNoise

Quilez's canonical fBM: a 2D value-noise base summed over N octaves whose frequency doubles and amplitude is multiplied by G = exp2(-H) per octave.

View →
Sample of Magneto Particles (Hodgin Magnetosphere)

Magneto Particles (Hodgin Magnetosphere)

hodgin_iquilezles · MagnetoParticles

Particles pulled and pushed by N orbiting magnetic anchors whose charges flip sign over time.

View →
Sample of Mulberry32 PRNG (stratified scatter)

Mulberry32 PRNG (stratified scatter)

hodgin_iquilezles · Mulberry32RNG

Mulberry32 is a tiny 32-bit-state pseudo-random number generator popularised in the demoscene and used pervasively by Quilez and other graphics programmers as the RNG inside procedural sketches.

View →
Sample of Saturn Rings (orbital resonance)

Saturn Rings (orbital resonance)

hodgin_iquilezles · SaturnRings

Thousands of particles are initialized in an annulus between inner_radius and outer_radius around a central mass, with circular orbital velocities v = sqrt(M/r).

View →
Sample of Smooth-Minimum SDF field

Smooth-Minimum SDF field

hodgin_iquilezles · SmoothMinSDF

Two animated 2D SDF primitives (a moving circle and a translating slab) are blended with Quilez's quadratic-polynomial smooth-minimum and rendered as a scalar field.

View →

See all hodgin_iquilezles artworks →