-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperlin.js
More file actions
58 lines (52 loc) · 1.15 KB
/
Copy pathperlin.js
File metadata and controls
58 lines (52 loc) · 1.15 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
let M = 4294967296,
// a - 1 should be divisible by m's prime factors
A = 1664525,
// c and m should be co-prime
C = 1;
/**
*
* The PRNG's seed
*
* */
let Z = Math.floor(Math.random() * M);
/**
*
* Linear congruential pseudorandom number generator (so we can repeat worlds via seeds)
*/
function rand() {
Z = (A * Z + C) % M;
return Z / M;
}
/**
*
* Interpolates smoothly between two values (pa and pb) based on how far you
* are between them (represented by px)
*
*/
function interpolate(pa, pb, px) {
let ft = px * Math.PI,
f = (1 - Math.cos(ft)) * 0.5;
return pa * (1 - f) + pb * f;
}
/**
*
* Generate n samplings of 1-dimensional perlin noise with the given amplitude
* and wavelength for use with terrain generation
*
* */
export function perlin1d(n, amp, wl) {
let fq = 1 / wl,
a = rand(),
b = rand();
let output = Array(n);
for (let x = 0; x < n; x++) {
if (x % wl === 0) {
a = b;
b = rand();
output[x] = a * amp;
} else {
output[x] = interpolate(a, b, (x % wl) / wl) * amp;
}
}
return output;
}