pf-value-noise
N-Dimensional Value Noise Generator - A value noise generator for any number of dimensions. Similar to, but faster than Perlin noise.
Examples
// Require the module to use it.const ValueNoise = // Create a 3D value noise generator.const noise3D = dimensions: 3 // Use it to make a 100×100×100 grid of valuesconst resolution = 100let data = for let x = 0; x < resolution; ++x for let y = 0; y < resolution; ++y for let z = 0; z < resolution; ++z data const _ = data = valuedata56217// 0.6594545530358533
The following example creates the above picture.
// Create the canvasconst createCanvas = const width height = 800 200 const canvas = const ctx = canvas // Create the image dataconst ValueNoise = const noise3D = dimensions: 3 seed: 'pillow' const resolution = 100const imageData = ctxlet dataIndex = 0for let row = 0; row < height; ++row for let col = 0; col < width; ++col imageDatadatadataIndex++ = noise3D * 256 | 0 imageDatadatadataIndex++ = noise3D * 256 | 0 imageDatadatadataIndex++ = noise3D * 256 | 0 ++dataIndex // Export the image dataconst fs = ctxcanvas
API
ValueNoise
({Class}): Represents a value noise generator.
const ValueNoise = const noiseGenerator =
ValueNoise.constructor([options])
Arguments
[options]
(Object): An objects of options.
Option | Type | Default | Description |
---|---|---|---|
seed |
String | null |
RNG's seed |
dimensions |
Number | 2 |
Number of dimensions |
min |
Number | 0 |
Minimum value returned |
max |
Number | 1 |
Maximum value returned |
wavelength |
Number | 1 |
Size of the first octave |
octaves |
Number | 8 |
Number of octaves to sample |
octaveScale |
Number | 1/2 |
Scaling for successive octaves |
persistence |
Number | 1/2 |
Weight for successive octaves |
interpolation |
Function | cosine | Interpolation function used |
Note that even with the same seed, a different order of <Perlin>.get()
calls can change the overall noise function since its values are generated lazily.
wavelength
sets the size of the first octave, and each successive octave will be octaveScale
times the previous. The octaves are centered about the origin and added together according to their weight. The first octave has a weight of 1
, and each successive octave will be persistence
times the previous.
The octaves are sampled using the interpolation
function with signature function(a, b, t)
that returns a value between a
and b
according to the parameter 0 <= t <= 1
. The default interpolation function used is cosine interpolation.
{ return 1 - Math / 2 * b - a + a}
After the octaves are sampled and added together, the values are adjusted to fall between min
and max
. Note that the value distribution is roughly Gaussian depending on the number of octaves.
ValueNoise.prototype.get(coordinates)
Arguments
coordinates
(Array): The data point to get. Its length should matchdimensions
.
Returns
- (Number): The value at those coordinates.
const noise4D = dimensions: 4 noise4D// 0.538503118881535