@margoengine/cameras 0.0.0-stage → 1.0.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.
@@ -0,0 +1,150 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import type { Camera, Engine, EnginePlugin, Vec3 } from '@margoengine/core/extension';
4
+ export { fitBox, type BoxFit, type BoxFitOptions, type BoxFitView } from './fit-box.js';
5
+ /** Options of `CameraProjection`: orthographic or perspective framing, frustum bounds, zoom and clip range. */
6
+ export interface ProjectionOptions {
7
+ /** 'orthographic' (default) or 'perspective'; both accept exactly vertical views. */
8
+ mode?: 'orthographic' | 'perspective';
9
+ /** Visible world height at zoom 1. Default: the perspective height at the target, 2·distance·tan(fov/2), so dolly controls zoom. */
10
+ height?: number;
11
+ /** Orthographic frustum bounds in view units. When all four are set they replace height and the canvas aspect. */
12
+ left?: number;
13
+ right?: number;
14
+ top?: number;
15
+ bottom?: number;
16
+ /** Zoom factor: divides the frustum about its centre. */
17
+ zoom?: number;
18
+ /** Eye-space clip distances, may be negative. Default: camera.far deep, centred on the target. */
19
+ near?: number;
20
+ far?: number;
21
+ /** Screen up when the view is parallel to camera world up; must not be parallel to that view. Default [0,0,-1]. */
22
+ up?: Vec3;
23
+ /** Render only a sub-rectangle of a larger frustum; see `ViewOffset`. Default: none, the whole frustum. */
24
+ viewOffset?: ViewOffset;
25
+ /** Lens focal length in millimetres. Replaces `camera.fov` with the field of view of this lens on `filmGauge`. Default: none, `camera.fov` rules. */
26
+ focalLength?: number;
27
+ /** Sensor size in millimetres along the fitted axis (see `sensorFit`). Default 36, a full-frame sensor's width. */
28
+ filmGauge?: number;
29
+ /** Which image axis `filmGauge` spans: 'horizontal', 'vertical', or 'auto' (the longer side, Blender's default). */
30
+ sensorFit?: SensorFit;
31
+ }
32
+ /** The image axis a sensor size spans. */
33
+ export type SensorFit = 'horizontal' | 'vertical' | 'auto';
34
+ /** Options of `lensToFov` and `fovToLens`. */
35
+ export interface LensOptions {
36
+ /** Sensor size in millimetres along the fitted axis. Default 36. */
37
+ filmGauge?: number;
38
+ /** Default 'auto': `filmGauge` spans the longer image side. */
39
+ sensorFit?: SensorFit;
40
+ }
41
+ /**
42
+ * Vertical field of view in degrees of a lens of `focalLength` mm for an image of `aspect` (width / height) on a sensor
43
+ * `filmGauge` mm along `sensorFit`: 2·atan(sensor / 2f) on that axis, converted to the vertical axis.
44
+ * A 50 mm lens on a 36 mm wide sensor is 39.6° horizontally and 26.99° vertically at 3:2.
45
+ */
46
+ export declare function lensToFov(focalLength: number, aspect: number, { filmGauge, sensorFit }?: LensOptions): number;
47
+ /** The focal length in millimetres that gives vertical field of view `fov` degrees; the inverse of `lensToFov`. */
48
+ export declare function fovToLens(fov: number, aspect: number, { filmGauge, sensorFit }?: LensOptions): number;
49
+ /**
50
+ * A sub-rectangle of a larger image, in pixels of that image with the origin at its top left. The frustum is built
51
+ * for the full image (aspect `fullWidth / fullHeight`) and the view shows only the rectangle, so the canvas or render
52
+ * target should be `width` by `height`. `x` and `y` may be fractions (jittered rendering) or negative.
53
+ */
54
+ export interface ViewOffset {
55
+ fullWidth: number;
56
+ fullHeight: number;
57
+ x: number;
58
+ y: number;
59
+ width: number;
60
+ height: number;
61
+ }
62
+ /** Splits a `fullWidth` by `fullHeight` image into `tileWidth` by `tileHeight` offsets, row by row from the top left; edge tiles are smaller. */
63
+ export declare function tileOffsets(fullWidth: number, fullHeight: number, tileWidth: number, tileHeight?: number): ViewOffset[];
64
+ /** Options of `CameraProjection.fit`: the view's aspect, the animation time and the margin around the box. */
65
+ export interface FitOptions {
66
+ /** Width / height of the view. Default: the engine canvas (`setup`), else 1. */
67
+ aspect?: number;
68
+ /** Seconds; 0 jumps. Default 0.3, like core's `fitToBoundingBox`. */
69
+ duration?: number;
70
+ /** At least 1. Default 1.1. */
71
+ padding?: number;
72
+ /**
73
+ * Perspective only: 'sphere' (default) frames the box's bounding sphere, as core's `fitToBoundingBox`; 'box' moves
74
+ * the camera, along its own view, to the closest point from which all eight corners sit in the padded frame (see
75
+ * {@link fitBox}), so a wide, flat or tall model fills the frame. Orthographic fits always use the box's outline.
76
+ */
77
+ framing?: 'sphere' | 'box';
78
+ }
79
+ /**
80
+ * Orthographic projection and exactly vertical views for core's Camera, through its `project` and `up` hooks, so the
81
+ * matrix, culling planes, `engine.pick`, every pass and every `camera.matrix` reader get the same view.
82
+ */
83
+ export declare class CameraProjection implements EnginePlugin, ProjectionOptions {
84
+ readonly name = "cameras";
85
+ mode: 'orthographic' | 'perspective';
86
+ height?: number;
87
+ left?: number;
88
+ right?: number;
89
+ top?: number;
90
+ bottom?: number;
91
+ zoom: number;
92
+ near?: number;
93
+ far?: number;
94
+ up: Vec3;
95
+ viewOffset?: ViewOffset;
96
+ focalLength?: number;
97
+ filmGauge: number;
98
+ sensorFit: SensorFit;
99
+ private camera?;
100
+ private worldUp;
101
+ private canvas?;
102
+ /** The engine this projection was attached to as a plugin, so dispose() can detach it from its scene. */
103
+ private host?;
104
+ private animation?;
105
+ /** Same as `engine.use(new CameraProjection(...))`; returns the CameraProjection. */
106
+ static attach(engine: EngineKernel, options?: ProjectionOptions): CameraProjection;
107
+ constructor(options?: ProjectionOptions);
108
+ setup(engine: Pick<Engine, 'camera'> & {
109
+ canvas?: {
110
+ width: number;
111
+ height: number;
112
+ };
113
+ sceneOf?: EngineKernel['sceneOf'];
114
+ }): void;
115
+ /**
116
+ * Frames a box given as `[minX, minY, minZ, maxX, maxY, maxZ]` (`model.bounds`). Perspective: core's
117
+ * `fitToBoundingBox`. Orthographic: keeps the view direction and distance, centres the box and sets `zoom` so the
118
+ * box's projected outline fits (tighter than a bounding sphere). Animated by the engine (`onPreUpdate`).
119
+ */
120
+ fit(bounds: ArrayLike<number>, { aspect, duration, padding, framing, }?: FitOptions): void;
121
+ /** Advances an orthographic `fit`; the engine calls it each frame. Call it yourself after `attach(camera)`. */
122
+ onPreUpdate(delta: number): void;
123
+ /**
124
+ * Renders the `width` by `height` rectangle at (`x`, `y`) of a `fullWidth` by `fullHeight` image (see `ViewOffset`).
125
+ * Works in both modes and with any `zoom`, `height` or bounds; `fit` still frames the whole canvas.
126
+ */
127
+ setViewOffset(fullWidth: number, fullHeight: number, x: number, y: number, width: number, height: number): void;
128
+ /**
129
+ * With a `focalLength`, writes the lens's vertical field of view for `aspect` to `camera.fov`, so passes and controls
130
+ * that read `camera.fov` agree with the projection, and returns it; else returns `camera.fov`. The engine does this
131
+ * each frame (`onPreUpdate`); call it yourself without an engine.
132
+ */
133
+ applyLens(aspect: number): number;
134
+ /** Goes back to the whole frustum. */
135
+ clearViewOffset(): void;
136
+ /** Use without an engine, e.g. on a second Camera. */
137
+ attach(camera: Camera): void;
138
+ /** Gives the camera back its own projection and up, and detaches from the engine's scene it was attached to. */
139
+ dispose(): void;
140
+ [Symbol.dispose]: () => void;
141
+ /**
142
+ * True when the orthographic view size is fixed (`height` or all four bounds set), so moving the camera along its
143
+ * view direction does not change what is visible. Also exposed on the attached camera for controls.
144
+ */
145
+ get fixedOrthographic(): boolean;
146
+ private screenUp;
147
+ private vfov;
148
+ private project;
149
+ private build;
150
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { t as CameraProjection, a as fitBox, n as fovToLens, r as lensToFov, i as tileOffsets } from "./src-_OQ7cR69.js";
@@ -0,0 +1,59 @@
1
+ import { i as e, n as t } from "./view-rect-p0kR0Q6B.js";
2
+ //#region ../shared/layers.js
3
+ var n = Symbol.for("margoengine.layers");
4
+ function r() {
5
+ return globalThis[n] ??= {
6
+ masks: /* @__PURE__ */ new WeakMap(),
7
+ cameras: /* @__PURE__ */ new WeakMap(),
8
+ source: null,
9
+ used: !1,
10
+ maskOf(e) {
11
+ if (this.source) return this.source(e) >>> 0;
12
+ if (!this.used) return 1;
13
+ let t = e;
14
+ for (let e = 0; t && e < 64; e++) {
15
+ let e = this.masks.get(t);
16
+ if (e !== void 0) return e;
17
+ let n = t.engine.store.parents[t.id];
18
+ t = n >= 0 ? t.engine.meshes.get(n) : void 0;
19
+ }
20
+ return 1;
21
+ }
22
+ };
23
+ }
24
+ function i(e, n) {
25
+ let i = r();
26
+ i.masks.set(e, t(n, "Render layer mask")), i.used = !0, e.engine && (e.engine.collected = -1);
27
+ }
28
+ //#endregion
29
+ //#region src/layers.ts
30
+ var a = 4294967295, o = r();
31
+ function s(...e) {
32
+ let t = 0;
33
+ for (let n of e) {
34
+ if (!Number.isInteger(n) || n < 0 || n > 31) throw RangeError(`Layer ${n} is outside 0..31; pass a layer index from 0 to 31`);
35
+ t |= 1 << n;
36
+ }
37
+ return t >>> 0;
38
+ }
39
+ function c(e, t) {
40
+ i(e, t);
41
+ }
42
+ function l(e) {
43
+ return o.maskOf(e);
44
+ }
45
+ function u(e) {
46
+ if (e !== null && typeof e != "function") throw TypeError("Layer source must be a function (mesh) => mask or null; pass such a function or null");
47
+ o.source = e;
48
+ }
49
+ function d(e, n) {
50
+ o.cameras.set(e, t(n, "Camera layer mask"));
51
+ }
52
+ function f(e) {
53
+ return o.cameras.get(e) ?? 4294967295;
54
+ }
55
+ function p(n, r, i, a) {
56
+ return e(n, t(r, "Layer mask"), a, i);
57
+ }
58
+ //#endregion
59
+ export { d as a, p as c, s as i, f as n, c as o, l as r, u as s, a as t };
@@ -0,0 +1,36 @@
1
+ import type { EngineKernel, Mesh } from '@margoengine/core';
2
+ import type { Camera } from '@margoengine/core/extension';
3
+ /** Every layer: the mask that filters nothing. */
4
+ export declare const ALL_LAYERS = 4294967295;
5
+ /** Mask with the given layers (0-31) enabled: `layerMask(0, 2)` is 0b101. */
6
+ export declare function layerMask(...layers: number[]): number;
7
+ /**
8
+ * Sets the layer mask of `mesh` (32-bit, default 1 = layer 0), the one mask drawing and @margoengine/bvh raycasts read.
9
+ * For drawing, its descendants without a mask of their own follow it, so `setRenderLayers(model.root, mask)` layers a
10
+ * whole model; a Raycaster tests a mesh's own mask (a model primitive: its node's, else `model.root`'s).
11
+ * @throws RangeError for a mask that is not an integer from 0 to 0xffffffff.
12
+ */
13
+ export declare function setRenderLayers(mesh: Mesh, mask: number): void;
14
+ /** Render layer mask of `mesh`: its own, else its nearest ancestor's, else 1; or the `useLayerSource` reader's value. */
15
+ export declare function getRenderLayers(mesh: Mesh): number;
16
+ /**
17
+ * Reads every mesh mask for drawing through `source` instead of `setRenderLayers`, or restores the default with null.
18
+ * @throws TypeError when `source` is neither a function nor null.
19
+ */
20
+ export declare function useLayerSource(source: ((mesh: Mesh) => number) | null): void;
21
+ /**
22
+ * Sets the layer mask a camera draws (default every layer): the engine draws its frame, `render({camera})` and every
23
+ * view through that camera with it, unless a view sets its own `layers`.
24
+ * @throws RangeError for a mask that is not an integer from 0 to 0xffffffff.
25
+ */
26
+ export declare function setCameraLayers(camera: Camera, mask: number): void;
27
+ /** Layer mask `camera` draws: the one set with `setCameraLayers`, else every layer. */
28
+ export declare function getCameraLayers(camera: Camera): number;
29
+ /**
30
+ * Runs `run` with only the meshes in `mask` (and not in `exclude`) visible, then makes the hidden ones visible again,
31
+ * also when `run` throws: for a capture that also leaves out given meshes (a mirror leaving out itself). To filter
32
+ * what a view draws by layers alone, give the view `layers` or its camera `setCameraLayers` instead; that hides
33
+ * nothing. A hidden parent hides its children.
34
+ * @throws RangeError for a mask that is not an integer from 0 to 0xffffffff.
35
+ */
36
+ export declare function withLayers<T>(engine: EngineKernel, mask: number, run: () => T, exclude?: Iterable<Mesh>): T;
package/dist/layers.js ADDED
@@ -0,0 +1 @@
1
+ export { t as ALL_LAYERS, n as getCameraLayers, r as getRenderLayers, i as layerMask, a as setCameraLayers, o as setRenderLayers, s as useLayerSource, c as withLayers } from "./layers-CkdgcqTK.js";
@@ -0,0 +1,82 @@
1
+ import type { EngineKernel } from '@margoengine/core';
2
+ import type { Camera, EnginePlugin, Vec3 } from '@margoengine/core/extension';
3
+ /** A flat display or wall in world space, from three corners: lower left, lower right and upper left. Counter clockwise seen from the viewer. */
4
+ export interface Screen {
5
+ lowerLeft: Vec3;
6
+ lowerRight: Vec3;
7
+ upperLeft: Vec3;
8
+ }
9
+ /** The result of `offAxisFrustum`: the frustum bounds on the near plane and the screen's axes. */
10
+ export interface OffAxisFrustum {
11
+ /** Frustum bounds on the near plane, along the screen's right and up axes. */
12
+ left: number;
13
+ right: number;
14
+ bottom: number;
15
+ top: number;
16
+ /** Unit axes of the screen: right, up, and the normal pointing at the viewer. */
17
+ xAxis: Vec3;
18
+ yAxis: Vec3;
19
+ normal: Vec3;
20
+ /** Distance from the eye to the screen plane. */
21
+ distance: number;
22
+ }
23
+ /** A plane `[nx, ny, nz, d]`: points with `n·p + d >= 0` are kept. */
24
+ export type Plane = readonly [number, number, number, number];
25
+ /**
26
+ * The asymmetric frustum that makes a flat screen a window onto the scene for an eye at `eye`: the bounds on the plane
27
+ * at distance `near` along the screen's normal. Head-tracked displays, CAVE walls and portals use it.
28
+ */
29
+ export declare function offAxisFrustum(screen: Screen, eye: ArrayLike<number>, near: number): OffAxisFrustum;
30
+ /**
31
+ * Off-axis (asymmetric) projection for a flat screen: head-tracked displays, CAVE walls and portals. The camera looks
32
+ * straight into the screen from the eye, so `camera.matrix`, culling, picking and every pass see the same view. Move
33
+ * the eye with `setEye`. The canvas aspect is ignored: the screen's shape rules, so size the canvas to match it.
34
+ */
35
+ export declare class OffAxisProjection implements EnginePlugin {
36
+ screen: Screen;
37
+ readonly name = "offaxis";
38
+ private camera?;
39
+ private eye;
40
+ private up?;
41
+ constructor(screen: Screen, eye?: Vec3);
42
+ /** Same as `engine.use(new OffAxisProjection(screen, eye))`; returns the projection. */
43
+ static attach(engine: EngineKernel, screen: Screen, eye?: Vec3): OffAxisProjection;
44
+ setup(engine: {
45
+ camera: Camera;
46
+ }): void;
47
+ /** Use without an engine, e.g. on a second Camera. Throws when the camera already has a projection. */
48
+ attach(camera: Camera): void;
49
+ /** Moves the eye (world coordinates) and aims the camera straight into the screen. */
50
+ setEye(eye: Vec3): void;
51
+ dispose(): void;
52
+ [Symbol.dispose]: () => void;
53
+ private project;
54
+ }
55
+ /**
56
+ * Replaces the near plane of the projection `P` (column-major, edited in place) with `plane` given in eye space, so
57
+ * everything on the plane's negative side is clipped, with no extra clip plane in the shader. Works for perspective and
58
+ * orthographic matrices in both depth conventions (`webgpu`: reversed, near 1 and far 0; else -1 to 1). The far plane
59
+ * stays at the corner opposite the plane. The eye must be on the clipped side.
60
+ */
61
+ export declare function obliqueProjection(P: Float64Array, plane: Plane, webgpu: boolean): void;
62
+ /**
63
+ * Oblique near plane for any camera: set `plane` (world space, `[nx, ny, nz, d]`, points with `n·p + d >= 0` are kept)
64
+ * and everything behind it is clipped, as portals and mirrors need. It wraps the camera's projection (core's, or
65
+ * `CameraProjection`'s or `OffAxisProjection`'s), so attach it last and `dispose()` it first. With no `plane` it
66
+ * changes nothing. The camera has to stay on the clipped side of the plane.
67
+ */
68
+ export declare class ObliqueNearPlane {
69
+ /** World space plane, or undefined for none. */
70
+ plane?: Plane;
71
+ private camera?;
72
+ private inner?;
73
+ private hook?;
74
+ constructor(plane?: Plane);
75
+ /** Wraps `camera.project`; returns this. */
76
+ attach(camera: Camera): this;
77
+ /** Restores the projection this wrapped. */
78
+ dispose(): void;
79
+ [Symbol.dispose]: () => void;
80
+ /** The world plane in the camera's eye space, from its position, target and up (the basis core builds the view from). */
81
+ private eyePlane;
82
+ }
@@ -0,0 +1,175 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { t as e } from "./errors-Cp7bu2gW.js";
5
+ //#region src/offaxis.ts
6
+ var t = (e, t) => [
7
+ e[0] - t[0],
8
+ e[1] - t[1],
9
+ e[2] - t[2]
10
+ ], n = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = (e) => {
11
+ let t = Math.hypot(e[0], e[1], e[2]);
12
+ return [
13
+ e[0] / t,
14
+ e[1] / t,
15
+ e[2] / t
16
+ ];
17
+ }, i = (e, t) => [
18
+ e[1] * t[2] - e[2] * t[1],
19
+ e[2] * t[0] - e[0] * t[2],
20
+ e[0] * t[1] - e[1] * t[0]
21
+ ];
22
+ function a(e, a, o) {
23
+ if (![
24
+ e.lowerLeft,
25
+ e.lowerRight,
26
+ e.upperLeft,
27
+ a
28
+ ].every((e) => e?.length >= 3 && Array.from(e).every(Number.isFinite)) || !(o > 0) || o === Infinity) throw RangeError("Invalid off-axis screen; pass three finite corners, a finite eye and near above 0");
29
+ let s = t(e.lowerRight, e.lowerLeft), c = t(e.upperLeft, e.lowerLeft), l = i(s, c);
30
+ if (!Math.hypot(...l)) throw RangeError("Invalid off-axis screen; the corners must not lie on one line");
31
+ let u = r(s), d = r(c), f = r(l), p = t(e.lowerLeft, a), m = t(e.lowerRight, a), h = t(e.upperLeft, a), g = -n(p, f);
32
+ if (!(g > 0)) throw RangeError("Invalid off-axis eye; put the eye in front of the screen, on the side its corners face");
33
+ let _ = o / g;
34
+ return {
35
+ left: n(u, p) * _,
36
+ right: n(u, m) * _,
37
+ bottom: n(d, p) * _,
38
+ top: n(d, h) * _,
39
+ xAxis: u,
40
+ yAxis: d,
41
+ normal: f,
42
+ distance: g
43
+ };
44
+ }
45
+ var o = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
46
+ constructor(e, t = [
47
+ 0,
48
+ 0,
49
+ 1
50
+ ]) {
51
+ this.screen = e, this.name = "offaxis", this.eye = t;
52
+ }
53
+ static attach(e, n, r) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
54
+ let i = new t(n, r);
55
+ return e.use(i), i;
56
+ }
57
+ setup(e) {
58
+ this.attach(e.camera);
59
+ }
60
+ attach(t) {
61
+ if (this.camera) throw e("attached", "Projection is already attached; dispose() it or create another");
62
+ if (t.project) throw e("attached", "Camera already has a projection; dispose that projection first");
63
+ this.camera = t, this.up = t.up, t.project = (e, n, r) => this.project(t, e, r), this.setEye(this.eye);
64
+ }
65
+ setEye(t) {
66
+ let n = this.camera;
67
+ if (!n) throw e("state", "Projection is not attached; call attach(camera) or engine.use(projection) first");
68
+ let r = a(this.screen, t, n.near);
69
+ this.eye = [
70
+ t[0],
71
+ t[1],
72
+ t[2]
73
+ ], n.position = this.eye, n.target = [
74
+ t[0] - r.normal[0],
75
+ t[1] - r.normal[1],
76
+ t[2] - r.normal[2]
77
+ ], n.up = r.yAxis;
78
+ }
79
+ dispose() {
80
+ let e = this.camera;
81
+ e && (this.camera = void 0, e.project = void 0, this.up && (e.up = this.up));
82
+ }
83
+ [Symbol.dispose ?? Symbol()]() {
84
+ this.dispose();
85
+ }
86
+ project(e, t, n) {
87
+ let r = e.near, i = e.far, o = a(this.screen, e.position, r), s = o.right - o.left, c = o.top - o.bottom, l = r - i;
88
+ t[0] = 2 * r / s, t[5] = 2 * r / c, t[8] = (o.right + o.left) / s, t[9] = (o.top + o.bottom) / c, t[10] = n ? -r / l : (i + r) / l, t[11] = -1, t[14] = n ? -i * r / l : 2 * i * r / l;
89
+ }
90
+ };
91
+ function s(e, t) {
92
+ let n = e.map((e, n) => [...e, t[n]]);
93
+ for (let e = 0; e < 4; e++) {
94
+ let t = e;
95
+ for (let r = e + 1; r < 4; r++) Math.abs(n[r][e]) > Math.abs(n[t][e]) && (t = r);
96
+ if (!(Math.abs(n[t][e]) > 1e-300)) return null;
97
+ [n[e], n[t]] = [n[t], n[e]];
98
+ for (let t = 0; t < 4; t++) {
99
+ if (t === e) continue;
100
+ let r = n[t][e] / n[e][e];
101
+ for (let i = e; i < 5; i++) n[t][i] -= r * n[e][i];
102
+ }
103
+ }
104
+ return n.map((e, t) => e[4] / e[t]);
105
+ }
106
+ function c(e, t, n) {
107
+ if (t.length !== 4 || !t.every(Number.isFinite) || !Math.hypot(t[0], t[1], t[2])) throw RangeError("Invalid oblique plane; pass finite [nx, ny, nz, d] with a normal that is not zero");
108
+ if (!(t[3] < 0)) throw RangeError("Invalid oblique plane; the camera must be on the clipped side, behind the plane");
109
+ let r = [
110
+ 0,
111
+ 1,
112
+ 2,
113
+ 3
114
+ ].map((t) => [
115
+ 0,
116
+ 1,
117
+ 2,
118
+ 3
119
+ ].map((n) => e[n * 4 + t])), i = [
120
+ 0,
121
+ 1,
122
+ 2,
123
+ 3
124
+ ].map((e) => r.map((t) => t[e])), a = n ? 1 : -1, o = +!n, c = s(i, [...t]);
125
+ if (!c) throw RangeError("Invalid oblique plane; the projection matrix cannot be inverted");
126
+ let l = s(r, [
127
+ Math.sign(c[0]) || 1,
128
+ Math.sign(c[1]) || 1,
129
+ o,
130
+ 1
131
+ ]);
132
+ if (!l) throw RangeError("Invalid oblique plane; the projection matrix cannot be inverted");
133
+ let u = t[0] * l[0] + t[1] * l[1] + t[2] * l[2] + t[3] * l[3];
134
+ if (!u) throw RangeError("Invalid oblique plane; it passes through the far corner, move or tilt it");
135
+ let d = e[3] * l[0] + e[7] * l[1] + e[11] * l[2] + e[15] * l[3], f = (o - a) * d / u;
136
+ for (let n = 0; n < 4; n++) e[n * 4 + 2] = a * e[n * 4 + 3] + f * t[n];
137
+ }
138
+ var l = class {
139
+ constructor(e) {
140
+ this.plane = e;
141
+ }
142
+ attach(t) {
143
+ if (this.camera) throw e("attached", "Oblique plane is already attached; dispose() it or create another");
144
+ return this.camera = t, this.inner = t.project, this.hook = (e, n, r) => {
145
+ if (this.inner) this.inner(e, n, r);
146
+ else {
147
+ let i = t.near, a = t.far, o = i - a, s = 1 / Math.tan(t.fov * Math.PI / 360);
148
+ e[0] = s / n, e[5] = s, e[10] = r ? -i / o : (a + i) / o, e[11] = -1, e[14] = r ? -a * i / o : 2 * a * i / o;
149
+ }
150
+ this.plane && c(e, this.eyePlane(t, this.plane), r);
151
+ }, t.project = this.hook, this;
152
+ }
153
+ dispose() {
154
+ let e = this.camera;
155
+ e && (e.project === this.hook && (e.project = this.inner), this.camera = void 0);
156
+ }
157
+ [Symbol.dispose ?? Symbol()]() {
158
+ this.dispose();
159
+ }
160
+ eyePlane(e, a) {
161
+ let o = e.position, s = r(t(o, e.target)), c = r(i(e.up, s)), l = i(s, c), u = [
162
+ a[0],
163
+ a[1],
164
+ a[2]
165
+ ];
166
+ return [
167
+ n(c, u),
168
+ n(l, u),
169
+ n(s, u),
170
+ n(u, o) + a[3]
171
+ ];
172
+ }
173
+ };
174
+ //#endregion
175
+ export { l as ObliqueNearPlane, o as OffAxisProjection, c as obliqueProjection, a as offAxisFrustum };
@@ -0,0 +1,63 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel, EnginePlugin, Mesh } from '@margoengine/core';
3
+ import { type Vec3 } from '@margoengine/core/extension';
4
+ /** Options of `FloatingOrigin`. */
5
+ export interface FloatingOriginOptions {
6
+ /** Rebase when the camera is farther from the origin than `ratio` × its distance to `camera.target`. Default 1000. */
7
+ ratio?: number;
8
+ }
9
+ /** Called after a rebase with the shift (new origin − old origin) and the new origin, both float64 world coordinates. */
10
+ export type RebaseListener = (shift: Vec3, origin: Vec3) => void;
11
+ /**
12
+ * Floating origin for scenes kilometres from the world origin (plant layouts, BIM, GIS). Root meshes placed with
13
+ * `place` keep float64 world positions on the CPU; core sees them relative to `origin`, a point near the camera, so
14
+ * core's float32 matrices stay small. The camera, controls, picks, bvh hits and section planes work in those local
15
+ * coordinates; `toWorld`/`toLocal` convert. When the camera drifts past the threshold the origin moves to the camera:
16
+ * roots and camera shift together, so nothing visibly moves (one full transform update of the placed roots).
17
+ */
18
+ export declare class FloatingOrigin implements EnginePlugin {
19
+ readonly name = "floating-origin";
20
+ ratio: number;
21
+ /** Number of rebases so far. */
22
+ rebases: number;
23
+ private value;
24
+ private engine?;
25
+ private readonly roots;
26
+ private readonly listeners;
27
+ /** Same as `engine.use(new FloatingOrigin(options))`; returns the FloatingOrigin. */
28
+ static attach(engine: EngineKernel, options?: FloatingOriginOptions): FloatingOrigin;
29
+ constructor({ ratio }?: FloatingOriginOptions);
30
+ setup(engine: EngineKernel): void;
31
+ /** Float64 world position of local [0, 0, 0]. */
32
+ get origin(): Vec3;
33
+ private get camera();
34
+ /** Places a root mesh (or a gltf\/pbr `model.root`) at a float64 world position and keeps it there across rebases. */
35
+ place(mesh: Mesh, world: ArrayLike<number>): void;
36
+ /** Stops tracking a mesh (it keeps its current local position). */
37
+ forget(mesh: Mesh): void;
38
+ /** Float64 world position of a placed root, or undefined. A root moved through `mesh.position` since is re-read. */
39
+ worldPosition(mesh: Mesh): Vec3 | undefined;
40
+ /** Local (core) point → float64 world, e.g. a bvh hit point or a measurement anchor. */
41
+ toWorld(local: ArrayLike<number>): Vec3;
42
+ /** Float64 world point → local (core) coordinates. */
43
+ toLocal(world: ArrayLike<number>): Vec3;
44
+ /** World plane [nx, ny, nz, d] (n·x + d = 0) → local plane, e.g. for materials `clippingPlanes`; redo it in `onRebase`. */
45
+ toLocalPlane(plane: ArrayLike<number>): [number, number, number, number];
46
+ /** Points the camera in float64 world coordinates (rebasing first when needed). */
47
+ lookAt(position: ArrayLike<number>, target: ArrayLike<number>): void;
48
+ /** Camera position in float64 world coordinates. */
49
+ get cameraPosition(): Vec3;
50
+ /** Camera target in float64 world coordinates. */
51
+ get cameraTarget(): Vec3;
52
+ /** Calls `listener(shift, origin)` after each rebase (shift app-held local data: saved views, planes); returns an unsubscribe. */
53
+ onRebase(listener: RebaseListener): () => void;
54
+ /** Moves the origin to a float64 world point (default: the camera); the camera and placed roots shift with it. */
55
+ rebase(origin?: ArrayLike<number>): void;
56
+ /** Rebases when the camera is past the threshold; the engine calls it before each update. */
57
+ onPreUpdate(): void;
58
+ dispose(): void;
59
+ [Symbol.dispose]: () => void;
60
+ private check;
61
+ private write;
62
+ private sync;
63
+ }