@n3rdw1z4rd/core 0.11.0 → 0.14.0
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 +68 -2
- package/dist/index.d.mts +445 -34
- package/dist/index.d.ts +445 -34
- package/dist/index.js +2239 -255
- package/dist/index.mjs +2175 -254
- package/package.json +9 -4
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
+
// src/astar.ts
|
|
2
|
+
var DIRECTIONS_4_WAY = [
|
|
3
|
+
{ x: 0, y: -1 },
|
|
4
|
+
{ x: 1, y: 0 },
|
|
5
|
+
{ x: 0, y: 1 },
|
|
6
|
+
{ x: -1, y: 0 }
|
|
7
|
+
];
|
|
8
|
+
var AStar = class {
|
|
9
|
+
map;
|
|
10
|
+
constructor(map) {
|
|
11
|
+
this.map = map;
|
|
12
|
+
}
|
|
13
|
+
_manhattan(a, b) {
|
|
14
|
+
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
|
|
15
|
+
}
|
|
16
|
+
_isWalkable(point, walkable = [0]) {
|
|
17
|
+
const value = this.map.get(point.x, point.y);
|
|
18
|
+
return walkable.includes(value);
|
|
19
|
+
}
|
|
20
|
+
_key(x, y) {
|
|
21
|
+
return `${x},${y}`;
|
|
22
|
+
}
|
|
23
|
+
// NOTE: ported as-is from rogue-craft. useAdjacent is currently hard-coded
|
|
24
|
+
// to true below - the original had a known unresolved bug where setting it
|
|
25
|
+
// to false via params caused an infinite loop, so that path is disabled
|
|
26
|
+
// rather than exposed broken.
|
|
27
|
+
findPath(a, b, params = {}) {
|
|
28
|
+
const useAdjacent = true;
|
|
29
|
+
const walkable = params.walkableValues ?? [0];
|
|
30
|
+
const openSet = [];
|
|
31
|
+
const closedSet = /* @__PURE__ */ new Set();
|
|
32
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
33
|
+
const startNode = {
|
|
34
|
+
pos: a,
|
|
35
|
+
g: 0,
|
|
36
|
+
h: this._manhattan(a, b),
|
|
37
|
+
f: this._manhattan(a, b)
|
|
38
|
+
};
|
|
39
|
+
openSet.push(startNode);
|
|
40
|
+
nodeMap.set(this._key(a.x, a.y), startNode);
|
|
41
|
+
const targetKeys = /* @__PURE__ */ new Set();
|
|
42
|
+
targetKeys.add(this._key(b.x, b.y));
|
|
43
|
+
if (useAdjacent) {
|
|
44
|
+
for (const dir of DIRECTIONS_4_WAY) {
|
|
45
|
+
const nx = b.x + dir.x;
|
|
46
|
+
const ny = b.y + dir.y;
|
|
47
|
+
targetKeys.add(this._key(nx, ny));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
while (openSet.length > 0) {
|
|
51
|
+
openSet.sort((a2, b2) => a2.f - b2.f);
|
|
52
|
+
const current = openSet.shift();
|
|
53
|
+
const currentKey = this._key(current.pos.x, current.pos.y);
|
|
54
|
+
if (targetKeys.has(currentKey)) {
|
|
55
|
+
const path = [];
|
|
56
|
+
let curr = current;
|
|
57
|
+
while (curr) {
|
|
58
|
+
path.push(curr.pos);
|
|
59
|
+
curr = curr.parent;
|
|
60
|
+
}
|
|
61
|
+
path.pop();
|
|
62
|
+
return path.reverse();
|
|
63
|
+
}
|
|
64
|
+
closedSet.add(currentKey);
|
|
65
|
+
for (const dir of DIRECTIONS_4_WAY) {
|
|
66
|
+
const neighborPos = {
|
|
67
|
+
x: current.pos.x + dir.x,
|
|
68
|
+
y: current.pos.y + dir.y
|
|
69
|
+
};
|
|
70
|
+
const key = this._key(neighborPos.x, neighborPos.y);
|
|
71
|
+
if (!this._isWalkable(neighborPos, walkable) || closedSet.has(key)) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const g = current.g + 1;
|
|
75
|
+
let neighbor = nodeMap.get(key);
|
|
76
|
+
if (!neighbor) {
|
|
77
|
+
neighbor = {
|
|
78
|
+
pos: neighborPos,
|
|
79
|
+
g,
|
|
80
|
+
h: this._manhattan(neighborPos, b),
|
|
81
|
+
f: g + this._manhattan(neighborPos, b),
|
|
82
|
+
parent: current
|
|
83
|
+
};
|
|
84
|
+
nodeMap.set(key, neighbor);
|
|
85
|
+
openSet.push(neighbor);
|
|
86
|
+
} else if (g < neighbor.g) {
|
|
87
|
+
neighbor.g = g;
|
|
88
|
+
neighbor.f = g + neighbor.h;
|
|
89
|
+
neighbor.parent = current;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
1
97
|
// src/clock-stats.ts
|
|
2
98
|
var ClockStats = class {
|
|
3
99
|
divElement;
|
|
@@ -131,6 +227,767 @@ var loginf = console.log.bind(console, "%c[inf]", "font-weight: bold;");
|
|
|
131
227
|
var logwrn = console.log.bind(console, "%c[wrn]", "font-weight: bold; color: orange;");
|
|
132
228
|
var logerr = console.log.bind(console, "%c[err]", "font-weight: bold; color: red;");
|
|
133
229
|
|
|
230
|
+
// src/math.ts
|
|
231
|
+
var DEG2RAD = 0.01745329;
|
|
232
|
+
var PI = 3.141592653589793;
|
|
233
|
+
var TWO_PI = PI * 2;
|
|
234
|
+
var HALF_PI = PI / 2;
|
|
235
|
+
function abs(n) {
|
|
236
|
+
return n < 0 ? -n : n;
|
|
237
|
+
}
|
|
238
|
+
function min(a, b) {
|
|
239
|
+
return a < b ? a : b;
|
|
240
|
+
}
|
|
241
|
+
function max(a, b) {
|
|
242
|
+
return a > b ? a : b;
|
|
243
|
+
}
|
|
244
|
+
function floor(n) {
|
|
245
|
+
const t = n | 0;
|
|
246
|
+
return n < 0 && t !== n ? t - 1 : t;
|
|
247
|
+
}
|
|
248
|
+
function ceil(n) {
|
|
249
|
+
const t = n | 0;
|
|
250
|
+
return n > 0 && t !== n ? t + 1 : t;
|
|
251
|
+
}
|
|
252
|
+
function round(n) {
|
|
253
|
+
return floor(n + 0.5);
|
|
254
|
+
}
|
|
255
|
+
function sqrt(n) {
|
|
256
|
+
return n ** 0.5;
|
|
257
|
+
}
|
|
258
|
+
function pow(base, exp) {
|
|
259
|
+
return base ** exp;
|
|
260
|
+
}
|
|
261
|
+
function hypot2(x, y) {
|
|
262
|
+
return (x * x + y * y) ** 0.5;
|
|
263
|
+
}
|
|
264
|
+
function hypot3(x, y, z) {
|
|
265
|
+
return (x * x + y * y + z * z) ** 0.5;
|
|
266
|
+
}
|
|
267
|
+
function imul(a, b) {
|
|
268
|
+
b |= 0;
|
|
269
|
+
let result = (a & 4194303) * b;
|
|
270
|
+
if (a & 4290772992) result += (a & 4290772992) * b | 0;
|
|
271
|
+
return result | 0;
|
|
272
|
+
}
|
|
273
|
+
function wrapAngle(n) {
|
|
274
|
+
n = n % TWO_PI;
|
|
275
|
+
if (n > PI) n -= TWO_PI;
|
|
276
|
+
else if (n < -PI) n += TWO_PI;
|
|
277
|
+
return n;
|
|
278
|
+
}
|
|
279
|
+
function sinSeries(n) {
|
|
280
|
+
const n2 = n * n;
|
|
281
|
+
return n * (1 + n2 * (-1 / 6 + n2 * (1 / 120 + n2 * (-1 / 5040 + n2 * (1 / 362880 + n2 * (-1 / 39916800))))));
|
|
282
|
+
}
|
|
283
|
+
function cosSeries(n) {
|
|
284
|
+
const n2 = n * n;
|
|
285
|
+
return 1 + n2 * (-1 / 2 + n2 * (1 / 24 + n2 * (-1 / 720 + n2 * (1 / 40320 + n2 * (-1 / 3628800)))));
|
|
286
|
+
}
|
|
287
|
+
function reduceAngle(n) {
|
|
288
|
+
let x = wrapAngle(n);
|
|
289
|
+
let sinSign = 1;
|
|
290
|
+
if (x < 0) {
|
|
291
|
+
x = -x;
|
|
292
|
+
sinSign = -1;
|
|
293
|
+
}
|
|
294
|
+
let cosSign = 1;
|
|
295
|
+
if (x > HALF_PI) {
|
|
296
|
+
x = PI - x;
|
|
297
|
+
cosSign = -1;
|
|
298
|
+
}
|
|
299
|
+
return [x, sinSign, cosSign];
|
|
300
|
+
}
|
|
301
|
+
function sin(n) {
|
|
302
|
+
const [x, sinSign] = reduceAngle(n);
|
|
303
|
+
return sinSign * sinSeries(x);
|
|
304
|
+
}
|
|
305
|
+
function cos(n) {
|
|
306
|
+
const [x, , cosSign] = reduceAngle(n);
|
|
307
|
+
return cosSign * cosSeries(x);
|
|
308
|
+
}
|
|
309
|
+
function clamp(value, lo, hi) {
|
|
310
|
+
return max(lo, min(hi, value));
|
|
311
|
+
}
|
|
312
|
+
function deg2rad(value) {
|
|
313
|
+
return value * DEG2RAD;
|
|
314
|
+
}
|
|
315
|
+
function lerp(start, end, scale) {
|
|
316
|
+
return start * (1 - scale) + end * scale;
|
|
317
|
+
}
|
|
318
|
+
function distance(x1, y1, x2, y2) {
|
|
319
|
+
return hypot2(x2 - x1, y2 - y1);
|
|
320
|
+
}
|
|
321
|
+
function squaredDistance(x1, y1, x2, y2) {
|
|
322
|
+
const dx = x1 - x2;
|
|
323
|
+
const dy = y1 - y2;
|
|
324
|
+
return dx * dx + dy * dy;
|
|
325
|
+
}
|
|
326
|
+
function manhattanDistance(x1, y1, x2, y2) {
|
|
327
|
+
return abs(x1 - x2) + abs(y1 - y2);
|
|
328
|
+
}
|
|
329
|
+
function roundTo(value, digits, base = 10) {
|
|
330
|
+
const p = pow(base, digits);
|
|
331
|
+
return round(value * p) / p;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/color.ts
|
|
335
|
+
var Color = class _Color {
|
|
336
|
+
_hex = "#ffffffff";
|
|
337
|
+
_r = 255;
|
|
338
|
+
_g = 255;
|
|
339
|
+
_b = 255;
|
|
340
|
+
_a = 255;
|
|
341
|
+
get r() {
|
|
342
|
+
return this._r;
|
|
343
|
+
}
|
|
344
|
+
set r(value) {
|
|
345
|
+
this._r = clamp(value, 0, 255);
|
|
346
|
+
this._hex = this._to_hex();
|
|
347
|
+
}
|
|
348
|
+
get g() {
|
|
349
|
+
return this._g;
|
|
350
|
+
}
|
|
351
|
+
set g(value) {
|
|
352
|
+
this._g = clamp(value, 0, 255);
|
|
353
|
+
this._hex = this._to_hex();
|
|
354
|
+
}
|
|
355
|
+
get b() {
|
|
356
|
+
return this._b;
|
|
357
|
+
}
|
|
358
|
+
set b(value) {
|
|
359
|
+
this._b = clamp(value, 0, 255);
|
|
360
|
+
this._hex = this._to_hex();
|
|
361
|
+
}
|
|
362
|
+
get a() {
|
|
363
|
+
return this._a;
|
|
364
|
+
}
|
|
365
|
+
set a(value) {
|
|
366
|
+
this._a = clamp(value, 0, 255);
|
|
367
|
+
this._hex = this._to_hex();
|
|
368
|
+
}
|
|
369
|
+
get hexStr() {
|
|
370
|
+
return this._hex;
|
|
371
|
+
}
|
|
372
|
+
set hexStr(value) {
|
|
373
|
+
if (value.startsWith("#")) {
|
|
374
|
+
[this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
|
|
375
|
+
this._hex = value;
|
|
376
|
+
} else {
|
|
377
|
+
logwrn(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
get rgba() {
|
|
381
|
+
return [this._r, this._g, this._b, this._a];
|
|
382
|
+
}
|
|
383
|
+
constructor(r = 255, g = 255, b = 255, a = 255) {
|
|
384
|
+
if (typeof r === "string") {
|
|
385
|
+
if (r.startsWith("#")) {
|
|
386
|
+
[r, g, b, a] = this._parse_hex_color_string(r);
|
|
387
|
+
} else {
|
|
388
|
+
logwrn(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
|
|
389
|
+
r = g = b = a = 255;
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
[r, g, b, a] = [r, g, b, a].map((n) => _Color._normalize(n));
|
|
393
|
+
}
|
|
394
|
+
this.r = r;
|
|
395
|
+
this.g = g;
|
|
396
|
+
this.b = b;
|
|
397
|
+
this.a = a;
|
|
398
|
+
}
|
|
399
|
+
static _normalize(value) {
|
|
400
|
+
return clamp(0 | (value > 0 && value < 1 ? value * 255 : value), 0, 255);
|
|
401
|
+
}
|
|
402
|
+
_to_hex() {
|
|
403
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
|
|
404
|
+
}
|
|
405
|
+
_parse_hex_color_string(hex) {
|
|
406
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex);
|
|
407
|
+
return result ? [
|
|
408
|
+
parseInt(result[1], 16),
|
|
409
|
+
parseInt(result[2], 16),
|
|
410
|
+
parseInt(result[3], 16),
|
|
411
|
+
result[4] ? parseInt(result[4], 16) : 255
|
|
412
|
+
] : [255, 255, 255, 255];
|
|
413
|
+
}
|
|
414
|
+
static fromHsv(h, s, v, a = 255) {
|
|
415
|
+
let r = 0;
|
|
416
|
+
let g = 0;
|
|
417
|
+
let b = 0;
|
|
418
|
+
let i = Math.floor(h * 6);
|
|
419
|
+
let f = h * 6 - i;
|
|
420
|
+
let p = v * (1 - s);
|
|
421
|
+
let q = v * (1 - f * s);
|
|
422
|
+
let t = v * (1 - (1 - f) * s);
|
|
423
|
+
switch (i % 6) {
|
|
424
|
+
case 0:
|
|
425
|
+
r = v, g = t, b = p;
|
|
426
|
+
break;
|
|
427
|
+
case 1:
|
|
428
|
+
r = q, g = v, b = p;
|
|
429
|
+
break;
|
|
430
|
+
case 2:
|
|
431
|
+
r = p, g = v, b = t;
|
|
432
|
+
break;
|
|
433
|
+
case 3:
|
|
434
|
+
r = p, g = q, b = v;
|
|
435
|
+
break;
|
|
436
|
+
case 4:
|
|
437
|
+
r = t, g = p, b = v;
|
|
438
|
+
break;
|
|
439
|
+
case 5:
|
|
440
|
+
r = v, g = p, b = q;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
return new _Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);
|
|
444
|
+
}
|
|
445
|
+
static get BLACK() {
|
|
446
|
+
return new _Color(0, 0, 0, 255);
|
|
447
|
+
}
|
|
448
|
+
static get GRAY() {
|
|
449
|
+
return new _Color(128, 128, 128, 255);
|
|
450
|
+
}
|
|
451
|
+
static get WHITE() {
|
|
452
|
+
return new _Color(255, 255, 255, 255);
|
|
453
|
+
}
|
|
454
|
+
static get RED() {
|
|
455
|
+
return new _Color(255, 0, 0, 255);
|
|
456
|
+
}
|
|
457
|
+
static get GREEN() {
|
|
458
|
+
return new _Color(0, 255, 0, 255);
|
|
459
|
+
}
|
|
460
|
+
static get BLUE() {
|
|
461
|
+
return new _Color(0, 0, 255, 255);
|
|
462
|
+
}
|
|
463
|
+
static get YELLOW() {
|
|
464
|
+
return new _Color(255, 255, 0, 255);
|
|
465
|
+
}
|
|
466
|
+
static get ORANGE() {
|
|
467
|
+
return new _Color(255, 128, 0, 255);
|
|
468
|
+
}
|
|
469
|
+
static get PURPLE() {
|
|
470
|
+
return new _Color(128, 0, 128, 255);
|
|
471
|
+
}
|
|
472
|
+
static get CYAN() {
|
|
473
|
+
return new _Color(0, 255, 255, 255);
|
|
474
|
+
}
|
|
475
|
+
static get MAGENTA() {
|
|
476
|
+
return new _Color(255, 0, 255, 255);
|
|
477
|
+
}
|
|
478
|
+
static fromName(name) {
|
|
479
|
+
const element = document.createElement("div");
|
|
480
|
+
element.style.color = name;
|
|
481
|
+
document.body.appendChild(element);
|
|
482
|
+
const computedColor = getComputedStyle(element).color;
|
|
483
|
+
document.body.removeChild(element);
|
|
484
|
+
const rgba = computedColor.match(/\d+/g);
|
|
485
|
+
if (rgba && rgba.length === 4) {
|
|
486
|
+
const [r, g, b, a] = rgba.map(Number);
|
|
487
|
+
return new _Color(r, g, b, a);
|
|
488
|
+
} else {
|
|
489
|
+
logwrn(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
|
|
490
|
+
return _Color.TRANSPARENT;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
static get TRANSPARENT() {
|
|
494
|
+
return new _Color(0, 0, 0, 0);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/dungeon-generator.ts
|
|
499
|
+
import { Heap } from "heap-js";
|
|
500
|
+
|
|
501
|
+
// src/map2d.ts
|
|
502
|
+
var DEFAULT_VALUE = -1;
|
|
503
|
+
var Map2D = class {
|
|
504
|
+
defaultValue;
|
|
505
|
+
_data;
|
|
506
|
+
get size() {
|
|
507
|
+
return this._data.size;
|
|
508
|
+
}
|
|
509
|
+
constructor(defaultValue = DEFAULT_VALUE) {
|
|
510
|
+
this.defaultValue = defaultValue;
|
|
511
|
+
this._data = /* @__PURE__ */ new Map();
|
|
512
|
+
}
|
|
513
|
+
_floor(x, y) {
|
|
514
|
+
return [floor(x), floor(y)];
|
|
515
|
+
}
|
|
516
|
+
_key(x, y) {
|
|
517
|
+
return `${x},${y}`;
|
|
518
|
+
}
|
|
519
|
+
_pos(key) {
|
|
520
|
+
return key.split(",").map((v) => parseInt(v));
|
|
521
|
+
}
|
|
522
|
+
clear() {
|
|
523
|
+
this._data.clear();
|
|
524
|
+
}
|
|
525
|
+
get(x, y, defaultValue = this.defaultValue) {
|
|
526
|
+
[x, y] = this._floor(x, y);
|
|
527
|
+
return this._data.get(this._key(x, y)) ?? defaultValue;
|
|
528
|
+
}
|
|
529
|
+
set(x, y, value = this.defaultValue) {
|
|
530
|
+
[x, y] = this._floor(x, y);
|
|
531
|
+
const key = this._key(x, y);
|
|
532
|
+
if (value === this.defaultValue) {
|
|
533
|
+
this._data.delete(key);
|
|
534
|
+
} else {
|
|
535
|
+
this._data.set(key, value);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
forEach(callback) {
|
|
539
|
+
this._data.forEach((v, key) => {
|
|
540
|
+
const [x, y] = this._pos(key);
|
|
541
|
+
callback(x, y, v);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// src/vector.ts
|
|
547
|
+
var Vector = class _Vector {
|
|
548
|
+
x;
|
|
549
|
+
y;
|
|
550
|
+
constructor(x = 0, y = 0) {
|
|
551
|
+
this.x = x;
|
|
552
|
+
this.y = y;
|
|
553
|
+
}
|
|
554
|
+
clone() {
|
|
555
|
+
return new _Vector(this.x, this.y);
|
|
556
|
+
}
|
|
557
|
+
distanceTo(that) {
|
|
558
|
+
return distance(
|
|
559
|
+
this.x,
|
|
560
|
+
this.y,
|
|
561
|
+
that.x,
|
|
562
|
+
that.y
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
floored() {
|
|
566
|
+
return new _Vector(
|
|
567
|
+
Math.floor(this.x),
|
|
568
|
+
Math.floor(this.y)
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
plus(x, y) {
|
|
572
|
+
if (x instanceof _Vector) {
|
|
573
|
+
y = x.y;
|
|
574
|
+
x = x.x;
|
|
575
|
+
} else if (y === void 0) {
|
|
576
|
+
y = x;
|
|
577
|
+
}
|
|
578
|
+
this.x += x;
|
|
579
|
+
this.y += y;
|
|
580
|
+
return this;
|
|
581
|
+
}
|
|
582
|
+
minus(x, y) {
|
|
583
|
+
if (x instanceof _Vector) {
|
|
584
|
+
y = x.y;
|
|
585
|
+
x = x.x;
|
|
586
|
+
} else if (y === void 0) {
|
|
587
|
+
y = x;
|
|
588
|
+
}
|
|
589
|
+
this.x -= x;
|
|
590
|
+
this.y -= y;
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
times(x, y) {
|
|
594
|
+
if (x instanceof _Vector) {
|
|
595
|
+
y = x.y;
|
|
596
|
+
x = x.x;
|
|
597
|
+
} else if (y === void 0) {
|
|
598
|
+
y = x;
|
|
599
|
+
}
|
|
600
|
+
this.x *= x;
|
|
601
|
+
this.y *= y;
|
|
602
|
+
return this;
|
|
603
|
+
}
|
|
604
|
+
dividedBy(x, y) {
|
|
605
|
+
if (x instanceof _Vector) {
|
|
606
|
+
y = x.y;
|
|
607
|
+
x = x.x;
|
|
608
|
+
} else if (y === void 0) {
|
|
609
|
+
y = x;
|
|
610
|
+
}
|
|
611
|
+
this.x /= x || 1;
|
|
612
|
+
this.y /= y || 1;
|
|
613
|
+
return this;
|
|
614
|
+
}
|
|
615
|
+
static get NORTH() {
|
|
616
|
+
return new _Vector(0, -1);
|
|
617
|
+
}
|
|
618
|
+
static get SOUTH() {
|
|
619
|
+
return new _Vector(0, 1);
|
|
620
|
+
}
|
|
621
|
+
static get EAST() {
|
|
622
|
+
return new _Vector(1, 0);
|
|
623
|
+
}
|
|
624
|
+
static get WEST() {
|
|
625
|
+
return new _Vector(-1, 0);
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
// src/rectangle.ts
|
|
630
|
+
var Rectangle = class _Rectangle {
|
|
631
|
+
x;
|
|
632
|
+
y;
|
|
633
|
+
width;
|
|
634
|
+
height;
|
|
635
|
+
constructor(x = 0, y = 0, width = 1, height = 1) {
|
|
636
|
+
this.x = x;
|
|
637
|
+
this.y = y;
|
|
638
|
+
this.width = width;
|
|
639
|
+
this.height = height;
|
|
640
|
+
}
|
|
641
|
+
area() {
|
|
642
|
+
return this.width * this.height;
|
|
643
|
+
}
|
|
644
|
+
contains(target, padding = 0) {
|
|
645
|
+
return target instanceof Vector ? target.x >= this.x - padding && target.x <= this.x + this.width + padding && target.y >= this.y - padding && target.y <= this.y + this.height + padding : target.x >= this.x + padding && target.y >= this.y + padding && target.x + target.width <= this.x + this.width - padding && target.y + target.height <= this.y + this.height - padding;
|
|
646
|
+
}
|
|
647
|
+
intersects(other, padding = 0) {
|
|
648
|
+
return !(other.x - padding > this.x + this.width + padding || other.x + other.width + padding < this.x - padding || other.y - padding > this.y + this.height + padding || other.y + other.height + padding < this.y - padding);
|
|
649
|
+
}
|
|
650
|
+
center() {
|
|
651
|
+
return new Vector(
|
|
652
|
+
this.x + this.width / 2,
|
|
653
|
+
this.y + this.height / 2
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
distanceTo(other) {
|
|
657
|
+
return this.center().distanceTo(other.center());
|
|
658
|
+
}
|
|
659
|
+
clone() {
|
|
660
|
+
return new _Rectangle(this.x, this.y, this.width, this.height);
|
|
661
|
+
}
|
|
662
|
+
equals(other) {
|
|
663
|
+
return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
|
|
664
|
+
}
|
|
665
|
+
grow(padding) {
|
|
666
|
+
return new _Rectangle(
|
|
667
|
+
this.x - padding,
|
|
668
|
+
this.y - padding,
|
|
669
|
+
this.width + padding * 2,
|
|
670
|
+
this.height + padding * 2
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
vectorInside(vec, padding = 0) {
|
|
674
|
+
return vec.x >= this.x - padding && vec.x <= this.x + this.width + padding && vec.y >= this.y - padding && vec.y <= this.y + this.height + padding;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/rng.ts
|
|
679
|
+
var Rng = class {
|
|
680
|
+
__seed;
|
|
681
|
+
_seed;
|
|
682
|
+
_uid_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
683
|
+
constructor() {
|
|
684
|
+
this._seed = Date.now();
|
|
685
|
+
this.__seed = this._seed;
|
|
686
|
+
this._uid_characters = this.shuffle(this._uid_characters);
|
|
687
|
+
this.seed = this.nexti;
|
|
688
|
+
}
|
|
689
|
+
get seed() {
|
|
690
|
+
return this._seed;
|
|
691
|
+
}
|
|
692
|
+
set seed(value) {
|
|
693
|
+
this._seed = value;
|
|
694
|
+
this.__seed = this._seed;
|
|
695
|
+
}
|
|
696
|
+
get startingSeed() {
|
|
697
|
+
return this.__seed;
|
|
698
|
+
}
|
|
699
|
+
get nextf() {
|
|
700
|
+
this._seed |= 0;
|
|
701
|
+
this._seed = this._seed + 2654435769 | 0;
|
|
702
|
+
let t = this._seed ^ this._seed >>> 16;
|
|
703
|
+
t = imul(t, 569420461);
|
|
704
|
+
t = t ^ t >>> 15;
|
|
705
|
+
t = imul(t, 1935289751);
|
|
706
|
+
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
|
|
707
|
+
}
|
|
708
|
+
get nexti() {
|
|
709
|
+
return this.nextf * Number.MAX_SAFE_INTEGER | 0;
|
|
710
|
+
}
|
|
711
|
+
// NOTE: as of the core/apparatus consolidation, range(min, max) treats
|
|
712
|
+
// max as EXCLUSIVE (previous published versions of this package treated
|
|
713
|
+
// it as inclusive - if you're upgrading from an older @n3rdw1z4rd/core,
|
|
714
|
+
// range(0, 10) now returns [0, 10) instead of [0, 10]).
|
|
715
|
+
range(min2, max2) {
|
|
716
|
+
if (max2 === void 0) {
|
|
717
|
+
max2 = min2;
|
|
718
|
+
min2 = 0;
|
|
719
|
+
}
|
|
720
|
+
return min2 + this.nextf * (max2 - min2) | 0;
|
|
721
|
+
}
|
|
722
|
+
randomUnitVector() {
|
|
723
|
+
const theta = this.nextf * 2 * PI;
|
|
724
|
+
return [cos(theta), sin(theta)];
|
|
725
|
+
}
|
|
726
|
+
// Box-Muller-ish gaussian sample, mean 0.5 stddev 1/6 (so ~99.7% of
|
|
727
|
+
// samples land in [0, 1]). Uses native Math.log since a pure natural-log
|
|
728
|
+
// implementation isn't part of math.ts yet.
|
|
729
|
+
parkMillerNormal() {
|
|
730
|
+
const mean = 1 / 2;
|
|
731
|
+
const stddev = 1 / 6;
|
|
732
|
+
let u = 0;
|
|
733
|
+
let v = 0;
|
|
734
|
+
while (u === 0) u = this.nextf;
|
|
735
|
+
while (v === 0) v = this.nextf;
|
|
736
|
+
const n = sqrt(-2 * Math.log(u)) * cos(2 * PI * v);
|
|
737
|
+
return n * stddev + mean;
|
|
738
|
+
}
|
|
739
|
+
choose(...args) {
|
|
740
|
+
if (args.length === 1) {
|
|
741
|
+
if (Array.isArray(args[0])) {
|
|
742
|
+
return args[0][this.range(args[0].length - 1)];
|
|
743
|
+
} else if (typeof args[0] === "string") {
|
|
744
|
+
return args[0].charAt(this.range(args[0].length - 1));
|
|
745
|
+
} else {
|
|
746
|
+
return args[0];
|
|
747
|
+
}
|
|
748
|
+
} else {
|
|
749
|
+
return args[this.range(args.length - 1)];
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
shuffle(value) {
|
|
753
|
+
if (Array.isArray(value)) {
|
|
754
|
+
return value.sort(() => 0.5 - this.nextf);
|
|
755
|
+
} else {
|
|
756
|
+
return value.split("").sort(() => 0.5 - this.nextf).join("");
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
uid(length = 16) {
|
|
760
|
+
const uid = [];
|
|
761
|
+
for (let i = 0; i < length; i++) uid.push(this.choose(this._uid_characters));
|
|
762
|
+
return uid.join("");
|
|
763
|
+
}
|
|
764
|
+
randomMatrix(size) {
|
|
765
|
+
const rows = [];
|
|
766
|
+
for (let i = 0; i < size; i++) {
|
|
767
|
+
const row = [];
|
|
768
|
+
for (let j = 0; j < size; j++) {
|
|
769
|
+
row.push(this.nextf * 2 - 1);
|
|
770
|
+
}
|
|
771
|
+
rows.push(row);
|
|
772
|
+
}
|
|
773
|
+
return rows;
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
var rng = new Rng();
|
|
777
|
+
|
|
778
|
+
// src/dungeon-generator.ts
|
|
779
|
+
var TILE_EMPTY = 0;
|
|
780
|
+
var TILE_FLOOR = 1;
|
|
781
|
+
var TILE_PATH = 2;
|
|
782
|
+
var DEFAULT_LEVEL_SIZE = 64;
|
|
783
|
+
var DEFAULT_ROOM_MIN_SIZE = 3;
|
|
784
|
+
var DEFAULT_ROOM_MAX_SIZE = 9;
|
|
785
|
+
var DEFAULT_ROOM_PADDING = 1;
|
|
786
|
+
var DEFAULT_ROOM_MAX_ITERATIONS = 1e3;
|
|
787
|
+
var DEFAULT_PATH_TIMEOUT = 1e4;
|
|
788
|
+
var DEFAULT_PATH_EXTRA_PATHS = 0;
|
|
789
|
+
var DEFAULT_PATH_EXTRA_PATH_DENSITY = 0.1;
|
|
790
|
+
var DEFAULT_DOOR_CHANCE_EXISTS = 0.5;
|
|
791
|
+
var DIRECTIONS_8_WAY = [
|
|
792
|
+
{ x: -1, y: 0 },
|
|
793
|
+
// left
|
|
794
|
+
{ x: -1, y: -1 },
|
|
795
|
+
// left-up
|
|
796
|
+
{ x: 0, y: -1 },
|
|
797
|
+
// up
|
|
798
|
+
{ x: 1, y: -1 },
|
|
799
|
+
// up-right
|
|
800
|
+
{ x: 1, y: 0 },
|
|
801
|
+
// right
|
|
802
|
+
{ x: 1, y: 1 },
|
|
803
|
+
// right-down
|
|
804
|
+
{ x: 0, y: 1 },
|
|
805
|
+
// down
|
|
806
|
+
{ x: -1, y: 1 }
|
|
807
|
+
// down-left
|
|
808
|
+
];
|
|
809
|
+
function _plot(map, data, tile) {
|
|
810
|
+
data.forEach((element) => {
|
|
811
|
+
if (element instanceof Rectangle) {
|
|
812
|
+
for (let y = 0; y < element.height; y++) {
|
|
813
|
+
for (let x = 0; x < element.width; x++) {
|
|
814
|
+
map.set(element.x + x, element.y + y, tile);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
} else if (Array.isArray(element)) {
|
|
818
|
+
element.forEach((point) => {
|
|
819
|
+
map.set(point.x, point.y, tile);
|
|
820
|
+
});
|
|
821
|
+
} else {
|
|
822
|
+
map.set(element.x, element.y, tile);
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
function _generateRooms(params = {}) {
|
|
827
|
+
const size = params.size ?? DEFAULT_LEVEL_SIZE;
|
|
828
|
+
const minRoomSize = params.minSize ?? DEFAULT_ROOM_MIN_SIZE;
|
|
829
|
+
const maxRoomSize = params.maxSize ?? DEFAULT_ROOM_MAX_SIZE;
|
|
830
|
+
const padding = params.padding ?? DEFAULT_ROOM_PADDING;
|
|
831
|
+
const maxIterations = params.maxIterations ?? DEFAULT_ROOM_MAX_ITERATIONS;
|
|
832
|
+
const rooms = [];
|
|
833
|
+
let iterations = 0;
|
|
834
|
+
while (iterations < maxIterations) {
|
|
835
|
+
const baseSize = rng.range(minRoomSize, maxRoomSize);
|
|
836
|
+
const aspectRatio = 0.75 + rng.parkMillerNormal() * 0.5;
|
|
837
|
+
const width = Math.min(maxRoomSize, Math.max(minRoomSize, Math.round(baseSize * aspectRatio)));
|
|
838
|
+
const height = Math.min(maxRoomSize, Math.max(minRoomSize, Math.round(baseSize / aspectRatio)));
|
|
839
|
+
const x = Math.floor(rng.range(1 + padding, size - width - padding));
|
|
840
|
+
const y = Math.floor(rng.range(1 + padding, size - height - padding));
|
|
841
|
+
const room = new Rectangle(x, y, width, height);
|
|
842
|
+
if (!rooms.some((existing) => room.intersects(existing, padding))) {
|
|
843
|
+
rooms.push(room);
|
|
844
|
+
iterations = 0;
|
|
845
|
+
} else {
|
|
846
|
+
iterations += 1;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return rooms;
|
|
850
|
+
}
|
|
851
|
+
function _generateMST(rooms, params = {}) {
|
|
852
|
+
const timeout = params.timeout ?? DEFAULT_PATH_TIMEOUT;
|
|
853
|
+
const extraPathLevels = params.extraPaths ?? DEFAULT_PATH_EXTRA_PATHS;
|
|
854
|
+
const extraPathDensity = params.extraPathDensity ?? DEFAULT_PATH_EXTRA_PATH_DENSITY;
|
|
855
|
+
const startTime = Date.now();
|
|
856
|
+
const mst = [];
|
|
857
|
+
const extraPaths = [];
|
|
858
|
+
const visited = /* @__PURE__ */ new Set();
|
|
859
|
+
const heap = new Heap((a, b) => a.cost - b.cost);
|
|
860
|
+
visited.add(0);
|
|
861
|
+
const zeroCenter = rooms[0].center();
|
|
862
|
+
for (let i = 1; i < rooms.length; i++) {
|
|
863
|
+
const iCenter = rooms[i].center();
|
|
864
|
+
heap.push({
|
|
865
|
+
from: 0,
|
|
866
|
+
to: i,
|
|
867
|
+
cost: squaredDistance(
|
|
868
|
+
zeroCenter.x,
|
|
869
|
+
zeroCenter.y,
|
|
870
|
+
iCenter.x,
|
|
871
|
+
iCenter.y
|
|
872
|
+
)
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
while (visited.size < rooms.length && Date.now() - startTime < timeout) {
|
|
876
|
+
let edge = heap.pop();
|
|
877
|
+
while (edge && visited.has(edge.to)) {
|
|
878
|
+
edge = heap.pop();
|
|
879
|
+
}
|
|
880
|
+
if (!edge) break;
|
|
881
|
+
const { from, to } = edge;
|
|
882
|
+
visited.add(to);
|
|
883
|
+
mst.push({ a: rooms[from], b: rooms[to] });
|
|
884
|
+
const toCenter = rooms[to].center();
|
|
885
|
+
for (let i = 0; i < rooms.length; i++) {
|
|
886
|
+
if (!visited.has(i)) {
|
|
887
|
+
const iCenter = rooms[i].center();
|
|
888
|
+
heap.push({
|
|
889
|
+
from: to,
|
|
890
|
+
to: i,
|
|
891
|
+
cost: squaredDistance(
|
|
892
|
+
toCenter.x,
|
|
893
|
+
toCenter.y,
|
|
894
|
+
iCenter.x,
|
|
895
|
+
iCenter.y
|
|
896
|
+
)
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
const remainingEdges = heap.toArray().sort((a, b) => a.cost - b.cost);
|
|
902
|
+
for (let i = 0; i < extraPathLevels && i < remainingEdges.length; i++) {
|
|
903
|
+
const e = remainingEdges[i];
|
|
904
|
+
if (rng.nextf < extraPathDensity) {
|
|
905
|
+
extraPaths.push({
|
|
906
|
+
a: rooms[e.from],
|
|
907
|
+
b: rooms[e.to]
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
return mst.concat(extraPaths);
|
|
912
|
+
}
|
|
913
|
+
function _getRectEdgePoints(rect) {
|
|
914
|
+
const points = [];
|
|
915
|
+
const x2 = rect.x + rect.width - 1;
|
|
916
|
+
const y2 = rect.y + rect.height - 1;
|
|
917
|
+
for (let x1 = rect.x; x1 < x2; x1++) {
|
|
918
|
+
points.push({ x: x1, y: rect.y });
|
|
919
|
+
points.push({ x: x1, y: y2 });
|
|
920
|
+
}
|
|
921
|
+
for (let y1 = rect.y; y1 < y2; y1++) {
|
|
922
|
+
points.push({ x: rect.x, y: y1 });
|
|
923
|
+
points.push({ x: x2, y: y1 });
|
|
924
|
+
}
|
|
925
|
+
return points;
|
|
926
|
+
}
|
|
927
|
+
function _getShortestPathBetweenRects(rectA, rectB) {
|
|
928
|
+
const edgeA = _getRectEdgePoints(rectA);
|
|
929
|
+
const edgeB = _getRectEdgePoints(rectB);
|
|
930
|
+
let paths = { a: edgeA[0], b: edgeB[0], distance: Infinity };
|
|
931
|
+
for (const a of edgeA) {
|
|
932
|
+
for (const b of edgeB) {
|
|
933
|
+
const d = distance(a.x, a.y, b.x, b.y);
|
|
934
|
+
if (d < paths.distance) {
|
|
935
|
+
paths = { a, b, distance: d };
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return { a: paths.a, b: paths.b };
|
|
940
|
+
}
|
|
941
|
+
function _generatePaths(map, rooms, params = {}) {
|
|
942
|
+
const aStar = new AStar(map);
|
|
943
|
+
const paths = [];
|
|
944
|
+
_generateMST(rooms, params).forEach((rects) => {
|
|
945
|
+
const { a, b } = _getShortestPathBetweenRects(rects.a, rects.b);
|
|
946
|
+
paths.push(aStar.findPath(a, b, params.astarFindPathParams));
|
|
947
|
+
});
|
|
948
|
+
return paths;
|
|
949
|
+
}
|
|
950
|
+
function GenerateRooms(params = {}) {
|
|
951
|
+
const startTime = Date.now();
|
|
952
|
+
const map = params.map ?? new Map2D(TILE_EMPTY);
|
|
953
|
+
const rooms = _generateRooms(params.rooms);
|
|
954
|
+
_plot(map, rooms, TILE_FLOOR);
|
|
955
|
+
const paths = _generatePaths(map, rooms, params.paths);
|
|
956
|
+
_plot(map, paths, TILE_PATH);
|
|
957
|
+
log("GenerateRooms: done:", Date.now() - startTime, "ms");
|
|
958
|
+
return { map, rooms, paths };
|
|
959
|
+
}
|
|
960
|
+
function GenerateDoors(map, paths, params = {}) {
|
|
961
|
+
const chanceExists = params.chanceExists ?? DEFAULT_DOOR_CHANCE_EXISTS;
|
|
962
|
+
const possibleDoorPoints = [];
|
|
963
|
+
paths.forEach((points) => {
|
|
964
|
+
if (points.length > 1) {
|
|
965
|
+
possibleDoorPoints.push(points[0]);
|
|
966
|
+
possibleDoorPoints.push(points[points.length - 1]);
|
|
967
|
+
}
|
|
968
|
+
});
|
|
969
|
+
const doors = [];
|
|
970
|
+
possibleDoorPoints.forEach((doorPoint) => {
|
|
971
|
+
if (rng.nextf < chanceExists && (map.get(doorPoint.x, doorPoint.y - 1) === map.defaultValue && map.get(doorPoint.x, doorPoint.y + 1) === map.defaultValue || map.get(doorPoint.x - 1, doorPoint.y) === map.defaultValue && map.get(doorPoint.x + 1, doorPoint.y) === map.defaultValue)) {
|
|
972
|
+
doors.push(doorPoint);
|
|
973
|
+
}
|
|
974
|
+
});
|
|
975
|
+
return doors;
|
|
976
|
+
}
|
|
977
|
+
function GenerateWalls(map) {
|
|
978
|
+
const walls = [];
|
|
979
|
+
map.forEach((x, y, _v) => {
|
|
980
|
+
DIRECTIONS_8_WAY.forEach((dir) => {
|
|
981
|
+
const nx = dir.x + x;
|
|
982
|
+
const ny = dir.y + y;
|
|
983
|
+
if (map.get(nx, ny) === map.defaultValue) {
|
|
984
|
+
walls.push({ x: nx, y: ny });
|
|
985
|
+
}
|
|
986
|
+
});
|
|
987
|
+
});
|
|
988
|
+
return walls;
|
|
989
|
+
}
|
|
990
|
+
|
|
134
991
|
// src/ecs.ts
|
|
135
992
|
var ECS = class _ECS {
|
|
136
993
|
_globals = /* @__PURE__ */ new Map();
|
|
@@ -371,17 +1228,33 @@ var Emitter = class {
|
|
|
371
1228
|
this.listeners.get(eventName)?.forEach((listener) => listener(...args, eventName));
|
|
372
1229
|
return this;
|
|
373
1230
|
}
|
|
1231
|
+
clear(eventName) {
|
|
1232
|
+
if (eventName) {
|
|
1233
|
+
this.listeners.delete(eventName);
|
|
1234
|
+
} else {
|
|
1235
|
+
this.listeners.clear();
|
|
1236
|
+
}
|
|
1237
|
+
return this;
|
|
1238
|
+
}
|
|
374
1239
|
};
|
|
375
1240
|
var GlobalEmitter = new Emitter();
|
|
1241
|
+
var EventBus = class extends EventTarget {
|
|
1242
|
+
emit(type, detail) {
|
|
1243
|
+
this.dispatchEvent(new CustomEvent(type, { detail }));
|
|
1244
|
+
}
|
|
1245
|
+
on(type, cb) {
|
|
1246
|
+
this.addEventListener(type, cb);
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
var eventBus = new EventBus();
|
|
376
1250
|
|
|
377
1251
|
// src/input.ts
|
|
378
|
-
import { vec2 } from "gl-matrix";
|
|
379
1252
|
var Input = class extends Emitter {
|
|
380
1253
|
inputThreshold = 200;
|
|
381
1254
|
_keyStates = {};
|
|
382
1255
|
_mouseButtonStates = {};
|
|
383
|
-
_mousePosition =
|
|
384
|
-
_mousePosition2 =
|
|
1256
|
+
_mousePosition = [0, 0];
|
|
1257
|
+
_mousePosition2 = [0, 0];
|
|
385
1258
|
get mousePosition() {
|
|
386
1259
|
return this._mousePosition;
|
|
387
1260
|
}
|
|
@@ -457,13 +1330,13 @@ var Input = class extends Emitter {
|
|
|
457
1330
|
_onMouseMove(ev) {
|
|
458
1331
|
const props = this._getCommonEventProps(ev);
|
|
459
1332
|
const { buttons, offsetX, offsetY, movementX, movementY } = ev;
|
|
460
|
-
this._mousePosition =
|
|
1333
|
+
this._mousePosition = [offsetX, offsetY];
|
|
461
1334
|
const width = window.innerWidth;
|
|
462
1335
|
const height = window.innerHeight;
|
|
463
|
-
this._mousePosition2 =
|
|
1336
|
+
this._mousePosition2 = [
|
|
464
1337
|
ev.clientX / width * 2 - 1,
|
|
465
1338
|
-(ev.clientY / height) * 2 + 1
|
|
466
|
-
|
|
1339
|
+
];
|
|
467
1340
|
this.emit("mouse_move", {
|
|
468
1341
|
...props,
|
|
469
1342
|
buttons,
|
|
@@ -497,132 +1370,82 @@ var Input = class extends Emitter {
|
|
|
497
1370
|
}
|
|
498
1371
|
};
|
|
499
1372
|
|
|
500
|
-
// src/
|
|
501
|
-
var
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
function lerp(start, end, scale) {
|
|
509
|
-
return start * (1 - scale) + end * scale;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// src/misc.ts
|
|
513
|
-
function isNullOrUndefined(target) {
|
|
514
|
-
return target === null || target === void 0;
|
|
515
|
-
}
|
|
516
|
-
var getRedYellowGreenGradientHex = (min, max) => {
|
|
517
|
-
if (max === void 0) {
|
|
518
|
-
max = min;
|
|
519
|
-
min = 0;
|
|
1373
|
+
// src/map3d.ts
|
|
1374
|
+
var DEFAULT_VALUE2 = -1;
|
|
1375
|
+
var Map3D = class {
|
|
1376
|
+
defaultValue;
|
|
1377
|
+
_data;
|
|
1378
|
+
get data() {
|
|
1379
|
+
return this._data;
|
|
520
1380
|
}
|
|
521
|
-
|
|
522
|
-
|
|
1381
|
+
constructor(defaultValue = DEFAULT_VALUE2) {
|
|
1382
|
+
this.defaultValue = defaultValue;
|
|
1383
|
+
this._data = /* @__PURE__ */ new Map();
|
|
523
1384
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
return [
|
|
527
|
-
"#ff0000",
|
|
528
|
-
"#ff3300",
|
|
529
|
-
"#ff6600",
|
|
530
|
-
"#ff9900",
|
|
531
|
-
"#ffcc00",
|
|
532
|
-
"#ffff00",
|
|
533
|
-
"#ccff00",
|
|
534
|
-
"#99ff00",
|
|
535
|
-
"#66ff00",
|
|
536
|
-
"#33ff00",
|
|
537
|
-
"#00ff00"
|
|
538
|
-
][index];
|
|
539
|
-
};
|
|
540
|
-
|
|
541
|
-
// src/noise.ts
|
|
542
|
-
import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
|
|
543
|
-
|
|
544
|
-
// src/rng.ts
|
|
545
|
-
import { vec2 as vec22 } from "gl-matrix";
|
|
546
|
-
var Rng = class {
|
|
547
|
-
_seed = Date.now();
|
|
548
|
-
_uid_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
549
|
-
constructor() {
|
|
550
|
-
this._uid_characters = this.shuffle(this._uid_characters);
|
|
551
|
-
this.seed = this.nexti;
|
|
1385
|
+
_floor(x, y, z) {
|
|
1386
|
+
return [floor(x), floor(y), floor(z)];
|
|
552
1387
|
}
|
|
553
|
-
|
|
554
|
-
return
|
|
1388
|
+
_key(x, y, z) {
|
|
1389
|
+
return `${x},${y},${z}`;
|
|
555
1390
|
}
|
|
556
|
-
|
|
557
|
-
|
|
1391
|
+
_pos(key) {
|
|
1392
|
+
return key.split(",").map((v) => parseInt(v));
|
|
558
1393
|
}
|
|
559
|
-
|
|
560
|
-
this.
|
|
561
|
-
this._seed = this._seed + 2654435769 | 0;
|
|
562
|
-
let t = this._seed ^ this._seed >>> 16;
|
|
563
|
-
t = Math.imul(t, 569420461);
|
|
564
|
-
t = t ^ t >>> 15;
|
|
565
|
-
t = Math.imul(t, 1935289751);
|
|
566
|
-
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
|
|
567
|
-
}
|
|
568
|
-
get nexti() {
|
|
569
|
-
return this.nextf * Number.MAX_SAFE_INTEGER | 0;
|
|
570
|
-
}
|
|
571
|
-
range(min, max) {
|
|
572
|
-
if (max === void 0) {
|
|
573
|
-
if (min !== 0) {
|
|
574
|
-
max = min;
|
|
575
|
-
min = 0;
|
|
576
|
-
} else {
|
|
577
|
-
max = Number.MAX_SAFE_INTEGER;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
return this.nextf * (max - min + 1) + min | 0;
|
|
1394
|
+
clear() {
|
|
1395
|
+
this._data.clear();
|
|
581
1396
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
return args[0][this.range(args[0].length - 1)];
|
|
586
|
-
} else if (typeof args[0] === "string") {
|
|
587
|
-
return args[0].charAt(this.range(args[0].length - 1));
|
|
588
|
-
} else {
|
|
589
|
-
return args[0];
|
|
590
|
-
}
|
|
591
|
-
} else {
|
|
592
|
-
return args[this.range(args.length - 1)];
|
|
593
|
-
}
|
|
1397
|
+
get(x, y, z, defaultValue = this.defaultValue) {
|
|
1398
|
+
[x, y, z] = this._floor(x, y, z);
|
|
1399
|
+
return this._data.get(this._key(x, y, z)) ?? defaultValue;
|
|
594
1400
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
1401
|
+
set(x, y, z, value = this.defaultValue) {
|
|
1402
|
+
[x, y, z] = this._floor(x, y, z);
|
|
1403
|
+
const key = this._key(x, y, z);
|
|
1404
|
+
if (value === this.defaultValue) {
|
|
1405
|
+
this._data.delete(key);
|
|
598
1406
|
} else {
|
|
599
|
-
|
|
1407
|
+
this._data.set(key, value);
|
|
600
1408
|
}
|
|
601
1409
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
1410
|
+
forEach(callback) {
|
|
1411
|
+
this._data.forEach((v, key) => {
|
|
1412
|
+
const [x, y, z] = this._pos(key);
|
|
1413
|
+
callback(x, y, z, v);
|
|
1414
|
+
});
|
|
606
1415
|
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
1416
|
+
};
|
|
1417
|
+
|
|
1418
|
+
// src/misc.ts
|
|
1419
|
+
function isNullOrUndefined(target) {
|
|
1420
|
+
return target === null || target === void 0;
|
|
1421
|
+
}
|
|
1422
|
+
var getRedYellowGreenGradientHex = (min2, max2) => {
|
|
1423
|
+
if (max2 === void 0) {
|
|
1424
|
+
max2 = min2;
|
|
1425
|
+
min2 = 0;
|
|
617
1426
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
return vec22.fromValues(Math.cos(theta), Math.sin(theta));
|
|
1427
|
+
if (max2 < 1) {
|
|
1428
|
+
max2 = 1;
|
|
621
1429
|
}
|
|
1430
|
+
const percent = min2 / max2;
|
|
1431
|
+
const index = clamp(Math.floor(percent * 10), 0, 10);
|
|
1432
|
+
return [
|
|
1433
|
+
"#ff0000",
|
|
1434
|
+
"#ff3300",
|
|
1435
|
+
"#ff6600",
|
|
1436
|
+
"#ff9900",
|
|
1437
|
+
"#ffcc00",
|
|
1438
|
+
"#ffff00",
|
|
1439
|
+
"#ccff00",
|
|
1440
|
+
"#99ff00",
|
|
1441
|
+
"#66ff00",
|
|
1442
|
+
"#33ff00",
|
|
1443
|
+
"#00ff00"
|
|
1444
|
+
][index];
|
|
622
1445
|
};
|
|
623
|
-
var rng = new Rng();
|
|
624
1446
|
|
|
625
1447
|
// src/noise.ts
|
|
1448
|
+
import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
|
|
626
1449
|
var Noise = class _Noise {
|
|
627
1450
|
static _n2d;
|
|
628
1451
|
static _n3d;
|
|
@@ -682,8 +1505,284 @@ var Noise = class _Noise {
|
|
|
682
1505
|
}
|
|
683
1506
|
};
|
|
684
1507
|
|
|
1508
|
+
// src/particle-renderer.ts
|
|
1509
|
+
var Renderer = class {
|
|
1510
|
+
_screenCanvas;
|
|
1511
|
+
_bufferCanvas;
|
|
1512
|
+
screenContext;
|
|
1513
|
+
bufferContext;
|
|
1514
|
+
get width() {
|
|
1515
|
+
return this._screenCanvas.width;
|
|
1516
|
+
}
|
|
1517
|
+
get height() {
|
|
1518
|
+
return this._screenCanvas.height;
|
|
1519
|
+
}
|
|
1520
|
+
constructor(canvas) {
|
|
1521
|
+
this._screenCanvas = canvas ?? document.createElement("canvas");
|
|
1522
|
+
this._bufferCanvas = new OffscreenCanvas(this._screenCanvas.width, this._screenCanvas.height);
|
|
1523
|
+
this.screenContext = this._screenCanvas.getContext("2d");
|
|
1524
|
+
this.bufferContext = this._bufferCanvas.getContext("2d");
|
|
1525
|
+
if (!this.screenContext || !this.bufferContext) {
|
|
1526
|
+
throw "Failed to create CanvasRenderingContext2D";
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
appendTo(target, autoResize = true) {
|
|
1530
|
+
if (this._screenCanvas.parentNode) {
|
|
1531
|
+
this._screenCanvas.parentNode.removeChild(this._screenCanvas);
|
|
1532
|
+
}
|
|
1533
|
+
target.appendChild(this._screenCanvas);
|
|
1534
|
+
if (autoResize) {
|
|
1535
|
+
this.resize();
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
resize(displayWidth, displayHeight) {
|
|
1539
|
+
const { width, height } = this._screenCanvas.parentElement?.getBoundingClientRect() ?? this._screenCanvas.getBoundingClientRect();
|
|
1540
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
1541
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
1542
|
+
if (this._screenCanvas.width !== displayWidth || this._screenCanvas.height !== displayHeight) {
|
|
1543
|
+
this._screenCanvas.width = displayWidth;
|
|
1544
|
+
this._screenCanvas.height = displayHeight;
|
|
1545
|
+
this._bufferCanvas.width = displayWidth;
|
|
1546
|
+
this._bufferCanvas.height = displayHeight;
|
|
1547
|
+
return true;
|
|
1548
|
+
}
|
|
1549
|
+
return false;
|
|
1550
|
+
}
|
|
1551
|
+
setPixel(x, y, color, size = 2) {
|
|
1552
|
+
this.bufferContext.fillStyle = color.hexStr;
|
|
1553
|
+
this.bufferContext.fillRect(x - size / 2, y - size / 2, size, size);
|
|
1554
|
+
}
|
|
1555
|
+
render() {
|
|
1556
|
+
this.screenContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
1557
|
+
this.screenContext.drawImage(this._bufferCanvas, 0, 0);
|
|
1558
|
+
this.bufferContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
1559
|
+
}
|
|
1560
|
+
};
|
|
1561
|
+
|
|
1562
|
+
// src/spatial-partition-2d.ts
|
|
1563
|
+
var SpatialPartition2d = class {
|
|
1564
|
+
cells;
|
|
1565
|
+
cellSize;
|
|
1566
|
+
entities;
|
|
1567
|
+
constructor(cellSize, entities) {
|
|
1568
|
+
this.cellSize = cellSize;
|
|
1569
|
+
this.entities = entities;
|
|
1570
|
+
this.cells = Array.from(
|
|
1571
|
+
{ length: Math.ceil(1 / cellSize) },
|
|
1572
|
+
() => Array.from({ length: Math.ceil(1 / cellSize) }, () => [])
|
|
1573
|
+
);
|
|
1574
|
+
for (const entity of this.entities) {
|
|
1575
|
+
this.addEntity(entity);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
addEntity(entity) {
|
|
1579
|
+
const cell = this.getCell(entity.x, entity.y);
|
|
1580
|
+
cell.push(entity);
|
|
1581
|
+
}
|
|
1582
|
+
getCell(x, y) {
|
|
1583
|
+
const cellX = Math.floor(x / this.cellSize);
|
|
1584
|
+
const cellY = Math.floor(y / this.cellSize);
|
|
1585
|
+
return this.cells[cellY][cellX];
|
|
1586
|
+
}
|
|
1587
|
+
getCellNeighbors(x, y) {
|
|
1588
|
+
const neighbors = [];
|
|
1589
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
1590
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
1591
|
+
let nx = x + dx;
|
|
1592
|
+
let ny = y + dy;
|
|
1593
|
+
if (nx < 0) nx = this.cells[0].length - 1;
|
|
1594
|
+
else if (nx >= this.cells[0].length) nx = 0;
|
|
1595
|
+
if (ny < 0) ny = this.cells.length - 1;
|
|
1596
|
+
else if (ny >= this.cells.length) ny = 0;
|
|
1597
|
+
neighbors.push(this.cells[ny][nx]);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
return neighbors;
|
|
1601
|
+
}
|
|
1602
|
+
};
|
|
1603
|
+
|
|
1604
|
+
// src/particle-system-2d.ts
|
|
1605
|
+
var colors = [
|
|
1606
|
+
Color.RED,
|
|
1607
|
+
Color.GREEN,
|
|
1608
|
+
Color.BLUE,
|
|
1609
|
+
Color.YELLOW,
|
|
1610
|
+
Color.MAGENTA
|
|
1611
|
+
];
|
|
1612
|
+
var ParticleSystem2d = class {
|
|
1613
|
+
particleCount = 2e3;
|
|
1614
|
+
_particleSize = 2;
|
|
1615
|
+
_frictionHalfLife = 0.04;
|
|
1616
|
+
_range = 0.1;
|
|
1617
|
+
_rangeFactor = 0.1;
|
|
1618
|
+
_attractionMatrix = rng.randomMatrix(colors.length);
|
|
1619
|
+
_particles = [];
|
|
1620
|
+
_spatialPartition;
|
|
1621
|
+
constructor() {
|
|
1622
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
1623
|
+
this._particles.push({
|
|
1624
|
+
x: rng.nextf,
|
|
1625
|
+
y: rng.nextf,
|
|
1626
|
+
vx: 0,
|
|
1627
|
+
vy: 0,
|
|
1628
|
+
color: rng.range(colors.length)
|
|
1629
|
+
});
|
|
1630
|
+
}
|
|
1631
|
+
this._spatialPartition = new SpatialPartition2d(this._range, this._particles);
|
|
1632
|
+
}
|
|
1633
|
+
_calcForce(r, a, beta = 0.3) {
|
|
1634
|
+
let f = 0;
|
|
1635
|
+
if (r < beta) {
|
|
1636
|
+
f = r / beta - 1;
|
|
1637
|
+
} else if (beta < r && r < 1) {
|
|
1638
|
+
f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
1639
|
+
}
|
|
1640
|
+
return f;
|
|
1641
|
+
}
|
|
1642
|
+
_updateVelocities(deltaTimeSeconds) {
|
|
1643
|
+
const frictionFactor = Math.pow(
|
|
1644
|
+
0.5,
|
|
1645
|
+
deltaTimeSeconds / this._frictionHalfLife
|
|
1646
|
+
);
|
|
1647
|
+
for (let y = 0; y < this._spatialPartition.cells.length; y++) {
|
|
1648
|
+
for (let x = 0; x < this._spatialPartition.cells[y].length; x++) {
|
|
1649
|
+
const cell = this._spatialPartition.cells[y][x];
|
|
1650
|
+
for (let i = 0; i < cell.length; i++) {
|
|
1651
|
+
let fx = 0;
|
|
1652
|
+
let fy = 0;
|
|
1653
|
+
const p1 = cell[i];
|
|
1654
|
+
const neighborCells = this._spatialPartition.getCellNeighbors(x, y);
|
|
1655
|
+
for (const neighborCell of neighborCells) {
|
|
1656
|
+
for (let j = 0; j < neighborCell.length; j++) {
|
|
1657
|
+
if (cell === neighborCell && i === j) continue;
|
|
1658
|
+
const p2 = neighborCell[j];
|
|
1659
|
+
let rx = p2.x - p1.x;
|
|
1660
|
+
if (Math.abs(rx) > 0.5) rx = rx > 0 ? rx - 1 : rx + 1;
|
|
1661
|
+
let ry = p2.y - p1.y;
|
|
1662
|
+
if (Math.abs(ry) > 0.5) ry = ry > 0 ? ry - 1 : ry + 1;
|
|
1663
|
+
const d = Math.hypot(rx, ry);
|
|
1664
|
+
if (d > 0 && d < this._range) {
|
|
1665
|
+
const f = this._calcForce(
|
|
1666
|
+
d / this._range,
|
|
1667
|
+
this._attractionMatrix[p1.color][p2.color]
|
|
1668
|
+
);
|
|
1669
|
+
fx += rx / d * f;
|
|
1670
|
+
fy += ry / d * f;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
fx *= this._range * this._rangeFactor;
|
|
1675
|
+
fy *= this._range * this._rangeFactor;
|
|
1676
|
+
p1.vx *= frictionFactor;
|
|
1677
|
+
p1.vy *= frictionFactor;
|
|
1678
|
+
p1.vx += fx;
|
|
1679
|
+
p1.vy += fy;
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
_updatePositions(deltaTimeSeconds, renderer) {
|
|
1685
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
1686
|
+
const particle = this._particles[i];
|
|
1687
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
1688
|
+
particle.x += particle.vx * deltaTimeSeconds;
|
|
1689
|
+
particle.y += particle.vy * deltaTimeSeconds;
|
|
1690
|
+
if (particle.x < 0) particle.x = 1 + particle.x % 1;
|
|
1691
|
+
if (particle.x > 1) particle.x = particle.x % 1;
|
|
1692
|
+
if (particle.y < 0) particle.y = 1 + particle.y % 1;
|
|
1693
|
+
if (particle.y > 1) particle.y = particle.y % 1;
|
|
1694
|
+
renderer.setPixel(
|
|
1695
|
+
particle.x * renderer.width,
|
|
1696
|
+
particle.y * renderer.height,
|
|
1697
|
+
colors[particle.color],
|
|
1698
|
+
this._particleSize
|
|
1699
|
+
);
|
|
1700
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
1701
|
+
if (newCell !== oldCell) {
|
|
1702
|
+
const index = oldCell.indexOf(particle);
|
|
1703
|
+
if (index !== -1) {
|
|
1704
|
+
oldCell.splice(index, 1);
|
|
1705
|
+
}
|
|
1706
|
+
newCell.push(particle);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
update(renderer, dt) {
|
|
1711
|
+
this._updateVelocities(dt);
|
|
1712
|
+
this._updatePositions(dt, renderer);
|
|
1713
|
+
}
|
|
1714
|
+
};
|
|
1715
|
+
|
|
1716
|
+
// src/poisson-disk-sampler.ts
|
|
1717
|
+
function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
|
|
1718
|
+
const k = maxPoints;
|
|
1719
|
+
const cellSize = minDist / Math.SQRT2;
|
|
1720
|
+
const gridWidth = Math.ceil(width / cellSize);
|
|
1721
|
+
const gridHeight = Math.ceil(height / cellSize);
|
|
1722
|
+
const grid = Array.from(
|
|
1723
|
+
{ length: gridWidth },
|
|
1724
|
+
() => Array(gridHeight).fill(null)
|
|
1725
|
+
);
|
|
1726
|
+
const samples = [];
|
|
1727
|
+
const active = [];
|
|
1728
|
+
function gridIndex(p) {
|
|
1729
|
+
return {
|
|
1730
|
+
i: Math.floor(p.x / cellSize),
|
|
1731
|
+
j: Math.floor(p.y / cellSize)
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
function isFarEnough(p) {
|
|
1735
|
+
const { i, j } = gridIndex(p);
|
|
1736
|
+
for (let x = i - 2; x <= i + 2; x++) {
|
|
1737
|
+
for (let y = j - 2; y <= j + 2; y++) {
|
|
1738
|
+
if (x < 0 || y < 0 || x >= gridWidth || y >= gridHeight) continue;
|
|
1739
|
+
const neighbor = grid[x][y];
|
|
1740
|
+
if (!neighbor) continue;
|
|
1741
|
+
const dx = neighbor.x - p.x;
|
|
1742
|
+
const dy = neighbor.y - p.y;
|
|
1743
|
+
if (dx * dx + dy * dy < minDist * minDist) {
|
|
1744
|
+
return false;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
return true;
|
|
1749
|
+
}
|
|
1750
|
+
const initial = {
|
|
1751
|
+
x: rng.range(0, width),
|
|
1752
|
+
y: rng.range(0, height)
|
|
1753
|
+
};
|
|
1754
|
+
const { i: i0, j: j0 } = gridIndex(initial);
|
|
1755
|
+
grid[i0][j0] = initial;
|
|
1756
|
+
samples.push(initial);
|
|
1757
|
+
active.push(initial);
|
|
1758
|
+
while (active.length) {
|
|
1759
|
+
const index = Math.floor(rng.range(0, active.length));
|
|
1760
|
+
const point = active[index];
|
|
1761
|
+
let found = false;
|
|
1762
|
+
for (let n = 0; n < k; n++) {
|
|
1763
|
+
const angle = rng.range(0, Math.PI * 2);
|
|
1764
|
+
const dist = rng.range(minDist, 2 * minDist);
|
|
1765
|
+
const newPoint = {
|
|
1766
|
+
x: point.x + Math.cos(angle) * dist,
|
|
1767
|
+
y: point.y + Math.sin(angle) * dist
|
|
1768
|
+
};
|
|
1769
|
+
if (newPoint.x >= 0 && newPoint.y >= 0 && newPoint.x < width && newPoint.y < height && isFarEnough(newPoint)) {
|
|
1770
|
+
const { i, j } = gridIndex(newPoint);
|
|
1771
|
+
grid[i][j] = newPoint;
|
|
1772
|
+
samples.push(newPoint);
|
|
1773
|
+
active.push(newPoint);
|
|
1774
|
+
found = true;
|
|
1775
|
+
break;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
if (!found) {
|
|
1779
|
+
active.splice(index, 1);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
return samples;
|
|
1783
|
+
}
|
|
1784
|
+
|
|
685
1785
|
// src/renderer/canvas-renderer.ts
|
|
686
|
-
import { vec2 as vec23 } from "gl-matrix";
|
|
687
1786
|
var CanvasRenderer = class {
|
|
688
1787
|
ctx;
|
|
689
1788
|
clearColor = "#000000";
|
|
@@ -698,7 +1797,7 @@ var CanvasRenderer = class {
|
|
|
698
1797
|
return this.ctx?.canvas.height ?? 0;
|
|
699
1798
|
}
|
|
700
1799
|
get center() {
|
|
701
|
-
return
|
|
1800
|
+
return [this.width / 2, this.height / 2];
|
|
702
1801
|
}
|
|
703
1802
|
constructor(canvas) {
|
|
704
1803
|
this.ctx = (canvas ?? document.createElement("canvas")).getContext("2d");
|
|
@@ -829,16 +1928,16 @@ function CompileShader(gl, type, source) {
|
|
|
829
1928
|
}
|
|
830
1929
|
return shader;
|
|
831
1930
|
}
|
|
832
|
-
function CreateProgram(gl,
|
|
833
|
-
if (typeof
|
|
834
|
-
|
|
1931
|
+
function CreateProgram(gl, vertexShader2, fragmentShader2) {
|
|
1932
|
+
if (typeof vertexShader2 === "string") {
|
|
1933
|
+
vertexShader2 = CompileShader(gl, ShaderType.VERTEX, vertexShader2);
|
|
835
1934
|
}
|
|
836
|
-
if (typeof
|
|
837
|
-
|
|
1935
|
+
if (typeof fragmentShader2 === "string") {
|
|
1936
|
+
fragmentShader2 = CompileShader(gl, ShaderType.FRAGMENT, fragmentShader2);
|
|
838
1937
|
}
|
|
839
1938
|
const program = gl.createProgram();
|
|
840
|
-
gl.attachShader(program,
|
|
841
|
-
gl.attachShader(program,
|
|
1939
|
+
gl.attachShader(program, vertexShader2);
|
|
1940
|
+
gl.attachShader(program, fragmentShader2);
|
|
842
1941
|
gl.linkProgram(program);
|
|
843
1942
|
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
844
1943
|
throw `CreateProgram: failed to link program: ${gl.getProgramInfoLog(program)}`;
|
|
@@ -1111,6 +2210,69 @@ var SimplexNoise = class {
|
|
|
1111
2210
|
}
|
|
1112
2211
|
};
|
|
1113
2212
|
|
|
2213
|
+
// src/spatial-partition-3d.ts
|
|
2214
|
+
var SpatialPartition3d = class {
|
|
2215
|
+
cells;
|
|
2216
|
+
cellSize;
|
|
2217
|
+
entities;
|
|
2218
|
+
constructor(cellSize, entities) {
|
|
2219
|
+
this.cellSize = cellSize;
|
|
2220
|
+
this.entities = entities;
|
|
2221
|
+
const gridSize = Math.ceil(1 / cellSize);
|
|
2222
|
+
this.cells = Array.from(
|
|
2223
|
+
{ length: gridSize },
|
|
2224
|
+
() => Array.from(
|
|
2225
|
+
{ length: gridSize },
|
|
2226
|
+
() => Array.from({ length: gridSize }, () => [])
|
|
2227
|
+
)
|
|
2228
|
+
);
|
|
2229
|
+
for (const entity of this.entities) {
|
|
2230
|
+
this.addEntity(entity);
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
getCell(x, y, z) {
|
|
2234
|
+
const cx = Math.floor(x / this.cellSize);
|
|
2235
|
+
const cy = Math.floor(y / this.cellSize);
|
|
2236
|
+
const cz = Math.floor(z / this.cellSize);
|
|
2237
|
+
return this.cells[cz][cy][cx];
|
|
2238
|
+
}
|
|
2239
|
+
getCellNeighbors(cx, cy, cz) {
|
|
2240
|
+
const neighbors = [];
|
|
2241
|
+
const gridSize = this.cells.length;
|
|
2242
|
+
for (let dz = -1; dz <= 1; dz++) {
|
|
2243
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
2244
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
2245
|
+
let nx = (cx + dx + gridSize) % gridSize;
|
|
2246
|
+
let ny = (cy + dy + gridSize) % gridSize;
|
|
2247
|
+
let nz = (cz + dz + gridSize) % gridSize;
|
|
2248
|
+
neighbors.push(this.cells[nz][ny][nx]);
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
return neighbors;
|
|
2253
|
+
}
|
|
2254
|
+
addEntity(entity) {
|
|
2255
|
+
const cell = this.getCell(entity.x, entity.y, entity.z);
|
|
2256
|
+
cell.push(entity);
|
|
2257
|
+
}
|
|
2258
|
+
};
|
|
2259
|
+
|
|
2260
|
+
// src/tilemap.ts
|
|
2261
|
+
var Tilemap = class {
|
|
2262
|
+
map = /* @__PURE__ */ new Map();
|
|
2263
|
+
_key(x, y) {
|
|
2264
|
+
x = floor(x);
|
|
2265
|
+
y = floor(y);
|
|
2266
|
+
return `${x}x${y}`;
|
|
2267
|
+
}
|
|
2268
|
+
get(x, y) {
|
|
2269
|
+
return this.map.get(this._key(x, y));
|
|
2270
|
+
}
|
|
2271
|
+
set(x, y, v) {
|
|
2272
|
+
this.map.set(this._key(x, y), v);
|
|
2273
|
+
}
|
|
2274
|
+
};
|
|
2275
|
+
|
|
1114
2276
|
// src/three/cube-sphere-geometry.ts
|
|
1115
2277
|
import { BoxGeometry, Vector3 } from "three";
|
|
1116
2278
|
var CubeSphereGeometry = class extends BoxGeometry {
|
|
@@ -1130,6 +2292,8 @@ var CubeSphereGeometry = class extends BoxGeometry {
|
|
|
1130
2292
|
}
|
|
1131
2293
|
attrPositions.needsUpdate = true;
|
|
1132
2294
|
}
|
|
2295
|
+
size;
|
|
2296
|
+
segments;
|
|
1133
2297
|
_halfSize;
|
|
1134
2298
|
applyFractalBrownianMotion(octaves, persistence, scale, amplitude) {
|
|
1135
2299
|
const attrPositions = this.attributes.position;
|
|
@@ -1159,15 +2323,15 @@ var CubeSphereGeometry = class extends BoxGeometry {
|
|
|
1159
2323
|
};
|
|
1160
2324
|
|
|
1161
2325
|
// src/three/texture-atlas.ts
|
|
1162
|
-
import {
|
|
1163
|
-
import { TextureLoader, MeshLambertMaterial, NearestFilter } from "three";
|
|
2326
|
+
import { TextureLoader, MeshLambertMaterial, NearestFilter, RepeatWrapping } from "three";
|
|
1164
2327
|
function LoadTexture(url) {
|
|
1165
2328
|
return new Promise((res, rej) => {
|
|
1166
2329
|
new TextureLoader().load(
|
|
1167
2330
|
url,
|
|
1168
2331
|
(texture) => {
|
|
1169
|
-
const
|
|
1170
|
-
const
|
|
2332
|
+
const data = texture.source.data;
|
|
2333
|
+
const width = data.width;
|
|
2334
|
+
const height = data.height;
|
|
1171
2335
|
res({ width, height, texture });
|
|
1172
2336
|
},
|
|
1173
2337
|
(_ev) => {
|
|
@@ -1176,6 +2340,22 @@ function LoadTexture(url) {
|
|
|
1176
2340
|
);
|
|
1177
2341
|
});
|
|
1178
2342
|
}
|
|
2343
|
+
function CreateSubTexture(texture, x, y, w, h) {
|
|
2344
|
+
texture = texture.clone();
|
|
2345
|
+
texture.magFilter = NearestFilter;
|
|
2346
|
+
texture.wrapS = RepeatWrapping;
|
|
2347
|
+
const data = texture.source.data;
|
|
2348
|
+
const textureWidth = data.width;
|
|
2349
|
+
const textureHeight = data.height;
|
|
2350
|
+
w /= textureWidth;
|
|
2351
|
+
h /= textureHeight;
|
|
2352
|
+
x /= textureWidth;
|
|
2353
|
+
y = 1 - h - y / textureHeight;
|
|
2354
|
+
texture.offset.set(x, y);
|
|
2355
|
+
texture.repeat.set(w, h);
|
|
2356
|
+
texture.needsUpdate = true;
|
|
2357
|
+
return texture;
|
|
2358
|
+
}
|
|
1179
2359
|
var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
1180
2360
|
constructor(textureData, textureWidth, textureHeight = textureWidth, params) {
|
|
1181
2361
|
super({
|
|
@@ -1191,13 +2371,16 @@ var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
|
1191
2371
|
this._uh = this.textureHeight / this.textureData.height;
|
|
1192
2372
|
this.textureData.texture.magFilter = NearestFilter;
|
|
1193
2373
|
}
|
|
2374
|
+
textureData;
|
|
2375
|
+
textureWidth;
|
|
2376
|
+
textureHeight;
|
|
1194
2377
|
_uw;
|
|
1195
2378
|
_uh;
|
|
1196
2379
|
getUv(voxel, ux, uy) {
|
|
1197
|
-
return
|
|
2380
|
+
return [
|
|
1198
2381
|
(voxel + ux) * this._uw,
|
|
1199
2382
|
1 - (1 - uy) * this._uh
|
|
1200
|
-
|
|
2383
|
+
];
|
|
1201
2384
|
}
|
|
1202
2385
|
static CreateFromUrl(url, textureWidth, textureHeight = textureWidth, params) {
|
|
1203
2386
|
return LoadTexture(url).then((textureData) => new _TextureAtlas(
|
|
@@ -1208,6 +2391,61 @@ var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
|
1208
2391
|
));
|
|
1209
2392
|
}
|
|
1210
2393
|
};
|
|
2394
|
+
var AtlasTextureMaterial = class _AtlasTextureMaterial extends MeshLambertMaterial {
|
|
2395
|
+
textureData;
|
|
2396
|
+
tilePixelWidth;
|
|
2397
|
+
tilePixelHeight;
|
|
2398
|
+
_tileCount;
|
|
2399
|
+
_uvWidth;
|
|
2400
|
+
_uvHeight;
|
|
2401
|
+
_textureOffsetWidth;
|
|
2402
|
+
_textureOffsetHeight;
|
|
2403
|
+
constructor(textureData, atlasParams = {}, materialParams = {}) {
|
|
2404
|
+
super({
|
|
2405
|
+
map: textureData.texture,
|
|
2406
|
+
alphaTest: 0.1,
|
|
2407
|
+
transparent: true,
|
|
2408
|
+
name: "AtlasTextureMaterial",
|
|
2409
|
+
...materialParams
|
|
2410
|
+
});
|
|
2411
|
+
this.textureData = textureData;
|
|
2412
|
+
this.textureData.texture.magFilter = NearestFilter;
|
|
2413
|
+
this.tilePixelWidth = atlasParams.tilePixelWidth ?? 16;
|
|
2414
|
+
this.tilePixelHeight = atlasParams.tilePixelHeight ?? 16;
|
|
2415
|
+
this._textureOffsetWidth = atlasParams.tilePixelOffsetWidth ?? 0;
|
|
2416
|
+
this._textureOffsetHeight = atlasParams.tilePixelOffsetHeight ?? 0;
|
|
2417
|
+
this._tileCount = Math.floor(this.textureData.width / this.tilePixelWidth);
|
|
2418
|
+
this._uvWidth = this.tilePixelWidth / (this.textureData.width - this._textureOffsetWidth);
|
|
2419
|
+
this._uvHeight = this.tilePixelHeight / (this.textureData.height - this._textureOffsetHeight);
|
|
2420
|
+
}
|
|
2421
|
+
getTileUVs(tileIndex, vertexIndex) {
|
|
2422
|
+
const col = tileIndex % this._tileCount;
|
|
2423
|
+
const row = Math.floor(tileIndex / this._tileCount);
|
|
2424
|
+
const baseU = this._textureOffsetWidth / this.textureData.width + col * this._uvWidth;
|
|
2425
|
+
const baseV = this._textureOffsetHeight / this.textureData.height + row * this._uvHeight;
|
|
2426
|
+
const uvCorners = [
|
|
2427
|
+
[0, 0],
|
|
2428
|
+
// bottom-left
|
|
2429
|
+
[1, 0],
|
|
2430
|
+
// bottom-right
|
|
2431
|
+
[0, 1],
|
|
2432
|
+
// top-left
|
|
2433
|
+
[1, 1]
|
|
2434
|
+
// top-right
|
|
2435
|
+
];
|
|
2436
|
+
const [u, v] = uvCorners[vertexIndex];
|
|
2437
|
+
const uvx = baseU + u * this._uvWidth;
|
|
2438
|
+
const uvy = 1 - (baseV + v * this._uvHeight);
|
|
2439
|
+
return [uvx, uvy];
|
|
2440
|
+
}
|
|
2441
|
+
static fromUrl(url, atlasParams = {}, materialParams = {}) {
|
|
2442
|
+
return LoadTexture(url).then((textureData) => new _AtlasTextureMaterial(
|
|
2443
|
+
textureData,
|
|
2444
|
+
atlasParams,
|
|
2445
|
+
materialParams
|
|
2446
|
+
));
|
|
2447
|
+
}
|
|
2448
|
+
};
|
|
1211
2449
|
|
|
1212
2450
|
// src/three/threejs-boiler-plate.ts
|
|
1213
2451
|
import { AmbientLight, BoxGeometry as BoxGeometry2, DirectionalLight, GridHelper, Mesh, MeshLambertMaterial as MeshLambertMaterial2, PlaneGeometry, Raycaster, Scene, Vector2, WebGLRenderer } from "three";
|
|
@@ -1229,7 +2467,7 @@ var ThreeJsCameraRig = class extends Group {
|
|
|
1229
2467
|
super();
|
|
1230
2468
|
this.gimbal = new Group();
|
|
1231
2469
|
this.add(this.gimbal);
|
|
1232
|
-
this.camera = new PerspectiveCamera(
|
|
2470
|
+
this.camera = params?.camera ?? new PerspectiveCamera(
|
|
1233
2471
|
params?.fov ?? 75,
|
|
1234
2472
|
params?.aspect ?? 2,
|
|
1235
2473
|
params?.near ?? 0.1,
|
|
@@ -1262,6 +2500,7 @@ var ThreeJsBoilerPlate = class {
|
|
|
1262
2500
|
raycaster = new Raycaster();
|
|
1263
2501
|
renderer;
|
|
1264
2502
|
cameraRig;
|
|
2503
|
+
orbitControls;
|
|
1265
2504
|
get canvas() {
|
|
1266
2505
|
return this.renderer.domElement;
|
|
1267
2506
|
}
|
|
@@ -1274,14 +2513,38 @@ var ThreeJsBoilerPlate = class {
|
|
|
1274
2513
|
this.renderer = new WebGLRenderer(params?.renderer);
|
|
1275
2514
|
this.cameraRig = new ThreeJsCameraRig(params?.camera);
|
|
1276
2515
|
this.scene.add(this.cameraRig);
|
|
1277
|
-
|
|
2516
|
+
if (params?.parentElement) {
|
|
2517
|
+
this.appendTo(params.parentElement);
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
// Mutually exclusive with enableOrbitControls() - pick one. Gives you
|
|
2521
|
+
// manual orbit()/dolly() with tilt clamping and target-following, driven
|
|
2522
|
+
// by this.input's mouse events.
|
|
2523
|
+
enableCameraRigControls() {
|
|
2524
|
+
this.orbitControls?.dispose();
|
|
2525
|
+
this.orbitControls = void 0;
|
|
2526
|
+
this.input.on("mouse_move", ({ deltaX, deltaY }) => {
|
|
1278
2527
|
if (this.input.isMouseButtonDown(0)) {
|
|
1279
2528
|
this.cameraRig.orbit(deltaX, deltaY);
|
|
1280
2529
|
}
|
|
1281
2530
|
}).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
2531
|
+
}
|
|
2532
|
+
// Mutually exclusive with enableCameraRigControls() - pick one. Uses
|
|
2533
|
+
// Three's own OrbitControls instead of the hand-rolled rig math; call
|
|
2534
|
+
// update() once per frame in your render loop while this is active.
|
|
2535
|
+
// Async because OrbitControls only ships as an ESM-only addon module -
|
|
2536
|
+
// a dynamic import avoids forcing this whole package (built as both
|
|
2537
|
+
// CJS and ESM) to statically import an ESM-only file.
|
|
2538
|
+
async enableOrbitControls() {
|
|
2539
|
+
const { OrbitControls } = await import("three/examples/jsm/Addons.js");
|
|
2540
|
+
this.orbitControls = new OrbitControls(this.camera, this.canvas);
|
|
2541
|
+
this.orbitControls.minDistance = 0.5;
|
|
2542
|
+
this.orbitControls.maxDistance = 100;
|
|
2543
|
+
this.orbitControls.update();
|
|
2544
|
+
}
|
|
2545
|
+
// Call once per frame if enableOrbitControls() is active - no-op otherwise.
|
|
2546
|
+
update() {
|
|
2547
|
+
this.orbitControls?.update();
|
|
1285
2548
|
}
|
|
1286
2549
|
appendTo(htmlElement) {
|
|
1287
2550
|
if (this.canvas.parentElement) {
|
|
@@ -1310,6 +2573,9 @@ var ThreeJsBoilerPlate = class {
|
|
|
1310
2573
|
if (params.ambientLight !== false) this.scene.add(new AmbientLight());
|
|
1311
2574
|
if (params.directionalLight !== false) this.scene.add(new DirectionalLight());
|
|
1312
2575
|
if (params.gridHelper !== false) this.scene.add(new GridHelper(100, 100, 16711680));
|
|
2576
|
+
if (params.enableControls === true) {
|
|
2577
|
+
this.enableCameraRigControls();
|
|
2578
|
+
}
|
|
1313
2579
|
}
|
|
1314
2580
|
pick() {
|
|
1315
2581
|
const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
|
|
@@ -1343,7 +2609,7 @@ var ThreeJsPlayerController = class extends Group2 {
|
|
|
1343
2609
|
constructor(camera) {
|
|
1344
2610
|
super();
|
|
1345
2611
|
this.velocity = new Vector32();
|
|
1346
|
-
this.cameraRig = new ThreeJsCameraRig(camera);
|
|
2612
|
+
this.cameraRig = new ThreeJsCameraRig({ camera });
|
|
1347
2613
|
this.add(this.cameraRig);
|
|
1348
2614
|
}
|
|
1349
2615
|
update(deltaTime) {
|
|
@@ -1353,93 +2619,106 @@ var ThreeJsPlayerController = class extends Group2 {
|
|
|
1353
2619
|
}
|
|
1354
2620
|
};
|
|
1355
2621
|
|
|
1356
|
-
// src/three/voxel-
|
|
1357
|
-
import { BufferAttribute, BufferGeometry } from "three";
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
colors = [
|
|
1368
|
-
[1, 0, 0, 1],
|
|
1369
|
-
// Red
|
|
1370
|
-
[0, 1, 0, 1],
|
|
1371
|
-
// Green
|
|
1372
|
-
[0, 0, 1, 1]
|
|
1373
|
-
// Blue
|
|
1374
|
-
];
|
|
1375
|
-
constructor() {
|
|
2622
|
+
// src/three/voxel-mesh.ts
|
|
2623
|
+
import { BufferAttribute, BufferGeometry, Color as Color2, Mesh as Mesh2 } from "three";
|
|
2624
|
+
var VoxelMesh = class extends Mesh2 {
|
|
2625
|
+
type = "VoxelMesh";
|
|
2626
|
+
width;
|
|
2627
|
+
depth;
|
|
2628
|
+
height;
|
|
2629
|
+
colorPalette;
|
|
2630
|
+
voxels;
|
|
2631
|
+
needsGeometryUpdate = true;
|
|
2632
|
+
constructor(width, depth, height, material, name) {
|
|
1376
2633
|
super();
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
2634
|
+
this.width = width;
|
|
2635
|
+
this.depth = depth;
|
|
2636
|
+
this.height = height;
|
|
2637
|
+
this.material = material;
|
|
2638
|
+
this.material.vertexColors = true;
|
|
2639
|
+
this.name = name ?? "VoxelMesh";
|
|
2640
|
+
this.colorPalette = BuildDefaultPalette();
|
|
2641
|
+
this.voxels = /* @__PURE__ */ new Map();
|
|
2642
|
+
this.geometry = new BufferGeometry();
|
|
1385
2643
|
this.updateGeometry();
|
|
1386
2644
|
}
|
|
2645
|
+
_xyz2k(x, y, z) {
|
|
2646
|
+
x = Math.abs(Math.floor(x));
|
|
2647
|
+
y = Math.abs(Math.floor(y));
|
|
2648
|
+
z = Math.abs(Math.floor(z));
|
|
2649
|
+
return `${x},${y},${z}`;
|
|
2650
|
+
}
|
|
2651
|
+
_k2xyz(k) {
|
|
2652
|
+
const [x, y, z] = k.split(",").map((s) => parseInt(s));
|
|
2653
|
+
return { x, y, z };
|
|
2654
|
+
}
|
|
1387
2655
|
get(x, y, z) {
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
2656
|
+
let n = -1;
|
|
2657
|
+
if (x >= 0 && y >= 0 && z >= 0 && x < this.width && y < this.height && z < this.depth) {
|
|
2658
|
+
n = this.voxels.get(this._xyz2k(x, y, z)) ?? -1;
|
|
2659
|
+
}
|
|
2660
|
+
return n;
|
|
2661
|
+
}
|
|
2662
|
+
set(x, y, z, n) {
|
|
2663
|
+
if (x >= 0 && y >= 0 && z >= 0 && x < this.width && y < this.height && z < this.depth) {
|
|
2664
|
+
if (n === void 0) this.voxels.delete(this._xyz2k(x, y, z));
|
|
2665
|
+
else this.voxels.set(this._xyz2k(x, y, z), n);
|
|
2666
|
+
this.needsGeometryUpdate = true;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
clearVoxels() {
|
|
2670
|
+
this.voxels.clear();
|
|
2671
|
+
}
|
|
2672
|
+
setSize(width, depth, height) {
|
|
2673
|
+
this.forEachVoxel((x, y, z) => {
|
|
2674
|
+
if (x >= width || y >= height || z >= depth) this.set(x, y, z);
|
|
2675
|
+
});
|
|
2676
|
+
this.width = width;
|
|
2677
|
+
this.depth = depth;
|
|
2678
|
+
this.height = height;
|
|
2679
|
+
this.needsGeometryUpdate = true;
|
|
2680
|
+
}
|
|
2681
|
+
forEachVoxel(callback) {
|
|
2682
|
+
this.voxels.forEach((n, k) => {
|
|
2683
|
+
const { x, y, z } = this._k2xyz(k);
|
|
2684
|
+
callback(x, y, z, n);
|
|
2685
|
+
});
|
|
2686
|
+
}
|
|
2687
|
+
importVoxelData(voxelData) {
|
|
2688
|
+
this.clearVoxels();
|
|
2689
|
+
this.name = voxelData.name;
|
|
2690
|
+
this.setSize(voxelData.width, voxelData.depth, voxelData.height);
|
|
2691
|
+
voxelData.palette.forEach((color, i) => {
|
|
2692
|
+
if (i < this.colorPalette.length) {
|
|
2693
|
+
this.colorPalette[i] = color;
|
|
2694
|
+
} else {
|
|
2695
|
+
this.colorPalette.push(color);
|
|
2696
|
+
}
|
|
2697
|
+
});
|
|
2698
|
+
voxelData.voxels.forEach((voxel) => {
|
|
2699
|
+
const { x, y, z, color } = voxel;
|
|
2700
|
+
this.set(x, y, z, color);
|
|
2701
|
+
});
|
|
2702
|
+
this.updateGeometry();
|
|
2703
|
+
}
|
|
1416
2704
|
updateGeometry() {
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
if (voxel) {
|
|
1424
|
-
const [x, y, z] = key2xyz(key);
|
|
2705
|
+
if (this.needsGeometryUpdate) {
|
|
2706
|
+
const positions = [];
|
|
2707
|
+
const normals = [];
|
|
2708
|
+
const colors2 = [];
|
|
2709
|
+
const indices = [];
|
|
2710
|
+
this.forEachVoxel((x, y, z, n) => {
|
|
1425
2711
|
VoxelFaces.forEach((faces) => {
|
|
1426
2712
|
const [dx, dy, dz] = faces[0];
|
|
1427
|
-
const neighborVoxel = this.get(x + dx, y + dy, z + dz)
|
|
1428
|
-
if (
|
|
2713
|
+
const neighborVoxel = this.get(x + dx, y + dy, z + dz);
|
|
2714
|
+
if (neighborVoxel < 0) {
|
|
1429
2715
|
const positionIndex = positions.length / 3;
|
|
1430
|
-
faces.forEach((
|
|
1431
|
-
const [
|
|
1432
|
-
nx,
|
|
1433
|
-
ny,
|
|
1434
|
-
nz,
|
|
1435
|
-
px,
|
|
1436
|
-
py,
|
|
1437
|
-
pz
|
|
1438
|
-
/*ux, uy*/
|
|
1439
|
-
] = faceVerts;
|
|
2716
|
+
faces.forEach((face) => {
|
|
2717
|
+
const [nx, ny, nz, px, py, pz] = face;
|
|
1440
2718
|
positions.push(x + px, y + py, z + pz);
|
|
1441
2719
|
normals.push(nx, ny, nz);
|
|
1442
|
-
|
|
2720
|
+
const { r, g, b } = new Color2(this.colorPalette[n]);
|
|
2721
|
+
colors2.push(r, g, b, 1);
|
|
1443
2722
|
});
|
|
1444
2723
|
indices.push(
|
|
1445
2724
|
positionIndex,
|
|
@@ -1449,105 +2728,747 @@ var VoxelGeometry = class extends BufferGeometry {
|
|
|
1449
2728
|
positionIndex + 1,
|
|
1450
2729
|
positionIndex + 3
|
|
1451
2730
|
);
|
|
1452
|
-
needsUpdate = true;
|
|
1453
2731
|
}
|
|
1454
2732
|
});
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
this.attributes.position.needsUpdate = true;
|
|
1463
|
-
this.computeVertexNormals();
|
|
2733
|
+
});
|
|
2734
|
+
this.geometry.setAttribute("position", new BufferAttribute(new Float32Array(positions), 3));
|
|
2735
|
+
this.geometry.setAttribute("normal", new BufferAttribute(new Float32Array(normals), 3));
|
|
2736
|
+
this.geometry.setAttribute("color", new BufferAttribute(new Float32Array(colors2), 4));
|
|
2737
|
+
this.geometry.setIndex(indices);
|
|
2738
|
+
this.geometry.computeBoundingSphere();
|
|
2739
|
+
this.updateMatrixWorld();
|
|
1464
2740
|
}
|
|
2741
|
+
this.needsGeometryUpdate = false;
|
|
2742
|
+
}
|
|
2743
|
+
exportData() {
|
|
2744
|
+
const voxelData = {
|
|
2745
|
+
name: this.name,
|
|
2746
|
+
width: this.width,
|
|
2747
|
+
depth: this.depth,
|
|
2748
|
+
height: this.height,
|
|
2749
|
+
palette: [...this.colorPalette],
|
|
2750
|
+
voxels: []
|
|
2751
|
+
};
|
|
2752
|
+
this.forEachVoxel((x, y, z, color) => {
|
|
2753
|
+
voxelData.voxels.push({ x, y, z, color });
|
|
2754
|
+
});
|
|
2755
|
+
return voxelData;
|
|
1465
2756
|
}
|
|
1466
2757
|
};
|
|
1467
|
-
var lo = -0.5;
|
|
1468
|
-
var hi = 0.5;
|
|
1469
2758
|
var VoxelFaces = [
|
|
1470
|
-
// [nx, ny, nz, px, py, pz
|
|
2759
|
+
// [nx, ny, nz, px, py, pz]
|
|
1471
2760
|
[
|
|
1472
2761
|
//left
|
|
1473
|
-
[-1, 0, 0,
|
|
1474
|
-
[-1, 0, 0,
|
|
1475
|
-
[-1, 0, 0,
|
|
1476
|
-
[-1, 0, 0,
|
|
2762
|
+
[-1, 0, 0, 0, 1, 0],
|
|
2763
|
+
[-1, 0, 0, 0, 0, 0],
|
|
2764
|
+
[-1, 0, 0, 0, 1, 1],
|
|
2765
|
+
[-1, 0, 0, 0, 0, 1]
|
|
1477
2766
|
],
|
|
1478
2767
|
[
|
|
1479
2768
|
//right
|
|
1480
|
-
[1, 0, 0,
|
|
1481
|
-
[1, 0, 0,
|
|
1482
|
-
[1, 0, 0,
|
|
1483
|
-
[1, 0, 0,
|
|
2769
|
+
[1, 0, 0, 1, 1, 1],
|
|
2770
|
+
[1, 0, 0, 1, 0, 1],
|
|
2771
|
+
[1, 0, 0, 1, 1, 0],
|
|
2772
|
+
[1, 0, 0, 1, 0, 0]
|
|
1484
2773
|
],
|
|
1485
2774
|
[
|
|
1486
2775
|
//bottom
|
|
1487
|
-
[0, -1, 0,
|
|
1488
|
-
[0, -1, 0,
|
|
1489
|
-
[0, -1, 0,
|
|
1490
|
-
[0, -1, 0,
|
|
2776
|
+
[0, -1, 0, 1, 0, 1],
|
|
2777
|
+
[0, -1, 0, 0, 0, 1],
|
|
2778
|
+
[0, -1, 0, 1, 0, 0],
|
|
2779
|
+
[0, -1, 0, 0, 0, 0]
|
|
1491
2780
|
],
|
|
1492
2781
|
[
|
|
1493
2782
|
//top
|
|
1494
|
-
[0, 1, 0,
|
|
1495
|
-
[0, 1, 0,
|
|
1496
|
-
[0, 1, 0,
|
|
1497
|
-
[0, 1, 0,
|
|
2783
|
+
[0, 1, 0, 0, 1, 1],
|
|
2784
|
+
[0, 1, 0, 1, 1, 1],
|
|
2785
|
+
[0, 1, 0, 0, 1, 0],
|
|
2786
|
+
[0, 1, 0, 1, 1, 0]
|
|
1498
2787
|
],
|
|
1499
2788
|
[
|
|
1500
2789
|
//back
|
|
1501
|
-
[0, 0, -1,
|
|
1502
|
-
[0, 0, -1,
|
|
1503
|
-
[0, 0, -1,
|
|
1504
|
-
[0, 0, -1,
|
|
2790
|
+
[0, 0, -1, 1, 0, 0],
|
|
2791
|
+
[0, 0, -1, 0, 0, 0],
|
|
2792
|
+
[0, 0, -1, 1, 1, 0],
|
|
2793
|
+
[0, 0, -1, 0, 1, 0]
|
|
1505
2794
|
],
|
|
1506
2795
|
[
|
|
1507
2796
|
//front
|
|
1508
|
-
[0, 0, 1,
|
|
1509
|
-
[0, 0, 1,
|
|
1510
|
-
[0, 0, 1,
|
|
1511
|
-
[0, 0, 1,
|
|
2797
|
+
[0, 0, 1, 0, 0, 1],
|
|
2798
|
+
[0, 0, 1, 1, 0, 1],
|
|
2799
|
+
[0, 0, 1, 0, 1, 1],
|
|
2800
|
+
[0, 0, 1, 1, 1, 1]
|
|
1512
2801
|
]
|
|
1513
2802
|
];
|
|
2803
|
+
function BuildDefaultPalette(palette) {
|
|
2804
|
+
palette = palette ?? Colors.rgb48;
|
|
2805
|
+
const grayscale = [];
|
|
2806
|
+
const count = 16;
|
|
2807
|
+
for (let i = 0; i < count; i++) {
|
|
2808
|
+
const grey = Math.floor(i / count * 255);
|
|
2809
|
+
grayscale.push(`rgb(${grey}, ${grey}, ${grey})`);
|
|
2810
|
+
}
|
|
2811
|
+
return [...palette, ...grayscale];
|
|
2812
|
+
}
|
|
2813
|
+
var Colors = {
|
|
2814
|
+
rgb12: [
|
|
2815
|
+
"#FF0000",
|
|
2816
|
+
"#FF8800",
|
|
2817
|
+
"#FFFF00",
|
|
2818
|
+
"#88FF00",
|
|
2819
|
+
"#00FF00",
|
|
2820
|
+
"#00FF88",
|
|
2821
|
+
"#00FFFF",
|
|
2822
|
+
"#0088FF",
|
|
2823
|
+
"#0000FF",
|
|
2824
|
+
"#8800FF",
|
|
2825
|
+
"#FF00FF",
|
|
2826
|
+
"#FF0080"
|
|
2827
|
+
],
|
|
2828
|
+
rgb24: [
|
|
2829
|
+
"#FF0000",
|
|
2830
|
+
"#FF4000",
|
|
2831
|
+
"#FF8000",
|
|
2832
|
+
"#FFBF00",
|
|
2833
|
+
"#FFFF00",
|
|
2834
|
+
"#CCFF00",
|
|
2835
|
+
"#80FF00",
|
|
2836
|
+
"#40FF00",
|
|
2837
|
+
"#00FF00",
|
|
2838
|
+
"#00FF40",
|
|
2839
|
+
"#00FF80",
|
|
2840
|
+
"#00FFCC",
|
|
2841
|
+
"#00FFFF",
|
|
2842
|
+
"#00CCFF",
|
|
2843
|
+
"#0080FF",
|
|
2844
|
+
"#0040FF",
|
|
2845
|
+
"#0000FF",
|
|
2846
|
+
"#4000FF",
|
|
2847
|
+
"#8000FF",
|
|
2848
|
+
"#CC00FF",
|
|
2849
|
+
"#FF00FF",
|
|
2850
|
+
"#FF00CC",
|
|
2851
|
+
"#FF0080",
|
|
2852
|
+
"#FF0040"
|
|
2853
|
+
],
|
|
2854
|
+
rgb48: [
|
|
2855
|
+
"#FF0000",
|
|
2856
|
+
"#FF2000",
|
|
2857
|
+
"#FF4000",
|
|
2858
|
+
"#FF6000",
|
|
2859
|
+
"#FF8000",
|
|
2860
|
+
"#FFAA00",
|
|
2861
|
+
"#FFCC00",
|
|
2862
|
+
"#FFEE00",
|
|
2863
|
+
"#FFFF00",
|
|
2864
|
+
"#DDFF00",
|
|
2865
|
+
"#CCFF00",
|
|
2866
|
+
"#AAFF00",
|
|
2867
|
+
"#80FF00",
|
|
2868
|
+
"#60FF00",
|
|
2869
|
+
"#40FF00",
|
|
2870
|
+
"#20FF00",
|
|
2871
|
+
"#00FF00",
|
|
2872
|
+
"#00FF20",
|
|
2873
|
+
"#00FF40",
|
|
2874
|
+
"#00FF60",
|
|
2875
|
+
"#00FF80",
|
|
2876
|
+
"#00FFAA",
|
|
2877
|
+
"#00FFCC",
|
|
2878
|
+
"#00FFDD",
|
|
2879
|
+
"#00FFFF",
|
|
2880
|
+
"#00DDFF",
|
|
2881
|
+
"#00CCFF",
|
|
2882
|
+
"#0099FF",
|
|
2883
|
+
"#0080FF",
|
|
2884
|
+
"#0060FF",
|
|
2885
|
+
"#0040FF",
|
|
2886
|
+
"#0020FF",
|
|
2887
|
+
"#0000FF",
|
|
2888
|
+
"#2000FF",
|
|
2889
|
+
"#4000FF",
|
|
2890
|
+
"#6000FF",
|
|
2891
|
+
"#8000FF",
|
|
2892
|
+
"#AA00FF",
|
|
2893
|
+
"#CC00FF",
|
|
2894
|
+
"#DD00FF",
|
|
2895
|
+
"#FF00FF",
|
|
2896
|
+
"#FF00EE",
|
|
2897
|
+
"#FF00CC",
|
|
2898
|
+
"#FF00AA",
|
|
2899
|
+
"#FF0080",
|
|
2900
|
+
"#FF0060",
|
|
2901
|
+
"#FF0040",
|
|
2902
|
+
"#FF0020"
|
|
2903
|
+
],
|
|
2904
|
+
rgb64: [
|
|
2905
|
+
"#000000",
|
|
2906
|
+
"#000055",
|
|
2907
|
+
"#0000AA",
|
|
2908
|
+
"#0000FF",
|
|
2909
|
+
"#005500",
|
|
2910
|
+
"#005555",
|
|
2911
|
+
"#0055AA",
|
|
2912
|
+
"#0055FF",
|
|
2913
|
+
"#00AA00",
|
|
2914
|
+
"#00AA55",
|
|
2915
|
+
"#00AAAA",
|
|
2916
|
+
"#00AAFF",
|
|
2917
|
+
"#00FF00",
|
|
2918
|
+
"#00FF55",
|
|
2919
|
+
"#00FFAA",
|
|
2920
|
+
"#00FFFF",
|
|
2921
|
+
"#550000",
|
|
2922
|
+
"#550055",
|
|
2923
|
+
"#5500AA",
|
|
2924
|
+
"#5500FF",
|
|
2925
|
+
"#555500",
|
|
2926
|
+
"#555555",
|
|
2927
|
+
"#5555AA",
|
|
2928
|
+
"#5555FF",
|
|
2929
|
+
"#55AA00",
|
|
2930
|
+
"#55AA55",
|
|
2931
|
+
"#55AAAA",
|
|
2932
|
+
"#55AAFF",
|
|
2933
|
+
"#55FF00",
|
|
2934
|
+
"#55FF55",
|
|
2935
|
+
"#55FFAA",
|
|
2936
|
+
"#55FFFF",
|
|
2937
|
+
"#AA0000",
|
|
2938
|
+
"#AA0055",
|
|
2939
|
+
"#AA00AA",
|
|
2940
|
+
"#AA00FF",
|
|
2941
|
+
"#AA5500",
|
|
2942
|
+
"#AA5555",
|
|
2943
|
+
"#AA55AA",
|
|
2944
|
+
"#AA55FF",
|
|
2945
|
+
"#AAAA00",
|
|
2946
|
+
"#AAAA55",
|
|
2947
|
+
"#AAAAAA",
|
|
2948
|
+
"#AAAAFF",
|
|
2949
|
+
"#AAFF00",
|
|
2950
|
+
"#AAFF55",
|
|
2951
|
+
"#AAFFAA",
|
|
2952
|
+
"#AAFFFF",
|
|
2953
|
+
"#FF0000",
|
|
2954
|
+
"#FF0055",
|
|
2955
|
+
"#FF00AA",
|
|
2956
|
+
"#FF00FF",
|
|
2957
|
+
"#FF5500",
|
|
2958
|
+
"#FF5555",
|
|
2959
|
+
"#FF55AA",
|
|
2960
|
+
"#FF55FF",
|
|
2961
|
+
"#FFAA00",
|
|
2962
|
+
"#FFAA55",
|
|
2963
|
+
"#FFAAAA",
|
|
2964
|
+
"#FFAAFF",
|
|
2965
|
+
"#FFFF00",
|
|
2966
|
+
"#FFFF55",
|
|
2967
|
+
"#FFFFAA",
|
|
2968
|
+
"#FFFFFF"
|
|
2969
|
+
]
|
|
2970
|
+
};
|
|
2971
|
+
|
|
2972
|
+
// src/three/particle-system-3d.ts
|
|
2973
|
+
import { BufferGeometry as BufferGeometry2, Float32BufferAttribute, Group as Group3, Points, ShaderMaterial, Vector3 as Vector33 } from "three";
|
|
2974
|
+
var vertexShader = `
|
|
2975
|
+
uniform float pointSize;
|
|
2976
|
+
|
|
2977
|
+
varying vec4 vColor;
|
|
2978
|
+
|
|
2979
|
+
void main() {
|
|
2980
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
2981
|
+
|
|
2982
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
2983
|
+
gl_PointSize = pointSize;
|
|
2984
|
+
|
|
2985
|
+
vColor = color;
|
|
2986
|
+
}`;
|
|
2987
|
+
var fragmentShader = `
|
|
2988
|
+
varying vec4 vColor;
|
|
2989
|
+
|
|
2990
|
+
void main() {
|
|
2991
|
+
gl_FragColor = vColor;
|
|
2992
|
+
}`;
|
|
2993
|
+
var Colors2 = [
|
|
2994
|
+
[1, 0, 0, 1],
|
|
2995
|
+
// red
|
|
2996
|
+
[0, 1, 0, 1],
|
|
2997
|
+
// green,
|
|
2998
|
+
[0, 0, 1, 1],
|
|
2999
|
+
// blue,
|
|
3000
|
+
[1, 1, 0, 1],
|
|
3001
|
+
// yellow,
|
|
3002
|
+
[1, 0, 1, 1]
|
|
3003
|
+
// magenta,
|
|
3004
|
+
];
|
|
3005
|
+
var ParticleSystem3d = class extends Group3 {
|
|
3006
|
+
particleCount = 1e3;
|
|
3007
|
+
_geometry;
|
|
3008
|
+
_particles = [];
|
|
3009
|
+
_frictionHalfLife = 0.04;
|
|
3010
|
+
_range = 0.2;
|
|
3011
|
+
_rangeFactor = 0.1;
|
|
3012
|
+
_attractionMatrix;
|
|
3013
|
+
_spatialPartition;
|
|
3014
|
+
constructor() {
|
|
3015
|
+
super();
|
|
3016
|
+
const uniforms = {
|
|
3017
|
+
pointSize: { value: 4 }
|
|
3018
|
+
};
|
|
3019
|
+
this.position.x = -0.5;
|
|
3020
|
+
this.position.y = -0.5;
|
|
3021
|
+
this.position.z = -0.5;
|
|
3022
|
+
this._geometry = new BufferGeometry2();
|
|
3023
|
+
this._geometry.setAttribute("position", new Float32BufferAttribute([], 3));
|
|
3024
|
+
this._geometry.setAttribute("color", new Float32BufferAttribute([], 4));
|
|
3025
|
+
const material = new ShaderMaterial({
|
|
3026
|
+
uniforms,
|
|
3027
|
+
vertexShader,
|
|
3028
|
+
fragmentShader,
|
|
3029
|
+
depthTest: true,
|
|
3030
|
+
depthWrite: false,
|
|
3031
|
+
vertexColors: true
|
|
3032
|
+
});
|
|
3033
|
+
const pointsMesh = new Points(this._geometry, material);
|
|
3034
|
+
this.add(pointsMesh);
|
|
3035
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
3036
|
+
this._particles.push({
|
|
3037
|
+
x: rng.nextf,
|
|
3038
|
+
y: rng.nextf,
|
|
3039
|
+
z: rng.nextf,
|
|
3040
|
+
vx: 0,
|
|
3041
|
+
vy: 0,
|
|
3042
|
+
vz: 0,
|
|
3043
|
+
color: rng.range(Colors2.length)
|
|
3044
|
+
});
|
|
3045
|
+
}
|
|
3046
|
+
this._attractionMatrix = rng.randomMatrix(Colors2.length);
|
|
3047
|
+
this._spatialPartition = new SpatialPartition3d(this._range, this._particles);
|
|
3048
|
+
}
|
|
3049
|
+
_calcForce(r, a, beta = 0.3) {
|
|
3050
|
+
let f = 0;
|
|
3051
|
+
if (r < beta) {
|
|
3052
|
+
f = r / beta - 1;
|
|
3053
|
+
} else if (beta < r && r < 1) {
|
|
3054
|
+
f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
3055
|
+
}
|
|
3056
|
+
return f;
|
|
3057
|
+
}
|
|
3058
|
+
_updateVelocities(dt) {
|
|
3059
|
+
const frictionFactor = Math.pow(
|
|
3060
|
+
0.5,
|
|
3061
|
+
dt / this._frictionHalfLife
|
|
3062
|
+
);
|
|
3063
|
+
for (let z = 0; z < this._spatialPartition.cells.length; z++) {
|
|
3064
|
+
for (let y = 0; y < this._spatialPartition.cells[z].length; y++) {
|
|
3065
|
+
for (let x = 0; x < this._spatialPartition.cells[z][y].length; x++) {
|
|
3066
|
+
const cell = this._spatialPartition.cells[z][y][x];
|
|
3067
|
+
for (let i = 0; i < cell.length; i++) {
|
|
3068
|
+
let fx = 0, fy = 0, fz = 0;
|
|
3069
|
+
const p1 = cell[i];
|
|
3070
|
+
const neighbors = this._spatialPartition.getCellNeighbors(x, y, z);
|
|
3071
|
+
for (const neighborCell of neighbors) {
|
|
3072
|
+
for (let j = 0; j < neighborCell.length; j++) {
|
|
3073
|
+
if (cell === neighborCell && i === j) continue;
|
|
3074
|
+
const p2 = neighborCell[j];
|
|
3075
|
+
let dx = p2.x - p1.x;
|
|
3076
|
+
let dy = p2.y - p1.y;
|
|
3077
|
+
let dz = p2.z - p1.z;
|
|
3078
|
+
if (Math.abs(dx) > 0.5) dx = dx > 0 ? dx - 1 : dx + 1;
|
|
3079
|
+
if (Math.abs(dy) > 0.5) dy = dy > 0 ? dy - 1 : dy + 1;
|
|
3080
|
+
if (Math.abs(dz) > 0.5) dz = dz > 0 ? dz - 1 : dz + 1;
|
|
3081
|
+
const dist = Math.hypot(dx, dy, dz);
|
|
3082
|
+
if (dist > 0 && dist < this._range) {
|
|
3083
|
+
const f = this._calcForce(dist / this._range, this._attractionMatrix[p1.color][p2.color]);
|
|
3084
|
+
fx += dx / dist * f;
|
|
3085
|
+
fy += dy / dist * f;
|
|
3086
|
+
fz += dz / dist * f;
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
}
|
|
3090
|
+
fx *= this._range * this._rangeFactor;
|
|
3091
|
+
fy *= this._range * this._rangeFactor;
|
|
3092
|
+
fz *= this._range * this._rangeFactor;
|
|
3093
|
+
p1.vx *= frictionFactor;
|
|
3094
|
+
p1.vy *= frictionFactor;
|
|
3095
|
+
p1.vz *= frictionFactor;
|
|
3096
|
+
p1.vx += fx;
|
|
3097
|
+
p1.vy += fy;
|
|
3098
|
+
p1.vz += fz;
|
|
3099
|
+
}
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
_updatePositions(dt) {
|
|
3105
|
+
for (let i = 0; i < this._particles.length; i++) {
|
|
3106
|
+
const particle = this._particles[i];
|
|
3107
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
3108
|
+
particle.x += particle.vx * dt;
|
|
3109
|
+
particle.y += particle.vy * dt;
|
|
3110
|
+
particle.z += particle.vz * dt;
|
|
3111
|
+
if (particle.x < 0) particle.x = (particle.x + 1) % 1;
|
|
3112
|
+
if (particle.x > 1) particle.x = particle.x % 1;
|
|
3113
|
+
if (particle.y < 0) particle.y = (particle.y + 1) % 1;
|
|
3114
|
+
if (particle.y > 1) particle.y = particle.y % 1;
|
|
3115
|
+
if (particle.z < 0) particle.z = (particle.z + 1) % 1;
|
|
3116
|
+
if (particle.z > 1) particle.z = particle.z % 1;
|
|
3117
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
3118
|
+
if (newCell !== oldCell) {
|
|
3119
|
+
const index = oldCell.indexOf(particle);
|
|
3120
|
+
if (index !== -1) {
|
|
3121
|
+
oldCell.splice(index, 1);
|
|
3122
|
+
}
|
|
3123
|
+
newCell.push(particle);
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
_sortParticles(camera) {
|
|
3128
|
+
this._particles.sort((a, b) => {
|
|
3129
|
+
const d1 = camera.position.distanceTo(new Vector33(a.x, a.y, a.z));
|
|
3130
|
+
const d2 = camera.position.distanceTo(new Vector33(b.x, b.y, b.z));
|
|
3131
|
+
return d1 > d2 ? -1 : d1 > d1 ? 1 : 0;
|
|
3132
|
+
});
|
|
3133
|
+
}
|
|
3134
|
+
_updateGeometry() {
|
|
3135
|
+
const positions = [];
|
|
3136
|
+
const colors2 = [];
|
|
3137
|
+
for (const particle of this._particles) {
|
|
3138
|
+
positions.push(
|
|
3139
|
+
particle.x,
|
|
3140
|
+
particle.y,
|
|
3141
|
+
particle.z
|
|
3142
|
+
);
|
|
3143
|
+
colors2.push(
|
|
3144
|
+
Colors2[particle.color][0],
|
|
3145
|
+
Colors2[particle.color][1],
|
|
3146
|
+
Colors2[particle.color][2],
|
|
3147
|
+
Colors2[particle.color][3]
|
|
3148
|
+
);
|
|
3149
|
+
}
|
|
3150
|
+
this._geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
|
|
3151
|
+
this._geometry.setAttribute("color", new Float32BufferAttribute(colors2, 4));
|
|
3152
|
+
this._geometry.attributes.position.needsUpdate = true;
|
|
3153
|
+
}
|
|
3154
|
+
update(camera, dt) {
|
|
3155
|
+
this._updateVelocities(dt);
|
|
3156
|
+
this._updatePositions(dt);
|
|
3157
|
+
this._sortParticles(camera);
|
|
3158
|
+
this._updateGeometry();
|
|
3159
|
+
}
|
|
3160
|
+
};
|
|
3161
|
+
|
|
3162
|
+
// src/three/asteroid-mesh.ts
|
|
3163
|
+
import { IcosahedronGeometry, Mesh as Mesh3, MeshStandardMaterial as MeshStandardMaterial2 } from "three";
|
|
3164
|
+
import { createNoise3D as createNoise3D2 } from "simplex-noise";
|
|
3165
|
+
var AsteroidMesh = class extends Mesh3 {
|
|
3166
|
+
constructor(options = {}) {
|
|
3167
|
+
const radius = options.radius ?? 1;
|
|
3168
|
+
const detail = options.detail ?? 1;
|
|
3169
|
+
const noiseScale = options.noiseScale ?? 1;
|
|
3170
|
+
const displacement = options.displacement ?? 0.1;
|
|
3171
|
+
const secondaryNoise = options.secondaryNoise ?? false;
|
|
3172
|
+
const color = options.color ?? 8947848;
|
|
3173
|
+
const geometry = new IcosahedronGeometry(radius, detail);
|
|
3174
|
+
const material = new MeshStandardMaterial2({ color });
|
|
3175
|
+
super(geometry, material);
|
|
3176
|
+
const noise3d = createNoise3D2(() => rng.nextf);
|
|
3177
|
+
const noise3d2 = secondaryNoise ? createNoise3D2(() => rng.nextf) : null;
|
|
3178
|
+
const position = this.geometry.attributes.position;
|
|
3179
|
+
const vertices = position.array;
|
|
3180
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
3181
|
+
const x = vertices[i];
|
|
3182
|
+
const y = vertices[i + 1];
|
|
3183
|
+
const z = vertices[i + 2];
|
|
3184
|
+
const n1 = noise3d(x * noiseScale, y * noiseScale, z * noiseScale);
|
|
3185
|
+
const n2 = secondaryNoise ? 0.5 * noise3d2(x * noiseScale * 2, y * noiseScale * 2, z * noiseScale * 2) : 0;
|
|
3186
|
+
const noiseValue = n1 + n2;
|
|
3187
|
+
const displacementValue = 1 + noiseValue * displacement;
|
|
3188
|
+
vertices[i] = x * displacementValue;
|
|
3189
|
+
vertices[i + 1] = y * displacementValue;
|
|
3190
|
+
vertices[i + 2] = z * displacementValue;
|
|
3191
|
+
}
|
|
3192
|
+
position.needsUpdate = true;
|
|
3193
|
+
this.geometry.computeVertexNormals();
|
|
3194
|
+
}
|
|
3195
|
+
};
|
|
3196
|
+
|
|
3197
|
+
// src/three/starfield.ts
|
|
3198
|
+
import { BufferAttribute as BufferAttribute2, BufferGeometry as BufferGeometry3, Group as Group4, Mesh as Mesh4, MeshBasicMaterial as MeshBasicMaterial2, PointLight, Points as Points2, PointsMaterial, SphereGeometry, Vector3 as Vector34 } from "three";
|
|
3199
|
+
function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215) {
|
|
3200
|
+
const geometry = new BufferGeometry3();
|
|
3201
|
+
const positions = new Float32Array(count * 3);
|
|
3202
|
+
for (let i = 0; i < count; i++) {
|
|
3203
|
+
const theta = rng.nextf * Math.PI * 2;
|
|
3204
|
+
const phi = Math.acos(rng.nextf * 2 - 1);
|
|
3205
|
+
const x = radius * Math.sin(phi) * Math.cos(theta);
|
|
3206
|
+
const y = radius * Math.sin(phi) * Math.sin(theta);
|
|
3207
|
+
const z = radius * Math.cos(phi);
|
|
3208
|
+
positions.set([x, y, z], i * 3);
|
|
3209
|
+
}
|
|
3210
|
+
geometry.setAttribute("position", new BufferAttribute2(positions, 3));
|
|
3211
|
+
const material = new PointsMaterial({
|
|
3212
|
+
color,
|
|
3213
|
+
size: 5,
|
|
3214
|
+
sizeAttenuation: true,
|
|
3215
|
+
transparent: true,
|
|
3216
|
+
opacity: 0.9
|
|
3217
|
+
});
|
|
3218
|
+
const stars = new Points2(geometry, material);
|
|
3219
|
+
stars.name = "Starfield";
|
|
3220
|
+
return stars;
|
|
3221
|
+
}
|
|
3222
|
+
function CreateSystemStar(position) {
|
|
3223
|
+
const star = new Group4();
|
|
3224
|
+
const starPos = position ?? new Vector34();
|
|
3225
|
+
const starGeo = new SphereGeometry(50, 32, 32);
|
|
3226
|
+
const starMat = new MeshBasicMaterial2({ color: 16768426 });
|
|
3227
|
+
const starMesh = new Mesh4(starGeo, starMat);
|
|
3228
|
+
starMesh.position.copy(starPos);
|
|
3229
|
+
star.add(starMesh);
|
|
3230
|
+
const starLight = new PointLight(16777215, 10, 0, 0);
|
|
3231
|
+
starLight.position.copy(starPos);
|
|
3232
|
+
star.add(starLight);
|
|
3233
|
+
return star;
|
|
3234
|
+
}
|
|
3235
|
+
|
|
3236
|
+
// src/three/palette-controller.ts
|
|
3237
|
+
import { Color as Color3, Vector3 as Vector35 } from "three";
|
|
3238
|
+
async function CreatePaletteController(parentGui, colorPalette, onChange, folderName = "palette") {
|
|
3239
|
+
const { Controller } = await import("three/examples/jsm/libs/lil-gui.module.min.js");
|
|
3240
|
+
class PaletteControllerImpl extends Controller {
|
|
3241
|
+
$innerDiv;
|
|
3242
|
+
colorButtons = [];
|
|
3243
|
+
colorButtonSize = 20;
|
|
3244
|
+
selectedBorderStyle = "2px dashed black";
|
|
3245
|
+
currentColorIndex = 0;
|
|
3246
|
+
eyedropperButton;
|
|
3247
|
+
colorPalette;
|
|
3248
|
+
_onColorChange;
|
|
3249
|
+
constructor() {
|
|
3250
|
+
const parent = parentGui.addFolder(folderName);
|
|
3251
|
+
super(parent, {}, "", "function");
|
|
3252
|
+
this.colorPalette = colorPalette;
|
|
3253
|
+
this._onColorChange = onChange;
|
|
3254
|
+
this.$widget.style.setProperty("display", "flex");
|
|
3255
|
+
this.$widget.style.setProperty("justify-content", "center");
|
|
3256
|
+
this.$innerDiv = document.createElement("div");
|
|
3257
|
+
this.$innerDiv.style.setProperty("display", "flex");
|
|
3258
|
+
this.$innerDiv.style.setProperty("flex-wrap", "wrap");
|
|
3259
|
+
this.$innerDiv.style.setProperty("width", `${this.colorButtonSize * Math.floor(Math.sqrt(this.colorPalette.length)) + 2}px`);
|
|
3260
|
+
this.$innerDiv.appendChild(this.$name);
|
|
3261
|
+
this.$widget.appendChild(this.$innerDiv);
|
|
3262
|
+
this.colorPalette.forEach((color) => this._createColorButton(color));
|
|
3263
|
+
if (window.EyeDropper) {
|
|
3264
|
+
this.eyedropperButton = parent.add({
|
|
3265
|
+
eyedropper: () => {
|
|
3266
|
+
this.eyedropperButton?.disable(true);
|
|
3267
|
+
new window.EyeDropper().open().then(({ sRGBHex }) => {
|
|
3268
|
+
const i = this.findClosestColorMatchIndex(sRGBHex);
|
|
3269
|
+
this.setSelectedColor(i);
|
|
3270
|
+
this._onColorChange?.(i);
|
|
3271
|
+
}).finally(() => this.eyedropperButton?.disable(false)).catch(() => {
|
|
3272
|
+
});
|
|
3273
|
+
}
|
|
3274
|
+
}, "eyedropper").name("findClosestColor");
|
|
3275
|
+
}
|
|
3276
|
+
this.name("");
|
|
3277
|
+
this.updateDisplay();
|
|
3278
|
+
}
|
|
3279
|
+
_onColorButtonClicked(i, button) {
|
|
3280
|
+
if (button === 0) {
|
|
3281
|
+
this.setSelectedColor(i);
|
|
3282
|
+
this._onColorChange?.(i);
|
|
3283
|
+
this.colorButtons[i].blur();
|
|
3284
|
+
} else if (button === 2 && window.EyeDropper) {
|
|
3285
|
+
new window.EyeDropper().open().then(({ sRGBHex }) => {
|
|
3286
|
+
this.colorPalette[i] = sRGBHex;
|
|
3287
|
+
this.colorButtons[i].setAttribute("title", `${sRGBHex}`);
|
|
3288
|
+
this.colorButtons[i].style.setProperty("background", sRGBHex);
|
|
3289
|
+
this.setSelectedColor(i);
|
|
3290
|
+
this._onColorChange?.(i);
|
|
3291
|
+
this.colorButtons[i].blur();
|
|
3292
|
+
}).finally(() => this.eyedropperButton?.disable(false)).catch(() => {
|
|
3293
|
+
});
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
_createColorButton(color) {
|
|
3297
|
+
const i = this.colorButtons.length;
|
|
3298
|
+
const colorButton = document.createElement("div");
|
|
3299
|
+
colorButton.setAttribute("id", `colorButton${i}`);
|
|
3300
|
+
colorButton.setAttribute("title", `${color}`);
|
|
3301
|
+
colorButton.style.setProperty("width", `${this.colorButtonSize}px`);
|
|
3302
|
+
colorButton.style.setProperty("height", `${this.colorButtonSize}px`);
|
|
3303
|
+
colorButton.style.setProperty("background", color);
|
|
3304
|
+
colorButton.addEventListener("pointerup", (ev) => this._onColorButtonClicked(i, ev.button));
|
|
3305
|
+
this.colorButtons.push(colorButton);
|
|
3306
|
+
this.$innerDiv.appendChild(colorButton);
|
|
3307
|
+
}
|
|
3308
|
+
updatePaletteColors() {
|
|
3309
|
+
this.colorPalette.forEach((color, i) => {
|
|
3310
|
+
if (i < this.colorButtons.length) {
|
|
3311
|
+
this.colorButtons[i].setAttribute("title", `${color}`);
|
|
3312
|
+
this.colorButtons[i].style.setProperty("background", color);
|
|
3313
|
+
} else {
|
|
3314
|
+
this._createColorButton(color);
|
|
3315
|
+
}
|
|
3316
|
+
});
|
|
3317
|
+
}
|
|
3318
|
+
setSelectedColor(colorIndex = 0) {
|
|
3319
|
+
this.colorButtons[this.currentColorIndex].style.removeProperty("border");
|
|
3320
|
+
this.currentColorIndex = clamp(colorIndex, 0, this.colorPalette.length);
|
|
3321
|
+
this.colorButtons[this.currentColorIndex].style.setProperty("border", this.selectedBorderStyle);
|
|
3322
|
+
}
|
|
3323
|
+
findClosestColorMatchIndex(color) {
|
|
3324
|
+
const pickedColor = new Color3(color);
|
|
3325
|
+
const pickedVector = new Vector35().setFromColor(pickedColor);
|
|
3326
|
+
const list = [];
|
|
3327
|
+
this.colorPalette.forEach((paletteColorString, i) => {
|
|
3328
|
+
const paletteColor = new Color3(paletteColorString);
|
|
3329
|
+
const paletteVector = new Vector35().setFromColor(paletteColor);
|
|
3330
|
+
const d = paletteVector.distanceTo(pickedVector);
|
|
3331
|
+
list.push({ i, d });
|
|
3332
|
+
});
|
|
3333
|
+
list.sort((a, b) => a.d - b.d);
|
|
3334
|
+
const closest = list.shift();
|
|
3335
|
+
return closest?.i ?? 0;
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
3338
|
+
return new PaletteControllerImpl();
|
|
3339
|
+
}
|
|
3340
|
+
|
|
3341
|
+
// src/three/ecs.ts
|
|
3342
|
+
import { Group as Group5 } from "three";
|
|
3343
|
+
var entityCounter = 0;
|
|
3344
|
+
function GenerateEntityId() {
|
|
3345
|
+
return `entity_${entityCounter++}`;
|
|
3346
|
+
}
|
|
3347
|
+
function CreateThreeEntity(obj) {
|
|
3348
|
+
const entity = obj ?? new Group5();
|
|
3349
|
+
entity.userData.entityId = GenerateEntityId();
|
|
3350
|
+
entity.userData.components = {};
|
|
3351
|
+
return entity;
|
|
3352
|
+
}
|
|
3353
|
+
function AddThreeComponent(entity, name, component) {
|
|
3354
|
+
entity.userData.components[name] = component;
|
|
3355
|
+
}
|
|
3356
|
+
function GetThreeComponent(entity, name) {
|
|
3357
|
+
return entity.userData.components[name];
|
|
3358
|
+
}
|
|
3359
|
+
function RemoveThreeComponent(entity, name) {
|
|
3360
|
+
delete entity.userData.components[name];
|
|
3361
|
+
}
|
|
3362
|
+
function IsThreeEntity(obj) {
|
|
3363
|
+
return obj.userData.entityId !== void 0;
|
|
3364
|
+
}
|
|
3365
|
+
|
|
3366
|
+
// src/worker-interface.ts
|
|
3367
|
+
var WorkerInterface = class extends Emitter {
|
|
3368
|
+
_worker;
|
|
3369
|
+
constructor(url) {
|
|
3370
|
+
super();
|
|
3371
|
+
this._worker = new Worker(url, { type: "module" });
|
|
3372
|
+
this._worker.onerror = (error) => this.emit("error", error);
|
|
3373
|
+
this._worker.onmessage = (message) => this.emit("message", message);
|
|
3374
|
+
}
|
|
3375
|
+
postMessage(message, options) {
|
|
3376
|
+
this._worker.postMessage(message, options);
|
|
3377
|
+
}
|
|
3378
|
+
terminate() {
|
|
3379
|
+
this._worker.terminate();
|
|
3380
|
+
}
|
|
3381
|
+
};
|
|
1514
3382
|
export {
|
|
3383
|
+
AStar,
|
|
3384
|
+
AddThreeComponent,
|
|
3385
|
+
AsteroidMesh,
|
|
3386
|
+
AtlasTextureMaterial,
|
|
3387
|
+
BuildDefaultPalette,
|
|
1515
3388
|
CanvasRenderer,
|
|
1516
3389
|
Clock,
|
|
1517
3390
|
ClockStats,
|
|
3391
|
+
Color,
|
|
3392
|
+
Colors,
|
|
1518
3393
|
CompileShader,
|
|
3394
|
+
CreatePaletteController,
|
|
1519
3395
|
CreateProgram,
|
|
1520
3396
|
CreateProgramInfo,
|
|
3397
|
+
CreateStarfield,
|
|
3398
|
+
CreateSubTexture,
|
|
3399
|
+
CreateSystemStar,
|
|
3400
|
+
CreateThreeEntity,
|
|
1521
3401
|
CreateWebGlContext,
|
|
1522
3402
|
CubeSphereGeometry,
|
|
1523
3403
|
ECS,
|
|
1524
3404
|
Emitter,
|
|
3405
|
+
EventBus,
|
|
3406
|
+
GenerateDoors,
|
|
3407
|
+
GenerateEntityId,
|
|
3408
|
+
GenerateRooms,
|
|
3409
|
+
GenerateWalls,
|
|
1525
3410
|
GetProgramLocations,
|
|
3411
|
+
GetThreeComponent,
|
|
1526
3412
|
GlobalEmitter,
|
|
1527
3413
|
Input,
|
|
3414
|
+
IsThreeEntity,
|
|
1528
3415
|
LoadTexture,
|
|
3416
|
+
Map2D,
|
|
3417
|
+
Map3D,
|
|
1529
3418
|
Noise,
|
|
3419
|
+
PI,
|
|
3420
|
+
ParticleSystem2d,
|
|
3421
|
+
ParticleSystem3d,
|
|
3422
|
+
PoissonDiskSampler,
|
|
3423
|
+
Rectangle,
|
|
3424
|
+
RemoveThreeComponent,
|
|
3425
|
+
Renderer,
|
|
1530
3426
|
ResizeWebGlContext,
|
|
1531
3427
|
Rng,
|
|
1532
3428
|
ShaderType,
|
|
1533
3429
|
SimplexNoise,
|
|
3430
|
+
SpatialPartition2d,
|
|
3431
|
+
SpatialPartition3d,
|
|
3432
|
+
TILE_EMPTY,
|
|
3433
|
+
TILE_FLOOR,
|
|
3434
|
+
TILE_PATH,
|
|
1534
3435
|
TextureAtlas,
|
|
1535
3436
|
ThreeJsBoilerPlate,
|
|
1536
3437
|
ThreeJsCameraRig,
|
|
1537
3438
|
ThreeJsPlayerController,
|
|
1538
|
-
|
|
3439
|
+
Tilemap,
|
|
3440
|
+
Vector,
|
|
3441
|
+
VoxelFaces,
|
|
3442
|
+
VoxelMesh,
|
|
1539
3443
|
WebGlRenderer,
|
|
3444
|
+
WorkerInterface,
|
|
3445
|
+
abs,
|
|
3446
|
+
ceil,
|
|
1540
3447
|
clamp,
|
|
3448
|
+
cos,
|
|
1541
3449
|
deg2rad,
|
|
3450
|
+
distance,
|
|
3451
|
+
eventBus,
|
|
3452
|
+
floor,
|
|
1542
3453
|
getRedYellowGreenGradientHex,
|
|
3454
|
+
hypot2,
|
|
3455
|
+
hypot3,
|
|
3456
|
+
imul,
|
|
1543
3457
|
isNullOrUndefined,
|
|
1544
|
-
key2xyz,
|
|
1545
3458
|
lerp,
|
|
1546
3459
|
log,
|
|
1547
3460
|
logdev,
|
|
1548
3461
|
logerr,
|
|
1549
3462
|
loginf,
|
|
1550
3463
|
logwrn,
|
|
3464
|
+
manhattanDistance,
|
|
3465
|
+
max,
|
|
3466
|
+
min,
|
|
3467
|
+
pow,
|
|
1551
3468
|
rng,
|
|
1552
|
-
|
|
3469
|
+
round,
|
|
3470
|
+
roundTo,
|
|
3471
|
+
sin,
|
|
3472
|
+
sqrt,
|
|
3473
|
+
squaredDistance
|
|
1553
3474
|
};
|