@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
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { EditorCamera } from "./editor-camera.js";
|
|
2
|
+
import { BufferGeometry, ColorRepresentation, LineBasicMaterial, LineSegments } from "three";
|
|
3
|
+
//#region src/three/adaptive-grid.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* An infinite-looking grid on the XY plane for editor views: each {@link AdaptiveGrid.update}
|
|
6
|
+
* rebuilds just the lines in view, doubling the spacing when zoomed out so lines never get
|
|
7
|
+
* closer than `minSpacingPx`. Optional origin cross.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const grid = new AdaptiveGrid({ color: '#262626' });
|
|
11
|
+
* scene.add(grid);
|
|
12
|
+
* // per frame, after moving the camera:
|
|
13
|
+
* grid.update(view, 32);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @category Three.js
|
|
17
|
+
*/
|
|
18
|
+
export declare class AdaptiveGrid extends LineSegments<BufferGeometry, LineBasicMaterial> {
|
|
19
|
+
minSpacingPx: number;
|
|
20
|
+
/** draw the grid lines (false: just the origin cross, if `origin`) */
|
|
21
|
+
lines: boolean;
|
|
22
|
+
/** draw a small cross at the origin (`originSizePx` long) */
|
|
23
|
+
origin: boolean;
|
|
24
|
+
originSizePx: number;
|
|
25
|
+
/** the most lines per axis (a far-off perspective view stops there) */
|
|
26
|
+
maxLines: number;
|
|
27
|
+
private readonly gridColor;
|
|
28
|
+
private readonly originColor;
|
|
29
|
+
private positions;
|
|
30
|
+
private colors;
|
|
31
|
+
constructor(options?: {
|
|
32
|
+
color?: ColorRepresentation;
|
|
33
|
+
originColor?: ColorRepresentation;
|
|
34
|
+
minSpacingPx?: number;
|
|
35
|
+
origin?: boolean;
|
|
36
|
+
originSizePx?: number;
|
|
37
|
+
});
|
|
38
|
+
/** Rebuilds the lines for the current view; `cell` is the base spacing in world units. */
|
|
39
|
+
update(view: EditorCamera, cell: number, z?: number): void;
|
|
40
|
+
private ensure;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { BufferAttribute, BufferGeometry, Color, LineBasicMaterial, LineSegments } from "three";
|
|
2
|
+
//#region src/three/adaptive-grid.ts
|
|
3
|
+
/**
|
|
4
|
+
* An infinite-looking grid on the XY plane for editor views: each {@link AdaptiveGrid.update}
|
|
5
|
+
* rebuilds just the lines in view, doubling the spacing when zoomed out so lines never get
|
|
6
|
+
* closer than `minSpacingPx`. Optional origin cross.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const grid = new AdaptiveGrid({ color: '#262626' });
|
|
10
|
+
* scene.add(grid);
|
|
11
|
+
* // per frame, after moving the camera:
|
|
12
|
+
* grid.update(view, 32);
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @category Three.js
|
|
16
|
+
*/
|
|
17
|
+
var AdaptiveGrid = class extends LineSegments {
|
|
18
|
+
minSpacingPx;
|
|
19
|
+
/** draw the grid lines (false: just the origin cross, if `origin`) */
|
|
20
|
+
lines = true;
|
|
21
|
+
/** draw a small cross at the origin (`originSizePx` long) */
|
|
22
|
+
origin;
|
|
23
|
+
originSizePx;
|
|
24
|
+
/** the most lines per axis (a far-off perspective view stops there) */
|
|
25
|
+
maxLines = 400;
|
|
26
|
+
gridColor;
|
|
27
|
+
originColor;
|
|
28
|
+
positions = /* @__PURE__ */ new Float32Array(0);
|
|
29
|
+
colors = /* @__PURE__ */ new Float32Array(0);
|
|
30
|
+
constructor(options = {}) {
|
|
31
|
+
super(new BufferGeometry(), new LineBasicMaterial({
|
|
32
|
+
vertexColors: true,
|
|
33
|
+
depthWrite: false,
|
|
34
|
+
transparent: true
|
|
35
|
+
}));
|
|
36
|
+
this.gridColor = new Color(options.color ?? "#262626");
|
|
37
|
+
this.originColor = new Color(options.originColor ?? "#555555");
|
|
38
|
+
this.minSpacingPx = options.minSpacingPx ?? 8;
|
|
39
|
+
this.origin = options.origin ?? true;
|
|
40
|
+
this.originSizePx = options.originSizePx ?? 10;
|
|
41
|
+
this.frustumCulled = false;
|
|
42
|
+
this.renderOrder = -1;
|
|
43
|
+
}
|
|
44
|
+
/** Rebuilds the lines for the current view; `cell` is the base spacing in world units. */
|
|
45
|
+
update(view, cell, z = 0) {
|
|
46
|
+
let step = cell;
|
|
47
|
+
while (step * view.zoom < this.minSpacingPx) step *= 2;
|
|
48
|
+
const pts = [
|
|
49
|
+
[0, 0],
|
|
50
|
+
[view.width, 0],
|
|
51
|
+
[0, view.height],
|
|
52
|
+
[view.width, view.height],
|
|
53
|
+
[view.width / 2, view.height / 2]
|
|
54
|
+
].map(([x, y]) => view.screenToWorld(x, y, z)).filter((p) => p !== null);
|
|
55
|
+
const far = step * this.maxLines / 2;
|
|
56
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
57
|
+
for (const p of pts) {
|
|
58
|
+
x0 = Math.min(x0, p.x);
|
|
59
|
+
x1 = Math.max(x1, p.x);
|
|
60
|
+
y0 = Math.min(y0, p.y);
|
|
61
|
+
y1 = Math.max(y1, p.y);
|
|
62
|
+
}
|
|
63
|
+
if (!pts.length || pts.length < 5) {
|
|
64
|
+
x0 = view.target.x - far;
|
|
65
|
+
x1 = view.target.x + far;
|
|
66
|
+
y0 = view.target.y - far;
|
|
67
|
+
y1 = view.target.y + far;
|
|
68
|
+
}
|
|
69
|
+
x0 = Math.max(x0, view.target.x - far);
|
|
70
|
+
x1 = Math.min(x1, view.target.x + far);
|
|
71
|
+
y0 = Math.max(y0, view.target.y - far);
|
|
72
|
+
y1 = Math.min(y1, view.target.y + far);
|
|
73
|
+
x0 = Math.floor(x0 / step) * step;
|
|
74
|
+
x1 = Math.ceil(x1 / step) * step;
|
|
75
|
+
y0 = Math.floor(y0 / step) * step;
|
|
76
|
+
y1 = Math.ceil(y1 / step) * step;
|
|
77
|
+
const nx = this.lines ? Math.max(0, Math.round((x1 - x0) / step) + 1) : 0;
|
|
78
|
+
const ny = this.lines ? Math.max(0, Math.round((y1 - y0) / step) + 1) : 0;
|
|
79
|
+
const segments = nx + ny + (this.origin ? 2 : 0);
|
|
80
|
+
this.ensure(segments * 2);
|
|
81
|
+
const pos = this.positions;
|
|
82
|
+
const col = this.colors;
|
|
83
|
+
const gc = this.gridColor;
|
|
84
|
+
let i = 0;
|
|
85
|
+
const seg = (ax, ay, bx, by, c) => {
|
|
86
|
+
pos[i * 3] = ax;
|
|
87
|
+
pos[i * 3 + 1] = ay;
|
|
88
|
+
pos[i * 3 + 2] = z;
|
|
89
|
+
col[i * 3] = c.r;
|
|
90
|
+
col[i * 3 + 1] = c.g;
|
|
91
|
+
col[i * 3 + 2] = c.b;
|
|
92
|
+
i++;
|
|
93
|
+
pos[i * 3] = bx;
|
|
94
|
+
pos[i * 3 + 1] = by;
|
|
95
|
+
pos[i * 3 + 2] = z;
|
|
96
|
+
col[i * 3] = c.r;
|
|
97
|
+
col[i * 3 + 1] = c.g;
|
|
98
|
+
col[i * 3 + 2] = c.b;
|
|
99
|
+
i++;
|
|
100
|
+
};
|
|
101
|
+
for (let k = 0; k < nx; k++) seg(x0 + k * step, y0, x0 + k * step, y1, gc);
|
|
102
|
+
for (let k = 0; k < ny; k++) seg(x0, y0 + k * step, x1, y0 + k * step, gc);
|
|
103
|
+
if (this.origin) {
|
|
104
|
+
const s = this.originSizePx / view.zoom;
|
|
105
|
+
seg(-s, 0, s, 0, this.originColor);
|
|
106
|
+
seg(0, -s, 0, s, this.originColor);
|
|
107
|
+
}
|
|
108
|
+
const g = this.geometry;
|
|
109
|
+
g.getAttribute("position").needsUpdate = true;
|
|
110
|
+
g.getAttribute("color").needsUpdate = true;
|
|
111
|
+
g.setDrawRange(0, i);
|
|
112
|
+
}
|
|
113
|
+
ensure(vertices) {
|
|
114
|
+
if (this.positions.length >= vertices * 3) return;
|
|
115
|
+
const size = Math.max(vertices * 3, this.positions.length * 2, 192);
|
|
116
|
+
this.positions = new Float32Array(size);
|
|
117
|
+
this.colors = new Float32Array(size);
|
|
118
|
+
this.geometry.setAttribute("position", new BufferAttribute(this.positions, 3));
|
|
119
|
+
this.geometry.setAttribute("color", new BufferAttribute(this.colors, 3));
|
|
120
|
+
}
|
|
121
|
+
dispose() {
|
|
122
|
+
this.geometry.dispose();
|
|
123
|
+
this.material.dispose();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
//#endregion
|
|
127
|
+
export { AdaptiveGrid };
|
package/dist/three/ecs.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export type ThreeComponent = Record<string, any>;
|
|
|
15
15
|
export interface ThreeEntity extends Object3D {
|
|
16
16
|
/** `Object3D.userData` with the entity's id and components */
|
|
17
17
|
userData: {
|
|
18
|
-
/**
|
|
18
|
+
/** the object's `uuid` (kept in sync by {@link World}) */
|
|
19
19
|
entityId: string;
|
|
20
20
|
/** components by name */
|
|
21
21
|
components: Record<string, ThreeComponent>;
|
|
@@ -25,12 +25,14 @@ export interface ThreeEntity extends Object3D {
|
|
|
25
25
|
/**
|
|
26
26
|
* Returns a new id `"entity_<n>"` from a module-level counter (unique per page load, not random).
|
|
27
27
|
*
|
|
28
|
+
* @deprecated entities now use their `Object3D.uuid` as id (stable across save and load);
|
|
29
|
+
* this is no longer used by {@link CreateThreeEntity}
|
|
28
30
|
* @category ECS
|
|
29
31
|
*/
|
|
30
32
|
export declare function GenerateEntityId(): string;
|
|
31
33
|
/**
|
|
32
|
-
* Turns `obj` (default: a new `Group`) into an entity in place:
|
|
33
|
-
*
|
|
34
|
+
* Turns `obj` (default: a new `Group`) into an entity in place: an empty component set
|
|
35
|
+
* (replacing any it had), with its `uuid` as id.
|
|
34
36
|
*
|
|
35
37
|
* ```ts
|
|
36
38
|
* const ship = CreateThreeEntity(shipMesh);
|
package/dist/three/ecs.js
CHANGED
|
@@ -4,14 +4,16 @@ let entityCounter = 0;
|
|
|
4
4
|
/**
|
|
5
5
|
* Returns a new id `"entity_<n>"` from a module-level counter (unique per page load, not random).
|
|
6
6
|
*
|
|
7
|
+
* @deprecated entities now use their `Object3D.uuid` as id (stable across save and load);
|
|
8
|
+
* this is no longer used by {@link CreateThreeEntity}
|
|
7
9
|
* @category ECS
|
|
8
10
|
*/
|
|
9
11
|
function GenerateEntityId() {
|
|
10
12
|
return `entity_${entityCounter++}`;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
|
-
* Turns `obj` (default: a new `Group`) into an entity in place:
|
|
14
|
-
*
|
|
15
|
+
* Turns `obj` (default: a new `Group`) into an entity in place: an empty component set
|
|
16
|
+
* (replacing any it had), with its `uuid` as id.
|
|
15
17
|
*
|
|
16
18
|
* ```ts
|
|
17
19
|
* const ship = CreateThreeEntity(shipMesh);
|
|
@@ -24,7 +26,7 @@ function GenerateEntityId() {
|
|
|
24
26
|
*/
|
|
25
27
|
function CreateThreeEntity(obj) {
|
|
26
28
|
const entity = obj ?? new Group();
|
|
27
|
-
entity.userData.entityId =
|
|
29
|
+
entity.userData.entityId = entity.uuid;
|
|
28
30
|
entity.userData.components = {};
|
|
29
31
|
return entity;
|
|
30
32
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { OrthographicCamera, PerspectiveCamera, Raycaster, Vector3 } from "three";
|
|
2
|
+
//#region src/three/editor-camera.d.ts
|
|
3
|
+
/** Screen edges covered by UI, CSS px. */
|
|
4
|
+
export interface ViewInsets {
|
|
5
|
+
l: number;
|
|
6
|
+
r: number;
|
|
7
|
+
t: number;
|
|
8
|
+
b: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* An editor viewport camera: orthographic or perspective, with pan, zoom-at-cursor, orbit
|
|
12
|
+
* and fit-to-bounds, and conversions between screen (CSS px, relative to the canvas) and
|
|
13
|
+
* world coordinates.
|
|
14
|
+
*
|
|
15
|
+
* Both projections share one rig — a `target` the camera looks at, `yaw`/`pitch` around it,
|
|
16
|
+
* and a `zoom` in screen px per world unit at the target — so switching projection keeps the
|
|
17
|
+
* framing. `yaw = pitch = 0` looks down -Z at the XY plane: a 2D view (three.js convention,
|
|
18
|
+
* +Y up). Set `orbit = false` to lock it there.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const view = new EditorCamera();
|
|
22
|
+
* view.resize(canvas.clientWidth, canvas.clientHeight);
|
|
23
|
+
* view.pan(dx, dy); // the world follows the pointer
|
|
24
|
+
* view.zoomAt(x, y, 1.1); // the point under the cursor stays put
|
|
25
|
+
* renderer.render(scene, view.camera); // the active three.js camera (updated by the calls above)
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @category Three.js
|
|
29
|
+
*/
|
|
30
|
+
export declare class EditorCamera {
|
|
31
|
+
readonly ortho: OrthographicCamera;
|
|
32
|
+
readonly perspective: PerspectiveCamera;
|
|
33
|
+
/** the point looked at (and orbited around) */
|
|
34
|
+
readonly target: Vector3;
|
|
35
|
+
/** radians around +Y */
|
|
36
|
+
yaw: number;
|
|
37
|
+
/** radians; positive looks down from above */
|
|
38
|
+
pitch: number;
|
|
39
|
+
/** screen px per world unit, at the target */
|
|
40
|
+
zoom: number;
|
|
41
|
+
minZoom: number;
|
|
42
|
+
maxZoom: number;
|
|
43
|
+
/** false locks yaw/pitch at 0 (a 2D view) */
|
|
44
|
+
orbit: boolean;
|
|
45
|
+
/** pitch limit, radians */
|
|
46
|
+
maxPitch: number;
|
|
47
|
+
/** view size, CSS px */
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
private _projection;
|
|
51
|
+
private readonly raycaster;
|
|
52
|
+
private readonly ndc;
|
|
53
|
+
private readonly plane;
|
|
54
|
+
constructor(options?: {
|
|
55
|
+
projection?: 'orthographic' | 'perspective';
|
|
56
|
+
fov?: number;
|
|
57
|
+
orbit?: boolean;
|
|
58
|
+
});
|
|
59
|
+
get projection(): 'orthographic' | 'perspective';
|
|
60
|
+
set projection(p: 'orthographic' | 'perspective');
|
|
61
|
+
/** the active three.js camera */
|
|
62
|
+
get camera(): OrthographicCamera | PerspectiveCamera;
|
|
63
|
+
/** distance from the target that gives `zoom` px per unit (perspective) */
|
|
64
|
+
get distance(): number;
|
|
65
|
+
resize(width: number, height: number): void;
|
|
66
|
+
/** Recomputes both cameras from the rig; the other methods call it. */
|
|
67
|
+
update(): void;
|
|
68
|
+
/** A ray from the camera through the screen point (CSS px). */
|
|
69
|
+
rayAt(sx: number, sy: number): Raycaster;
|
|
70
|
+
/**
|
|
71
|
+
* The world point under the screen point on the plane `z = planeZ` (default: through the
|
|
72
|
+
* target), or null if the view is edge-on to it.
|
|
73
|
+
*/
|
|
74
|
+
screenToWorld(sx: number, sy: number, planeZ?: number, out?: Vector3): Vector3 | null;
|
|
75
|
+
/** The screen point (CSS px) of a world point; `z` > 1 means behind the camera. */
|
|
76
|
+
worldToScreen(point: Vector3): {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
z: number;
|
|
80
|
+
};
|
|
81
|
+
/** Pans by a screen-space delta: the world follows the pointer. */
|
|
82
|
+
pan(dx: number, dy: number): void;
|
|
83
|
+
/** Orbits by a screen-space delta (no-op while `orbit` is false). */
|
|
84
|
+
rotate(dx: number, dy: number, speed?: number): void;
|
|
85
|
+
/** Zooms by `factor`, keeping the world point under (sx, sy) fixed where possible. */
|
|
86
|
+
zoomAt(sx: number, sy: number, factor: number): void;
|
|
87
|
+
/**
|
|
88
|
+
* Frames a box on the XY plane inside the view, leaving `margin` CSS px and keeping
|
|
89
|
+
* clear of `insets` (screen edges covered by UI). Keeps yaw/pitch.
|
|
90
|
+
*/
|
|
91
|
+
fit(minX: number, minY: number, maxX: number, maxY: number, insets?: ViewInsets, margin?: number, maxZoom?: number): void;
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { MathUtils, OrthographicCamera, PerspectiveCamera, Plane, Raycaster, Vector2, Vector3 } from "three";
|
|
2
|
+
//#region src/three/editor-camera.ts
|
|
3
|
+
/**
|
|
4
|
+
* An editor viewport camera: orthographic or perspective, with pan, zoom-at-cursor, orbit
|
|
5
|
+
* and fit-to-bounds, and conversions between screen (CSS px, relative to the canvas) and
|
|
6
|
+
* world coordinates.
|
|
7
|
+
*
|
|
8
|
+
* Both projections share one rig — a `target` the camera looks at, `yaw`/`pitch` around it,
|
|
9
|
+
* and a `zoom` in screen px per world unit at the target — so switching projection keeps the
|
|
10
|
+
* framing. `yaw = pitch = 0` looks down -Z at the XY plane: a 2D view (three.js convention,
|
|
11
|
+
* +Y up). Set `orbit = false` to lock it there.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const view = new EditorCamera();
|
|
15
|
+
* view.resize(canvas.clientWidth, canvas.clientHeight);
|
|
16
|
+
* view.pan(dx, dy); // the world follows the pointer
|
|
17
|
+
* view.zoomAt(x, y, 1.1); // the point under the cursor stays put
|
|
18
|
+
* renderer.render(scene, view.camera); // the active three.js camera (updated by the calls above)
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @category Three.js
|
|
22
|
+
*/
|
|
23
|
+
var EditorCamera = class {
|
|
24
|
+
ortho;
|
|
25
|
+
perspective;
|
|
26
|
+
/** the point looked at (and orbited around) */
|
|
27
|
+
target = new Vector3();
|
|
28
|
+
/** radians around +Y */
|
|
29
|
+
yaw = 0;
|
|
30
|
+
/** radians; positive looks down from above */
|
|
31
|
+
pitch = 0;
|
|
32
|
+
/** screen px per world unit, at the target */
|
|
33
|
+
zoom = 1;
|
|
34
|
+
minZoom = .01;
|
|
35
|
+
maxZoom = 100;
|
|
36
|
+
/** false locks yaw/pitch at 0 (a 2D view) */
|
|
37
|
+
orbit = true;
|
|
38
|
+
/** pitch limit, radians */
|
|
39
|
+
maxPitch = Math.PI / 2 - .01;
|
|
40
|
+
/** view size, CSS px */
|
|
41
|
+
width = 1;
|
|
42
|
+
height = 1;
|
|
43
|
+
_projection = "orthographic";
|
|
44
|
+
raycaster = new Raycaster();
|
|
45
|
+
ndc = new Vector2();
|
|
46
|
+
plane = new Plane();
|
|
47
|
+
constructor(options = {}) {
|
|
48
|
+
this.ortho = new OrthographicCamera(-1, 1, 1, -1, .1, 1e6);
|
|
49
|
+
this.perspective = new PerspectiveCamera(options.fov ?? 50, 1, .1, 1e6);
|
|
50
|
+
this._projection = options.projection ?? "orthographic";
|
|
51
|
+
if (options.orbit !== void 0) this.orbit = options.orbit;
|
|
52
|
+
this.update();
|
|
53
|
+
}
|
|
54
|
+
get projection() {
|
|
55
|
+
return this._projection;
|
|
56
|
+
}
|
|
57
|
+
set projection(p) {
|
|
58
|
+
this._projection = p;
|
|
59
|
+
this.update();
|
|
60
|
+
}
|
|
61
|
+
/** the active three.js camera */
|
|
62
|
+
get camera() {
|
|
63
|
+
return this._projection === "orthographic" ? this.ortho : this.perspective;
|
|
64
|
+
}
|
|
65
|
+
/** distance from the target that gives `zoom` px per unit (perspective) */
|
|
66
|
+
get distance() {
|
|
67
|
+
const halfFov = MathUtils.degToRad(this.perspective.fov) / 2;
|
|
68
|
+
return this.height / 2 / Math.tan(halfFov) / this.zoom;
|
|
69
|
+
}
|
|
70
|
+
resize(width, height) {
|
|
71
|
+
this.width = Math.max(1, width);
|
|
72
|
+
this.height = Math.max(1, height);
|
|
73
|
+
this.update();
|
|
74
|
+
}
|
|
75
|
+
/** Recomputes both cameras from the rig; the other methods call it. */
|
|
76
|
+
update() {
|
|
77
|
+
if (!this.orbit) this.yaw = this.pitch = 0;
|
|
78
|
+
this.pitch = MathUtils.clamp(this.pitch, -this.maxPitch, this.maxPitch);
|
|
79
|
+
this.zoom = MathUtils.clamp(this.zoom, this.minZoom, this.maxZoom);
|
|
80
|
+
const dir = new Vector3(Math.sin(this.yaw) * Math.cos(this.pitch), Math.sin(this.pitch), Math.cos(this.yaw) * Math.cos(this.pitch));
|
|
81
|
+
const p = this.perspective;
|
|
82
|
+
p.aspect = this.width / this.height;
|
|
83
|
+
p.position.copy(this.target).addScaledVector(dir, this.distance);
|
|
84
|
+
p.near = Math.max(.01, this.distance / 1e3);
|
|
85
|
+
p.far = this.distance * 1e3;
|
|
86
|
+
p.lookAt(this.target);
|
|
87
|
+
p.updateProjectionMatrix();
|
|
88
|
+
const o = this.ortho;
|
|
89
|
+
const hw = this.width / 2 / this.zoom;
|
|
90
|
+
const hh = this.height / 2 / this.zoom;
|
|
91
|
+
o.left = -hw;
|
|
92
|
+
o.right = hw;
|
|
93
|
+
o.top = hh;
|
|
94
|
+
o.bottom = -hh;
|
|
95
|
+
const back = Math.max(1e4, Math.max(hw, hh) * 20);
|
|
96
|
+
o.near = .1;
|
|
97
|
+
o.far = back * 2;
|
|
98
|
+
o.position.copy(this.target).addScaledVector(dir, back);
|
|
99
|
+
o.lookAt(this.target);
|
|
100
|
+
o.updateProjectionMatrix();
|
|
101
|
+
p.updateMatrixWorld();
|
|
102
|
+
o.updateMatrixWorld();
|
|
103
|
+
}
|
|
104
|
+
/** A ray from the camera through the screen point (CSS px). */
|
|
105
|
+
rayAt(sx, sy) {
|
|
106
|
+
this.ndc.set(sx / this.width * 2 - 1, -(sy / this.height) * 2 + 1);
|
|
107
|
+
this.raycaster.setFromCamera(this.ndc, this.camera);
|
|
108
|
+
return this.raycaster;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The world point under the screen point on the plane `z = planeZ` (default: through the
|
|
112
|
+
* target), or null if the view is edge-on to it.
|
|
113
|
+
*/
|
|
114
|
+
screenToWorld(sx, sy, planeZ = this.target.z, out = new Vector3()) {
|
|
115
|
+
this.plane.set(new Vector3(0, 0, 1), -planeZ);
|
|
116
|
+
return this.rayAt(sx, sy).ray.intersectPlane(this.plane, out);
|
|
117
|
+
}
|
|
118
|
+
/** The screen point (CSS px) of a world point; `z` > 1 means behind the camera. */
|
|
119
|
+
worldToScreen(point) {
|
|
120
|
+
const v = point.clone().project(this.camera);
|
|
121
|
+
return {
|
|
122
|
+
x: (v.x + 1) / 2 * this.width,
|
|
123
|
+
y: (1 - v.y) / 2 * this.height,
|
|
124
|
+
z: v.z
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/** Pans by a screen-space delta: the world follows the pointer. */
|
|
128
|
+
pan(dx, dy) {
|
|
129
|
+
const cam = this.camera;
|
|
130
|
+
const right = new Vector3().setFromMatrixColumn(cam.matrixWorld, 0);
|
|
131
|
+
const up = new Vector3().setFromMatrixColumn(cam.matrixWorld, 1);
|
|
132
|
+
this.target.addScaledVector(right, -dx / this.zoom).addScaledVector(up, dy / this.zoom);
|
|
133
|
+
this.update();
|
|
134
|
+
}
|
|
135
|
+
/** Orbits by a screen-space delta (no-op while `orbit` is false). */
|
|
136
|
+
rotate(dx, dy, speed = .005) {
|
|
137
|
+
if (!this.orbit) return;
|
|
138
|
+
this.yaw -= dx * speed;
|
|
139
|
+
this.pitch += dy * speed;
|
|
140
|
+
this.update();
|
|
141
|
+
}
|
|
142
|
+
/** Zooms by `factor`, keeping the world point under (sx, sy) fixed where possible. */
|
|
143
|
+
zoomAt(sx, sy, factor) {
|
|
144
|
+
const before = this.screenToWorld(sx, sy);
|
|
145
|
+
this.zoom *= factor;
|
|
146
|
+
this.update();
|
|
147
|
+
const after = before && this.screenToWorld(sx, sy);
|
|
148
|
+
if (before && after) {
|
|
149
|
+
this.target.add(before.sub(after));
|
|
150
|
+
this.update();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Frames a box on the XY plane inside the view, leaving `margin` CSS px and keeping
|
|
155
|
+
* clear of `insets` (screen edges covered by UI). Keeps yaw/pitch.
|
|
156
|
+
*/
|
|
157
|
+
fit(minX, minY, maxX, maxY, insets = {
|
|
158
|
+
l: 0,
|
|
159
|
+
r: 0,
|
|
160
|
+
t: 0,
|
|
161
|
+
b: 0
|
|
162
|
+
}, margin = 48, maxZoom = 2) {
|
|
163
|
+
const w = Math.max(1e-6, maxX - minX);
|
|
164
|
+
const h = Math.max(1e-6, maxY - minY);
|
|
165
|
+
const viewW = Math.max(1, this.width - insets.l - insets.r - margin * 2);
|
|
166
|
+
const viewH = Math.max(1, this.height - insets.t - insets.b - margin * 2);
|
|
167
|
+
this.zoom = MathUtils.clamp(Math.min(viewW / w, viewH / h), this.minZoom, Math.min(maxZoom, this.maxZoom));
|
|
168
|
+
this.target.set((minX + maxX) / 2, (minY + maxY) / 2, this.target.z);
|
|
169
|
+
this.update();
|
|
170
|
+
this.pan((insets.l - insets.r) / 2, (insets.t - insets.b) / 2);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
//#endregion
|
|
174
|
+
export { EditorCamera };
|
package/dist/three/index.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
import { EditorCamera, ViewInsets } from "./editor-camera.js";
|
|
2
|
+
import { AdaptiveGrid } from "./adaptive-grid.js";
|
|
1
3
|
import { AsteroidMesh, AsteroidOptions } from "./asteroid-mesh.js";
|
|
2
4
|
import { CameraRig, CameraRigParams, PitchLimits } from "./camera-rig.js";
|
|
3
5
|
import { CubeSphereGeometry } from "./cube-sphere-geometry.js";
|
|
4
6
|
import { AddThreeComponent, CreateThreeEntity, GenerateEntityId, GetThreeComponent, IsThreeEntity, RemoveThreeComponent, ThreeComponent, ThreeEntity, ThreeSystem } from "./ecs.js";
|
|
5
7
|
import { ParticleSystem3d } from "./particle-system-3d.js";
|
|
8
|
+
import { ComponentRegistry, ComponentSchema, ComponentType, FieldDef, FieldType, applyFieldValue, component, componentToJson, componentType, components, defineComponent, field, fieldToJson, inferFieldType } from "./schema.js";
|
|
9
|
+
import { ComponentRef, EntityDoc, SCENE_FORMAT, SCENE_VERSION, SceneDoc, World, WorldEvents, WorldSystem } from "./world.js";
|
|
10
|
+
import { EntityHit, PickOptions, entityOf, pickEntity } from "./picking.js";
|
|
6
11
|
import { Renderer } from "./renderer.js";
|
|
7
12
|
import { CreateStarfield, CreateSystemStar } from "./starfield.js";
|
|
8
13
|
import { CreateSubTexture, LoadTexture, TextureData } from "./texture-utils.js";
|
|
9
14
|
import { AtlasParams, TextureAtlas, UV } from "./texture-atlas.js";
|
|
10
15
|
import { BuildVoxelGeometryOptions, VoxelGeometry, buildVoxelGeometry } from "./voxel-geometry.js";
|
|
11
16
|
import { VoxelBounds, VoxelMesh, VoxelMeshOptions } from "./voxel-mesh.js";
|
|
12
|
-
export { AddThreeComponent, AsteroidMesh, AsteroidOptions, AtlasParams, BuildVoxelGeometryOptions, CameraRig, CameraRigParams, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, PitchLimits, RemoveThreeComponent, Renderer, TextureAtlas, TextureData, ThreeComponent, ThreeEntity, ThreeSystem, UV, VoxelBounds, VoxelGeometry, VoxelMesh, VoxelMeshOptions, buildVoxelGeometry };
|
|
17
|
+
export { AdaptiveGrid, AddThreeComponent, AsteroidMesh, AsteroidOptions, AtlasParams, BuildVoxelGeometryOptions, CameraRig, CameraRigParams, ComponentRef, ComponentRegistry, ComponentSchema, ComponentType, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, EditorCamera, EntityDoc, EntityHit, FieldDef, FieldType, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, PickOptions, PitchLimits, RemoveThreeComponent, Renderer, SCENE_FORMAT, SCENE_VERSION, SceneDoc, TextureAtlas, TextureData, ThreeComponent, ThreeEntity, ThreeSystem, UV, ViewInsets, VoxelBounds, VoxelGeometry, VoxelMesh, VoxelMeshOptions, World, WorldEvents, WorldSystem, applyFieldValue, buildVoxelGeometry, component, componentToJson, componentType, components, defineComponent, entityOf, field, fieldToJson, inferFieldType, pickEntity };
|
package/dist/three/index.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
import { AdaptiveGrid } from "./adaptive-grid.js";
|
|
1
2
|
import { AsteroidMesh } from "./asteroid-mesh.js";
|
|
2
3
|
import { CameraRig } from "./camera-rig.js";
|
|
3
4
|
import { CubeSphereGeometry } from "./cube-sphere-geometry.js";
|
|
4
5
|
import { AddThreeComponent, CreateThreeEntity, GenerateEntityId, GetThreeComponent, IsThreeEntity, RemoveThreeComponent } from "./ecs.js";
|
|
6
|
+
import { EditorCamera } from "./editor-camera.js";
|
|
5
7
|
import { ParticleSystem3d } from "./particle-system-3d.js";
|
|
8
|
+
import { entityOf, pickEntity } from "./picking.js";
|
|
6
9
|
import { Renderer } from "./renderer.js";
|
|
10
|
+
import { ComponentRegistry, applyFieldValue, component, componentToJson, componentType, components, defineComponent, field, fieldToJson, inferFieldType } from "./schema.js";
|
|
7
11
|
import { CreateStarfield, CreateSystemStar } from "./starfield.js";
|
|
8
12
|
import { CreateSubTexture, LoadTexture } from "./texture-utils.js";
|
|
9
13
|
import { TextureAtlas } from "./texture-atlas.js";
|
|
10
14
|
import { VoxelGeometry, buildVoxelGeometry } from "./voxel-geometry.js";
|
|
11
15
|
import { VoxelMesh } from "./voxel-mesh.js";
|
|
12
|
-
|
|
16
|
+
import { SCENE_FORMAT, SCENE_VERSION, World } from "./world.js";
|
|
17
|
+
export { AdaptiveGrid, AddThreeComponent, AsteroidMesh, CameraRig, ComponentRegistry, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, EditorCamera, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, RemoveThreeComponent, Renderer, SCENE_FORMAT, SCENE_VERSION, TextureAtlas, VoxelGeometry, VoxelMesh, World, applyFieldValue, buildVoxelGeometry, component, componentToJson, componentType, components, defineComponent, entityOf, field, fieldToJson, inferFieldType, pickEntity };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { EditorCamera } from "./editor-camera.js";
|
|
2
|
+
import { ThreeEntity } from "./ecs.js";
|
|
3
|
+
import { World } from "./world.js";
|
|
4
|
+
import { Object3D, Vector3 } from "three";
|
|
5
|
+
//#region src/three/picking.d.ts
|
|
6
|
+
/** @category Three.js */
|
|
7
|
+
export interface EntityHit {
|
|
8
|
+
entity: ThreeEntity;
|
|
9
|
+
/** world point hit */
|
|
10
|
+
point: Vector3;
|
|
11
|
+
distance: number;
|
|
12
|
+
/** the object actually hit (e.g. a mesh a render system made under the entity) */
|
|
13
|
+
object: Object3D;
|
|
14
|
+
}
|
|
15
|
+
/** @category Three.js */
|
|
16
|
+
export interface PickOptions {
|
|
17
|
+
/**
|
|
18
|
+
* On a miss, also try rays this many CSS px around the point and take the hit closest to
|
|
19
|
+
* it — small things are hard to hit with a finger.
|
|
20
|
+
*/
|
|
21
|
+
slopPx?: number;
|
|
22
|
+
/** skip hits on objects this returns false for (e.g. gizmos, helpers) */
|
|
23
|
+
filter?: (object: Object3D) => boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The entity under a screen point (CSS px): raycasts the world's scene and walks up from
|
|
27
|
+
* whatever was hit to the closest entity of `world`. Invisible objects are skipped.
|
|
28
|
+
*
|
|
29
|
+
* @category Three.js
|
|
30
|
+
*/
|
|
31
|
+
export declare function pickEntity(world: World, view: EditorCamera, sx: number, sy: number, options?: PickOptions): EntityHit | null;
|
|
32
|
+
/** The closest entity of `world` at or above `object`, or null. */
|
|
33
|
+
export declare function entityOf(world: World, object: Object3D): ThreeEntity | null;
|
|
34
|
+
//#endregion
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/three/picking.ts
|
|
2
|
+
/**
|
|
3
|
+
* The entity under a screen point (CSS px): raycasts the world's scene and walks up from
|
|
4
|
+
* whatever was hit to the closest entity of `world`. Invisible objects are skipped.
|
|
5
|
+
*
|
|
6
|
+
* @category Three.js
|
|
7
|
+
*/
|
|
8
|
+
function pickEntity(world, view, sx, sy, options = {}) {
|
|
9
|
+
world.root.updateMatrixWorld();
|
|
10
|
+
const direct = castAt(world, view, sx, sy, options.filter);
|
|
11
|
+
if (direct || !options.slopPx) return direct;
|
|
12
|
+
let best = null;
|
|
13
|
+
let bestD = Infinity;
|
|
14
|
+
for (const r of [options.slopPx / 2, options.slopPx]) {
|
|
15
|
+
for (let k = 0; k < 8; k++) {
|
|
16
|
+
const a = k / 8 * Math.PI * 2;
|
|
17
|
+
const hit = castAt(world, view, sx + Math.cos(a) * r, sy + Math.sin(a) * r, options.filter);
|
|
18
|
+
if (hit && r < bestD) {
|
|
19
|
+
best = hit;
|
|
20
|
+
bestD = r;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (best) break;
|
|
24
|
+
}
|
|
25
|
+
return best;
|
|
26
|
+
}
|
|
27
|
+
function castAt(world, view, sx, sy, filter) {
|
|
28
|
+
const hits = view.rayAt(sx, sy).intersectObject(world.root, true);
|
|
29
|
+
for (const hit of hits) {
|
|
30
|
+
if (!visible(hit.object) || filter && !filter(hit.object)) continue;
|
|
31
|
+
const entity = entityOf(world, hit.object);
|
|
32
|
+
if (entity) return {
|
|
33
|
+
entity,
|
|
34
|
+
point: hit.point,
|
|
35
|
+
distance: hit.distance,
|
|
36
|
+
object: hit.object
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/** The closest entity of `world` at or above `object`, or null. */
|
|
42
|
+
function entityOf(world, object) {
|
|
43
|
+
let o = object;
|
|
44
|
+
while (o) {
|
|
45
|
+
if (world.has(o)) return o;
|
|
46
|
+
o = o.parent;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
function visible(object) {
|
|
51
|
+
let o = object;
|
|
52
|
+
while (o) {
|
|
53
|
+
if (!o.visible) return false;
|
|
54
|
+
o = o.parent;
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { entityOf, pickEntity };
|