@margoengine/cameras 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +621 -3
- package/dist/errors-Cp7bu2gW.js +7 -0
- package/dist/fit-box.d.ts +34 -0
- package/dist/gpu-BRxrrZK3.js +31 -0
- package/dist/hologram.d.ts +75 -0
- package/dist/hologram.js +208 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +1 -0
- package/dist/layers-CkdgcqTK.js +59 -0
- package/dist/layers.d.ts +36 -0
- package/dist/layers.js +1 -0
- package/dist/offaxis.d.ts +82 -0
- package/dist/offaxis.js +175 -0
- package/dist/origin.d.ts +63 -0
- package/dist/origin.js +147 -0
- package/dist/panini.d.ts +90 -0
- package/dist/panini.js +184 -0
- package/dist/panorama.d.ts +46 -0
- package/dist/panorama.js +128 -0
- package/dist/physical.d.ts +99 -0
- package/dist/physical.js +77 -0
- package/dist/src-_OQ7cR69.js +353 -0
- package/dist/stereo.d.ts +61 -0
- package/dist/stereo.js +181 -0
- package/dist/view-rect-p0kR0Q6B.js +38 -0
- package/dist/wide-cube.d.ts +123 -0
- package/dist/wide-cube.js +215 -0
- package/dist/wide-laws-Dj0410P1.js +20 -0
- package/dist/wide-laws.d.ts +5 -0
- package/package.json +99 -6
|
@@ -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
|
+
}
|
package/dist/offaxis.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// @margo/core-built 0.1.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("0.1.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("0.1.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 };
|
package/dist/origin.d.ts
ADDED
|
@@ -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
|
+
}
|
package/dist/origin.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./errors-Cp7bu2gW.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime, meshRuntime as n } from "@margoengine/core/extension";
|
|
6
|
+
//#region src/origin.ts
|
|
7
|
+
var r = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
8
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
9
|
+
let n = new r(t);
|
|
10
|
+
return e.use(n), n;
|
|
11
|
+
}
|
|
12
|
+
constructor({ ratio: e = 1e3 } = {}) {
|
|
13
|
+
if (this.name = "floating-origin", this.ratio = 1e3, this.rebases = 0, this.value = [
|
|
14
|
+
0,
|
|
15
|
+
0,
|
|
16
|
+
0
|
|
17
|
+
], this.roots = /* @__PURE__ */ new Map(), this.listeners = /* @__PURE__ */ new Set(), !(e > 0)) throw RangeError("Invalid ratio; pass a number above 0");
|
|
18
|
+
this.ratio = e;
|
|
19
|
+
}
|
|
20
|
+
setup(e) {
|
|
21
|
+
this.engine = t(e);
|
|
22
|
+
}
|
|
23
|
+
get origin() {
|
|
24
|
+
return [...this.value];
|
|
25
|
+
}
|
|
26
|
+
get camera() {
|
|
27
|
+
if (!this.engine) throw e("state", "FloatingOrigin is not attached; call engine.use(origin) first");
|
|
28
|
+
return this.engine.camera;
|
|
29
|
+
}
|
|
30
|
+
place(e, t) {
|
|
31
|
+
let r = n(e);
|
|
32
|
+
if (r.check(), r.engine !== this.engine) throw TypeError("Foreign mesh; place meshes created by the engine this origin is attached to");
|
|
33
|
+
if (r.engine.store.parents[r.id] !== -1) throw TypeError("place() takes root meshes; place the root of the hierarchy, not a child");
|
|
34
|
+
let a = i(t, "Invalid world position"), o = this.roots.get(r) ?? /* @__PURE__ */ new Float64Array(6);
|
|
35
|
+
o.set(a), this.roots.set(r, o), this.write(r, o);
|
|
36
|
+
}
|
|
37
|
+
forget(e) {
|
|
38
|
+
this.roots.delete(n(e));
|
|
39
|
+
}
|
|
40
|
+
worldPosition(e) {
|
|
41
|
+
let t = n(e), r = this.roots.get(t);
|
|
42
|
+
if (r) return this.sync(t, r), [
|
|
43
|
+
r[0],
|
|
44
|
+
r[1],
|
|
45
|
+
r[2]
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
toWorld(e) {
|
|
49
|
+
let t = this.value;
|
|
50
|
+
return [
|
|
51
|
+
e[0] + t[0],
|
|
52
|
+
e[1] + t[1],
|
|
53
|
+
e[2] + t[2]
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
toLocal(e) {
|
|
57
|
+
let t = this.value;
|
|
58
|
+
return [
|
|
59
|
+
e[0] - t[0],
|
|
60
|
+
e[1] - t[1],
|
|
61
|
+
e[2] - t[2]
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
toLocalPlane(e) {
|
|
65
|
+
let t = this.value;
|
|
66
|
+
return [
|
|
67
|
+
e[0],
|
|
68
|
+
e[1],
|
|
69
|
+
e[2],
|
|
70
|
+
e[3] + e[0] * t[0] + e[1] * t[1] + e[2] * t[2]
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
lookAt(e, t) {
|
|
74
|
+
let n = i(e, "Invalid camera"), r = i(t, "Invalid camera"), a = this.camera;
|
|
75
|
+
a.cancelFit(), a.position = this.toLocal(n), a.target = this.toLocal(r), this.check();
|
|
76
|
+
}
|
|
77
|
+
get cameraPosition() {
|
|
78
|
+
return this.toWorld(this.camera.position);
|
|
79
|
+
}
|
|
80
|
+
get cameraTarget() {
|
|
81
|
+
return this.toWorld(this.camera.target);
|
|
82
|
+
}
|
|
83
|
+
onRebase(e) {
|
|
84
|
+
return this.listeners.add(e), () => {
|
|
85
|
+
this.listeners.delete(e);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
rebase(e = this.cameraPosition) {
|
|
89
|
+
let t = i(e, "Invalid origin"), n = this.value, r = [
|
|
90
|
+
t[0] - n[0],
|
|
91
|
+
t[1] - n[1],
|
|
92
|
+
t[2] - n[2]
|
|
93
|
+
], a = this.camera;
|
|
94
|
+
this.value = t;
|
|
95
|
+
let o = (e) => [
|
|
96
|
+
e[0] - r[0],
|
|
97
|
+
e[1] - r[1],
|
|
98
|
+
e[2] - r[2]
|
|
99
|
+
];
|
|
100
|
+
a.position = o(a.position), a.target = o(a.target);
|
|
101
|
+
let s = Object.values(a).find((e) => e && typeof e == "object" && "fromTarget" in e);
|
|
102
|
+
if (s) for (let e of [
|
|
103
|
+
"position",
|
|
104
|
+
"target",
|
|
105
|
+
"from",
|
|
106
|
+
"fromTarget"
|
|
107
|
+
]) s[e] = o(s[e]);
|
|
108
|
+
for (let [e, t] of this.roots) e.engine.meshes.get(e.id) === e ? (this.sync(e, t, n), this.write(e, t)) : this.roots.delete(e);
|
|
109
|
+
this.rebases++;
|
|
110
|
+
for (let e of this.listeners) e(r, this.origin);
|
|
111
|
+
}
|
|
112
|
+
onPreUpdate() {
|
|
113
|
+
this.engine && this.check();
|
|
114
|
+
}
|
|
115
|
+
dispose() {
|
|
116
|
+
this.engine = void 0, this.roots.clear(), this.listeners.clear();
|
|
117
|
+
}
|
|
118
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
119
|
+
this.dispose();
|
|
120
|
+
}
|
|
121
|
+
check() {
|
|
122
|
+
let e = this.camera, t = e.position, n = e.target;
|
|
123
|
+
Math.hypot(t[0], t[1], t[2]) > this.ratio * Math.hypot(t[0] - n[0], t[1] - n[1], t[2] - n[2]) && this.rebase();
|
|
124
|
+
}
|
|
125
|
+
write(e, t) {
|
|
126
|
+
let n = e.engine.store.transforms, r = this.value;
|
|
127
|
+
n.x[e.id] = t[3] = Math.fround(t[0] - r[0]), n.y[e.id] = t[4] = Math.fround(t[1] - r[1]), n.z[e.id] = t[5] = Math.fround(t[2] - r[2]);
|
|
128
|
+
}
|
|
129
|
+
sync(e, t, n = this.value) {
|
|
130
|
+
let r = e.engine.store.transforms, i = r.x[e.id], a = r.y[e.id], o = r.z[e.id];
|
|
131
|
+
(i !== t[3] || a !== t[4] || o !== t[5]) && (t[0] = i + n[0], t[1] = a + n[1], t[2] = o + n[2], t[3] = i, t[4] = a, t[5] = o);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
function i(e, t) {
|
|
135
|
+
if (!e || e.length !== 3 || ![
|
|
136
|
+
0,
|
|
137
|
+
1,
|
|
138
|
+
2
|
|
139
|
+
].every((t) => typeof e[t] == "number" && Number.isFinite(e[t]))) throw RangeError(t);
|
|
140
|
+
return [
|
|
141
|
+
e[0],
|
|
142
|
+
e[1],
|
|
143
|
+
e[2]
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
export { r as FloatingOrigin };
|
package/dist/panini.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import { type WideAngleMode } from './wide-laws.js';
|
|
5
|
+
export type { WideAngleMode } from './wide-laws.js';
|
|
6
|
+
/** Options of {@link WideAngleMap} and {@link WideAngle}. */
|
|
7
|
+
export interface WideAngleOptions {
|
|
8
|
+
/** Default 'panini'. The fisheye modes are r = θ (equidistant), 2 sin(θ/2) (equisolid), 2 tan(θ/2) (stereographic) and sin θ (orthographic). */
|
|
9
|
+
mode?: WideAngleMode;
|
|
10
|
+
/** Panini compression d: 0 is the plain rectilinear picture, 1 the classic Panini, larger bends verticals toward a cylinder. 0 or more, default 1. */
|
|
11
|
+
distance?: number;
|
|
12
|
+
/** Zoom in just enough that no output pixel falls outside the source picture. Default true; false leaves black where the source ends. */
|
|
13
|
+
crop?: boolean;
|
|
14
|
+
/** False passes the picture through. Default true. Also `effect.enabled`. */
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The mapping between the wide picture a camera draws (rectilinear, `fov` degrees vertical) and the projected output,
|
|
19
|
+
* both as normalised device coordinates (x right and y up, -1 to 1). The same maths runs in the shader; use it to turn a
|
|
20
|
+
* pointer position on the output into a position on the source for `engine.pick`, or to place overlays.
|
|
21
|
+
*/
|
|
22
|
+
export declare class WideAngleMap {
|
|
23
|
+
readonly mode: WideAngleMode;
|
|
24
|
+
readonly distance: number;
|
|
25
|
+
/** Tangent of the half angle across and up the source picture. */
|
|
26
|
+
readonly tx: number;
|
|
27
|
+
readonly ty: number;
|
|
28
|
+
/** Half extent of the output plane before `zoom`, across and up. */
|
|
29
|
+
readonly halfWidth: number;
|
|
30
|
+
readonly halfHeight: number;
|
|
31
|
+
/** Factor the output plane is shrunk by so it stays inside the source (1 when `crop` is false or nothing is outside). */
|
|
32
|
+
readonly zoom: number;
|
|
33
|
+
constructor(fov: number, aspect: number, { mode, distance, crop }?: WideAngleOptions);
|
|
34
|
+
/** Source position (normalised, x right and y up) shown at output position (`u`, `v`), or null where the output shows nothing. */
|
|
35
|
+
toSource(u: number, v: number): [number, number] | null;
|
|
36
|
+
/** Output position of the source position (`x`, `y`), or null when it falls outside the output. */
|
|
37
|
+
toOutput(x: number, y: number): [number, number] | null;
|
|
38
|
+
private sourceOf;
|
|
39
|
+
private fitZoom;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Panini and fisheye projection as a stage of `PostStack`, after the display effect: draw the scene with a wide
|
|
43
|
+
* perspective camera (for example `camera.fov = 100`) and this resamples it. It reads `engine.camera.fov` and the
|
|
44
|
+
* frame's shape each frame, so a camera with a lens (`CameraProjection.focalLength`) works unchanged. Perspective
|
|
45
|
+
* cameras only. The source is one rectilinear picture, so a fisheye reaches only as far as that picture does (under
|
|
46
|
+
* 180 degrees across), and the corners are cropped unless `crop` is false. `WideAngleCube` in
|
|
47
|
+
* `@margoengine/cameras/wide-cube` reads a cube capture and reaches past that.
|
|
48
|
+
* @throws RangeError for an unknown `mode`, a negative `distance` or a field of view outside 0 to 180; MargoError
|
|
49
|
+
* `'state'` outside a PostStack, `'unsupported'` on a WebGL engine, `'attached'` when joined twice, `'disposed'`
|
|
50
|
+
* after `dispose()`.
|
|
51
|
+
*/
|
|
52
|
+
export declare class WideAngle implements EnginePlugin {
|
|
53
|
+
readonly options: WideAngleOptions;
|
|
54
|
+
readonly name = "wide-angle";
|
|
55
|
+
private engine?;
|
|
56
|
+
private stack?;
|
|
57
|
+
private stacked;
|
|
58
|
+
private disposed;
|
|
59
|
+
private uniform?;
|
|
60
|
+
private sampler?;
|
|
61
|
+
private pipeline?;
|
|
62
|
+
private key;
|
|
63
|
+
private group?;
|
|
64
|
+
private input?;
|
|
65
|
+
private mapKey;
|
|
66
|
+
private lastMap?;
|
|
67
|
+
private readonly values;
|
|
68
|
+
private readonly attachment;
|
|
69
|
+
private readonly descriptor;
|
|
70
|
+
constructor(options?: WideAngleOptions);
|
|
71
|
+
/**
|
|
72
|
+
* Adds a new WideAngle to the scene's PostStack after its last LDR effect, or to a new PostStack when the scene has
|
|
73
|
+
* none (needs `@margoengine/post`). Disposing it takes it out, and a stack made for it goes with it.
|
|
74
|
+
*/
|
|
75
|
+
static attach(engine: EngineKernel, options?: WideAngleOptions): Promise<WideAngle>;
|
|
76
|
+
/** Whether the picture is projected (mirrors `options.enabled`). */
|
|
77
|
+
get enabled(): boolean;
|
|
78
|
+
set enabled(value: boolean);
|
|
79
|
+
/** The mapping for a camera of `fov` degrees (vertical) and a view of `aspect`, with these options. */
|
|
80
|
+
map(fov: number, aspect: number): WideAngleMap;
|
|
81
|
+
private joinStack;
|
|
82
|
+
setup(engine: Engine): void;
|
|
83
|
+
onDeviceRestored(engine: Engine): void;
|
|
84
|
+
/** Removes the effect and releases its GPU resources. Safe to call twice. */
|
|
85
|
+
dispose(): void;
|
|
86
|
+
[Symbol.dispose]: () => void;
|
|
87
|
+
private draw;
|
|
88
|
+
private pipelineFor;
|
|
89
|
+
private release;
|
|
90
|
+
}
|