@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +675 -2
- package/dist/clearance.d.ts +92 -0
- package/dist/clearance.js +138 -0
- 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/shots.d.ts +41 -0
- package/dist/shots.js +31 -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 +105 -4
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Settings of a {@link PhysicalCamera}. The defaults match `DOF` (50 mm, f/2.8, focus at 5) and `MotionBlur` (180 degrees at 60 fps). */
|
|
2
|
+
export interface PhysicalOptions {
|
|
3
|
+
/** Lens focal length in millimetres. Default 50. */
|
|
4
|
+
focalLength?: number;
|
|
5
|
+
/** F-number N (the aperture diameter is focalLength / N). Default 2.8. */
|
|
6
|
+
aperture?: number;
|
|
7
|
+
/** Exposure time in seconds. Default 1/120, a 180 degree shutter at 60 fps. */
|
|
8
|
+
shutter?: number;
|
|
9
|
+
/** Sensor sensitivity. Default 100. */
|
|
10
|
+
iso?: number;
|
|
11
|
+
/** Distance to the plane of focus in scene units (metres). Default 5. */
|
|
12
|
+
focusDistance?: number;
|
|
13
|
+
/** Sensor size in millimetres along its long side. Default 36. */
|
|
14
|
+
filmGauge?: number;
|
|
15
|
+
/** Frames per second, to turn the shutter time into a share of the frame interval. Default 60. */
|
|
16
|
+
frameRate?: number;
|
|
17
|
+
/** The exposure value (EV at ISO 100) that renders as 0 stops. Default: that of the default settings, so an unchanged scene looks as before. */
|
|
18
|
+
referenceEV?: number;
|
|
19
|
+
}
|
|
20
|
+
/** What `depthOfField` returns: distances in scene units; `far` is Infinity when the focus is at or beyond the hyperfocal distance. */
|
|
21
|
+
export interface DepthOfField {
|
|
22
|
+
hyperfocal: number;
|
|
23
|
+
near: number;
|
|
24
|
+
far: number;
|
|
25
|
+
}
|
|
26
|
+
/** Things `PhysicalCamera.apply` writes to. Each is optional and only touched when given. */
|
|
27
|
+
export interface PhysicalTargets {
|
|
28
|
+
/** A `DOF` effect: gets `focalDistance`, `focalLength` (metres) and `aperture`. */
|
|
29
|
+
dof?: {
|
|
30
|
+
options: {
|
|
31
|
+
focalDistance?: number;
|
|
32
|
+
focalLength?: number;
|
|
33
|
+
aperture?: number;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/** A `MotionBlur` effect: gets `shutter`, the share of the frame interval. */
|
|
37
|
+
motionBlur?: {
|
|
38
|
+
options: {
|
|
39
|
+
shutter?: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
/** Anything with an `exposure` option in stops: `Tonemap`, `Bloom`, `PostStack`. */
|
|
43
|
+
exposure?: {
|
|
44
|
+
options: {
|
|
45
|
+
exposure?: number;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/** A core camera: gets `fov` for `aspect`. */
|
|
49
|
+
camera?: {
|
|
50
|
+
fov: number;
|
|
51
|
+
};
|
|
52
|
+
/** A `CameraProjection`: gets `focalLength` and `filmGauge`, so it also follows the lens through `fit`. */
|
|
53
|
+
projection?: {
|
|
54
|
+
focalLength?: number;
|
|
55
|
+
filmGauge?: number;
|
|
56
|
+
};
|
|
57
|
+
/** Width / height of the view, for the field of view. Default 16 / 9. */
|
|
58
|
+
aspect?: number;
|
|
59
|
+
}
|
|
60
|
+
/** Exposure value at ISO 100 of an f-number, exposure time and ISO: log2(N² / t) − log2(ISO / 100). A stop is 1. */
|
|
61
|
+
export declare function ev100(aperture: number, shutter: number, iso?: number): number;
|
|
62
|
+
/**
|
|
63
|
+
* A camera described the way photographers do: focal length, aperture, shutter time and ISO. The three drive one
|
|
64
|
+
* picture together: the exposure, the depth of field and the motion blur all follow from the same numbers. `apply`
|
|
65
|
+
* writes them to the effects you use; the getters give the numbers for your own.
|
|
66
|
+
*/
|
|
67
|
+
export declare class PhysicalCamera implements Required<PhysicalOptions> {
|
|
68
|
+
focalLength: number;
|
|
69
|
+
aperture: number;
|
|
70
|
+
shutter: number;
|
|
71
|
+
iso: number;
|
|
72
|
+
focusDistance: number;
|
|
73
|
+
filmGauge: number;
|
|
74
|
+
frameRate: number;
|
|
75
|
+
referenceEV: number;
|
|
76
|
+
constructor(options?: PhysicalOptions);
|
|
77
|
+
/** Exposure value at ISO 100: lower is brighter. */
|
|
78
|
+
get ev100(): number;
|
|
79
|
+
/** Exposure in stops relative to `referenceEV`: a stop wider aperture, a doubled shutter time or a doubled ISO each add 1. */
|
|
80
|
+
get exposure(): number;
|
|
81
|
+
/** `2 ** exposure`: the factor to multiply scene light by. */
|
|
82
|
+
get exposureScale(): number;
|
|
83
|
+
/** The shutter time as a share of the frame interval, 0 to 1 (`MotionBlur.shutter`); a longer exposure than a frame is a full frame. */
|
|
84
|
+
get shutterFraction(): number;
|
|
85
|
+
/** The shutter as the angle of a rotary shutter, 0 to 360 degrees. */
|
|
86
|
+
get shutterAngle(): number;
|
|
87
|
+
/** Vertical field of view in degrees for a view of `aspect` (width / height). */
|
|
88
|
+
fov(aspect: number): number;
|
|
89
|
+
/**
|
|
90
|
+
* The range that is acceptably sharp, for a circle of confusion `coc` mm on the sensor (default `filmGauge / 1500`,
|
|
91
|
+
* 0.024 mm on 36 mm): the hyperfocal distance, and the nearest and farthest sharp distances for the focus distance.
|
|
92
|
+
*/
|
|
93
|
+
depthOfField(coc?: number): DepthOfField;
|
|
94
|
+
/** Diameter in millimetres of the blur disc on the sensor for a point at `distance`; 0 at the focus distance. */
|
|
95
|
+
blurDiameter(distance: number): number;
|
|
96
|
+
/** Writes the camera to the effects and camera given; the same numbers reach each. */
|
|
97
|
+
apply({ dof, motionBlur, exposure, camera, projection, aspect }: PhysicalTargets): void;
|
|
98
|
+
private check;
|
|
99
|
+
}
|
package/dist/physical.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { r as e } from "./src-_OQ7cR69.js";
|
|
2
|
+
//#region src/physical.ts
|
|
3
|
+
var t = {
|
|
4
|
+
focalLength: 50,
|
|
5
|
+
aperture: 2.8,
|
|
6
|
+
shutter: 1 / 120,
|
|
7
|
+
iso: 100,
|
|
8
|
+
focusDistance: 5,
|
|
9
|
+
filmGauge: 36,
|
|
10
|
+
frameRate: 60
|
|
11
|
+
};
|
|
12
|
+
function n(e, t, n = 100) {
|
|
13
|
+
if (![
|
|
14
|
+
e,
|
|
15
|
+
t,
|
|
16
|
+
n
|
|
17
|
+
].every((e) => e > 0 && e < Infinity)) throw RangeError("Invalid physical camera; pass an aperture, shutter time and ISO that are finite and above 0");
|
|
18
|
+
return Math.log2(e * e / t) - Math.log2(n / 100);
|
|
19
|
+
}
|
|
20
|
+
var r = class {
|
|
21
|
+
constructor(e = {}) {
|
|
22
|
+
let r = {
|
|
23
|
+
...t,
|
|
24
|
+
...e
|
|
25
|
+
};
|
|
26
|
+
this.focalLength = r.focalLength, this.aperture = r.aperture, this.shutter = r.shutter, this.iso = r.iso, this.focusDistance = r.focusDistance, this.filmGauge = r.filmGauge, this.frameRate = r.frameRate, this.referenceEV = e.referenceEV ?? n(t.aperture, t.shutter, t.iso), this.check();
|
|
27
|
+
}
|
|
28
|
+
get ev100() {
|
|
29
|
+
return this.check(), n(this.aperture, this.shutter, this.iso);
|
|
30
|
+
}
|
|
31
|
+
get exposure() {
|
|
32
|
+
return this.referenceEV - this.ev100;
|
|
33
|
+
}
|
|
34
|
+
get exposureScale() {
|
|
35
|
+
return 2 ** this.exposure;
|
|
36
|
+
}
|
|
37
|
+
get shutterFraction() {
|
|
38
|
+
return this.check(), Math.min(1, this.shutter * this.frameRate);
|
|
39
|
+
}
|
|
40
|
+
get shutterAngle() {
|
|
41
|
+
return this.shutterFraction * 360;
|
|
42
|
+
}
|
|
43
|
+
fov(t) {
|
|
44
|
+
return this.check(), e(this.focalLength, t, { filmGauge: this.filmGauge });
|
|
45
|
+
}
|
|
46
|
+
depthOfField(e = this.filmGauge / 1500) {
|
|
47
|
+
if (this.check(), !(e > 0 && e < Infinity)) throw RangeError("Invalid physical camera; pass a circle of confusion above 0");
|
|
48
|
+
let t = this.focalLength / 1e3, n = e / 1e3, r = this.focusDistance, i = t * t / (this.aperture * n) + t;
|
|
49
|
+
return {
|
|
50
|
+
hyperfocal: i,
|
|
51
|
+
near: r * (i - t) / (i + r - 2 * t),
|
|
52
|
+
far: r >= i ? Infinity : r * (i - t) / (i - r)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
blurDiameter(e) {
|
|
56
|
+
if (this.check(), !(e > 0)) throw RangeError("Invalid physical camera; pass a distance above 0");
|
|
57
|
+
let t = this.focalLength / 1e3, n = this.focusDistance;
|
|
58
|
+
return t * t / (this.aperture * (n - t)) * (Math.abs(e - n) / e) * 1e3;
|
|
59
|
+
}
|
|
60
|
+
apply({ dof: e, motionBlur: t, exposure: n, camera: r, projection: i, aspect: a = 16 / 9 }) {
|
|
61
|
+
this.check(), e && (e.options.focalDistance = this.focusDistance, e.options.focalLength = this.focalLength / 1e3, e.options.aperture = this.aperture), t && (t.options.shutter = this.shutterFraction), n && (n.options.exposure = this.exposure), r && (r.fov = this.fov(a)), i && (i.focalLength = this.focalLength, i.filmGauge = this.filmGauge);
|
|
62
|
+
}
|
|
63
|
+
check() {
|
|
64
|
+
if (![
|
|
65
|
+
this.focalLength,
|
|
66
|
+
this.aperture,
|
|
67
|
+
this.shutter,
|
|
68
|
+
this.iso,
|
|
69
|
+
this.focusDistance,
|
|
70
|
+
this.filmGauge,
|
|
71
|
+
this.frameRate
|
|
72
|
+
].every((e) => e > 0 && e < Infinity) || !Number.isFinite(this.referenceEV)) throw RangeError("Invalid physical camera; focal length, aperture, shutter, ISO, focus distance, film gauge and frame rate must be finite and above 0");
|
|
73
|
+
if (this.focusDistance <= this.focalLength / 1e3) throw RangeError("Invalid physical camera; the focus distance must be beyond the focal length");
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
//#endregion
|
|
77
|
+
export { r as PhysicalCamera, n as ev100 };
|
package/dist/shots.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Camera, Vec3 } from '@margoengine/core/extension';
|
|
2
|
+
/** One framed view: the eye sits at `target + offset * max(1, fit * height / width)`. */
|
|
3
|
+
export interface Shot {
|
|
4
|
+
/** Point the camera looks at, in world units. Read on every update, so it may be changed in place. */
|
|
5
|
+
target: Vec3;
|
|
6
|
+
/** Eye position relative to `target` for a landscape view. */
|
|
7
|
+
offset: Vec3;
|
|
8
|
+
/** How much a portrait view backs away: the offset is scaled by max(1, fit * height / width). Default 1. */
|
|
9
|
+
fit?: number;
|
|
10
|
+
/** Vertical field of view in degrees. Set on the camera only when both shots give one. */
|
|
11
|
+
fov?: number;
|
|
12
|
+
}
|
|
13
|
+
/** Options for {@link ShotBlend}. */
|
|
14
|
+
export interface ShotBlendOptions {
|
|
15
|
+
/** Exponential easing rate per second; 15 settles to 0.001 in about 0.46 s. Default 15. */
|
|
16
|
+
rate?: number;
|
|
17
|
+
/** Jumps straight to the goal while `matches` is true, as for `matchMedia('(prefers-reduced-motion: reduce)')`. */
|
|
18
|
+
still?: {
|
|
19
|
+
readonly matches: boolean;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Frames a camera at, or on the way between, two {@link Shot}s. */
|
|
23
|
+
export declare class ShotBlend {
|
|
24
|
+
#private;
|
|
25
|
+
/** Shot at blend 0. */
|
|
26
|
+
readonly from: Shot;
|
|
27
|
+
/** Shot at blend 1. */
|
|
28
|
+
readonly to: Shot;
|
|
29
|
+
/** Where the blend is heading: 0 for `from`, 1 for `to`, or any value between. Default 0. */
|
|
30
|
+
goal: number;
|
|
31
|
+
/** The current blend, which eases toward {@link goal} on each update. Starts at 0. */
|
|
32
|
+
blend: number;
|
|
33
|
+
constructor(camera: Camera, from: Shot, to: Shot, { rate, still }?: ShotBlendOptions);
|
|
34
|
+
/** True while the blend has not reached its goal. */
|
|
35
|
+
get moving(): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Eases the blend toward the goal over `delta` seconds and frames the camera for a `width` x `height` view. Returns
|
|
38
|
+
* true while the blend is still moving, so a page that renders on demand knows to draw another frame.
|
|
39
|
+
*/
|
|
40
|
+
update(delta: number, width: number, height: number): boolean;
|
|
41
|
+
}
|
package/dist/shots.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { n as e } from "./errors-Cp7bu2gW.js";
|
|
2
|
+
import { lerp as t } from "@margoengine/math/mathutils";
|
|
3
|
+
//#region src/shots.ts
|
|
4
|
+
var n = class {
|
|
5
|
+
#e;
|
|
6
|
+
#t;
|
|
7
|
+
#n;
|
|
8
|
+
constructor(t, n, r, { rate: i = 15, still: a } = {}) {
|
|
9
|
+
if (this.goal = 0, this.blend = 0, !(i > 0) || !Number.isFinite(i)) throw e("ShotBlend rate must be a finite number above 0", "cameras");
|
|
10
|
+
this.#e = t, this.from = n, this.to = r, this.#t = i, this.#n = a;
|
|
11
|
+
}
|
|
12
|
+
get moving() {
|
|
13
|
+
return this.blend !== this.goal;
|
|
14
|
+
}
|
|
15
|
+
update(n, r, i) {
|
|
16
|
+
if (!(n >= 0) || !Number.isFinite(n)) throw e("ShotBlend delta must be a finite number of seconds >= 0", "cameras");
|
|
17
|
+
if (!(r > 0 && i > 0)) throw e("ShotBlend width and height must be above 0", "cameras");
|
|
18
|
+
let { from: a, to: o, goal: s } = this, c = this.blend = this.#n?.matches || Math.abs(this.blend - s) < .001 ? s : t(s, this.blend, Math.exp(-n * this.#t)), l = Math.max(1, t(a.fit ?? 1, o.fit ?? 1, c) * i / r), u = (e) => t(a.target[e], o.target[e], c), d = [
|
|
19
|
+
u(0),
|
|
20
|
+
u(1),
|
|
21
|
+
u(2)
|
|
22
|
+
], f = (e) => d[e] + l * t(a.offset[e], o.offset[e], c);
|
|
23
|
+
return this.#e.target = d, this.#e.position = [
|
|
24
|
+
f(0),
|
|
25
|
+
f(1),
|
|
26
|
+
f(2)
|
|
27
|
+
], a.fov !== void 0 && o.fov !== void 0 && (this.#e.fov = t(a.fov, o.fov, c)), this.moving;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { n as ShotBlend };
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e, t } from "./errors-Cp7bu2gW.js";
|
|
5
|
+
//#region src/fit-box.ts
|
|
6
|
+
var n = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = (e, t) => [
|
|
7
|
+
e[1] * t[2] - e[2] * t[1],
|
|
8
|
+
e[2] * t[0] - e[0] * t[2],
|
|
9
|
+
e[0] * t[1] - e[1] * t[0]
|
|
10
|
+
], i = (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
|
+
};
|
|
18
|
+
function a(e, t, r, i) {
|
|
19
|
+
let a = [], o = [];
|
|
20
|
+
for (let s of [t[0], t[3]]) for (let c of [t[1], t[4]]) for (let l of [t[2], t[5]]) a.push(n(e, [
|
|
21
|
+
s,
|
|
22
|
+
c,
|
|
23
|
+
l
|
|
24
|
+
])), o.push(n(r, [
|
|
25
|
+
s,
|
|
26
|
+
c,
|
|
27
|
+
l
|
|
28
|
+
]) - i);
|
|
29
|
+
let s = 0, c = Infinity;
|
|
30
|
+
for (let e = 0; e < 8; e++) for (let t = 0; t < 8; t++) {
|
|
31
|
+
let n = (a[e] * o[t] + a[t] * o[e]) / (o[e] + o[t]), r = -Infinity, i = -Infinity;
|
|
32
|
+
for (let e = 0; e < 8; e++) r = Math.max(r, (a[e] - n) / o[e]), i = Math.max(i, (n - a[e]) / o[e]);
|
|
33
|
+
Math.abs(r - i) < c && (c = Math.abs(r - i), s = n);
|
|
34
|
+
}
|
|
35
|
+
return s;
|
|
36
|
+
}
|
|
37
|
+
function o(t, o, { aspect: s = 1, padding: c = 1.1 } = {}) {
|
|
38
|
+
let l = Array.from(o), u = t.up ?? [
|
|
39
|
+
0,
|
|
40
|
+
1,
|
|
41
|
+
0
|
|
42
|
+
], d = [
|
|
43
|
+
...l,
|
|
44
|
+
t.fov,
|
|
45
|
+
s,
|
|
46
|
+
c,
|
|
47
|
+
...Array.from(t.position),
|
|
48
|
+
...Array.from(t.target),
|
|
49
|
+
...Array.from(u)
|
|
50
|
+
];
|
|
51
|
+
if (l.length !== 6 || t.position.length !== 3 || t.target.length !== 3 || u.length !== 3 || !d.every(Number.isFinite) || l[0] > l[3] || l[1] > l[4] || l[2] > l[5] || !(t.fov > 0 && t.fov < 180) || !(s > 0) || !(c >= 1)) throw e("Invalid fitBox; pass bounds [minX, minY, minZ, maxX, maxY, maxZ] with min <= max, fov between 0 and 180, aspect > 0 and padding >= 1, all finite", "camera");
|
|
52
|
+
if (l[0] === l[3] && l[1] === l[4] && l[2] === l[5]) throw e("Invalid fitBox; the bounds are a single point, pass a box with extent", "camera");
|
|
53
|
+
let f = [
|
|
54
|
+
t.target[0] - t.position[0],
|
|
55
|
+
t.target[1] - t.position[1],
|
|
56
|
+
t.target[2] - t.position[2]
|
|
57
|
+
], p = r(f, u);
|
|
58
|
+
if (!(Math.hypot(...f) > 0) || !(Math.hypot(...p) > 1e-12 * Math.hypot(...f) * Math.hypot(u[0], u[1], u[2]))) throw e("Invalid fitBox view; position and target must differ and the view must not be parallel to up", "camera");
|
|
59
|
+
let m = i(f), h = i(p), g = r(h, m), _ = Math.tan(t.fov * Math.PI / 360) / c, v = _ * s, y = -Infinity, b = -Infinity, x = -Infinity, S = -Infinity;
|
|
60
|
+
for (let e of [l[0], l[3]]) for (let t of [l[1], l[4]]) for (let r of [l[2], l[5]]) {
|
|
61
|
+
let i = [
|
|
62
|
+
e,
|
|
63
|
+
t,
|
|
64
|
+
r
|
|
65
|
+
], a = n(h, i), o = n(g, i), s = n(m, i);
|
|
66
|
+
b = Math.max(b, a - v * s), y = Math.max(y, -a - v * s), S = Math.max(S, o - _ * s), x = Math.max(x, -o - _ * s);
|
|
67
|
+
}
|
|
68
|
+
let C = Math.min(-(b + y) / (2 * v), -(S + x) / (2 * _)), w = a(h, l, m, C), T = a(g, l, m, C), E = n(m, [
|
|
69
|
+
(l[0] + l[3]) / 2,
|
|
70
|
+
(l[1] + l[4]) / 2,
|
|
71
|
+
(l[2] + l[5]) / 2
|
|
72
|
+
]) - C, D = [
|
|
73
|
+
0,
|
|
74
|
+
1,
|
|
75
|
+
2
|
|
76
|
+
].map((e) => h[e] * w + g[e] * T + m[e] * C), O = D.map((e, t) => e + m[t] * E), k = Infinity;
|
|
77
|
+
for (let e of [l[0], l[3]]) for (let t of [l[1], l[4]]) for (let r of [l[2], l[5]]) k = Math.min(k, n(m, [
|
|
78
|
+
e,
|
|
79
|
+
t,
|
|
80
|
+
r
|
|
81
|
+
]) - C);
|
|
82
|
+
return {
|
|
83
|
+
position: D,
|
|
84
|
+
target: O,
|
|
85
|
+
distance: E,
|
|
86
|
+
nearest: k
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/index.ts
|
|
91
|
+
var s = (e, t, n, r) => {
|
|
92
|
+
if (![
|
|
93
|
+
e,
|
|
94
|
+
t,
|
|
95
|
+
n
|
|
96
|
+
].every(Number.isFinite) || e <= 0 || t <= 0 || n <= 0 || ![
|
|
97
|
+
"horizontal",
|
|
98
|
+
"vertical",
|
|
99
|
+
"auto"
|
|
100
|
+
].includes(r)) throw RangeError("Invalid projection lens; pass finite focal length or fov, aspect and film gauge above 0 and sensorFit 'horizontal', 'vertical' or 'auto'");
|
|
101
|
+
}, c = (e, t) => t === "horizontal" || t === "auto" && e >= 1;
|
|
102
|
+
function l(e, t, { filmGauge: n = 36, sensorFit: r = "auto" } = {}) {
|
|
103
|
+
s(e, t, n, r);
|
|
104
|
+
let i = n / (2 * e);
|
|
105
|
+
return 360 / Math.PI * Math.atan(c(t, r) ? i / t : i);
|
|
106
|
+
}
|
|
107
|
+
function u(e, t, { filmGauge: n = 36, sensorFit: r = "auto" } = {}) {
|
|
108
|
+
if (s(e, t, n, r), e >= 180) throw RangeError("Invalid projection lens; pass a field of view below 180 degrees");
|
|
109
|
+
let i = Math.tan(e * Math.PI / 360);
|
|
110
|
+
return n / (2 * (c(t, r) ? i * t : i));
|
|
111
|
+
}
|
|
112
|
+
function d(e, t, n, r = n) {
|
|
113
|
+
if (![
|
|
114
|
+
e,
|
|
115
|
+
t,
|
|
116
|
+
n,
|
|
117
|
+
r
|
|
118
|
+
].every((e) => Number.isInteger(e) && e > 0)) throw RangeError("Invalid projection tiles; pass whole numbers above 0 for the full size and the tile size");
|
|
119
|
+
let i = [];
|
|
120
|
+
for (let a = 0; a < t; a += r) for (let o = 0; o < e; o += n) i.push({
|
|
121
|
+
fullWidth: e,
|
|
122
|
+
fullHeight: t,
|
|
123
|
+
x: o,
|
|
124
|
+
y: a,
|
|
125
|
+
width: Math.min(n, e - o),
|
|
126
|
+
height: Math.min(r, t - a)
|
|
127
|
+
});
|
|
128
|
+
return i;
|
|
129
|
+
}
|
|
130
|
+
var f = 1e-9, p = class n {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
131
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
132
|
+
let r = new n(t);
|
|
133
|
+
return e.use(r), r;
|
|
134
|
+
}
|
|
135
|
+
constructor(e = {}) {
|
|
136
|
+
this.name = "cameras", this.mode = "orthographic", this.zoom = 1, this.up = [
|
|
137
|
+
0,
|
|
138
|
+
0,
|
|
139
|
+
-1
|
|
140
|
+
], this.filmGauge = 36, this.sensorFit = "auto", this.worldUp = [
|
|
141
|
+
0,
|
|
142
|
+
1,
|
|
143
|
+
0
|
|
144
|
+
], Object.assign(this, e);
|
|
145
|
+
}
|
|
146
|
+
setup(e) {
|
|
147
|
+
this.attach(e.camera), this.canvas = e.canvas, e.sceneOf && (this.host = e);
|
|
148
|
+
}
|
|
149
|
+
fit(n, { aspect: r = this.canvas ? this.canvas.width / this.canvas.height : 1, duration: i = .3, padding: a = 1.1, framing: s = "sphere" } = {}) {
|
|
150
|
+
let c = this.camera;
|
|
151
|
+
if (!c) throw t("state", "Projection is not attached; call attach(camera) or engine.use(projection) first");
|
|
152
|
+
let l = Array.from(n), u = l.slice(0, 3), d = l.slice(3);
|
|
153
|
+
if (l.length !== 6 || !l.every(Number.isFinite) || u.some((e, t) => e > d[t]) || ![
|
|
154
|
+
r,
|
|
155
|
+
i,
|
|
156
|
+
a
|
|
157
|
+
].every(Number.isFinite) || r <= 0 || i < 0 || a < 1) throw RangeError("Invalid fit; pass min <= max, aspect > 0, duration >= 0 and padding >= 1, all finite");
|
|
158
|
+
if (s !== "sphere" && s !== "box") throw e("Invalid fit framing; use 'sphere' or 'box'", "camera");
|
|
159
|
+
if (this.animation = void 0, this.applyLens(r), this.mode === "perspective" && s === "box") {
|
|
160
|
+
c.cancelFit();
|
|
161
|
+
let e = o({
|
|
162
|
+
position: c.position,
|
|
163
|
+
target: c.target,
|
|
164
|
+
fov: this.vfov(c, r),
|
|
165
|
+
up: this.screenUp(c)
|
|
166
|
+
}, l, {
|
|
167
|
+
aspect: r,
|
|
168
|
+
padding: a
|
|
169
|
+
});
|
|
170
|
+
this.animation = {
|
|
171
|
+
from: c.position,
|
|
172
|
+
to: e.position,
|
|
173
|
+
fromTarget: c.target,
|
|
174
|
+
toTarget: e.target,
|
|
175
|
+
fromZoom: this.zoom,
|
|
176
|
+
toZoom: this.zoom,
|
|
177
|
+
elapsed: 0,
|
|
178
|
+
duration: i
|
|
179
|
+
}, this.onPreUpdate(0);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (this.mode === "perspective") {
|
|
183
|
+
c.fitToBoundingBox(u, d, {
|
|
184
|
+
aspect: r,
|
|
185
|
+
duration: i,
|
|
186
|
+
padding: a
|
|
187
|
+
});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
c.cancelFit();
|
|
191
|
+
let f = c.position, p = c.target, m = [
|
|
192
|
+
0,
|
|
193
|
+
1,
|
|
194
|
+
2
|
|
195
|
+
].map((e) => f[e] - p[e]), h = Math.hypot(...m), g = m.map((e) => -e / h), _ = this.screenUp(c), v = [
|
|
196
|
+
g[1] * _[2] - g[2] * _[1],
|
|
197
|
+
g[2] * _[0] - g[0] * _[2],
|
|
198
|
+
g[0] * _[1] - g[1] * _[0]
|
|
199
|
+
], y = Math.hypot(...v);
|
|
200
|
+
v = v.map((e) => e / y);
|
|
201
|
+
let b = [
|
|
202
|
+
v[1] * g[2] - v[2] * g[1],
|
|
203
|
+
v[2] * g[0] - v[0] * g[2],
|
|
204
|
+
v[0] * g[1] - v[1] * g[0]
|
|
205
|
+
], x = d.map((e, t) => (e - u[t]) / 2), S = d.map((e, t) => (e + u[t]) / 2), C = x.reduce((e, t, n) => e + Math.abs(v[n]) * t, 0), w = x.reduce((e, t, n) => e + Math.abs(b[n]) * t, 0), T = [
|
|
206
|
+
this.left,
|
|
207
|
+
this.right,
|
|
208
|
+
this.top,
|
|
209
|
+
this.bottom
|
|
210
|
+
], E, D = [0, 0];
|
|
211
|
+
if (T.every((e) => e !== void 0)) {
|
|
212
|
+
let [e, t, n, r] = T;
|
|
213
|
+
E = Math.min((t - e) / 2 / C, (n - r) / 2 / w) / a, D = [(t + e) / 2, (n + r) / 2];
|
|
214
|
+
} else E = (this.height ?? 2 * h * Math.tan(this.vfov(c, r) * Math.PI / 360)) / 2 / Math.max(w, C / r) / a;
|
|
215
|
+
if (!(E > 0 && E < Infinity) || !y) throw RangeError("Invalid fit; the bounds are empty or flat along the view, pass a box with volume");
|
|
216
|
+
if (this.near === void 0 && this.far === void 0 && Math.hypot(...x) >= c.far / 2) throw RangeError("Bounds exceed far; raise camera.far or set far on the projection");
|
|
217
|
+
let O = S.map((e, t) => e - v[t] * D[0] - b[t] * D[1]), k = O.map((e, t) => e + m[t]);
|
|
218
|
+
this.animation = {
|
|
219
|
+
from: f,
|
|
220
|
+
to: k,
|
|
221
|
+
fromTarget: p,
|
|
222
|
+
toTarget: O,
|
|
223
|
+
fromZoom: this.zoom,
|
|
224
|
+
toZoom: E,
|
|
225
|
+
elapsed: 0,
|
|
226
|
+
duration: i
|
|
227
|
+
}, this.onPreUpdate(0);
|
|
228
|
+
}
|
|
229
|
+
onPreUpdate(e) {
|
|
230
|
+
let t = this.animation, n = this.camera;
|
|
231
|
+
if (n && this.canvas && this.focalLength !== void 0) {
|
|
232
|
+
let e = this.viewOffset, t = e ? e.fullWidth / e.fullHeight : this.canvas.width / this.canvas.height;
|
|
233
|
+
t > 0 && t < Infinity && this.applyLens(t);
|
|
234
|
+
}
|
|
235
|
+
if (!t || !n) return;
|
|
236
|
+
let r = t.duration ? Math.min(1, (t.elapsed += e) / t.duration) : 1, i = r * r * (3 - 2 * r), a = (e, t) => e.map((e, n) => e + (t[n] - e) * i);
|
|
237
|
+
n.position = a(t.from, t.to), n.target = a(t.fromTarget, t.toTarget), this.zoom = t.fromZoom * (t.toZoom / t.fromZoom) ** i, r === 1 && (this.animation = void 0);
|
|
238
|
+
}
|
|
239
|
+
setViewOffset(e, t, n, r, i, a) {
|
|
240
|
+
this.viewOffset = {
|
|
241
|
+
fullWidth: e,
|
|
242
|
+
fullHeight: t,
|
|
243
|
+
x: n,
|
|
244
|
+
y: r,
|
|
245
|
+
width: i,
|
|
246
|
+
height: a
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
applyLens(e) {
|
|
250
|
+
let n = this.camera;
|
|
251
|
+
if (!n) throw t("state", "Projection is not attached; call attach(camera) or engine.use(projection) first");
|
|
252
|
+
return n.fov = this.vfov(n, e);
|
|
253
|
+
}
|
|
254
|
+
clearViewOffset() {
|
|
255
|
+
this.viewOffset = void 0;
|
|
256
|
+
}
|
|
257
|
+
attach(e) {
|
|
258
|
+
if (this.camera) throw t("attached", "Projection is already attached; dispose() it or create another");
|
|
259
|
+
if (e.project) throw t("attached", "Camera already has a projection; dispose that projection first");
|
|
260
|
+
this.camera = e, this.worldUp = e.up, Object.defineProperty(e, "up", {
|
|
261
|
+
configurable: !0,
|
|
262
|
+
enumerable: !0,
|
|
263
|
+
get: () => this.screenUp(e),
|
|
264
|
+
set: (e) => {
|
|
265
|
+
this.worldUp = e;
|
|
266
|
+
}
|
|
267
|
+
}), Object.defineProperty(e, "fixedOrthographic", {
|
|
268
|
+
configurable: !0,
|
|
269
|
+
get: () => this.fixedOrthographic
|
|
270
|
+
}), e.project = (t, n, r) => this.project(e, t, n, r);
|
|
271
|
+
}
|
|
272
|
+
dispose() {
|
|
273
|
+
let e = this.camera, t = this.host;
|
|
274
|
+
this.host = void 0, e && (this.camera = void 0, this.animation = void 0, Object.defineProperty(e, "up", {
|
|
275
|
+
configurable: !0,
|
|
276
|
+
enumerable: !0,
|
|
277
|
+
writable: !0,
|
|
278
|
+
value: this.worldUp
|
|
279
|
+
}), delete e.fixedOrthographic, e.project = void 0), t?.sceneOf(this)?.removePlugin(this.name);
|
|
280
|
+
}
|
|
281
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
282
|
+
this.dispose();
|
|
283
|
+
}
|
|
284
|
+
get fixedOrthographic() {
|
|
285
|
+
return this.mode === "orthographic" && (this.height !== void 0 || [
|
|
286
|
+
this.left,
|
|
287
|
+
this.right,
|
|
288
|
+
this.top,
|
|
289
|
+
this.bottom
|
|
290
|
+
].every((e) => e !== void 0));
|
|
291
|
+
}
|
|
292
|
+
screenUp(e) {
|
|
293
|
+
let t = e.position, n = e.target, r = t[0] - n[0], i = t[1] - n[1], a = t[2] - n[2], o = this.worldUp, s = (e) => Math.hypot(e[1] * a - e[2] * i, e[2] * r - e[0] * a, e[0] * i - e[1] * r), c = f * Math.hypot(r, i, a);
|
|
294
|
+
if (!o.every(Number.isFinite) || !(Math.hypot(...o) > 0)) throw RangeError("Invalid world up; set camera.up to a finite nonzero direction");
|
|
295
|
+
if (s(o) > c * Math.hypot(...o)) return o;
|
|
296
|
+
let l = this.up;
|
|
297
|
+
if (!l.every(Number.isFinite) || !(s(l) > c * Math.hypot(...l))) throw RangeError("Invalid vertical up; set up to a finite vector that is not parallel to the view axis");
|
|
298
|
+
return l;
|
|
299
|
+
}
|
|
300
|
+
vfov(e, t) {
|
|
301
|
+
return this.focalLength === void 0 ? e.fov : l(this.focalLength, t, {
|
|
302
|
+
filmGauge: this.filmGauge,
|
|
303
|
+
sensorFit: this.sensorFit
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
project(e, t, n, r) {
|
|
307
|
+
let i = this.viewOffset;
|
|
308
|
+
if (!i) return this.build(e, t, n, r);
|
|
309
|
+
if (![
|
|
310
|
+
i.fullWidth,
|
|
311
|
+
i.fullHeight,
|
|
312
|
+
i.x,
|
|
313
|
+
i.y,
|
|
314
|
+
i.width,
|
|
315
|
+
i.height
|
|
316
|
+
].every(Number.isFinite) || !(i.fullWidth > 0 && i.fullHeight > 0 && i.width > 0 && i.height > 0)) throw RangeError("Invalid projection view offset; pass finite numbers with fullWidth, fullHeight, width and height above 0");
|
|
317
|
+
this.build(e, t, i.fullWidth / i.fullHeight, r);
|
|
318
|
+
let a = i.fullWidth / i.width, o = i.fullHeight / i.height, s = (i.fullWidth - 2 * i.x - i.width) / i.width, c = (i.height - i.fullHeight + 2 * i.y) / i.height;
|
|
319
|
+
for (let e = 0; e < 4; e++) t[e * 4] = a * t[e * 4] + s * t[e * 4 + 3], t[e * 4 + 1] = o * t[e * 4 + 1] + c * t[e * 4 + 3];
|
|
320
|
+
}
|
|
321
|
+
build(e, t, n, r) {
|
|
322
|
+
let i = e.near, a = e.far;
|
|
323
|
+
if (this.mode === "perspective") {
|
|
324
|
+
let o = i - a, s = 1 / Math.tan(this.vfov(e, n) * Math.PI / 360);
|
|
325
|
+
t[0] = s / n, t[5] = s, t[10] = r ? -i / o : (a + i) / o, t[11] = -1, t[14] = r ? -a * i / o : 2 * a * i / o;
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (this.mode !== "orthographic") throw RangeError("Invalid projection mode; use 'perspective' or 'orthographic'");
|
|
329
|
+
let o = e.position, s = e.target, c = Math.hypot(o[0] - s[0], o[1] - s[1], o[2] - s[2]), l = [
|
|
330
|
+
this.left,
|
|
331
|
+
this.right,
|
|
332
|
+
this.top,
|
|
333
|
+
this.bottom
|
|
334
|
+
], u = (this.height ?? 2 * c * Math.tan(this.vfov(e, n) * Math.PI / 360)) / 2, [d, f, p, m] = l.every((e) => e !== void 0) ? l : [
|
|
335
|
+
-u * n,
|
|
336
|
+
u * n,
|
|
337
|
+
u,
|
|
338
|
+
-u
|
|
339
|
+
], h = this.near ?? c - a / 2, g = this.far ?? c + a / 2, _ = (f - d) / (2 * this.zoom), v = (p - m) / (2 * this.zoom), y = (f + d) / 2, b = (p + m) / 2;
|
|
340
|
+
if (d = y - _, f = y + _, p = b + v, m = b - v, ![
|
|
341
|
+
d,
|
|
342
|
+
f,
|
|
343
|
+
p,
|
|
344
|
+
m,
|
|
345
|
+
h,
|
|
346
|
+
g,
|
|
347
|
+
this.zoom
|
|
348
|
+
].every(Number.isFinite) || this.zoom <= 0 || f <= d || p <= m || g <= h) throw RangeError("Invalid projection; pass finite bounds with left < right, bottom < top and near < far");
|
|
349
|
+
t[0] = 2 / (f - d), t[5] = 2 / (p - m), t[10] = r ? 1 / (g - h) : -2 / (g - h), t[12] = -(f + d) / (f - d), t[13] = -(p + m) / (p - m), t[14] = r ? g / (g - h) : -(g + h) / (g - h), t[15] = 1;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
//#endregion
|
|
353
|
+
export { o as a, d as i, u as n, l as r, p as t };
|
package/dist/stereo.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { Camera, type RenderView } from '@margoengine/core/extension';
|
|
4
|
+
/** Non-XR stereo packing. Left eye is first unless swapEyes is set. */
|
|
5
|
+
export type StereoMode = 'side-by-side' | 'top-bottom' | 'anaglyph' | 'parallax-barrier';
|
|
6
|
+
/** Geometry of the two parallel eyes. Distances use the scene's world units (normally metres). */
|
|
7
|
+
export interface StereoRigOptions {
|
|
8
|
+
/** Interocular distance, >= 0. Default 0.064 m. */
|
|
9
|
+
eyeSeparation?: number;
|
|
10
|
+
/** Zero-parallax distance, > 0 or Infinity. Default distance to the camera target. */
|
|
11
|
+
focusDistance?: number;
|
|
12
|
+
}
|
|
13
|
+
/** Two off-axis cameras. No toe-in, vertical disparity or changes to the source camera's pose/projection. */
|
|
14
|
+
export declare class StereoRig {
|
|
15
|
+
/** Left eye camera, reusable in engine views. */
|
|
16
|
+
readonly left: Camera;
|
|
17
|
+
/** Right eye camera, reusable in engine views. */
|
|
18
|
+
readonly right: Camera;
|
|
19
|
+
/** Mutable eye geometry; validated on update. */
|
|
20
|
+
readonly options: StereoRigOptions;
|
|
21
|
+
constructor(options?: StereoRigOptions);
|
|
22
|
+
/** Refresh the eyes from a camera. Aspect may be read lazily after the engine has resized its canvas. */
|
|
23
|
+
update(source: Camera, aspect?: number | (() => number)): void;
|
|
24
|
+
}
|
|
25
|
+
/** Stereo renderer settings. Update options before render; the engine's normal scene passes run for each eye. */
|
|
26
|
+
export interface StereoOutputOptions extends StereoRigOptions {
|
|
27
|
+
/** Default 'side-by-side'. */
|
|
28
|
+
mode?: StereoMode;
|
|
29
|
+
/** Source camera. Default engine.camera. */
|
|
30
|
+
camera?: Camera;
|
|
31
|
+
/** Scene to render. Default engine.scene. */
|
|
32
|
+
scene?: RenderView['scene'];
|
|
33
|
+
/** Layer mask for both eyes. Default the source camera's mask. */
|
|
34
|
+
layers?: number;
|
|
35
|
+
/** Reverse the eyes for crossed viewing or different display wiring. Default false. */
|
|
36
|
+
swapEyes?: boolean;
|
|
37
|
+
/** Interleave columns (default) or rows for parallax-barrier output. Physical drawing-buffer pixels. */
|
|
38
|
+
barrierAxis?: 'columns' | 'rows';
|
|
39
|
+
/** Which stripe parity contains the left eye: 0 (default) or 1. */
|
|
40
|
+
barrierPhase?: 0 | 1;
|
|
41
|
+
}
|
|
42
|
+
/** Replaces the application's engine.render call. Two scene draws and one packing pass, with no XR session. */
|
|
43
|
+
export declare class StereoOutput {
|
|
44
|
+
private readonly engine;
|
|
45
|
+
/** Creates stereo output for the application's frame loop. Defaults match the constructor. */
|
|
46
|
+
static attach(engine: EngineKernel, options?: StereoOutputOptions): StereoOutput;
|
|
47
|
+
/** Mutable settings, validated on render. */
|
|
48
|
+
readonly options: StereoOutputOptions;
|
|
49
|
+
/** The eye cameras, also usable for picking or custom view layouts. */
|
|
50
|
+
readonly rig: StereoRig;
|
|
51
|
+
private state?;
|
|
52
|
+
private disposed;
|
|
53
|
+
constructor(engine: EngineKernel, options?: StereoOutputOptions);
|
|
54
|
+
/** Render once; animation/plugin updates happen once for the pair. Use from your application's frame loop. */
|
|
55
|
+
render(delta?: number): void;
|
|
56
|
+
private ensure;
|
|
57
|
+
private release;
|
|
58
|
+
/** Release eye textures and uniforms. Safe to call more than once. */
|
|
59
|
+
dispose(): void;
|
|
60
|
+
[Symbol.dispose]: () => void;
|
|
61
|
+
}
|