@n3rdw1z4rd/core 0.11.0 → 0.13.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/dist/index.d.mts +442 -26
- package/dist/index.d.ts +442 -26
- package/dist/index.js +2239 -252
- package/dist/index.mjs +2175 -251
- package/package.json +9 -2
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,8 +1228,25 @@ 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
1252
|
import { vec2 } from "gl-matrix";
|
|
@@ -497,132 +1371,82 @@ var Input = class extends Emitter {
|
|
|
497
1371
|
}
|
|
498
1372
|
};
|
|
499
1373
|
|
|
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;
|
|
1374
|
+
// src/map3d.ts
|
|
1375
|
+
var DEFAULT_VALUE2 = -1;
|
|
1376
|
+
var Map3D = class {
|
|
1377
|
+
defaultValue;
|
|
1378
|
+
_data;
|
|
1379
|
+
get data() {
|
|
1380
|
+
return this._data;
|
|
520
1381
|
}
|
|
521
|
-
|
|
522
|
-
|
|
1382
|
+
constructor(defaultValue = DEFAULT_VALUE2) {
|
|
1383
|
+
this.defaultValue = defaultValue;
|
|
1384
|
+
this._data = /* @__PURE__ */ new Map();
|
|
523
1385
|
}
|
|
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;
|
|
1386
|
+
_floor(x, y, z) {
|
|
1387
|
+
return [floor(x), floor(y), floor(z)];
|
|
552
1388
|
}
|
|
553
|
-
|
|
554
|
-
return
|
|
1389
|
+
_key(x, y, z) {
|
|
1390
|
+
return `${x},${y},${z}`;
|
|
555
1391
|
}
|
|
556
|
-
|
|
557
|
-
|
|
1392
|
+
_pos(key) {
|
|
1393
|
+
return key.split(",").map((v) => parseInt(v));
|
|
558
1394
|
}
|
|
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;
|
|
581
|
-
}
|
|
582
|
-
choose(...args) {
|
|
583
|
-
if (args.length === 1) {
|
|
584
|
-
if (Array.isArray(args[0])) {
|
|
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
|
-
}
|
|
594
|
-
}
|
|
595
|
-
shuffle(value) {
|
|
596
|
-
if (Array.isArray(value)) {
|
|
597
|
-
return value.sort(() => 0.5 - this.nextf);
|
|
598
|
-
} else {
|
|
599
|
-
return value.split("").sort(() => 0.5 - this.nextf).join("");
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
uid(length = 16) {
|
|
603
|
-
const uid = [];
|
|
604
|
-
for (let i = 0; i < length; i++) uid.push(this.choose(this._uid_characters));
|
|
605
|
-
return uid.join("");
|
|
1395
|
+
clear() {
|
|
1396
|
+
this._data.clear();
|
|
606
1397
|
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
1398
|
+
get(x, y, z, defaultValue = this.defaultValue) {
|
|
1399
|
+
[x, y, z] = this._floor(x, y, z);
|
|
1400
|
+
return this._data.get(this._key(x, y, z)) ?? defaultValue;
|
|
1401
|
+
}
|
|
1402
|
+
set(x, y, z, value = this.defaultValue) {
|
|
1403
|
+
[x, y, z] = this._floor(x, y, z);
|
|
1404
|
+
const key = this._key(x, y, z);
|
|
1405
|
+
if (value === this.defaultValue) {
|
|
1406
|
+
this._data.delete(key);
|
|
1407
|
+
} else {
|
|
1408
|
+
this._data.set(key, value);
|
|
615
1409
|
}
|
|
616
|
-
return rows;
|
|
617
1410
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1411
|
+
forEach(callback) {
|
|
1412
|
+
this._data.forEach((v, key) => {
|
|
1413
|
+
const [x, y, z] = this._pos(key);
|
|
1414
|
+
callback(x, y, z, v);
|
|
1415
|
+
});
|
|
621
1416
|
}
|
|
622
1417
|
};
|
|
623
|
-
|
|
1418
|
+
|
|
1419
|
+
// src/misc.ts
|
|
1420
|
+
function isNullOrUndefined(target) {
|
|
1421
|
+
return target === null || target === void 0;
|
|
1422
|
+
}
|
|
1423
|
+
var getRedYellowGreenGradientHex = (min2, max2) => {
|
|
1424
|
+
if (max2 === void 0) {
|
|
1425
|
+
max2 = min2;
|
|
1426
|
+
min2 = 0;
|
|
1427
|
+
}
|
|
1428
|
+
if (max2 < 1) {
|
|
1429
|
+
max2 = 1;
|
|
1430
|
+
}
|
|
1431
|
+
const percent = min2 / max2;
|
|
1432
|
+
const index = clamp(Math.floor(percent * 10), 0, 10);
|
|
1433
|
+
return [
|
|
1434
|
+
"#ff0000",
|
|
1435
|
+
"#ff3300",
|
|
1436
|
+
"#ff6600",
|
|
1437
|
+
"#ff9900",
|
|
1438
|
+
"#ffcc00",
|
|
1439
|
+
"#ffff00",
|
|
1440
|
+
"#ccff00",
|
|
1441
|
+
"#99ff00",
|
|
1442
|
+
"#66ff00",
|
|
1443
|
+
"#33ff00",
|
|
1444
|
+
"#00ff00"
|
|
1445
|
+
][index];
|
|
1446
|
+
};
|
|
624
1447
|
|
|
625
1448
|
// src/noise.ts
|
|
1449
|
+
import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
|
|
626
1450
|
var Noise = class _Noise {
|
|
627
1451
|
static _n2d;
|
|
628
1452
|
static _n3d;
|
|
@@ -682,8 +1506,285 @@ var Noise = class _Noise {
|
|
|
682
1506
|
}
|
|
683
1507
|
};
|
|
684
1508
|
|
|
1509
|
+
// src/particle-renderer.ts
|
|
1510
|
+
var Renderer = class {
|
|
1511
|
+
_screenCanvas;
|
|
1512
|
+
_bufferCanvas;
|
|
1513
|
+
screenContext;
|
|
1514
|
+
bufferContext;
|
|
1515
|
+
get width() {
|
|
1516
|
+
return this._screenCanvas.width;
|
|
1517
|
+
}
|
|
1518
|
+
get height() {
|
|
1519
|
+
return this._screenCanvas.height;
|
|
1520
|
+
}
|
|
1521
|
+
constructor(canvas) {
|
|
1522
|
+
this._screenCanvas = canvas ?? document.createElement("canvas");
|
|
1523
|
+
this._bufferCanvas = new OffscreenCanvas(this._screenCanvas.width, this._screenCanvas.height);
|
|
1524
|
+
this.screenContext = this._screenCanvas.getContext("2d");
|
|
1525
|
+
this.bufferContext = this._bufferCanvas.getContext("2d");
|
|
1526
|
+
if (!this.screenContext || !this.bufferContext) {
|
|
1527
|
+
throw "Failed to create CanvasRenderingContext2D";
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
appendTo(target, autoResize = true) {
|
|
1531
|
+
if (this._screenCanvas.parentNode) {
|
|
1532
|
+
this._screenCanvas.parentNode.removeChild(this._screenCanvas);
|
|
1533
|
+
}
|
|
1534
|
+
target.appendChild(this._screenCanvas);
|
|
1535
|
+
if (autoResize) {
|
|
1536
|
+
this.resize();
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
resize(displayWidth, displayHeight) {
|
|
1540
|
+
const { width, height } = this._screenCanvas.parentElement?.getBoundingClientRect() ?? this._screenCanvas.getBoundingClientRect();
|
|
1541
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
1542
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
1543
|
+
if (this._screenCanvas.width !== displayWidth || this._screenCanvas.height !== displayHeight) {
|
|
1544
|
+
this._screenCanvas.width = displayWidth;
|
|
1545
|
+
this._screenCanvas.height = displayHeight;
|
|
1546
|
+
this._bufferCanvas.width = displayWidth;
|
|
1547
|
+
this._bufferCanvas.height = displayHeight;
|
|
1548
|
+
return true;
|
|
1549
|
+
}
|
|
1550
|
+
return false;
|
|
1551
|
+
}
|
|
1552
|
+
setPixel(x, y, color, size = 2) {
|
|
1553
|
+
this.bufferContext.fillStyle = color.hexStr;
|
|
1554
|
+
this.bufferContext.fillRect(x - size / 2, y - size / 2, size, size);
|
|
1555
|
+
}
|
|
1556
|
+
render() {
|
|
1557
|
+
this.screenContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
1558
|
+
this.screenContext.drawImage(this._bufferCanvas, 0, 0);
|
|
1559
|
+
this.bufferContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
1560
|
+
}
|
|
1561
|
+
};
|
|
1562
|
+
|
|
1563
|
+
// src/spatial-partition-2d.ts
|
|
1564
|
+
var SpatialPartition2d = class {
|
|
1565
|
+
cells;
|
|
1566
|
+
cellSize;
|
|
1567
|
+
entities;
|
|
1568
|
+
constructor(cellSize, entities) {
|
|
1569
|
+
this.cellSize = cellSize;
|
|
1570
|
+
this.entities = entities;
|
|
1571
|
+
this.cells = Array.from(
|
|
1572
|
+
{ length: Math.ceil(1 / cellSize) },
|
|
1573
|
+
() => Array.from({ length: Math.ceil(1 / cellSize) }, () => [])
|
|
1574
|
+
);
|
|
1575
|
+
for (const entity of this.entities) {
|
|
1576
|
+
this.addEntity(entity);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
addEntity(entity) {
|
|
1580
|
+
const cell = this.getCell(entity.x, entity.y);
|
|
1581
|
+
cell.push(entity);
|
|
1582
|
+
}
|
|
1583
|
+
getCell(x, y) {
|
|
1584
|
+
const cellX = Math.floor(x / this.cellSize);
|
|
1585
|
+
const cellY = Math.floor(y / this.cellSize);
|
|
1586
|
+
return this.cells[cellY][cellX];
|
|
1587
|
+
}
|
|
1588
|
+
getCellNeighbors(x, y) {
|
|
1589
|
+
const neighbors = [];
|
|
1590
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
1591
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
1592
|
+
let nx = x + dx;
|
|
1593
|
+
let ny = y + dy;
|
|
1594
|
+
if (nx < 0) nx = this.cells[0].length - 1;
|
|
1595
|
+
else if (nx >= this.cells[0].length) nx = 0;
|
|
1596
|
+
if (ny < 0) ny = this.cells.length - 1;
|
|
1597
|
+
else if (ny >= this.cells.length) ny = 0;
|
|
1598
|
+
neighbors.push(this.cells[ny][nx]);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
return neighbors;
|
|
1602
|
+
}
|
|
1603
|
+
};
|
|
1604
|
+
|
|
1605
|
+
// src/particle-system-2d.ts
|
|
1606
|
+
var colors = [
|
|
1607
|
+
Color.RED,
|
|
1608
|
+
Color.GREEN,
|
|
1609
|
+
Color.BLUE,
|
|
1610
|
+
Color.YELLOW,
|
|
1611
|
+
Color.MAGENTA
|
|
1612
|
+
];
|
|
1613
|
+
var ParticleSystem2d = class {
|
|
1614
|
+
particleCount = 2e3;
|
|
1615
|
+
_particleSize = 2;
|
|
1616
|
+
_frictionHalfLife = 0.04;
|
|
1617
|
+
_range = 0.1;
|
|
1618
|
+
_rangeFactor = 0.1;
|
|
1619
|
+
_attractionMatrix = rng.randomMatrix(colors.length);
|
|
1620
|
+
_particles = [];
|
|
1621
|
+
_spatialPartition;
|
|
1622
|
+
constructor() {
|
|
1623
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
1624
|
+
this._particles.push({
|
|
1625
|
+
x: rng.nextf,
|
|
1626
|
+
y: rng.nextf,
|
|
1627
|
+
vx: 0,
|
|
1628
|
+
vy: 0,
|
|
1629
|
+
color: rng.range(colors.length)
|
|
1630
|
+
});
|
|
1631
|
+
}
|
|
1632
|
+
this._spatialPartition = new SpatialPartition2d(this._range, this._particles);
|
|
1633
|
+
}
|
|
1634
|
+
_calcForce(r, a, beta = 0.3) {
|
|
1635
|
+
let f = 0;
|
|
1636
|
+
if (r < beta) {
|
|
1637
|
+
f = r / beta - 1;
|
|
1638
|
+
} else if (beta < r && r < 1) {
|
|
1639
|
+
f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
1640
|
+
}
|
|
1641
|
+
return f;
|
|
1642
|
+
}
|
|
1643
|
+
_updateVelocities(deltaTimeSeconds) {
|
|
1644
|
+
const frictionFactor = Math.pow(
|
|
1645
|
+
0.5,
|
|
1646
|
+
deltaTimeSeconds / this._frictionHalfLife
|
|
1647
|
+
);
|
|
1648
|
+
for (let y = 0; y < this._spatialPartition.cells.length; y++) {
|
|
1649
|
+
for (let x = 0; x < this._spatialPartition.cells[y].length; x++) {
|
|
1650
|
+
const cell = this._spatialPartition.cells[y][x];
|
|
1651
|
+
for (let i = 0; i < cell.length; i++) {
|
|
1652
|
+
let fx = 0;
|
|
1653
|
+
let fy = 0;
|
|
1654
|
+
const p1 = cell[i];
|
|
1655
|
+
const neighborCells = this._spatialPartition.getCellNeighbors(x, y);
|
|
1656
|
+
for (const neighborCell of neighborCells) {
|
|
1657
|
+
for (let j = 0; j < neighborCell.length; j++) {
|
|
1658
|
+
if (cell === neighborCell && i === j) continue;
|
|
1659
|
+
const p2 = neighborCell[j];
|
|
1660
|
+
let rx = p2.x - p1.x;
|
|
1661
|
+
if (Math.abs(rx) > 0.5) rx = rx > 0 ? rx - 1 : rx + 1;
|
|
1662
|
+
let ry = p2.y - p1.y;
|
|
1663
|
+
if (Math.abs(ry) > 0.5) ry = ry > 0 ? ry - 1 : ry + 1;
|
|
1664
|
+
const d = Math.hypot(rx, ry);
|
|
1665
|
+
if (d > 0 && d < this._range) {
|
|
1666
|
+
const f = this._calcForce(
|
|
1667
|
+
d / this._range,
|
|
1668
|
+
this._attractionMatrix[p1.color][p2.color]
|
|
1669
|
+
);
|
|
1670
|
+
fx += rx / d * f;
|
|
1671
|
+
fy += ry / d * f;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
fx *= this._range * this._rangeFactor;
|
|
1676
|
+
fy *= this._range * this._rangeFactor;
|
|
1677
|
+
p1.vx *= frictionFactor;
|
|
1678
|
+
p1.vy *= frictionFactor;
|
|
1679
|
+
p1.vx += fx;
|
|
1680
|
+
p1.vy += fy;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
_updatePositions(deltaTimeSeconds, renderer) {
|
|
1686
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
1687
|
+
const particle = this._particles[i];
|
|
1688
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
1689
|
+
particle.x += particle.vx * deltaTimeSeconds;
|
|
1690
|
+
particle.y += particle.vy * deltaTimeSeconds;
|
|
1691
|
+
if (particle.x < 0) particle.x = 1 + particle.x % 1;
|
|
1692
|
+
if (particle.x > 1) particle.x = particle.x % 1;
|
|
1693
|
+
if (particle.y < 0) particle.y = 1 + particle.y % 1;
|
|
1694
|
+
if (particle.y > 1) particle.y = particle.y % 1;
|
|
1695
|
+
renderer.setPixel(
|
|
1696
|
+
particle.x * renderer.width,
|
|
1697
|
+
particle.y * renderer.height,
|
|
1698
|
+
colors[particle.color],
|
|
1699
|
+
this._particleSize
|
|
1700
|
+
);
|
|
1701
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
1702
|
+
if (newCell !== oldCell) {
|
|
1703
|
+
const index = oldCell.indexOf(particle);
|
|
1704
|
+
if (index !== -1) {
|
|
1705
|
+
oldCell.splice(index, 1);
|
|
1706
|
+
}
|
|
1707
|
+
newCell.push(particle);
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
update(renderer, dt) {
|
|
1712
|
+
this._updateVelocities(dt);
|
|
1713
|
+
this._updatePositions(dt, renderer);
|
|
1714
|
+
}
|
|
1715
|
+
};
|
|
1716
|
+
|
|
1717
|
+
// src/poisson-disk-sampler.ts
|
|
1718
|
+
function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
|
|
1719
|
+
const k = maxPoints;
|
|
1720
|
+
const cellSize = minDist / Math.SQRT2;
|
|
1721
|
+
const gridWidth = Math.ceil(width / cellSize);
|
|
1722
|
+
const gridHeight = Math.ceil(height / cellSize);
|
|
1723
|
+
const grid = Array.from(
|
|
1724
|
+
{ length: gridWidth },
|
|
1725
|
+
() => Array(gridHeight).fill(null)
|
|
1726
|
+
);
|
|
1727
|
+
const samples = [];
|
|
1728
|
+
const active = [];
|
|
1729
|
+
function gridIndex(p) {
|
|
1730
|
+
return {
|
|
1731
|
+
i: Math.floor(p.x / cellSize),
|
|
1732
|
+
j: Math.floor(p.y / cellSize)
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
function isFarEnough(p) {
|
|
1736
|
+
const { i, j } = gridIndex(p);
|
|
1737
|
+
for (let x = i - 2; x <= i + 2; x++) {
|
|
1738
|
+
for (let y = j - 2; y <= j + 2; y++) {
|
|
1739
|
+
if (x < 0 || y < 0 || x >= gridWidth || y >= gridHeight) continue;
|
|
1740
|
+
const neighbor = grid[x][y];
|
|
1741
|
+
if (!neighbor) continue;
|
|
1742
|
+
const dx = neighbor.x - p.x;
|
|
1743
|
+
const dy = neighbor.y - p.y;
|
|
1744
|
+
if (dx * dx + dy * dy < minDist * minDist) {
|
|
1745
|
+
return false;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
return true;
|
|
1750
|
+
}
|
|
1751
|
+
const initial = {
|
|
1752
|
+
x: rng.range(0, width),
|
|
1753
|
+
y: rng.range(0, height)
|
|
1754
|
+
};
|
|
1755
|
+
const { i: i0, j: j0 } = gridIndex(initial);
|
|
1756
|
+
grid[i0][j0] = initial;
|
|
1757
|
+
samples.push(initial);
|
|
1758
|
+
active.push(initial);
|
|
1759
|
+
while (active.length) {
|
|
1760
|
+
const index = Math.floor(rng.range(0, active.length));
|
|
1761
|
+
const point = active[index];
|
|
1762
|
+
let found = false;
|
|
1763
|
+
for (let n = 0; n < k; n++) {
|
|
1764
|
+
const angle = rng.range(0, Math.PI * 2);
|
|
1765
|
+
const dist = rng.range(minDist, 2 * minDist);
|
|
1766
|
+
const newPoint = {
|
|
1767
|
+
x: point.x + Math.cos(angle) * dist,
|
|
1768
|
+
y: point.y + Math.sin(angle) * dist
|
|
1769
|
+
};
|
|
1770
|
+
if (newPoint.x >= 0 && newPoint.y >= 0 && newPoint.x < width && newPoint.y < height && isFarEnough(newPoint)) {
|
|
1771
|
+
const { i, j } = gridIndex(newPoint);
|
|
1772
|
+
grid[i][j] = newPoint;
|
|
1773
|
+
samples.push(newPoint);
|
|
1774
|
+
active.push(newPoint);
|
|
1775
|
+
found = true;
|
|
1776
|
+
break;
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
if (!found) {
|
|
1780
|
+
active.splice(index, 1);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
return samples;
|
|
1784
|
+
}
|
|
1785
|
+
|
|
685
1786
|
// src/renderer/canvas-renderer.ts
|
|
686
|
-
import { vec2 as
|
|
1787
|
+
import { vec2 as vec22 } from "gl-matrix";
|
|
687
1788
|
var CanvasRenderer = class {
|
|
688
1789
|
ctx;
|
|
689
1790
|
clearColor = "#000000";
|
|
@@ -698,7 +1799,7 @@ var CanvasRenderer = class {
|
|
|
698
1799
|
return this.ctx?.canvas.height ?? 0;
|
|
699
1800
|
}
|
|
700
1801
|
get center() {
|
|
701
|
-
return
|
|
1802
|
+
return vec22.fromValues(this.width / 2, this.height / 2);
|
|
702
1803
|
}
|
|
703
1804
|
constructor(canvas) {
|
|
704
1805
|
this.ctx = (canvas ?? document.createElement("canvas")).getContext("2d");
|
|
@@ -829,16 +1930,16 @@ function CompileShader(gl, type, source) {
|
|
|
829
1930
|
}
|
|
830
1931
|
return shader;
|
|
831
1932
|
}
|
|
832
|
-
function CreateProgram(gl,
|
|
833
|
-
if (typeof
|
|
834
|
-
|
|
1933
|
+
function CreateProgram(gl, vertexShader2, fragmentShader2) {
|
|
1934
|
+
if (typeof vertexShader2 === "string") {
|
|
1935
|
+
vertexShader2 = CompileShader(gl, ShaderType.VERTEX, vertexShader2);
|
|
835
1936
|
}
|
|
836
|
-
if (typeof
|
|
837
|
-
|
|
1937
|
+
if (typeof fragmentShader2 === "string") {
|
|
1938
|
+
fragmentShader2 = CompileShader(gl, ShaderType.FRAGMENT, fragmentShader2);
|
|
838
1939
|
}
|
|
839
1940
|
const program = gl.createProgram();
|
|
840
|
-
gl.attachShader(program,
|
|
841
|
-
gl.attachShader(program,
|
|
1941
|
+
gl.attachShader(program, vertexShader2);
|
|
1942
|
+
gl.attachShader(program, fragmentShader2);
|
|
842
1943
|
gl.linkProgram(program);
|
|
843
1944
|
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
844
1945
|
throw `CreateProgram: failed to link program: ${gl.getProgramInfoLog(program)}`;
|
|
@@ -1111,6 +2212,69 @@ var SimplexNoise = class {
|
|
|
1111
2212
|
}
|
|
1112
2213
|
};
|
|
1113
2214
|
|
|
2215
|
+
// src/spatial-partition-3d.ts
|
|
2216
|
+
var SpatialPartition3d = class {
|
|
2217
|
+
cells;
|
|
2218
|
+
cellSize;
|
|
2219
|
+
entities;
|
|
2220
|
+
constructor(cellSize, entities) {
|
|
2221
|
+
this.cellSize = cellSize;
|
|
2222
|
+
this.entities = entities;
|
|
2223
|
+
const gridSize = Math.ceil(1 / cellSize);
|
|
2224
|
+
this.cells = Array.from(
|
|
2225
|
+
{ length: gridSize },
|
|
2226
|
+
() => Array.from(
|
|
2227
|
+
{ length: gridSize },
|
|
2228
|
+
() => Array.from({ length: gridSize }, () => [])
|
|
2229
|
+
)
|
|
2230
|
+
);
|
|
2231
|
+
for (const entity of this.entities) {
|
|
2232
|
+
this.addEntity(entity);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
getCell(x, y, z) {
|
|
2236
|
+
const cx = Math.floor(x / this.cellSize);
|
|
2237
|
+
const cy = Math.floor(y / this.cellSize);
|
|
2238
|
+
const cz = Math.floor(z / this.cellSize);
|
|
2239
|
+
return this.cells[cz][cy][cx];
|
|
2240
|
+
}
|
|
2241
|
+
getCellNeighbors(cx, cy, cz) {
|
|
2242
|
+
const neighbors = [];
|
|
2243
|
+
const gridSize = this.cells.length;
|
|
2244
|
+
for (let dz = -1; dz <= 1; dz++) {
|
|
2245
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
2246
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
2247
|
+
let nx = (cx + dx + gridSize) % gridSize;
|
|
2248
|
+
let ny = (cy + dy + gridSize) % gridSize;
|
|
2249
|
+
let nz = (cz + dz + gridSize) % gridSize;
|
|
2250
|
+
neighbors.push(this.cells[nz][ny][nx]);
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
return neighbors;
|
|
2255
|
+
}
|
|
2256
|
+
addEntity(entity) {
|
|
2257
|
+
const cell = this.getCell(entity.x, entity.y, entity.z);
|
|
2258
|
+
cell.push(entity);
|
|
2259
|
+
}
|
|
2260
|
+
};
|
|
2261
|
+
|
|
2262
|
+
// src/tilemap.ts
|
|
2263
|
+
var Tilemap = class {
|
|
2264
|
+
map = /* @__PURE__ */ new Map();
|
|
2265
|
+
_key(x, y) {
|
|
2266
|
+
x = floor(x);
|
|
2267
|
+
y = floor(y);
|
|
2268
|
+
return `${x}x${y}`;
|
|
2269
|
+
}
|
|
2270
|
+
get(x, y) {
|
|
2271
|
+
return this.map.get(this._key(x, y));
|
|
2272
|
+
}
|
|
2273
|
+
set(x, y, v) {
|
|
2274
|
+
this.map.set(this._key(x, y), v);
|
|
2275
|
+
}
|
|
2276
|
+
};
|
|
2277
|
+
|
|
1114
2278
|
// src/three/cube-sphere-geometry.ts
|
|
1115
2279
|
import { BoxGeometry, Vector3 } from "three";
|
|
1116
2280
|
var CubeSphereGeometry = class extends BoxGeometry {
|
|
@@ -1130,6 +2294,8 @@ var CubeSphereGeometry = class extends BoxGeometry {
|
|
|
1130
2294
|
}
|
|
1131
2295
|
attrPositions.needsUpdate = true;
|
|
1132
2296
|
}
|
|
2297
|
+
size;
|
|
2298
|
+
segments;
|
|
1133
2299
|
_halfSize;
|
|
1134
2300
|
applyFractalBrownianMotion(octaves, persistence, scale, amplitude) {
|
|
1135
2301
|
const attrPositions = this.attributes.position;
|
|
@@ -1159,15 +2325,16 @@ var CubeSphereGeometry = class extends BoxGeometry {
|
|
|
1159
2325
|
};
|
|
1160
2326
|
|
|
1161
2327
|
// src/three/texture-atlas.ts
|
|
1162
|
-
import { vec2 as
|
|
1163
|
-
import { TextureLoader, MeshLambertMaterial, NearestFilter } from "three";
|
|
2328
|
+
import { vec2 as vec23 } from "gl-matrix";
|
|
2329
|
+
import { TextureLoader, MeshLambertMaterial, NearestFilter, RepeatWrapping } from "three";
|
|
1164
2330
|
function LoadTexture(url) {
|
|
1165
2331
|
return new Promise((res, rej) => {
|
|
1166
2332
|
new TextureLoader().load(
|
|
1167
2333
|
url,
|
|
1168
2334
|
(texture) => {
|
|
1169
|
-
const
|
|
1170
|
-
const
|
|
2335
|
+
const data = texture.source.data;
|
|
2336
|
+
const width = data.width;
|
|
2337
|
+
const height = data.height;
|
|
1171
2338
|
res({ width, height, texture });
|
|
1172
2339
|
},
|
|
1173
2340
|
(_ev) => {
|
|
@@ -1176,6 +2343,22 @@ function LoadTexture(url) {
|
|
|
1176
2343
|
);
|
|
1177
2344
|
});
|
|
1178
2345
|
}
|
|
2346
|
+
function CreateSubTexture(texture, x, y, w, h) {
|
|
2347
|
+
texture = texture.clone();
|
|
2348
|
+
texture.magFilter = NearestFilter;
|
|
2349
|
+
texture.wrapS = RepeatWrapping;
|
|
2350
|
+
const data = texture.source.data;
|
|
2351
|
+
const textureWidth = data.width;
|
|
2352
|
+
const textureHeight = data.height;
|
|
2353
|
+
w /= textureWidth;
|
|
2354
|
+
h /= textureHeight;
|
|
2355
|
+
x /= textureWidth;
|
|
2356
|
+
y = 1 - h - y / textureHeight;
|
|
2357
|
+
texture.offset.set(x, y);
|
|
2358
|
+
texture.repeat.set(w, h);
|
|
2359
|
+
texture.needsUpdate = true;
|
|
2360
|
+
return texture;
|
|
2361
|
+
}
|
|
1179
2362
|
var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
1180
2363
|
constructor(textureData, textureWidth, textureHeight = textureWidth, params) {
|
|
1181
2364
|
super({
|
|
@@ -1191,10 +2374,13 @@ var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
|
1191
2374
|
this._uh = this.textureHeight / this.textureData.height;
|
|
1192
2375
|
this.textureData.texture.magFilter = NearestFilter;
|
|
1193
2376
|
}
|
|
2377
|
+
textureData;
|
|
2378
|
+
textureWidth;
|
|
2379
|
+
textureHeight;
|
|
1194
2380
|
_uw;
|
|
1195
2381
|
_uh;
|
|
1196
2382
|
getUv(voxel, ux, uy) {
|
|
1197
|
-
return
|
|
2383
|
+
return vec23.fromValues(
|
|
1198
2384
|
(voxel + ux) * this._uw,
|
|
1199
2385
|
1 - (1 - uy) * this._uh
|
|
1200
2386
|
);
|
|
@@ -1208,6 +2394,61 @@ var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
|
1208
2394
|
));
|
|
1209
2395
|
}
|
|
1210
2396
|
};
|
|
2397
|
+
var AtlasTextureMaterial = class _AtlasTextureMaterial extends MeshLambertMaterial {
|
|
2398
|
+
textureData;
|
|
2399
|
+
tilePixelWidth;
|
|
2400
|
+
tilePixelHeight;
|
|
2401
|
+
_tileCount;
|
|
2402
|
+
_uvWidth;
|
|
2403
|
+
_uvHeight;
|
|
2404
|
+
_textureOffsetWidth;
|
|
2405
|
+
_textureOffsetHeight;
|
|
2406
|
+
constructor(textureData, atlasParams = {}, materialParams = {}) {
|
|
2407
|
+
super({
|
|
2408
|
+
map: textureData.texture,
|
|
2409
|
+
alphaTest: 0.1,
|
|
2410
|
+
transparent: true,
|
|
2411
|
+
name: "AtlasTextureMaterial",
|
|
2412
|
+
...materialParams
|
|
2413
|
+
});
|
|
2414
|
+
this.textureData = textureData;
|
|
2415
|
+
this.textureData.texture.magFilter = NearestFilter;
|
|
2416
|
+
this.tilePixelWidth = atlasParams.tilePixelWidth ?? 16;
|
|
2417
|
+
this.tilePixelHeight = atlasParams.tilePixelHeight ?? 16;
|
|
2418
|
+
this._textureOffsetWidth = atlasParams.tilePixelOffsetWidth ?? 0;
|
|
2419
|
+
this._textureOffsetHeight = atlasParams.tilePixelOffsetHeight ?? 0;
|
|
2420
|
+
this._tileCount = Math.floor(this.textureData.width / this.tilePixelWidth);
|
|
2421
|
+
this._uvWidth = this.tilePixelWidth / (this.textureData.width - this._textureOffsetWidth);
|
|
2422
|
+
this._uvHeight = this.tilePixelHeight / (this.textureData.height - this._textureOffsetHeight);
|
|
2423
|
+
}
|
|
2424
|
+
getTileUVs(tileIndex, vertexIndex) {
|
|
2425
|
+
const col = tileIndex % this._tileCount;
|
|
2426
|
+
const row = Math.floor(tileIndex / this._tileCount);
|
|
2427
|
+
const baseU = this._textureOffsetWidth / this.textureData.width + col * this._uvWidth;
|
|
2428
|
+
const baseV = this._textureOffsetHeight / this.textureData.height + row * this._uvHeight;
|
|
2429
|
+
const uvCorners = [
|
|
2430
|
+
[0, 0],
|
|
2431
|
+
// bottom-left
|
|
2432
|
+
[1, 0],
|
|
2433
|
+
// bottom-right
|
|
2434
|
+
[0, 1],
|
|
2435
|
+
// top-left
|
|
2436
|
+
[1, 1]
|
|
2437
|
+
// top-right
|
|
2438
|
+
];
|
|
2439
|
+
const [u, v] = uvCorners[vertexIndex];
|
|
2440
|
+
const uvx = baseU + u * this._uvWidth;
|
|
2441
|
+
const uvy = 1 - (baseV + v * this._uvHeight);
|
|
2442
|
+
return [uvx, uvy];
|
|
2443
|
+
}
|
|
2444
|
+
static fromUrl(url, atlasParams = {}, materialParams = {}) {
|
|
2445
|
+
return LoadTexture(url).then((textureData) => new _AtlasTextureMaterial(
|
|
2446
|
+
textureData,
|
|
2447
|
+
atlasParams,
|
|
2448
|
+
materialParams
|
|
2449
|
+
));
|
|
2450
|
+
}
|
|
2451
|
+
};
|
|
1211
2452
|
|
|
1212
2453
|
// src/three/threejs-boiler-plate.ts
|
|
1213
2454
|
import { AmbientLight, BoxGeometry as BoxGeometry2, DirectionalLight, GridHelper, Mesh, MeshLambertMaterial as MeshLambertMaterial2, PlaneGeometry, Raycaster, Scene, Vector2, WebGLRenderer } from "three";
|
|
@@ -1229,7 +2470,7 @@ var ThreeJsCameraRig = class extends Group {
|
|
|
1229
2470
|
super();
|
|
1230
2471
|
this.gimbal = new Group();
|
|
1231
2472
|
this.add(this.gimbal);
|
|
1232
|
-
this.camera = new PerspectiveCamera(
|
|
2473
|
+
this.camera = params?.camera ?? new PerspectiveCamera(
|
|
1233
2474
|
params?.fov ?? 75,
|
|
1234
2475
|
params?.aspect ?? 2,
|
|
1235
2476
|
params?.near ?? 0.1,
|
|
@@ -1262,6 +2503,7 @@ var ThreeJsBoilerPlate = class {
|
|
|
1262
2503
|
raycaster = new Raycaster();
|
|
1263
2504
|
renderer;
|
|
1264
2505
|
cameraRig;
|
|
2506
|
+
orbitControls;
|
|
1265
2507
|
get canvas() {
|
|
1266
2508
|
return this.renderer.domElement;
|
|
1267
2509
|
}
|
|
@@ -1274,14 +2516,38 @@ var ThreeJsBoilerPlate = class {
|
|
|
1274
2516
|
this.renderer = new WebGLRenderer(params?.renderer);
|
|
1275
2517
|
this.cameraRig = new ThreeJsCameraRig(params?.camera);
|
|
1276
2518
|
this.scene.add(this.cameraRig);
|
|
1277
|
-
|
|
2519
|
+
if (params?.parentElement) {
|
|
2520
|
+
this.appendTo(params.parentElement);
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
// Mutually exclusive with enableOrbitControls() - pick one. Gives you
|
|
2524
|
+
// manual orbit()/dolly() with tilt clamping and target-following, driven
|
|
2525
|
+
// by this.input's mouse events.
|
|
2526
|
+
enableCameraRigControls() {
|
|
2527
|
+
this.orbitControls?.dispose();
|
|
2528
|
+
this.orbitControls = void 0;
|
|
2529
|
+
this.input.on("mouse_move", ({ deltaX, deltaY }) => {
|
|
1278
2530
|
if (this.input.isMouseButtonDown(0)) {
|
|
1279
2531
|
this.cameraRig.orbit(deltaX, deltaY);
|
|
1280
2532
|
}
|
|
1281
2533
|
}).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
2534
|
+
}
|
|
2535
|
+
// Mutually exclusive with enableCameraRigControls() - pick one. Uses
|
|
2536
|
+
// Three's own OrbitControls instead of the hand-rolled rig math; call
|
|
2537
|
+
// update() once per frame in your render loop while this is active.
|
|
2538
|
+
// Async because OrbitControls only ships as an ESM-only addon module -
|
|
2539
|
+
// a dynamic import avoids forcing this whole package (built as both
|
|
2540
|
+
// CJS and ESM) to statically import an ESM-only file.
|
|
2541
|
+
async enableOrbitControls() {
|
|
2542
|
+
const { OrbitControls } = await import("three/examples/jsm/Addons.js");
|
|
2543
|
+
this.orbitControls = new OrbitControls(this.camera, this.canvas);
|
|
2544
|
+
this.orbitControls.minDistance = 0.5;
|
|
2545
|
+
this.orbitControls.maxDistance = 100;
|
|
2546
|
+
this.orbitControls.update();
|
|
2547
|
+
}
|
|
2548
|
+
// Call once per frame if enableOrbitControls() is active - no-op otherwise.
|
|
2549
|
+
update() {
|
|
2550
|
+
this.orbitControls?.update();
|
|
1285
2551
|
}
|
|
1286
2552
|
appendTo(htmlElement) {
|
|
1287
2553
|
if (this.canvas.parentElement) {
|
|
@@ -1310,6 +2576,9 @@ var ThreeJsBoilerPlate = class {
|
|
|
1310
2576
|
if (params.ambientLight !== false) this.scene.add(new AmbientLight());
|
|
1311
2577
|
if (params.directionalLight !== false) this.scene.add(new DirectionalLight());
|
|
1312
2578
|
if (params.gridHelper !== false) this.scene.add(new GridHelper(100, 100, 16711680));
|
|
2579
|
+
if (params.enableControls === true) {
|
|
2580
|
+
this.enableCameraRigControls();
|
|
2581
|
+
}
|
|
1313
2582
|
}
|
|
1314
2583
|
pick() {
|
|
1315
2584
|
const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
|
|
@@ -1343,7 +2612,7 @@ var ThreeJsPlayerController = class extends Group2 {
|
|
|
1343
2612
|
constructor(camera) {
|
|
1344
2613
|
super();
|
|
1345
2614
|
this.velocity = new Vector32();
|
|
1346
|
-
this.cameraRig = new ThreeJsCameraRig(camera);
|
|
2615
|
+
this.cameraRig = new ThreeJsCameraRig({ camera });
|
|
1347
2616
|
this.add(this.cameraRig);
|
|
1348
2617
|
}
|
|
1349
2618
|
update(deltaTime) {
|
|
@@ -1353,93 +2622,106 @@ var ThreeJsPlayerController = class extends Group2 {
|
|
|
1353
2622
|
}
|
|
1354
2623
|
};
|
|
1355
2624
|
|
|
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() {
|
|
2625
|
+
// src/three/voxel-mesh.ts
|
|
2626
|
+
import { BufferAttribute, BufferGeometry, Color as Color2, Mesh as Mesh2 } from "three";
|
|
2627
|
+
var VoxelMesh = class extends Mesh2 {
|
|
2628
|
+
type = "VoxelMesh";
|
|
2629
|
+
width;
|
|
2630
|
+
depth;
|
|
2631
|
+
height;
|
|
2632
|
+
colorPalette;
|
|
2633
|
+
voxels;
|
|
2634
|
+
needsGeometryUpdate = true;
|
|
2635
|
+
constructor(width, depth, height, material, name) {
|
|
1376
2636
|
super();
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
2637
|
+
this.width = width;
|
|
2638
|
+
this.depth = depth;
|
|
2639
|
+
this.height = height;
|
|
2640
|
+
this.material = material;
|
|
2641
|
+
this.material.vertexColors = true;
|
|
2642
|
+
this.name = name ?? "VoxelMesh";
|
|
2643
|
+
this.colorPalette = BuildDefaultPalette();
|
|
2644
|
+
this.voxels = /* @__PURE__ */ new Map();
|
|
2645
|
+
this.geometry = new BufferGeometry();
|
|
1385
2646
|
this.updateGeometry();
|
|
1386
2647
|
}
|
|
2648
|
+
_xyz2k(x, y, z) {
|
|
2649
|
+
x = Math.abs(Math.floor(x));
|
|
2650
|
+
y = Math.abs(Math.floor(y));
|
|
2651
|
+
z = Math.abs(Math.floor(z));
|
|
2652
|
+
return `${x},${y},${z}`;
|
|
2653
|
+
}
|
|
2654
|
+
_k2xyz(k) {
|
|
2655
|
+
const [x, y, z] = k.split(",").map((s) => parseInt(s));
|
|
2656
|
+
return { x, y, z };
|
|
2657
|
+
}
|
|
1387
2658
|
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
|
-
|
|
2659
|
+
let n = -1;
|
|
2660
|
+
if (x >= 0 && y >= 0 && z >= 0 && x < this.width && y < this.height && z < this.depth) {
|
|
2661
|
+
n = this.voxels.get(this._xyz2k(x, y, z)) ?? -1;
|
|
2662
|
+
}
|
|
2663
|
+
return n;
|
|
2664
|
+
}
|
|
2665
|
+
set(x, y, z, n) {
|
|
2666
|
+
if (x >= 0 && y >= 0 && z >= 0 && x < this.width && y < this.height && z < this.depth) {
|
|
2667
|
+
if (n === void 0) this.voxels.delete(this._xyz2k(x, y, z));
|
|
2668
|
+
else this.voxels.set(this._xyz2k(x, y, z), n);
|
|
2669
|
+
this.needsGeometryUpdate = true;
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
clearVoxels() {
|
|
2673
|
+
this.voxels.clear();
|
|
2674
|
+
}
|
|
2675
|
+
setSize(width, depth, height) {
|
|
2676
|
+
this.forEachVoxel((x, y, z) => {
|
|
2677
|
+
if (x >= width || y >= height || z >= depth) this.set(x, y, z);
|
|
2678
|
+
});
|
|
2679
|
+
this.width = width;
|
|
2680
|
+
this.depth = depth;
|
|
2681
|
+
this.height = height;
|
|
2682
|
+
this.needsGeometryUpdate = true;
|
|
2683
|
+
}
|
|
2684
|
+
forEachVoxel(callback) {
|
|
2685
|
+
this.voxels.forEach((n, k) => {
|
|
2686
|
+
const { x, y, z } = this._k2xyz(k);
|
|
2687
|
+
callback(x, y, z, n);
|
|
2688
|
+
});
|
|
2689
|
+
}
|
|
2690
|
+
importVoxelData(voxelData) {
|
|
2691
|
+
this.clearVoxels();
|
|
2692
|
+
this.name = voxelData.name;
|
|
2693
|
+
this.setSize(voxelData.width, voxelData.depth, voxelData.height);
|
|
2694
|
+
voxelData.palette.forEach((color, i) => {
|
|
2695
|
+
if (i < this.colorPalette.length) {
|
|
2696
|
+
this.colorPalette[i] = color;
|
|
2697
|
+
} else {
|
|
2698
|
+
this.colorPalette.push(color);
|
|
2699
|
+
}
|
|
2700
|
+
});
|
|
2701
|
+
voxelData.voxels.forEach((voxel) => {
|
|
2702
|
+
const { x, y, z, color } = voxel;
|
|
2703
|
+
this.set(x, y, z, color);
|
|
2704
|
+
});
|
|
2705
|
+
this.updateGeometry();
|
|
2706
|
+
}
|
|
1416
2707
|
updateGeometry() {
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
if (voxel) {
|
|
1424
|
-
const [x, y, z] = key2xyz(key);
|
|
2708
|
+
if (this.needsGeometryUpdate) {
|
|
2709
|
+
const positions = [];
|
|
2710
|
+
const normals = [];
|
|
2711
|
+
const colors2 = [];
|
|
2712
|
+
const indices = [];
|
|
2713
|
+
this.forEachVoxel((x, y, z, n) => {
|
|
1425
2714
|
VoxelFaces.forEach((faces) => {
|
|
1426
2715
|
const [dx, dy, dz] = faces[0];
|
|
1427
|
-
const neighborVoxel = this.get(x + dx, y + dy, z + dz)
|
|
1428
|
-
if (
|
|
2716
|
+
const neighborVoxel = this.get(x + dx, y + dy, z + dz);
|
|
2717
|
+
if (neighborVoxel < 0) {
|
|
1429
2718
|
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;
|
|
2719
|
+
faces.forEach((face) => {
|
|
2720
|
+
const [nx, ny, nz, px, py, pz] = face;
|
|
1440
2721
|
positions.push(x + px, y + py, z + pz);
|
|
1441
2722
|
normals.push(nx, ny, nz);
|
|
1442
|
-
|
|
2723
|
+
const { r, g, b } = new Color2(this.colorPalette[n]);
|
|
2724
|
+
colors2.push(r, g, b, 1);
|
|
1443
2725
|
});
|
|
1444
2726
|
indices.push(
|
|
1445
2727
|
positionIndex,
|
|
@@ -1449,105 +2731,747 @@ var VoxelGeometry = class extends BufferGeometry {
|
|
|
1449
2731
|
positionIndex + 1,
|
|
1450
2732
|
positionIndex + 3
|
|
1451
2733
|
);
|
|
1452
|
-
needsUpdate = true;
|
|
1453
2734
|
}
|
|
1454
2735
|
});
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
this.attributes.position.needsUpdate = true;
|
|
1463
|
-
this.computeVertexNormals();
|
|
2736
|
+
});
|
|
2737
|
+
this.geometry.setAttribute("position", new BufferAttribute(new Float32Array(positions), 3));
|
|
2738
|
+
this.geometry.setAttribute("normal", new BufferAttribute(new Float32Array(normals), 3));
|
|
2739
|
+
this.geometry.setAttribute("color", new BufferAttribute(new Float32Array(colors2), 4));
|
|
2740
|
+
this.geometry.setIndex(indices);
|
|
2741
|
+
this.geometry.computeBoundingSphere();
|
|
2742
|
+
this.updateMatrixWorld();
|
|
1464
2743
|
}
|
|
2744
|
+
this.needsGeometryUpdate = false;
|
|
2745
|
+
}
|
|
2746
|
+
exportData() {
|
|
2747
|
+
const voxelData = {
|
|
2748
|
+
name: this.name,
|
|
2749
|
+
width: this.width,
|
|
2750
|
+
depth: this.depth,
|
|
2751
|
+
height: this.height,
|
|
2752
|
+
palette: [...this.colorPalette],
|
|
2753
|
+
voxels: []
|
|
2754
|
+
};
|
|
2755
|
+
this.forEachVoxel((x, y, z, color) => {
|
|
2756
|
+
voxelData.voxels.push({ x, y, z, color });
|
|
2757
|
+
});
|
|
2758
|
+
return voxelData;
|
|
1465
2759
|
}
|
|
1466
2760
|
};
|
|
1467
|
-
var lo = -0.5;
|
|
1468
|
-
var hi = 0.5;
|
|
1469
2761
|
var VoxelFaces = [
|
|
1470
|
-
// [nx, ny, nz, px, py, pz
|
|
2762
|
+
// [nx, ny, nz, px, py, pz]
|
|
1471
2763
|
[
|
|
1472
2764
|
//left
|
|
1473
|
-
[-1, 0, 0,
|
|
1474
|
-
[-1, 0, 0,
|
|
1475
|
-
[-1, 0, 0,
|
|
1476
|
-
[-1, 0, 0,
|
|
2765
|
+
[-1, 0, 0, 0, 1, 0],
|
|
2766
|
+
[-1, 0, 0, 0, 0, 0],
|
|
2767
|
+
[-1, 0, 0, 0, 1, 1],
|
|
2768
|
+
[-1, 0, 0, 0, 0, 1]
|
|
1477
2769
|
],
|
|
1478
2770
|
[
|
|
1479
2771
|
//right
|
|
1480
|
-
[1, 0, 0,
|
|
1481
|
-
[1, 0, 0,
|
|
1482
|
-
[1, 0, 0,
|
|
1483
|
-
[1, 0, 0,
|
|
2772
|
+
[1, 0, 0, 1, 1, 1],
|
|
2773
|
+
[1, 0, 0, 1, 0, 1],
|
|
2774
|
+
[1, 0, 0, 1, 1, 0],
|
|
2775
|
+
[1, 0, 0, 1, 0, 0]
|
|
1484
2776
|
],
|
|
1485
2777
|
[
|
|
1486
2778
|
//bottom
|
|
1487
|
-
[0, -1, 0,
|
|
1488
|
-
[0, -1, 0,
|
|
1489
|
-
[0, -1, 0,
|
|
1490
|
-
[0, -1, 0,
|
|
2779
|
+
[0, -1, 0, 1, 0, 1],
|
|
2780
|
+
[0, -1, 0, 0, 0, 1],
|
|
2781
|
+
[0, -1, 0, 1, 0, 0],
|
|
2782
|
+
[0, -1, 0, 0, 0, 0]
|
|
1491
2783
|
],
|
|
1492
2784
|
[
|
|
1493
2785
|
//top
|
|
1494
|
-
[0, 1, 0,
|
|
1495
|
-
[0, 1, 0,
|
|
1496
|
-
[0, 1, 0,
|
|
1497
|
-
[0, 1, 0,
|
|
2786
|
+
[0, 1, 0, 0, 1, 1],
|
|
2787
|
+
[0, 1, 0, 1, 1, 1],
|
|
2788
|
+
[0, 1, 0, 0, 1, 0],
|
|
2789
|
+
[0, 1, 0, 1, 1, 0]
|
|
1498
2790
|
],
|
|
1499
2791
|
[
|
|
1500
2792
|
//back
|
|
1501
|
-
[0, 0, -1,
|
|
1502
|
-
[0, 0, -1,
|
|
1503
|
-
[0, 0, -1,
|
|
1504
|
-
[0, 0, -1,
|
|
2793
|
+
[0, 0, -1, 1, 0, 0],
|
|
2794
|
+
[0, 0, -1, 0, 0, 0],
|
|
2795
|
+
[0, 0, -1, 1, 1, 0],
|
|
2796
|
+
[0, 0, -1, 0, 1, 0]
|
|
1505
2797
|
],
|
|
1506
2798
|
[
|
|
1507
2799
|
//front
|
|
1508
|
-
[0, 0, 1,
|
|
1509
|
-
[0, 0, 1,
|
|
1510
|
-
[0, 0, 1,
|
|
1511
|
-
[0, 0, 1,
|
|
2800
|
+
[0, 0, 1, 0, 0, 1],
|
|
2801
|
+
[0, 0, 1, 1, 0, 1],
|
|
2802
|
+
[0, 0, 1, 0, 1, 1],
|
|
2803
|
+
[0, 0, 1, 1, 1, 1]
|
|
1512
2804
|
]
|
|
1513
2805
|
];
|
|
2806
|
+
function BuildDefaultPalette(palette) {
|
|
2807
|
+
palette = palette ?? Colors.rgb48;
|
|
2808
|
+
const grayscale = [];
|
|
2809
|
+
const count = 16;
|
|
2810
|
+
for (let i = 0; i < count; i++) {
|
|
2811
|
+
const grey = Math.floor(i / count * 255);
|
|
2812
|
+
grayscale.push(`rgb(${grey}, ${grey}, ${grey})`);
|
|
2813
|
+
}
|
|
2814
|
+
return [...palette, ...grayscale];
|
|
2815
|
+
}
|
|
2816
|
+
var Colors = {
|
|
2817
|
+
rgb12: [
|
|
2818
|
+
"#FF0000",
|
|
2819
|
+
"#FF8800",
|
|
2820
|
+
"#FFFF00",
|
|
2821
|
+
"#88FF00",
|
|
2822
|
+
"#00FF00",
|
|
2823
|
+
"#00FF88",
|
|
2824
|
+
"#00FFFF",
|
|
2825
|
+
"#0088FF",
|
|
2826
|
+
"#0000FF",
|
|
2827
|
+
"#8800FF",
|
|
2828
|
+
"#FF00FF",
|
|
2829
|
+
"#FF0080"
|
|
2830
|
+
],
|
|
2831
|
+
rgb24: [
|
|
2832
|
+
"#FF0000",
|
|
2833
|
+
"#FF4000",
|
|
2834
|
+
"#FF8000",
|
|
2835
|
+
"#FFBF00",
|
|
2836
|
+
"#FFFF00",
|
|
2837
|
+
"#CCFF00",
|
|
2838
|
+
"#80FF00",
|
|
2839
|
+
"#40FF00",
|
|
2840
|
+
"#00FF00",
|
|
2841
|
+
"#00FF40",
|
|
2842
|
+
"#00FF80",
|
|
2843
|
+
"#00FFCC",
|
|
2844
|
+
"#00FFFF",
|
|
2845
|
+
"#00CCFF",
|
|
2846
|
+
"#0080FF",
|
|
2847
|
+
"#0040FF",
|
|
2848
|
+
"#0000FF",
|
|
2849
|
+
"#4000FF",
|
|
2850
|
+
"#8000FF",
|
|
2851
|
+
"#CC00FF",
|
|
2852
|
+
"#FF00FF",
|
|
2853
|
+
"#FF00CC",
|
|
2854
|
+
"#FF0080",
|
|
2855
|
+
"#FF0040"
|
|
2856
|
+
],
|
|
2857
|
+
rgb48: [
|
|
2858
|
+
"#FF0000",
|
|
2859
|
+
"#FF2000",
|
|
2860
|
+
"#FF4000",
|
|
2861
|
+
"#FF6000",
|
|
2862
|
+
"#FF8000",
|
|
2863
|
+
"#FFAA00",
|
|
2864
|
+
"#FFCC00",
|
|
2865
|
+
"#FFEE00",
|
|
2866
|
+
"#FFFF00",
|
|
2867
|
+
"#DDFF00",
|
|
2868
|
+
"#CCFF00",
|
|
2869
|
+
"#AAFF00",
|
|
2870
|
+
"#80FF00",
|
|
2871
|
+
"#60FF00",
|
|
2872
|
+
"#40FF00",
|
|
2873
|
+
"#20FF00",
|
|
2874
|
+
"#00FF00",
|
|
2875
|
+
"#00FF20",
|
|
2876
|
+
"#00FF40",
|
|
2877
|
+
"#00FF60",
|
|
2878
|
+
"#00FF80",
|
|
2879
|
+
"#00FFAA",
|
|
2880
|
+
"#00FFCC",
|
|
2881
|
+
"#00FFDD",
|
|
2882
|
+
"#00FFFF",
|
|
2883
|
+
"#00DDFF",
|
|
2884
|
+
"#00CCFF",
|
|
2885
|
+
"#0099FF",
|
|
2886
|
+
"#0080FF",
|
|
2887
|
+
"#0060FF",
|
|
2888
|
+
"#0040FF",
|
|
2889
|
+
"#0020FF",
|
|
2890
|
+
"#0000FF",
|
|
2891
|
+
"#2000FF",
|
|
2892
|
+
"#4000FF",
|
|
2893
|
+
"#6000FF",
|
|
2894
|
+
"#8000FF",
|
|
2895
|
+
"#AA00FF",
|
|
2896
|
+
"#CC00FF",
|
|
2897
|
+
"#DD00FF",
|
|
2898
|
+
"#FF00FF",
|
|
2899
|
+
"#FF00EE",
|
|
2900
|
+
"#FF00CC",
|
|
2901
|
+
"#FF00AA",
|
|
2902
|
+
"#FF0080",
|
|
2903
|
+
"#FF0060",
|
|
2904
|
+
"#FF0040",
|
|
2905
|
+
"#FF0020"
|
|
2906
|
+
],
|
|
2907
|
+
rgb64: [
|
|
2908
|
+
"#000000",
|
|
2909
|
+
"#000055",
|
|
2910
|
+
"#0000AA",
|
|
2911
|
+
"#0000FF",
|
|
2912
|
+
"#005500",
|
|
2913
|
+
"#005555",
|
|
2914
|
+
"#0055AA",
|
|
2915
|
+
"#0055FF",
|
|
2916
|
+
"#00AA00",
|
|
2917
|
+
"#00AA55",
|
|
2918
|
+
"#00AAAA",
|
|
2919
|
+
"#00AAFF",
|
|
2920
|
+
"#00FF00",
|
|
2921
|
+
"#00FF55",
|
|
2922
|
+
"#00FFAA",
|
|
2923
|
+
"#00FFFF",
|
|
2924
|
+
"#550000",
|
|
2925
|
+
"#550055",
|
|
2926
|
+
"#5500AA",
|
|
2927
|
+
"#5500FF",
|
|
2928
|
+
"#555500",
|
|
2929
|
+
"#555555",
|
|
2930
|
+
"#5555AA",
|
|
2931
|
+
"#5555FF",
|
|
2932
|
+
"#55AA00",
|
|
2933
|
+
"#55AA55",
|
|
2934
|
+
"#55AAAA",
|
|
2935
|
+
"#55AAFF",
|
|
2936
|
+
"#55FF00",
|
|
2937
|
+
"#55FF55",
|
|
2938
|
+
"#55FFAA",
|
|
2939
|
+
"#55FFFF",
|
|
2940
|
+
"#AA0000",
|
|
2941
|
+
"#AA0055",
|
|
2942
|
+
"#AA00AA",
|
|
2943
|
+
"#AA00FF",
|
|
2944
|
+
"#AA5500",
|
|
2945
|
+
"#AA5555",
|
|
2946
|
+
"#AA55AA",
|
|
2947
|
+
"#AA55FF",
|
|
2948
|
+
"#AAAA00",
|
|
2949
|
+
"#AAAA55",
|
|
2950
|
+
"#AAAAAA",
|
|
2951
|
+
"#AAAAFF",
|
|
2952
|
+
"#AAFF00",
|
|
2953
|
+
"#AAFF55",
|
|
2954
|
+
"#AAFFAA",
|
|
2955
|
+
"#AAFFFF",
|
|
2956
|
+
"#FF0000",
|
|
2957
|
+
"#FF0055",
|
|
2958
|
+
"#FF00AA",
|
|
2959
|
+
"#FF00FF",
|
|
2960
|
+
"#FF5500",
|
|
2961
|
+
"#FF5555",
|
|
2962
|
+
"#FF55AA",
|
|
2963
|
+
"#FF55FF",
|
|
2964
|
+
"#FFAA00",
|
|
2965
|
+
"#FFAA55",
|
|
2966
|
+
"#FFAAAA",
|
|
2967
|
+
"#FFAAFF",
|
|
2968
|
+
"#FFFF00",
|
|
2969
|
+
"#FFFF55",
|
|
2970
|
+
"#FFFFAA",
|
|
2971
|
+
"#FFFFFF"
|
|
2972
|
+
]
|
|
2973
|
+
};
|
|
2974
|
+
|
|
2975
|
+
// src/three/particle-system-3d.ts
|
|
2976
|
+
import { BufferGeometry as BufferGeometry2, Float32BufferAttribute, Group as Group3, Points, ShaderMaterial, Vector3 as Vector33 } from "three";
|
|
2977
|
+
var vertexShader = `
|
|
2978
|
+
uniform float pointSize;
|
|
2979
|
+
|
|
2980
|
+
varying vec4 vColor;
|
|
2981
|
+
|
|
2982
|
+
void main() {
|
|
2983
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
2984
|
+
|
|
2985
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
2986
|
+
gl_PointSize = pointSize;
|
|
2987
|
+
|
|
2988
|
+
vColor = color;
|
|
2989
|
+
}`;
|
|
2990
|
+
var fragmentShader = `
|
|
2991
|
+
varying vec4 vColor;
|
|
2992
|
+
|
|
2993
|
+
void main() {
|
|
2994
|
+
gl_FragColor = vColor;
|
|
2995
|
+
}`;
|
|
2996
|
+
var Colors2 = [
|
|
2997
|
+
[1, 0, 0, 1],
|
|
2998
|
+
// red
|
|
2999
|
+
[0, 1, 0, 1],
|
|
3000
|
+
// green,
|
|
3001
|
+
[0, 0, 1, 1],
|
|
3002
|
+
// blue,
|
|
3003
|
+
[1, 1, 0, 1],
|
|
3004
|
+
// yellow,
|
|
3005
|
+
[1, 0, 1, 1]
|
|
3006
|
+
// magenta,
|
|
3007
|
+
];
|
|
3008
|
+
var ParticleSystem3d = class extends Group3 {
|
|
3009
|
+
particleCount = 1e3;
|
|
3010
|
+
_geometry;
|
|
3011
|
+
_particles = [];
|
|
3012
|
+
_frictionHalfLife = 0.04;
|
|
3013
|
+
_range = 0.2;
|
|
3014
|
+
_rangeFactor = 0.1;
|
|
3015
|
+
_attractionMatrix;
|
|
3016
|
+
_spatialPartition;
|
|
3017
|
+
constructor() {
|
|
3018
|
+
super();
|
|
3019
|
+
const uniforms = {
|
|
3020
|
+
pointSize: { value: 4 }
|
|
3021
|
+
};
|
|
3022
|
+
this.position.x = -0.5;
|
|
3023
|
+
this.position.y = -0.5;
|
|
3024
|
+
this.position.z = -0.5;
|
|
3025
|
+
this._geometry = new BufferGeometry2();
|
|
3026
|
+
this._geometry.setAttribute("position", new Float32BufferAttribute([], 3));
|
|
3027
|
+
this._geometry.setAttribute("color", new Float32BufferAttribute([], 4));
|
|
3028
|
+
const material = new ShaderMaterial({
|
|
3029
|
+
uniforms,
|
|
3030
|
+
vertexShader,
|
|
3031
|
+
fragmentShader,
|
|
3032
|
+
depthTest: true,
|
|
3033
|
+
depthWrite: false,
|
|
3034
|
+
vertexColors: true
|
|
3035
|
+
});
|
|
3036
|
+
const pointsMesh = new Points(this._geometry, material);
|
|
3037
|
+
this.add(pointsMesh);
|
|
3038
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
3039
|
+
this._particles.push({
|
|
3040
|
+
x: rng.nextf,
|
|
3041
|
+
y: rng.nextf,
|
|
3042
|
+
z: rng.nextf,
|
|
3043
|
+
vx: 0,
|
|
3044
|
+
vy: 0,
|
|
3045
|
+
vz: 0,
|
|
3046
|
+
color: rng.range(Colors2.length)
|
|
3047
|
+
});
|
|
3048
|
+
}
|
|
3049
|
+
this._attractionMatrix = rng.randomMatrix(Colors2.length);
|
|
3050
|
+
this._spatialPartition = new SpatialPartition3d(this._range, this._particles);
|
|
3051
|
+
}
|
|
3052
|
+
_calcForce(r, a, beta = 0.3) {
|
|
3053
|
+
let f = 0;
|
|
3054
|
+
if (r < beta) {
|
|
3055
|
+
f = r / beta - 1;
|
|
3056
|
+
} else if (beta < r && r < 1) {
|
|
3057
|
+
f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
3058
|
+
}
|
|
3059
|
+
return f;
|
|
3060
|
+
}
|
|
3061
|
+
_updateVelocities(dt) {
|
|
3062
|
+
const frictionFactor = Math.pow(
|
|
3063
|
+
0.5,
|
|
3064
|
+
dt / this._frictionHalfLife
|
|
3065
|
+
);
|
|
3066
|
+
for (let z = 0; z < this._spatialPartition.cells.length; z++) {
|
|
3067
|
+
for (let y = 0; y < this._spatialPartition.cells[z].length; y++) {
|
|
3068
|
+
for (let x = 0; x < this._spatialPartition.cells[z][y].length; x++) {
|
|
3069
|
+
const cell = this._spatialPartition.cells[z][y][x];
|
|
3070
|
+
for (let i = 0; i < cell.length; i++) {
|
|
3071
|
+
let fx = 0, fy = 0, fz = 0;
|
|
3072
|
+
const p1 = cell[i];
|
|
3073
|
+
const neighbors = this._spatialPartition.getCellNeighbors(x, y, z);
|
|
3074
|
+
for (const neighborCell of neighbors) {
|
|
3075
|
+
for (let j = 0; j < neighborCell.length; j++) {
|
|
3076
|
+
if (cell === neighborCell && i === j) continue;
|
|
3077
|
+
const p2 = neighborCell[j];
|
|
3078
|
+
let dx = p2.x - p1.x;
|
|
3079
|
+
let dy = p2.y - p1.y;
|
|
3080
|
+
let dz = p2.z - p1.z;
|
|
3081
|
+
if (Math.abs(dx) > 0.5) dx = dx > 0 ? dx - 1 : dx + 1;
|
|
3082
|
+
if (Math.abs(dy) > 0.5) dy = dy > 0 ? dy - 1 : dy + 1;
|
|
3083
|
+
if (Math.abs(dz) > 0.5) dz = dz > 0 ? dz - 1 : dz + 1;
|
|
3084
|
+
const dist = Math.hypot(dx, dy, dz);
|
|
3085
|
+
if (dist > 0 && dist < this._range) {
|
|
3086
|
+
const f = this._calcForce(dist / this._range, this._attractionMatrix[p1.color][p2.color]);
|
|
3087
|
+
fx += dx / dist * f;
|
|
3088
|
+
fy += dy / dist * f;
|
|
3089
|
+
fz += dz / dist * f;
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
fx *= this._range * this._rangeFactor;
|
|
3094
|
+
fy *= this._range * this._rangeFactor;
|
|
3095
|
+
fz *= this._range * this._rangeFactor;
|
|
3096
|
+
p1.vx *= frictionFactor;
|
|
3097
|
+
p1.vy *= frictionFactor;
|
|
3098
|
+
p1.vz *= frictionFactor;
|
|
3099
|
+
p1.vx += fx;
|
|
3100
|
+
p1.vy += fy;
|
|
3101
|
+
p1.vz += fz;
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
_updatePositions(dt) {
|
|
3108
|
+
for (let i = 0; i < this._particles.length; i++) {
|
|
3109
|
+
const particle = this._particles[i];
|
|
3110
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
3111
|
+
particle.x += particle.vx * dt;
|
|
3112
|
+
particle.y += particle.vy * dt;
|
|
3113
|
+
particle.z += particle.vz * dt;
|
|
3114
|
+
if (particle.x < 0) particle.x = (particle.x + 1) % 1;
|
|
3115
|
+
if (particle.x > 1) particle.x = particle.x % 1;
|
|
3116
|
+
if (particle.y < 0) particle.y = (particle.y + 1) % 1;
|
|
3117
|
+
if (particle.y > 1) particle.y = particle.y % 1;
|
|
3118
|
+
if (particle.z < 0) particle.z = (particle.z + 1) % 1;
|
|
3119
|
+
if (particle.z > 1) particle.z = particle.z % 1;
|
|
3120
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
3121
|
+
if (newCell !== oldCell) {
|
|
3122
|
+
const index = oldCell.indexOf(particle);
|
|
3123
|
+
if (index !== -1) {
|
|
3124
|
+
oldCell.splice(index, 1);
|
|
3125
|
+
}
|
|
3126
|
+
newCell.push(particle);
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
_sortParticles(camera) {
|
|
3131
|
+
this._particles.sort((a, b) => {
|
|
3132
|
+
const d1 = camera.position.distanceTo(new Vector33(a.x, a.y, a.z));
|
|
3133
|
+
const d2 = camera.position.distanceTo(new Vector33(b.x, b.y, b.z));
|
|
3134
|
+
return d1 > d2 ? -1 : d1 > d1 ? 1 : 0;
|
|
3135
|
+
});
|
|
3136
|
+
}
|
|
3137
|
+
_updateGeometry() {
|
|
3138
|
+
const positions = [];
|
|
3139
|
+
const colors2 = [];
|
|
3140
|
+
for (const particle of this._particles) {
|
|
3141
|
+
positions.push(
|
|
3142
|
+
particle.x,
|
|
3143
|
+
particle.y,
|
|
3144
|
+
particle.z
|
|
3145
|
+
);
|
|
3146
|
+
colors2.push(
|
|
3147
|
+
Colors2[particle.color][0],
|
|
3148
|
+
Colors2[particle.color][1],
|
|
3149
|
+
Colors2[particle.color][2],
|
|
3150
|
+
Colors2[particle.color][3]
|
|
3151
|
+
);
|
|
3152
|
+
}
|
|
3153
|
+
this._geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
|
|
3154
|
+
this._geometry.setAttribute("color", new Float32BufferAttribute(colors2, 4));
|
|
3155
|
+
this._geometry.attributes.position.needsUpdate = true;
|
|
3156
|
+
}
|
|
3157
|
+
update(camera, dt) {
|
|
3158
|
+
this._updateVelocities(dt);
|
|
3159
|
+
this._updatePositions(dt);
|
|
3160
|
+
this._sortParticles(camera);
|
|
3161
|
+
this._updateGeometry();
|
|
3162
|
+
}
|
|
3163
|
+
};
|
|
3164
|
+
|
|
3165
|
+
// src/three/asteroid-mesh.ts
|
|
3166
|
+
import { IcosahedronGeometry, Mesh as Mesh3, MeshStandardMaterial as MeshStandardMaterial2 } from "three";
|
|
3167
|
+
import { createNoise3D as createNoise3D2 } from "simplex-noise";
|
|
3168
|
+
var AsteroidMesh = class extends Mesh3 {
|
|
3169
|
+
constructor(options = {}) {
|
|
3170
|
+
const radius = options.radius ?? 1;
|
|
3171
|
+
const detail = options.detail ?? 1;
|
|
3172
|
+
const noiseScale = options.noiseScale ?? 1;
|
|
3173
|
+
const displacement = options.displacement ?? 0.1;
|
|
3174
|
+
const secondaryNoise = options.secondaryNoise ?? false;
|
|
3175
|
+
const color = options.color ?? 8947848;
|
|
3176
|
+
const geometry = new IcosahedronGeometry(radius, detail);
|
|
3177
|
+
const material = new MeshStandardMaterial2({ color });
|
|
3178
|
+
super(geometry, material);
|
|
3179
|
+
const noise3d = createNoise3D2(() => rng.nextf);
|
|
3180
|
+
const noise3d2 = secondaryNoise ? createNoise3D2(() => rng.nextf) : null;
|
|
3181
|
+
const position = this.geometry.attributes.position;
|
|
3182
|
+
const vertices = position.array;
|
|
3183
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
3184
|
+
const x = vertices[i];
|
|
3185
|
+
const y = vertices[i + 1];
|
|
3186
|
+
const z = vertices[i + 2];
|
|
3187
|
+
const n1 = noise3d(x * noiseScale, y * noiseScale, z * noiseScale);
|
|
3188
|
+
const n2 = secondaryNoise ? 0.5 * noise3d2(x * noiseScale * 2, y * noiseScale * 2, z * noiseScale * 2) : 0;
|
|
3189
|
+
const noiseValue = n1 + n2;
|
|
3190
|
+
const displacementValue = 1 + noiseValue * displacement;
|
|
3191
|
+
vertices[i] = x * displacementValue;
|
|
3192
|
+
vertices[i + 1] = y * displacementValue;
|
|
3193
|
+
vertices[i + 2] = z * displacementValue;
|
|
3194
|
+
}
|
|
3195
|
+
position.needsUpdate = true;
|
|
3196
|
+
this.geometry.computeVertexNormals();
|
|
3197
|
+
}
|
|
3198
|
+
};
|
|
3199
|
+
|
|
3200
|
+
// src/three/starfield.ts
|
|
3201
|
+
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";
|
|
3202
|
+
function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215) {
|
|
3203
|
+
const geometry = new BufferGeometry3();
|
|
3204
|
+
const positions = new Float32Array(count * 3);
|
|
3205
|
+
for (let i = 0; i < count; i++) {
|
|
3206
|
+
const theta = rng.nextf * Math.PI * 2;
|
|
3207
|
+
const phi = Math.acos(rng.nextf * 2 - 1);
|
|
3208
|
+
const x = radius * Math.sin(phi) * Math.cos(theta);
|
|
3209
|
+
const y = radius * Math.sin(phi) * Math.sin(theta);
|
|
3210
|
+
const z = radius * Math.cos(phi);
|
|
3211
|
+
positions.set([x, y, z], i * 3);
|
|
3212
|
+
}
|
|
3213
|
+
geometry.setAttribute("position", new BufferAttribute2(positions, 3));
|
|
3214
|
+
const material = new PointsMaterial({
|
|
3215
|
+
color,
|
|
3216
|
+
size: 5,
|
|
3217
|
+
sizeAttenuation: true,
|
|
3218
|
+
transparent: true,
|
|
3219
|
+
opacity: 0.9
|
|
3220
|
+
});
|
|
3221
|
+
const stars = new Points2(geometry, material);
|
|
3222
|
+
stars.name = "Starfield";
|
|
3223
|
+
return stars;
|
|
3224
|
+
}
|
|
3225
|
+
function CreateSystemStar(position) {
|
|
3226
|
+
const star = new Group4();
|
|
3227
|
+
const starPos = position ?? new Vector34();
|
|
3228
|
+
const starGeo = new SphereGeometry(50, 32, 32);
|
|
3229
|
+
const starMat = new MeshBasicMaterial2({ color: 16768426 });
|
|
3230
|
+
const starMesh = new Mesh4(starGeo, starMat);
|
|
3231
|
+
starMesh.position.copy(starPos);
|
|
3232
|
+
star.add(starMesh);
|
|
3233
|
+
const starLight = new PointLight(16777215, 10, 0, 0);
|
|
3234
|
+
starLight.position.copy(starPos);
|
|
3235
|
+
star.add(starLight);
|
|
3236
|
+
return star;
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3239
|
+
// src/three/palette-controller.ts
|
|
3240
|
+
import { Color as Color3, Vector3 as Vector35 } from "three";
|
|
3241
|
+
async function CreatePaletteController(parentGui, colorPalette, onChange, folderName = "palette") {
|
|
3242
|
+
const { Controller } = await import("three/examples/jsm/libs/lil-gui.module.min.js");
|
|
3243
|
+
class PaletteControllerImpl extends Controller {
|
|
3244
|
+
$innerDiv;
|
|
3245
|
+
colorButtons = [];
|
|
3246
|
+
colorButtonSize = 20;
|
|
3247
|
+
selectedBorderStyle = "2px dashed black";
|
|
3248
|
+
currentColorIndex = 0;
|
|
3249
|
+
eyedropperButton;
|
|
3250
|
+
colorPalette;
|
|
3251
|
+
_onColorChange;
|
|
3252
|
+
constructor() {
|
|
3253
|
+
const parent = parentGui.addFolder(folderName);
|
|
3254
|
+
super(parent, {}, "", "function");
|
|
3255
|
+
this.colorPalette = colorPalette;
|
|
3256
|
+
this._onColorChange = onChange;
|
|
3257
|
+
this.$widget.style.setProperty("display", "flex");
|
|
3258
|
+
this.$widget.style.setProperty("justify-content", "center");
|
|
3259
|
+
this.$innerDiv = document.createElement("div");
|
|
3260
|
+
this.$innerDiv.style.setProperty("display", "flex");
|
|
3261
|
+
this.$innerDiv.style.setProperty("flex-wrap", "wrap");
|
|
3262
|
+
this.$innerDiv.style.setProperty("width", `${this.colorButtonSize * Math.floor(Math.sqrt(this.colorPalette.length)) + 2}px`);
|
|
3263
|
+
this.$innerDiv.appendChild(this.$name);
|
|
3264
|
+
this.$widget.appendChild(this.$innerDiv);
|
|
3265
|
+
this.colorPalette.forEach((color) => this._createColorButton(color));
|
|
3266
|
+
if (window.EyeDropper) {
|
|
3267
|
+
this.eyedropperButton = parent.add({
|
|
3268
|
+
eyedropper: () => {
|
|
3269
|
+
this.eyedropperButton?.disable(true);
|
|
3270
|
+
new window.EyeDropper().open().then(({ sRGBHex }) => {
|
|
3271
|
+
const i = this.findClosestColorMatchIndex(sRGBHex);
|
|
3272
|
+
this.setSelectedColor(i);
|
|
3273
|
+
this._onColorChange?.(i);
|
|
3274
|
+
}).finally(() => this.eyedropperButton?.disable(false)).catch(() => {
|
|
3275
|
+
});
|
|
3276
|
+
}
|
|
3277
|
+
}, "eyedropper").name("findClosestColor");
|
|
3278
|
+
}
|
|
3279
|
+
this.name("");
|
|
3280
|
+
this.updateDisplay();
|
|
3281
|
+
}
|
|
3282
|
+
_onColorButtonClicked(i, button) {
|
|
3283
|
+
if (button === 0) {
|
|
3284
|
+
this.setSelectedColor(i);
|
|
3285
|
+
this._onColorChange?.(i);
|
|
3286
|
+
this.colorButtons[i].blur();
|
|
3287
|
+
} else if (button === 2 && window.EyeDropper) {
|
|
3288
|
+
new window.EyeDropper().open().then(({ sRGBHex }) => {
|
|
3289
|
+
this.colorPalette[i] = sRGBHex;
|
|
3290
|
+
this.colorButtons[i].setAttribute("title", `${sRGBHex}`);
|
|
3291
|
+
this.colorButtons[i].style.setProperty("background", sRGBHex);
|
|
3292
|
+
this.setSelectedColor(i);
|
|
3293
|
+
this._onColorChange?.(i);
|
|
3294
|
+
this.colorButtons[i].blur();
|
|
3295
|
+
}).finally(() => this.eyedropperButton?.disable(false)).catch(() => {
|
|
3296
|
+
});
|
|
3297
|
+
}
|
|
3298
|
+
}
|
|
3299
|
+
_createColorButton(color) {
|
|
3300
|
+
const i = this.colorButtons.length;
|
|
3301
|
+
const colorButton = document.createElement("div");
|
|
3302
|
+
colorButton.setAttribute("id", `colorButton${i}`);
|
|
3303
|
+
colorButton.setAttribute("title", `${color}`);
|
|
3304
|
+
colorButton.style.setProperty("width", `${this.colorButtonSize}px`);
|
|
3305
|
+
colorButton.style.setProperty("height", `${this.colorButtonSize}px`);
|
|
3306
|
+
colorButton.style.setProperty("background", color);
|
|
3307
|
+
colorButton.addEventListener("pointerup", (ev) => this._onColorButtonClicked(i, ev.button));
|
|
3308
|
+
this.colorButtons.push(colorButton);
|
|
3309
|
+
this.$innerDiv.appendChild(colorButton);
|
|
3310
|
+
}
|
|
3311
|
+
updatePaletteColors() {
|
|
3312
|
+
this.colorPalette.forEach((color, i) => {
|
|
3313
|
+
if (i < this.colorButtons.length) {
|
|
3314
|
+
this.colorButtons[i].setAttribute("title", `${color}`);
|
|
3315
|
+
this.colorButtons[i].style.setProperty("background", color);
|
|
3316
|
+
} else {
|
|
3317
|
+
this._createColorButton(color);
|
|
3318
|
+
}
|
|
3319
|
+
});
|
|
3320
|
+
}
|
|
3321
|
+
setSelectedColor(colorIndex = 0) {
|
|
3322
|
+
this.colorButtons[this.currentColorIndex].style.removeProperty("border");
|
|
3323
|
+
this.currentColorIndex = clamp(colorIndex, 0, this.colorPalette.length);
|
|
3324
|
+
this.colorButtons[this.currentColorIndex].style.setProperty("border", this.selectedBorderStyle);
|
|
3325
|
+
}
|
|
3326
|
+
findClosestColorMatchIndex(color) {
|
|
3327
|
+
const pickedColor = new Color3(color);
|
|
3328
|
+
const pickedVector = new Vector35().setFromColor(pickedColor);
|
|
3329
|
+
const list = [];
|
|
3330
|
+
this.colorPalette.forEach((paletteColorString, i) => {
|
|
3331
|
+
const paletteColor = new Color3(paletteColorString);
|
|
3332
|
+
const paletteVector = new Vector35().setFromColor(paletteColor);
|
|
3333
|
+
const d = paletteVector.distanceTo(pickedVector);
|
|
3334
|
+
list.push({ i, d });
|
|
3335
|
+
});
|
|
3336
|
+
list.sort((a, b) => a.d - b.d);
|
|
3337
|
+
const closest = list.shift();
|
|
3338
|
+
return closest?.i ?? 0;
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
return new PaletteControllerImpl();
|
|
3342
|
+
}
|
|
3343
|
+
|
|
3344
|
+
// src/three/ecs.ts
|
|
3345
|
+
import { Group as Group5 } from "three";
|
|
3346
|
+
var entityCounter = 0;
|
|
3347
|
+
function GenerateEntityId() {
|
|
3348
|
+
return `entity_${entityCounter++}`;
|
|
3349
|
+
}
|
|
3350
|
+
function CreateThreeEntity(obj) {
|
|
3351
|
+
const entity = obj ?? new Group5();
|
|
3352
|
+
entity.userData.entityId = GenerateEntityId();
|
|
3353
|
+
entity.userData.components = {};
|
|
3354
|
+
return entity;
|
|
3355
|
+
}
|
|
3356
|
+
function AddThreeComponent(entity, name, component) {
|
|
3357
|
+
entity.userData.components[name] = component;
|
|
3358
|
+
}
|
|
3359
|
+
function GetThreeComponent(entity, name) {
|
|
3360
|
+
return entity.userData.components[name];
|
|
3361
|
+
}
|
|
3362
|
+
function RemoveThreeComponent(entity, name) {
|
|
3363
|
+
delete entity.userData.components[name];
|
|
3364
|
+
}
|
|
3365
|
+
function IsThreeEntity(obj) {
|
|
3366
|
+
return obj.userData.entityId !== void 0;
|
|
3367
|
+
}
|
|
3368
|
+
|
|
3369
|
+
// src/worker-interface.ts
|
|
3370
|
+
var WorkerInterface = class extends Emitter {
|
|
3371
|
+
_worker;
|
|
3372
|
+
constructor(url) {
|
|
3373
|
+
super();
|
|
3374
|
+
this._worker = new Worker(url, { type: "module" });
|
|
3375
|
+
this._worker.onerror = (error) => this.emit("error", error);
|
|
3376
|
+
this._worker.onmessage = (message) => this.emit("message", message);
|
|
3377
|
+
}
|
|
3378
|
+
postMessage(message, options) {
|
|
3379
|
+
this._worker.postMessage(message, options);
|
|
3380
|
+
}
|
|
3381
|
+
terminate() {
|
|
3382
|
+
this._worker.terminate();
|
|
3383
|
+
}
|
|
3384
|
+
};
|
|
1514
3385
|
export {
|
|
3386
|
+
AStar,
|
|
3387
|
+
AddThreeComponent,
|
|
3388
|
+
AsteroidMesh,
|
|
3389
|
+
AtlasTextureMaterial,
|
|
3390
|
+
BuildDefaultPalette,
|
|
1515
3391
|
CanvasRenderer,
|
|
1516
3392
|
Clock,
|
|
1517
3393
|
ClockStats,
|
|
3394
|
+
Color,
|
|
3395
|
+
Colors,
|
|
1518
3396
|
CompileShader,
|
|
3397
|
+
CreatePaletteController,
|
|
1519
3398
|
CreateProgram,
|
|
1520
3399
|
CreateProgramInfo,
|
|
3400
|
+
CreateStarfield,
|
|
3401
|
+
CreateSubTexture,
|
|
3402
|
+
CreateSystemStar,
|
|
3403
|
+
CreateThreeEntity,
|
|
1521
3404
|
CreateWebGlContext,
|
|
1522
3405
|
CubeSphereGeometry,
|
|
1523
3406
|
ECS,
|
|
1524
3407
|
Emitter,
|
|
3408
|
+
EventBus,
|
|
3409
|
+
GenerateDoors,
|
|
3410
|
+
GenerateEntityId,
|
|
3411
|
+
GenerateRooms,
|
|
3412
|
+
GenerateWalls,
|
|
1525
3413
|
GetProgramLocations,
|
|
3414
|
+
GetThreeComponent,
|
|
1526
3415
|
GlobalEmitter,
|
|
1527
3416
|
Input,
|
|
3417
|
+
IsThreeEntity,
|
|
1528
3418
|
LoadTexture,
|
|
3419
|
+
Map2D,
|
|
3420
|
+
Map3D,
|
|
1529
3421
|
Noise,
|
|
3422
|
+
PI,
|
|
3423
|
+
ParticleSystem2d,
|
|
3424
|
+
ParticleSystem3d,
|
|
3425
|
+
PoissonDiskSampler,
|
|
3426
|
+
Rectangle,
|
|
3427
|
+
RemoveThreeComponent,
|
|
3428
|
+
Renderer,
|
|
1530
3429
|
ResizeWebGlContext,
|
|
1531
3430
|
Rng,
|
|
1532
3431
|
ShaderType,
|
|
1533
3432
|
SimplexNoise,
|
|
3433
|
+
SpatialPartition2d,
|
|
3434
|
+
SpatialPartition3d,
|
|
3435
|
+
TILE_EMPTY,
|
|
3436
|
+
TILE_FLOOR,
|
|
3437
|
+
TILE_PATH,
|
|
1534
3438
|
TextureAtlas,
|
|
1535
3439
|
ThreeJsBoilerPlate,
|
|
1536
3440
|
ThreeJsCameraRig,
|
|
1537
3441
|
ThreeJsPlayerController,
|
|
1538
|
-
|
|
3442
|
+
Tilemap,
|
|
3443
|
+
Vector,
|
|
3444
|
+
VoxelFaces,
|
|
3445
|
+
VoxelMesh,
|
|
1539
3446
|
WebGlRenderer,
|
|
3447
|
+
WorkerInterface,
|
|
3448
|
+
abs,
|
|
3449
|
+
ceil,
|
|
1540
3450
|
clamp,
|
|
3451
|
+
cos,
|
|
1541
3452
|
deg2rad,
|
|
3453
|
+
distance,
|
|
3454
|
+
eventBus,
|
|
3455
|
+
floor,
|
|
1542
3456
|
getRedYellowGreenGradientHex,
|
|
3457
|
+
hypot2,
|
|
3458
|
+
hypot3,
|
|
3459
|
+
imul,
|
|
1543
3460
|
isNullOrUndefined,
|
|
1544
|
-
key2xyz,
|
|
1545
3461
|
lerp,
|
|
1546
3462
|
log,
|
|
1547
3463
|
logdev,
|
|
1548
3464
|
logerr,
|
|
1549
3465
|
loginf,
|
|
1550
3466
|
logwrn,
|
|
3467
|
+
manhattanDistance,
|
|
3468
|
+
max,
|
|
3469
|
+
min,
|
|
3470
|
+
pow,
|
|
1551
3471
|
rng,
|
|
1552
|
-
|
|
3472
|
+
round,
|
|
3473
|
+
roundTo,
|
|
3474
|
+
sin,
|
|
3475
|
+
sqrt,
|
|
3476
|
+
squaredDistance
|
|
1553
3477
|
};
|