Open
Description
what about a "generic" noise
function, with variable-length arguments, ie:
generic | implied | comment |
---|---|---|
noise() |
noise2D(0, 0) |
0 by default |
noise(1) |
noise2D(1,1) |
same value for 2nd arg |
noise(1,2) |
noise2D(1,2) |
|
noise(1,2,3) |
noise3D(1,2,3) |
|
noise(1,2,3,4) |
noise4D(1,2,3,4) |
|
noise(1,2,3,4,5) |
noise4D(1,2,3,4,5) |
NB: when more than 4 args => 5th, 6th... will just be ignored by noise4D |
This is inspired from https://processing.org/reference/noise_.html
function noise(...args) {
let dim // 2 or 3 or 4
let args2 = [...args] // copy of args
if (args.length < 2) {
// 0 or 1 arg => noise2d
const arg = args[0] || 0
args2 = [arg, arg]
dim = 2
} else {
// 2 or 3 or 4 or more args => noise2D or noise3D or noise4D
dim = Math.min(args.length, 4)
}
return simplex[`noise${dim}D`](...args2) /2 + 0.5 // normalize [0;1]
}
NB: I've chosen to normalise the value to ]0;1[
but maybe you prefer staying ]-1;1[
working demo: https://codepen.io/abernier/pen/ExvqNyj