@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,353 @@
|
|
|
1
|
+
// @margo/core-built 0.1.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("0.1.0",import.meta.url);
|
|
131
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.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
|
+
}
|
package/dist/stereo.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const a=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e } from "./layers-CkdgcqTK.js";
|
|
5
|
+
import { n as t, r as n, t as r } from "./gpu-BRxrrZK3.js";
|
|
6
|
+
import { Camera as i, engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
import { MargoError as o } from "@margoengine/math/errors";
|
|
8
|
+
//#region src/stereo.ts
|
|
9
|
+
var s = class {
|
|
10
|
+
constructor(e = {}) {
|
|
11
|
+
this.left = new i(), this.right = new i(), this.options = { ...e };
|
|
12
|
+
}
|
|
13
|
+
update(e, t = 1) {
|
|
14
|
+
let n = () => typeof t == "function" ? t() : t;
|
|
15
|
+
e.update(n(), !0);
|
|
16
|
+
let r = e.position, i = e.target, a = e.up, o = [
|
|
17
|
+
r[0] - i[0],
|
|
18
|
+
r[1] - i[1],
|
|
19
|
+
r[2] - i[2]
|
|
20
|
+
], s = Math.hypot(...o), c = [
|
|
21
|
+
a[1] * o[2] - a[2] * o[1],
|
|
22
|
+
a[2] * o[0] - a[0] * o[2],
|
|
23
|
+
a[0] * o[1] - a[1] * o[0]
|
|
24
|
+
], l = Math.hypot(...c), u = this.options.eyeSeparation ?? .064, d = this.options.focusDistance ?? s;
|
|
25
|
+
if (!Number.isFinite(u) || u < 0 || !(d > 0)) throw RangeError("Invalid stereo geometry; pass finite eyeSeparation >= 0 and focusDistance > 0 or Infinity");
|
|
26
|
+
for (let [t, o] of [[this.left, -1], [this.right, 1]]) {
|
|
27
|
+
let s = o * u / 2, f = c.map((e) => e / l * s);
|
|
28
|
+
t.position = r.map((e, t) => e + f[t]), t.target = i.map((e, t) => e + f[t]), t.up = a, t.fov = e.fov, t.near = e.near, t.far = e.far, t.project = (t, r, i) => {
|
|
29
|
+
let a = n();
|
|
30
|
+
if (!Number.isFinite(a) || a <= 0) throw RangeError("Invalid stereo aspect; pass a finite aspect > 0");
|
|
31
|
+
if (e.project) e.project(t, a, i);
|
|
32
|
+
else {
|
|
33
|
+
let n = 1 / Math.tan(e.fov * Math.PI / 360), r = e.near, o = e.far, s = r - o;
|
|
34
|
+
t[0] = n / a, t[5] = n, t[10] = i ? -r / s : (o + r) / s, t[11] = -1, t[14] = i ? -o * r / s : 2 * o * r / s;
|
|
35
|
+
}
|
|
36
|
+
t[11] === 0 ? t[12] += t[0] * s : t[8] -= t[0] * s / d;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}, c = [
|
|
41
|
+
"side-by-side",
|
|
42
|
+
"top-bottom",
|
|
43
|
+
"anaglyph",
|
|
44
|
+
"parallax-barrier"
|
|
45
|
+
], l = `${r}
|
|
46
|
+
@group(0) @binding(0) var leftEye:texture_2d<f32>;
|
|
47
|
+
@group(0) @binding(1) var rightEye:texture_2d<f32>;
|
|
48
|
+
@group(0) @binding(2) var linearSampler:sampler;
|
|
49
|
+
@group(0) @binding(3) var<uniform> settings:array<vec4f,2>;
|
|
50
|
+
fn linear(c:vec3f)->vec3f{return select(pow((c+0.055)/1.055,vec3f(2.4)),c/12.92,c<=vec3f(0.04045));}
|
|
51
|
+
fn srgb(c:vec3f)->vec3f {let x=clamp(c,vec3f(0),vec3f(1));return select(1.055*pow(x,vec3f(1.0/2.4))-0.055,12.92*x,x<=vec3f(0.0031308));}
|
|
52
|
+
@fragment fn fragment(i:Vertex)->@location(0) vec4f {
|
|
53
|
+
let mode=settings[0].z; var uv=i.uv;var eye=0.0;
|
|
54
|
+
if(mode<0.5){eye=select(0.0,1.0,uv.x>=0.5);uv.x=fract(uv.x*2.0);}
|
|
55
|
+
else if(mode<1.5){eye=select(0.0,1.0,uv.y>=0.5);uv.y=fract(uv.y*2.0);}
|
|
56
|
+
else if(mode>2.5){let stripe=select(i.position.x,i.position.y,settings[1].x>0.5);eye=f32((u32(stripe)+u32(settings[1].y))%2u);}
|
|
57
|
+
var l=textureSampleLevel(leftEye,linearSampler,uv,0.0);var r=textureSampleLevel(rightEye,linearSampler,uv,0.0);
|
|
58
|
+
if(settings[0].w>0.5){let hold=l;l=r;r=hold;}
|
|
59
|
+
if(mode>1.5&&mode<2.5){
|
|
60
|
+
let a=linear(l.rgb);let b=linear(r.rgb);
|
|
61
|
+
let lc=vec3f(dot(a,vec3f(0.437,0.449,0.164)),dot(a,vec3f(-0.062,-0.062,-0.024)),dot(a,vec3f(-0.048,-0.050,-0.017)));
|
|
62
|
+
let rc=vec3f(dot(b,vec3f(-0.011,-0.032,-0.007)),dot(b,vec3f(0.377,0.761,0.009)),dot(b,vec3f(-0.026,-0.093,1.234)));
|
|
63
|
+
return vec4f(srgb(clamp(lc,vec3f(0),vec3f(1))+clamp(rc,vec3f(0),vec3f(1))),max(l.a,r.a));
|
|
64
|
+
}
|
|
65
|
+
return select(l,r,eye>0.5);
|
|
66
|
+
}`, u = class r {
|
|
67
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
68
|
+
return new r(e, t);
|
|
69
|
+
}
|
|
70
|
+
constructor(e, t = {}) {
|
|
71
|
+
this.engine = e, this.disposed = !1, this.options = { ...t }, this.rig = new s(this.options);
|
|
72
|
+
}
|
|
73
|
+
render(t = 0) {
|
|
74
|
+
if (this.disposed) throw new o("disposed", "Stereo output is disposed; create another StereoOutput");
|
|
75
|
+
if (!Number.isFinite(t) || t < 0) throw RangeError("Invalid stereo delta; pass finite seconds >= 0");
|
|
76
|
+
let n = this.engine, r = a(n), i = r.gpu, { mode: s = "side-by-side", swapEyes: l = !1, barrierAxis: u = "columns", barrierPhase: d = 0 } = this.options, f = c.indexOf(s);
|
|
77
|
+
if (f < 0 || !["columns", "rows"].includes(u) || ![0, 1].includes(d) || typeof l != "boolean") throw TypeError("Invalid stereo packing; use a supported mode, columns or rows, parity 0 or 1 and boolean swapEyes");
|
|
78
|
+
if (n.backend !== "webgpu" || !i) throw new o("unsupported", "Stereo output needs WebGPU or the WebGL2 compatibility layer");
|
|
79
|
+
let p = n.canvas, m = Math.max(1, Math.ceil(p.width / (f === 0 ? 2 : 1))), h = Math.max(1, Math.ceil(p.height / (f === 1 ? 2 : 1))), g = r.renderTarget.format, _ = this.ensure(i, m, h, g), v = this.options.camera ?? n.camera;
|
|
80
|
+
v.advance(t), Object.assign(this.rig.options, {
|
|
81
|
+
eyeSeparation: this.options.eyeSeparation,
|
|
82
|
+
focusDistance: this.options.focusDistance
|
|
83
|
+
}), this.rig.update(v, () => p.width / p.height * (f === 0 ? .5 : f === 1 ? 2 : 1));
|
|
84
|
+
let y = this.options.scene, b = this.options.layers ?? e(v);
|
|
85
|
+
n.render({
|
|
86
|
+
delta: t,
|
|
87
|
+
views: [{
|
|
88
|
+
id: "stereo:left",
|
|
89
|
+
camera: this.rig.left,
|
|
90
|
+
viewport: [
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
1,
|
|
94
|
+
1
|
|
95
|
+
],
|
|
96
|
+
target: { color: _.left },
|
|
97
|
+
scene: y,
|
|
98
|
+
layers: b
|
|
99
|
+
}, {
|
|
100
|
+
id: "stereo:right",
|
|
101
|
+
camera: this.rig.right,
|
|
102
|
+
viewport: [
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
1,
|
|
106
|
+
1
|
|
107
|
+
],
|
|
108
|
+
target: { color: _.right },
|
|
109
|
+
scene: y,
|
|
110
|
+
layers: b
|
|
111
|
+
}]
|
|
112
|
+
});
|
|
113
|
+
let x = p.getContext("webgpu");
|
|
114
|
+
if (!x) throw new o("state", "Stereo output needs the engine canvas context; create a WebGPU engine first");
|
|
115
|
+
i.queue.writeBuffer(_.uniform, 0, new Float32Array([
|
|
116
|
+
p.width,
|
|
117
|
+
p.height,
|
|
118
|
+
f,
|
|
119
|
+
+!!l,
|
|
120
|
+
+(u === "rows"),
|
|
121
|
+
d,
|
|
122
|
+
0,
|
|
123
|
+
0
|
|
124
|
+
]));
|
|
125
|
+
let S = i.createCommandEncoder(), C = S.beginRenderPass({ colorAttachments: [{
|
|
126
|
+
view: x.getCurrentTexture().createView(),
|
|
127
|
+
loadOp: "clear",
|
|
128
|
+
storeOp: "store"
|
|
129
|
+
}] });
|
|
130
|
+
C.setPipeline(_.pipeline), C.setBindGroup(0, _.bindings), C.draw(3), C.end(), i.queue.submit([S.finish()]);
|
|
131
|
+
}
|
|
132
|
+
ensure(e, r, i, a) {
|
|
133
|
+
if (this.state?.device === e && this.state.width === r && this.state.height === i && this.state.format === a) return this.state;
|
|
134
|
+
this.release();
|
|
135
|
+
let o = [], s;
|
|
136
|
+
try {
|
|
137
|
+
for (let t = 0; t < 2; t++) o.push(e.createTexture({
|
|
138
|
+
size: [r, i],
|
|
139
|
+
format: "rgba8unorm",
|
|
140
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
141
|
+
}));
|
|
142
|
+
s = e.createBuffer({
|
|
143
|
+
size: 32,
|
|
144
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
145
|
+
});
|
|
146
|
+
let c = n(e, l, a), [u, d] = o, f = t(e, c, [
|
|
147
|
+
u.createView(),
|
|
148
|
+
d.createView(),
|
|
149
|
+
e.createSampler({
|
|
150
|
+
magFilter: "linear",
|
|
151
|
+
minFilter: "linear"
|
|
152
|
+
}),
|
|
153
|
+
{ buffer: s }
|
|
154
|
+
]);
|
|
155
|
+
return this.state = {
|
|
156
|
+
device: e,
|
|
157
|
+
width: r,
|
|
158
|
+
height: i,
|
|
159
|
+
format: a,
|
|
160
|
+
left: u,
|
|
161
|
+
right: d,
|
|
162
|
+
uniform: s,
|
|
163
|
+
pipeline: c,
|
|
164
|
+
bindings: f
|
|
165
|
+
};
|
|
166
|
+
} catch (e) {
|
|
167
|
+
throw o.forEach((e) => e.destroy()), s?.destroy(), e;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
release() {
|
|
171
|
+
this.state?.left.destroy(), this.state?.right.destroy(), this.state?.uniform.destroy(), this.state = void 0;
|
|
172
|
+
}
|
|
173
|
+
dispose() {
|
|
174
|
+
this.disposed = !0, this.release();
|
|
175
|
+
}
|
|
176
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
177
|
+
this.dispose();
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
export { u as StereoOutput, s as StereoRig };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region ../shared/view-rect.js
|
|
2
|
+
function e(e) {
|
|
3
|
+
return e?.view?.viewport;
|
|
4
|
+
}
|
|
5
|
+
function t(t, n) {
|
|
6
|
+
let r = e(n);
|
|
7
|
+
if (!r) return;
|
|
8
|
+
t.setViewport(r[0], r[1], r[2], r[3], 0, 1);
|
|
9
|
+
let i = n.view.scissor ?? r;
|
|
10
|
+
t.setScissorRect(i[0], i[1], i[2], i[3]);
|
|
11
|
+
}
|
|
12
|
+
function n(e, t = "clear") {
|
|
13
|
+
return e?.view?.index ? "load" : t;
|
|
14
|
+
}
|
|
15
|
+
function r() {
|
|
16
|
+
return globalThis[Symbol.for("margoengine.layers")];
|
|
17
|
+
}
|
|
18
|
+
function i(e) {
|
|
19
|
+
return r()?.maskOf(e) ?? 1;
|
|
20
|
+
}
|
|
21
|
+
function a(e, t) {
|
|
22
|
+
if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`${t} ${e} is not a 32-bit layer mask; pass an integer from 0 to 0xffffffff`);
|
|
23
|
+
return e >>> 0;
|
|
24
|
+
}
|
|
25
|
+
function o(e, t, n, a, o = e.meshes) {
|
|
26
|
+
let s = r(), c = [], l = (e) => o.get(e.id) === e, u = (t) => {
|
|
27
|
+
l(t) && !e.store.hidden[t.id] && (t.visible = !1, c.push(t));
|
|
28
|
+
};
|
|
29
|
+
if (t !== 4294967295 && (s?.used || s?.source || !(t & 1))) for (let e of o.values()) i(e) & t || u(e);
|
|
30
|
+
if (n) for (let e of n) u(e);
|
|
31
|
+
try {
|
|
32
|
+
return a();
|
|
33
|
+
} finally {
|
|
34
|
+
if (!e.disposed) for (let e of c) l(e) && (e.visible = !0);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { o as i, a as n, n as r, t };
|