q5 2.14.4 → 2.14.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/q5.js +4 -11
- package/q5.min.js +1 -1
- package/q5js_brand.webp +0 -0
- package/q5js_icon.png +0 -0
- package/src/q5-2d-canvas.js +0 -202
- package/src/q5-2d-drawing.js +0 -399
- package/src/q5-2d-image.js +0 -330
- package/src/q5-2d-soft-filters.js +0 -145
- package/src/q5-2d-text.js +0 -279
- package/src/q5-ai.js +0 -65
- package/src/q5-canvas.js +0 -367
- package/src/q5-color.js +0 -322
- package/src/q5-core.js +0 -319
- package/src/q5-display.js +0 -101
- package/src/q5-dom.js +0 -2
- package/src/q5-input.js +0 -215
- package/src/q5-math.js +0 -424
- package/src/q5-noisier.js +0 -264
- package/src/q5-record.js +0 -366
- package/src/q5-sensors.js +0 -98
- package/src/q5-sound.js +0 -64
- package/src/q5-util.js +0 -50
- package/src/q5-vector.js +0 -305
- package/src/q5-webgpu-canvas.js +0 -565
- package/src/q5-webgpu-drawing.js +0 -638
- package/src/q5-webgpu-image.js +0 -268
- package/src/q5-webgpu-text.js +0 -594
- package/src/readme.md +0 -248
package/src/q5-math.js
DELETED
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
Q5.modules.math = ($, q) => {
|
|
2
|
-
$.RADIANS = 0;
|
|
3
|
-
$.DEGREES = 1;
|
|
4
|
-
|
|
5
|
-
$.PI = Math.PI;
|
|
6
|
-
$.HALF_PI = Math.PI / 2;
|
|
7
|
-
$.QUARTER_PI = Math.PI / 4;
|
|
8
|
-
$.TWO_PI = $.TAU = Math.PI * 2;
|
|
9
|
-
|
|
10
|
-
$.abs = Math.abs;
|
|
11
|
-
$.ceil = Math.ceil;
|
|
12
|
-
$.exp = Math.exp;
|
|
13
|
-
$.floor = $.int = Math.floor;
|
|
14
|
-
$.loge = Math.log;
|
|
15
|
-
$.mag = Math.hypot;
|
|
16
|
-
$.max = Math.max;
|
|
17
|
-
$.min = Math.min;
|
|
18
|
-
$.round = Math.round;
|
|
19
|
-
$.pow = Math.pow;
|
|
20
|
-
$.sqrt = Math.sqrt;
|
|
21
|
-
|
|
22
|
-
$.SHR3 = 1;
|
|
23
|
-
$.LCG = 2;
|
|
24
|
-
|
|
25
|
-
let angleMode = ($._angleMode = 0);
|
|
26
|
-
|
|
27
|
-
$.angleMode = (mode) => {
|
|
28
|
-
angleMode = $._angleMode = mode == 0 || mode == 'radians' ? 0 : 1;
|
|
29
|
-
return !angleMode ? 'radians' : 'degrees';
|
|
30
|
-
};
|
|
31
|
-
let DEGTORAD = ($._DEGTORAD = Math.PI / 180);
|
|
32
|
-
let RADTODEG = ($._RADTODEG = 180 / Math.PI);
|
|
33
|
-
$.degrees = (x) => x * $._RADTODEG;
|
|
34
|
-
$.radians = (x) => x * $._DEGTORAD;
|
|
35
|
-
|
|
36
|
-
$.map = Q5.prototype.map = (value, istart, istop, ostart, ostop, clamp) => {
|
|
37
|
-
let val = ostart + (ostop - ostart) * (((value - istart) * 1.0) / (istop - istart));
|
|
38
|
-
if (!clamp) {
|
|
39
|
-
return val;
|
|
40
|
-
}
|
|
41
|
-
if (ostart < ostop) {
|
|
42
|
-
return Math.min(Math.max(val, ostart), ostop);
|
|
43
|
-
} else {
|
|
44
|
-
return Math.min(Math.max(val, ostop), ostart);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
$.dist = function () {
|
|
49
|
-
let a = arguments;
|
|
50
|
-
if (a.length == 2) return Math.hypot(a[0].x - a[1].x, a[0].y - a[1].y);
|
|
51
|
-
if (a.length == 4) return Math.hypot(a[0] - a[2], a[1] - a[3]);
|
|
52
|
-
return Math.hypot(a[0] - a[3], a[1] - a[4], a[2] - a[5]);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
$.lerp = (a, b, t) => a * (1 - t) + b * t;
|
|
56
|
-
$.constrain = (x, lo, hi) => Math.min(Math.max(x, lo), hi);
|
|
57
|
-
$.norm = (value, start, stop) => $.map(value, start, stop, 0, 1);
|
|
58
|
-
$.sq = (x) => x * x;
|
|
59
|
-
$.fract = (x) => x - Math.floor(x);
|
|
60
|
-
|
|
61
|
-
$.sin = (a) => Math.sin(!angleMode ? a : a * DEGTORAD);
|
|
62
|
-
$.cos = (a) => Math.cos(!angleMode ? a : a * DEGTORAD);
|
|
63
|
-
$.tan = (a) => Math.tan(!angleMode ? a : a * DEGTORAD);
|
|
64
|
-
|
|
65
|
-
$.asin = (x) => {
|
|
66
|
-
let a = Math.asin(x);
|
|
67
|
-
return !angleMode ? a : a * RADTODEG;
|
|
68
|
-
};
|
|
69
|
-
$.acos = (x) => {
|
|
70
|
-
let a = Math.acos(x);
|
|
71
|
-
return !angleMode ? a : a * RADTODEG;
|
|
72
|
-
};
|
|
73
|
-
$.atan = (x) => {
|
|
74
|
-
let a = Math.atan(x);
|
|
75
|
-
return !angleMode ? a : a * RADTODEG;
|
|
76
|
-
};
|
|
77
|
-
$.atan2 = (y, x) => {
|
|
78
|
-
let a = Math.atan2(y, x);
|
|
79
|
-
return !angleMode ? a : a * RADTODEG;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
function lcg() {
|
|
83
|
-
const m = 4294967296;
|
|
84
|
-
const a = 1664525;
|
|
85
|
-
const c = 1013904223;
|
|
86
|
-
let seed, z;
|
|
87
|
-
return {
|
|
88
|
-
setSeed(val) {
|
|
89
|
-
z = seed = (val == null ? Math.random() * m : val) >>> 0;
|
|
90
|
-
},
|
|
91
|
-
getSeed() {
|
|
92
|
-
return seed;
|
|
93
|
-
},
|
|
94
|
-
rand() {
|
|
95
|
-
z = (a * z + c) % m;
|
|
96
|
-
return z / m;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function shr3() {
|
|
102
|
-
let jsr, seed;
|
|
103
|
-
let m = 4294967295;
|
|
104
|
-
return {
|
|
105
|
-
setSeed(val) {
|
|
106
|
-
jsr = seed = (val == null ? Math.random() * m : val) >>> 0;
|
|
107
|
-
},
|
|
108
|
-
getSeed() {
|
|
109
|
-
return seed;
|
|
110
|
-
},
|
|
111
|
-
rand() {
|
|
112
|
-
jsr ^= jsr << 17;
|
|
113
|
-
jsr ^= jsr >> 13;
|
|
114
|
-
jsr ^= jsr << 5;
|
|
115
|
-
return (jsr >>> 0) / m;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
let rng1 = shr3();
|
|
121
|
-
rng1.setSeed();
|
|
122
|
-
|
|
123
|
-
$.randomSeed = (seed) => rng1.setSeed(seed);
|
|
124
|
-
$.random = (a, b) => {
|
|
125
|
-
if (a === undefined) return rng1.rand();
|
|
126
|
-
if (typeof a == 'number') {
|
|
127
|
-
if (b !== undefined) {
|
|
128
|
-
return rng1.rand() * (b - a) + a;
|
|
129
|
-
} else {
|
|
130
|
-
return rng1.rand() * a;
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
return a[Math.trunc(a.length * rng1.rand())];
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
$.randomGenerator = (method) => {
|
|
138
|
-
if (method == $.LCG) rng1 = lcg();
|
|
139
|
-
else if (method == $.SHR3) rng1 = shr3();
|
|
140
|
-
rng1.setSeed();
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
var ziggurat = new (function () {
|
|
144
|
-
var iz;
|
|
145
|
-
var jz;
|
|
146
|
-
var kn = new Array(128);
|
|
147
|
-
var ke = new Array(256);
|
|
148
|
-
var hz;
|
|
149
|
-
var wn = new Array(128);
|
|
150
|
-
var fn = new Array(128);
|
|
151
|
-
var we = new Array(256);
|
|
152
|
-
var fe = new Array(256);
|
|
153
|
-
var SHR3 = () => {
|
|
154
|
-
return rng1.rand() * 4294967296 - 2147483648;
|
|
155
|
-
};
|
|
156
|
-
var UNI = () => {
|
|
157
|
-
return 0.5 + (SHR3() << 0) * 0.2328306e-9;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
var RNOR = () => {
|
|
161
|
-
hz = SHR3();
|
|
162
|
-
iz = hz & 127;
|
|
163
|
-
return Math.abs(hz) < kn[iz] ? hz * wn[iz] : nfix();
|
|
164
|
-
};
|
|
165
|
-
var REXP = () => {
|
|
166
|
-
jz = SHR3() >>> 0;
|
|
167
|
-
iz = jz & 255;
|
|
168
|
-
return jz < kn[iz] ? jz * we[iz] : efix();
|
|
169
|
-
};
|
|
170
|
-
var nfix = () => {
|
|
171
|
-
var r = 3.44262;
|
|
172
|
-
var x, y;
|
|
173
|
-
var u1, u2;
|
|
174
|
-
for (;;) {
|
|
175
|
-
x = hz * wn[iz];
|
|
176
|
-
if (iz == 0) {
|
|
177
|
-
do {
|
|
178
|
-
u1 = UNI();
|
|
179
|
-
u2 = UNI();
|
|
180
|
-
x = -Math.log(u1) * 0.2904764;
|
|
181
|
-
y = -Math.log(u2);
|
|
182
|
-
} while (y + y < x * x);
|
|
183
|
-
return hz > 0 ? r + x : -r - x;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (fn[iz] + UNI() * (fn[iz - 1] - fn[iz]) < Math.exp(-0.5 * x * x)) {
|
|
187
|
-
return x;
|
|
188
|
-
}
|
|
189
|
-
hz = SHR3();
|
|
190
|
-
iz = hz & 127;
|
|
191
|
-
if (Math.abs(hz) < kn[iz]) {
|
|
192
|
-
return hz * wn[iz];
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
var efix = () => {
|
|
197
|
-
var x;
|
|
198
|
-
for (;;) {
|
|
199
|
-
if (iz == 0) {
|
|
200
|
-
return 7.69711 - Math.log(UNI());
|
|
201
|
-
}
|
|
202
|
-
x = jz * we[iz];
|
|
203
|
-
if (fe[iz] + UNI() * (fe[iz - 1] - fe[iz]) < Math.exp(-x)) {
|
|
204
|
-
return x;
|
|
205
|
-
}
|
|
206
|
-
jz = SHR3();
|
|
207
|
-
iz = jz & 255;
|
|
208
|
-
if (jz < ke[iz]) {
|
|
209
|
-
return jz * we[iz];
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
var zigset = () => {
|
|
215
|
-
var m1 = 2147483648;
|
|
216
|
-
var m2 = 4294967296;
|
|
217
|
-
var dn = 3.442619855899;
|
|
218
|
-
var tn = dn;
|
|
219
|
-
var vn = 9.91256303526217e-3;
|
|
220
|
-
var q;
|
|
221
|
-
var de = 7.697117470131487;
|
|
222
|
-
var te = de;
|
|
223
|
-
var ve = 3.949659822581572e-3;
|
|
224
|
-
var i;
|
|
225
|
-
|
|
226
|
-
/* Tables for RNOR */
|
|
227
|
-
q = vn / Math.exp(-0.5 * dn * dn);
|
|
228
|
-
kn[0] = Math.floor((dn / q) * m1);
|
|
229
|
-
kn[1] = 0;
|
|
230
|
-
wn[0] = q / m1;
|
|
231
|
-
wn[127] = dn / m1;
|
|
232
|
-
fn[0] = 1;
|
|
233
|
-
fn[127] = Math.exp(-0.5 * dn * dn);
|
|
234
|
-
for (i = 126; i >= 1; i--) {
|
|
235
|
-
dn = Math.sqrt(-2 * Math.log(vn / dn + Math.exp(-0.5 * dn * dn)));
|
|
236
|
-
kn[i + 1] = Math.floor((dn / tn) * m1);
|
|
237
|
-
tn = dn;
|
|
238
|
-
fn[i] = Math.exp(-0.5 * dn * dn);
|
|
239
|
-
wn[i] = dn / m1;
|
|
240
|
-
}
|
|
241
|
-
/*Tables for REXP */
|
|
242
|
-
q = ve / Math.exp(-de);
|
|
243
|
-
ke[0] = Math.floor((de / q) * m2);
|
|
244
|
-
ke[1] = 0;
|
|
245
|
-
we[0] = q / m2;
|
|
246
|
-
we[255] = de / m2;
|
|
247
|
-
fe[0] = 1;
|
|
248
|
-
fe[255] = Math.exp(-de);
|
|
249
|
-
for (i = 254; i >= 1; i--) {
|
|
250
|
-
de = -Math.log(ve / de + Math.exp(-de));
|
|
251
|
-
ke[i + 1] = Math.floor((de / te) * m2);
|
|
252
|
-
te = de;
|
|
253
|
-
fe[i] = Math.exp(-de);
|
|
254
|
-
we[i] = de / m2;
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
this.SHR3 = SHR3;
|
|
258
|
-
this.UNI = UNI;
|
|
259
|
-
this.RNOR = RNOR;
|
|
260
|
-
this.REXP = REXP;
|
|
261
|
-
this.zigset = zigset;
|
|
262
|
-
})();
|
|
263
|
-
ziggurat.hasInit = false;
|
|
264
|
-
|
|
265
|
-
$.randomGaussian = (mean, std) => {
|
|
266
|
-
if (!ziggurat.hasInit) {
|
|
267
|
-
ziggurat.zigset();
|
|
268
|
-
ziggurat.hasInit = true;
|
|
269
|
-
}
|
|
270
|
-
return ziggurat.RNOR() * std + mean;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
$.randomExponential = () => {
|
|
274
|
-
if (!ziggurat.hasInit) {
|
|
275
|
-
ziggurat.zigset();
|
|
276
|
-
ziggurat.hasInit = true;
|
|
277
|
-
}
|
|
278
|
-
return ziggurat.REXP();
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
$.PERLIN = 'perlin';
|
|
282
|
-
$.SIMPLEX = 'simplex';
|
|
283
|
-
$.BLOCKY = 'blocky';
|
|
284
|
-
|
|
285
|
-
$.Noise = Q5.PerlinNoise;
|
|
286
|
-
let _noise;
|
|
287
|
-
|
|
288
|
-
$.noiseMode = (mode) => {
|
|
289
|
-
q.Noise = Q5[mode[0].toUpperCase() + mode.slice(1) + 'Noise'];
|
|
290
|
-
_noise = null;
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
$.noiseSeed = (seed) => {
|
|
294
|
-
_noise = new $.Noise(seed);
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
$.noise = (x = 0, y = 0, z = 0) => {
|
|
298
|
-
_noise ??= new $.Noise();
|
|
299
|
-
return _noise.noise(x, y, z);
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
$.noiseDetail = (lod, falloff) => {
|
|
303
|
-
_noise ??= new $.Noise();
|
|
304
|
-
if (lod > 0) _noise.octaves = lod;
|
|
305
|
-
if (falloff > 0) _noise.falloff = falloff;
|
|
306
|
-
};
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
Q5.Noise = class {};
|
|
310
|
-
|
|
311
|
-
Q5.PerlinNoise = class extends Q5.Noise {
|
|
312
|
-
constructor(seed) {
|
|
313
|
-
super();
|
|
314
|
-
this.grad3 = [
|
|
315
|
-
[1, 1, 0],
|
|
316
|
-
[-1, 1, 0],
|
|
317
|
-
[1, -1, 0],
|
|
318
|
-
[-1, -1, 0],
|
|
319
|
-
[1, 0, 1],
|
|
320
|
-
[-1, 0, 1],
|
|
321
|
-
[1, 0, -1],
|
|
322
|
-
[-1, 0, -1],
|
|
323
|
-
[0, 1, 1],
|
|
324
|
-
[0, -1, 1],
|
|
325
|
-
[0, 1, -1],
|
|
326
|
-
[0, -1, -1]
|
|
327
|
-
];
|
|
328
|
-
this.octaves = 1;
|
|
329
|
-
this.falloff = 0.5;
|
|
330
|
-
if (seed == undefined) {
|
|
331
|
-
this.p = Array.from({ length: 256 }, () => Math.floor(Math.random() * 256));
|
|
332
|
-
} else {
|
|
333
|
-
this.p = this.seedPermutation(seed);
|
|
334
|
-
}
|
|
335
|
-
this.p = this.p.concat(this.p);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
seedPermutation(seed) {
|
|
339
|
-
let p = [];
|
|
340
|
-
for (let i = 0; i < 256; i++) {
|
|
341
|
-
p[i] = i;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
let n, q;
|
|
345
|
-
for (let i = 255; i > 0; i--) {
|
|
346
|
-
seed = (seed * 16807) % 2147483647;
|
|
347
|
-
n = seed % (i + 1);
|
|
348
|
-
q = p[i];
|
|
349
|
-
p[i] = p[n];
|
|
350
|
-
p[n] = q;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return p;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
dot(g, x, y, z) {
|
|
357
|
-
return g[0] * x + g[1] * y + g[2] * z;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
mix(a, b, t) {
|
|
361
|
-
return (1 - t) * a + t * b;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
fade(t) {
|
|
365
|
-
return t * t * t * (t * (t * 6 - 15) + 10);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
noise(x, y, z) {
|
|
369
|
-
let t = this;
|
|
370
|
-
let total = 0;
|
|
371
|
-
let freq = 1;
|
|
372
|
-
let amp = 1;
|
|
373
|
-
let maxAmp = 0;
|
|
374
|
-
|
|
375
|
-
for (let i = 0; i < t.octaves; i++) {
|
|
376
|
-
const X = Math.floor(x * freq) & 255;
|
|
377
|
-
const Y = Math.floor(y * freq) & 255;
|
|
378
|
-
const Z = Math.floor(z * freq) & 255;
|
|
379
|
-
|
|
380
|
-
const xf = x * freq - Math.floor(x * freq);
|
|
381
|
-
const yf = y * freq - Math.floor(y * freq);
|
|
382
|
-
const zf = z * freq - Math.floor(z * freq);
|
|
383
|
-
|
|
384
|
-
const u = t.fade(xf);
|
|
385
|
-
const v = t.fade(yf);
|
|
386
|
-
const w = t.fade(zf);
|
|
387
|
-
|
|
388
|
-
const A = t.p[X] + Y;
|
|
389
|
-
const AA = t.p[A] + Z;
|
|
390
|
-
const AB = t.p[A + 1] + Z;
|
|
391
|
-
const B = t.p[X + 1] + Y;
|
|
392
|
-
const BA = t.p[B] + Z;
|
|
393
|
-
const BB = t.p[B + 1] + Z;
|
|
394
|
-
|
|
395
|
-
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);
|
|
396
|
-
const lerp2 = t.mix(
|
|
397
|
-
t.dot(t.grad3[t.p[AB] % 12], xf, yf - 1, zf),
|
|
398
|
-
t.dot(t.grad3[t.p[BB] % 12], xf - 1, yf - 1, zf),
|
|
399
|
-
u
|
|
400
|
-
);
|
|
401
|
-
const lerp3 = t.mix(
|
|
402
|
-
t.dot(t.grad3[t.p[AA + 1] % 12], xf, yf, zf - 1),
|
|
403
|
-
t.dot(t.grad3[t.p[BA + 1] % 12], xf - 1, yf, zf - 1),
|
|
404
|
-
u
|
|
405
|
-
);
|
|
406
|
-
const lerp4 = t.mix(
|
|
407
|
-
t.dot(t.grad3[t.p[AB + 1] % 12], xf, yf - 1, zf - 1),
|
|
408
|
-
t.dot(t.grad3[t.p[BB + 1] % 12], xf - 1, yf - 1, zf - 1),
|
|
409
|
-
u
|
|
410
|
-
);
|
|
411
|
-
|
|
412
|
-
const mix1 = t.mix(lerp1, lerp2, v);
|
|
413
|
-
const mix2 = t.mix(lerp3, lerp4, v);
|
|
414
|
-
|
|
415
|
-
total += t.mix(mix1, mix2, w) * amp;
|
|
416
|
-
|
|
417
|
-
maxAmp += amp;
|
|
418
|
-
amp *= t.falloff;
|
|
419
|
-
freq *= 2;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return (total / maxAmp + 1) / 2;
|
|
423
|
-
}
|
|
424
|
-
};
|
package/src/q5-noisier.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
/* additional noise algorithms */
|
|
2
|
-
Q5.SimplexNoise = class extends Q5.Noise {
|
|
3
|
-
constructor(seed) {
|
|
4
|
-
super();
|
|
5
|
-
this.grad3 = [
|
|
6
|
-
[1, 1, 0],
|
|
7
|
-
[-1, 1, 0],
|
|
8
|
-
[1, -1, 0],
|
|
9
|
-
[-1, -1, 0],
|
|
10
|
-
[1, 0, 1],
|
|
11
|
-
[-1, 0, 1],
|
|
12
|
-
[1, 0, -1],
|
|
13
|
-
[-1, 0, -1],
|
|
14
|
-
[0, 1, 1],
|
|
15
|
-
[0, -1, 1],
|
|
16
|
-
[0, 1, -1],
|
|
17
|
-
[0, -1, -1]
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
this.octaves = 1;
|
|
21
|
-
this.falloff = 0.5;
|
|
22
|
-
|
|
23
|
-
if (seed === undefined) {
|
|
24
|
-
this.p = Array.from({ length: 256 }, () => Math.floor(Math.random() * 256));
|
|
25
|
-
} else {
|
|
26
|
-
this.p = this.seedPermutation(seed);
|
|
27
|
-
}
|
|
28
|
-
this.perm = Array.from({ length: 512 }, (_, i) => this.p[i & 255]);
|
|
29
|
-
|
|
30
|
-
this.F3 = 1.0 / 3.0;
|
|
31
|
-
this.G3 = 1.0 / 6.0;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
seedPermutation(seed) {
|
|
35
|
-
let p = [];
|
|
36
|
-
for (let i = 0; i < 256; i++) {
|
|
37
|
-
p[i] = i;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let n, q;
|
|
41
|
-
for (let i = 255; i > 0; i--) {
|
|
42
|
-
seed = (seed * 16807) % 2147483647;
|
|
43
|
-
n = seed % (i + 1);
|
|
44
|
-
q = p[i];
|
|
45
|
-
p[i] = p[n];
|
|
46
|
-
p[n] = q;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return p;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
dot(g, x, y, z) {
|
|
53
|
-
return g[0] * x + g[1] * y + g[2] * z;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
noise(xin, yin, zin) {
|
|
57
|
-
let total = 0;
|
|
58
|
-
let freq = 1;
|
|
59
|
-
let amp = 1;
|
|
60
|
-
let maxAmp = 0;
|
|
61
|
-
|
|
62
|
-
for (let i = 0; i < this.octaves; i++) {
|
|
63
|
-
let n0, n1, n2, n3;
|
|
64
|
-
let s = (xin * freq + yin * freq + zin * freq) * this.F3;
|
|
65
|
-
let i = Math.floor(xin * freq + s);
|
|
66
|
-
let j = Math.floor(yin * freq + s);
|
|
67
|
-
let k = Math.floor(zin * freq + s);
|
|
68
|
-
let t = (i + j + k) * this.G3;
|
|
69
|
-
let X0 = i - t;
|
|
70
|
-
let Y0 = j - t;
|
|
71
|
-
let Z0 = k - t;
|
|
72
|
-
let x0 = xin * freq - X0;
|
|
73
|
-
let y0 = yin * freq - Y0;
|
|
74
|
-
let z0 = zin * freq - Z0;
|
|
75
|
-
|
|
76
|
-
let i1, j1, k1;
|
|
77
|
-
let i2, j2, k2;
|
|
78
|
-
|
|
79
|
-
if (x0 >= y0) {
|
|
80
|
-
if (y0 >= z0) {
|
|
81
|
-
i1 = 1;
|
|
82
|
-
j1 = 0;
|
|
83
|
-
k1 = 0;
|
|
84
|
-
i2 = 1;
|
|
85
|
-
j2 = 1;
|
|
86
|
-
k2 = 0;
|
|
87
|
-
} else if (x0 >= z0) {
|
|
88
|
-
i1 = 1;
|
|
89
|
-
j1 = 0;
|
|
90
|
-
k1 = 0;
|
|
91
|
-
i2 = 1;
|
|
92
|
-
j2 = 0;
|
|
93
|
-
k2 = 1;
|
|
94
|
-
} else {
|
|
95
|
-
i1 = 0;
|
|
96
|
-
j1 = 0;
|
|
97
|
-
k1 = 1;
|
|
98
|
-
i2 = 1;
|
|
99
|
-
j2 = 0;
|
|
100
|
-
k2 = 1;
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
if (y0 < z0) {
|
|
104
|
-
i1 = 0;
|
|
105
|
-
j1 = 0;
|
|
106
|
-
k1 = 1;
|
|
107
|
-
i2 = 0;
|
|
108
|
-
j2 = 1;
|
|
109
|
-
k2 = 1;
|
|
110
|
-
} else if (x0 < z0) {
|
|
111
|
-
i1 = 0;
|
|
112
|
-
j1 = 1;
|
|
113
|
-
k1 = 0;
|
|
114
|
-
i2 = 0;
|
|
115
|
-
j2 = 1;
|
|
116
|
-
k2 = 1;
|
|
117
|
-
} else {
|
|
118
|
-
i1 = 0;
|
|
119
|
-
j1 = 1;
|
|
120
|
-
k1 = 0;
|
|
121
|
-
i2 = 1;
|
|
122
|
-
j2 = 1;
|
|
123
|
-
k2 = 0;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
let x1 = x0 - i1 + this.G3;
|
|
128
|
-
let y1 = y0 - j1 + this.G3;
|
|
129
|
-
let z1 = z0 - k1 + this.G3;
|
|
130
|
-
let x2 = x0 - i2 + 2.0 * this.G3;
|
|
131
|
-
let y2 = y0 - j2 + 2.0 * this.G3;
|
|
132
|
-
let z2 = z0 - k2 + 2.0 * this.G3;
|
|
133
|
-
let x3 = x0 - 1.0 + 3.0 * this.G3;
|
|
134
|
-
let y3 = y0 - 1.0 + 3.0 * this.G3;
|
|
135
|
-
let z3 = z0 - 1.0 + 3.0 * this.G3;
|
|
136
|
-
|
|
137
|
-
let ii = i & 255;
|
|
138
|
-
let jj = j & 255;
|
|
139
|
-
let kk = k & 255;
|
|
140
|
-
|
|
141
|
-
let gi0 = this.perm[ii + this.perm[jj + this.perm[kk]]] % 12;
|
|
142
|
-
let gi1 = this.perm[ii + i1 + this.perm[jj + j1 + this.perm[kk + k1]]] % 12;
|
|
143
|
-
let gi2 = this.perm[ii + i2 + this.perm[jj + j2 + this.perm[kk + k2]]] % 12;
|
|
144
|
-
let gi3 = this.perm[ii + 1 + this.perm[jj + 1 + this.perm[kk + 1]]] % 12;
|
|
145
|
-
|
|
146
|
-
let t0 = 0.5 - x0 * x0 - y0 * y0 - z0 * z0;
|
|
147
|
-
if (t0 < 0) n0 = 0.0;
|
|
148
|
-
else {
|
|
149
|
-
t0 *= t0;
|
|
150
|
-
n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0, z0);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let t1 = 0.5 - x1 * x1 - y1 * y1 - z1 * z1;
|
|
154
|
-
if (t1 < 0) n1 = 0.0;
|
|
155
|
-
else {
|
|
156
|
-
t1 *= t1;
|
|
157
|
-
n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1, z1);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
let t2 = 0.5 - x2 * x2 - y2 * y2 - z2 * z2;
|
|
161
|
-
if (t2 < 0) n2 = 0.0;
|
|
162
|
-
else {
|
|
163
|
-
t2 *= t2;
|
|
164
|
-
n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2, z2);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
let t3 = 0.5 - x3 * x3 - y3 * y3 - z3 * z3;
|
|
168
|
-
if (t3 < 0) n3 = 0.0;
|
|
169
|
-
else {
|
|
170
|
-
t3 *= t3;
|
|
171
|
-
n3 = t3 * t3 * this.dot(this.grad3[gi3], x3, y3, z3);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
total += 32.0 * (n0 + n1 + n2 + n3) * amp;
|
|
175
|
-
|
|
176
|
-
maxAmp += amp;
|
|
177
|
-
amp *= this.falloff;
|
|
178
|
-
freq *= 2;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return (total / maxAmp + 1) / 2;
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
Q5.BlockyNoise = class extends Q5.Noise {
|
|
186
|
-
constructor(seed) {
|
|
187
|
-
super();
|
|
188
|
-
this.YWRAPB = 4;
|
|
189
|
-
this.YWRAP = 1 << this.YWRAPB;
|
|
190
|
-
this.ZWRAPB = 8;
|
|
191
|
-
this.ZWRAP = 1 << this.ZWRAPB;
|
|
192
|
-
this.size = 4095;
|
|
193
|
-
this.octaves = 1;
|
|
194
|
-
this.falloff = 0.5;
|
|
195
|
-
|
|
196
|
-
seed ??= Math.random() * 4294967295;
|
|
197
|
-
this.p = new Array(this.size + 1);
|
|
198
|
-
for (var i = 0; i < this.size + 1; i++) {
|
|
199
|
-
seed ^= seed << 17;
|
|
200
|
-
seed ^= seed >> 13;
|
|
201
|
-
seed ^= seed << 5;
|
|
202
|
-
this.p[i] = (seed >>> 0) / 4294967295;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
scaled_cosine(i) {
|
|
207
|
-
return 0.5 * (1.0 - Math.cos(i * Math.PI));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
noise(x = 0, y = 0, z = 0) {
|
|
211
|
-
let t = this;
|
|
212
|
-
if (x < 0) x = -x;
|
|
213
|
-
if (y < 0) y = -y;
|
|
214
|
-
if (z < 0) z = -z;
|
|
215
|
-
var xi = Math.floor(x),
|
|
216
|
-
yi = Math.floor(y),
|
|
217
|
-
zi = Math.floor(z);
|
|
218
|
-
var xf = x - xi;
|
|
219
|
-
var yf = y - yi;
|
|
220
|
-
var zf = z - zi;
|
|
221
|
-
var rxf, ryf;
|
|
222
|
-
var r = 0;
|
|
223
|
-
var amp = 0.5;
|
|
224
|
-
var n1, n2, n3;
|
|
225
|
-
for (var o = 0; o < t.octaves; o++) {
|
|
226
|
-
var f = xi + (yi << t.YWRAPB) + (zi << t.ZWRAPB);
|
|
227
|
-
rxf = t.scaled_cosine(xf);
|
|
228
|
-
ryf = t.scaled_cosine(yf);
|
|
229
|
-
n1 = t.p[f & t.size];
|
|
230
|
-
n1 += rxf * (t.p[(f + 1) & t.size] - n1);
|
|
231
|
-
n2 = t.p[(f + t.YWRAP) & t.size];
|
|
232
|
-
n2 += rxf * (t.p[(f + t.YWRAP + 1) & t.size] - n2);
|
|
233
|
-
n1 += ryf * (n2 - n1);
|
|
234
|
-
f += t.ZWRAP;
|
|
235
|
-
n2 = t.p[f & t.size];
|
|
236
|
-
n2 += rxf * (t.p[(f + 1) & t.size] - n2);
|
|
237
|
-
n3 = t.p[(f + t.YWRAP) & t.size];
|
|
238
|
-
n3 += rxf * (t.p[(f + t.YWRAP + 1) & t.size] - n3);
|
|
239
|
-
n2 += ryf * (n3 - n2);
|
|
240
|
-
n1 += t.scaled_cosine(zf) * (n2 - n1);
|
|
241
|
-
r += n1 * amp;
|
|
242
|
-
amp *= t.falloff;
|
|
243
|
-
xi <<= 1;
|
|
244
|
-
xf *= 2;
|
|
245
|
-
yi <<= 1;
|
|
246
|
-
yf *= 2;
|
|
247
|
-
zi <<= 1;
|
|
248
|
-
zf *= 2;
|
|
249
|
-
if (xf >= 1.0) {
|
|
250
|
-
xi++;
|
|
251
|
-
xf--;
|
|
252
|
-
}
|
|
253
|
-
if (yf >= 1.0) {
|
|
254
|
-
yi++;
|
|
255
|
-
yf--;
|
|
256
|
-
}
|
|
257
|
-
if (zf >= 1.0) {
|
|
258
|
-
zi++;
|
|
259
|
-
zf--;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return r;
|
|
263
|
-
}
|
|
264
|
-
};
|