-
Notifications
You must be signed in to change notification settings - Fork 12
/
q5-math.js
423 lines (377 loc) · 8.89 KB
/
q5-math.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
Q5.modules.math = ($, q) => {
$.RADIANS = 0;
$.DEGREES = 1;
$.PI = Math.PI;
$.HALF_PI = Math.PI / 2;
$.QUARTER_PI = Math.PI / 4;
$.abs = Math.abs;
$.ceil = Math.ceil;
$.exp = Math.exp;
$.floor = $.int = Math.floor;
$.loge = Math.log;
$.mag = Math.hypot;
$.max = Math.max;
$.min = Math.min;
$.round = Math.round;
$.pow = Math.pow;
$.sqrt = Math.sqrt;
$.SHR3 = 1;
$.LCG = 2;
let angleMode = ($._angleMode = 0);
$.angleMode = (mode) => {
angleMode = $._angleMode = mode == 0 || mode == 'radians' ? 0 : 1;
return !angleMode ? 'radians' : 'degrees';
};
let DEGTORAD = ($._DEGTORAD = Math.PI / 180);
let RADTODEG = ($._RADTODEG = 180 / Math.PI);
$.degrees = (x) => x * $._RADTODEG;
$.radians = (x) => x * $._DEGTORAD;
$.map = Q5.prototype.map = (value, istart, istop, ostart, ostop, clamp) => {
let val = ostart + (ostop - ostart) * (((value - istart) * 1.0) / (istop - istart));
if (!clamp) {
return val;
}
if (ostart < ostop) {
return Math.min(Math.max(val, ostart), ostop);
} else {
return Math.min(Math.max(val, ostop), ostart);
}
};
$.dist = function () {
let a = arguments;
if (a.length == 2) return Math.hypot(a[0].x - a[1].x, a[0].y - a[1].y);
if (a.length == 4) return Math.hypot(a[0] - a[2], a[1] - a[3]);
return Math.hypot(a[0] - a[3], a[1] - a[4], a[2] - a[5]);
};
$.lerp = (a, b, t) => a * (1 - t) + b * t;
$.constrain = (x, lo, hi) => Math.min(Math.max(x, lo), hi);
$.norm = (value, start, stop) => $.map(value, start, stop, 0, 1);
$.sq = (x) => x * x;
$.fract = (x) => x - Math.floor(x);
$.sin = (a) => Math.sin(!angleMode ? a : a * DEGTORAD);
$.cos = (a) => Math.cos(!angleMode ? a : a * DEGTORAD);
$.tan = (a) => Math.tan(!angleMode ? a : a * DEGTORAD);
$.asin = (x) => {
let a = Math.asin(x);
return !angleMode ? a : a * RADTODEG;
};
$.acos = (x) => {
let a = Math.acos(x);
return !angleMode ? a : a * RADTODEG;
};
$.atan = (x) => {
let a = Math.atan(x);
return !angleMode ? a : a * RADTODEG;
};
$.atan2 = (y, x) => {
let a = Math.atan2(y, x);
return !angleMode ? a : a * RADTODEG;
};
function lcg() {
const m = 4294967296;
const a = 1664525;
const c = 1013904223;
let seed, z;
return {
setSeed(val) {
z = seed = (val == null ? Math.random() * m : val) >>> 0;
},
getSeed() {
return seed;
},
rand() {
z = (a * z + c) % m;
return z / m;
}
};
}
function shr3() {
let jsr, seed;
let m = 4294967295;
return {
setSeed(val) {
jsr = seed = (val == null ? Math.random() * m : val) >>> 0;
},
getSeed() {
return seed;
},
rand() {
jsr ^= jsr << 17;
jsr ^= jsr >> 13;
jsr ^= jsr << 5;
return (jsr >>> 0) / m;
}
};
}
let rng1 = shr3();
rng1.setSeed();
$.randomSeed = (seed) => rng1.setSeed(seed);
$.random = (a, b) => {
if (a === undefined) return rng1.rand();
if (typeof a == 'number') {
if (b !== undefined) {
return rng1.rand() * (b - a) + a;
} else {
return rng1.rand() * a;
}
} else {
return a[Math.trunc(a.length * rng1.rand())];
}
};
$.randomGenerator = (method) => {
if (method == $.LCG) rng1 = lcg();
else if (method == $.SHR3) rng1 = shr3();
rng1.setSeed();
};
var ziggurat = new (function () {
var iz;
var jz;
var kn = new Array(128);
var ke = new Array(256);
var hz;
var wn = new Array(128);
var fn = new Array(128);
var we = new Array(256);
var fe = new Array(256);
var SHR3 = () => {
return rng1.rand() * 4294967296 - 2147483648;
};
var UNI = () => {
return 0.5 + (SHR3() << 0) * 0.2328306e-9;
};
var RNOR = () => {
hz = SHR3();
iz = hz & 127;
return Math.abs(hz) < kn[iz] ? hz * wn[iz] : nfix();
};
var REXP = () => {
jz = SHR3() >>> 0;
iz = jz & 255;
return jz < kn[iz] ? jz * we[iz] : efix();
};
var nfix = () => {
var r = 3.44262;
var x, y;
var u1, u2;
for (;;) {
x = hz * wn[iz];
if (iz == 0) {
do {
u1 = UNI();
u2 = UNI();
x = -Math.log(u1) * 0.2904764;
y = -Math.log(u2);
} while (y + y < x * x);
return hz > 0 ? r + x : -r - x;
}
if (fn[iz] + UNI() * (fn[iz - 1] - fn[iz]) < Math.exp(-0.5 * x * x)) {
return x;
}
hz = SHR3();
iz = hz & 127;
if (Math.abs(hz) < kn[iz]) {
return hz * wn[iz];
}
}
};
var efix = () => {
var x;
for (;;) {
if (iz == 0) {
return 7.69711 - Math.log(UNI());
}
x = jz * we[iz];
if (fe[iz] + UNI() * (fe[iz - 1] - fe[iz]) < Math.exp(-x)) {
return x;
}
jz = SHR3();
iz = jz & 255;
if (jz < ke[iz]) {
return jz * we[iz];
}
}
};
var zigset = () => {
var m1 = 2147483648;
var m2 = 4294967296;
var dn = 3.442619855899;
var tn = dn;
var vn = 9.91256303526217e-3;
var q;
var de = 7.697117470131487;
var te = de;
var ve = 3.949659822581572e-3;
var i;
/* Tables for RNOR */
q = vn / Math.exp(-0.5 * dn * dn);
kn[0] = Math.floor((dn / q) * m1);
kn[1] = 0;
wn[0] = q / m1;
wn[127] = dn / m1;
fn[0] = 1;
fn[127] = Math.exp(-0.5 * dn * dn);
for (i = 126; i >= 1; i--) {
dn = Math.sqrt(-2 * Math.log(vn / dn + Math.exp(-0.5 * dn * dn)));
kn[i + 1] = Math.floor((dn / tn) * m1);
tn = dn;
fn[i] = Math.exp(-0.5 * dn * dn);
wn[i] = dn / m1;
}
/*Tables for REXP */
q = ve / Math.exp(-de);
ke[0] = Math.floor((de / q) * m2);
ke[1] = 0;
we[0] = q / m2;
we[255] = de / m2;
fe[0] = 1;
fe[255] = Math.exp(-de);
for (i = 254; i >= 1; i--) {
de = -Math.log(ve / de + Math.exp(-de));
ke[i + 1] = Math.floor((de / te) * m2);
te = de;
fe[i] = Math.exp(-de);
we[i] = de / m2;
}
};
this.SHR3 = SHR3;
this.UNI = UNI;
this.RNOR = RNOR;
this.REXP = REXP;
this.zigset = zigset;
})();
ziggurat.hasInit = false;
$.randomGaussian = (mean, std) => {
if (!ziggurat.hasInit) {
ziggurat.zigset();
ziggurat.hasInit = true;
}
return ziggurat.RNOR() * std + mean;
};
$.randomExponential = () => {
if (!ziggurat.hasInit) {
ziggurat.zigset();
ziggurat.hasInit = true;
}
return ziggurat.REXP();
};
$.PERLIN = 'perlin';
$.SIMPLEX = 'simplex';
$.BLOCKY = 'blocky';
$.Noise = Q5.PerlinNoise;
let _noise;
$.noiseMode = (mode) => {
q.Noise = Q5[mode[0].toUpperCase() + mode.slice(1) + 'Noise'];
_noise = null;
};
$.noiseSeed = (seed) => {
_noise = new $.Noise(seed);
};
$.noise = (x = 0, y = 0, z = 0) => {
_noise ??= new $.Noise();
return _noise.noise(x, y, z);
};
$.noiseDetail = (lod, falloff) => {
_noise ??= new $.Noise();
if (lod > 0) _noise.octaves = lod;
if (falloff > 0) _noise.falloff = falloff;
};
};
Q5.Noise = class {};
Q5.PerlinNoise = class extends Q5.Noise {
constructor(seed) {
super();
this.grad3 = [
[1, 1, 0],
[-1, 1, 0],
[1, -1, 0],
[-1, -1, 0],
[1, 0, 1],
[-1, 0, 1],
[1, 0, -1],
[-1, 0, -1],
[0, 1, 1],
[0, -1, 1],
[0, 1, -1],
[0, -1, -1]
];
this.octaves = 1;
this.falloff = 0.5;
if (seed == undefined) {
this.p = Array.from({ length: 256 }, () => Math.floor(Math.random() * 256));
} else {
this.p = this.seedPermutation(seed);
}
this.p = this.p.concat(this.p);
}
seedPermutation(seed) {
let p = [];
for (let i = 0; i < 256; i++) {
p[i] = i;
}
let n, q;
for (let i = 255; i > 0; i--) {
seed = (seed * 16807) % 2147483647;
n = seed % (i + 1);
q = p[i];
p[i] = p[n];
p[n] = q;
}
return p;
}
dot(g, x, y, z) {
return g[0] * x + g[1] * y + g[2] * z;
}
mix(a, b, t) {
return (1 - t) * a + t * b;
}
fade(t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
noise(x, y, z) {
let t = this;
let total = 0;
let freq = 1;
let amp = 1;
let maxAmp = 0;
for (let i = 0; i < t.octaves; i++) {
const X = Math.floor(x * freq) & 255;
const Y = Math.floor(y * freq) & 255;
const Z = Math.floor(z * freq) & 255;
const xf = x * freq - Math.floor(x * freq);
const yf = y * freq - Math.floor(y * freq);
const zf = z * freq - Math.floor(z * freq);
const u = t.fade(xf);
const v = t.fade(yf);
const w = t.fade(zf);
const A = t.p[X] + Y;
const AA = t.p[A] + Z;
const AB = t.p[A + 1] + Z;
const B = t.p[X + 1] + Y;
const BA = t.p[B] + Z;
const BB = t.p[B + 1] + Z;
const lerp1 = t.mix(t.dot(t.grad3[t.p[AA] % 12], xf, yf, zf), t.dot(t.grad3[t.p[BA] % 12], xf - 1, yf, zf), u);
const lerp2 = t.mix(
t.dot(t.grad3[t.p[AB] % 12], xf, yf - 1, zf),
t.dot(t.grad3[t.p[BB] % 12], xf - 1, yf - 1, zf),
u
);
const lerp3 = t.mix(
t.dot(t.grad3[t.p[AA + 1] % 12], xf, yf, zf - 1),
t.dot(t.grad3[t.p[BA + 1] % 12], xf - 1, yf, zf - 1),
u
);
const lerp4 = t.mix(
t.dot(t.grad3[t.p[AB + 1] % 12], xf, yf - 1, zf - 1),
t.dot(t.grad3[t.p[BB + 1] % 12], xf - 1, yf - 1, zf - 1),
u
);
const mix1 = t.mix(lerp1, lerp2, v);
const mix2 = t.mix(lerp3, lerp4, v);
total += t.mix(mix1, mix2, w) * amp;
maxAmp += amp;
amp *= t.falloff;
freq *= 2;
}
return (total / maxAmp + 1) / 2;
}
};