@margoengine/cameras 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ import type { Vec3 } from '@margoengine/core/extension';
2
+ /** An upright cylinder in the way of a view: a trunk, a crown, a hedge bush or a post. Metres, y up. */
3
+ export interface Occluder {
4
+ readonly x: number;
5
+ readonly z: number;
6
+ /** Its lowest and highest y (world heights, not heights above the ground). */
7
+ readonly bottom: number;
8
+ readonly top: number;
9
+ readonly radius: number;
10
+ /** The share of a crossing ray it stops, 0 to 1. Default 1 (solid). */
11
+ readonly opacity?: number;
12
+ }
13
+ /** What can stand between a camera and its subject. */
14
+ export interface SightScene {
15
+ /** Ground height at `(x, z)`. Default none (nothing is below ground). */
16
+ readonly ground?: (x: number, z: number) => number;
17
+ readonly occluders?: readonly Occluder[];
18
+ }
19
+ /** A camera and what it films, world coordinates. */
20
+ export interface SightView {
21
+ readonly position: Vec3;
22
+ readonly target: Vec3;
23
+ /** Vertical field of view, degrees. */
24
+ readonly fov: number;
25
+ /** Width over height. Default 16 / 9. */
26
+ readonly aspect?: number;
27
+ /** The subject: its foot `centre`, `width` across and `height`, metres. Default a 1 m box centred on `target`. */
28
+ readonly subject?: {
29
+ readonly centre: Vec3;
30
+ readonly width: number;
31
+ readonly height: number;
32
+ };
33
+ }
34
+ /** What {@link sightClearance} accepts as clear. */
35
+ export interface ClearanceLimits {
36
+ /** Least share of the subject seen, 0 to 1. Default 0.5. */
37
+ readonly subject?: number;
38
+ /** Foreground: nearer than this share of the distance to the subject, 0 to 1. Default 0.4. */
39
+ readonly near?: number;
40
+ /** Most of the frame the foreground occluders may cover, 0 to 1. Default 0.15. */
41
+ readonly foreground?: number;
42
+ /** Most of the frame's middle (half its width and height) they may cover, 0 to 1. Default 0.05. */
43
+ readonly centre?: number;
44
+ /** No occluder in frame nearer the lens than this, metres. Default 4. */
45
+ readonly lens?: number;
46
+ }
47
+ /** The measures of one view. */
48
+ export interface Clearance {
49
+ /** Share of the subject seen past the ground and the occluders, 0 to 1. */
50
+ readonly subject: number;
51
+ /** Share of the frame covered by foreground occluders, 0 to 1. */
52
+ readonly foreground: number;
53
+ /** The same for the frame's middle. */
54
+ readonly centre: number;
55
+ /** Distance to the nearest occluder in frame within the foreground or the lens limit, metres (`Infinity` for none). */
56
+ readonly lens: number;
57
+ /** Whether every measure is within the limits. */
58
+ readonly clear: boolean;
59
+ /** How far outside the limits the view is: 0 when clear, larger when worse. */
60
+ readonly penalty: number;
61
+ }
62
+ /**
63
+ * Measures what a camera sees of its subject past the ground and the occluders, and whether something covers the lens or
64
+ * the foreground. A long lens low over rising ground scores `subject` 0; a trunk a few metres off raises `centre`.
65
+ * @throws RangeError for a view with no direction, a field of view outside (0, 180) or an
66
+ * occluder with a radius or height range that is not positive.
67
+ * @example
68
+ * const seen = sightClearance(
69
+ * { position: [0, 2, 100], target: [0, 2, 0], fov: 20 },
70
+ * { ground: () => 0, occluders: [{ x: 0, z: 92, bottom: 0, top: 15, radius: 0.6 }] },
71
+ * );
72
+ * if (!seen.clear) console.log(seen.centre, seen.subject);
73
+ */
74
+ export declare function sightClearance(view: SightView, scene?: SightScene, limits?: ClearanceLimits): Clearance;
75
+ /** The pick of {@link clearestShot}. */
76
+ export interface ClearShot<T> {
77
+ readonly choice: T;
78
+ /** Its worst view's measures. */
79
+ readonly clearance: Clearance;
80
+ }
81
+ /**
82
+ * The first camera choice, in the order given (best first: a turn before a lift), whose every view is clear; when none
83
+ * is, the one whose worst view is least bad. A moving shot passes its start, middle and end as its views.
84
+ * @throws RangeError for no choices or a choice with no views.
85
+ * @example
86
+ * const lifts = [1.7, 3, 6, 12];
87
+ * const best = clearestShot(lifts, (h) => [{ position: [0, h, 100], target: [0, 2, 0], fov: 20 }], {
88
+ * ground: (x, z) => (Math.abs(z - 50) < 10 ? 4 : 0),
89
+ * });
90
+ * console.log(best.choice, best.clearance.clear); // the lowest height that sees over the bank
91
+ */
92
+ export declare function clearestShot<T>(choices: Iterable<T>, views: (choice: T) => readonly SightView[], scene?: SightScene, limits?: ClearanceLimits): ClearShot<T>;
@@ -0,0 +1,138 @@
1
+ import { n as e } from "./errors-Cp7bu2gW.js";
2
+ //#region src/clearance.ts
3
+ var t = 32, n = 18, r = (t, n) => {
4
+ if (!t) throw e(`Camera clearance: ${n}`, "camera-clearance");
5
+ }, i = (e) => e?.length === 3 && [
6
+ e[0],
7
+ e[1],
8
+ e[2]
9
+ ].every(Number.isFinite);
10
+ function a(e, t, n) {
11
+ let r = e[0] - n.x, i = e[2] - n.z, a = t[0] * t[0] + t[2] * t[2], o = r * t[0] + i * t[2], s = r * r + i * i - n.radius * n.radius, c = 0, l = Infinity;
12
+ if (a < 1e-12) {
13
+ if (s > 0) return Infinity;
14
+ } else {
15
+ let e = o * o - a * s;
16
+ if (e <= 0) return Infinity;
17
+ let t = Math.sqrt(e);
18
+ c = Math.max(0, (-o - t) / a), l = (-o + t) / a;
19
+ }
20
+ if (Math.abs(t[1]) < 1e-12) {
21
+ if (e[1] < n.bottom || e[1] > n.top) return Infinity;
22
+ } else {
23
+ let r = (n.bottom - e[1]) / t[1], i = (n.top - e[1]) / t[1];
24
+ c = Math.max(c, Math.min(r, i)), l = Math.min(l, Math.max(r, i));
25
+ }
26
+ return c < l ? c : Infinity;
27
+ }
28
+ function o(e, t, n, r) {
29
+ for (let i = .5; i < n; i += Math.max(.5, i * .01)) if (e[1] + t[1] * i < r(e[0] + t[0] * i, e[2] + t[2] * i)) return !0;
30
+ return !1;
31
+ }
32
+ function s(e, s = {}, c = {}) {
33
+ let { position: l, target: u, fov: d, aspect: f = 16 / 9 } = e;
34
+ r(i(l) && i(u), "position and target must be three finite numbers"), r(d > 0 && d < 180 && f > 0, "fov must be inside (0, 180) degrees and aspect above 0");
35
+ let p = {
36
+ subject: c.subject ?? .5,
37
+ near: c.near ?? .4,
38
+ foreground: c.foreground ?? .15,
39
+ centre: c.centre ?? .05,
40
+ lens: c.lens ?? 4
41
+ }, m = [
42
+ u[0] - l[0],
43
+ u[1] - l[1],
44
+ u[2] - l[2]
45
+ ], h = Math.hypot(m[0], m[1], m[2]), g = Math.hypot(m[0], m[2]);
46
+ r(g > 1e-6, "the view must not be empty or straight up or down"), m[0] /= h, m[1] /= h, m[2] /= h;
47
+ let _ = [
48
+ -m[2] * (h / g),
49
+ 0,
50
+ m[0] * (h / g)
51
+ ], v = [
52
+ _[1] * m[2] - _[2] * m[1],
53
+ _[2] * m[0] - _[0] * m[2],
54
+ _[0] * m[1] - _[1] * m[0]
55
+ ], y = Math.tan(d * Math.PI / 360), b = y * f, x = e.subject ?? {
56
+ centre: [
57
+ u[0],
58
+ u[1] - .5,
59
+ u[2]
60
+ ],
61
+ width: 1,
62
+ height: 1
63
+ }, S = Math.hypot(x.centre[0] - l[0], x.centre[2] - l[2]), C = Math.max(0, S - x.width / 2), w = Math.atan(b), T = m[0] * (h / g), E = m[2] * (h / g), D = [];
64
+ for (let e of s.occluders ?? []) {
65
+ r(e.radius > 0 && e.top > e.bottom, "an occluder needs a radius above 0 and top above bottom");
66
+ let t = e.x - l[0], n = e.z - l[2], i = Math.hypot(t, n);
67
+ i - e.radius > Math.max(h, S) + x.width || i > e.radius && Math.abs(Math.atan2(t * E - n * T, t * T + n * E)) > w + Math.asin(e.radius / i) + .02 || D.push(e);
68
+ }
69
+ let O = p.near * S, k = Math.max(O, p.lens), A = D.filter((e) => Math.hypot(e.x - l[0], e.z - l[2]) - e.radius < k), j = 0, M = 0, N = 0, P = Infinity;
70
+ for (let e = 0; e < n; e++) for (let r = 0; r < t; r++) {
71
+ let i = ((r + .5) / t * 2 - 1) * b, c = ((e + .5) / n * 2 - 1) * y, u = [
72
+ m[0] + _[0] * i + v[0] * c,
73
+ m[1] + _[1] * i + v[1] * c,
74
+ m[2] + _[2] * i + v[2] * c
75
+ ], d = Math.hypot(u[0], u[1], u[2]);
76
+ u[0] /= d, u[1] /= d, u[2] /= d;
77
+ let f = 1;
78
+ for (let e of A) {
79
+ let t = a(l, u, e);
80
+ t >= k || s.ground && t < P && o(l, u, t, s.ground) || (P = Math.min(P, t), t < O && (f *= 1 - (e.opacity ?? 1)));
81
+ }
82
+ j += 1 - f, Math.abs((r + .5) / t - .5) < .25 && Math.abs((e + .5) / n - .5) < .25 && (M += 1 - f, N++);
83
+ }
84
+ let F = [
85
+ _[0],
86
+ 0,
87
+ _[2]
88
+ ], I = 0, L = 0;
89
+ for (let e of [
90
+ .05,
91
+ .5,
92
+ .95
93
+ ]) for (let t of [
94
+ -.4,
95
+ -.2,
96
+ 0,
97
+ .2,
98
+ .4
99
+ ]) {
100
+ let n = x.centre[0] + F[0] * t * x.width, r = x.centre[2] + F[2] * t * x.width, i = Math.max(x.centre[1], s.ground?.(n, r) ?? x.centre[1]), c = [
101
+ n - l[0],
102
+ i + e * x.height - l[1],
103
+ r - l[2]
104
+ ], u = Math.hypot(c[0], c[1], c[2]);
105
+ if (L++, u < 1e-6 || (c[0] /= u, c[1] /= u, c[2] /= u, s.ground && o(l, c, u - Math.max(1, u * .01), s.ground))) continue;
106
+ let d = u * C / Math.max(S, 1e-6), f = 1;
107
+ for (let e of D) a(l, c, e) < d && (f *= 1 - (e.opacity ?? 1));
108
+ I += f;
109
+ }
110
+ let R = I / L, z = j / 576, B = M / N, V = Math.max(0, p.subject - R) * 4 + Math.max(0, z - p.foreground) * 2 + Math.max(0, B - p.centre) * 4 + (P < p.lens ? 1 + (p.lens - P) / p.lens : 0);
111
+ return {
112
+ subject: R,
113
+ foreground: z,
114
+ centre: B,
115
+ lens: P,
116
+ clear: V === 0,
117
+ penalty: V
118
+ };
119
+ }
120
+ function c(e, t, n = {}, i = {}) {
121
+ let a;
122
+ for (let o of e) {
123
+ let e;
124
+ for (let r of t(o)) {
125
+ let t = s(r, n, i);
126
+ if ((!e || t.penalty > e.penalty) && (e = t), a && e.penalty >= a.clearance.penalty) break;
127
+ }
128
+ r(e !== void 0, "every choice needs at least one view");
129
+ let c = e;
130
+ if ((!a || c.penalty < a.clearance.penalty) && (a = {
131
+ choice: o,
132
+ clearance: c
133
+ }), a.clearance.clear) break;
134
+ }
135
+ return r(a !== void 0, "give at least one choice"), a;
136
+ }
137
+ //#endregion
138
+ export { c as clearestShot, s as sightClearance };
@@ -0,0 +1,7 @@
1
+ //#region ../shared/errors.js
2
+ var e = (e, t) => Object.assign(Error(t), {
3
+ name: "MargoError",
4
+ code: e
5
+ }), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (e, n) => RangeError(t(e, n));
6
+ //#endregion
7
+ export { n, e as t };
@@ -0,0 +1,34 @@
1
+ import type { Vec3 } from '@margoengine/core/extension';
2
+ /** The view {@link fitBox} keeps: its direction (`target - position`), vertical field of view and screen up. */
3
+ export interface BoxFitView {
4
+ position: ArrayLike<number>;
5
+ target: ArrayLike<number>;
6
+ /** Vertical field of view in degrees, as core's `Camera.fov`. */
7
+ fov: number;
8
+ /** Screen up, not parallel to the view. Default [0, 1, 0], as core's `Camera.up`. */
9
+ up?: ArrayLike<number>;
10
+ }
11
+ /** Options of {@link fitBox}. */
12
+ export interface BoxFitOptions {
13
+ /** Width / height of the frame. Default 1. */
14
+ aspect?: number;
15
+ /** At least 1: the box fills 1 / padding of the frame's width or height, whichever binds. Default 1.1. */
16
+ padding?: number;
17
+ }
18
+ /** Where {@link fitBox} puts the camera. */
19
+ export interface BoxFit {
20
+ position: Vec3;
21
+ /** On the view axis, level with the box's centre. */
22
+ target: Vec3;
23
+ /** From `position` to `target`. */
24
+ distance: number;
25
+ /** The nearest corner's depth along the view, so a caller can check it against `camera.near`. */
26
+ nearest: number;
27
+ }
28
+ /**
29
+ * The closest camera position, along the view's own direction, from which all eight corners of `bounds`
30
+ * (`[minX, minY, minZ, maxX, maxY, maxZ]`, as `model.bounds`) sit inside the frame shrunk by `padding`, centred. Pure:
31
+ * apply it with `camera.position = fit.position; camera.target = fit.target`, or use
32
+ * `CameraProjection.fit(bounds, { framing: 'box' })` to animate it.
33
+ */
34
+ export declare function fitBox(view: BoxFitView, bounds: ArrayLike<number>, { aspect, padding }?: BoxFitOptions): BoxFit;
@@ -0,0 +1,31 @@
1
+ (globalThis[Symbol.for("margoengine.frame-resources")] ??= { devices: /* @__PURE__ */ new WeakMap() }).devices;
2
+ //#endregion
3
+ //#region ../shared/gpu.js
4
+ var e = "\nstruct Vertex { @builtin(position) position:vec4f,@location(0) uv:vec2f }\n@vertex fn vertex(@builtin(vertex_index) index:u32)->Vertex {\n let p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[index];\n return Vertex(vec4f(p,0,1),vec2f(p.x*0.5+0.5,0.5-p.y*0.5));\n}";
5
+ function t(e, t, n) {
6
+ return e.createBindGroup({
7
+ layout: t.getBindGroupLayout(0),
8
+ entries: n.map((e, t) => ({
9
+ binding: t,
10
+ resource: e
11
+ }))
12
+ });
13
+ }
14
+ function n(e, t, n, r = {}) {
15
+ let i = e.createShaderModule({ code: t });
16
+ return e.createRenderPipeline({
17
+ layout: "auto",
18
+ vertex: {
19
+ module: i,
20
+ entryPoint: "vertex"
21
+ },
22
+ fragment: {
23
+ module: i,
24
+ entryPoint: "fragment",
25
+ targets: [{ format: n }]
26
+ },
27
+ ...r
28
+ });
29
+ }
30
+ //#endregion
31
+ export { t as n, n as r, e as t };
@@ -0,0 +1,75 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import { Camera, type RenderView } from '@margoengine/core/extension';
4
+ /** A side of the target. front sits in the bottom slot, back in the top, left and right in their own slots. */
5
+ export type HologramSide = 'front' | 'back' | 'left' | 'right';
6
+ /** How one view is laid on the screen. */
7
+ export interface HologramViewOrientation {
8
+ /** Counter-clockwise turn of the picture in degrees: 0 keeps its top up, 90 turns its top to the left. */
9
+ rotation?: 0 | 90 | 180 | 270;
10
+ /** Mirror the picture left to right before the turn, as a pyramid face's reflection needs. */
11
+ mirror?: boolean;
12
+ }
13
+ /** Default orientations: every view mirrored, its top pointing away from the screen centre (screen-down pyramid). */
14
+ export declare const HOLOGRAM_VIEWS: Readonly<Record<HologramSide, Readonly<Required<HologramViewOrientation>>>>;
15
+ /** Camera geometry of the four views. Distances use world units (normally metres). */
16
+ export interface HologramRigOptions {
17
+ /** Distance from the target to each camera, > 0. Default the source camera's distance to its target. */
18
+ distance?: number;
19
+ }
20
+ /** Four cameras around the source camera's target, a quarter turn apart about its up axis. */
21
+ export declare class HologramRig {
22
+ /** Looks at the target from the source camera's side. */
23
+ readonly front: Camera;
24
+ /** Looks from the opposite side. */
25
+ readonly back: Camera;
26
+ /** Looks from the source camera's left. */
27
+ readonly left: Camera;
28
+ /** Looks from the source camera's right. */
29
+ readonly right: Camera;
30
+ /** Mutable geometry; validated on update. */
31
+ readonly options: HologramRigOptions;
32
+ constructor(options?: HologramRigOptions);
33
+ /** Place the four cameras from a source camera. Its pose and projection are not changed; views are square. */
34
+ update(source: Camera): void;
35
+ }
36
+ /** Four-view output settings. Update them before render. */
37
+ export interface HologramOutputOptions extends HologramRigOptions {
38
+ /** Source camera: its target is the centre and its side is the front. Default engine.camera. */
39
+ camera?: Camera;
40
+ /** Scene to render. Default engine.scene. */
41
+ scene?: RenderView['scene'];
42
+ /** Layer mask for all four views. Default the source camera's mask. */
43
+ layers?: number;
44
+ /**
45
+ * Side of the empty centre square, as a fraction of the canvas's smaller side, in [0, 1). Default 0.2. Each view is
46
+ * (1 - gap) / 2 across; below 1/3 the views meet along the diagonals and each is cut to its wedge, like a face.
47
+ */
48
+ gap?: number;
49
+ /** Per-side orientation; missing fields keep {@link HOLOGRAM_VIEWS}. */
50
+ views?: Partial<Record<HologramSide, HologramViewOrientation>>;
51
+ /** Clear colour of each view, `#rgb` or `#rrggbb`, or false for the scene's background. Default '#000000'. */
52
+ background?: string | false;
53
+ }
54
+ /** Replaces the application's engine.render call: four square scene views and one layout pass onto the canvas. */
55
+ export declare class HologramOutput {
56
+ private readonly engine;
57
+ /** Creates four-view output for the application's frame loop. Same as the constructor. */
58
+ static attach(engine: EngineKernel, options?: HologramOutputOptions): HologramOutput;
59
+ /** Mutable settings, validated on render. */
60
+ readonly options: HologramOutputOptions;
61
+ /** The four cameras, also usable for picking or custom layouts. */
62
+ readonly rig: HologramRig;
63
+ private state?;
64
+ private disposed;
65
+ constructor(engine: EngineKernel, options?: HologramOutputOptions);
66
+ /** The resolved orientation of every side, after validation. */
67
+ orientations(): Record<HologramSide, Required<HologramViewOrientation>>;
68
+ /** Render once; animation and plugin updates happen once for the four views. Use from your frame loop. */
69
+ render(delta?: number): void;
70
+ private ensure;
71
+ private release;
72
+ /** Release the four view textures and uniforms. Safe to call more than once. */
73
+ dispose(): void;
74
+ [Symbol.dispose]: () => void;
75
+ }
@@ -0,0 +1,208 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const a=(engine,version="1.0.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
+ import { hexColor as s } from "@margoengine/math/color";
9
+ //#region src/hologram.ts
10
+ var c = "; see https://margoengine.com/errors#camera-hologram", l = Object.freeze({
11
+ front: Object.freeze({
12
+ rotation: 180,
13
+ mirror: !0
14
+ }),
15
+ back: Object.freeze({
16
+ rotation: 0,
17
+ mirror: !0
18
+ }),
19
+ left: Object.freeze({
20
+ rotation: 90,
21
+ mirror: !0
22
+ }),
23
+ right: Object.freeze({
24
+ rotation: 270,
25
+ mirror: !0
26
+ })
27
+ }), u = [
28
+ "front",
29
+ "back",
30
+ "left",
31
+ "right"
32
+ ], d = class {
33
+ constructor(e = {}) {
34
+ this.front = new i(), this.back = new i(), this.left = new i(), this.right = new i(), this.options = { ...e };
35
+ }
36
+ update(e) {
37
+ e.update(1, !0);
38
+ let t = e.position, n = e.target, r = e.up, i = Math.hypot(...r), a = r.map((e) => e / i), s = [
39
+ t[0] - n[0],
40
+ t[1] - n[1],
41
+ t[2] - n[2]
42
+ ], l = s[0] * a[0] + s[1] * a[1] + s[2] * a[2], d = s.map((e, t) => e - l * a[t]), f = Math.hypot(...d), p = this.options.distance ?? Math.hypot(...s);
43
+ if (!(f > 1e-9 * Math.hypot(...s))) throw new o("state", "Hologram source camera looks along its up axis; tilt it or change camera.up" + c);
44
+ if (!Number.isFinite(p) || p <= 0) throw RangeError("Invalid hologram distance; pass a finite distance > 0" + c);
45
+ let m = d.map((e) => e / f), h = [
46
+ a[1] * m[2] - a[2] * m[1],
47
+ a[2] * m[0] - a[0] * m[2],
48
+ a[0] * m[1] - a[1] * m[0]
49
+ ], g = p / Math.hypot(...s), _ = l * g, v = f * g, y = {
50
+ front: m,
51
+ back: m.map((e) => -e),
52
+ right: h,
53
+ left: h.map((e) => -e)
54
+ };
55
+ for (let t of u) {
56
+ let i = this[t], o = y[t];
57
+ i.position = n.map((e, t) => e + o[t] * v + a[t] * _), i.target = [
58
+ n[0],
59
+ n[1],
60
+ n[2]
61
+ ], i.up = r, i.fov = e.fov, i.near = e.near, i.far = e.far, i.project = e.project && ((t, n, r) => e.project(t, 1, r));
62
+ }
63
+ }
64
+ }, f = `${r}
65
+ @group(0) @binding(0) var front:texture_2d<f32>;
66
+ @group(0) @binding(1) var back:texture_2d<f32>;
67
+ @group(0) @binding(2) var left:texture_2d<f32>;
68
+ @group(0) @binding(3) var right:texture_2d<f32>;
69
+ @group(0) @binding(4) var linearSampler:sampler;
70
+ @group(0) @binding(5) var<uniform> settings:array<vec4f,4>;
71
+ @fragment fn fragment(i:Vertex)->@location(0) vec4f {
72
+ let size=settings[0].xy;let g=settings[0].z;let v=settings[0].w;
73
+ let d=(i.position.xy-size*0.5)/min(size.x,size.y);
74
+ var side=0u;var s=vec2f(0);
75
+ if(abs(d.y)>=abs(d.x)){
76
+ if(d.y>0.0){side=0u;s=vec2f(d.x+v*0.5,d.y-g*0.5)/v;}else{side=1u;s=vec2f(d.x+v*0.5,d.y+g*0.5+v)/v;}
77
+ } else {
78
+ if(d.x<0.0){side=2u;s=vec2f(d.x+g*0.5+v,d.y+v*0.5)/v;}else{side=3u;s=vec2f(d.x-g*0.5,d.y+v*0.5)/v;}
79
+ }
80
+ if(any(s<vec2f(0))||any(s>vec2f(1))){return vec4f(0,0,0,1);}
81
+ var c=s-0.5;
82
+ for(var k=0u;k<u32(settings[1][side]+0.5);k++){c=vec2f(-c.y,c.x);}
83
+ if(settings[2][side]>0.5){c.x=-c.x;}
84
+ let q=clamp(c+0.5,vec2f(settings[3].x),vec2f(1.0-settings[3].x));
85
+ let a=textureSampleLevel(front,linearSampler,q,0.0);let b=textureSampleLevel(back,linearSampler,q,0.0);
86
+ let l=textureSampleLevel(left,linearSampler,q,0.0);let r=textureSampleLevel(right,linearSampler,q,0.0);
87
+ if(side==0u){return a;} if(side==1u){return b;} if(side==2u){return l;} return r;
88
+ }`, p = class r {
89
+ static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
90
+ return new r(e, t);
91
+ }
92
+ constructor(e, t = {}) {
93
+ this.engine = e, this.disposed = !1, this.options = { ...t }, this.rig = new d(this.options);
94
+ }
95
+ orientations() {
96
+ let e = this.options.views ?? {}, t = {};
97
+ for (let t of Object.keys(e)) if (!u.includes(t)) throw TypeError(`Unknown hologram side ${t}; use front, back, left or right; see https://margoengine.com/errors#camera-hologram`);
98
+ for (let n of u) {
99
+ let { rotation: r = l[n].rotation, mirror: i = l[n].mirror } = e[n] ?? {};
100
+ if (![
101
+ 0,
102
+ 90,
103
+ 180,
104
+ 270
105
+ ].includes(r) || typeof i != "boolean") throw TypeError("Invalid hologram view orientation; use rotation 0, 90, 180 or 270 and boolean mirror" + c);
106
+ t[n] = {
107
+ rotation: r,
108
+ mirror: i
109
+ };
110
+ }
111
+ return t;
112
+ }
113
+ render(t = 0) {
114
+ if (this.disposed) throw new o("disposed", "Hologram output is disposed; create another HologramOutput" + c);
115
+ if (!Number.isFinite(t) || t < 0) throw RangeError("Invalid hologram delta; pass finite seconds >= 0" + c);
116
+ let { gap: n = .2, background: r = "#000000" } = this.options;
117
+ if (!Number.isFinite(n) || n < 0 || n >= 1) throw RangeError("Invalid hologram gap; pass a fraction in [0, 1)" + c);
118
+ if (r !== !1 && (String(r).length > 7 || !s(String(r)))) throw TypeError("Invalid hologram background; pass #rgb, #rrggbb or false" + c);
119
+ let i = this.orientations(), l = this.engine, d = a(l), f = d.gpu;
120
+ if (l.backend !== "webgpu" || !f) throw new o("unsupported", "Hologram output needs WebGPU or the WebGL2 compatibility layer" + c);
121
+ let p = l.canvas, m = (1 - n) / 2, h = Math.max(1, Math.ceil(m * Math.min(p.width, p.height))), g = this.ensure(f, h, d.renderTarget.format), _ = this.options.camera ?? l.camera;
122
+ _.advance(t), this.rig.options.distance = this.options.distance, this.rig.update(_);
123
+ let v = this.options.scene, y = this.options.layers ?? e(_), b = r === !1 ? void 0 : { color: r };
124
+ l.render({
125
+ delta: t,
126
+ views: u.map((e, t) => ({
127
+ id: "hologram:" + e,
128
+ camera: this.rig[e],
129
+ viewport: [
130
+ 0,
131
+ 0,
132
+ 1,
133
+ 1
134
+ ],
135
+ target: { color: g.textures[t] },
136
+ scene: v,
137
+ layers: y,
138
+ clear: b
139
+ }))
140
+ });
141
+ let x = p.getContext("webgpu");
142
+ if (!x) throw new o("state", "Hologram output needs the engine canvas context; create a WebGPU engine first" + c);
143
+ f.queue.writeBuffer(g.uniform, 0, new Float32Array([
144
+ p.width,
145
+ p.height,
146
+ n,
147
+ m,
148
+ ...u.map((e) => i[e].rotation / 90),
149
+ ...u.map((e) => +!!i[e].mirror),
150
+ .5 / h,
151
+ 0,
152
+ 0,
153
+ 0
154
+ ]));
155
+ let S = f.createCommandEncoder(), C = S.beginRenderPass({ colorAttachments: [{
156
+ view: x.getCurrentTexture().createView(),
157
+ loadOp: "clear",
158
+ storeOp: "store"
159
+ }] });
160
+ C.setPipeline(g.pipeline), C.setBindGroup(0, g.bindings), C.draw(3), C.end(), f.queue.submit([S.finish()]);
161
+ }
162
+ ensure(e, r, i) {
163
+ if (this.state?.device === e && this.state.side === r && this.state.format === i) return this.state;
164
+ this.release();
165
+ let a = [], o;
166
+ try {
167
+ for (let t = 0; t < 4; t++) a.push(e.createTexture({
168
+ size: [r, r],
169
+ format: "rgba8unorm",
170
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
171
+ }));
172
+ o = e.createBuffer({
173
+ size: 64,
174
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
175
+ });
176
+ let s = n(e, f, i), c = t(e, s, [
177
+ ...a.map((e) => e.createView()),
178
+ e.createSampler({
179
+ magFilter: "linear",
180
+ minFilter: "linear"
181
+ }),
182
+ { buffer: o }
183
+ ]);
184
+ return this.state = {
185
+ device: e,
186
+ side: r,
187
+ format: i,
188
+ textures: a,
189
+ uniform: o,
190
+ pipeline: s,
191
+ bindings: c
192
+ };
193
+ } catch (e) {
194
+ throw a.forEach((e) => e.destroy()), o?.destroy(), e;
195
+ }
196
+ }
197
+ release() {
198
+ this.state?.textures.forEach((e) => e.destroy()), this.state?.uniform.destroy(), this.state = void 0;
199
+ }
200
+ dispose() {
201
+ this.disposed = !0, this.release();
202
+ }
203
+ [Symbol.dispose ?? Symbol()]() {
204
+ this.dispose();
205
+ }
206
+ };
207
+ //#endregion
208
+ export { l as HOLOGRAM_VIEWS, p as HologramOutput, d as HologramRig };