jwagner / jwagner/simplex-noise.js

`noise()` generic function

Open
#42 4 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
1.8k
Forks
127
PR merge metrics
No merged PRs in 30d

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

```js
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.