npm install noise3d
var noise = require("noise3d");
var perlin = noise.createPerlin({
interpolation: noise.interpolation.linear,
permutation: noise.array.shuffle(noise.array.range(0, 255), Math.random)
});
var brownian = noise.createBrownianMotion({
octaves: 4,
persistence: 0.5,
noise: perlin
});
for (var x = 0; x < 512; x++) {
for (var y = 0; y < 512; y++) {
var z = 1; // keep z constant for 2D noise
image[x][y] = 127 + brownian(x / 64, y / 64, z) * 128;
}
}All noise functions implement following interface:
value = noise(x, y, z)xx coordinateyy coordinatezz coordinatevaluenoise value between [-1, +1]
Perlin Noise
var perlin = noise.createPerlin({
interpolation: noise.interpolation.linear,
permutation: noise.array.shuffle(noise.array.range(0, 255), Math.random)
});params.interpolationinterpolation method (see utility methods)params.permutationpermutation array (numbers 0 to 255 in pseudorandom order)
Checkerboard Pattern
var checker = noise.createCheckerboard({
interpolation: noise.interpolation.nearestNeighbour,
size: 2
});params.interpolationinterpolation method (see utility methods)params.sizesize between checkerboard rectangles
Constant Value
var constant = noise.createConstant({
value: 0.3
});params.valueconstant value between [-1, +1]
Inverts noise values
var invert = noise.createInverter({
noise: perlin
});params.noisethe noise function (x, y, z) to invert
Fractal Brownian Motion
var brownian = noise.createBrownianMotion({
octaves: 4,
persistence: 0.5,
noise: perlin
});params.octavesnumber of octavesparams.persistencepersistence (amplitude)params.noiseinput noise function to fractionally combine
All interpolation methods implement following interface:
value = interpolate(a, b, t)- noise.interpolation.nearestNeighbour
- noise.interpolation.linear
- noise.interpolation.cosine
Create an array with items between a and b
noise.array.range(3, 5) == [3, 4, 5]Shuffle an array
noise.array.shuffle([3, 4, 5], Math.random)arraythe array to be shuffledrandoma random function that generates numbers between [0, 1)