@n3rdw1z4rd/core 0.20.0 → 0.21.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/graph/doc.d.ts +141 -0
- package/dist/graph/doc.js +173 -0
- package/dist/graph/evaluate.d.ts +114 -0
- package/dist/graph/evaluate.js +319 -0
- package/dist/graph/index.d.ts +9 -0
- package/dist/graph/index.js +9 -0
- package/dist/graph/node.d.ts +153 -0
- package/dist/graph/node.js +94 -0
- package/dist/graph/nodes.d.ts +11 -0
- package/dist/graph/nodes.js +399 -0
- package/dist/graph/script.d.ts +56 -0
- package/dist/graph/script.js +98 -0
- package/dist/graph/sources.d.ts +11 -0
- package/dist/graph/sources.js +263 -0
- package/dist/graph/subgraph.d.ts +36 -0
- package/dist/graph/subgraph.js +280 -0
- package/dist/graph/types.d.ts +56 -0
- package/dist/graph/types.js +67 -0
- package/dist/graph.cjs +1709 -0
- package/dist/graph.d.cts +568 -0
- package/dist/index.cjs +53 -52
- package/dist/rng-Dhqq7owV.cjs +642 -0
- package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
- package/dist/three/adaptive-grid.d.ts +43 -0
- package/dist/three/adaptive-grid.js +127 -0
- package/dist/three/ecs.d.ts +5 -3
- package/dist/three/ecs.js +5 -3
- package/dist/three/editor-camera.d.ts +93 -0
- package/dist/three/editor-camera.js +174 -0
- package/dist/three/index.d.ts +6 -1
- package/dist/three/index.js +6 -1
- package/dist/three/picking.d.ts +34 -0
- package/dist/three/picking.js +59 -0
- package/dist/three/schema.d.ts +139 -0
- package/dist/three/schema.js +214 -0
- package/dist/three/world.d.ts +192 -0
- package/dist/three/world.js +363 -0
- package/dist/three.cjs +970 -23
- package/dist/three.d.cts +497 -5
- package/dist/vite.cjs +40 -0
- package/dist/vite.d.cts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +39 -0
- package/package.json +21 -1
package/dist/three.cjs
CHANGED
|
@@ -1,6 +1,132 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_rng = require("./rng-Dhqq7owV.cjs");
|
|
3
|
+
const require_spatial_partition_3d = require("./spatial-partition-3d-CmSCL87Z.cjs");
|
|
3
4
|
let three = require("three");
|
|
5
|
+
//#region src/three/adaptive-grid.ts
|
|
6
|
+
/**
|
|
7
|
+
* An infinite-looking grid on the XY plane for editor views: each {@link AdaptiveGrid.update}
|
|
8
|
+
* rebuilds just the lines in view, doubling the spacing when zoomed out so lines never get
|
|
9
|
+
* closer than `minSpacingPx`. Optional origin cross.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const grid = new AdaptiveGrid({ color: '#262626' });
|
|
13
|
+
* scene.add(grid);
|
|
14
|
+
* // per frame, after moving the camera:
|
|
15
|
+
* grid.update(view, 32);
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @category Three.js
|
|
19
|
+
*/
|
|
20
|
+
var AdaptiveGrid = class extends three.LineSegments {
|
|
21
|
+
minSpacingPx;
|
|
22
|
+
/** draw the grid lines (false: just the origin cross, if `origin`) */
|
|
23
|
+
lines = true;
|
|
24
|
+
/** draw a small cross at the origin (`originSizePx` long) */
|
|
25
|
+
origin;
|
|
26
|
+
originSizePx;
|
|
27
|
+
/** the most lines per axis (a far-off perspective view stops there) */
|
|
28
|
+
maxLines = 400;
|
|
29
|
+
gridColor;
|
|
30
|
+
originColor;
|
|
31
|
+
positions = /* @__PURE__ */ new Float32Array(0);
|
|
32
|
+
colors = /* @__PURE__ */ new Float32Array(0);
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
super(new three.BufferGeometry(), new three.LineBasicMaterial({
|
|
35
|
+
vertexColors: true,
|
|
36
|
+
depthWrite: false,
|
|
37
|
+
transparent: true
|
|
38
|
+
}));
|
|
39
|
+
this.gridColor = new three.Color(options.color ?? "#262626");
|
|
40
|
+
this.originColor = new three.Color(options.originColor ?? "#555555");
|
|
41
|
+
this.minSpacingPx = options.minSpacingPx ?? 8;
|
|
42
|
+
this.origin = options.origin ?? true;
|
|
43
|
+
this.originSizePx = options.originSizePx ?? 10;
|
|
44
|
+
this.frustumCulled = false;
|
|
45
|
+
this.renderOrder = -1;
|
|
46
|
+
}
|
|
47
|
+
/** Rebuilds the lines for the current view; `cell` is the base spacing in world units. */
|
|
48
|
+
update(view, cell, z = 0) {
|
|
49
|
+
let step = cell;
|
|
50
|
+
while (step * view.zoom < this.minSpacingPx) step *= 2;
|
|
51
|
+
const pts = [
|
|
52
|
+
[0, 0],
|
|
53
|
+
[view.width, 0],
|
|
54
|
+
[0, view.height],
|
|
55
|
+
[view.width, view.height],
|
|
56
|
+
[view.width / 2, view.height / 2]
|
|
57
|
+
].map(([x, y]) => view.screenToWorld(x, y, z)).filter((p) => p !== null);
|
|
58
|
+
const far = step * this.maxLines / 2;
|
|
59
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
60
|
+
for (const p of pts) {
|
|
61
|
+
x0 = Math.min(x0, p.x);
|
|
62
|
+
x1 = Math.max(x1, p.x);
|
|
63
|
+
y0 = Math.min(y0, p.y);
|
|
64
|
+
y1 = Math.max(y1, p.y);
|
|
65
|
+
}
|
|
66
|
+
if (!pts.length || pts.length < 5) {
|
|
67
|
+
x0 = view.target.x - far;
|
|
68
|
+
x1 = view.target.x + far;
|
|
69
|
+
y0 = view.target.y - far;
|
|
70
|
+
y1 = view.target.y + far;
|
|
71
|
+
}
|
|
72
|
+
x0 = Math.max(x0, view.target.x - far);
|
|
73
|
+
x1 = Math.min(x1, view.target.x + far);
|
|
74
|
+
y0 = Math.max(y0, view.target.y - far);
|
|
75
|
+
y1 = Math.min(y1, view.target.y + far);
|
|
76
|
+
x0 = Math.floor(x0 / step) * step;
|
|
77
|
+
x1 = Math.ceil(x1 / step) * step;
|
|
78
|
+
y0 = Math.floor(y0 / step) * step;
|
|
79
|
+
y1 = Math.ceil(y1 / step) * step;
|
|
80
|
+
const nx = this.lines ? Math.max(0, Math.round((x1 - x0) / step) + 1) : 0;
|
|
81
|
+
const ny = this.lines ? Math.max(0, Math.round((y1 - y0) / step) + 1) : 0;
|
|
82
|
+
const segments = nx + ny + (this.origin ? 2 : 0);
|
|
83
|
+
this.ensure(segments * 2);
|
|
84
|
+
const pos = this.positions;
|
|
85
|
+
const col = this.colors;
|
|
86
|
+
const gc = this.gridColor;
|
|
87
|
+
let i = 0;
|
|
88
|
+
const seg = (ax, ay, bx, by, c) => {
|
|
89
|
+
pos[i * 3] = ax;
|
|
90
|
+
pos[i * 3 + 1] = ay;
|
|
91
|
+
pos[i * 3 + 2] = z;
|
|
92
|
+
col[i * 3] = c.r;
|
|
93
|
+
col[i * 3 + 1] = c.g;
|
|
94
|
+
col[i * 3 + 2] = c.b;
|
|
95
|
+
i++;
|
|
96
|
+
pos[i * 3] = bx;
|
|
97
|
+
pos[i * 3 + 1] = by;
|
|
98
|
+
pos[i * 3 + 2] = z;
|
|
99
|
+
col[i * 3] = c.r;
|
|
100
|
+
col[i * 3 + 1] = c.g;
|
|
101
|
+
col[i * 3 + 2] = c.b;
|
|
102
|
+
i++;
|
|
103
|
+
};
|
|
104
|
+
for (let k = 0; k < nx; k++) seg(x0 + k * step, y0, x0 + k * step, y1, gc);
|
|
105
|
+
for (let k = 0; k < ny; k++) seg(x0, y0 + k * step, x1, y0 + k * step, gc);
|
|
106
|
+
if (this.origin) {
|
|
107
|
+
const s = this.originSizePx / view.zoom;
|
|
108
|
+
seg(-s, 0, s, 0, this.originColor);
|
|
109
|
+
seg(0, -s, 0, s, this.originColor);
|
|
110
|
+
}
|
|
111
|
+
const g = this.geometry;
|
|
112
|
+
g.getAttribute("position").needsUpdate = true;
|
|
113
|
+
g.getAttribute("color").needsUpdate = true;
|
|
114
|
+
g.setDrawRange(0, i);
|
|
115
|
+
}
|
|
116
|
+
ensure(vertices) {
|
|
117
|
+
if (this.positions.length >= vertices * 3) return;
|
|
118
|
+
const size = Math.max(vertices * 3, this.positions.length * 2, 192);
|
|
119
|
+
this.positions = new Float32Array(size);
|
|
120
|
+
this.colors = new Float32Array(size);
|
|
121
|
+
this.geometry.setAttribute("position", new three.BufferAttribute(this.positions, 3));
|
|
122
|
+
this.geometry.setAttribute("color", new three.BufferAttribute(this.colors, 3));
|
|
123
|
+
}
|
|
124
|
+
dispose() {
|
|
125
|
+
this.geometry.dispose();
|
|
126
|
+
this.material.dispose();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
//#endregion
|
|
4
130
|
//#region src/three/asteroid-mesh.ts
|
|
5
131
|
/**
|
|
6
132
|
* Lumpy rock: an icosphere whose vertices are pushed in and out by 3D noise, with a
|
|
@@ -45,7 +171,7 @@ var AsteroidMesh = class extends three.Mesh {
|
|
|
45
171
|
//#endregion
|
|
46
172
|
//#region src/three/camera-rig.ts
|
|
47
173
|
const DEFAULT_DISTANCE = 5;
|
|
48
|
-
const damp = (current, target, damping, deltaTime) => damping > 0 ?
|
|
174
|
+
const damp = (current, target, damping, deltaTime) => damping > 0 ? require_rng.lerp(current, target, 1 - Math.exp(-damping * deltaTime)) : target;
|
|
49
175
|
/**
|
|
50
176
|
* Third-person camera that orbits a target and zooms seamlessly into first person.
|
|
51
177
|
*
|
|
@@ -91,13 +217,13 @@ var CameraRig = class extends three.Group {
|
|
|
91
217
|
firstPersonThreshold = .5;
|
|
92
218
|
/** pitch range in third person; default -80° to 30° */
|
|
93
219
|
pitchLimits = {
|
|
94
|
-
min:
|
|
95
|
-
max:
|
|
220
|
+
min: require_rng.radians(-80),
|
|
221
|
+
max: require_rng.radians(30)
|
|
96
222
|
};
|
|
97
223
|
/** pitch range in first person; the range blends between the two while zooming */
|
|
98
224
|
firstPersonPitchLimits = {
|
|
99
|
-
min:
|
|
100
|
-
max:
|
|
225
|
+
min: require_rng.radians(-89),
|
|
226
|
+
max: require_rng.radians(89)
|
|
101
227
|
};
|
|
102
228
|
/** sideways camera shift in third person (over-the-shoulder) */
|
|
103
229
|
shoulderOffset = 0;
|
|
@@ -192,7 +318,7 @@ var CameraRig = class extends three.Group {
|
|
|
192
318
|
this._clampPitch();
|
|
193
319
|
this.rotation.set(0, this.yaw, 0);
|
|
194
320
|
this.gimbal.rotation.set(this.pitch, 0, 0);
|
|
195
|
-
const shoulder = this.shoulderOffset *
|
|
321
|
+
const shoulder = this.shoulderOffset * require_rng.saturate(this._distance / this.shoulderFadeDistance);
|
|
196
322
|
this._cameraDistance = this._collide(shoulder, this._distance, deltaTime);
|
|
197
323
|
const scale = this._distance > 0 ? this._cameraDistance / this._distance : 0;
|
|
198
324
|
this.camera.position.set(shoulder * scale, 0, this._cameraDistance);
|
|
@@ -222,12 +348,12 @@ var CameraRig = class extends three.Group {
|
|
|
222
348
|
return distance;
|
|
223
349
|
}
|
|
224
350
|
_clampDistance(distance) {
|
|
225
|
-
distance =
|
|
351
|
+
distance = require_rng.clamp(distance, this.minDistance, this.maxDistance);
|
|
226
352
|
return this.minDistance <= 0 && distance < this.firstPersonThreshold ? 0 : distance;
|
|
227
353
|
}
|
|
228
354
|
_clampPitch() {
|
|
229
|
-
const t =
|
|
230
|
-
this.pitch =
|
|
355
|
+
const t = require_rng.saturate(this._distance / Math.max(this.firstPersonThreshold, 1e-6));
|
|
356
|
+
this.pitch = require_rng.clamp(this.pitch, require_rng.lerp(this.firstPersonPitchLimits.min, this.pitchLimits.min, t), require_rng.lerp(this.firstPersonPitchLimits.max, this.pitchLimits.max, t));
|
|
231
357
|
}
|
|
232
358
|
_setFirstPerson(firstPerson) {
|
|
233
359
|
if (firstPerson === this._firstPerson) return;
|
|
@@ -479,14 +605,16 @@ let entityCounter = 0;
|
|
|
479
605
|
/**
|
|
480
606
|
* Returns a new id `"entity_<n>"` from a module-level counter (unique per page load, not random).
|
|
481
607
|
*
|
|
608
|
+
* @deprecated entities now use their `Object3D.uuid` as id (stable across save and load);
|
|
609
|
+
* this is no longer used by {@link CreateThreeEntity}
|
|
482
610
|
* @category ECS
|
|
483
611
|
*/
|
|
484
612
|
function GenerateEntityId() {
|
|
485
613
|
return `entity_${entityCounter++}`;
|
|
486
614
|
}
|
|
487
615
|
/**
|
|
488
|
-
* Turns `obj` (default: a new `Group`) into an entity in place:
|
|
489
|
-
*
|
|
616
|
+
* Turns `obj` (default: a new `Group`) into an entity in place: an empty component set
|
|
617
|
+
* (replacing any it had), with its `uuid` as id.
|
|
490
618
|
*
|
|
491
619
|
* ```ts
|
|
492
620
|
* const ship = CreateThreeEntity(shipMesh);
|
|
@@ -499,7 +627,7 @@ function GenerateEntityId() {
|
|
|
499
627
|
*/
|
|
500
628
|
function CreateThreeEntity(obj) {
|
|
501
629
|
const entity = obj ?? new three.Group();
|
|
502
|
-
entity.userData.entityId =
|
|
630
|
+
entity.userData.entityId = entity.uuid;
|
|
503
631
|
entity.userData.components = {};
|
|
504
632
|
return entity;
|
|
505
633
|
}
|
|
@@ -536,6 +664,178 @@ function IsThreeEntity(obj) {
|
|
|
536
664
|
return obj.userData.entityId !== void 0;
|
|
537
665
|
}
|
|
538
666
|
//#endregion
|
|
667
|
+
//#region src/three/editor-camera.ts
|
|
668
|
+
/**
|
|
669
|
+
* An editor viewport camera: orthographic or perspective, with pan, zoom-at-cursor, orbit
|
|
670
|
+
* and fit-to-bounds, and conversions between screen (CSS px, relative to the canvas) and
|
|
671
|
+
* world coordinates.
|
|
672
|
+
*
|
|
673
|
+
* Both projections share one rig — a `target` the camera looks at, `yaw`/`pitch` around it,
|
|
674
|
+
* and a `zoom` in screen px per world unit at the target — so switching projection keeps the
|
|
675
|
+
* framing. `yaw = pitch = 0` looks down -Z at the XY plane: a 2D view (three.js convention,
|
|
676
|
+
* +Y up). Set `orbit = false` to lock it there.
|
|
677
|
+
*
|
|
678
|
+
* ```ts
|
|
679
|
+
* const view = new EditorCamera();
|
|
680
|
+
* view.resize(canvas.clientWidth, canvas.clientHeight);
|
|
681
|
+
* view.pan(dx, dy); // the world follows the pointer
|
|
682
|
+
* view.zoomAt(x, y, 1.1); // the point under the cursor stays put
|
|
683
|
+
* renderer.render(scene, view.camera); // the active three.js camera (updated by the calls above)
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* @category Three.js
|
|
687
|
+
*/
|
|
688
|
+
var EditorCamera = class {
|
|
689
|
+
ortho;
|
|
690
|
+
perspective;
|
|
691
|
+
/** the point looked at (and orbited around) */
|
|
692
|
+
target = new three.Vector3();
|
|
693
|
+
/** radians around +Y */
|
|
694
|
+
yaw = 0;
|
|
695
|
+
/** radians; positive looks down from above */
|
|
696
|
+
pitch = 0;
|
|
697
|
+
/** screen px per world unit, at the target */
|
|
698
|
+
zoom = 1;
|
|
699
|
+
minZoom = .01;
|
|
700
|
+
maxZoom = 100;
|
|
701
|
+
/** false locks yaw/pitch at 0 (a 2D view) */
|
|
702
|
+
orbit = true;
|
|
703
|
+
/** pitch limit, radians */
|
|
704
|
+
maxPitch = Math.PI / 2 - .01;
|
|
705
|
+
/** view size, CSS px */
|
|
706
|
+
width = 1;
|
|
707
|
+
height = 1;
|
|
708
|
+
_projection = "orthographic";
|
|
709
|
+
raycaster = new three.Raycaster();
|
|
710
|
+
ndc = new three.Vector2();
|
|
711
|
+
plane = new three.Plane();
|
|
712
|
+
constructor(options = {}) {
|
|
713
|
+
this.ortho = new three.OrthographicCamera(-1, 1, 1, -1, .1, 1e6);
|
|
714
|
+
this.perspective = new three.PerspectiveCamera(options.fov ?? 50, 1, .1, 1e6);
|
|
715
|
+
this._projection = options.projection ?? "orthographic";
|
|
716
|
+
if (options.orbit !== void 0) this.orbit = options.orbit;
|
|
717
|
+
this.update();
|
|
718
|
+
}
|
|
719
|
+
get projection() {
|
|
720
|
+
return this._projection;
|
|
721
|
+
}
|
|
722
|
+
set projection(p) {
|
|
723
|
+
this._projection = p;
|
|
724
|
+
this.update();
|
|
725
|
+
}
|
|
726
|
+
/** the active three.js camera */
|
|
727
|
+
get camera() {
|
|
728
|
+
return this._projection === "orthographic" ? this.ortho : this.perspective;
|
|
729
|
+
}
|
|
730
|
+
/** distance from the target that gives `zoom` px per unit (perspective) */
|
|
731
|
+
get distance() {
|
|
732
|
+
const halfFov = three.MathUtils.degToRad(this.perspective.fov) / 2;
|
|
733
|
+
return this.height / 2 / Math.tan(halfFov) / this.zoom;
|
|
734
|
+
}
|
|
735
|
+
resize(width, height) {
|
|
736
|
+
this.width = Math.max(1, width);
|
|
737
|
+
this.height = Math.max(1, height);
|
|
738
|
+
this.update();
|
|
739
|
+
}
|
|
740
|
+
/** Recomputes both cameras from the rig; the other methods call it. */
|
|
741
|
+
update() {
|
|
742
|
+
if (!this.orbit) this.yaw = this.pitch = 0;
|
|
743
|
+
this.pitch = three.MathUtils.clamp(this.pitch, -this.maxPitch, this.maxPitch);
|
|
744
|
+
this.zoom = three.MathUtils.clamp(this.zoom, this.minZoom, this.maxZoom);
|
|
745
|
+
const dir = new three.Vector3(Math.sin(this.yaw) * Math.cos(this.pitch), Math.sin(this.pitch), Math.cos(this.yaw) * Math.cos(this.pitch));
|
|
746
|
+
const p = this.perspective;
|
|
747
|
+
p.aspect = this.width / this.height;
|
|
748
|
+
p.position.copy(this.target).addScaledVector(dir, this.distance);
|
|
749
|
+
p.near = Math.max(.01, this.distance / 1e3);
|
|
750
|
+
p.far = this.distance * 1e3;
|
|
751
|
+
p.lookAt(this.target);
|
|
752
|
+
p.updateProjectionMatrix();
|
|
753
|
+
const o = this.ortho;
|
|
754
|
+
const hw = this.width / 2 / this.zoom;
|
|
755
|
+
const hh = this.height / 2 / this.zoom;
|
|
756
|
+
o.left = -hw;
|
|
757
|
+
o.right = hw;
|
|
758
|
+
o.top = hh;
|
|
759
|
+
o.bottom = -hh;
|
|
760
|
+
const back = Math.max(1e4, Math.max(hw, hh) * 20);
|
|
761
|
+
o.near = .1;
|
|
762
|
+
o.far = back * 2;
|
|
763
|
+
o.position.copy(this.target).addScaledVector(dir, back);
|
|
764
|
+
o.lookAt(this.target);
|
|
765
|
+
o.updateProjectionMatrix();
|
|
766
|
+
p.updateMatrixWorld();
|
|
767
|
+
o.updateMatrixWorld();
|
|
768
|
+
}
|
|
769
|
+
/** A ray from the camera through the screen point (CSS px). */
|
|
770
|
+
rayAt(sx, sy) {
|
|
771
|
+
this.ndc.set(sx / this.width * 2 - 1, -(sy / this.height) * 2 + 1);
|
|
772
|
+
this.raycaster.setFromCamera(this.ndc, this.camera);
|
|
773
|
+
return this.raycaster;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* The world point under the screen point on the plane `z = planeZ` (default: through the
|
|
777
|
+
* target), or null if the view is edge-on to it.
|
|
778
|
+
*/
|
|
779
|
+
screenToWorld(sx, sy, planeZ = this.target.z, out = new three.Vector3()) {
|
|
780
|
+
this.plane.set(new three.Vector3(0, 0, 1), -planeZ);
|
|
781
|
+
return this.rayAt(sx, sy).ray.intersectPlane(this.plane, out);
|
|
782
|
+
}
|
|
783
|
+
/** The screen point (CSS px) of a world point; `z` > 1 means behind the camera. */
|
|
784
|
+
worldToScreen(point) {
|
|
785
|
+
const v = point.clone().project(this.camera);
|
|
786
|
+
return {
|
|
787
|
+
x: (v.x + 1) / 2 * this.width,
|
|
788
|
+
y: (1 - v.y) / 2 * this.height,
|
|
789
|
+
z: v.z
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
/** Pans by a screen-space delta: the world follows the pointer. */
|
|
793
|
+
pan(dx, dy) {
|
|
794
|
+
const cam = this.camera;
|
|
795
|
+
const right = new three.Vector3().setFromMatrixColumn(cam.matrixWorld, 0);
|
|
796
|
+
const up = new three.Vector3().setFromMatrixColumn(cam.matrixWorld, 1);
|
|
797
|
+
this.target.addScaledVector(right, -dx / this.zoom).addScaledVector(up, dy / this.zoom);
|
|
798
|
+
this.update();
|
|
799
|
+
}
|
|
800
|
+
/** Orbits by a screen-space delta (no-op while `orbit` is false). */
|
|
801
|
+
rotate(dx, dy, speed = .005) {
|
|
802
|
+
if (!this.orbit) return;
|
|
803
|
+
this.yaw -= dx * speed;
|
|
804
|
+
this.pitch += dy * speed;
|
|
805
|
+
this.update();
|
|
806
|
+
}
|
|
807
|
+
/** Zooms by `factor`, keeping the world point under (sx, sy) fixed where possible. */
|
|
808
|
+
zoomAt(sx, sy, factor) {
|
|
809
|
+
const before = this.screenToWorld(sx, sy);
|
|
810
|
+
this.zoom *= factor;
|
|
811
|
+
this.update();
|
|
812
|
+
const after = before && this.screenToWorld(sx, sy);
|
|
813
|
+
if (before && after) {
|
|
814
|
+
this.target.add(before.sub(after));
|
|
815
|
+
this.update();
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Frames a box on the XY plane inside the view, leaving `margin` CSS px and keeping
|
|
820
|
+
* clear of `insets` (screen edges covered by UI). Keeps yaw/pitch.
|
|
821
|
+
*/
|
|
822
|
+
fit(minX, minY, maxX, maxY, insets = {
|
|
823
|
+
l: 0,
|
|
824
|
+
r: 0,
|
|
825
|
+
t: 0,
|
|
826
|
+
b: 0
|
|
827
|
+
}, margin = 48, maxZoom = 2) {
|
|
828
|
+
const w = Math.max(1e-6, maxX - minX);
|
|
829
|
+
const h = Math.max(1e-6, maxY - minY);
|
|
830
|
+
const viewW = Math.max(1, this.width - insets.l - insets.r - margin * 2);
|
|
831
|
+
const viewH = Math.max(1, this.height - insets.t - insets.b - margin * 2);
|
|
832
|
+
this.zoom = three.MathUtils.clamp(Math.min(viewW / w, viewH / h), this.minZoom, Math.min(maxZoom, this.maxZoom));
|
|
833
|
+
this.target.set((minX + maxX) / 2, (minY + maxY) / 2, this.target.z);
|
|
834
|
+
this.update();
|
|
835
|
+
this.pan((insets.l - insets.r) / 2, (insets.t - insets.b) / 2);
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
//#endregion
|
|
539
839
|
//#region src/three/particle-system-3d.ts
|
|
540
840
|
const vertexShader = `
|
|
541
841
|
uniform float pointSize;
|
|
@@ -626,7 +926,7 @@ var ParticleSystem3d = class extends three.Group {
|
|
|
626
926
|
_attractionMatrix;
|
|
627
927
|
_spatialPartition;
|
|
628
928
|
/** the same seed always produces the same starting state (positions, colors, attraction matrix) */
|
|
629
|
-
constructor(seed =
|
|
929
|
+
constructor(seed = require_rng.rng.nextUint()) {
|
|
630
930
|
super();
|
|
631
931
|
this.seed = seed;
|
|
632
932
|
const uniforms = { pointSize: { value: 4 } };
|
|
@@ -650,13 +950,13 @@ var ParticleSystem3d = class extends three.Group {
|
|
|
650
950
|
const pointsMesh = new three.Points(this._geometry, material);
|
|
651
951
|
this.add(pointsMesh);
|
|
652
952
|
for (let i = 0; i < this.particleCount; i++) this._particles.push({
|
|
653
|
-
x:
|
|
654
|
-
y:
|
|
655
|
-
z:
|
|
953
|
+
x: require_rng.Random.float(seed, i, 0),
|
|
954
|
+
y: require_rng.Random.float(seed, i, 1),
|
|
955
|
+
z: require_rng.Random.float(seed, i, 2),
|
|
656
956
|
vx: 0,
|
|
657
957
|
vy: 0,
|
|
658
958
|
vz: 0,
|
|
659
|
-
color: Math.floor(
|
|
959
|
+
color: Math.floor(require_rng.Random.float(seed, i, 3) * Colors.length)
|
|
660
960
|
});
|
|
661
961
|
this._attractionMatrix = require_spatial_partition_3d.randomMatrix(Colors.length, seed);
|
|
662
962
|
this._spatialPartition = new require_spatial_partition_3d.SpatialPartition3d(this._range, this._particles);
|
|
@@ -712,9 +1012,9 @@ var ParticleSystem3d = class extends three.Group {
|
|
|
712
1012
|
particle.x += particle.vx * dt;
|
|
713
1013
|
particle.y += particle.vy * dt;
|
|
714
1014
|
particle.z += particle.vz * dt;
|
|
715
|
-
particle.x =
|
|
716
|
-
particle.y =
|
|
717
|
-
particle.z =
|
|
1015
|
+
particle.x = require_rng.fract(particle.x);
|
|
1016
|
+
particle.y = require_rng.fract(particle.y);
|
|
1017
|
+
particle.z = require_rng.fract(particle.z);
|
|
718
1018
|
const newCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
719
1019
|
if (newCell !== oldCell) {
|
|
720
1020
|
const index = oldCell.indexOf(particle);
|
|
@@ -753,6 +1053,64 @@ var ParticleSystem3d = class extends three.Group {
|
|
|
753
1053
|
}
|
|
754
1054
|
};
|
|
755
1055
|
//#endregion
|
|
1056
|
+
//#region src/three/picking.ts
|
|
1057
|
+
/**
|
|
1058
|
+
* The entity under a screen point (CSS px): raycasts the world's scene and walks up from
|
|
1059
|
+
* whatever was hit to the closest entity of `world`. Invisible objects are skipped.
|
|
1060
|
+
*
|
|
1061
|
+
* @category Three.js
|
|
1062
|
+
*/
|
|
1063
|
+
function pickEntity(world, view, sx, sy, options = {}) {
|
|
1064
|
+
world.root.updateMatrixWorld();
|
|
1065
|
+
const direct = castAt(world, view, sx, sy, options.filter);
|
|
1066
|
+
if (direct || !options.slopPx) return direct;
|
|
1067
|
+
let best = null;
|
|
1068
|
+
let bestD = Infinity;
|
|
1069
|
+
for (const r of [options.slopPx / 2, options.slopPx]) {
|
|
1070
|
+
for (let k = 0; k < 8; k++) {
|
|
1071
|
+
const a = k / 8 * Math.PI * 2;
|
|
1072
|
+
const hit = castAt(world, view, sx + Math.cos(a) * r, sy + Math.sin(a) * r, options.filter);
|
|
1073
|
+
if (hit && r < bestD) {
|
|
1074
|
+
best = hit;
|
|
1075
|
+
bestD = r;
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
if (best) break;
|
|
1079
|
+
}
|
|
1080
|
+
return best;
|
|
1081
|
+
}
|
|
1082
|
+
function castAt(world, view, sx, sy, filter) {
|
|
1083
|
+
const hits = view.rayAt(sx, sy).intersectObject(world.root, true);
|
|
1084
|
+
for (const hit of hits) {
|
|
1085
|
+
if (!visible(hit.object) || filter && !filter(hit.object)) continue;
|
|
1086
|
+
const entity = entityOf(world, hit.object);
|
|
1087
|
+
if (entity) return {
|
|
1088
|
+
entity,
|
|
1089
|
+
point: hit.point,
|
|
1090
|
+
distance: hit.distance,
|
|
1091
|
+
object: hit.object
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1094
|
+
return null;
|
|
1095
|
+
}
|
|
1096
|
+
/** The closest entity of `world` at or above `object`, or null. */
|
|
1097
|
+
function entityOf(world, object) {
|
|
1098
|
+
let o = object;
|
|
1099
|
+
while (o) {
|
|
1100
|
+
if (world.has(o)) return o;
|
|
1101
|
+
o = o.parent;
|
|
1102
|
+
}
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
function visible(object) {
|
|
1106
|
+
let o = object;
|
|
1107
|
+
while (o) {
|
|
1108
|
+
if (!o.visible) return false;
|
|
1109
|
+
o = o.parent;
|
|
1110
|
+
}
|
|
1111
|
+
return true;
|
|
1112
|
+
}
|
|
1113
|
+
//#endregion
|
|
756
1114
|
//#region src/three/renderer.ts
|
|
757
1115
|
/**
|
|
758
1116
|
* `WebGLRenderer` that uses the device pixel ratio and can size itself to its parent element.
|
|
@@ -808,6 +1166,219 @@ var Renderer = class extends three.WebGLRenderer {
|
|
|
808
1166
|
}
|
|
809
1167
|
};
|
|
810
1168
|
//#endregion
|
|
1169
|
+
//#region src/three/schema.ts
|
|
1170
|
+
const isObj = (v) => typeof v === "object" && v !== null;
|
|
1171
|
+
/** Works out a field's type from a value, e.g. a class field's initial value. */
|
|
1172
|
+
function inferFieldType(value) {
|
|
1173
|
+
if (typeof value === "number") return "number";
|
|
1174
|
+
if (typeof value === "boolean") return "boolean";
|
|
1175
|
+
if (isObj(value)) {
|
|
1176
|
+
if (value.isColor) return "color";
|
|
1177
|
+
if (value.isVector3 || "z" in value && "x" in value && "y" in value) return "vec3";
|
|
1178
|
+
if (value.isVector2 || "x" in value && "y" in value) return "vec2";
|
|
1179
|
+
}
|
|
1180
|
+
return "string";
|
|
1181
|
+
}
|
|
1182
|
+
/** A field value as plain JSON: vectors become arrays, colors `#rrggbb` strings. */
|
|
1183
|
+
function fieldToJson(def, value) {
|
|
1184
|
+
switch (def.type) {
|
|
1185
|
+
case "vec2": return isObj(value) ? [value.x ?? 0, value.y ?? 0] : value;
|
|
1186
|
+
case "vec3": return isObj(value) ? [
|
|
1187
|
+
value.x ?? 0,
|
|
1188
|
+
value.y ?? 0,
|
|
1189
|
+
value.z ?? 0
|
|
1190
|
+
] : value;
|
|
1191
|
+
case "color": return isObj(value) && value.isColor ? `#${value.getHexString()}` : value;
|
|
1192
|
+
default: return isObj(value) ? structuredClone(value) : value;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Applies a JSON (or live) value to a field. When the field currently holds a three.js
|
|
1197
|
+
* object (a `Vector3`, a `Color`…) it is updated in place, so references to it stay valid.
|
|
1198
|
+
*/
|
|
1199
|
+
function applyFieldValue(target, key, def, json) {
|
|
1200
|
+
const current = target[key];
|
|
1201
|
+
if (def.type === "vec2" || def.type === "vec3") {
|
|
1202
|
+
const [x, y, z] = Array.isArray(json) ? json : isObj(json) ? [
|
|
1203
|
+
json.x,
|
|
1204
|
+
json.y,
|
|
1205
|
+
json.z
|
|
1206
|
+
] : [];
|
|
1207
|
+
if (isObj(current) && typeof current.set === "function") def.type === "vec3" ? current.set(x ?? 0, y ?? 0, z ?? 0) : current.set(x ?? 0, y ?? 0);
|
|
1208
|
+
else target[key] = def.type === "vec3" ? {
|
|
1209
|
+
x: x ?? 0,
|
|
1210
|
+
y: y ?? 0,
|
|
1211
|
+
z: z ?? 0
|
|
1212
|
+
} : {
|
|
1213
|
+
x: x ?? 0,
|
|
1214
|
+
y: y ?? 0
|
|
1215
|
+
};
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
if (def.type === "color" && isObj(current) && current.isColor) {
|
|
1219
|
+
if (isObj(json) && json.isColor) current.copy(json);
|
|
1220
|
+
else current.set(json);
|
|
1221
|
+
return;
|
|
1222
|
+
}
|
|
1223
|
+
target[key] = isObj(json) ? structuredClone(json) : json;
|
|
1224
|
+
}
|
|
1225
|
+
/** The saved form of a component: every non-transient field, as JSON. */
|
|
1226
|
+
function componentToJson(type, component) {
|
|
1227
|
+
const out = {};
|
|
1228
|
+
for (const [key, def] of Object.entries(type.schema.fields)) if (!def.transient) out[key] = fieldToJson(def, component[key]);
|
|
1229
|
+
return out;
|
|
1230
|
+
}
|
|
1231
|
+
function applyData(type, instance, data) {
|
|
1232
|
+
if (!data) return;
|
|
1233
|
+
for (const [key, def] of Object.entries(type.schema.fields)) if (key in data) applyFieldValue(instance, key, def, data[key]);
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* Declares a component type from plain field definitions; instances are plain objects.
|
|
1237
|
+
*
|
|
1238
|
+
* @category ECS
|
|
1239
|
+
*/
|
|
1240
|
+
function defineComponent(name, fields, registry = components) {
|
|
1241
|
+
const type = {
|
|
1242
|
+
schema: {
|
|
1243
|
+
name,
|
|
1244
|
+
fields: { ...fields }
|
|
1245
|
+
},
|
|
1246
|
+
create(data) {
|
|
1247
|
+
const instance = {};
|
|
1248
|
+
for (const [key, def] of Object.entries(fields)) instance[key] = isObj(def.default) ? structuredClone(def.default) : def.default ?? defaultFor(def);
|
|
1249
|
+
applyData(type, instance, data);
|
|
1250
|
+
return instance;
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
registry?.register(type);
|
|
1254
|
+
return type;
|
|
1255
|
+
}
|
|
1256
|
+
function defaultFor(def) {
|
|
1257
|
+
switch (def.type) {
|
|
1258
|
+
case "number": return def.min !== void 0 && def.min > 0 ? def.min : 0;
|
|
1259
|
+
case "boolean": return false;
|
|
1260
|
+
case "color": return "#ffffff";
|
|
1261
|
+
case "vec2": return {
|
|
1262
|
+
x: 0,
|
|
1263
|
+
y: 0
|
|
1264
|
+
};
|
|
1265
|
+
case "vec3": return {
|
|
1266
|
+
x: 0,
|
|
1267
|
+
y: 0,
|
|
1268
|
+
z: 0
|
|
1269
|
+
};
|
|
1270
|
+
case "enum": return def.options?.[0] ?? "";
|
|
1271
|
+
case "entity": return null;
|
|
1272
|
+
default: return "";
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
const METADATA = Symbol.metadata ?? Symbol.for("Symbol.metadata");
|
|
1276
|
+
const FIELDS = Symbol("n3rdw1z4rd.fields");
|
|
1277
|
+
/**
|
|
1278
|
+
* Field decorator: marks a class field as part of the component's schema. The type is
|
|
1279
|
+
* inferred from the initial value when not given.
|
|
1280
|
+
*
|
|
1281
|
+
* @category ECS
|
|
1282
|
+
*/
|
|
1283
|
+
function field(options = {}) {
|
|
1284
|
+
return function(_value, context) {
|
|
1285
|
+
if (context.static || context.private) throw new Error("@field: only public instance fields can be fields");
|
|
1286
|
+
const meta = context.metadata;
|
|
1287
|
+
if (!Object.hasOwn(meta, FIELDS)) meta[FIELDS] = { ...meta[FIELDS] ?? {} };
|
|
1288
|
+
meta[FIELDS][String(context.name)] = options;
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Class decorator: registers the class as a component type (in the default registry, or
|
|
1293
|
+
* the one given). Give it a name: class names don't survive minification.
|
|
1294
|
+
*
|
|
1295
|
+
* @category ECS
|
|
1296
|
+
*/
|
|
1297
|
+
function component(name, registry = components) {
|
|
1298
|
+
return function(ctor, context) {
|
|
1299
|
+
context.addInitializer(() => {
|
|
1300
|
+
registry.register(componentType(ctor, name ?? String(context.name)));
|
|
1301
|
+
});
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
const classTypes = /* @__PURE__ */ new WeakMap();
|
|
1305
|
+
/**
|
|
1306
|
+
* The {@link ComponentType} for a class with `@field`s (made once, then cached). Field types
|
|
1307
|
+
* and defaults come from a fresh instance.
|
|
1308
|
+
*
|
|
1309
|
+
* @category ECS
|
|
1310
|
+
*/
|
|
1311
|
+
function componentType(ctor, name = ctor.name) {
|
|
1312
|
+
const cached = classTypes.get(ctor);
|
|
1313
|
+
if (cached) return cached;
|
|
1314
|
+
const declared = ctor[METADATA]?.[FIELDS] ?? {};
|
|
1315
|
+
const sample = new ctor();
|
|
1316
|
+
const fields = {};
|
|
1317
|
+
for (const [key, opts] of Object.entries(declared)) {
|
|
1318
|
+
const type = opts.type ?? (opts.options ? "enum" : inferFieldType(sample[key]));
|
|
1319
|
+
fields[key] = {
|
|
1320
|
+
...opts,
|
|
1321
|
+
type,
|
|
1322
|
+
default: fieldToJson({ type }, sample[key])
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
const type = {
|
|
1326
|
+
schema: {
|
|
1327
|
+
name,
|
|
1328
|
+
fields
|
|
1329
|
+
},
|
|
1330
|
+
ctor,
|
|
1331
|
+
create(data) {
|
|
1332
|
+
const instance = new ctor();
|
|
1333
|
+
applyData(type, instance, data);
|
|
1334
|
+
return instance;
|
|
1335
|
+
}
|
|
1336
|
+
};
|
|
1337
|
+
classTypes.set(ctor, type);
|
|
1338
|
+
return type;
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* Component types by name (and by class). Scenes are saved with type names and loaded
|
|
1342
|
+
* through a registry.
|
|
1343
|
+
*
|
|
1344
|
+
* @category ECS
|
|
1345
|
+
*/
|
|
1346
|
+
var ComponentRegistry = class {
|
|
1347
|
+
byName = /* @__PURE__ */ new Map();
|
|
1348
|
+
register(type) {
|
|
1349
|
+
const existing = this.byName.get(type.schema.name);
|
|
1350
|
+
if (existing && existing !== type) console.warn(`ComponentRegistry: replacing component type "${type.schema.name}"`);
|
|
1351
|
+
this.byName.set(type.schema.name, type);
|
|
1352
|
+
return type;
|
|
1353
|
+
}
|
|
1354
|
+
get(name) {
|
|
1355
|
+
return this.byName.get(name);
|
|
1356
|
+
}
|
|
1357
|
+
has(name) {
|
|
1358
|
+
return this.byName.has(name);
|
|
1359
|
+
}
|
|
1360
|
+
/** The type of a name, class, type or instance (via its constructor); undefined if unknown. */
|
|
1361
|
+
resolve(ref) {
|
|
1362
|
+
if (typeof ref === "string") return this.get(ref);
|
|
1363
|
+
if (typeof ref === "function") {
|
|
1364
|
+
const t = classTypes.get(ref);
|
|
1365
|
+
return t && this.get(t.schema.name) === t ? t : void 0;
|
|
1366
|
+
}
|
|
1367
|
+
if ("schema" in ref && "create" in ref) return ref;
|
|
1368
|
+
return this.resolve(ref.constructor);
|
|
1369
|
+
}
|
|
1370
|
+
/** every registered type, e.g. for an "add component" menu */
|
|
1371
|
+
all() {
|
|
1372
|
+
return [...this.byName.values()];
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
/**
|
|
1376
|
+
* The default registry, used by `@component` and {@link defineComponent} unless given another.
|
|
1377
|
+
*
|
|
1378
|
+
* @category ECS
|
|
1379
|
+
*/
|
|
1380
|
+
const components = new ComponentRegistry();
|
|
1381
|
+
//#endregion
|
|
811
1382
|
//#region src/three/starfield.ts
|
|
812
1383
|
/**
|
|
813
1384
|
* Points spread uniformly over a sphere's surface, for a distant star backdrop (point size 5 with
|
|
@@ -821,7 +1392,7 @@ var Renderer = class extends three.WebGLRenderer {
|
|
|
821
1392
|
* @category Geometry & Meshes
|
|
822
1393
|
*/
|
|
823
1394
|
function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215, seed) {
|
|
824
|
-
const random = seed === void 0 ?
|
|
1395
|
+
const random = seed === void 0 ? require_rng.rng : new require_rng.Rng(seed);
|
|
825
1396
|
const geometry = new three.BufferGeometry();
|
|
826
1397
|
const positions = new Float32Array(count * 3);
|
|
827
1398
|
for (let i = 0; i < count; i++) {
|
|
@@ -1033,7 +1604,7 @@ function buildVoxelGeometry(voxels, options = {}) {
|
|
|
1033
1604
|
if (value === empty && neighbor) value = neighbor(x, y, z);
|
|
1034
1605
|
return value !== empty;
|
|
1035
1606
|
};
|
|
1036
|
-
const colorOf = (value) => palette[value] ?? options.missingColor ??
|
|
1607
|
+
const colorOf = (value) => palette[value] ?? options.missingColor ?? require_rng.Random.uint(value, 1) & 16777215;
|
|
1037
1608
|
const planes = /* @__PURE__ */ new Map();
|
|
1038
1609
|
const p = [
|
|
1039
1610
|
0,
|
|
@@ -1286,14 +1857,376 @@ var VoxelMesh = class extends three.Mesh {
|
|
|
1286
1857
|
}
|
|
1287
1858
|
};
|
|
1288
1859
|
//#endregion
|
|
1860
|
+
//#region src/three/world.ts
|
|
1861
|
+
/**
|
|
1862
|
+
* A scene of entities: three.js objects with components (see {@link CreateThreeEntity}),
|
|
1863
|
+
* tracked by their `Object3D.uuid`.
|
|
1864
|
+
*
|
|
1865
|
+
* The world keeps an index of which entities have which components (for {@link World.query}),
|
|
1866
|
+
* runs systems, tells listeners what changed (so editor panels stay in sync), and saves and
|
|
1867
|
+
* loads the whole scene as plain JSON ({@link SceneDoc}).
|
|
1868
|
+
*
|
|
1869
|
+
* The three.js scene graph is the hierarchy: parent an entity under another and it moves
|
|
1870
|
+
* with it. Transforms live on the objects themselves (`position`, `rotation`, `scale`), so
|
|
1871
|
+
* there is no Transform component.
|
|
1872
|
+
*
|
|
1873
|
+
* ```ts
|
|
1874
|
+
* const world = new World();
|
|
1875
|
+
* const ball = world.create('ball');
|
|
1876
|
+
* world.addComponent(ball, RigidBody, { mass: 2 });
|
|
1877
|
+
*
|
|
1878
|
+
* world.addSystem({ query: ['RigidBody'], update(entities, dt) { ... } });
|
|
1879
|
+
* renderer.setAnimationLoop(() => { world.update(clock.tick()); renderer.render(world.root, camera); });
|
|
1880
|
+
*
|
|
1881
|
+
* const saved = world.serialize(); // JSON-safe
|
|
1882
|
+
* world.load(saved); // same entities, same uuids
|
|
1883
|
+
* ```
|
|
1884
|
+
*
|
|
1885
|
+
* Add and remove entities through the world (not `Object3D.add`/`remove` directly), and call
|
|
1886
|
+
* {@link World.changed} after editing a component in place, so the index and listeners stay
|
|
1887
|
+
* current.
|
|
1888
|
+
*
|
|
1889
|
+
* @category ECS
|
|
1890
|
+
*/
|
|
1891
|
+
var World = class {
|
|
1892
|
+
root;
|
|
1893
|
+
registry;
|
|
1894
|
+
entities = /* @__PURE__ */ new Map();
|
|
1895
|
+
index = /* @__PURE__ */ new Map();
|
|
1896
|
+
systems = [];
|
|
1897
|
+
listeners = /* @__PURE__ */ new Map();
|
|
1898
|
+
constructor(options = {}) {
|
|
1899
|
+
this.root = options.root ?? new three.Scene();
|
|
1900
|
+
this.registry = options.registry ?? components;
|
|
1901
|
+
}
|
|
1902
|
+
/** number of entities */
|
|
1903
|
+
get size() {
|
|
1904
|
+
return this.entities.size;
|
|
1905
|
+
}
|
|
1906
|
+
[Symbol.iterator]() {
|
|
1907
|
+
return this.entities.values();
|
|
1908
|
+
}
|
|
1909
|
+
/** The entity with this uuid. */
|
|
1910
|
+
get(uuid) {
|
|
1911
|
+
return this.entities.get(uuid);
|
|
1912
|
+
}
|
|
1913
|
+
has(obj) {
|
|
1914
|
+
return this.entities.get(obj.uuid) === obj;
|
|
1915
|
+
}
|
|
1916
|
+
/** Makes a new entity (an empty `Group`, or `obj`) and adds it under `parent` (default: the root). */
|
|
1917
|
+
create(name = "", parent = this.root, obj) {
|
|
1918
|
+
const entity = CreateThreeEntity(obj);
|
|
1919
|
+
if (name) entity.name = name;
|
|
1920
|
+
return this.add(entity, parent);
|
|
1921
|
+
}
|
|
1922
|
+
/**
|
|
1923
|
+
* Puts `obj` under `parent` (default: the root) and starts tracking it — and every entity
|
|
1924
|
+
* among its descendants. `obj` is made an entity if it isn't one.
|
|
1925
|
+
*/
|
|
1926
|
+
add(obj, parent = this.root) {
|
|
1927
|
+
if (!IsThreeEntity(obj)) CreateThreeEntity(obj);
|
|
1928
|
+
parent.add(obj);
|
|
1929
|
+
obj.traverse((o) => {
|
|
1930
|
+
if (IsThreeEntity(o)) this.track(o);
|
|
1931
|
+
});
|
|
1932
|
+
return obj;
|
|
1933
|
+
}
|
|
1934
|
+
/** Takes the entity (and its descendants) out of the scene and stops tracking them. */
|
|
1935
|
+
remove(entity) {
|
|
1936
|
+
const gone = [];
|
|
1937
|
+
entity.traverse((o) => {
|
|
1938
|
+
if (this.entities.get(o.uuid) === o) gone.push(o);
|
|
1939
|
+
});
|
|
1940
|
+
entity.removeFromParent();
|
|
1941
|
+
for (const e of gone.reverse()) this.untrack(e);
|
|
1942
|
+
}
|
|
1943
|
+
/** Removes every entity. */
|
|
1944
|
+
clear() {
|
|
1945
|
+
for (const child of [...this.root.children]) {
|
|
1946
|
+
let hasEntity = false;
|
|
1947
|
+
child.traverse((o) => hasEntity ||= IsThreeEntity(o));
|
|
1948
|
+
if (hasEntity) this.remove(child);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Moves `entity` under `parent` (default: the root). By default its world transform is
|
|
1953
|
+
* kept (it doesn't jump); pass `keepWorldTransform = false` to keep its local one instead.
|
|
1954
|
+
*/
|
|
1955
|
+
reparent(entity, parent = this.root, keepWorldTransform = true) {
|
|
1956
|
+
let p = parent;
|
|
1957
|
+
while (p) {
|
|
1958
|
+
if (p === entity) throw new Error("World.reparent: cannot parent an entity under itself");
|
|
1959
|
+
p = p.parent;
|
|
1960
|
+
}
|
|
1961
|
+
if (keepWorldTransform) parent.attach(entity);
|
|
1962
|
+
else parent.add(entity);
|
|
1963
|
+
this.emit("reparented", entity, parent);
|
|
1964
|
+
}
|
|
1965
|
+
/** The closest ancestor that is an entity of this world, or null. */
|
|
1966
|
+
parentOf(entity) {
|
|
1967
|
+
let p = entity.parent;
|
|
1968
|
+
while (p && p !== this.root) {
|
|
1969
|
+
if (this.entities.get(p.uuid) === p) return p;
|
|
1970
|
+
p = p.parent;
|
|
1971
|
+
}
|
|
1972
|
+
return null;
|
|
1973
|
+
}
|
|
1974
|
+
/** The entities directly under `parent` (default: the root), looking through non-entity objects. */
|
|
1975
|
+
childrenOf(parent = this.root) {
|
|
1976
|
+
const out = [];
|
|
1977
|
+
const visit = (o) => {
|
|
1978
|
+
for (const c of o.children) if (this.entities.get(c.uuid) === c) out.push(c);
|
|
1979
|
+
else visit(c);
|
|
1980
|
+
};
|
|
1981
|
+
visit(parent);
|
|
1982
|
+
return out;
|
|
1983
|
+
}
|
|
1984
|
+
/** A deep copy of the entity and its descendant entities (new uuids), added next to it. */
|
|
1985
|
+
clone(entity, parent = entity.parent ?? this.root) {
|
|
1986
|
+
const docs = this.serializeEntities([entity]);
|
|
1987
|
+
const [copy] = this.instantiate(docs, {
|
|
1988
|
+
parent,
|
|
1989
|
+
newIds: true
|
|
1990
|
+
});
|
|
1991
|
+
return copy;
|
|
1992
|
+
}
|
|
1993
|
+
track(entity) {
|
|
1994
|
+
entity.userData.entityId = entity.uuid;
|
|
1995
|
+
const existing = this.entities.get(entity.uuid);
|
|
1996
|
+
if (existing === entity) return;
|
|
1997
|
+
if (existing) throw new Error(`World: another entity already has uuid ${entity.uuid}`);
|
|
1998
|
+
this.entities.set(entity.uuid, entity);
|
|
1999
|
+
for (const name of Object.keys(entity.userData.components)) this.indexSet(name).add(entity);
|
|
2000
|
+
this.emit("entityAdded", entity);
|
|
2001
|
+
}
|
|
2002
|
+
untrack(entity) {
|
|
2003
|
+
if (!this.entities.delete(entity.uuid)) return;
|
|
2004
|
+
for (const name of Object.keys(entity.userData.components)) this.index.get(name)?.delete(entity);
|
|
2005
|
+
this.emit("entityRemoved", entity);
|
|
2006
|
+
}
|
|
2007
|
+
indexSet(name) {
|
|
2008
|
+
let set = this.index.get(name);
|
|
2009
|
+
if (!set) this.index.set(name, set = /* @__PURE__ */ new Set());
|
|
2010
|
+
return set;
|
|
2011
|
+
}
|
|
2012
|
+
/** The registered type for a name, class or type; throws if unknown. */
|
|
2013
|
+
typeOf(ref) {
|
|
2014
|
+
const type = this.registry.resolve(ref);
|
|
2015
|
+
if (!type) throw new Error(`World: unknown component type ${typeof ref === "string" ? `"${ref}"` : String(ref?.name ?? ref)}`);
|
|
2016
|
+
return type;
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Adds a component made from its type's defaults plus `data`, replacing one of the same
|
|
2020
|
+
* type. Returns the component.
|
|
2021
|
+
*/
|
|
2022
|
+
addComponent(entity, ref, data) {
|
|
2023
|
+
const type = this.typeOf(ref);
|
|
2024
|
+
const name = type.schema.name;
|
|
2025
|
+
const previous = entity.userData.components[name];
|
|
2026
|
+
if (previous) this.emit("componentRemoved", entity, name, previous);
|
|
2027
|
+
const component = type.create(data);
|
|
2028
|
+
entity.userData.components[name] = component;
|
|
2029
|
+
if (this.has(entity)) this.indexSet(name).add(entity);
|
|
2030
|
+
this.emit("componentAdded", entity, name, component);
|
|
2031
|
+
return component;
|
|
2032
|
+
}
|
|
2033
|
+
getComponent(entity, ref) {
|
|
2034
|
+
return entity.userData.components[this.nameOf(ref)];
|
|
2035
|
+
}
|
|
2036
|
+
hasComponent(entity, ref) {
|
|
2037
|
+
return this.nameOf(ref) in entity.userData.components;
|
|
2038
|
+
}
|
|
2039
|
+
removeComponent(entity, ref) {
|
|
2040
|
+
const name = this.nameOf(ref);
|
|
2041
|
+
const component = entity.userData.components[name];
|
|
2042
|
+
if (component === void 0) return;
|
|
2043
|
+
delete entity.userData.components[name];
|
|
2044
|
+
this.index.get(name)?.delete(entity);
|
|
2045
|
+
this.emit("componentRemoved", entity, name, component);
|
|
2046
|
+
}
|
|
2047
|
+
/** The names of the entity's components, in the order they were added. */
|
|
2048
|
+
componentNames(entity) {
|
|
2049
|
+
return Object.keys(entity.userData.components);
|
|
2050
|
+
}
|
|
2051
|
+
/**
|
|
2052
|
+
* Tells listeners a component (or the entity's transform/name, with `component` omitted)
|
|
2053
|
+
* was edited in place. `field` narrows it to one field.
|
|
2054
|
+
*/
|
|
2055
|
+
changed(entity, component, field) {
|
|
2056
|
+
this.emit("changed", entity, component === void 0 ? void 0 : this.nameOf(component), field);
|
|
2057
|
+
}
|
|
2058
|
+
/** Entities that have every one of the given components. */
|
|
2059
|
+
query(...refs) {
|
|
2060
|
+
if (refs.length === 0) return [...this.entities.values()];
|
|
2061
|
+
const sets = refs.map((r) => this.nameOf(r)).map((n) => this.index.get(n) ?? /* @__PURE__ */ new Set());
|
|
2062
|
+
sets.sort((a, b) => a.size - b.size);
|
|
2063
|
+
const [smallest, ...rest] = sets;
|
|
2064
|
+
const out = [];
|
|
2065
|
+
for (const e of smallest) if (rest.every((s) => s.has(e))) out.push(e);
|
|
2066
|
+
return out;
|
|
2067
|
+
}
|
|
2068
|
+
/** Entities whose name is `name`. */
|
|
2069
|
+
findByName(name) {
|
|
2070
|
+
return [...this.entities.values()].filter((e) => e.name === name);
|
|
2071
|
+
}
|
|
2072
|
+
nameOf(ref) {
|
|
2073
|
+
if (typeof ref === "string") return ref;
|
|
2074
|
+
return this.typeOf(ref).schema.name;
|
|
2075
|
+
}
|
|
2076
|
+
/**
|
|
2077
|
+
* Adds a system, run by {@link World.update} in the order added. With a `query`, it gets
|
|
2078
|
+
* the entities that have all of those components; without, every entity.
|
|
2079
|
+
*/
|
|
2080
|
+
addSystem(system, query) {
|
|
2081
|
+
const s = typeof system === "function" ? {
|
|
2082
|
+
update: system,
|
|
2083
|
+
query
|
|
2084
|
+
} : system;
|
|
2085
|
+
this.systems.push(s);
|
|
2086
|
+
return s;
|
|
2087
|
+
}
|
|
2088
|
+
removeSystem(system) {
|
|
2089
|
+
const i = this.systems.indexOf(system);
|
|
2090
|
+
if (i >= 0) this.systems.splice(i, 1);
|
|
2091
|
+
}
|
|
2092
|
+
/** Runs every enabled system once. */
|
|
2093
|
+
update(delta) {
|
|
2094
|
+
for (const s of [...this.systems]) {
|
|
2095
|
+
if (s.enabled === false) continue;
|
|
2096
|
+
s.update(this.query(...s.query ?? []), delta, this);
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
/** Listens for an event; returns a function that stops listening. */
|
|
2100
|
+
on(event, listener) {
|
|
2101
|
+
let set = this.listeners.get(event);
|
|
2102
|
+
if (!set) this.listeners.set(event, set = /* @__PURE__ */ new Set());
|
|
2103
|
+
set.add(listener);
|
|
2104
|
+
return () => set.delete(listener);
|
|
2105
|
+
}
|
|
2106
|
+
emit(event, ...args) {
|
|
2107
|
+
const set = this.listeners.get(event);
|
|
2108
|
+
if (set) for (const l of [...set]) l(...args);
|
|
2109
|
+
}
|
|
2110
|
+
/** The whole scene as plain JSON. */
|
|
2111
|
+
serialize(meta) {
|
|
2112
|
+
return {
|
|
2113
|
+
format: SCENE_FORMAT,
|
|
2114
|
+
version: 1,
|
|
2115
|
+
entities: this.serializeEntities(this.childrenOf()),
|
|
2116
|
+
...meta ? { meta } : {}
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Replaces the scene with a saved one (or, with `replace: false`, adds to it). Entities
|
|
2121
|
+
* keep their saved uuids.
|
|
2122
|
+
*/
|
|
2123
|
+
load(doc, options = {}) {
|
|
2124
|
+
if (doc?.format !== "n3rdw1z4rd.scene") throw new Error("World.load: not a scene document");
|
|
2125
|
+
if (doc.version > 1) throw new Error(`World.load: scene version ${doc.version} is newer than supported (1)`);
|
|
2126
|
+
if (options.replace !== false) this.clear();
|
|
2127
|
+
return this.instantiate(doc.entities);
|
|
2128
|
+
}
|
|
2129
|
+
/**
|
|
2130
|
+
* `entities` and their descendant entities, depth-first (parents before children), as JSON:
|
|
2131
|
+
* e.g. a prefab or the clipboard.
|
|
2132
|
+
*/
|
|
2133
|
+
serializeEntities(entities) {
|
|
2134
|
+
const out = [];
|
|
2135
|
+
const visit = (e, parent) => {
|
|
2136
|
+
out.push(this.serializeEntity(e, parent));
|
|
2137
|
+
for (const c of this.childrenOf(e)) visit(c, e.uuid);
|
|
2138
|
+
};
|
|
2139
|
+
for (const e of entities) visit(e, null);
|
|
2140
|
+
return out;
|
|
2141
|
+
}
|
|
2142
|
+
serializeEntity(e, parent) {
|
|
2143
|
+
const doc = {
|
|
2144
|
+
uuid: e.uuid,
|
|
2145
|
+
parent,
|
|
2146
|
+
position: e.position.toArray(),
|
|
2147
|
+
rotation: [
|
|
2148
|
+
e.rotation.x,
|
|
2149
|
+
e.rotation.y,
|
|
2150
|
+
e.rotation.z
|
|
2151
|
+
],
|
|
2152
|
+
scale: e.scale.toArray(),
|
|
2153
|
+
components: {}
|
|
2154
|
+
};
|
|
2155
|
+
if (e.name) doc.name = e.name;
|
|
2156
|
+
if (e.rotation.order !== "XYZ") doc.rotationOrder = e.rotation.order;
|
|
2157
|
+
if (!e.visible) doc.visible = false;
|
|
2158
|
+
for (const [name, component] of Object.entries(e.userData.components)) {
|
|
2159
|
+
const type = this.registry.get(name);
|
|
2160
|
+
doc.components[name] = type ? componentToJson(type, component) : structuredClone(component);
|
|
2161
|
+
}
|
|
2162
|
+
return doc;
|
|
2163
|
+
}
|
|
2164
|
+
/**
|
|
2165
|
+
* Makes entities from saved docs. A doc whose `parent` isn't among `docs` goes under
|
|
2166
|
+
* `options.parent` (default: the root). With `newIds`, every entity gets a fresh uuid and
|
|
2167
|
+
* `entity` fields pointing inside the set are remapped (pasting, prefabs, clone).
|
|
2168
|
+
*/
|
|
2169
|
+
instantiate(docs, options = {}) {
|
|
2170
|
+
const parent = options.parent ?? this.root;
|
|
2171
|
+
const ids = /* @__PURE__ */ new Map();
|
|
2172
|
+
for (const d of docs) ids.set(d.uuid, options.newIds ? three.MathUtils.generateUUID() : d.uuid);
|
|
2173
|
+
const made = /* @__PURE__ */ new Map();
|
|
2174
|
+
const roots = [];
|
|
2175
|
+
for (const d of docs) {
|
|
2176
|
+
const entity = CreateThreeEntity();
|
|
2177
|
+
entity.uuid = ids.get(d.uuid);
|
|
2178
|
+
entity.name = d.name ?? "";
|
|
2179
|
+
entity.position.fromArray(d.position ?? [
|
|
2180
|
+
0,
|
|
2181
|
+
0,
|
|
2182
|
+
0
|
|
2183
|
+
]);
|
|
2184
|
+
entity.rotation.set(...d.rotation ?? [
|
|
2185
|
+
0,
|
|
2186
|
+
0,
|
|
2187
|
+
0
|
|
2188
|
+
], d.rotationOrder ?? "XYZ");
|
|
2189
|
+
entity.scale.fromArray(d.scale ?? [
|
|
2190
|
+
1,
|
|
2191
|
+
1,
|
|
2192
|
+
1
|
|
2193
|
+
]);
|
|
2194
|
+
entity.visible = d.visible !== false;
|
|
2195
|
+
for (const [name, data] of Object.entries(d.components ?? {})) {
|
|
2196
|
+
const type = this.registry.get(name);
|
|
2197
|
+
if (!type) {
|
|
2198
|
+
entity.userData.components[name] = structuredClone(data);
|
|
2199
|
+
continue;
|
|
2200
|
+
}
|
|
2201
|
+
const component = type.create(data);
|
|
2202
|
+
if (options.newIds) {
|
|
2203
|
+
for (const [key, def] of Object.entries(type.schema.fields)) if (def.type === "entity" && ids.has(component[key])) component[key] = ids.get(component[key]);
|
|
2204
|
+
}
|
|
2205
|
+
entity.userData.components[name] = component;
|
|
2206
|
+
}
|
|
2207
|
+
made.set(d.uuid, entity);
|
|
2208
|
+
const p = d.parent !== null && d.parent !== void 0 ? made.get(d.parent) : void 0;
|
|
2209
|
+
if (p) p.add(entity);
|
|
2210
|
+
else roots.push(entity);
|
|
2211
|
+
}
|
|
2212
|
+
for (const r of roots) this.add(r, parent);
|
|
2213
|
+
return roots;
|
|
2214
|
+
}
|
|
2215
|
+
};
|
|
2216
|
+
const SCENE_FORMAT = "n3rdw1z4rd.scene";
|
|
2217
|
+
const SCENE_VERSION = 1;
|
|
2218
|
+
//#endregion
|
|
2219
|
+
exports.AdaptiveGrid = AdaptiveGrid;
|
|
1289
2220
|
exports.AddThreeComponent = AddThreeComponent;
|
|
1290
2221
|
exports.AsteroidMesh = AsteroidMesh;
|
|
1291
2222
|
exports.CameraRig = CameraRig;
|
|
2223
|
+
exports.ComponentRegistry = ComponentRegistry;
|
|
1292
2224
|
exports.CreateStarfield = CreateStarfield;
|
|
1293
2225
|
exports.CreateSubTexture = CreateSubTexture;
|
|
1294
2226
|
exports.CreateSystemStar = CreateSystemStar;
|
|
1295
2227
|
exports.CreateThreeEntity = CreateThreeEntity;
|
|
1296
2228
|
exports.CubeSphereGeometry = CubeSphereGeometry;
|
|
2229
|
+
exports.EditorCamera = EditorCamera;
|
|
1297
2230
|
exports.GenerateEntityId = GenerateEntityId;
|
|
1298
2231
|
exports.GetThreeComponent = GetThreeComponent;
|
|
1299
2232
|
exports.IsThreeEntity = IsThreeEntity;
|
|
@@ -1301,7 +2234,21 @@ exports.LoadTexture = LoadTexture;
|
|
|
1301
2234
|
exports.ParticleSystem3d = ParticleSystem3d;
|
|
1302
2235
|
exports.RemoveThreeComponent = RemoveThreeComponent;
|
|
1303
2236
|
exports.Renderer = Renderer;
|
|
2237
|
+
exports.SCENE_FORMAT = SCENE_FORMAT;
|
|
2238
|
+
exports.SCENE_VERSION = SCENE_VERSION;
|
|
1304
2239
|
exports.TextureAtlas = TextureAtlas;
|
|
1305
2240
|
exports.VoxelGeometry = VoxelGeometry;
|
|
1306
2241
|
exports.VoxelMesh = VoxelMesh;
|
|
2242
|
+
exports.World = World;
|
|
2243
|
+
exports.applyFieldValue = applyFieldValue;
|
|
1307
2244
|
exports.buildVoxelGeometry = buildVoxelGeometry;
|
|
2245
|
+
exports.component = component;
|
|
2246
|
+
exports.componentToJson = componentToJson;
|
|
2247
|
+
exports.componentType = componentType;
|
|
2248
|
+
exports.components = components;
|
|
2249
|
+
exports.defineComponent = defineComponent;
|
|
2250
|
+
exports.entityOf = entityOf;
|
|
2251
|
+
exports.field = field;
|
|
2252
|
+
exports.fieldToJson = fieldToJson;
|
|
2253
|
+
exports.inferFieldType = inferFieldType;
|
|
2254
|
+
exports.pickEntity = pickEntity;
|