@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/dist/panini.js ADDED
@@ -0,0 +1,184 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { t as e } from "./errors-Cp7bu2gW.js";
5
+ import { r as t, t as n } from "./view-rect-p0kR0Q6B.js";
6
+ import { n as r, r as i, t as a } from "./gpu-BRxrrZK3.js";
7
+ import { n as o, r as s, t as c } from "./wide-laws-Dj0410P1.js";
8
+ //#region ../shared/effect-lifecycle.js
9
+ function l(e, t) {
10
+ e.release(), e.engine = void 0, e.setup(t);
11
+ }
12
+ function u(e) {
13
+ if (e.disposed) return !1;
14
+ e.disposed = !0;
15
+ let t = e.engine;
16
+ return e.engine = void 0, t?.sceneOf(e)?.removePlugin(e.name), e.release(), !0;
17
+ }
18
+ //#endregion
19
+ //#region src/panini.ts
20
+ var d = 1e-9, f = class {
21
+ constructor(e, t, { mode: n = "panini", distance: r = 1, crop: i = !0 } = {}) {
22
+ if (!(n in s)) throw RangeError("Invalid wide angle mode; use 'panini', 'equidistant', 'equisolid', 'stereographic' or 'orthographic'");
23
+ if (!(r >= 0 && r < Infinity)) throw RangeError("Invalid wide angle distance; use a finite number from 0");
24
+ if (!(t > 0 && t < Infinity) || !(e > 0 && e < 180)) throw RangeError("Invalid wide angle view; use a field of view between 0 and 180 degrees and an aspect above 0");
25
+ this.mode = n, this.distance = r, this.ty = Math.tan(e * Math.PI / 360), this.tx = this.ty * t;
26
+ let a = Math.atan(this.tx);
27
+ this.halfWidth = n === "panini" ? (r + 1) * Math.sin(a) / (r + Math.cos(a)) : c[n](a), this.halfHeight = this.halfWidth / t, this.zoom = 1, i && (this.zoom = this.fitZoom());
28
+ }
29
+ toSource(e, t) {
30
+ return this.sourceOf(e, t, this.zoom);
31
+ }
32
+ toOutput(e, t) {
33
+ let n = e * this.tx, r = t * this.ty, i, a;
34
+ if (this.mode === "panini") {
35
+ let e = this.distance, t = Math.atan(n), o = Math.cos(t), s = (e + 1) / (e + o);
36
+ i = s * Math.sin(t), a = s * r * o;
37
+ } else {
38
+ let e = Math.hypot(n, r), t = e ? c[this.mode](Math.atan(e)) / e : 0;
39
+ i = n * t, a = r * t;
40
+ }
41
+ let o = i / (this.halfWidth * this.zoom), s = a / (this.halfHeight * this.zoom);
42
+ return Math.abs(o) <= 1.000000001 && Math.abs(s) <= 1.000000001 ? [o, s] : null;
43
+ }
44
+ sourceOf(e, t, n) {
45
+ let r = e * this.halfWidth * n, i = t * this.halfHeight * n, a, s;
46
+ if (this.mode === "panini") {
47
+ let e = this.distance, t = r / (e + 1), n = Math.atan(t) + Math.asin(Math.max(-1, Math.min(1, t * e / Math.sqrt(1 + t * t)))), o = Math.cos(n);
48
+ if (!(o > d)) return null;
49
+ a = Math.tan(n), s = i / ((e + 1) / (e + o) * o);
50
+ } else {
51
+ let e = Math.hypot(r, i), t = o[this.mode](e);
52
+ if (t === null || !(t < Math.PI / 2 - d)) return null;
53
+ let n = e ? Math.tan(t) / e : 0;
54
+ a = r * n, s = i * n;
55
+ }
56
+ let c = a / this.tx, l = s / this.ty;
57
+ return Math.abs(c) <= 1.000000001 && Math.abs(l) <= 1.000000001 ? [c, l] : null;
58
+ }
59
+ fitZoom() {
60
+ let e = (e) => {
61
+ for (let t = 0; t <= 64; t++) {
62
+ let n = t / 64 * 2 - 1;
63
+ for (let [t, r] of [
64
+ [n, 1],
65
+ [n, -1],
66
+ [1, n],
67
+ [-1, n]
68
+ ]) if (!this.sourceOf(t, r, e)) return !1;
69
+ }
70
+ return !0;
71
+ };
72
+ if (e(1)) return 1;
73
+ let t = 0, n = 1;
74
+ for (let r = 0; r < 40; r++) {
75
+ let r = (t + n) / 2;
76
+ e(r) ? t = r : n = r;
77
+ }
78
+ return t;
79
+ }
80
+ }, p = `${a}
81
+ struct Wide {a:vec4f,b:vec4f}
82
+ @group(0) @binding(0) var<uniform> w:Wide;
83
+ @group(0) @binding(1) var frame:texture_2d<f32>;
84
+ @group(0) @binding(2) var smp:sampler;
85
+ const HALF_PI=1.5707963267948966;
86
+
87
+
88
+ @fragment fn fragment(v:Vertex)->@location(0) vec4f {
89
+ let mode=i32(w.a.x);
90
+ if (mode<0) {return textureSampleLevel(frame,smp,v.uv,0);}
91
+ let d=w.a.y;
92
+ let p=(v.uv*2-1)*vec2f(1,-1)*w.b.xy;
93
+ var s=vec2f(0);
94
+ if (mode==0) {
95
+ let xp=p.x/(d+1);
96
+ let phi=atan(xp)+asin(clamp(xp*d/sqrt(1+xp*xp),-1,1));
97
+ let c=cos(phi);
98
+ if (c<=1e-9) {return vec4f(0,0,0,1);}
99
+ s=vec2f(tan(phi),p.y/(((d+1)/(d+c))*c));
100
+ } else {
101
+ let r=length(p);
102
+ var theta=r;
103
+ if (mode==2) {if (r>2) {return vec4f(0,0,0,1);} theta=2*asin(r/2);}
104
+ else if (mode==3) {theta=2*atan(r/2);}
105
+ else if (mode==4) {if (r>1) {return vec4f(0,0,0,1);} theta=asin(r);}
106
+ if (theta>=HALF_PI-1e-9) {return vec4f(0,0,0,1);}
107
+ if (r>0) {s=p*(tan(theta)/r);}
108
+ }
109
+ let q=s/w.b.zw;
110
+ if (abs(q.x)>1.0000001||abs(q.y)>1.0000001) {return vec4f(0,0,0,1);}
111
+ return textureSampleLevel(frame,smp,vec2f(q.x*0.5+0.5,0.5-q.y*0.5),0);
112
+ }`, m = class a {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
113
+ constructor(e = {}) {
114
+ this.options = e, this.name = "wide-angle", this.stacked = !1, this.disposed = !1, this.key = "", this.mapKey = "", this.values = /* @__PURE__ */ new Float32Array(8), this.attachment = {
115
+ view: void 0,
116
+ loadOp: "clear",
117
+ storeOp: "store"
118
+ }, this.descriptor = { colorAttachments: [this.attachment] }, this.map(60, 1);
119
+ }
120
+ static async attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
121
+ let { PostStack: n } = await import("@margoengine/post"), r = new a(t);
122
+ return r.stack = n.join(e, r), r;
123
+ }
124
+ get enabled() {
125
+ return this.options.enabled ?? !0;
126
+ }
127
+ set enabled(e) {
128
+ if (typeof e != "boolean") throw TypeError("WideAngle enabled must be a boolean; pass true or false");
129
+ this.options.enabled = e;
130
+ }
131
+ map(e, t) {
132
+ let n = this.options, r = `${e}:${t}:${n.mode}:${n.distance}:${n.crop}`;
133
+ return (r !== this.mapKey || !this.lastMap) && (this.lastMap = new f(e, t, n), this.mapKey = r), this.lastMap;
134
+ }
135
+ joinStack(t) {
136
+ if (this.engine || this.stacked || this.disposed) throw e("attached", "WideAngle is already attached; create a new WideAngle");
137
+ return this.stacked = !0, {
138
+ kind: "ldr",
139
+ render: (e, t, n) => this.draw(e, t, n)
140
+ };
141
+ }
142
+ setup(t) {
143
+ if (this.disposed) throw e("disposed", "WideAngle is disposed; create a new WideAngle");
144
+ if (!this.stacked) throw e("state", "WideAngle runs inside a PostStack; add it to PostStack.join(engine, effect)");
145
+ if (!t.gpu) throw e("unsupported", "WideAngle requires WebGPU; it does not run on the WebGL2 fallback");
146
+ this.engine = t, this.uniform = t.gpu.createBuffer({
147
+ size: 32,
148
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
149
+ }), this.sampler = t.gpu.createSampler({
150
+ magFilter: "linear",
151
+ minFilter: "linear"
152
+ });
153
+ }
154
+ onDeviceRestored(e) {
155
+ l(this, e);
156
+ }
157
+ dispose() {
158
+ u(this) && (this.stack?.removeEffect(this), this.stack = void 0);
159
+ }
160
+ [Symbol.dispose ?? Symbol()]() {
161
+ this.dispose();
162
+ }
163
+ draw(e, i, a) {
164
+ let o = e.device, c = this.engine, l = this.map(c.camera.fov, e.width / e.height), u = this.values;
165
+ u[0] = this.enabled ? s[l.mode] : -1, u[1] = l.distance, u[4] = l.halfWidth * l.zoom, u[5] = l.halfHeight * l.zoom, u[6] = l.tx, u[7] = l.ty, o.queue.writeBuffer(this.uniform, 0, u);
166
+ let d = this.pipelineFor(o, a);
167
+ this.input !== i && (this.input = i, this.group = r(o, d, [
168
+ { buffer: this.uniform },
169
+ i,
170
+ this.sampler
171
+ ])), this.attachment.view = a.view, this.attachment.loadOp = t(e, "clear");
172
+ let f = e.encoder.beginRenderPass(this.descriptor);
173
+ f.setPipeline(d), f.setBindGroup(0, this.group), n(f, e), f.draw(3), f.end();
174
+ }
175
+ pipelineFor(e, t) {
176
+ let n = t.multisample?.count ?? 1;
177
+ return this.key !== `${t.format}/${n}` && (this.key = `${t.format}/${n}`, this.pipeline = i(e, p, t.format, { multisample: t.multisample }), this.input = void 0), this.pipeline;
178
+ }
179
+ release() {
180
+ this.uniform?.destroy(), this.uniform = this.sampler = this.pipeline = this.group = this.input = void 0, this.key = "";
181
+ }
182
+ };
183
+ //#endregion
184
+ export { m as WideAngle, f as WideAngleMap };
@@ -0,0 +1,46 @@
1
+ import type { EngineKernel } from '@margoengine/core';
2
+ import { type Vec3 } from '@margoengine/core/extension';
3
+ /** A standard WebGPU cube source, such as an attached CubeCamera. The caller retains ownership. */
4
+ export interface PanoramaCube {
5
+ /** Linear-light cube view. */
6
+ readonly view: GPUTextureView;
7
+ /** Refresh all six faces; return the number drawn. Omit for an already baked cube. */
8
+ update?(faces?: number): number;
9
+ }
10
+ /** Options for a single, complete 360 by 180 degree capture. */
11
+ export interface PanoramaOptions {
12
+ /** Use a caller-owned cube instead of making a temporary CubeCamera. */
13
+ cube?: PanoramaCube;
14
+ /** Temporary cube face edge, a power of two from 8 to 2048. Default 256. */
15
+ size?: number;
16
+ /** Capture point. Default the engine camera position. */
17
+ position?: Vec3;
18
+ /** Image width, an even positive integer. Height is width / 2. Default 4 * size. */
19
+ width?: number;
20
+ /** Longitude of the image centre, in radians. Zero looks along -Z; positive turns toward +X. Default 0. */
21
+ yaw?: number;
22
+ /** 'srgb' is RGBA8 for viewers and PNG; 'linear' is unclipped RGBA32F for baking. Default 'srgb'. */
23
+ format?: 'srgb' | 'linear';
24
+ /** Exposure in stops before the sRGB transfer function; ignored for linear output. Default 0. */
25
+ exposure?: number;
26
+ /** Abort the operation, including GPU readback and PNG encoding waits. */
27
+ signal?: AbortSignal;
28
+ }
29
+ /** Top-to-bottom equirectangular pixels, with north (+Y) at the top and -Z at the centre. */
30
+ export type PanoramaImage = {
31
+ width: number;
32
+ height: number;
33
+ format: 'srgb';
34
+ data: Uint8ClampedArray;
35
+ } | {
36
+ width: number;
37
+ height: number;
38
+ format: 'linear';
39
+ data: Float32Array;
40
+ };
41
+ /** Map normalised equirectangular coordinates to a world direction (u wraps, v is top to bottom). */
42
+ export declare function panoramaDirection(u: number, v: number, yaw?: number): Vec3;
43
+ /** Capture six scene faces and resample the whole sphere. No camera, canvas size or scene state is changed. */
44
+ export declare function capturePanorama(engine: EngineKernel, options?: PanoramaOptions): Promise<PanoramaImage>;
45
+ /** Encode viewer-ready sRGB pixels as a PNG Blob. Capture with format 'srgb' (the default) first. */
46
+ export declare function panoramaPNG(image: PanoramaImage, signal?: AbortSignal): Promise<Blob>;
@@ -0,0 +1,128 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const r=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { n as e, r as t, t as n } from "./gpu-BRxrrZK3.js";
5
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
6
+ import { MargoError as i } from "@margoengine/math/errors";
7
+ //#region src/panorama.ts
8
+ function a(e, t, n = 0) {
9
+ if (![
10
+ e,
11
+ t,
12
+ n
13
+ ].every(Number.isFinite) || t < 0 || t > 1) throw RangeError("Invalid panorama coordinates; pass finite u, yaw and v in 0..1");
14
+ let r = (e - .5) * 2 * Math.PI + n, i = (.5 - t) * Math.PI, a = Math.cos(i);
15
+ return [
16
+ a * Math.sin(r),
17
+ Math.sin(i),
18
+ -a * Math.cos(r)
19
+ ];
20
+ }
21
+ var o = `
22
+ ${n}
23
+ @group(0) @binding(0) var cube:texture_cube<f32>;
24
+ @group(0) @binding(1) var linearSampler:sampler;
25
+ @group(0) @binding(2) var<uniform> settings:vec4f;
26
+ fn srgb(c:vec3f)->vec3f {
27
+ let x=max(c,vec3f(0));
28
+ return select(1.055*pow(x,vec3f(1.0/2.4))-0.055,12.92*x,x<=vec3f(0.0031308));
29
+ }
30
+ @fragment fn fragment(i:Vertex)->@location(0) vec4f {
31
+ let longitude=(i.uv.x-0.5)*6.28318530718+settings.x;
32
+ let latitude=(0.5-i.uv.y)*3.14159265359;
33
+ let d=vec3f(cos(latitude)*sin(longitude),sin(latitude),-cos(latitude)*cos(longitude));
34
+ let c=textureSampleLevel(cube,linearSampler,d,0.0);
35
+ return vec4f(select(c.rgb,srgb(c.rgb*exp2(settings.y)),settings.z>0.5),c.a);
36
+ }`;
37
+ async function s(e, t) {
38
+ if (!t) return e;
39
+ let n, r = new Promise((r, i) => {
40
+ e.then(r, i), n = () => i(t.reason), t.addEventListener("abort", n, { once: !0 }), t.aborted && n();
41
+ });
42
+ try {
43
+ return await r;
44
+ } finally {
45
+ t.removeEventListener("abort", n);
46
+ }
47
+ }
48
+ async function c(n, a = {}) {
49
+ let { signal: c, yaw: l = 0, exposure: u = 0, format: d = "srgb", size: f = 256 } = a;
50
+ c?.throwIfAborted();
51
+ let p = a.width ?? f * 4, m = p / 2;
52
+ if (!Number.isInteger(p) || p < 2 || p % 2 || ![l, u].every(Number.isFinite)) throw RangeError("Invalid panorama size or orientation; pass an even width >= 2 and finite yaw and exposure");
53
+ if (d !== "linear" && d !== "srgb") throw TypeError("Invalid panorama format; pass srgb or linear");
54
+ let h = r(n).gpu;
55
+ if (n.backend !== "webgpu" || !h) throw new i("unsupported", "Panorama capture needs WebGPU or the WebGL2 compatibility layer");
56
+ if (p > h.limits.maxTextureDimension2D) throw RangeError("Panorama exceeds the device texture limit; reduce width");
57
+ let g = p * (d === "linear" ? 16 : 4), _ = Math.ceil(g / 256) * 256, v = _ * m;
58
+ if (v > h.limits.maxBufferSize) throw RangeError("Panorama exceeds the device buffer limit; reduce width");
59
+ let y, b, x, S;
60
+ try {
61
+ let r = a.cube;
62
+ if (!r) {
63
+ let { CubeCamera: e } = await s(import("@margoengine/environment/cube-camera"), c), t = e.attach(n, {
64
+ size: f,
65
+ position: a.position ?? n.camera.position
66
+ });
67
+ r = t, y = t;
68
+ }
69
+ if (c?.throwIfAborted(), r.update && r.update(6) !== 6) throw new i("state", "Panorama did not capture all six faces; capture after device restoration");
70
+ let C = d === "linear" ? "rgba32float" : "rgba8unorm", w = t(h, o, C);
71
+ x = h.createBuffer({
72
+ size: 16,
73
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
74
+ }), h.queue.writeBuffer(x, 0, new Float32Array([
75
+ l,
76
+ u,
77
+ +(d === "srgb"),
78
+ 0
79
+ ])), b = h.createTexture({
80
+ size: [p, m],
81
+ format: C,
82
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
83
+ }), S = h.createBuffer({
84
+ size: v,
85
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
86
+ });
87
+ let T = e(h, w, [
88
+ r.view,
89
+ h.createSampler({
90
+ minFilter: "linear",
91
+ magFilter: "linear"
92
+ }),
93
+ { buffer: x }
94
+ ]), E = h.createCommandEncoder(), D = E.beginRenderPass({ colorAttachments: [{
95
+ view: b.createView(),
96
+ loadOp: "clear",
97
+ storeOp: "store"
98
+ }] });
99
+ D.setPipeline(w), D.setBindGroup(0, T), D.draw(3), D.end(), E.copyTextureToBuffer({ texture: b }, {
100
+ buffer: S,
101
+ bytesPerRow: _
102
+ }, [p, m]), h.queue.submit([E.finish()]), await s(S.mapAsync(GPUMapMode.READ), c);
103
+ let O = new Uint8Array(S.getMappedRange()), k = new Uint8Array(g * m);
104
+ for (let e = 0; e < m; e++) k.set(O.subarray(e * _, e * _ + g), e * g);
105
+ return d === "linear" ? {
106
+ width: p,
107
+ height: m,
108
+ format: d,
109
+ data: new Float32Array(k.buffer)
110
+ } : {
111
+ width: p,
112
+ height: m,
113
+ format: d,
114
+ data: new Uint8ClampedArray(k.buffer)
115
+ };
116
+ } finally {
117
+ S?.unmap(), S?.destroy(), b?.destroy(), x?.destroy(), y?.dispose();
118
+ }
119
+ }
120
+ async function l(e, t) {
121
+ if (t?.throwIfAborted(), e.format !== "srgb") throw TypeError("PNG needs sRGB pixels; capture with format srgb");
122
+ if (!Number.isInteger(e.width) || e.width < 2 || e.width % 2 || e.height !== e.width / 2 || e.data.length !== e.width * e.height * 4) throw RangeError("Invalid panorama pixels; pass a complete 2:1 RGBA8 image");
123
+ let n = new OffscreenCanvas(e.width, e.height), r = n.getContext("2d");
124
+ if (!r) throw new i("unsupported", "PNG needs a 2D canvas; enable canvas support");
125
+ return r.putImageData(new ImageData(new Uint8ClampedArray(e.data), e.width, e.height), 0, 0), s(n.convertToBlob({ type: "image/png" }), t);
126
+ }
127
+ //#endregion
128
+ export { c as capturePanorama, a as panoramaDirection, l as panoramaPNG };
@@ -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
+ }
@@ -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 };