-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (55 loc) · 1.55 KB
/
index.js
File metadata and controls
71 lines (55 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { hasher, prng } = require("./externals");
class Noise {
// https://en.wikipedia.org/wiki/Perlin_noise
// https://joeiddon.github.io/projects/javascript/perlin.html
constructor(seed) {
this.gradients = {};
if (seed) {
let s = hasher(seed.toString());
this.rand = prng(s[0], s[1], s[2], s[3]);
} else {
this.rand = Math.random;
}
}
#randomGradient() {
let theta = this.rand() * 2 * Math.PI;
return { x: Math.cos(theta), y: Math.sin(theta) };
}
#interpolate(a0, a1, w) {
if (0.0 > w) return a0;
if (1.0 < w) return a1;
return (a1 - a0) * ((w * (w * 6.0 - 15.0) + 10.0) * w * w * w) + a0;
}
#dotGridGradient(ix, iy, x, y) {
let gradient = this.#randomGradient()
let dx = x - ix;
let dy = y - iy;
if (this.gradients[[ix, iy]]) {
gradient = this.gradients[[ix, iy]];
} else {
this.gradients[[ix, iy]] = gradient;
}
return dx * gradient.x + dy * gradient.y;
}
perlin(x, y) {
let x0 = Math.floor(x);
let x1 = x0 + 1;
let y0 = Math.floor(y);
let y1 = y0 + 1;
let sx = x - x0;
let sy = y - y0;
let n0, n1, ix0, ix1, value;
n0 = this.#dotGridGradient(x0, y0, x, y);
n1 = this.#dotGridGradient(x1, y0, x, y);
ix0 = this.#interpolate(n0, n1, sx);
n0 = this.#dotGridGradient(x0, y1, x, y);
n1 = this.#dotGridGradient(x1, y1, x, y);
ix1 = this.#interpolate(n0, n1, sx);
value = this.#interpolate(ix0, ix1, sy);
return value * 0.5 + 0.5;
}
static() {
return this.rand();
}
}
module.exports = Noise;