1.9 KiB
worley/worley
(no submodules)
Purpose
The worley module implements Steven Worley's cellular noise in 2D and 3D. At every point in space it computes the distance to the nearest (F1) and second-nearest (F2) feature point, where feature points are scattered per-cell according to a seeded PRNG. The resulting fields (F1, F2, F2-F1) give procedural cell structures used for lava rock, cracked surfaces, Voronoi textures, stone patterns, and biological cell walls.
Features
WorleyNoiseclass andworley2/worley3functional APIsWorleyMode:"F1" | "F2" | "F2-F1"- 3×3 (2D) / 3×3×3 (3D) neighbourhood search — the canonical compromise between quality and performance
- 1..3 feature points per cell, seeded per-cell so different seeds give completely different fields
Usage: Basic Example
import { worley2 } from "@suaveplan/noise";
const distance = worley2("stones", 0.25, 0.75); // F1 default
Usage: Advanced Example
import { WorleyNoise } from "@suaveplan/noise";
const w = new WorleyNoise("cracks");
for (let y = 0; y < 256; y += 1) {
for (let x = 0; x < 256; x += 1) {
const edge = w.noise2(x * 0.05, y * 0.05, "F2-F1");
// Paint edges where two cells meet equidistantly.
}
}
API Reference
class WorleyNoise(seed: string | number = 0)— one seeded fieldnoise2(x, y, mode?): number—modedefaults to"F1"noise3(x, y, z, mode?): numberworley2(seed, x, y, mode?): numberworley3(seed, x, y, z, mode?): number
Implementation Notes
Feature points are not cached between evaluations; we recreate the per-cell Sfc32Rng each time. This keeps the class stateless and thread-safe but makes repeated evaluation at nearby points slightly more expensive. For heavy workloads the simplest optimisation is to cache featurePoints2/3 results by cell coordinate — feel free to subclass and override those protected methods.