rayzee 10.0.0 → 11.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.
Files changed (173) hide show
  1. package/README.md +252 -44
  2. package/dist/ActiveColor-YvuQq44O.js +13 -0
  3. package/dist/ArchiveImporter-BRQvLggb.js +4300 -0
  4. package/dist/AssetConfig-DSEntE65.js +30 -0
  5. package/dist/BidirectionalIntegrator-9lZQxFVH.js +934 -0
  6. package/dist/BufferLayout-D_IBo2ci.js +150 -0
  7. package/dist/ColorManagement-oeEk7oSu.js +1488 -0
  8. package/dist/EngineEvents-Ci8dVWCP.js +73 -0
  9. package/dist/EngineIssues-CIZnXHsF.js +72 -0
  10. package/dist/FileFormats-sPGJeCRc.js +102 -0
  11. package/dist/Logger-dDDGj9Av.js +97 -0
  12. package/dist/{NeuralRetouch-CYgkQY8Z.js → NeuralRetouch-BCXylThH.js} +8 -6
  13. package/dist/{NeuralSuperRes-PkgDLQcD.js → NeuralSuperRes-sZAeS142.js} +55 -120
  14. package/dist/PathTracer-CQPW06Mm.js +29971 -0
  15. package/dist/PhysicalSky-BvqCw_dy.js +1219 -0
  16. package/dist/RayzeeRenderer-COpwotpU.js +3551 -0
  17. package/dist/SolarLimb-CPaeZVwz.js +721 -0
  18. package/dist/ToneMapGPU-CjU3osDd.js +177 -0
  19. package/dist/ViewTransforms-CYN7bq2w.js +255 -0
  20. package/dist/WorkingMatrix-C6y233hT.js +27 -0
  21. package/dist/ZipReader-gDSWr0I5.js +98 -0
  22. package/dist/addons/archives.es.js +3 -0
  23. package/dist/addons/bidirectional.es.js +2 -0
  24. package/dist/addons/color.es.js +3 -0
  25. package/dist/addons/formats.es.js +2 -0
  26. package/dist/addons/physical-sky.es.js +2 -0
  27. package/dist/addons/storage.es.js +3 -0
  28. package/dist/archiveFormats-BZw7TiDp.js +75 -0
  29. package/dist/identity-DrQsGT2o.js +39 -0
  30. package/dist/openStorage-DwcBRUj7.js +820 -0
  31. package/dist/rayzee-core.es.js +12 -0
  32. package/dist/rayzee.es.js +2551 -44024
  33. package/dist/rayzee.umd.js +528 -487
  34. package/dist/sceneCachePolicy-BSSsKY98.js +443 -0
  35. package/dist/shared-BGBcSxBQ.js +17 -0
  36. package/package.json +23 -2
  37. package/src/Color/ActiveColor.js +34 -0
  38. package/src/Color/BasicColor.js +147 -0
  39. package/src/Color/ColorManagement.js +7 -31
  40. package/src/Color/ViewTransforms.js +5 -1
  41. package/src/EngineDefaults.js +62 -1029
  42. package/src/EngineEvents.js +2 -0
  43. package/src/EngineIssues.js +2 -0
  44. package/src/Headless.js +6 -5
  45. package/src/Passes/AIUpscaler.js +58 -141
  46. package/src/Passes/OIDNDenoiser.js +92 -65
  47. package/src/Passes/OIDNTemporalHistory.js +1 -1
  48. package/src/PathTracerApp.js +620 -4026
  49. package/src/Pipeline/PipelineContext.js +0 -4
  50. package/src/Platform.js +36 -0
  51. package/src/Processor/ArchiveCache.js +1 -1
  52. package/src/Processor/ArchiveImporter.js +1259 -0
  53. package/src/Processor/AssetLoader.js +327 -1763
  54. package/src/Processor/AtmosphereModel.js +1 -2
  55. package/src/Processor/BVHBuilder.js +14 -126
  56. package/src/Processor/BVHLeafFold.js +4 -6
  57. package/src/Processor/BVHRefitter.js +67 -46
  58. package/src/Processor/BufferLayout.js +291 -0
  59. package/src/Processor/EmissiveTriangleBuilder.js +1 -1
  60. package/src/Processor/EnvironmentExactTable.js +187 -0
  61. package/src/Processor/EquirectHDRInfo.js +30 -223
  62. package/src/Processor/FileFormats.js +116 -0
  63. package/src/Processor/GLTFDecoders.js +85 -0
  64. package/src/Processor/GLTFDiffuseTransmission.js +63 -0
  65. package/src/Processor/GeometryExtractor.js +15 -5
  66. package/src/Processor/HostMemory.js +7 -1
  67. package/src/Processor/IESParser.js +47 -35
  68. package/src/Processor/InstanceTable.js +1 -1
  69. package/src/Processor/KernelManager.js +35 -2
  70. package/src/Processor/MaterialPacking.js +10 -8
  71. package/src/Processor/PBRT/PBRTMaterials.js +129 -41
  72. package/src/Processor/PBRT/PBRTParser.js +30 -17
  73. package/src/Processor/PBRT/PBRTSceneBuilder.js +548 -37
  74. package/src/Processor/PBRT/PBRTTextureBake.js +154 -0
  75. package/src/Processor/PBRT/PFM.js +59 -0
  76. package/src/Processor/PackedRayBuffer.js +116 -9
  77. package/src/Processor/ParallelBVHBuilder.js +3 -6
  78. package/src/Processor/PhysicalSky.js +5 -4
  79. package/src/Processor/PlatformImageLoader.js +50 -6
  80. package/src/Processor/QueueManager.js +39 -5
  81. package/src/Processor/ReinsertionOptimizer.js +2 -2
  82. package/src/Processor/SceneProcessor.js +53 -62
  83. package/src/Processor/ShaderBuilder.js +6 -16
  84. package/src/Processor/SimpleSky.js +1 -62
  85. package/src/Processor/SolarLimb.js +3 -0
  86. package/src/Processor/SplitBLAS.js +1 -1
  87. package/src/Processor/StorageTexturePool.js +22 -1
  88. package/src/Processor/TLASBuilder.js +1 -3
  89. package/src/Processor/TextureBuckets.js +237 -0
  90. package/src/Processor/TextureCreator.js +181 -8
  91. package/src/Processor/ToneMapGPU.js +64 -15
  92. package/src/Processor/Workers/BVHRefitWorker.js +5 -2
  93. package/src/Processor/Workers/BVHSubtreeWorker.js +1 -34
  94. package/src/Processor/Workers/BVHWorker.js +5 -18
  95. package/src/Processor/Workers/CDFWorker.js +6 -183
  96. package/src/Processor/Workers/TexturesWorker.js +103 -18
  97. package/src/Processor/archiveFormats.js +7 -0
  98. package/src/Processor/utils.js +2 -1
  99. package/src/RayzeeRenderer.js +3899 -0
  100. package/src/RenderSettings.js +68 -62
  101. package/src/Stages/ASVGF.js +13 -14
  102. package/src/Stages/AutoExposure.js +17 -17
  103. package/src/Stages/BilateralFilter.js +2 -1
  104. package/src/Stages/Compositor.js +8 -23
  105. package/src/Stages/DenoiserSettings.js +182 -0
  106. package/src/Stages/EdgeFilter.js +2 -1
  107. package/src/Stages/MotionVector.js +1 -1
  108. package/src/Stages/NRD.js +11 -5
  109. package/src/Stages/NormalDepth.js +12 -8
  110. package/src/Stages/PathTracer.js +338 -43
  111. package/src/Stages/PathTracerStage.js +69 -66
  112. package/src/Stages/Variance.js +1 -1
  113. package/src/Storage/BLASCache.js +1 -1
  114. package/src/Storage/CDFCache.js +18 -17
  115. package/src/Storage/DownloadCache.js +1 -1
  116. package/src/Storage/SceneGraphCodec.js +46 -3
  117. package/src/Storage/SpillStore.js +1 -1
  118. package/src/Storage/StorageManager.js +45 -9
  119. package/src/Storage/areas.js +4 -0
  120. package/src/TSL/BVHTraversal.js +50 -25
  121. package/src/TSL/Bidirectional.js +204 -0
  122. package/src/TSL/BidirectionalLamps.js +132 -0
  123. package/src/TSL/Clearcoat.js +15 -6
  124. package/src/TSL/Common.js +20 -15
  125. package/src/TSL/ConnectKernel.js +147 -0
  126. package/src/TSL/Debugger.js +1 -1
  127. package/src/TSL/Displacement.js +4 -3
  128. package/src/TSL/EmissiveSampling.js +65 -18
  129. package/src/TSL/Environment.js +77 -106
  130. package/src/TSL/EnvironmentCDF.js +85 -39
  131. package/src/TSL/ExtendKernel.js +8 -3
  132. package/src/TSL/FinalWriteKernel.js +4 -4
  133. package/src/TSL/GenerateKernel.js +18 -4
  134. package/src/TSL/HitFacet.js +35 -5
  135. package/src/TSL/LightBVHSampling.js +40 -9
  136. package/src/TSL/LightGenerateKernel.js +293 -0
  137. package/src/TSL/LightGuide.js +198 -0
  138. package/src/TSL/LightSplatKernel.js +165 -0
  139. package/src/TSL/LightVisibility.js +111 -0
  140. package/src/TSL/LightsCore.js +13 -38
  141. package/src/TSL/LightsDirect.js +28 -29
  142. package/src/TSL/LightsIndirect.js +3 -1
  143. package/src/TSL/LightsSampling.js +239 -51
  144. package/src/TSL/MaterialEvaluation.js +113 -31
  145. package/src/TSL/MaterialProperties.js +44 -17
  146. package/src/TSL/MaterialTransmission.js +52 -47
  147. package/src/TSL/MergeKernel.js +218 -0
  148. package/src/TSL/PathTracerCore.js +65 -29
  149. package/src/TSL/Random.js +5 -1
  150. package/src/TSL/SceneResources.js +51 -0
  151. package/src/TSL/ShadeKernel.js +964 -101
  152. package/src/TSL/Struct.js +10 -0
  153. package/src/TSL/Sun.js +1 -1
  154. package/src/TSL/TextureSampling.js +66 -61
  155. package/src/addons/archives.js +14 -0
  156. package/src/addons/bidirectional.js +13 -0
  157. package/src/addons/color.js +32 -0
  158. package/src/addons/formats.js +16 -0
  159. package/src/addons/physicalSky.js +14 -0
  160. package/src/addons/storage.js +19 -0
  161. package/src/core.js +31 -0
  162. package/src/index.js +13 -23
  163. package/src/integrators/BidirectionalIntegrator.js +833 -0
  164. package/src/managers/AnimationManager.js +88 -6
  165. package/src/managers/CameraManager.js +103 -46
  166. package/src/managers/DenoisingManager.js +14 -10
  167. package/src/managers/EnvironmentManager.js +125 -103
  168. package/src/managers/GoboManager.js +9 -0
  169. package/src/managers/MaterialDataManager.js +48 -37
  170. package/src/managers/UniformManager.js +32 -10
  171. package/dist/ToneMapGPU-B6XIe3DU.js +0 -544
  172. package/dist/rolldown-runtime-CAFD8bLK.js +0 -11
  173. package/src/Processor/TreeletOptimizer.js +0 -574
@@ -0,0 +1,4300 @@
1
+ import { n as e } from "./BidirectionalIntegrator-9lZQxFVH.js";
2
+ import { A as t, g as n, n as r, r as i } from "./sceneCachePolicy-BSSsKY98.js";
3
+ import { n as a, r as o } from "./EngineIssues-CIZnXHsF.js";
4
+ import { r as s } from "./shared-BGBcSxBQ.js";
5
+ import { a as c, n as l, r as u } from "./identity-DrQsGT2o.js";
6
+ import { n as d, t as f } from "./archiveFormats-BZw7TiDp.js";
7
+ import { n as p, t as m } from "./ZipReader-gDSWr0I5.js";
8
+ import { AnimationClip as h, BooleanKeyframeTrack as g, BufferAttribute as _, BufferGeometry as v, Color as y, DataTexture as b, DirectionalLight as x, DoubleSide as S, EquirectangularReflectionMapping as C, Float32BufferAttribute as w, FloatType as T, FrontSide as E, Group as D, InstancedBufferAttribute as O, InstancedMesh as k, LinearFilter as A, LoadingManager as j, MaterialLoader as M, Matrix4 as N, Mesh as ee, MeshPhysicalMaterial as te, NoColorSpace as ne, NumberKeyframeTrack as re, Object3D as ie, OrthographicCamera as ae, PerspectiveCamera as oe, PointLight as se, Quaternion as ce, QuaternionKeyframeTrack as le, RGBAFormat as P, RepeatWrapping as ue, SRGBColorSpace as F, SphereGeometry as de, SpotLight as fe, Texture as pe, Uint32BufferAttribute as I, Vector3 as L, VectorKeyframeTrack as me } from "three";
9
+ import { HDRLoader as he } from "three/addons/loaders/HDRLoader.js";
10
+ import { Gunzip as ge, strFromU8 as R, zipSync as _e } from "three/addons/libs/fflate.module.js";
11
+ import { EXRLoader as ve } from "three/addons/loaders/EXRLoader.js";
12
+ //#region src/Processor/ArchiveReader.js
13
+ var z = 512, ye = 4 << 20, be = 15e8, xe = new TextDecoder();
14
+ function Se(e) {
15
+ return e.length >= 2 && e[0] === 31 && e[1] === 139;
16
+ }
17
+ function Ce(e) {
18
+ return e.length >= 4 && e[0] === 80 && e[1] === 75 && (e[2] === 3 || e[2] === 5 || e[2] === 7);
19
+ }
20
+ function we(e) {
21
+ return e.length >= 263 && String.fromCharCode(...e.subarray(257, 262)) === "ustar";
22
+ }
23
+ function Te(e) {
24
+ return Se(e) ? "gzip" : Ce(e) ? "zip" : we(e) ? "tar" : null;
25
+ }
26
+ function B(e, t, n) {
27
+ let r = t, i = t + n;
28
+ for (; r < i && e[r] !== 0;) r++;
29
+ return xe.decode(e.subarray(t, r));
30
+ }
31
+ function Ee(e, t, n) {
32
+ if (e[t] & 128) {
33
+ let r = e[t] & 127;
34
+ for (let i = t + 1; i < t + n; i++) r = r * 256 + e[i];
35
+ return r;
36
+ }
37
+ let r = B(e, t, n).trim();
38
+ if (!r) return 0;
39
+ let i = parseInt(r, 8);
40
+ return Number.isFinite(i) ? i : 0;
41
+ }
42
+ function De(e) {
43
+ let t = [];
44
+ for (let n of e.replace(/\\/g, "/").split("/")) n === "" || n === "." || (n === ".." ? t.pop() : t.push(n));
45
+ return t.join("/");
46
+ }
47
+ function Oe(e) {
48
+ let t = xe.decode(e).match(/^\d+ path=(.*)$/m);
49
+ return t ? t[1] : null;
50
+ }
51
+ var ke = (e) => e + (z - e % z) % z, Ae = (e) => e === "L" || e === "x" || e === "X";
52
+ function je(e) {
53
+ let t = B(e, 345, 155), n = B(e, 0, 100);
54
+ return {
55
+ type: String.fromCharCode(e[156] || 48),
56
+ size: Ee(e, 124, 12),
57
+ name: t ? `${t}/${n}` : n
58
+ };
59
+ }
60
+ function Me(e, t) {
61
+ return e === "5" || e === "g" || e === "K" || t.endsWith("/") || e !== "0" && e !== " " ? null : De(t) || null;
62
+ }
63
+ function Ne(e) {
64
+ for (let t = 0; t < z; t++) if (e[t] !== 0) return !1;
65
+ return !0;
66
+ }
67
+ var Pe = class {
68
+ constructor({ filter: e = null, retain: t = null, byteBudget: n = be, onEntry: r = null } = {}) {
69
+ this.filter = e, this.retain = t, this.byteBudget = n, this.onEntry = r, this.entries = Object.create(null), this.listing = [], this.retainedBytes = 0, this.truncated = !1, this.finished = !1, this._header = new Uint8Array(z), this._headerLen = 0, this._state = "header", this._remaining = 0, this._size = 0, this._copied = 0, this._dst = null, this._special = null, this._pendingName = null, this._abs = 0;
70
+ }
71
+ push(e) {
72
+ let t = this._abs;
73
+ this._abs += e.length;
74
+ let n = 0;
75
+ for (; n < e.length && !this.finished;) if (this._state === "header") {
76
+ let r = Math.min(z - this._headerLen, e.length - n);
77
+ this._header.set(e.subarray(n, n + r), this._headerLen), this._headerLen += r, n += r, this._headerLen === z && (this._headerLen = 0, this._bodyStart = t + n, this._readHeader());
78
+ } else {
79
+ let t = Math.min(this._remaining, e.length - n);
80
+ if (this._dst && this._copied < this._size) {
81
+ let r = Math.min(t, this._size - this._copied);
82
+ this._dst.set(e.subarray(n, n + r), this._copied), this._copied += r;
83
+ }
84
+ this._remaining -= t, n += t, this._remaining === 0 && this._endBody();
85
+ }
86
+ }
87
+ end() {
88
+ this.finished = !0;
89
+ }
90
+ result() {
91
+ return {
92
+ entries: this.entries,
93
+ listing: this.listing,
94
+ retainedBytes: this.retainedBytes,
95
+ truncated: this.truncated
96
+ };
97
+ }
98
+ _readHeader() {
99
+ let e = this._header, t = !0;
100
+ for (let n = 0; n < z; n++) if (e[n] !== 0) {
101
+ t = !1;
102
+ break;
103
+ }
104
+ if (t) {
105
+ this.finished = !0;
106
+ return;
107
+ }
108
+ let { type: n, size: r, name: i } = je(e), a = this._pendingName ?? i;
109
+ if (this._pendingName = null, this._size = r, this._copied = 0, this._remaining = ke(r), this._dst = null, this._special = null, this._indexed = !1, this._state = this._remaining > 0 ? "body" : "header", Ae(n)) {
110
+ this._special = n === "L" ? "longname" : "pax", this._dst = new Uint8Array(r);
111
+ return;
112
+ }
113
+ let o = Me(n, a);
114
+ o && (this.listing.push({
115
+ path: o,
116
+ size: r
117
+ }), this._indexed = this.filter ? this.filter(o, r) : !0, this._indexed && !(this.retain && !this.retain(o, r)) && (this.retainedBytes + r > this.byteBudget ? this.truncated = !0 : (this._dst = new Uint8Array(r), this.retainedBytes += r)), this._remaining === 0 && this._endBody());
118
+ }
119
+ _endBody() {
120
+ if (this._state = "header", this._special === "longname") {
121
+ this._pendingName = B(this._dst, 0, this._dst.length), this._dst = null;
122
+ return;
123
+ }
124
+ if (this._special === "pax") {
125
+ let e = Oe(this._dst);
126
+ e && (this._pendingName = e), this._dst = null;
127
+ return;
128
+ }
129
+ let e = this.listing[this.listing.length - 1];
130
+ this._indexed && e && (e.offset = this._bodyStart, this._indexed = !1), this._dst &&= (this.entries[e.path] = this._dst, this.onEntry?.(e.path, this._dst), null);
131
+ }
132
+ };
133
+ async function Fe(e, t) {
134
+ if (typeof DecompressionStream < "u" && typeof e?.stream == "function") {
135
+ let n = e.stream().pipeThrough(new DecompressionStream("gzip")).getReader();
136
+ for (;;) {
137
+ let { done: e, value: r } = await n.read();
138
+ if (e) break;
139
+ t(r);
140
+ }
141
+ return;
142
+ }
143
+ let n = null, r = new ge((e, r) => {
144
+ e instanceof Error ? n = e : e && e.length && t(e);
145
+ });
146
+ await Ie(e, (e, t) => {
147
+ if (r.push(e, t), n) throw n;
148
+ });
149
+ }
150
+ async function Ie(e, t) {
151
+ if (e instanceof Uint8Array) {
152
+ for (let n = 0; n < e.length; n += ye) {
153
+ let r = Math.min(n + ye, e.length);
154
+ t(e.subarray(n, r), r === e.length);
155
+ }
156
+ e.length === 0 && t(e, !0);
157
+ return;
158
+ }
159
+ let n = e.size;
160
+ for (let r = 0; r < n; r += ye) {
161
+ let i = Math.min(r + ye, n);
162
+ t(new Uint8Array(await e.slice(r, i).arrayBuffer()), i === n);
163
+ }
164
+ n === 0 && t(new Uint8Array(), !0);
165
+ }
166
+ async function Le(e, t = {}) {
167
+ let { onProgress: n, ...r } = t, i = new Pe(r), a = 0, o = 0;
168
+ return await Fe(e, (e) => {
169
+ i.push(e), a += e.length, n && a >= o && (o = a + (64 << 20), n({
170
+ entries: i.listing.length,
171
+ bytes: a
172
+ }));
173
+ }), i.end(), i.result();
174
+ }
175
+ var Re = 1 << 20;
176
+ async function ze(e, { onProgress: t = null } = {}) {
177
+ let n = e.size, r = [], i = null, a = 0, o = null, s = 0, c = 0, l = async (t, r) => {
178
+ if (i && t >= a && t + r <= a + i.length) return i.subarray(t - a, t - a + r);
179
+ let o = Math.min(n, t + Math.max(r, Re));
180
+ return i = new Uint8Array(await e.slice(t, o).arrayBuffer()), a = t, i.subarray(0, r);
181
+ };
182
+ for (; s + z <= n;) {
183
+ let e = await l(s, z);
184
+ if (Ne(e)) break;
185
+ let { type: n, size: i, name: a } = je(e), u = o ?? a;
186
+ o = null;
187
+ let d = s + z;
188
+ if (Ae(n)) {
189
+ let e = await l(d, i);
190
+ o = n === "L" ? B(e, 0, e.length) : Oe(e);
191
+ } else {
192
+ let e = Me(n, u);
193
+ e && r.push({
194
+ path: e,
195
+ size: i,
196
+ offset: d
197
+ });
198
+ }
199
+ s = d + ke(i), t && s >= c && (c = s + (256 << 20), t({
200
+ entries: r.length,
201
+ bytes: s
202
+ }));
203
+ }
204
+ return r;
205
+ }
206
+ function Be() {
207
+ let e = new Pe({ retain: () => !1 });
208
+ return {
209
+ push: (t) => e.push(t),
210
+ finish: () => (e.end(), e.listing.filter((e) => e.offset !== void 0))
211
+ };
212
+ }
213
+ async function Ve(e, t = {}) {
214
+ let { index: n = null, headersOnly: r = !1, onProgress: i = null, ...a } = t, o, s, c, l, u = null;
215
+ if (n || r) {
216
+ u = n?.listing ?? await ze(e, { onProgress: i });
217
+ let { filter: t } = a;
218
+ s = u.map((e) => !t || t(e.path, e.size) ? { ...e } : {
219
+ path: e.path,
220
+ size: e.size
221
+ }), o = Object.create(null), c = 0, l = !1;
222
+ } else {
223
+ let t = new Pe({ ...a });
224
+ await Ie(e, (e) => t.push(e)), t.end(), {entries: o, listing: s, retainedBytes: c, truncated: l} = t.result();
225
+ }
226
+ let d = /* @__PURE__ */ new Map();
227
+ for (let e of s) e.offset !== void 0 && d.set(e.path, e);
228
+ return {
229
+ entries: o,
230
+ listing: s,
231
+ read: async (t) => {
232
+ if (o[t]) return o[t];
233
+ let n = d.get(t);
234
+ return n ? new Uint8Array(await e.slice(n.offset, n.offset + n.size).arrayBuffer()) : null;
235
+ },
236
+ readHead: async (t, n) => {
237
+ if (o[t]) return o[t].subarray(0, n);
238
+ let r = d.get(t);
239
+ return r ? new Uint8Array(await e.slice(r.offset, r.offset + Math.min(r.size, n)).arrayBuffer()) : null;
240
+ },
241
+ retainedBytes: c,
242
+ truncated: l,
243
+ indexed: d.size,
244
+ index: u ? {
245
+ v: 1,
246
+ listing: u
247
+ } : null
248
+ };
249
+ }
250
+ function He(e) {
251
+ let t = (Array.isArray(e) ? e : [e]).filter(Boolean).map((e) => {
252
+ let t = De(e);
253
+ return {
254
+ p: t,
255
+ inside: t + "/"
256
+ };
257
+ });
258
+ return t.length === 0 ? () => !0 : (e) => {
259
+ let n = e.lastIndexOf("/"), r = n < 0 ? "" : e.slice(0, n);
260
+ if (r === "") return !0;
261
+ let i = r.lastIndexOf("/textures"), a = i >= 0 && i === r.length - 9 ? r.slice(0, i) + "/" : null;
262
+ for (let { p: n, inside: i } of t) if (e === n || e.startsWith(i) || i.startsWith(r + "/") || a !== null && i.startsWith(a)) return !0;
263
+ return !1;
264
+ };
265
+ }
266
+ function Ue(e) {
267
+ let t = e.filter((e) => e.path.toLowerCase().endsWith(".pbrt")).sort((e, t) => e.path.split("/").length - t.path.split("/").length || e.path.length - t.path.length || (e.path < t.path ? -1 : 1));
268
+ if (!t.length) return {
269
+ root: null,
270
+ elements: []
271
+ };
272
+ let n = t[0].path, r = n.lastIndexOf("/"), i = r < 0 ? "" : n.slice(0, r), a = i ? i + "/" : "", o = /* @__PURE__ */ new Map();
273
+ for (let t of e) {
274
+ if (!t.path.startsWith(a)) continue;
275
+ let e = t.path.slice(a.length), n = e.indexOf("/");
276
+ if (n < 0) continue;
277
+ let r = e.slice(0, n), i = o.get(r);
278
+ i || o.set(r, i = {
279
+ name: r,
280
+ prefix: a + r,
281
+ files: 0,
282
+ bytes: 0,
283
+ scenes: 0
284
+ }), i.files++, i.bytes += t.size, t.path.toLowerCase().endsWith(".pbrt") && i.scenes++;
285
+ }
286
+ return {
287
+ root: i,
288
+ elements: [...o.values()].filter((e) => e.scenes > 0).sort((e, t) => e.bytes - t.bytes)
289
+ };
290
+ }
291
+ //#endregion
292
+ //#region src/Processor/ArchiveCache.js
293
+ var We = 1, Ge = 3;
294
+ async function Ke(e, t) {
295
+ return `${e}:${u(await l(t))}`;
296
+ }
297
+ async function qe(e, { storage: t, label: n = e.name, onProgress: r = null }) {
298
+ if (!t || typeof DecompressionStream > "u") return null;
299
+ let i = t.area(s.ARCHIVES), o = await Ke("gunzip", e), c = await i.open(o), l = !!c;
300
+ if (!c) {
301
+ let l = await i.create(o, {
302
+ label: n,
303
+ expectedBytes: e.size * Ge
304
+ });
305
+ if (!l) return null;
306
+ let u = Be(), d = new TransformStream({ transform(e, t) {
307
+ u.push(e), t.enqueue(e);
308
+ } });
309
+ try {
310
+ let t = e.stream().pipeThrough(new DecompressionStream("gzip")).pipeThrough(d), n = await l.writeStream("data.tar", t, { onProgress: r });
311
+ await l.writeJSON("index.json", {
312
+ v: We,
313
+ listing: u.finish()
314
+ }), await l.commit({
315
+ compressedBytes: e.size,
316
+ tarBytes: n
317
+ });
318
+ } catch (e) {
319
+ if (await l.abort(), e?.name !== "QuotaExceededError") throw e;
320
+ return t._issue(a.STORAGE_QUOTA_EXCEEDED, `ran out of room unpacking ${n}; reading it in memory instead`, { area: s.ARCHIVES }), null;
321
+ }
322
+ if (c = await i.open(o, { wait: !0 }), !c) return null;
323
+ }
324
+ let u = await c.file("data.tar"), d = await c.json("index.json");
325
+ return !u || d?.v !== We ? (c.release(), null) : {
326
+ file: u,
327
+ index: d,
328
+ release: () => c.release(),
329
+ cached: l
330
+ };
331
+ }
332
+ async function Je(e, t) {
333
+ if (!t) return null;
334
+ let n = await t.area(s.ARCHIVES).open(await Ke("tarindex", e));
335
+ if (!n) return null;
336
+ try {
337
+ let e = await n.json("index.json");
338
+ return e?.v === We ? e : null;
339
+ } finally {
340
+ n.release();
341
+ }
342
+ }
343
+ async function Ye(e, t, n) {
344
+ if (!t || !n) return !1;
345
+ let r = await t.area(s.ARCHIVES).create(await Ke("tarindex", e), { label: `${e.name} (index)` });
346
+ if (!r) return !1;
347
+ try {
348
+ return await r.writeJSON("index.json", n), await r.commit(), !0;
349
+ } catch {
350
+ return await r.abort(), !1;
351
+ }
352
+ }
353
+ var Xe = ["diffuseTransmissionMap", "diffuseTransmissionColorMap"], Ze = "__rayzeeArchivePath", Qe = "__rayzeeArchiveLoader", $e = 64, et = 32 << 20, tt = 64 << 20, nt = {
354
+ Float32Array,
355
+ Float64Array,
356
+ Int8Array,
357
+ Int16Array,
358
+ Int32Array,
359
+ Uint8Array,
360
+ Uint16Array,
361
+ Uint32Array,
362
+ Uint8ClampedArray
363
+ }, rt = [
364
+ "name",
365
+ "mapping",
366
+ "channel",
367
+ "wrapS",
368
+ "wrapT",
369
+ "magFilter",
370
+ "minFilter",
371
+ "anisotropy",
372
+ "format",
373
+ "type",
374
+ "flipY",
375
+ "generateMipmaps",
376
+ "premultiplyAlpha",
377
+ "unpackAlignment",
378
+ "colorSpace"
379
+ ], it = [
380
+ "intensity",
381
+ "distance",
382
+ "decay",
383
+ "angle",
384
+ "penumbra"
385
+ ], at = [
386
+ "name",
387
+ "visible",
388
+ "castShadow",
389
+ "receiveShadow",
390
+ "frustumCulled",
391
+ "renderOrder",
392
+ "matrixAutoUpdate"
393
+ ], V = class extends Error {
394
+ constructor(e) {
395
+ super(e), this.name = "SceneGraphUnsupported";
396
+ }
397
+ };
398
+ function ot(e) {
399
+ return e.isInstancedMesh ? "InstancedMesh" : e.isSkinnedMesh || e.isBatchedMesh ? null : e.isMesh ? "Mesh" : e.isPerspectiveCamera ? "PerspectiveCamera" : e.isOrthographicCamera ? "OrthographicCamera" : e.isDirectionalLight ? "DirectionalLight" : e.isPointLight ? "PointLight" : e.isSpotLight ? "SpotLight" : e.isLight || e.isPoints || e.isLine || e.isSprite || e.isLOD ? null : e.isGroup ? "Group" : e.constructor === ie ? "Object3D" : null;
400
+ }
401
+ function st(e, t) {
402
+ try {
403
+ let t = JSON.parse(JSON.stringify(e ?? {}));
404
+ return delete t[Ze], delete t[Qe], t;
405
+ } catch {
406
+ throw new V(`${t}: userData is not plain JSON`);
407
+ }
408
+ }
409
+ var ct = class {
410
+ constructor() {
411
+ this.sections = [], this.length = 0;
412
+ }
413
+ add(e) {
414
+ let t = Math.ceil(this.length / $e) * $e;
415
+ return this.sections.push({
416
+ offset: t,
417
+ array: e
418
+ }), this.length = t + e.byteLength, {
419
+ offset: t,
420
+ byteLength: e.byteLength,
421
+ type: e.constructor.name,
422
+ length: e.length
423
+ };
424
+ }
425
+ };
426
+ function lt(e, t, n) {
427
+ if (e.isInterleavedBufferAttribute || !ArrayBuffer.isView(e.array)) throw new V(`${n}: interleaved or non-typed attribute`);
428
+ return {
429
+ section: t.add(e.array),
430
+ itemSize: e.itemSize,
431
+ normalized: e.normalized === !0,
432
+ name: e.name ?? ""
433
+ };
434
+ }
435
+ function ut(e, t) {
436
+ let n = `geometry ${e.name || e.uuid}`;
437
+ if (Object.keys(e.morphAttributes ?? {}).length) throw new V(`${n}: morph targets`);
438
+ let r = {};
439
+ for (let [i, a] of Object.entries(e.attributes)) r[i] = lt(a, t, n);
440
+ return {
441
+ uuid: e.uuid,
442
+ name: e.name,
443
+ attributes: r,
444
+ index: e.index ? lt(e.index, t, n) : null,
445
+ groups: e.groups.map((e) => ({ ...e })),
446
+ drawRange: { ...e.drawRange },
447
+ userData: st(e.userData, n)
448
+ };
449
+ }
450
+ function dt(e, t) {
451
+ let n = {
452
+ uuid: e.uuid,
453
+ userData: st(e.userData, `texture ${e.uuid}`)
454
+ };
455
+ for (let t of rt) n[t] = e[t];
456
+ for (let t of [
457
+ "repeat",
458
+ "offset",
459
+ "center"
460
+ ]) n[t] = e[t].toArray();
461
+ n.rotation = e.rotation;
462
+ let r = e.userData?.[Ze];
463
+ if (r) n.path = r, n.loader = e.userData.__rayzeeArchiveLoader ?? "image";
464
+ else if (e.isDataTexture && ArrayBuffer.isView(e.image?.data)) n.data = {
465
+ section: t.add(e.image.data),
466
+ width: e.image.width,
467
+ height: e.image.height
468
+ };
469
+ else throw new V(`texture ${e.name || e.uuid}: no archive path and no pixel data to keep`);
470
+ return n;
471
+ }
472
+ function ft(e, t) {
473
+ let n = e.toJSON(t);
474
+ n.exactColors = {};
475
+ for (let [t, r] of Object.entries(e)) r?.isColor && (n.exactColors[t] = [
476
+ r.r,
477
+ r.g,
478
+ r.b
479
+ ]);
480
+ e.ior !== void 0 && (n.exactIor = e.ior), e.diffuseTransmission !== void 0 && (n.diffuseTransmission = e.diffuseTransmission);
481
+ for (let t of Xe) e[t]?.isTexture && ((n.engineTextures ??= {})[t] = e[t].uuid);
482
+ return n;
483
+ }
484
+ function pt(e, t) {
485
+ for (let n of Object.values(e)) n?.isTexture && t.set(n.uuid, n);
486
+ }
487
+ function mt(e, { environment: t = null, animations: n = [], stats: r = {} } = {}) {
488
+ let i = new ct(), a = [], o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l = (e, t) => {
489
+ let n = ot(e);
490
+ if (!n) throw new V(`${e.type} "${e.name}" cannot be cached`);
491
+ let r = {
492
+ type: n,
493
+ uuid: e.uuid,
494
+ parent: t,
495
+ userData: st(e.userData, `${n} "${e.name}"`)
496
+ };
497
+ for (let t of at) r[t] = e[t];
498
+ if (r.position = e.position.toArray(), r.quaternion = e.quaternion.toArray(), r.scale = e.scale.toArray(), r.layers = e.layers.mask, e.matrixAutoUpdate || (r.matrix = e.matrix.toArray()), e.isMesh) {
499
+ o.set(e.geometry.uuid, e.geometry);
500
+ let t = Array.isArray(e.material) ? e.material : [e.material];
501
+ for (let e of t) s.set(e.uuid, e), pt(e, c);
502
+ r.geometry = e.geometry.uuid, r.material = Array.isArray(e.material) ? t.map((e) => e.uuid) : e.material.uuid;
503
+ }
504
+ if (e.isInstancedMesh && (r.count = e.count, r.instanceMatrix = i.add(e.instanceMatrix.array), r.instanceColor = e.instanceColor ? i.add(e.instanceColor.array) : null), e.isCamera) for (let t of [
505
+ "fov",
506
+ "aspect",
507
+ "near",
508
+ "far",
509
+ "zoom",
510
+ "focus",
511
+ "filmGauge",
512
+ "filmOffset",
513
+ "left",
514
+ "right",
515
+ "top",
516
+ "bottom"
517
+ ]) e[t] !== void 0 && (r[t] = e[t]);
518
+ if (e.isLight) {
519
+ r.color = e.color.toArray();
520
+ for (let t of it) e[t] !== void 0 && (r[t] = e[t]);
521
+ e.target && (r.target = e.target.uuid);
522
+ }
523
+ let u = a.push(r) - 1;
524
+ for (let t of e.children) l(t, u);
525
+ };
526
+ l(e, -1), t && c.set(t.uuid, t);
527
+ let u = {
528
+ textures: {},
529
+ images: {}
530
+ };
531
+ for (let e of c.keys()) u.textures[e] = { uuid: e };
532
+ return {
533
+ manifest: {
534
+ v: 2,
535
+ nodes: a,
536
+ geometries: [...o.values()].map((e) => ut(e, i)),
537
+ textures: [...c.values()].map((e) => dt(e, i)),
538
+ materials: [...s.values()].map((e) => ft(e, u)),
539
+ animations: n.map((e) => h.toJSON(e)),
540
+ environment: t ? t.uuid : null,
541
+ stats: r
542
+ },
543
+ sections: i.sections,
544
+ byteLength: i.length
545
+ };
546
+ }
547
+ async function ht(e, { manifest: t, sections: n }, { release: r = !1 } = {}) {
548
+ for (let t of n) {
549
+ let { offset: n, array: i } = t, a = new Uint8Array(i.buffer, i.byteOffset, i.byteLength);
550
+ for (let t = 0; t < a.length; t += et) await e.write("data.bin", a.subarray(t, Math.min(a.length, t + et)), { at: n + t });
551
+ r && (t.array = null);
552
+ }
553
+ n.length === 0 && await e.write("data.bin", new Uint8Array()), await e.writeJSON("graph.json", t);
554
+ }
555
+ function gt(e) {
556
+ let t = nt[e];
557
+ if (!t) throw Error(`scene graph: unknown array type ${e}`);
558
+ return t;
559
+ }
560
+ async function _t(e, t) {
561
+ let n = /* @__PURE__ */ new Map(), r = [...t].sort((e, t) => e.offset - t.offset), i = null, a = 0;
562
+ for (let t of r) {
563
+ let { offset: r, byteLength: o, type: s } = t, c = gt(s), l = r + o;
564
+ if (o > tt) {
565
+ n.set(r, new c(await e.slice(r, l).arrayBuffer()));
566
+ continue;
567
+ }
568
+ (!i || r < a || l > a + i.byteLength) && (a = r, i = await e.slice(r, Math.min(e.size, r + tt)).arrayBuffer()), n.set(r, new c(i.slice(r - a, l - a)));
569
+ }
570
+ return n;
571
+ }
572
+ function vt(e) {
573
+ let t = [];
574
+ for (let n of e.nodes) n.instanceMatrix && t.push(n.instanceMatrix), n.instanceColor && t.push(n.instanceColor);
575
+ for (let n of e.geometries) {
576
+ for (let e of Object.values(n.attributes)) t.push(e.section);
577
+ n.index && t.push(n.index.section);
578
+ }
579
+ for (let n of e.textures) n.data && t.push(n.data.section);
580
+ return t;
581
+ }
582
+ function yt(e, t) {
583
+ let n = new _(e.get(t.section.offset), t.itemSize, t.normalized);
584
+ return n.name = t.name, n;
585
+ }
586
+ function bt(e, t) {
587
+ for (let n of rt) t[n] !== void 0 && (e[n] = t[n]);
588
+ e.repeat.fromArray(t.repeat), e.offset.fromArray(t.offset), e.center.fromArray(t.center), e.rotation = t.rotation, e.uuid = t.uuid, Object.assign(e.userData, t.userData), e.needsUpdate = !0;
589
+ }
590
+ async function xt(e, t, { loadTexture: n }) {
591
+ if (e?.v !== 2) throw Error("scene graph: unknown format");
592
+ let r = await _t(t, vt(e)), i = (e) => r.get(e.offset), a = {};
593
+ for (let t of e.textures) {
594
+ let e;
595
+ if (t.path) {
596
+ if (e = await n(t.path, t.loader), !e) throw Error(`scene graph: "${t.path}" is missing from the archive`);
597
+ e.userData[Ze] = t.path, e.userData[Qe] = t.loader;
598
+ } else e = new b(i(t.data.section), t.data.width, t.data.height);
599
+ bt(e, t), a[t.uuid] = e;
600
+ }
601
+ let o = new M().setTextures(a), s = {};
602
+ for (let t of e.materials) {
603
+ let e = o.parse(t);
604
+ for (let [n, r] of Object.entries(t.exactColors ?? {})) n === "diffuseTransmissionColor" && (e[n] = new y()), e[n]?.setRGB(r[0], r[1], r[2]);
605
+ t.exactIor !== void 0 && (e.ior = t.exactIor), t.diffuseTransmission !== void 0 && (e.diffuseTransmission = t.diffuseTransmission);
606
+ for (let [n, r] of Object.entries(t.engineTextures ?? {})) a[r] && (e[n] = a[r]);
607
+ s[t.uuid] = e;
608
+ }
609
+ let c = {};
610
+ for (let t of e.geometries) {
611
+ let e = new v();
612
+ e.uuid = t.uuid, e.name = t.name;
613
+ for (let [n, i] of Object.entries(t.attributes)) e.setAttribute(n, yt(r, i));
614
+ t.index && e.setIndex(yt(r, t.index));
615
+ for (let n of t.groups) e.addGroup(n.start, n.count, n.materialIndex);
616
+ e.setDrawRange(t.drawRange.start, t.drawRange.count), e.userData = t.userData, c[t.uuid] = e;
617
+ }
618
+ let l = [];
619
+ for (let t of e.nodes) {
620
+ let e = Array.isArray(t.material) ? t.material.map((e) => s[e]) : s[t.material], n;
621
+ switch (t.type) {
622
+ case "InstancedMesh":
623
+ n = new k(c[t.geometry], e, 1), n.instanceMatrix = new O(i(t.instanceMatrix), 16), n.count = t.count, t.instanceColor && (n.instanceColor = new O(i(t.instanceColor), 3));
624
+ break;
625
+ case "Mesh":
626
+ n = new ee(c[t.geometry], e);
627
+ break;
628
+ case "PerspectiveCamera":
629
+ n = new oe();
630
+ break;
631
+ case "OrthographicCamera":
632
+ n = new ae();
633
+ break;
634
+ case "Group":
635
+ n = new D();
636
+ break;
637
+ case "DirectionalLight":
638
+ n = new x();
639
+ break;
640
+ case "PointLight":
641
+ n = new se();
642
+ break;
643
+ case "SpotLight":
644
+ n = new fe();
645
+ break;
646
+ default: n = new ie();
647
+ }
648
+ n.uuid = t.uuid;
649
+ for (let e of at) n[e] = t[e];
650
+ if (n.position.fromArray(t.position), n.quaternion.fromArray(t.quaternion), n.scale.fromArray(t.scale), n.layers.mask = t.layers, n.userData = t.userData, t.matrix && n.matrix.fromArray(t.matrix), n.isCamera) {
651
+ for (let e of [
652
+ "fov",
653
+ "aspect",
654
+ "near",
655
+ "far",
656
+ "zoom",
657
+ "focus",
658
+ "filmGauge",
659
+ "filmOffset",
660
+ "left",
661
+ "right",
662
+ "top",
663
+ "bottom"
664
+ ]) t[e] !== void 0 && (n[e] = t[e]);
665
+ n.updateProjectionMatrix();
666
+ }
667
+ if (n.isLight) {
668
+ n.color.fromArray(t.color);
669
+ for (let e of it) t[e] !== void 0 && (n[e] = t[e]);
670
+ }
671
+ l.push(n), t.parent >= 0 && l[t.parent].add(n);
672
+ }
673
+ let u = new Map(l.map((e) => [e.uuid, e]));
674
+ return e.nodes.forEach((e, t) => {
675
+ e.target && u.has(e.target) && (l[t].target = u.get(e.target));
676
+ }), {
677
+ root: l[0],
678
+ environment: e.environment ? a[e.environment] : null,
679
+ animations: e.animations.map((e) => h.parse(e)),
680
+ stats: e.stats
681
+ };
682
+ }
683
+ //#endregion
684
+ //#region src/Processor/PBRT/buffers.js
685
+ var St = (e) => e.byteOffset === 0 && e.byteLength === e.buffer.byteLength && typeof e.buffer.transfer == "function" && !(typeof SharedArrayBuffer < "u" && e.buffer instanceof SharedArrayBuffer);
686
+ function H(e) {
687
+ e && St(e) && e.buffer.transfer(0);
688
+ }
689
+ function U(e, t) {
690
+ let n = e.constructor;
691
+ if (St(e)) return new n(e.buffer.transfer(t * n.BYTES_PER_ELEMENT));
692
+ let r = new n(t);
693
+ return r.set(t < e.length ? e.subarray(0, t) : e), r;
694
+ }
695
+ //#endregion
696
+ //#region src/Processor/PBRT/PBRTTokenizer.js
697
+ var W = {
698
+ STRING: "string",
699
+ NUMBER: "number",
700
+ WORD: "word",
701
+ LBRACKET: "[",
702
+ RBRACKET: "]"
703
+ }, Ct = 9, wt = 10, Tt = 12, Et = 13, Dt = 32, Ot = 34, kt = 35, At = 43, jt = 45, Mt = 46, Nt = 48, Pt = 57, Ft = 91, It = 93, Lt = 69, Rt = 101, zt = (e) => e === Dt || e === Ct || e === wt || e === Et || e === Tt, G = (e) => e >= Nt && e <= Pt, Bt = 15, Vt = 22, Ht = (() => {
704
+ let e = new Float64Array(Vt + 1);
705
+ for (let t = 0; t < e.length; t++) e[t] = 10 ** t;
706
+ return e;
707
+ })(), Ut = new TextDecoder();
708
+ function K(e, t, n) {
709
+ if (n - t === 0) return "";
710
+ let r = !0;
711
+ for (let i = t; i < n; i++) if (e[i] > 127) {
712
+ r = !1;
713
+ break;
714
+ }
715
+ if (!r) return Ut.decode(e.subarray(t, n));
716
+ let i = "";
717
+ for (let r = t; r < n; r++) i += String.fromCharCode(e[r]);
718
+ return i;
719
+ }
720
+ var Wt = class {
721
+ constructor(e) {
722
+ this.bytes = typeof e == "string" ? new TextEncoder().encode(e) : e, this.i = 0, this.n = this.bytes.length, this._ahead = null;
723
+ }
724
+ peek() {
725
+ return this._ahead === null && (this._ahead = this._scan()), this._ahead;
726
+ }
727
+ next() {
728
+ let e = this.peek();
729
+ return this._ahead = null, e;
730
+ }
731
+ _scan() {
732
+ let e = this.bytes, t = this.i, n = this.n;
733
+ for (;;) {
734
+ for (; t < n && zt(e[t]);) t++;
735
+ if (t < n && e[t] === kt) {
736
+ for (; t < n && e[t] !== wt;) t++;
737
+ continue;
738
+ }
739
+ break;
740
+ }
741
+ if (t >= n) return this.i = t, null;
742
+ let r = e[t];
743
+ if (r === Ft) return this.i = t + 1, { type: W.LBRACKET };
744
+ if (r === It) return this.i = t + 1, { type: W.RBRACKET };
745
+ if (r === Ot) {
746
+ let r = ++t;
747
+ for (; t < n && e[t] !== Ot;) t++;
748
+ if (t >= n) throw Error("PBRT tokenizer: unterminated string literal");
749
+ return this.i = t + 1, {
750
+ type: W.STRING,
751
+ value: K(e, r, t)
752
+ };
753
+ }
754
+ if (G(r) || r === Mt && G(e[t + 1]) || (r === jt || r === At) && (G(e[t + 1]) || e[t + 1] === Mt && G(e[t + 2]))) return this._scanNumber(t);
755
+ let i = t;
756
+ for (; t < n;) {
757
+ let n = e[t];
758
+ if (zt(n) || n === Ot || n === Ft || n === It || n === kt) break;
759
+ t++;
760
+ }
761
+ return this.i = t, {
762
+ type: W.WORD,
763
+ value: K(e, i, t)
764
+ };
765
+ }
766
+ _scanNumber(e) {
767
+ let t = this.bytes, n = this.n, r = e, i = !1;
768
+ t[r] === jt ? (i = !0, r++) : t[r] === At && r++;
769
+ let a = 0, o = 0, s = 0;
770
+ for (; r < n && G(t[r]);) a = a * 10 + (t[r] - Nt), o++, r++;
771
+ if (r < n && t[r] === Mt) for (r++; r < n && G(t[r]);) a = a * 10 + (t[r] - Nt), o++, s++, r++;
772
+ let c = 0;
773
+ if (r < n && (t[r] === Rt || t[r] === Lt)) {
774
+ let e = r + 1, i = !1;
775
+ if (e < n && (t[e] === jt || t[e] === At) && (i = t[e] === jt, e++), e < n && G(t[e])) {
776
+ let a = 0;
777
+ for (; e < n && G(t[e]);) a = a * 10 + (t[e] - Nt), e++;
778
+ c = i ? -a : a, r = e;
779
+ }
780
+ }
781
+ if (this.i = r, o === 0) throw Error(`PBRT tokenizer: invalid number "${K(t, e, r)}"`);
782
+ let l = c - s;
783
+ if (!(o <= Bt && l >= -Vt && l <= Vt)) {
784
+ let n = Number(K(t, e, r));
785
+ if (Number.isNaN(n)) throw Error(`PBRT tokenizer: invalid number "${K(t, e, r)}"`);
786
+ return {
787
+ type: W.NUMBER,
788
+ value: n
789
+ };
790
+ }
791
+ let u = l === 0 ? a : l > 0 ? a * Ht[l] : a / Ht[-l];
792
+ return i && (u = -u), {
793
+ type: W.NUMBER,
794
+ value: u
795
+ };
796
+ }
797
+ };
798
+ //#endregion
799
+ //#region src/Processor/PBRT/PBRTMath.js
800
+ function Gt() {
801
+ return [
802
+ 1,
803
+ 0,
804
+ 0,
805
+ 0,
806
+ 0,
807
+ 1,
808
+ 0,
809
+ 0,
810
+ 0,
811
+ 0,
812
+ 1,
813
+ 0,
814
+ 0,
815
+ 0,
816
+ 0,
817
+ 1
818
+ ];
819
+ }
820
+ function Kt(e) {
821
+ return e[0] * (e[5] * e[10] - e[6] * e[9]) - e[4] * (e[1] * e[10] - e[2] * e[9]) + e[8] * (e[1] * e[6] - e[2] * e[5]);
822
+ }
823
+ function qt(e, t, n, r) {
824
+ for (let i = 0; i < 4; i++) {
825
+ let a = r[i * 4], o = r[i * 4 + 1], s = r[i * 4 + 2], c = r[i * 4 + 3];
826
+ for (let r = 0; r < 4; r++) e[i * 4 + r] = t[n + r] * a + t[n + 4 + r] * o + t[n + 8 + r] * s + t[n + 12 + r] * c;
827
+ }
828
+ return e;
829
+ }
830
+ function q(e, t) {
831
+ let n = e[0], r = e[1], i = e[2], a = e[3], o = e[4], s = e[5], c = e[6], l = e[7], u = e[8], d = e[9], f = e[10], p = e[11], m = e[12], h = e[13], g = e[14], _ = e[15], v = t[0], y = t[1], b = t[2], x = t[3], S = t[4], C = t[5], w = t[6], T = t[7], E = t[8], D = t[9], O = t[10], k = t[11], A = t[12], j = t[13], M = t[14], N = t[15];
832
+ return [
833
+ n * v + o * y + u * b + m * x,
834
+ r * v + s * y + d * b + h * x,
835
+ i * v + c * y + f * b + g * x,
836
+ a * v + l * y + p * b + _ * x,
837
+ n * S + o * C + u * w + m * T,
838
+ r * S + s * C + d * w + h * T,
839
+ i * S + c * C + f * w + g * T,
840
+ a * S + l * C + p * w + _ * T,
841
+ n * E + o * D + u * O + m * k,
842
+ r * E + s * D + d * O + h * k,
843
+ i * E + c * D + f * O + g * k,
844
+ a * E + l * D + p * O + _ * k,
845
+ n * A + o * j + u * M + m * N,
846
+ r * A + s * j + d * M + h * N,
847
+ i * A + c * j + f * M + g * N,
848
+ a * A + l * j + p * M + _ * N
849
+ ];
850
+ }
851
+ function Jt(e, t, n) {
852
+ return [
853
+ 1,
854
+ 0,
855
+ 0,
856
+ 0,
857
+ 0,
858
+ 1,
859
+ 0,
860
+ 0,
861
+ 0,
862
+ 0,
863
+ 1,
864
+ 0,
865
+ e,
866
+ t,
867
+ n,
868
+ 1
869
+ ];
870
+ }
871
+ function Yt(e, t, n) {
872
+ return [
873
+ e,
874
+ 0,
875
+ 0,
876
+ 0,
877
+ 0,
878
+ t,
879
+ 0,
880
+ 0,
881
+ 0,
882
+ 0,
883
+ n,
884
+ 0,
885
+ 0,
886
+ 0,
887
+ 0,
888
+ 1
889
+ ];
890
+ }
891
+ function Xt(e, t, n, r) {
892
+ let i = Math.hypot(t, n, r) || 1;
893
+ t /= i, n /= i, r /= i;
894
+ let a = e * Math.PI / 180, o = Math.cos(a), s = Math.sin(a), c = 1 - o, l = c * t * t + o, u = c * t * n - s * r, d = c * t * r + s * n, f = c * t * n + s * r, p = c * n * n + o, m = c * n * r - s * t;
895
+ return [
896
+ l,
897
+ f,
898
+ c * t * r - s * n,
899
+ 0,
900
+ u,
901
+ p,
902
+ c * n * r + s * t,
903
+ 0,
904
+ d,
905
+ m,
906
+ c * r * r + o,
907
+ 0,
908
+ 0,
909
+ 0,
910
+ 0,
911
+ 1
912
+ ];
913
+ }
914
+ function Zt(e, t) {
915
+ return [
916
+ e[0] - t[0],
917
+ e[1] - t[1],
918
+ e[2] - t[2]
919
+ ];
920
+ }
921
+ function Qt(e, t) {
922
+ return [
923
+ e[1] * t[2] - e[2] * t[1],
924
+ e[2] * t[0] - e[0] * t[2],
925
+ e[0] * t[1] - e[1] * t[0]
926
+ ];
927
+ }
928
+ function $t(e) {
929
+ let t = Math.hypot(e[0], e[1], e[2]) || 1;
930
+ return [
931
+ e[0] / t,
932
+ e[1] / t,
933
+ e[2] / t
934
+ ];
935
+ }
936
+ function en(e, t, n) {
937
+ let r = $t(Zt(t, e)), i = $t(Qt($t(n), r)), a = Qt(r, i);
938
+ return [
939
+ i[0],
940
+ i[1],
941
+ i[2],
942
+ 0,
943
+ a[0],
944
+ a[1],
945
+ a[2],
946
+ 0,
947
+ r[0],
948
+ r[1],
949
+ r[2],
950
+ 0,
951
+ e[0],
952
+ e[1],
953
+ e[2],
954
+ 1
955
+ ];
956
+ }
957
+ function J(e) {
958
+ let t = e[0], n = e[1], r = e[2], i = e[3], a = e[4], o = e[5], s = e[6], c = e[7], l = e[8], u = e[9], d = e[10], f = e[11], p = e[12], m = e[13], h = e[14], g = e[15], _ = u * h * c - m * d * c + m * s * f - o * h * f - u * s * g + o * d * g, v = p * d * c - l * h * c - p * s * f + a * h * f + l * s * g - a * d * g, y = l * m * c - p * u * c + p * o * f - a * m * f - l * o * g + a * u * g, b = p * u * s - l * m * s - p * o * d + a * m * d + l * o * h - a * u * h, x = t * _ + n * v + r * y + i * b;
959
+ if (x === 0) return Gt();
960
+ let S = 1 / x;
961
+ return [
962
+ _ * S,
963
+ (m * d * i - u * h * i - m * r * f + n * h * f + u * r * g - n * d * g) * S,
964
+ (o * h * i - m * s * i + m * r * c - n * h * c - o * r * g + n * s * g) * S,
965
+ (u * s * i - o * d * i - u * r * c + n * d * c + o * r * f - n * s * f) * S,
966
+ v * S,
967
+ (l * h * i - p * d * i + p * r * f - t * h * f - l * r * g + t * d * g) * S,
968
+ (p * s * i - a * h * i - p * r * c + t * h * c + a * r * g - t * s * g) * S,
969
+ (a * d * i - l * s * i + l * r * c - t * d * c - a * r * f + t * s * f) * S,
970
+ y * S,
971
+ (p * u * i - l * m * i - p * n * f + t * m * f + l * n * g - t * u * g) * S,
972
+ (a * m * i - p * o * i + p * n * c - t * m * c - a * n * g + t * o * g) * S,
973
+ (l * o * i - a * u * i - l * n * c + t * u * c + a * n * f - t * o * f) * S,
974
+ b * S,
975
+ (l * m * r - p * u * r + p * n * d - t * m * d - l * n * h + t * u * h) * S,
976
+ (p * o * r - a * m * r - p * n * s + t * m * s + a * n * h - t * o * h) * S,
977
+ (a * u * r - l * o * r + l * n * s - t * u * s - a * n * d + t * o * d) * S
978
+ ];
979
+ }
980
+ //#endregion
981
+ //#region src/Processor/PBRT/PBRTParser.js
982
+ var tn = 8, nn = 1, rn = 2, Y = 3, an = new Set([
983
+ "Identity",
984
+ "Transform",
985
+ "CoordinateSystem",
986
+ "CoordSysTransform",
987
+ "ActiveTransform",
988
+ "TransformTimes",
989
+ "Camera",
990
+ "Film",
991
+ "WorldBegin",
992
+ "MakeNamedMaterial",
993
+ "Texture",
994
+ "MakeNamedMedium",
995
+ "LightSource",
996
+ "AreaLightSource",
997
+ "ObjectBegin",
998
+ "ObjectEnd",
999
+ "ObjectInstance"
1000
+ ]);
1001
+ function on(e, t) {
1002
+ if (e === t) return !0;
1003
+ for (let n = 0; n < 16; n++) if (e[n] !== t[n]) return !1;
1004
+ return !0;
1005
+ }
1006
+ var sn = {
1007
+ integer: Int32Array,
1008
+ point3: Float32Array,
1009
+ point2: Float32Array,
1010
+ vector3: Float32Array,
1011
+ vector2: Float32Array,
1012
+ vector: Float32Array,
1013
+ normal: Float32Array,
1014
+ rgb: Float32Array
1015
+ }, cn = class {
1016
+ constructor(e = {}) {
1017
+ this.releaseInclude = e.releaseInclude || null, this.resolveInclude = e.resolveInclude || (() => {
1018
+ throw Error("PBRTParser: Include used but no resolveInclude provided");
1019
+ }), this.maxPlacements = e.maxPlacements ?? Infinity, this.instanceIncludes = e.instanceIncludes !== !1, this._includes = /* @__PURE__ */ new Map(), this._stateIds = /* @__PURE__ */ new WeakMap(), this._nextStateId = 1, this._effects = 0, this._movedShapes = !1, this.ir = {
1020
+ film: null,
1021
+ camera: null,
1022
+ namedMaterials: /* @__PURE__ */ new Map(),
1023
+ namedTextures: /* @__PURE__ */ new Map(),
1024
+ shapes: [],
1025
+ lights: [],
1026
+ instances: /* @__PURE__ */ new Map(),
1027
+ instanceCount: 0,
1028
+ skippedInstances: 0,
1029
+ objects: /* @__PURE__ */ new Map(),
1030
+ media: /* @__PURE__ */ new Map(),
1031
+ transformTimes: {
1032
+ start: 0,
1033
+ end: 1
1034
+ },
1035
+ hasMotion: !1,
1036
+ warnings: []
1037
+ }, this.ctm = Gt(), this.ctmEnd = null, this.activeTransform = Y, this._ctmSource = null, this._ctmValue = null, this.state = {
1038
+ material: null,
1039
+ areaLight: null,
1040
+ reverseOrientation: !1,
1041
+ interior: null
1042
+ }, this.attributeStack = [], this.transformStack = [], this.coordSystems = /* @__PURE__ */ new Map(), this.currentObject = null, this.dirStack = [""], this.stream = null, this._warnedUnknown = /* @__PURE__ */ new Set();
1043
+ }
1044
+ async parse(e, t = "") {
1045
+ this.dirStack = [t], await this._run(new Wt(e)), this._movedShapes && (this.ir.shapes = this.ir.shapes.filter(Boolean));
1046
+ for (let e of this.ir.instances.values()) e.matrices = U(e.matrices, e.count * 16), e.matricesEnd &&= U(e.matricesEnd, e.count * 16);
1047
+ return this.ir;
1048
+ }
1049
+ _addInstance(e, t = this.ctm) {
1050
+ if (this.ir.instanceCount >= this.maxPlacements) {
1051
+ this.ir.skippedInstances++;
1052
+ return;
1053
+ }
1054
+ let n = this.ir.instances.get(e);
1055
+ n || this.ir.instances.set(e, n = {
1056
+ name: e,
1057
+ count: 0,
1058
+ matrices: new Float32Array(512),
1059
+ matricesEnd: null
1060
+ });
1061
+ let r = (n.count + 1) * 16;
1062
+ if (r > n.matrices.length) {
1063
+ let e = Math.max(r, n.matrices.length * 2);
1064
+ n.matrices = U(n.matrices, e), n.matricesEnd &&= U(n.matricesEnd, e);
1065
+ }
1066
+ let i = n.count * 16;
1067
+ for (let e = 0; e < 16; e++) n.matrices[i + e] = t[e];
1068
+ let a = this._ctmEndSnapshot();
1069
+ if (a && !n.matricesEnd && (n.matricesEnd = n.matrices.slice()), n.matricesEnd) {
1070
+ let e = a || t;
1071
+ for (let t = 0; t < 16; t++) n.matricesEnd[i + t] = e[t];
1072
+ }
1073
+ n.count++, this.ir.instanceCount++;
1074
+ }
1075
+ _ctmSnapshot() {
1076
+ return this._ctmSource !== this.ctm && (this._ctmSource = this.ctm, this._ctmValue = this.ctm.slice()), this._ctmValue;
1077
+ }
1078
+ _ctmEndSnapshot() {
1079
+ return this.ctmEnd === null || on(this.ctmEnd, this.ctm) ? null : (this.ir.hasMotion = !0, this.ctmEnd.slice());
1080
+ }
1081
+ _concat(e) {
1082
+ if (this.activeTransform === Y && this.ctmEnd === null) {
1083
+ this.ctm = q(this.ctm, e);
1084
+ return;
1085
+ }
1086
+ let t = this.ctmEnd ?? this.ctm;
1087
+ this.ctmEnd = this.activeTransform & rn ? q(t, e) : t, this.activeTransform & nn && (this.ctm = q(this.ctm, e));
1088
+ }
1089
+ _setCTM(e) {
1090
+ if (this.activeTransform === Y) {
1091
+ this.ctm = e, this.ctmEnd = null;
1092
+ return;
1093
+ }
1094
+ let t = this.ctmEnd ?? this.ctm;
1095
+ this.ctmEnd = this.activeTransform & rn ? e : t, this.activeTransform & nn && (this.ctm = e);
1096
+ }
1097
+ _pushGraphicsState() {
1098
+ this.attributeStack.push({
1099
+ ctm: this.ctm,
1100
+ ctmEnd: this.ctmEnd,
1101
+ activeTransform: this.activeTransform,
1102
+ material: this.state.material,
1103
+ areaLight: this.state.areaLight,
1104
+ reverseOrientation: this.state.reverseOrientation,
1105
+ interior: this.state.interior
1106
+ });
1107
+ }
1108
+ _popGraphicsState() {
1109
+ let e = this.attributeStack.pop();
1110
+ e && (this.ctm = e.ctm, this.ctmEnd = e.ctmEnd, this.activeTransform = e.activeTransform, this.state = {
1111
+ material: e.material,
1112
+ areaLight: e.areaLight,
1113
+ reverseOrientation: e.reverseOrientation,
1114
+ interior: e.interior
1115
+ });
1116
+ }
1117
+ _peek() {
1118
+ return this.stream.peek();
1119
+ }
1120
+ _next() {
1121
+ return this.stream.next();
1122
+ }
1123
+ _expectNumber(e) {
1124
+ let t = this._next();
1125
+ if (!t || t.type !== W.NUMBER) throw Error(`PBRT parser: expected number for ${e}, got ${t ? t.value : "EOF"}`);
1126
+ return t.value;
1127
+ }
1128
+ _expectString(e) {
1129
+ let t = this._next();
1130
+ if (!t || t.type !== W.STRING) throw Error(`PBRT parser: expected string for ${e}, got ${t ? t.value : "EOF"}`);
1131
+ return t.value;
1132
+ }
1133
+ _readNumbers(e) {
1134
+ let t = [];
1135
+ for (let n = 0; n < e; n++) t.push(this._expectNumber("matrix/transform"));
1136
+ return t;
1137
+ }
1138
+ _readBracketedOrBareNumbers(e) {
1139
+ if (this._peek() && this._peek().type === W.LBRACKET) {
1140
+ this._next();
1141
+ let e = [];
1142
+ for (; this._peek() && this._peek().type !== W.RBRACKET;) e.push(this._expectNumber("transform element"));
1143
+ return this._next(), e;
1144
+ }
1145
+ return this._readNumbers(e);
1146
+ }
1147
+ _parseParams() {
1148
+ let e = {};
1149
+ for (; this._peek() && this._peek().type === W.STRING;) {
1150
+ let t = this._next().value.trim().split(/\s+/), n = t[0], r = t[1] === void 0 ? t[0] : t[1];
1151
+ e[r] = {
1152
+ type: n,
1153
+ value: this._parseParamValue(n)
1154
+ };
1155
+ }
1156
+ return e;
1157
+ }
1158
+ _parseParamValue(e) {
1159
+ let t = this._peek() && this._peek().type === W.LBRACKET;
1160
+ t && this._next();
1161
+ let n = () => {
1162
+ let e = this._peek();
1163
+ return t ? e && e.type !== W.RBRACKET : !1;
1164
+ };
1165
+ if (!t) return [this._coerceValueToken(this._next())];
1166
+ if (this._peek() && this._peek().type === W.NUMBER) {
1167
+ let t = [];
1168
+ for (; n() && t.length < tn;) {
1169
+ let e = this._next();
1170
+ if (e.type !== W.NUMBER) return this._finishMixedValue(t, t.length, e);
1171
+ t.push(e.value);
1172
+ }
1173
+ if (!n()) return this._next(), t;
1174
+ let r = new (sn[e] || Float64Array)(tn * 4);
1175
+ r.set(t);
1176
+ let i = t.length;
1177
+ for (; n();) {
1178
+ let e = this._next();
1179
+ if (e.type !== W.NUMBER) return this._finishMixedValue(r, i, e);
1180
+ i === r.length && (r = U(r, r.length * 2)), r[i++] = e.value;
1181
+ }
1182
+ return this._next(), i === r.length ? r : U(r, i);
1183
+ }
1184
+ let r = [];
1185
+ for (; n();) r.push(this._coerceValueToken(this._next()));
1186
+ return this._next(), r;
1187
+ }
1188
+ _finishMixedValue(e, t, n) {
1189
+ let r = [];
1190
+ for (let n = 0; n < t; n++) r.push(e[n]);
1191
+ for (r.push(this._coerceValueToken(n)); this._peek() && this._peek().type !== W.RBRACKET;) r.push(this._coerceValueToken(this._next()));
1192
+ return this._next(), r;
1193
+ }
1194
+ _coerceValueToken(e) {
1195
+ if (!e) throw Error("PBRT parser: unexpected EOF in parameter value");
1196
+ if (e.type === W.NUMBER || e.type === W.STRING) return e.value;
1197
+ if (e.type === W.WORD) return e.value === "true" ? !0 : e.value === "false" ? !1 : e.value;
1198
+ throw Error(`PBRT parser: unexpected token in parameter value: ${e.type}`);
1199
+ }
1200
+ async _run(e) {
1201
+ let t = this.stream;
1202
+ this.stream = e;
1203
+ for (let e = this._next(); e !== null; e = this._next()) {
1204
+ if (e.type !== W.WORD) throw Error(`PBRT parser: expected directive, got ${e.type} ${e.value ?? ""}`);
1205
+ an.has(e.value) && this._effects++;
1206
+ let t = this._directive(e.value);
1207
+ t !== void 0 && await t;
1208
+ }
1209
+ this.stream = t;
1210
+ }
1211
+ _directive(e) {
1212
+ switch (e) {
1213
+ case "Identity":
1214
+ this._setCTM(Gt());
1215
+ break;
1216
+ case "Translate": {
1217
+ let [e, t, n] = this._readNumbers(3);
1218
+ this._concat(Jt(e, t, n));
1219
+ break;
1220
+ }
1221
+ case "Scale": {
1222
+ let [e, t, n] = this._readNumbers(3);
1223
+ this._concat(Yt(e, t, n));
1224
+ break;
1225
+ }
1226
+ case "Rotate": {
1227
+ let [e, t, n, r] = this._readNumbers(4);
1228
+ this._concat(Xt(e, t, n, r));
1229
+ break;
1230
+ }
1231
+ case "LookAt": {
1232
+ let e = this._readNumbers(9), t = en([
1233
+ e[0],
1234
+ e[1],
1235
+ e[2]
1236
+ ], [
1237
+ e[3],
1238
+ e[4],
1239
+ e[5]
1240
+ ], [
1241
+ e[6],
1242
+ e[7],
1243
+ e[8]
1244
+ ]);
1245
+ this._concat(J(t));
1246
+ break;
1247
+ }
1248
+ case "Transform":
1249
+ this._setCTM(this._readBracketedOrBareNumbers(16));
1250
+ break;
1251
+ case "ConcatTransform":
1252
+ this._concat(this._readBracketedOrBareNumbers(16));
1253
+ break;
1254
+ case "CoordinateSystem":
1255
+ this.coordSystems.set(this._expectString("CoordinateSystem"), {
1256
+ ctm: this.ctm,
1257
+ ctmEnd: this.ctmEnd
1258
+ });
1259
+ break;
1260
+ case "CoordSysTransform": {
1261
+ let e = this.coordSystems.get(this._expectString("CoordSysTransform"));
1262
+ e && (this.ctm = e.ctm, this.ctmEnd = e.ctmEnd);
1263
+ break;
1264
+ }
1265
+ case "Camera": {
1266
+ let e = this._expectString("Camera type"), t = this._parseParams();
1267
+ this.ir.camera = {
1268
+ type: e,
1269
+ params: t,
1270
+ cameraToWorld: J(this.ctm)
1271
+ };
1272
+ let n = this._ctmEndSnapshot();
1273
+ n && (this.ir.camera.cameraToWorldEnd = J(n));
1274
+ break;
1275
+ }
1276
+ case "Film": {
1277
+ this._expectString("Film type");
1278
+ let e = this._parseParams();
1279
+ this.ir.film = {
1280
+ xresolution: this._num(e.xresolution, 1280),
1281
+ yresolution: this._num(e.yresolution, 720),
1282
+ filename: this._str(e.filename, null)
1283
+ };
1284
+ break;
1285
+ }
1286
+ case "Integrator":
1287
+ case "Sampler":
1288
+ case "PixelFilter":
1289
+ case "Filter":
1290
+ case "Accelerator":
1291
+ case "ColorSpace":
1292
+ case "Option":
1293
+ this._skipTypeAndParams();
1294
+ break;
1295
+ case "WorldBegin":
1296
+ this.ctm = Gt(), this.ctmEnd = null, this.activeTransform = Y, this.state = {
1297
+ material: null,
1298
+ areaLight: null,
1299
+ reverseOrientation: !1,
1300
+ interior: null
1301
+ };
1302
+ break;
1303
+ case "WorldEnd": break;
1304
+ case "AttributeBegin":
1305
+ this._pushGraphicsState();
1306
+ break;
1307
+ case "AttributeEnd":
1308
+ this._popGraphicsState();
1309
+ break;
1310
+ case "TransformBegin":
1311
+ this.transformStack.push({
1312
+ ctm: this.ctm,
1313
+ ctmEnd: this.ctmEnd,
1314
+ activeTransform: this.activeTransform
1315
+ });
1316
+ break;
1317
+ case "TransformEnd": {
1318
+ let e = this.transformStack.pop();
1319
+ e && (this.ctm = e.ctm, this.ctmEnd = e.ctmEnd, this.activeTransform = e.activeTransform);
1320
+ break;
1321
+ }
1322
+ case "ReverseOrientation":
1323
+ this.state.reverseOrientation = !this.state.reverseOrientation;
1324
+ break;
1325
+ case "Attribute":
1326
+ this._expectString("Attribute target"), this._parseParams();
1327
+ break;
1328
+ case "ActiveTransform": {
1329
+ let e = this._next()?.value;
1330
+ e === "StartTime" ? this.activeTransform = nn : e === "EndTime" ? this.activeTransform = rn : e === "All" ? this.activeTransform = Y : this._warn(`ActiveTransform "${e}" not recognised`);
1331
+ break;
1332
+ }
1333
+ case "TransformTimes": {
1334
+ let [e, t] = this._readNumbers(2);
1335
+ this.ir.transformTimes = {
1336
+ start: e,
1337
+ end: t
1338
+ };
1339
+ break;
1340
+ }
1341
+ case "MediumInterface": {
1342
+ let e = this._peek()?.type === W.STRING ? this._next().value : "";
1343
+ this._peek()?.type === W.STRING && this._next(), this.state.interior = e || null;
1344
+ break;
1345
+ }
1346
+ case "MakeNamedMedium": {
1347
+ let e = this._expectString("MakeNamedMedium name");
1348
+ this.ir.media.set(e, this._parseParams());
1349
+ break;
1350
+ }
1351
+ case "Material": {
1352
+ let e = this._expectString("Material type"), t = this._parseParams();
1353
+ this.state.material = {
1354
+ type: e,
1355
+ params: t
1356
+ };
1357
+ break;
1358
+ }
1359
+ case "MakeNamedMaterial": {
1360
+ let e = this._expectString("MakeNamedMaterial name"), t = this._parseParams(), n = this._str(t.type, "diffuse");
1361
+ this.ir.namedMaterials.set(e, {
1362
+ type: n,
1363
+ params: t
1364
+ });
1365
+ break;
1366
+ }
1367
+ case "NamedMaterial": {
1368
+ let e = this._expectString("NamedMaterial name"), t = this.ir.namedMaterials.get(e);
1369
+ this.state.material = t || {
1370
+ type: "diffuse",
1371
+ params: {},
1372
+ _missingRef: e
1373
+ }, t || this._warn(`NamedMaterial "${e}" referenced before definition`);
1374
+ break;
1375
+ }
1376
+ case "Texture": {
1377
+ let e = this._expectString("Texture name"), t = this._expectString("Texture data type"), n = this._expectString("Texture class"), r = this._parseParams();
1378
+ this.ir.namedTextures.set(e, {
1379
+ dataType: t,
1380
+ class: n,
1381
+ params: r
1382
+ });
1383
+ break;
1384
+ }
1385
+ case "AreaLightSource": {
1386
+ let e = this._expectString("AreaLightSource type"), t = this._parseParams();
1387
+ this.state.areaLight = {
1388
+ type: e,
1389
+ params: t
1390
+ };
1391
+ break;
1392
+ }
1393
+ case "LightSource": {
1394
+ let e = this._expectString("LightSource type"), t = this._parseParams();
1395
+ this.ctmEnd !== null && !on(this.ctmEnd, this.ctm) && this._warnOnce("animated-light", "animated LightSource transforms are not supported — using the start transform"), this.ir.lights.push({
1396
+ type: e,
1397
+ params: t,
1398
+ ctm: this._ctmSnapshot()
1399
+ });
1400
+ break;
1401
+ }
1402
+ case "Shape": {
1403
+ let e = this._expectString("Shape type"), t = this._parseParams(), n = t.alpha;
1404
+ if (n?.type === "float" && n.value[0] <= 0) break;
1405
+ n && !(n.type === "float" && n.value[0] >= 1) && this._warnOnce("alpha", "shape \"alpha\" between 0 and 1, or as a texture, is not supported — drawn opaque");
1406
+ let r = {
1407
+ type: e,
1408
+ params: t,
1409
+ ctm: this._ctmSnapshot(),
1410
+ material: this.state.material,
1411
+ areaLight: this.state.areaLight,
1412
+ reverseOrientation: this.state.reverseOrientation,
1413
+ interior: this.state.interior
1414
+ }, i = this.currentObject === null ? this._ctmEndSnapshot() : null;
1415
+ i ? r.ctmEnd = i : this.currentObject !== null && this.ctmEnd !== null && !on(this.ctmEnd, this.ctm) && this._warnOnce("animated-template", "animated transforms inside ObjectBegin are not supported — using the start transform"), this._emitShape(r);
1416
+ break;
1417
+ }
1418
+ case "ObjectBegin": {
1419
+ let e = this._expectString("ObjectBegin name");
1420
+ this._pushGraphicsState(), this.currentObject = e, this.ir.objects.has(e) || this.ir.objects.set(e, []);
1421
+ break;
1422
+ }
1423
+ case "ObjectEnd":
1424
+ this.currentObject = null, this._popGraphicsState();
1425
+ break;
1426
+ case "ObjectInstance": {
1427
+ let e = this._expectString("ObjectInstance name");
1428
+ this._addInstance(e);
1429
+ break;
1430
+ }
1431
+ case "Include":
1432
+ case "Import": {
1433
+ let t = this._expectString(e);
1434
+ return this._include(t);
1435
+ }
1436
+ default:
1437
+ this._warnUnknown(e), this._parseParams();
1438
+ break;
1439
+ }
1440
+ }
1441
+ _emitShape(e) {
1442
+ this.currentObject === null ? this.ir.shapes.push(e) : this.ir.objects.get(this.currentObject).push(e);
1443
+ }
1444
+ async _include(e) {
1445
+ let t = this.dirStack[this.dirStack.length - 1], n = this._includeKey(e, t);
1446
+ if (n !== null && this._placeRepeat(n, e)) return;
1447
+ let r = await this.resolveInclude(e, t);
1448
+ if (r == null) {
1449
+ this._warn(`Include target not found: ${e}`);
1450
+ return;
1451
+ }
1452
+ let i = n === null ? null : this._includeState(), a = e.includes("/") ? e.slice(0, e.lastIndexOf("/")) : "";
1453
+ this.dirStack.push(a);
1454
+ try {
1455
+ await this._run(new Wt(r));
1456
+ } finally {
1457
+ this.dirStack.pop(), this.releaseInclude?.(e, t, r);
1458
+ }
1459
+ i && this._sameIncludeState(i) && !this._includes.has(n) && this._includes.set(n, {
1460
+ start: i.shapes,
1461
+ end: this.ir.shapes.length,
1462
+ ctm: i.ctm.slice(),
1463
+ template: null
1464
+ });
1465
+ }
1466
+ _includeKey(e, t) {
1467
+ return !this.instanceIncludes || this.currentObject !== null || this.ctmEnd !== null || this.activeTransform !== Y || this.state.areaLight ? null : `${t}\0${e}\0${this._stateId(this.state.material)}\0${this.state.reverseOrientation}\0${this.state.interior}`;
1468
+ }
1469
+ _stateId(e) {
1470
+ if (!e) return 0;
1471
+ let t = this._stateIds.get(e);
1472
+ return t === void 0 && this._stateIds.set(e, t = this._nextStateId++), t;
1473
+ }
1474
+ _includeState() {
1475
+ return {
1476
+ shapes: this.ir.shapes.length,
1477
+ effects: this._effects,
1478
+ ctm: this.ctm,
1479
+ attributes: this.attributeStack.length,
1480
+ transforms: this.transformStack.length,
1481
+ material: this.state.material,
1482
+ reverseOrientation: this.state.reverseOrientation,
1483
+ interior: this.state.interior
1484
+ };
1485
+ }
1486
+ _sameIncludeState(e) {
1487
+ return this._effects === e.effects && this.ctmEnd === null && this.activeTransform === Y && on(this.ctm, e.ctm) && this.attributeStack.length === e.attributes && this.transformStack.length === e.transforms && this.state.material === e.material && this.state.areaLight === null && this.state.reverseOrientation === e.reverseOrientation && this.state.interior === e.interior;
1488
+ }
1489
+ _placeRepeat(e, t) {
1490
+ let n = this._includes.get(e);
1491
+ if (!n) return !1;
1492
+ if (n.template === null) {
1493
+ let { start: r, end: i } = n;
1494
+ if (i === r || this.ir.instanceCount + 2 > this.maxPlacements) return !1;
1495
+ for (let t = r; t < i; t++) if (!this.ir.shapes[t]) return this._includes.delete(e), !1;
1496
+ let a = `${t}#${this.ir.objects.size}`, o = J(n.ctm), s = [], c = null, l = null;
1497
+ for (let e = r; e < i; e++) {
1498
+ let t = this.ir.shapes[e];
1499
+ t.ctm !== c && (l = q(o, c = t.ctm)), t.relativeCTM = l, s.push(t), this.ir.shapes[e] = null;
1500
+ }
1501
+ this.ir.objects.set(a, s), this._addInstance(a, n.ctm), n.template = a, this._movedShapes = !0;
1502
+ }
1503
+ return this._addInstance(n.template), !0;
1504
+ }
1505
+ _skipTypeAndParams() {
1506
+ this._peek() && this._peek().type === W.STRING && this._next(), this._parseParams();
1507
+ }
1508
+ _skipNamedAndParams() {
1509
+ this._peek() && this._peek().type === W.STRING && this._next(), this._parseParams();
1510
+ }
1511
+ _num(e, t) {
1512
+ return e && e.value.length ? e.value[0] : t;
1513
+ }
1514
+ _str(e, t) {
1515
+ return e && e.value.length ? e.value[0] : t;
1516
+ }
1517
+ _warn(e) {
1518
+ this.ir.warnings.push(e);
1519
+ }
1520
+ _warnOnce(e, t) {
1521
+ this._warnedUnknown.has(e) || (this._warnedUnknown.add(e), this._warn(t));
1522
+ }
1523
+ _warnUnknown(e) {
1524
+ this._warnedUnknown.has(e) || (this._warnedUnknown.add(e), this._warn(`Unsupported directive ignored: ${e}`));
1525
+ }
1526
+ }, ln = {
1527
+ au: [
1528
+ 1,
1529
+ .78,
1530
+ .34
1531
+ ],
1532
+ gold: [
1533
+ 1,
1534
+ .78,
1535
+ .34
1536
+ ],
1537
+ cu: [
1538
+ .95,
1539
+ .64,
1540
+ .54
1541
+ ],
1542
+ copper: [
1543
+ .95,
1544
+ .64,
1545
+ .54
1546
+ ],
1547
+ ag: [
1548
+ .97,
1549
+ .96,
1550
+ .91
1551
+ ],
1552
+ silver: [
1553
+ .97,
1554
+ .96,
1555
+ .91
1556
+ ],
1557
+ al: [
1558
+ .91,
1559
+ .92,
1560
+ .92
1561
+ ],
1562
+ aluminium: [
1563
+ .91,
1564
+ .92,
1565
+ .92
1566
+ ],
1567
+ aluminum: [
1568
+ .91,
1569
+ .92,
1570
+ .92
1571
+ ],
1572
+ mgo: [
1573
+ .9,
1574
+ .9,
1575
+ .9
1576
+ ],
1577
+ tio2: [
1578
+ .9,
1579
+ .9,
1580
+ .9
1581
+ ]
1582
+ }, un = [
1583
+ .92,
1584
+ .92,
1585
+ .92
1586
+ ];
1587
+ function dn(e) {
1588
+ let t = Math.max(1e3, Math.min(4e4, e)) / 100, n, r, i;
1589
+ t <= 66 ? (n = 255, r = 99.47 * Math.log(t) - 161.12) : (n = 329.7 * (t - 60) ** -.1332, r = 288.12 * (t - 60) ** -.0755), i = t >= 66 ? 255 : t <= 19 ? 0 : 138.52 * Math.log(t - 10) - 305.04;
1590
+ let a = (e) => Math.max(0, Math.min(255, e)) / 255;
1591
+ return [
1592
+ a(n) ** 2.2,
1593
+ a(r) ** 2.2,
1594
+ a(i) ** 2.2
1595
+ ];
1596
+ }
1597
+ function X(e, t, n) {
1598
+ let r = e[t];
1599
+ return r && typeof r.value[0] == "number" ? r.value[0] : n;
1600
+ }
1601
+ function Z(e, t, n) {
1602
+ let r = e[t];
1603
+ return r && typeof r.value[0] == "string" ? r.value[0] : n;
1604
+ }
1605
+ function fn(e, t, n) {
1606
+ let r = e[t]?.value?.[0];
1607
+ return r === void 0 ? n : r === !0 || r === "true";
1608
+ }
1609
+ async function Q(e, t, n, r = null) {
1610
+ let i = e[t];
1611
+ if (!i) return {
1612
+ rgb: r,
1613
+ texture: null
1614
+ };
1615
+ if (i.type === "texture") {
1616
+ let e = i.value[0], t = await n.resolveNamedTexture(e);
1617
+ return t && t.texture ? {
1618
+ rgb: t.constant ?? null,
1619
+ texture: t.texture
1620
+ } : t && t.constant ? {
1621
+ rgb: t.constant,
1622
+ texture: null
1623
+ } : (n.warn(`texture "${e}" could not be resolved`), {
1624
+ rgb: r,
1625
+ texture: null
1626
+ });
1627
+ }
1628
+ if (i.type === "rgb" || i.type === "color") return {
1629
+ rgb: [
1630
+ i.value[0],
1631
+ i.value[1],
1632
+ i.value[2]
1633
+ ],
1634
+ texture: null
1635
+ };
1636
+ if (i.type === "float") {
1637
+ let e = i.value[0];
1638
+ return {
1639
+ rgb: [
1640
+ e,
1641
+ e,
1642
+ e
1643
+ ],
1644
+ texture: null
1645
+ };
1646
+ }
1647
+ if (i.type === "blackbody") return {
1648
+ rgb: dn(i.value[0]),
1649
+ texture: null
1650
+ };
1651
+ if (i.type === "spectrum") {
1652
+ if (typeof i.value[0] == "string") {
1653
+ let e = pn(i.value[0]);
1654
+ return e && ln[e] ? {
1655
+ rgb: ln[e].slice(),
1656
+ texture: null
1657
+ } : (n.warn(`named spectrum "${i.value[0]}" approximated to gray`), {
1658
+ rgb: [
1659
+ .5,
1660
+ .5,
1661
+ .5
1662
+ ],
1663
+ texture: null
1664
+ });
1665
+ }
1666
+ let e = 0, t = 0;
1667
+ for (let n = 1; n < i.value.length; n += 2) e += i.value[n], t++;
1668
+ let r = t ? e / t : .5;
1669
+ return {
1670
+ rgb: [
1671
+ r,
1672
+ r,
1673
+ r
1674
+ ],
1675
+ texture: null
1676
+ };
1677
+ }
1678
+ return {
1679
+ rgb: r,
1680
+ texture: null
1681
+ };
1682
+ }
1683
+ function pn(e) {
1684
+ let t = e.toLowerCase().match(/metal-([a-z]+)/);
1685
+ if (t) return t[1];
1686
+ for (let t of Object.keys(ln)) if (e.toLowerCase().includes(t)) return t;
1687
+ return null;
1688
+ }
1689
+ async function mn(e, t, n) {
1690
+ let r = e[t];
1691
+ return r ? typeof r.value[0] == "number" ? r.value[0] : r.type === "texture" ? await n.floatTextureMean?.(r.value[0]) ?? null : null : null;
1692
+ }
1693
+ async function hn(e, t, n, r = "") {
1694
+ let i = e.remaproughness?.value?.[0], a = (e) => i === !1 || i === "false" ? e : Math.sqrt(Math.max(e, 0)), o = null, s = await mn(e, `${r}roughness`, n);
1695
+ if (s !== null) o = a(s);
1696
+ else {
1697
+ let t = await mn(e, `${r}uroughness`, n), i = await mn(e, `${r}vroughness`, n);
1698
+ t !== null && (o = i === null ? a(t) : (a(t) + a(i)) / 2);
1699
+ }
1700
+ return o === null ? t : Math.sqrt(Math.max(o, 0));
1701
+ }
1702
+ async function gn(e, t, n, r, i, a) {
1703
+ let o = await Q(t, "reflectance", n, null), s = t[`${r}eta`], c = t[`${r}k`], l = s && s.type === "spectrum" && typeof s.value[0] == "string";
1704
+ if (o.rgb || o.texture) i(o);
1705
+ else if (s && c && !l) {
1706
+ let e = (await Q(t, `${r}eta`, n, [
1707
+ .2,
1708
+ .92,
1709
+ 1.1
1710
+ ])).rgb, i = (await Q(t, `${r}k`, n, [
1711
+ 3.9,
1712
+ 2.45,
1713
+ 2.14
1714
+ ])).rgb, o = (e, t) => ((e - 1) ** 2 + t ** 2) / ((e + 1) ** 2 + t ** 2);
1715
+ a([
1716
+ o(e[0], i[0]),
1717
+ o(e[1], i[1]),
1718
+ o(e[2], i[2])
1719
+ ]);
1720
+ } else a(s ? (await Q(t, `${r}eta`, n, un)).rgb || un : ln.cu);
1721
+ e.metalness = 1;
1722
+ }
1723
+ async function _n(e, t) {
1724
+ let n = e?.type || "diffuse", r = e?.params || {}, i = new te({
1725
+ side: S,
1726
+ roughness: 1,
1727
+ metalness: 0
1728
+ }), a = (e) => {
1729
+ e && i.color.setRGB(e[0], e[1], e[2]);
1730
+ }, o = (e) => {
1731
+ a(e.rgb), e.texture && (i.map = e.texture, e.rgb || i.color.setRGB(1, 1, 1));
1732
+ };
1733
+ switch (n) {
1734
+ case "diffuse":
1735
+ o(await Q(r, "reflectance", t, [
1736
+ .5,
1737
+ .5,
1738
+ .5
1739
+ ])), i.roughness = 1, i.metalness = 0, i.specularIntensity = 0;
1740
+ break;
1741
+ case "conductor":
1742
+ case "metal":
1743
+ await gn(i, r, t, "", o, a), i.roughness = await hn(r, .1, t);
1744
+ break;
1745
+ case "coatedconductor":
1746
+ await gn(i, r, t, "conductor.", o, a), i.roughness = await hn(r, 0, t, "conductor."), i.clearcoat = 1, i.clearcoatRoughness = await hn(r, 0, t, "interface.");
1747
+ break;
1748
+ case "dielectric":
1749
+ case "thindielectric":
1750
+ i.transmission = 1, i.metalness = 0, i.color.setRGB(1, 1, 1), i.ior = X(r, "eta", 1.5), i.roughness = await hn(r, 0, t), i.thickness = n === "thindielectric" ? 0 : X(r, "thickness", 0);
1751
+ break;
1752
+ case "coateddiffuse":
1753
+ o(await Q(r, "reflectance", t, [
1754
+ .5,
1755
+ .5,
1756
+ .5
1757
+ ])), i.roughness = .6, i.metalness = 0, i.clearcoat = 1, i.clearcoatRoughness = await hn(r, 0, t);
1758
+ break;
1759
+ case "diffusetransmission": {
1760
+ let e = X(r, "scale", 1), n = (t) => Math.min(1, Math.max(0, e * t)), a = await Q(r, "transmittance", t, [
1761
+ .25,
1762
+ .25,
1763
+ .25
1764
+ ]), s = a.rgb ? a.rgb.map(n) : [
1765
+ .5,
1766
+ .5,
1767
+ .5
1768
+ ], c = Math.min(.99, Math.max(...s)), l = await Q(r, "reflectance", t, [
1769
+ .25,
1770
+ .25,
1771
+ .25
1772
+ ]), u = (e) => Math.min(1, n(e) / (1 - c));
1773
+ o({
1774
+ texture: l.texture,
1775
+ rgb: l.rgb ? l.rgb.map(u) : l.texture ? [
1776
+ 1,
1777
+ 1,
1778
+ 1
1779
+ ].map(u) : null
1780
+ }), a.texture && t.warn("diffusetransmission: a textured \"transmittance\" is not supported — using 0.5"), i.diffuseTransmission = c, i.diffuseTransmissionColor = new y().setRGB(...s.map((e) => c > 0 ? Math.min(1, e / c) : 1)), i.roughness = 1, i.metalness = 0, i.specularIntensity = 0;
1781
+ break;
1782
+ }
1783
+ case "interface":
1784
+ case "none":
1785
+ case "":
1786
+ i.transmission = 1, i.ior = 1, i.roughness = 0, i.color.setRGB(1, 1, 1);
1787
+ break;
1788
+ case "mix": {
1789
+ let e = r.materials?.value || [], n = Math.max(0, Math.min(1, X(r, "amount", .5))), a = e[0] ? t.namedMaterials?.get(e[0]) : null, o = e[1] ? t.namedMaterials?.get(e[1]) : null;
1790
+ if (a && o) {
1791
+ let [e, s] = await Promise.all([_n(a, t), _n(o, t)]), c = r.amount, l = c?.type === "texture" ? await t.bakeMaterialMix?.(e, s, c.value[0]) : null;
1792
+ l && (n = l.mean);
1793
+ let u = (e, t) => e * (1 - n) + t * n;
1794
+ i.color.lerpColors(e.color, s.color, n), i.roughness = u(e.roughness, s.roughness), i.metalness = u(e.metalness, s.metalness), i.ior = u(e.ior ?? 1.5, s.ior ?? 1.5), i.transmission = u(e.transmission ?? 0, s.transmission ?? 0), i.thickness = u(e.thickness ?? 0, s.thickness ?? 0), i.clearcoat = u(e.clearcoat ?? 0, s.clearcoat ?? 0), i.clearcoatRoughness = u(e.clearcoatRoughness ?? 0, s.clearcoatRoughness ?? 0), i.emissive.lerpColors(e.emissive, s.emissive, n);
1795
+ let d = e.diffuseTransmission ?? 0, f = s.diffuseTransmission ?? 0;
1796
+ (d > 0 || f > 0) && (i.diffuseTransmission = u(d, f), i.diffuseTransmissionColor = new y().lerpColors(e.diffuseTransmissionColor ?? new y(1, 1, 1), s.diffuseTransmissionColor ?? new y(1, 1, 1), n)), i.emissiveIntensity = u(e.emissiveIntensity ?? 0, s.emissiveIntensity ?? 0), i.map = (n < .5 ? e.map : s.map) || null, i.normalMap = (n < .5 ? e.normalMap : s.normalMap) || null, l?.texture && (i.map = l.texture, i.color.setRGB(1, 1, 1));
1797
+ break;
1798
+ }
1799
+ t.warn(`material "mix" — could not resolve inner materials [${e.join(", ")}], falling back to diffuse`), i.color.set(new y(.6, .6, .6));
1800
+ break;
1801
+ }
1802
+ case "subsurface":
1803
+ case "hair":
1804
+ case "measured":
1805
+ t.warn(`material "${n}" not supported — using diffuse approximation`), a((await Q(r, "reflectance", t, [
1806
+ .5,
1807
+ .5,
1808
+ .5
1809
+ ])).rgb), i.roughness = 1;
1810
+ break;
1811
+ default:
1812
+ t.warn(`unknown material "${n}" — using neutral diffuse`), i.color.set(new y(.6, .6, .6));
1813
+ break;
1814
+ }
1815
+ let s = Z(r, "normalmap", null);
1816
+ return s && t.resolveNormalMap && (i.normalMap = await t.resolveNormalMap(s)), i.roughness = Math.max(0, Math.min(1, i.roughness)), i;
1817
+ }
1818
+ //#endregion
1819
+ //#region src/Processor/PBRT/PBRTTextureBake.js
1820
+ var vn = 4096, yn = (e) => e <= .04045 ? e / 12.92 : ((e + .055) / 1.055) ** 2.4, bn = (e) => e <= .0031308 ? e * 12.92 : 1.055 * e ** (1 / 2.4) - .055;
1821
+ function xn(e, { channels: t, decode: n = null, scale: r = 1, invert: i = !1, mapping: a = null }) {
1822
+ let { width: o, height: s } = e, c = e.channels ?? 4, l = new Float32Array(o * s * t), u = e.topDown !== !1;
1823
+ for (let a = 0; a < s; a++) {
1824
+ let d = u ? a : s - 1 - a;
1825
+ for (let s = 0; s < o; s++) {
1826
+ let u = (d * o + s) * c, f = (a * o + s) * t;
1827
+ for (let a = 0; a < t; a++) {
1828
+ let t = e.data[u + Math.min(a, c - 1)], o = r * (n ? n(t / 255) : t);
1829
+ l[f + a] = i ? Math.max(0, 1 - o) : o;
1830
+ }
1831
+ }
1832
+ }
1833
+ return {
1834
+ width: o,
1835
+ height: s,
1836
+ channels: t,
1837
+ data: l,
1838
+ su: 1,
1839
+ sv: 1,
1840
+ du: 0,
1841
+ dv: 0,
1842
+ ...a
1843
+ };
1844
+ }
1845
+ function Sn(e) {
1846
+ let t = 0;
1847
+ for (let n = 0; n < e.data.length; n++) t += e.data[n];
1848
+ return e.data.length ? t / e.data.length : 0;
1849
+ }
1850
+ function Cn(e, t, n, r) {
1851
+ let { width: i, height: a, channels: o, data: s } = e, c = t * i - .5, l = (1 - n) * a - .5, u = Math.floor(c), d = Math.floor(l), f = c - u, p = l - d, m = (e, t) => (e % t + t) % t, h = m(u, i), g = m(u + 1, i), _ = m(d, a), v = m(d + 1, a);
1852
+ for (let e = 0; e < 3; e++) {
1853
+ let t = Math.min(e, o - 1), n = s[(_ * i + h) * o + t] * (1 - f) + s[(_ * i + g) * o + t] * f, a = s[(v * i + h) * o + t] * (1 - f) + s[(v * i + g) * o + t] * f;
1854
+ r[e] = n * (1 - p) + a * p;
1855
+ }
1856
+ return r;
1857
+ }
1858
+ function wn(e, t) {
1859
+ let n = null;
1860
+ for (let t of e) t.layer && (!n || t.layer.width * t.layer.height > n.width * n.height) && (n = t.layer);
1861
+ if (!n) return null;
1862
+ let r = Math.min(n.width, vn), i = Math.min(n.height, vn), a = new Uint8Array(r * i * 4), o = e.map(() => [
1863
+ 0,
1864
+ 0,
1865
+ 0
1866
+ ]);
1867
+ for (let s = 0; s < i; s++) {
1868
+ let c = (s + .5) / i, l = (c - n.dv) / n.sv;
1869
+ for (let i = 0; i < r; i++) {
1870
+ let u = (i + .5) / r, d = (u - n.du) / n.su;
1871
+ for (let t = 0; t < e.length; t++) {
1872
+ let r = e[t], i = o[t];
1873
+ if (r.layer) {
1874
+ let e = r.layer;
1875
+ if (Cn(e, e === n ? u : e.su * d + e.du, e === n ? c : e.sv * l + e.dv, i), r.tint) for (let e = 0; e < 3; e++) i[e] *= r.tint[e];
1876
+ } else i[0] = r.rgb[0], i[1] = r.rgb[1], i[2] = r.rgb[2];
1877
+ if (r.clamp) for (let e = 0; e < 3; e++) i[e] = Math.min(1, Math.max(0, i[e]));
1878
+ }
1879
+ let f = t(o), p = (s * r + i) * 4;
1880
+ for (let e = 0; e < 3; e++) a[p + e] = Math.round(255 * bn(Math.min(1, Math.max(0, f[e]))));
1881
+ a[p + 3] = 255;
1882
+ }
1883
+ }
1884
+ return {
1885
+ data: a,
1886
+ width: r,
1887
+ height: i,
1888
+ mapping: {
1889
+ su: n.su,
1890
+ sv: n.sv,
1891
+ du: n.du,
1892
+ dv: n.dv
1893
+ }
1894
+ };
1895
+ }
1896
+ var Tn = ([e, t, n]) => {
1897
+ let r = n[0];
1898
+ return [
1899
+ e[0] + (t[0] - e[0]) * r,
1900
+ e[1] + (t[1] - e[1]) * r,
1901
+ e[2] + (t[2] - e[2]) * r
1902
+ ];
1903
+ };
1904
+ //#endregion
1905
+ //#region src/Processor/PBRT/LoopSubdivision.js
1906
+ function En(e) {
1907
+ return e === 3 ? 3 / 16 : 3 / (8 * e);
1908
+ }
1909
+ function Dn(e) {
1910
+ return 1 / (e + 3 / (8 * En(e)));
1911
+ }
1912
+ function On(e, t) {
1913
+ let n = /* @__PURE__ */ new Map(), r = [], i = [], a = [], o = [], s = [], c = t.length / 3;
1914
+ for (let l = 0; l < c; l++) for (let c = 0; c < 3; c++) {
1915
+ let u = t[l * 3 + c], d = t[l * 3 + (c + 1) % 3], f = t[l * 3 + (c + 2) % 3], p = u < d ? u : d, m = u < d ? d : u, h = p * e + m, g = n.get(h);
1916
+ g === void 0 ? (n.set(h, r.length), r.push(p), i.push(m), a.push(f), o.push(-1), s.push(1)) : (o[g] < 0 && (o[g] = f), s[g]++);
1917
+ }
1918
+ let l = Array.from({ length: e }, () => []), u = Array.from({ length: e }, () => []);
1919
+ for (let e = 0; e < r.length; e++) {
1920
+ let t = r[e], n = i[e];
1921
+ l[t].push(n), l[n].push(t), s[e] === 1 && (u[t].push(n), u[n].push(t));
1922
+ }
1923
+ return {
1924
+ edgeIndex: n,
1925
+ edgeV0: r,
1926
+ edgeV1: i,
1927
+ edgeOpp0: a,
1928
+ edgeOpp1: o,
1929
+ edgeFaces: s,
1930
+ ring: l,
1931
+ boundaryRing: u
1932
+ };
1933
+ }
1934
+ function kn(e, t, n, r, i) {
1935
+ let a = new Float64Array(e.length);
1936
+ for (let o = 0; o < n; o++) {
1937
+ let n = o * 3, s = t.boundaryRing[o];
1938
+ if (s.length === 2) {
1939
+ let t = s[0] * 3, r = s[1] * 3, o = 1 - 2 * i;
1940
+ for (let s = 0; s < 3; s++) a[n + s] = o * e[n + s] + i * (e[t + s] + e[r + s]);
1941
+ continue;
1942
+ }
1943
+ let c = t.ring[o], l = c.length;
1944
+ if (l === 0 || s.length > 0) {
1945
+ for (let t = 0; t < 3; t++) a[n + t] = e[n + t];
1946
+ continue;
1947
+ }
1948
+ let u = r(l), d = 0, f = 0, p = 0;
1949
+ for (let t = 0; t < l; t++) {
1950
+ let n = c[t] * 3;
1951
+ d += e[n], f += e[n + 1], p += e[n + 2];
1952
+ }
1953
+ let m = 1 - l * u;
1954
+ a[n] = m * e[n] + u * d, a[n + 1] = m * e[n + 1] + u * f, a[n + 2] = m * e[n + 2] + u * p;
1955
+ }
1956
+ return a;
1957
+ }
1958
+ function An(e, t) {
1959
+ let n = e.length / 3, r = On(n, t), i = r.edgeV0.length, a = kn(e, r, n, En, 1 / 8), o = new Float64Array((n + i) * 3);
1960
+ o.set(a);
1961
+ for (let t = 0; t < i; t++) {
1962
+ let i = r.edgeV0[t] * 3, a = r.edgeV1[t] * 3, s = (n + t) * 3, c = r.edgeOpp1[t];
1963
+ if (c < 0) for (let t = 0; t < 3; t++) o[s + t] = .5 * (e[i + t] + e[a + t]);
1964
+ else {
1965
+ let n = r.edgeOpp0[t] * 3, l = c * 3;
1966
+ for (let t = 0; t < 3; t++) o[s + t] = .375 * (e[i + t] + e[a + t]) + .125 * (e[n + t] + e[l + t]);
1967
+ }
1968
+ }
1969
+ let s = t.length / 3, c = new Int32Array(s * 12), l = (e, t) => n + r.edgeIndex.get((e < t ? e : t) * n + (e < t ? t : e));
1970
+ for (let e = 0; e < s; e++) {
1971
+ let n = t[e * 3], r = t[e * 3 + 1], i = t[e * 3 + 2], a = l(n, r), o = l(r, i), s = l(i, n), u = e * 12;
1972
+ c[u] = n, c[u + 1] = a, c[u + 2] = s, c[u + 3] = a, c[u + 4] = r, c[u + 5] = o, c[u + 6] = s, c[u + 7] = o, c[u + 8] = i, c[u + 9] = a, c[u + 10] = o, c[u + 11] = s;
1973
+ }
1974
+ return {
1975
+ positions: o,
1976
+ indices: c
1977
+ };
1978
+ }
1979
+ function jn(e, t, n, r = 2e6) {
1980
+ let i = Float64Array.from(e), a = Int32Array.from(t), o = 0;
1981
+ for (let e = 0; e < n && !(a.length / 3 * 4 > r); e++) {
1982
+ let e = An(i, a);
1983
+ i = e.positions, a = e.indices, o++;
1984
+ }
1985
+ let s = i.length / 3, c = kn(i, On(s, a), s, Dn, 1 / 5);
1986
+ return {
1987
+ positions: Float32Array.from(c),
1988
+ indices: Uint32Array.from(a),
1989
+ levels: o
1990
+ };
1991
+ }
1992
+ //#endregion
1993
+ //#region src/Processor/PBRT/EqualAreaOctahedral.js
1994
+ function Mn(e, t, n) {
1995
+ let r = Math.abs(e), i = Math.abs(t), a = Math.sqrt(Math.max(0, 1 - Math.abs(n))), o = Math.max(r, i), s = o === 0 ? 0 : Math.min(r, i) / o, c = 2 / Math.PI * Math.atan(s);
1996
+ r < i && (c = 1 - c);
1997
+ let l = c * a, u = a - l;
1998
+ if (n < 0) {
1999
+ let e = u;
2000
+ u = 1 - l, l = 1 - e;
2001
+ }
2002
+ return e < 0 && (u = -u), t < 0 && (l = -l), [.5 * (u + 1), .5 * (l + 1)];
2003
+ }
2004
+ var Nn = (() => {
2005
+ let e = new Float32Array(256);
2006
+ for (let t = 0; t < 256; t++) {
2007
+ let n = t / 255;
2008
+ e[t] = n <= .04045 ? n / 12.92 : ((n + .055) / 1.055) ** 2.4;
2009
+ }
2010
+ return e;
2011
+ })();
2012
+ function Pn(e, t, n, r, i, a, o, s) {
2013
+ let c = Math.min(Math.max(i * t - .5, 0), t - 1), l = Math.min(Math.max(a * n - .5, 0), n - 1), u = Math.floor(c), d = Math.floor(l), f = Math.min(u + 1, t - 1), p = Math.min(d + 1, n - 1), m = c - u, h = l - d, g = (d * t + u) * r, _ = (d * t + f) * r, v = (p * t + u) * r, y = (p * t + f) * r;
2014
+ for (let t = 0; t < 3; t++) {
2015
+ let n = s ? s[e[g + t]] : e[g + t], r = s ? s[e[_ + t]] : e[_ + t], i = s ? s[e[v + t]] : e[v + t], a = s ? s[e[y + t]] : e[y + t], c = n * (1 - m) + r * m, l = i * (1 - m) + a * m;
2016
+ o[t] = c * (1 - h) + l * h;
2017
+ }
2018
+ }
2019
+ function Fn(e, t, n = 1, r = 0) {
2020
+ let i = r || Math.min(e.width, 4096), a = Math.max(1, i >> 1), o = new Float32Array(i * a * 4), s = t ? J(t) : null, c = e.srgb ? Nn : null, l = [
2021
+ 0,
2022
+ 0,
2023
+ 0
2024
+ ];
2025
+ for (let t = 0; t < a; t++) {
2026
+ let r = (1 - (t + .5) / a) * Math.PI, u = Math.sin(r), d = Math.cos(r);
2027
+ for (let r = 0; r < i; r++) {
2028
+ let a = ((r + .5) / i - .5) * 2 * Math.PI, f = u * Math.cos(a), p = d, m = u * Math.sin(a);
2029
+ if (s) {
2030
+ let e = s, t = e[0] * f + e[4] * p + e[8] * m, n = e[1] * f + e[5] * p + e[9] * m, r = e[2] * f + e[6] * p + e[10] * m, i = Math.hypot(t, n, r) || 1;
2031
+ f = t / i, p = n / i, m = r / i;
2032
+ }
2033
+ let [h, g] = Mn(f, p, m);
2034
+ Pn(e.data, e.width, e.height, e.channels, h, e.bottomUp ? 1 - g : g, l, c);
2035
+ let _ = (t * i + r) * 4;
2036
+ o[_] = l[0] * n, o[_ + 1] = l[1] * n, o[_ + 2] = l[2] * n, o[_ + 3] = 1;
2037
+ }
2038
+ }
2039
+ return {
2040
+ data: o,
2041
+ width: i,
2042
+ height: a
2043
+ };
2044
+ }
2045
+ //#endregion
2046
+ //#region src/Processor/PBRT/PBRTCurves.js
2047
+ var In = 2;
2048
+ function Ln(e, t) {
2049
+ let n = e * e, r = n * e;
2050
+ t[0] = (1 - 3 * e + 3 * n - r) / 6, t[1] = (4 - 6 * n + 3 * r) / 6, t[2] = (1 + 3 * e + 3 * n - 3 * r) / 6, t[3] = r / 6;
2051
+ }
2052
+ function Rn(e, t) {
2053
+ let n = 1 - e;
2054
+ t[0] = n * n * n, t[1] = 3 * n * n * e, t[2] = 3 * n * e * e, t[3] = e * e * e;
2055
+ }
2056
+ function zn(e) {
2057
+ let t = Math.hypot(e[0], e[1], e[2]);
2058
+ return t > 0 && (e[0] /= t, e[1] /= t, e[2] /= t), e;
2059
+ }
2060
+ function Bn(e, t, n) {
2061
+ return n[0] = e[1] * t[2] - e[2] * t[1], n[1] = e[2] * t[0] - e[0] * t[2], n[2] = e[0] * t[1] - e[1] * t[0], n;
2062
+ }
2063
+ function Vn(e, t) {
2064
+ let n = e.length / 3;
2065
+ return t === "bspline" ? n - 3 : Math.max(1, (n - 1) / 3);
2066
+ }
2067
+ function Hn(e, t, n, r, i, a, o) {
2068
+ let s = Math.floor(r);
2069
+ s >= n && (s = n - 1);
2070
+ let c = r - s;
2071
+ t === "bspline" ? Ln(c, i) : Rn(c, i);
2072
+ let l = t === "bspline" ? s : s * 3, u = 0, d = 0, f = 0;
2073
+ for (let t = 0; t < 4; t++) {
2074
+ let n = (l + t) * 3;
2075
+ u += i[t] * e[n], d += i[t] * e[n + 1], f += i[t] * e[n + 2];
2076
+ }
2077
+ a[o] = u, a[o + 1] = d, a[o + 2] = f;
2078
+ }
2079
+ function Un(e, t, n) {
2080
+ let r = Vn(e, t);
2081
+ if (r < 1) return null;
2082
+ let i = Math.max(1, Math.round(n * r)), a = new Float64Array((i + 1) * 3), o = [
2083
+ 0,
2084
+ 0,
2085
+ 0,
2086
+ 0
2087
+ ];
2088
+ for (let n = 0; n <= i; n++) Hn(e, t, r, n / i * r, o, a, n * 3);
2089
+ return {
2090
+ points: a,
2091
+ count: i + 1,
2092
+ spans: r
2093
+ };
2094
+ }
2095
+ function Wn(e, t, n, r, i) {
2096
+ let a = e[t * 3], o = e[t * 3 + 1], s = e[t * 3 + 2], c = n[r * 3], l = n[r * 3 + 1], u = n[r * 3 + 2], d = n[i * 3] - c, f = n[i * 3 + 1] - l, p = n[i * 3 + 2] - u, m = d * d + f * f + p * p, h = m > 0 ? ((a - c) * d + (o - l) * f + (s - u) * p) / m : 0;
2097
+ return h = h < 0 ? 0 : h > 1 ? 1 : h, Math.hypot(a - c - h * d, o - l - h * f, s - u - h * p);
2098
+ }
2099
+ function Gn(e, t, n, r, i, a) {
2100
+ let { points: o, count: s, spans: c } = n;
2101
+ if (s < 3) return null;
2102
+ let l = new Float64Array((s - 1) * 3), u = [
2103
+ 0,
2104
+ 0,
2105
+ 0,
2106
+ 0
2107
+ ], d = s - 1;
2108
+ for (let n = 0; n < d; n++) Hn(e, t, c, (n + .5) / d * c, u, l, n * 3);
2109
+ let f = 0;
2110
+ for (let e = 0; e < d; e++) f = Math.max(f, Wn(l, e, o, e, e + 1));
2111
+ let p = (e) => Math.max(f, r * .5 * (i + (a - i) * (e / d))), m = (e, t) => {
2112
+ for (let n = e + 1; n < t; n++) if (Wn(o, n, o, e, t) > p(n)) return !1;
2113
+ for (let n = e; n < t; n++) if (Wn(l, n, o, e, t) > p(n + .5)) return !1;
2114
+ return !0;
2115
+ }, h = new Int32Array(s), g = 0;
2116
+ h[g++] = 0;
2117
+ for (let e = 0; e < d;) {
2118
+ let t = e + 1;
2119
+ for (; t < d && m(e, t + 1);) t++;
2120
+ h[g++] = t, e = t;
2121
+ }
2122
+ return g === s ? null : h.subarray(0, g);
2123
+ }
2124
+ function Kn(e) {
2125
+ let { P: t, basis: n = "bezier", width0: r = 1, width1: i = 1, N: a = null } = e, o = Math.max(1, Math.round(e.steps ?? In)), s = Math.max(1, Math.round(e.sides ?? 1));
2126
+ if (!t || t.length < 12) return null;
2127
+ let c = Un(t, n, o);
2128
+ if (!c) return null;
2129
+ let { points: l, count: u } = c, d = e.tolerance > 0 ? Gn(t, n, c, e.tolerance, r, i) : null, f = d ? d.length : u, p = s >= 3 ? s : s * 2, m = new Float32Array(f * p * 3), h = [
2130
+ 0,
2131
+ 0,
2132
+ 0
2133
+ ], g = [
2134
+ 0,
2135
+ 0,
2136
+ 0
2137
+ ], _ = [
2138
+ 0,
2139
+ 0,
2140
+ 0
2141
+ ], v = [
2142
+ 0,
2143
+ 0,
2144
+ 0
2145
+ ], y = !1, b = 0;
2146
+ for (let e = 0; e < u; e++) {
2147
+ let t = Math.max(0, e - 1), n = Math.min(u - 1, e + 1);
2148
+ if (h[0] = l[n * 3] - l[t * 3], h[1] = l[n * 3 + 1] - l[t * 3 + 1], h[2] = l[n * 3 + 2] - l[t * 3 + 2], zn(h), y) {
2149
+ let e = g[0] * h[0] + g[1] * h[1] + g[2] * h[2];
2150
+ g[0] -= e * h[0], g[1] -= e * h[1], g[2] -= e * h[2], Math.hypot(g[0], g[1], g[2]) < 1e-9 && Bn(h, [
2151
+ 0,
2152
+ 1,
2153
+ 0
2154
+ ], g), zn(g);
2155
+ } else {
2156
+ if (a && a.length >= 3) g[0] = a[0], g[1] = a[1], g[2] = a[2], Bn(h, g, v), g[0] = v[0], g[1] = v[1], g[2] = v[2];
2157
+ else {
2158
+ let e = Math.abs(h[0]), t = Math.abs(h[1]), n = Math.abs(h[2]);
2159
+ Bn(h, e < t && e < n ? [
2160
+ 1,
2161
+ 0,
2162
+ 0
2163
+ ] : t < n ? [
2164
+ 0,
2165
+ 1,
2166
+ 0
2167
+ ] : [
2168
+ 0,
2169
+ 0,
2170
+ 1
2171
+ ], g);
2172
+ }
2173
+ Math.hypot(g[0], g[1], g[2]) < 1e-9 && Bn(h, [
2174
+ 0,
2175
+ 1,
2176
+ 0
2177
+ ], g), zn(g), y = !0;
2178
+ }
2179
+ if (d && d[b] !== e) continue;
2180
+ Bn(h, g, _), zn(_);
2181
+ let o = e / (u - 1 || 1), c = (r + (i - r) * o) * .5, f = l[e * 3], x = l[e * 3 + 1], S = l[e * 3 + 2];
2182
+ for (let e = 0; e < p; e++) {
2183
+ let t, n, r;
2184
+ if (s >= 3) {
2185
+ let i = e / s * Math.PI * 2, a = Math.cos(i), o = Math.sin(i);
2186
+ t = g[0] * a + _[0] * o, n = g[1] * a + _[1] * o, r = g[2] * a + _[2] * o;
2187
+ } else {
2188
+ let i = e < 2 ? g : _, a = e % 2 == 0 ? -1 : 1;
2189
+ t = i[0] * a, n = i[1] * a, r = i[2] * a;
2190
+ }
2191
+ let i = (b * p + e) * 3;
2192
+ m[i] = f + t * c, m[i + 1] = x + n * c, m[i + 2] = S + r * c;
2193
+ }
2194
+ b++;
2195
+ }
2196
+ let x = s, S = new Uint32Array((f - 1) * x * 6), C = 0;
2197
+ for (let e = 0; e < f - 1; e++) for (let t = 0; t < x; t++) {
2198
+ let n = s >= 3 ? t : t * 2, r = s >= 3 ? (t + 1) % s : t * 2 + 1, i = e * p + n, a = e * p + r, o = (e + 1) * p + r, c = (e + 1) * p + n;
2199
+ S[C++] = i, S[C++] = a, S[C++] = o, S[C++] = i, S[C++] = o, S[C++] = c;
2200
+ }
2201
+ return {
2202
+ positions: m,
2203
+ indices: C === S.length ? S : U(S, C)
2204
+ };
2205
+ }
2206
+ //#endregion
2207
+ //#region src/Processor/PBRT/PBRTSceneBuilder.js
2208
+ var qn = new Set([
2209
+ "distant",
2210
+ "point",
2211
+ "spot"
2212
+ ]);
2213
+ async function Jn(e, t, n) {
2214
+ let r = e[t];
2215
+ if (r?.type === "spectrum" && typeof r.value[0] == "string" && r.value[0].startsWith("stdillum")) return [
2216
+ 1,
2217
+ 1,
2218
+ 1
2219
+ ];
2220
+ let i = (await Q(e, t, n, [
2221
+ 1,
2222
+ 1,
2223
+ 1
2224
+ ])).rgb || [
2225
+ 1,
2226
+ 1,
2227
+ 1
2228
+ ];
2229
+ if (!r || r.type === "rgb" || r.type === "color" || r.type === "float") return i;
2230
+ let a = .2126 * i[0] + .7152 * i[1] + .0722 * i[2];
2231
+ return a > 0 ? i.map((e) => e / a) : i;
2232
+ }
2233
+ var $ = [
2234
+ 1,
2235
+ 0,
2236
+ 0,
2237
+ 0,
2238
+ 0,
2239
+ 1,
2240
+ 0,
2241
+ 0,
2242
+ 0,
2243
+ 0,
2244
+ -1,
2245
+ 0,
2246
+ 0,
2247
+ 0,
2248
+ 0,
2249
+ 1
2250
+ ], Yn = 4096;
2251
+ function Xn(e) {
2252
+ return e instanceof Uint32Array ? new I(e, 1) : ArrayBuffer.isView(e) && e.BYTES_PER_ELEMENT === 4 ? new I(new Uint32Array(e.buffer, e.byteOffset, e.length), 1) : new I(Uint32Array.from(e), 1);
2253
+ }
2254
+ function Zn(e, t) {
2255
+ if (t <= e.length) return e;
2256
+ let n = e.length || 1;
2257
+ for (; n < t;) n *= 2;
2258
+ return U(e, n);
2259
+ }
2260
+ function Qn(e) {
2261
+ return e instanceof Float32Array ? e : Float32Array.from(e);
2262
+ }
2263
+ var $n = 1e3, er = 45e6, tr = 6e6, nr = 256, rr = 4096, ir = 5e5, ar = 2, or = {
2264
+ flat: 1,
2265
+ ribbon: 1,
2266
+ cylinder: 2
2267
+ }, sr = .05;
2268
+ function cr(e, t) {
2269
+ if (e.count !== t.count) return !1;
2270
+ for (let n = 0, r = e.count * 16; n < r; n++) if (e.matrices[n] !== t.matrices[n]) return !1;
2271
+ return !0;
2272
+ }
2273
+ function lr(e, t) {
2274
+ let n = Math.min(e.width, Yn), r = null;
2275
+ if (typeof OffscreenCanvas < "u") r = new OffscreenCanvas(n, n);
2276
+ else if (typeof document < "u") r = document.createElement("canvas"), r.width = r.height = n;
2277
+ else return null;
2278
+ try {
2279
+ let i = r.getContext("2d", { willReadFrequently: !0 });
2280
+ if (!i) return null;
2281
+ i.drawImage(e, 0, 0, n, n);
2282
+ let { data: a } = i.getImageData(0, 0, n, n);
2283
+ return {
2284
+ data: a,
2285
+ width: n,
2286
+ height: n,
2287
+ channels: 4,
2288
+ bottomUp: !1,
2289
+ srgb: t
2290
+ };
2291
+ } catch {
2292
+ return null;
2293
+ }
2294
+ }
2295
+ function ur(e) {
2296
+ let t = e?.image;
2297
+ if (!t) return null;
2298
+ if (ArrayBuffer.isView(t.data)) return {
2299
+ data: t.data,
2300
+ width: t.width,
2301
+ height: t.height,
2302
+ channels: 4,
2303
+ topDown: e.flipY === !0,
2304
+ float: !(t.data instanceof Uint8Array || t.data instanceof Uint8ClampedArray)
2305
+ };
2306
+ let { width: n, height: r } = t;
2307
+ if (!n || !r) return null;
2308
+ let i = null;
2309
+ if (typeof OffscreenCanvas < "u") i = new OffscreenCanvas(n, r);
2310
+ else if (typeof document < "u") i = document.createElement("canvas"), i.width = n, i.height = r;
2311
+ else return null;
2312
+ try {
2313
+ let e = i.getContext("2d", { willReadFrequently: !0 });
2314
+ return e ? (e.drawImage(t, 0, 0), {
2315
+ data: e.getImageData(0, 0, n, r).data,
2316
+ width: n,
2317
+ height: r,
2318
+ channels: 4,
2319
+ topDown: !0,
2320
+ float: !1
2321
+ }) : null;
2322
+ } catch {
2323
+ return null;
2324
+ }
2325
+ }
2326
+ function dr(e) {
2327
+ let t = Z(e, "encoding", "sRGB");
2328
+ if (t === "linear") return (e) => e;
2329
+ let n = /^gamma\s+([\d.]+)$/.exec(t);
2330
+ return n ? (e) => e ** Number(n[1]) : yn;
2331
+ }
2332
+ function fr(e) {
2333
+ return {
2334
+ su: X(e, "uscale", 1),
2335
+ sv: X(e, "vscale", 1),
2336
+ du: X(e, "udelta", 0),
2337
+ dv: X(e, "vdelta", 0)
2338
+ };
2339
+ }
2340
+ function pr(e, { su: t, sv: n, du: r, dv: i }) {
2341
+ e.repeat.set(t, n), e.offset.set(r, i);
2342
+ }
2343
+ function mr(e, { srgb: t }) {
2344
+ let n = e?.image?.data;
2345
+ if (!ArrayBuffer.isView(n) || n instanceof Uint8Array || n instanceof Uint8ClampedArray) return e;
2346
+ let { width: r, height: i } = e.image, a = new Uint8Array(r * i * 4), o = n.length / (r * i);
2347
+ for (let e = 0; e < r * i; e++) for (let r = 0; r < 4; r++) {
2348
+ let i = r < o ? Math.min(1, Math.max(0, n[e * o + r])) : 1;
2349
+ a[e * 4 + r] = Math.round(255 * (t && r < 3 ? bn(i) : i));
2350
+ }
2351
+ let s = new b(a, r, i, P);
2352
+ return s.flipY = e.flipY, s.colorSpace = t ? F : ne, s.wrapS = s.wrapT = ue, s.needsUpdate = !0, s;
2353
+ }
2354
+ function hr({ data: e, width: t, height: n, mapping: r }) {
2355
+ let i = new b(e, t, n, P);
2356
+ return i.colorSpace = F, i.wrapS = i.wrapT = ue, pr(i, r), i.needsUpdate = !0, i;
2357
+ }
2358
+ function gr(e) {
2359
+ let t = [e], n = e, r = [
2360
+ (e) => e.replace(/-renamed-\d+$/, ""),
2361
+ (e) => e.replace(/\d+$/, ""),
2362
+ (e) => e.replace(/_Color$/i, ""),
2363
+ (e) => e.replace(/\d+$/, "")
2364
+ ];
2365
+ for (let e of r) {
2366
+ let r = e(n);
2367
+ r !== n && r && t.push(n = r);
2368
+ }
2369
+ return t;
2370
+ }
2371
+ var _r = class {
2372
+ constructor(e = {}) {
2373
+ this.maxTriangles = e.maxTriangles ?? er, this.maxPlacements = e.maxPlacements ?? tr, this.mergeShapesAbove = e.mergeShapesAbove ?? nr, this.curveSteps = e.curveSteps ?? ar, this.curveSides = e.curveSides ?? null, this.curveTolerance = e.curveTolerance ?? sr, this.resolvePLY = e.resolvePLY || (async () => null), this.resolveImage = e.resolveImage || (async () => null), this.resolveEnvironment = e.resolveEnvironment || e.resolveImage || (async () => null), this.convertHandedness = e.convertHandedness === !0, this.warnings = [], this.report = [], this._materialCache = /* @__PURE__ */ new Map(), this._textureCache = /* @__PURE__ */ new Map(), this._geometryCache = /* @__PURE__ */ new Map(), this._materialBySignature = /* @__PURE__ */ new Map(), this._noUVMaterials = /* @__PURE__ */ new Map(), this._plyCache = /* @__PURE__ */ new Map(), this._imageCache = /* @__PURE__ */ new Map(), this._termCache = /* @__PURE__ */ new Map(), this._mediumMaterials = /* @__PURE__ */ new Map();
2374
+ }
2375
+ warn(e) {
2376
+ this.warnings.push(e);
2377
+ }
2378
+ async build(e) {
2379
+ this.ir = e, this._keepShapes = /* @__PURE__ */ new Set();
2380
+ for (let { name: t } of e.animatedInstances ?? []) for (let n of e.objects.get(t) ?? []) this._keepShapes.add(n);
2381
+ this._plyUsers = /* @__PURE__ */ new Map(), this._plyHeld = /* @__PURE__ */ new Set();
2382
+ for (let t of e.shapes) if (t?.type === "plymesh") {
2383
+ let e = Z(t.params, "filename", null);
2384
+ this._plyUsers.set(e, (this._plyUsers.get(e) ?? 0) + 1);
2385
+ }
2386
+ for (let t of e.objects.values()) for (let e of t) e.type === "plymesh" && this._plyHeld.add(Z(e.params, "filename", null));
2387
+ this._recoveredColors = 0, this.reportedMeshes = 0, this.triangleCount = 0, this.placementCount = 0, this.skippedForBudget = 0, this._countedGeometries = /* @__PURE__ */ new WeakSet();
2388
+ let t = new D();
2389
+ t.name = "PBRTScene", this._batches = e.shapes.length >= this.mergeShapesAbove ? /* @__PURE__ */ new Map() : null, this._mergedBatches = 0, this.mergedShapes = 0, this.droppedNoTemplate = 0, this._animated = [];
2390
+ for (let n = 0; n < e.shapes.length; n++) {
2391
+ if (this._overBudget()) {
2392
+ this.skippedForBudget += e.shapes.length - n;
2393
+ break;
2394
+ }
2395
+ let r = e.shapes[n], [i, a] = await Promise.all([this._batches ? this._createGeometry(r) : this._buildGeometry(r), this._getMaterial(r)]);
2396
+ if (!i) continue;
2397
+ if (this._batches && !r.motion && i.getAttribute("position").count <= rr) {
2398
+ this._mergeShape(r, i, a, t), e.shapes[n] = null;
2399
+ continue;
2400
+ }
2401
+ r.type === "plymesh" && this._plyHeld.add(Z(r.params, "filename", null));
2402
+ let o = this._meshFromGeometry(r, i, a, r.ctm, `shape_${n}`);
2403
+ o && (t.add(o), r.motion && this._animate(o, r.motion));
2404
+ }
2405
+ if (this._batches) {
2406
+ for (let e of this._batches.values()) this._flushBatch(e[0], t), this._flushBatch(e[1], t);
2407
+ this._batches = null;
2408
+ }
2409
+ await this._buildInstances(e, t), await this._buildAnimatedPlacements(e, t), this.skippedForBudget > 0 && this.warn(`stopped at ${this.triangleCount.toLocaleString()} stored triangles (budget ${this.maxTriangles.toLocaleString()}) and ${this.placementCount.toLocaleString()} placements (budget ${this.maxPlacements.toLocaleString()}); ${this.skippedForBudget.toLocaleString()} placement(s) skipped`);
2410
+ let n = null;
2411
+ e.camera && (n = this._buildCamera(e.camera, e.film), n && t.add(n));
2412
+ let r = e.animation ? this._buildClip(e, n) : [], i = await this._buildEnvironment(e.lights);
2413
+ return await this._buildLamps(e.lights, t), this._reportUnsupportedLights(e.lights), this._recoveredColors > 0 && this.warn(`${this._recoveredColors} unreadable texture(s) fell back to the like-named material's colour`), {
2414
+ group: t,
2415
+ camera: n,
2416
+ environment: i,
2417
+ animations: r,
2418
+ report: this.report,
2419
+ meshCount: this.reportedMeshes,
2420
+ triangleCount: this.triangleCount,
2421
+ placementCount: this.placementCount,
2422
+ mergedShapes: this.mergedShapes,
2423
+ droppedNoTemplate: this.droppedNoTemplate,
2424
+ skippedForBudget: this.skippedForBudget,
2425
+ warnings: this.warnings.concat(e.warnings || [])
2426
+ };
2427
+ }
2428
+ _overBudget() {
2429
+ return this.triangleCount >= this.maxTriangles || this.placementCount >= this.maxPlacements;
2430
+ }
2431
+ _accountGeometry(e) {
2432
+ let t = e.index ? e.index.count / 3 : e.getAttribute("position").count / 3;
2433
+ return this._countedGeometries.has(e) || (this._countedGeometries.add(e), this.triangleCount += t), t;
2434
+ }
2435
+ async _buildInstances(e, t) {
2436
+ let n = 0;
2437
+ e.skippedInstances > 0 && (this.skippedForBudget += e.skippedInstances);
2438
+ let r = new Float64Array(16), i = new N(), a = (e, a, o, s, c, l, u) => {
2439
+ let d = Math.max(0, this.maxPlacements - this.placementCount), f = Math.min(e.count, d);
2440
+ if (f < e.count && (this.skippedForBudget += e.count - f), f === 0) return;
2441
+ let p = new k(a, o, f);
2442
+ p.name = `instance_${n++}`, p.frustumCulled = !1;
2443
+ for (let t = 0; t < f; t++) {
2444
+ if (s) qt(r, e.matrices, t * 16, s);
2445
+ else for (let n = 0; n < 16; n++) r[n] = e.matrices[t * 16 + n];
2446
+ p.setMatrixAt(t, i.fromArray(this.convertHandedness ? q($, r) : r));
2447
+ }
2448
+ p.instanceMatrix.needsUpdate = !0, t.add(p), this.placementCount += f, this.reportedMeshes += f, this.report.length < $n && this.report.push({
2449
+ mesh: `${p.name} ×${f}`,
2450
+ shape: c,
2451
+ material: l,
2452
+ color: "#" + o.color.getHexString(),
2453
+ map: o.map ? "yes" : "-",
2454
+ uv: a.getAttribute("uv") ? "yes" : "NO",
2455
+ normals: a.getAttribute("normal") ? "yes" : "NO",
2456
+ emissive: o.emissiveIntensity > 0 ? `#${o.emissive.getHexString()}×${o.emissiveIntensity}` : "-",
2457
+ size: "instanced",
2458
+ tris: u
2459
+ });
2460
+ }, o = (e, t) => {
2461
+ t && t.triangles > 0 && a(e, this._batchGeometry(t), t.material, null, "merged", "-", t.triangles);
2462
+ };
2463
+ for (let { name: t, list: n, template: r } of this._samePlacementGroups(e)) {
2464
+ if (!r) {
2465
+ this.droppedNoTemplate += n.count, this.warn(`ObjectInstance "${t}" has no template — ${n.count.toLocaleString()} placement(s) dropped`);
2466
+ continue;
2467
+ }
2468
+ let e = 0;
2469
+ for (let t of r) t.type !== "plymesh" && e++;
2470
+ let i = e >= 2 ? /* @__PURE__ */ new Map() : null;
2471
+ for (let e of r) {
2472
+ if (this._overBudget()) {
2473
+ this.skippedForBudget += n.count;
2474
+ continue;
2475
+ }
2476
+ let [r, s] = await Promise.all([this._buildGeometry(e), this._getMaterial(e)]);
2477
+ if (!r) continue;
2478
+ if (i && e.type !== "plymesh" && r.getAttribute("position").count <= rr) {
2479
+ this._geometryCache.delete(e);
2480
+ let t = this._appendToBatch(i, e, r, s, e.relativeCTM || e.ctm);
2481
+ this._releaseMerged(e, r), o(n, t);
2482
+ continue;
2483
+ }
2484
+ let c = this._accountGeometry(r);
2485
+ a(n, r, this._materialForGeometry(e, r, s, `instances_${t}`), e.relativeCTM || e.ctm, e.type, e.material?.type || "diffuse", c);
2486
+ }
2487
+ if (i) for (let e of i.values()) o(n, e[0]), o(n, e[1]);
2488
+ H(n.matrices), H(n.matricesEnd);
2489
+ }
2490
+ }
2491
+ _samePlacementGroups(e) {
2492
+ let t = [], n = /* @__PURE__ */ new Map();
2493
+ for (let [r, i] of e.instances) {
2494
+ let a = e.objects.get(r);
2495
+ if (!a || i.matricesEnd) {
2496
+ t.push({
2497
+ name: r,
2498
+ list: i,
2499
+ template: a
2500
+ });
2501
+ continue;
2502
+ }
2503
+ let o = `${i.count}:${Array.prototype.join.call(i.matrices.subarray(0, 16), ",")}`, s = n.get(o);
2504
+ s || n.set(o, s = []);
2505
+ let c = s.find((e) => cr(e.list, i));
2506
+ if (c) {
2507
+ c.template === e.objects.get(c.name) && (c.template = c.template.slice());
2508
+ for (let e of a) c.template.push(e);
2509
+ H(i.matrices);
2510
+ continue;
2511
+ }
2512
+ let l = {
2513
+ name: r,
2514
+ list: i,
2515
+ template: a
2516
+ };
2517
+ s.push(l), t.push(l);
2518
+ }
2519
+ return t;
2520
+ }
2521
+ async _buildAnimatedPlacements(e, t) {
2522
+ let n = e.animatedInstances ?? [];
2523
+ for (let r = 0; r < n.length; r++) {
2524
+ let { name: i, motion: a } = n[r], o = e.objects.get(i);
2525
+ if (!o) {
2526
+ this.droppedNoTemplate++;
2527
+ continue;
2528
+ }
2529
+ if (this._overBudget()) {
2530
+ this.skippedForBudget += n.length - r;
2531
+ break;
2532
+ }
2533
+ let s = new D();
2534
+ s.name = `placement_${r}`;
2535
+ let c = Array.from(a.matrices.subarray(0, 16));
2536
+ new N().fromArray(this.convertHandedness ? q($, c) : c).decompose(s.position, s.quaternion, s.scale);
2537
+ for (let e = 0; e < o.length; e++) {
2538
+ let t = o[e], [n, r] = await Promise.all([this._buildGeometry(t), this._getMaterial(t)]);
2539
+ if (!n) continue;
2540
+ let i = this._meshFromGeometry(t, n, r, t.relativeCTM || t.ctm, `${s.name}_${e}`, !0);
2541
+ i && s.add(i);
2542
+ }
2543
+ s.children.length !== 0 && (t.add(s), this._animate(s, a));
2544
+ }
2545
+ }
2546
+ _animate(e, t) {
2547
+ t.visible && (e.visible = t.visible[0] === 1), this._animated.push({
2548
+ node: e,
2549
+ motion: t
2550
+ });
2551
+ }
2552
+ _buildClip(e, t) {
2553
+ let n = [];
2554
+ for (let { node: e, motion: t } of this._animated) if (this._transformTracks(n, e.name, t, (e, t) => {
2555
+ let n = Array.from(e.subarray(t, t + 16));
2556
+ return this.convertHandedness ? q($, n) : n;
2557
+ }), t.visible) {
2558
+ let r = t.times, i = r.map((e, t) => t === 0 ? 0 : (r[t - 1] + e) / 2);
2559
+ n.push(new g(`${e.name}.visible`, i, Array.from(t.visible, (e) => e === 1)));
2560
+ }
2561
+ let r = e.camera?.motion;
2562
+ if (t && r) {
2563
+ let e = new oe();
2564
+ if (this._transformTracks(n, t.name, r, (t, n) => (this._poseCamera(e, t.subarray(n, n + 16)), e.updateMatrix(), e.matrix.elements)), r.fov && t.isPerspectiveCamera) {
2565
+ let e = Array.from(r.fov, (e) => this._verticalFov({ fov: {
2566
+ type: "float",
2567
+ value: [e]
2568
+ } }, t.aspect));
2569
+ n.push(new re(`${t.name}.fov`, r.times.slice(), e));
2570
+ }
2571
+ }
2572
+ for (let e of n) e.optimize();
2573
+ return n.length === 0 ? [] : [new h(e.animation.name, e.animation.duration, n)];
2574
+ }
2575
+ _transformTracks(e, t, n, r) {
2576
+ let i = n.times.length, a = new Float32Array(i * 3), o = new Float32Array(i * 4), s = new Float32Array(i * 3), c = new N(), l = new L(), u = new ce(), d = new L();
2577
+ for (let e = 0; e < i; e++) c.fromArray(r(n.matrices, e * 16)).decompose(l, u, d), e > 0 && u.x * o[e * 4 - 4] + u.y * o[e * 4 - 3] + u.z * o[e * 4 - 2] + u.w * o[e * 4 - 1] < 0 && u.set(-u.x, -u.y, -u.z, -u.w), l.toArray(a, e * 3), u.toArray(o, e * 4), d.toArray(s, e * 3);
2578
+ let f = (e, t) => {
2579
+ for (let n = t; n < e.length; n++) if (e[n] !== e[n % t]) return !0;
2580
+ return !1;
2581
+ };
2582
+ f(a, 3) && e.push(new me(`${t}.position`, n.times.slice(), a)), f(o, 4) && e.push(new le(`${t}.quaternion`, n.times.slice(), o)), f(s, 3) && e.push(new me(`${t}.scale`, n.times.slice(), s));
2583
+ }
2584
+ async _buildShapeMesh(e, t, n) {
2585
+ let [r, i] = await Promise.all([this._buildGeometry(e), this._getMaterial(e)]);
2586
+ return r ? this._meshFromGeometry(e, r, i, t, n) : null;
2587
+ }
2588
+ _meshFromGeometry(e, t, n, r, i, a = !1) {
2589
+ let o = !!t.getAttribute("uv"), s = this._materialForGeometry(e, t, n, i), c = new ee(t, s);
2590
+ c.name = i;
2591
+ let l = this.convertHandedness && !a ? q($, r) : r;
2592
+ new N().fromArray(l).decompose(c.position, c.quaternion, c.scale), c.updateMatrix(), t.boundingBox || t.computeBoundingBox();
2593
+ let u = t.boundingBox ? t.boundingBox.clone().applyMatrix4(c.matrix).getSize(new L()) : new L(), d = this._accountGeometry(t);
2594
+ return this.placementCount++, this.reportedMeshes++, this.report.length < $n && this.report.push({
2595
+ mesh: i,
2596
+ shape: e.type,
2597
+ material: e.material?.type || "diffuse",
2598
+ color: "#" + s.color.getHexString(),
2599
+ map: s.map ? "yes" : "-",
2600
+ uv: o ? "yes" : "NO",
2601
+ normals: t.getAttribute("normal") ? "yes" : "NO",
2602
+ emissive: s.emissiveIntensity > 0 ? `#${s.emissive.getHexString()}×${s.emissiveIntensity}` : "-",
2603
+ size: `${u.x.toFixed(2)}×${u.y.toFixed(2)}×${u.z.toFixed(2)}`,
2604
+ tris: d
2605
+ }), c;
2606
+ }
2607
+ _materialForGeometry(e, t, n, r) {
2608
+ if (!n.map || t.getAttribute("uv")) return n;
2609
+ let i = this._noUVMaterials.get(n);
2610
+ return i || (this.warn(`${r} (${e.type}, "${e.material?.type || "diffuse"}") has a texture map but no UVs — dropping map, using base color`), i = n.clone(), i.map = null, this._noUVMaterials.set(n, i)), i;
2611
+ }
2612
+ _mergeShape(e, t, n, r) {
2613
+ this.reportedMeshes++;
2614
+ let i = this.convertHandedness ? q($, e.ctm) : e.ctm, a = this._appendToBatch(this._batches, e, t, n, i);
2615
+ this._releaseMerged(e, t), a && this._flushBatch(a, r);
2616
+ }
2617
+ _releaseMerged(e, t) {
2618
+ if (!this._keepShapes?.has(e) && !(e.type === "plymesh" && !this._lastPlyUse(Z(e.params, "filename", null)))) {
2619
+ for (let e in t.attributes) H(t.attributes[e].array);
2620
+ H(t.index?.array);
2621
+ for (let t in e.params) ArrayBuffer.isView(e.params[t]?.value) && H(e.params[t].value);
2622
+ }
2623
+ }
2624
+ _lastPlyUse(e) {
2625
+ let t = (this._plyUsers.get(e) ?? 1) - 1;
2626
+ return this._plyUsers.set(e, t), t > 0 || this._plyHeld.has(e) ? !1 : (this._plyCache.delete(e), !0);
2627
+ }
2628
+ _appendToBatch(e, t, n, r, i) {
2629
+ let a = this._materialForGeometry(t, n, r, "merged shape"), o = n.getAttribute("position"), s = n.getAttribute("normal"), c = n.getAttribute("uv"), l = n.index, u = o.count;
2630
+ this.triangleCount += (l ? l.count : u) / 3, this.mergedShapes++;
2631
+ let d = e.get(a);
2632
+ d || e.set(a, d = [null, null]);
2633
+ let f = +!!c, p = d[f];
2634
+ p || (d[f] = p = {
2635
+ material: a,
2636
+ positions: new Float32Array(3072),
2637
+ normals: new Float32Array(3072),
2638
+ uvs: c ? new Float32Array(2048) : null,
2639
+ indices: new Uint32Array(3072),
2640
+ vertexCount: 0,
2641
+ indexCount: 0,
2642
+ triangles: 0
2643
+ });
2644
+ let m = i, h = J(m), g = p.vertexCount;
2645
+ p.positions = Zn(p.positions, (g + u) * 3), p.normals = Zn(p.normals, (g + u) * 3), c && (p.uvs = Zn(p.uvs, (g + u) * 2));
2646
+ let _ = o.array, v = p.positions, y = s ? s.array : null, b = p.normals;
2647
+ for (let e = 0; e < u; e++) {
2648
+ let t = e * 3, n = (g + e) * 3, r = _[t], i = _[t + 1], a = _[t + 2];
2649
+ if (v[n] = m[0] * r + m[4] * i + m[8] * a + m[12], v[n + 1] = m[1] * r + m[5] * i + m[9] * a + m[13], v[n + 2] = m[2] * r + m[6] * i + m[10] * a + m[14], !y) continue;
2650
+ let o = y[t], s = y[t + 1], c = y[t + 2], l = h[0] * o + h[1] * s + h[2] * c, u = h[4] * o + h[5] * s + h[6] * c, d = h[8] * o + h[9] * s + h[10] * c, f = Math.hypot(l, u, d);
2651
+ f > 0 && (l /= f, u /= f, d /= f), b[n] = l, b[n + 1] = u, b[n + 2] = d;
2652
+ }
2653
+ c && p.uvs.set(c.array.subarray(0, u * 2), g * 2);
2654
+ let x = l ? l.count : u;
2655
+ p.indices = Zn(p.indices, p.indexCount + x);
2656
+ let S = p.indices, C = l ? l.array : null;
2657
+ for (let e = 0; e < x; e++) S[p.indexCount + e] = g + (C ? C[e] : e);
2658
+ return p.vertexCount += u, p.indexCount += x, p.triangles += x / 3, p.triangles >= ir ? (d[f] = null, p) : null;
2659
+ }
2660
+ _batchGeometry(e) {
2661
+ let t = new v();
2662
+ return t.setAttribute("position", new w(U(e.positions, e.vertexCount * 3), 3)), t.setAttribute("normal", new w(U(e.normals, e.vertexCount * 3), 3)), e.uvs && t.setAttribute("uv", new w(U(e.uvs, e.vertexCount * 2), 2)), t.setIndex(new I(U(e.indices, e.indexCount), 1)), e.positions = e.normals = e.uvs = e.indices = null, t;
2663
+ }
2664
+ _flushBatch(e, t) {
2665
+ if (!e || e.triangles === 0) return;
2666
+ let n = new ee(this._batchGeometry(e), e.material);
2667
+ n.name = `merged_${this._mergedBatches++}`, n.frustumCulled = !1, t.add(n), this.placementCount++, this.report.length < $n && this.report.push({
2668
+ mesh: n.name,
2669
+ shape: "merged",
2670
+ material: "-",
2671
+ color: "#" + e.material.color.getHexString(),
2672
+ map: e.material.map ? "yes" : "-",
2673
+ uv: e.uvs ? "yes" : "NO",
2674
+ normals: "yes",
2675
+ emissive: e.material.emissiveIntensity > 0 ? `#${e.material.emissive.getHexString()}×${e.material.emissiveIntensity}` : "-",
2676
+ size: "merged",
2677
+ tris: e.triangles
2678
+ });
2679
+ }
2680
+ _buildGeometry(e) {
2681
+ let t = this._geometryCache.get(e);
2682
+ return t || this._geometryCache.set(e, t = this._createGeometry(e)), t;
2683
+ }
2684
+ async _createGeometry(e) {
2685
+ let t = await this._shapeGeometry(e);
2686
+ return t && e.areaLight && !fn(e.areaLight.params, "twosided", !1) ? this._facingEmission(e, t) : t;
2687
+ }
2688
+ _facingEmission(e, t) {
2689
+ let n = t.userData.pbrtNormals ? t.getAttribute("normal") : null;
2690
+ if (!n && !e.reverseOrientation) return t;
2691
+ let r = t.getAttribute("position").array, i = n?.array, a = t.index?.array ?? null, o = a ? a.length : t.getAttribute("position").count, s = new Uint32Array(o), c = 0;
2692
+ for (let t = 0; t + 2 < o; t += 3) {
2693
+ let n = a ? a[t] : t, o = a ? a[t + 1] : t + 1, l = a ? a[t + 2] : t + 2, u = e.reverseOrientation;
2694
+ if (i) {
2695
+ let e = r[o * 3] - r[n * 3], t = r[o * 3 + 1] - r[n * 3 + 1], a = r[o * 3 + 2] - r[n * 3 + 2], s = r[l * 3] - r[n * 3], c = r[l * 3 + 1] - r[n * 3 + 1], d = r[l * 3 + 2] - r[n * 3 + 2], f = i[n * 3] + i[o * 3] + i[l * 3], p = i[n * 3 + 1] + i[o * 3 + 1] + i[l * 3 + 1], m = i[n * 3 + 2] + i[o * 3 + 2] + i[l * 3 + 2];
2696
+ u = (t * d - a * c) * f + (a * s - e * d) * p + (e * c - t * s) * m < 0;
2697
+ }
2698
+ s[t] = n, s[t + 1] = u ? l : o, s[t + 2] = u ? o : l, u && c++;
2699
+ }
2700
+ if (c === 0) return t;
2701
+ let l = new v();
2702
+ for (let e in t.attributes) l.setAttribute(e, t.getAttribute(e));
2703
+ return l.setIndex(new I(s, 1)), l;
2704
+ }
2705
+ _shapeGeometry(e) {
2706
+ switch (e.type) {
2707
+ case "trianglemesh": return this._triangleMesh(e.params);
2708
+ case "bilinearmesh": return this._bilinearMesh(e.params);
2709
+ case "loopsubdiv": return this._loopSubdiv(e.params);
2710
+ case "curve": return this._curve(e.params);
2711
+ case "plymesh": return this._plyMesh(e.params);
2712
+ case "sphere": return this._sphere(e.params);
2713
+ case "disk": return this._disk(e.params);
2714
+ default: return this.warn(`shape "${e.type}" not supported — skipped`), null;
2715
+ }
2716
+ }
2717
+ _triangleMesh(e) {
2718
+ let t = e.P?.value;
2719
+ if (!t || t.length < 9) return this.warn("trianglemesh missing P"), null;
2720
+ let n = new v();
2721
+ n.setAttribute("position", new w(Qn(t), 3));
2722
+ let r = e.N?.value;
2723
+ r && r.length === t.length && (n.setAttribute("normal", new w(Qn(r), 3)), n.userData.pbrtNormals = !0);
2724
+ let i = (e.uv || e.st)?.value;
2725
+ i && i.length === t.length / 3 * 2 && n.setAttribute("uv", new w(Qn(i), 2));
2726
+ let a = e.indices?.value;
2727
+ return a && a.length && n.setIndex(Xn(a)), r || n.computeVertexNormals(), n;
2728
+ }
2729
+ _curve(e) {
2730
+ let t = e.P?.value;
2731
+ if (!t || t.length < 12) return this.warn("curve needs at least 4 control points"), null;
2732
+ let n = Math.round(X(e, "degree", 3));
2733
+ if (n !== 3) return this.warn(`curve degree ${n} not supported — only cubic`), null;
2734
+ let r = Z(e, "type", "flat"), i = X(e, "width", 1), a = Kn({
2735
+ P: t,
2736
+ basis: Z(e, "basis", "bezier"),
2737
+ width0: X(e, "width0", i),
2738
+ width1: X(e, "width1", i),
2739
+ N: e.N?.value || null,
2740
+ steps: this.curveSteps,
2741
+ sides: this.curveSides ?? or[r] ?? 1,
2742
+ tolerance: this.curveTolerance
2743
+ });
2744
+ if (!a) return this.warn("curve could not be tessellated"), null;
2745
+ let o = new v();
2746
+ return o.setAttribute("position", new w(a.positions, 3)), o.setIndex(new I(a.indices, 1)), o.computeVertexNormals(), o;
2747
+ }
2748
+ _loopSubdiv(e) {
2749
+ let t = e.P?.value, n = e.indices?.value;
2750
+ if (!t || t.length < 9 || !n || n.length < 3) return this.warn("loopsubdiv missing P/indices"), null;
2751
+ let r = Math.max(0, Math.min(6, Math.round(X(e, "levels", 3)))), { positions: i, indices: a, levels: o } = jn(t, n, r);
2752
+ o < r && this.warn(`loopsubdiv refined ${o}/${r} levels — the control mesh is too dense for the triangle budget`);
2753
+ let s = new v();
2754
+ return s.setAttribute("position", new w(i, 3)), s.setIndex(new I(a, 1)), s.computeVertexNormals(), s;
2755
+ }
2756
+ _bilinearMesh(e) {
2757
+ let t = e.P?.value, n = e.indices?.value;
2758
+ if (!t || !n) return this.warn("bilinearmesh missing P/indices"), null;
2759
+ let r = [];
2760
+ for (let e = 0; e + 3 < n.length; e += 4) {
2761
+ let [t, i, a, o] = [
2762
+ n[e],
2763
+ n[e + 1],
2764
+ n[e + 2],
2765
+ n[e + 3]
2766
+ ];
2767
+ r.push(t, i, a, t, a, o);
2768
+ }
2769
+ let i = new v();
2770
+ return i.setAttribute("position", new w(Float32Array.from(t), 3)), i.setIndex(r), i.computeVertexNormals(), i;
2771
+ }
2772
+ _plyMesh(e) {
2773
+ let t = Z(e, "filename", null);
2774
+ if (!t) return this.warn("plymesh missing filename"), null;
2775
+ let n = this._plyCache.get(t);
2776
+ return n || this._plyCache.set(t, n = this._decodePly(t)), n;
2777
+ }
2778
+ async _decodePly(e) {
2779
+ try {
2780
+ let t = await this.resolvePLY(e);
2781
+ return t ? (t.getAttribute("normal") ? t.userData.pbrtNormals = !0 : t.computeVertexNormals(), t) : (this.warn(`plymesh file not found: ${e}`), null);
2782
+ } catch (t) {
2783
+ return this.warn(`failed to load plymesh ${e}: ${t.message}`), null;
2784
+ }
2785
+ }
2786
+ _sphere(e) {
2787
+ return new de(X(e, "radius", 1), 48, 32);
2788
+ }
2789
+ _disk(e) {
2790
+ let t = X(e, "radius", 1), n = X(e, "innerradius", 0), r = X(e, "height", 0), i = [], a = [];
2791
+ for (let e = 0; e < 48; e++) {
2792
+ let o = e / 48 * Math.PI * 2, s = (e + 1) / 48 * Math.PI * 2, c = i.length / 3;
2793
+ i.push(Math.cos(o) * n, Math.sin(o) * n, r), i.push(Math.cos(o) * t, Math.sin(o) * t, r), i.push(Math.cos(s) * t, Math.sin(s) * t, r), i.push(Math.cos(s) * n, Math.sin(s) * n, r), a.push(c, c + 1, c + 2, c, c + 2, c + 3);
2794
+ }
2795
+ let o = new v();
2796
+ return o.setAttribute("position", new w(Float32Array.from(i), 3)), o.setIndex(a), o.computeVertexNormals(), o;
2797
+ }
2798
+ async _getMaterial(e) {
2799
+ let t = await this._surfaceMaterial(e);
2800
+ return e.interior ? this._inMedium(t, e.interior) : t;
2801
+ }
2802
+ async _surfaceMaterial(e) {
2803
+ let t = this._materialCache.get(e.material);
2804
+ if (t || (t = /* @__PURE__ */ new Map(), this._materialCache.set(e.material, t)), t.has(e.areaLight)) return t.get(e.areaLight);
2805
+ let n = this._materialContext(), r = await _n(e.material, n);
2806
+ e.areaLight && await this._applyAreaLight(r, e.areaLight, n);
2807
+ let i = this._dedupeMaterial(r);
2808
+ return t.set(e.areaLight, i), i;
2809
+ }
2810
+ _materialContext() {
2811
+ return this._ctx ??= {
2812
+ resolveNamedTexture: (e) => this._resolveNamedTexture(e),
2813
+ namedMaterials: this.ir.namedMaterials,
2814
+ warn: (e) => this.warn(e),
2815
+ floatTextureMean: async (e) => {
2816
+ let t = await this._bakeTerm(e, 1);
2817
+ return t ? t.layer ? Sn(t.layer) : t.rgb[0] : null;
2818
+ },
2819
+ bakeMaterialMix: (e, t, n) => this._bakeMaterialMix(e, t, n),
2820
+ resolveNormalMap: async (e) => {
2821
+ let t = await this._image(e);
2822
+ if (!t) return null;
2823
+ let n = mr(t, { srgb: !1 }), r = n === t ? t.clone() : n;
2824
+ return r.colorSpace = ne, r;
2825
+ }
2826
+ };
2827
+ }
2828
+ async _inMedium(e, t) {
2829
+ if (!(e.transmission > 0)) return e;
2830
+ let n = this._mediumMaterials.get(e);
2831
+ return n || this._mediumMaterials.set(e, n = /* @__PURE__ */ new Map()), n.has(t) || n.set(t, this._attenuated(e, t)), n.get(t);
2832
+ }
2833
+ async _attenuated(e, t) {
2834
+ let n = this.ir.media?.get(t), r = n && Z(n, "type", "");
2835
+ if (r !== "homogeneous") return this.warn(n ? `medium "${t}" (${r}) is not supported — "${t}" glass left clear` : `medium "${t}" not defined`), e;
2836
+ n.preset && this.warn(`medium preset "${Z(n, "preset", "")}" is not supported — using sigma_a / sigma_s`);
2837
+ let i = this._materialContext(), a = (await Q(n, "sigma_a", i, [
2838
+ 1,
2839
+ 1,
2840
+ 1
2841
+ ])).rgb, o = (await Q(n, "sigma_s", i, [
2842
+ 1,
2843
+ 1,
2844
+ 1
2845
+ ])).rgb, s = X(n, "scale", 1), c = [
2846
+ 0,
2847
+ 1,
2848
+ 2
2849
+ ].map((e) => (a[e] + o[e]) * s), l = Math.max(...c);
2850
+ if (!(l > 0)) return e;
2851
+ let u = e.clone();
2852
+ return u.attenuationDistance = 1 / l, u.attenuationColor.setRGB(...c.map((e) => Math.exp(-e / l))), this._dedupeMaterial(u);
2853
+ }
2854
+ _dedupeMaterial(e) {
2855
+ let t = e.color, n = e.emissive, r = `${t.r},${t.g},${t.b}|${n.r},${n.g},${n.b}|${e.emissiveIntensity}|${e.map ? e.map.uuid : "-"}|${e.roughness}|${e.metalness}|${e.transmission}|${e.ior}|${e.thickness}|${e.clearcoat}|${e.clearcoatRoughness}|${e.opacity}|${e.side}|${e.normalMap ? e.normalMap.uuid : "-"}|${e.attenuationDistance}|${e.attenuationColor.toArray()}|${e.specularIntensity}|${e.diffuseTransmission ?? 0}|${e.diffuseTransmissionColor?.toArray() ?? "-"}`;
2856
+ return this._materialBySignature.get(r) || (this._materialBySignature.set(r, e), e);
2857
+ }
2858
+ async _applyAreaLight(e, t, n) {
2859
+ let r = await Jn(t.params, "L", n), i = X(t.params, "scale", 1);
2860
+ e.emissive.setRGB(r[0], r[1], r[2]), e.emissiveIntensity = i, e.side = fn(t.params, "twosided", !1) ? S : E;
2861
+ }
2862
+ async _resolveNamedTexture(e) {
2863
+ if (this._textureCache.has(e)) return this._textureCache.get(e);
2864
+ let t = this.ir.namedTextures.get(e), n = null;
2865
+ if (!t) this.warn(`named texture "${e}" not defined`);
2866
+ else if (t.class === "imagemap") n = await this._imageMapTexture(e, t);
2867
+ else if (t.class === "mix") n = await this._mixTexture(e, t);
2868
+ else if (t.class === "constant") {
2869
+ let e = t.params.value;
2870
+ e && e.type === "rgb" ? n = { constant: [
2871
+ e.value[0],
2872
+ e.value[1],
2873
+ e.value[2]
2874
+ ] } : e && (n = { constant: [
2875
+ e.value[0],
2876
+ e.value[0],
2877
+ e.value[0]
2878
+ ] });
2879
+ } else if (t.class === "scale") n = await this._resolveScaleTexture(e, t);
2880
+ else {
2881
+ let r = this._colorFromLikeNamedMaterial(e);
2882
+ r ? (n = { constant: r.rgb }, this._recoveredColors++) : this.warn(`texture class "${t.class}" not supported (texture "${e}")`);
2883
+ }
2884
+ return this._textureCache.set(e, n), n;
2885
+ }
2886
+ _image(e) {
2887
+ let t = this._imageCache.get(e);
2888
+ return t || (t = Promise.resolve().then(() => this.resolveImage(e)).catch((t) => (this.warn(`failed to load image ${e}: ${t.message}`), null)), this._imageCache.set(e, t)), t;
2889
+ }
2890
+ async _imageMapTexture(e, t) {
2891
+ let n = Z(t.params, "filename", null);
2892
+ if (!n) return null;
2893
+ let r = await this._image(n);
2894
+ if (!r) return this.warn(`image not found for texture "${e}": ${n}`), null;
2895
+ let i = Z(t.params, "mapping", "uv");
2896
+ i !== "uv" && this.warn(`texture "${e}": "${i}" mapping is not supported — using the mesh's uv`);
2897
+ let a = X(t.params, "scale", 1);
2898
+ if (a > 1 || fn(t.params, "invert", !1)) {
2899
+ let t = await this._bakeTerm(e, 3), n = t?.layer && wn([{
2900
+ ...t,
2901
+ clamp: !0
2902
+ }], ([e]) => e);
2903
+ if (n) return { texture: hr(n) };
2904
+ this.warn(`texture "${e}": its scale or invert could not be baked here — ignored`);
2905
+ }
2906
+ let o = mr(r, { srgb: !0 }), s = o === r ? r.clone() : o;
2907
+ return pr(s, fr(t.params)), s.needsUpdate = !0, a < 1 ? {
2908
+ texture: s,
2909
+ constant: [
2910
+ a,
2911
+ a,
2912
+ a
2913
+ ]
2914
+ } : { texture: s };
2915
+ }
2916
+ async _mixTexture(e, t) {
2917
+ let [n, r, i] = await Promise.all([
2918
+ this._paramTerm(t.params, "tex1", 3, [
2919
+ 0,
2920
+ 0,
2921
+ 0
2922
+ ]),
2923
+ this._paramTerm(t.params, "tex2", 3, [
2924
+ 1,
2925
+ 1,
2926
+ 1
2927
+ ]),
2928
+ this._paramTerm(t.params, "amount", 1, [
2929
+ .5,
2930
+ .5,
2931
+ .5
2932
+ ])
2933
+ ]);
2934
+ if (!n || !r || !i) return this.warn(`texture "${e}": a "mix" input could not be read here`), null;
2935
+ let a = (e) => e.map((e) => Math.min(1, Math.max(0, e))), o = wn([
2936
+ {
2937
+ ...n,
2938
+ clamp: !0
2939
+ },
2940
+ {
2941
+ ...r,
2942
+ clamp: !0
2943
+ },
2944
+ i
2945
+ ], Tn);
2946
+ return o ? { texture: hr(o) } : { constant: Tn([
2947
+ a(n.rgb),
2948
+ a(r.rgb),
2949
+ i.rgb
2950
+ ]) };
2951
+ }
2952
+ async _paramTerm(e, t, n, r) {
2953
+ let i = e[t];
2954
+ if (!i) return { rgb: r };
2955
+ if (i.type === "texture") return this._bakeTerm(i.value[0], n);
2956
+ let a = (await Q(e, t, this._materialContext(), r)).rgb;
2957
+ return a ? { rgb: a } : null;
2958
+ }
2959
+ _bakeTerm(e, t) {
2960
+ let n = `${e}\0${t}`, r = this._termCache.get(n);
2961
+ return r || this._termCache.set(n, r = this._createBakeTerm(e, t)), r;
2962
+ }
2963
+ async _createBakeTerm(e, t) {
2964
+ let n = this.ir.namedTextures.get(e);
2965
+ if (n?.class === "imagemap") {
2966
+ if (Z(n.params, "mapping", "uv") !== "uv") return null;
2967
+ let e = Z(n.params, "filename", null), r = ur(e && await this._image(e));
2968
+ return r ? { layer: xn(r, {
2969
+ channels: t,
2970
+ decode: r.float ? null : dr(n.params),
2971
+ scale: X(n.params, "scale", 1),
2972
+ invert: fn(n.params, "invert", !1),
2973
+ mapping: fr(n.params)
2974
+ }) } : null;
2975
+ }
2976
+ let r = await this._resolveNamedTexture(e);
2977
+ if (!r) return null;
2978
+ if (!r.texture) return r.constant ? { rgb: r.constant } : null;
2979
+ let i = this._textureLayer(r.texture, t);
2980
+ return i ? {
2981
+ layer: i,
2982
+ tint: r.constant ?? null
2983
+ } : null;
2984
+ }
2985
+ _textureLayer(e, t) {
2986
+ let n = ur(e);
2987
+ if (!n) return null;
2988
+ let r = !n.float && e.colorSpace === F;
2989
+ return xn(n, {
2990
+ channels: t,
2991
+ decode: n.float ? null : r ? yn : (e) => e,
2992
+ mapping: {
2993
+ su: e.repeat.x,
2994
+ sv: e.repeat.y,
2995
+ du: e.offset.x,
2996
+ dv: e.offset.y
2997
+ }
2998
+ });
2999
+ }
3000
+ async _bakeMaterialMix(e, t, n) {
3001
+ let r = await this._bakeTerm(n, 1);
3002
+ if (!r) return null;
3003
+ if (!r.layer) return {
3004
+ mean: r.rgb[0],
3005
+ texture: null
3006
+ };
3007
+ let i = Sn(r.layer), a = (e) => {
3008
+ if (!e.map) return {
3009
+ rgb: e.color.toArray(),
3010
+ clamp: !0
3011
+ };
3012
+ let t = this._textureLayer(e.map, 3);
3013
+ return t && {
3014
+ layer: t,
3015
+ tint: e.color.toArray(),
3016
+ clamp: !0
3017
+ };
3018
+ }, o = a(e), s = a(t);
3019
+ if (!o || !s) return {
3020
+ mean: i,
3021
+ texture: null
3022
+ };
3023
+ let c = wn([
3024
+ o,
3025
+ s,
3026
+ r
3027
+ ], Tn);
3028
+ return {
3029
+ mean: i,
3030
+ texture: c ? hr(c) : null
3031
+ };
3032
+ }
3033
+ _colorFromLikeNamedMaterial(e) {
3034
+ for (let t of gr(e)) {
3035
+ let e = (this.ir?.namedMaterials?.get(t))?.params?.reflectance;
3036
+ if (e && (e.type === "rgb" || e.type === "color")) return {
3037
+ name: t,
3038
+ rgb: [
3039
+ e.value[0],
3040
+ e.value[1],
3041
+ e.value[2]
3042
+ ]
3043
+ };
3044
+ }
3045
+ return null;
3046
+ }
3047
+ async _resolveScaleTexture(e, t) {
3048
+ if (t.params.scale?.type === "texture") {
3049
+ let [n, r] = await Promise.all([this._paramTerm(t.params, "tex", 3, [
3050
+ 1,
3051
+ 1,
3052
+ 1
3053
+ ]), this._paramTerm(t.params, "scale", 1, [
3054
+ 1,
3055
+ 1,
3056
+ 1
3057
+ ])]), i = ([e, t]) => [
3058
+ e[0] * t[0],
3059
+ e[1] * t[0],
3060
+ e[2] * t[0]
3061
+ ];
3062
+ if (n && r) {
3063
+ let e = wn([{
3064
+ ...n,
3065
+ clamp: !0
3066
+ }, r], i);
3067
+ return e ? { texture: hr(e) } : { constant: i([n.rgb, r.rgb]) };
3068
+ }
3069
+ this.warn(`texture "${e}": its "scale" texture could not be read here — ignored`);
3070
+ }
3071
+ let n = t.params.tex, r = null;
3072
+ if (n?.type === "texture" && typeof n.value[0] == "string") r = await this._resolveNamedTexture(n.value[0]);
3073
+ else if (n?.type === "rgb" || n?.type === "color") r = { constant: [
3074
+ n.value[0],
3075
+ n.value[1],
3076
+ n.value[2]
3077
+ ] };
3078
+ else if (n?.type === "float") {
3079
+ let e = n.value[0];
3080
+ r = { constant: [
3081
+ e,
3082
+ e,
3083
+ e
3084
+ ] };
3085
+ }
3086
+ let i = t.params.scale, a = [
3087
+ 1,
3088
+ 1,
3089
+ 1
3090
+ ];
3091
+ if (i?.type === "rgb" || i?.type === "color") a = [
3092
+ i.value[0],
3093
+ i.value[1],
3094
+ i.value[2]
3095
+ ];
3096
+ else if (i?.type === "float") {
3097
+ let e = i.value[0];
3098
+ a = [
3099
+ e,
3100
+ e,
3101
+ e
3102
+ ];
3103
+ }
3104
+ if (r?.texture) {
3105
+ let e = r.constant || [
3106
+ 1,
3107
+ 1,
3108
+ 1
3109
+ ];
3110
+ return {
3111
+ texture: r.texture,
3112
+ constant: [
3113
+ e[0] * a[0],
3114
+ e[1] * a[1],
3115
+ e[2] * a[2]
3116
+ ]
3117
+ };
3118
+ }
3119
+ let o = r?.constant || [
3120
+ 1,
3121
+ 1,
3122
+ 1
3123
+ ];
3124
+ return { constant: [
3125
+ o[0] * a[0],
3126
+ o[1] * a[1],
3127
+ o[2] * a[2]
3128
+ ] };
3129
+ }
3130
+ _buildCamera(e, t) {
3131
+ let n = t && t.yresolution ? t.xresolution / t.yresolution : 16 / 9, r;
3132
+ if (e.type === "orthographic") {
3133
+ let t = e.params?.screenwindow?.value, i = t?.length === 4 ? (t[3] - t[2]) / 2 : Math.max(1, 1 / n);
3134
+ r = new ae(-i * n, i * n, i, -i, .01, 1e4);
3135
+ } else r = new oe(this._verticalFov(e.params, n), n, .01, 1e4);
3136
+ return r.name = "PBRT Camera", this._poseCamera(r, e.cameraToWorld), r.updateMatrixWorld(!0), r;
3137
+ }
3138
+ _poseCamera(e, t) {
3139
+ let n = t, r = new L(n[12], n[13], n[14]), i = new L(n[8], n[9], n[10]), a = new L(n[4], n[5], n[6]);
3140
+ this.convertHandedness && (r.z *= -1, i.z *= -1, a.z *= -1), e.up.copy(a.normalize()), e.position.copy(r), e.scale.set(1, 1, 1), e.lookAt(r.clone().add(i)), Kt(t) > 0 !== this.convertHandedness && (e.scale.x = -1);
3141
+ }
3142
+ _verticalFov(e, t) {
3143
+ let n = X(e, "fov", 90);
3144
+ if (t >= 1) return n;
3145
+ let r = n * Math.PI / 180;
3146
+ return 2 * Math.atan(Math.tan(r / 2) / t) * 180 / Math.PI;
3147
+ }
3148
+ _equirectFromInfiniteLight(e, t, n, r) {
3149
+ let i = e.image;
3150
+ if (!i?.width || i.width !== i.height) return i?.width !== i?.height && this.warn(`infinite-light image "${r}" is ${i?.width}x${i?.height}, not square — read as equirectangular, but pbrt would read it as equal-area octahedral`), null;
3151
+ let a = null;
3152
+ if (i.data) {
3153
+ let e = i.data.length / (i.width * i.height);
3154
+ if (!Number.isInteger(e) || e < 3) return null;
3155
+ a = {
3156
+ data: i.data,
3157
+ width: i.width,
3158
+ height: i.height,
3159
+ channels: e,
3160
+ bottomUp: !0
3161
+ };
3162
+ } else if (a = lr(i, e.colorSpace === F), !a) return this.warn(`infinite-light image "${r}" could not be read back for octahedral conversion`), null;
3163
+ let { data: o, width: s, height: c } = Fn(a, t.ctm, n), l = new b(o, s, c, P, T);
3164
+ return l.mapping = C, l.minFilter = A, l.magFilter = A, l.needsUpdate = !0, l;
3165
+ }
3166
+ async _buildEnvironment(e) {
3167
+ let t = e.find((e) => e.type === "infinite");
3168
+ if (!t) return null;
3169
+ let n = X(t.params, "scale", 1), r = Z(t.params, "filename", null);
3170
+ if (r) try {
3171
+ let e = await this.resolveEnvironment(r);
3172
+ if (e) {
3173
+ let i = this._equirectFromInfiniteLight(e, t, n, r);
3174
+ return i ? { texture: i } : (e.mapping = C, { texture: e });
3175
+ }
3176
+ this.warn(`infinite-light image not found: ${r}`);
3177
+ } catch (e) {
3178
+ this.warn(`failed to load infinite-light image ${r}: ${e.message}`);
3179
+ }
3180
+ let i = (await Jn(t.params, "L", {
3181
+ resolveNamedTexture: async () => null,
3182
+ warn: (e) => this.warn(e)
3183
+ })).map((e) => e * n), a = new Float32Array(8);
3184
+ for (let e = 0; e < 2; e++) a[e * 4 + 0] = i[0], a[e * 4 + 1] = i[1], a[e * 4 + 2] = i[2], a[e * 4 + 3] = 1;
3185
+ let o = new b(a, 2, 1, P, T);
3186
+ return o.mapping = C, o.minFilter = A, o.magFilter = A, o.needsUpdate = !0, { texture: o };
3187
+ }
3188
+ async _buildLamps(e, t) {
3189
+ let n = {
3190
+ resolveNamedTexture: async () => null,
3191
+ warn: (e) => this.warn(e)
3192
+ };
3193
+ for (let r of e) {
3194
+ if (!qn.has(r.type)) continue;
3195
+ let e = new N().fromArray(this.convertHandedness ? q($, r.ctm) : r.ctm), i = (t, n) => {
3196
+ let i = r.params[t]?.value;
3197
+ return new L().fromArray(i?.length >= 3 ? i : n).applyMatrix4(e);
3198
+ }, a = await Jn(r.params, r.type === "distant" ? "L" : "I", n), o = X(r.params, "scale", 1), s = i("from", [
3199
+ 0,
3200
+ 0,
3201
+ 0
3202
+ ]), c = i("to", [
3203
+ 0,
3204
+ 0,
3205
+ 1
3206
+ ]), l;
3207
+ if (r.type === "distant") {
3208
+ let e = X(r.params, "illuminance", -1);
3209
+ e > 0 && (o *= e), l = new x(), l.userData.__luxConverted = !0;
3210
+ } else if (r.type === "point") {
3211
+ let e = X(r.params, "power", -1);
3212
+ o *= e > 0 ? e : 4 * Math.PI, l = new se(), l.userData.__candelaConverted = !0;
3213
+ } else {
3214
+ let e = Math.min(Math.max(X(r.params, "coneangle", 30), 0), 90) * Math.PI / 180, t = Math.min(Math.max(X(r.params, "conedeltaangle", 5) * Math.PI / 180, 0), e), n = Math.cos(e), i = Math.cos(e - t), a = X(r.params, "power", -1);
3215
+ a > 0 && (o *= a / (2 * Math.PI * (1 - i + (i - n) / 2))), o *= 4 * Math.PI, l = new fe(), l.angle = e, l.penumbra = n < 1 ? Math.min((i - n) / (1 - n), 1) : 0, l.userData.__candelaConverted = !0;
3216
+ }
3217
+ let u = Math.max(a[0], a[1], a[2]);
3218
+ !(u > 0) || !(o > 0) || (l.color.setRGB(a[0] / u, a[1] / u, a[2] / u), l.intensity = u * o, l.name = `pbrt ${r.type}`, l.position.copy(s), l.decay = 2, l.distance = 0, l.target && (l.target.position.subVectors(c, s), l.add(l.target)), t.add(l));
3219
+ }
3220
+ }
3221
+ _reportUnsupportedLights(e) {
3222
+ for (let t of e) t.type !== "infinite" && !qn.has(t.type) && this.warn(`light "${t.type}" not supported (only infinite, distant, point and spot lights and emissive area lights are mapped)`);
3223
+ }
3224
+ }, vr = 1e3, yr = 2048, br = 1e5;
3225
+ function xr(e) {
3226
+ let t = e[0], n = (e) => {
3227
+ let t = e.lastIndexOf("/"), n = /^(.*?)(\d+)\.pbrt$/i.exec(e.slice(t + 1));
3228
+ return n ? {
3229
+ group: e.slice(0, t + 1) + "\0" + n[1].toLowerCase(),
3230
+ number: parseInt(n[2], 10)
3231
+ } : null;
3232
+ }, r = t ? n(t) : null;
3233
+ if (!r) return null;
3234
+ let i = /* @__PURE__ */ new Map();
3235
+ for (let t of e) {
3236
+ let e = n(t);
3237
+ e && e.group === r.group && !i.has(e.number) && i.set(e.number, t);
3238
+ }
3239
+ return i.size < 2 ? null : [...i].sort((e, t) => e[0] - t[0]).slice(0, vr).map(([e, t]) => ({
3240
+ path: t,
3241
+ number: e
3242
+ }));
3243
+ }
3244
+ function Sr(e) {
3245
+ let t = e instanceof Float32Array || e instanceof Int32Array || e instanceof Uint32Array ? new Uint32Array(e.buffer, e.byteOffset, e.length) : null;
3246
+ t ||= new Uint32Array(Float32Array.from(e).buffer);
3247
+ let n = 2166136261;
3248
+ for (let e = 0; e < t.length; e++) n = Math.imul(n ^ t[e], 16777619), n ^= n >>> 15;
3249
+ return (n >>> 0).toString(36) + ":" + e.length;
3250
+ }
3251
+ function Cr(e) {
3252
+ let t = "";
3253
+ for (let n of Object.keys(e).sort()) {
3254
+ let { type: r, value: i } = e[n], a = ArrayBuffer.isView(i) || i.length > 16 && typeof i[0] == "number";
3255
+ t += `${r} ${n}=${a ? "#" + Sr(i) : i.join(",")};`;
3256
+ }
3257
+ return t;
3258
+ }
3259
+ var wr = class {
3260
+ constructor(e) {
3261
+ this.materialNames = /* @__PURE__ */ new Map();
3262
+ for (let [t, n] of e.namedMaterials) this.materialNames.set(n, t);
3263
+ this.cache = /* @__PURE__ */ new Map();
3264
+ }
3265
+ of(e) {
3266
+ return `${e.type}|${+!!e.reverseOrientation}|${Cr(e.params)}|${this._material(e.material)}|${this._light(e.areaLight)}`;
3267
+ }
3268
+ _material(e) {
3269
+ if (!e) return "-";
3270
+ let t = this.cache.get(e);
3271
+ if (t === void 0) {
3272
+ let n = this.materialNames.get(e) ?? e._missingRef;
3273
+ t = n === void 0 ? "I:" + e.type + ":" + Cr(e.params) : "N:" + n, this.cache.set(e, t);
3274
+ }
3275
+ return t;
3276
+ }
3277
+ _light(e) {
3278
+ if (!e) return "-";
3279
+ let t = this.cache.get(e);
3280
+ return t === void 0 && this.cache.set(e, t = "L:" + e.type + ":" + Cr(e.params)), t;
3281
+ }
3282
+ };
3283
+ function Tr(e, t) {
3284
+ let n = new Int32Array(t.length).fill(-1), r = 0;
3285
+ for (; r < e.length && r < t.length && e[r] === t[r];) n[r] = r, r++;
3286
+ let i = e.length, a = t.length;
3287
+ for (; i > r && a > r && e[i - 1] === t[a - 1];) n[--a] = --i;
3288
+ return r < i && r < a && !Er(e, r, i, t, r, a, n) && Or(e, r, i, t, r, a, n), n;
3289
+ }
3290
+ function Er(e, t, n, r, i, a, o) {
3291
+ let s = n - t, c = a - i, l = Math.min(s + c, yr), u = l + 1, d = new Int32Array(2 * l + 3), f = [];
3292
+ for (let n = 0; n <= l; n++) {
3293
+ for (let a = -n; a <= n; a += 2) {
3294
+ let l = a === -n || a !== n && d[u + a - 1] < d[u + a + 1] ? d[u + a + 1] : d[u + a - 1] + 1, p = l - a;
3295
+ for (; l < s && p < c && e[t + l] === r[i + p];) l++, p++;
3296
+ if (d[u + a] = l, l >= s && p >= c) return f.push(d.slice(u - n, u + n + 1)), Dr(f, s, c, t, i, o), !0;
3297
+ }
3298
+ f.push(d.slice(u - n, u + n + 1));
3299
+ }
3300
+ return !1;
3301
+ }
3302
+ function Dr(e, t, n, r, i, a) {
3303
+ let o = t, s = n;
3304
+ for (let t = e.length - 1; t > 0; t--) {
3305
+ let n = e[t - 1], c = (e) => n[e + t - 1], l = o - s, u = l === -t || l !== t && c(l - 1) < c(l + 1) ? l + 1 : l - 1, d = c(u), f = d - u;
3306
+ for (; o > d && s > f;) a[i + --s] = r + --o;
3307
+ o = d, s = f;
3308
+ }
3309
+ for (; o > 0 && s > 0;) a[i + --s] = r + --o;
3310
+ }
3311
+ function Or(e, t, n, r, i, a, o) {
3312
+ let s = /* @__PURE__ */ new Map();
3313
+ for (let r = t; r < n; r++) {
3314
+ let t = s.get(e[r]);
3315
+ t ? t.push(r) : s.set(e[r], [r]);
3316
+ }
3317
+ let c = /* @__PURE__ */ new Map();
3318
+ for (let e = i; e < a; e++) {
3319
+ let t = s.get(r[e]);
3320
+ if (!t) continue;
3321
+ let n = c.get(r[e]) ?? 0;
3322
+ n < t.length && (o[e] = t[n], c.set(r[e], n + 1));
3323
+ }
3324
+ }
3325
+ function kr(e, t, n, r) {
3326
+ for (let i = 0; i < 16; i++) if (e[t + i] !== n[r + i]) return !1;
3327
+ return !0;
3328
+ }
3329
+ function Ar(e, t, n) {
3330
+ let r = e.length, i = new Float32Array(16 * r), a = null, o = -1;
3331
+ for (let e = 0; e < r; e++) if (t(e)) {
3332
+ if (i.set(n(e), e * 16), o < 0) for (let t = 0; t < e; t++) i.copyWithin(t * 16, e * 16, e * 16 + 16);
3333
+ o = e;
3334
+ } else a ??= new Uint8Array(r).fill(1), a[e] = 0, o >= 0 && i.copyWithin(e * 16, o * 16, o * 16 + 16);
3335
+ return {
3336
+ times: e,
3337
+ matrices: i,
3338
+ visible: a
3339
+ };
3340
+ }
3341
+ function jr(e) {
3342
+ if (e.visible) return !1;
3343
+ let t = e.matrices;
3344
+ for (let e = 16; e < t.length; e += 16) if (!kr(t, 0, t, e)) return !1;
3345
+ return !0;
3346
+ }
3347
+ var Mr = class {
3348
+ constructor(e, t) {
3349
+ this.times = Float32Array.from(e), this.name = t, this.base = null, this.frame = -1, this.keyIds = /* @__PURE__ */ new Map(), this.tracks = [], this.prev = null, this.placements = /* @__PURE__ */ new Map(), this.templates = /* @__PURE__ */ new Map(), this.cameras = [], this.warnings = /* @__PURE__ */ new Set();
3350
+ }
3351
+ addFrame(e) {
3352
+ let t = ++this.frame;
3353
+ t === 0 ? this.base = e : this._adoptDefinitions(e);
3354
+ for (let t of e.warnings) this.warnings.add(t);
3355
+ let n = new wr(e), r = new Int32Array(e.shapes.length);
3356
+ for (let t = 0; t < r.length; t++) {
3357
+ let i = n.of(e.shapes[t]), a = this.keyIds.get(i);
3358
+ a === void 0 && this.keyIds.set(i, a = this.keyIds.size), r[t] = a;
3359
+ }
3360
+ let i = this.prev ? Tr(this.prev.keys, r) : null, a = Array(r.length);
3361
+ for (let o = 0; o < r.length; o++) {
3362
+ let r = e.shapes[o], s = i && i[o] >= 0 ? this.prev.tracks[i[o]] : null;
3363
+ s ? (s.last = t, s.ctms ? s.ctms.push(r.ctm) : r.ctm !== s.shape.ctm && !kr(r.ctm, 0, s.shape.ctm, 0) && (s.ctms = Array(t - s.first).fill(s.shape.ctm), s.ctms.push(r.ctm))) : (s = {
3364
+ shape: r,
3365
+ first: t,
3366
+ last: t,
3367
+ ctms: null
3368
+ }, t > 0 && this._adoptMaterial(r, n.materialNames), this.tracks.push(s)), a[o] = s;
3369
+ }
3370
+ this.prev = {
3371
+ keys: r,
3372
+ tracks: a
3373
+ }, this._addPlacements(e, t, this._resolveTemplates(e, t, n)), this.cameras.push(e.camera ? {
3374
+ cameraToWorld: e.camera.cameraToWorld,
3375
+ fov: e.camera.params?.fov?.value?.[0]
3376
+ } : null);
3377
+ }
3378
+ _adoptDefinitions(e) {
3379
+ for (let [t, n] of e.namedMaterials) this.base.namedMaterials.has(t) || this.base.namedMaterials.set(t, n);
3380
+ for (let [t, n] of e.namedTextures) this.base.namedTextures.has(t) || this.base.namedTextures.set(t, n);
3381
+ }
3382
+ _resolveTemplates(e, t, n) {
3383
+ let r = /* @__PURE__ */ new Map();
3384
+ for (let [i, a] of e.objects) {
3385
+ let e = "";
3386
+ for (let t of a) e += n.of(t) + "@" + Array.prototype.join.call(t.relativeCTM || t.ctm, ",") + ";";
3387
+ let o = i + "\0" + e, s = this.templates.get(o);
3388
+ if (s === void 0 && (s = t === 0 || !this.base.objects.has(i) ? i : `${i} @${t}`, this.templates.set(o, s), t > 0)) {
3389
+ for (let e of a) this._adoptMaterial(e, n.materialNames);
3390
+ this.base.objects.set(s, a);
3391
+ }
3392
+ r.set(i, s);
3393
+ }
3394
+ return r;
3395
+ }
3396
+ _adoptMaterial(e, t) {
3397
+ let n = e.material && t.get(e.material);
3398
+ n !== void 0 && (e.material = this.base.namedMaterials.get(n) ?? e.material);
3399
+ }
3400
+ _addPlacements(e, t, n) {
3401
+ for (let [r, i] of e.instances) {
3402
+ let e = n.get(r) ?? r, a = this.placements.get(e);
3403
+ if (a || this.placements.set(e, a = {
3404
+ counts: new Int32Array(this.times.length),
3405
+ diffs: /* @__PURE__ */ new Map()
3406
+ }), a.counts[t] = i.count, t === 0) continue;
3407
+ let o = this.base.instances.get(e), s = o?.count ?? 0;
3408
+ for (let e = 0; e < i.count; e++) {
3409
+ let n = e * 16;
3410
+ if (e < s && kr(i.matrices, n, o.matrices, n)) continue;
3411
+ let r = a.diffs.get(e);
3412
+ r || a.diffs.set(e, r = /* @__PURE__ */ new Map()), r.set(t, i.matrices.slice(n, n + 16));
3413
+ }
3414
+ }
3415
+ }
3416
+ finish() {
3417
+ let e = this.base, t = this.times, n = this.frame + 1;
3418
+ e.shapes = this.tracks.map((e) => {
3419
+ let { shape: r, first: i, last: a, ctms: o } = e;
3420
+ return (i > 0 || a < n - 1 || o) && (r.motion = Ar(t, (e) => e >= i && e <= a, (e) => o ? o[e - i] : r.ctm)), r;
3421
+ }), e.warnings = [...this.warnings], e.animatedInstances = this._finishPlacements(e, t, n);
3422
+ let r = this.cameras;
3423
+ if (r[0] && r.every(Boolean)) {
3424
+ let n = Ar(t, () => !0, (e) => r[e].cameraToWorld), i = r.map((e) => e.fov ?? r[0].fov), a = i.some((e) => e !== i[0]);
3425
+ (!jr(n) || a) && (e.camera.motion = {
3426
+ ...n,
3427
+ fov: a ? Float32Array.from(i) : null
3428
+ });
3429
+ }
3430
+ return e.animation = {
3431
+ name: this.name,
3432
+ duration: t[n - 1],
3433
+ frames: n
3434
+ }, e;
3435
+ }
3436
+ _finishPlacements(e, t, n) {
3437
+ let r = [], i = 0;
3438
+ for (let [a, { counts: o, diffs: s }] of this.placements) {
3439
+ let c = e.instances.get(a), l = c?.count ?? 0, u = Math.max(...o.subarray(0, n)), d = [];
3440
+ for (let e = 0; e < u; e++) {
3441
+ let u = s.get(e), f = !!u;
3442
+ for (let t = 0; t < n && !f; t++) f = e >= o[t];
3443
+ if (f && r.length < br) {
3444
+ let n = Ar(t, (t) => e < o[t], (t) => u?.get(t) ?? c.matrices.subarray(e * 16, e * 16 + 16));
3445
+ r.push({
3446
+ name: a,
3447
+ motion: n
3448
+ });
3449
+ } else f && i++, e < l && d.push(e);
3450
+ }
3451
+ if (c && d.length !== l) {
3452
+ let t = new Float32Array(d.length * 16);
3453
+ d.forEach((e, n) => t.set(c.matrices.subarray(e * 16, e * 16 + 16), n * 16)), e.instanceCount -= l - d.length, c.matrices = t, c.matricesEnd = null, c.count = d.length;
3454
+ }
3455
+ }
3456
+ return i > 0 && e.warnings.push(`${i.toLocaleString()} moving placement(s) past ${br.toLocaleString()} left static`), r;
3457
+ }
3458
+ };
3459
+ function Nr(e) {
3460
+ if (!e.hasMotion) return e;
3461
+ let { start: t, end: n } = e.transformTimes, r = n - t;
3462
+ if (!(r > 0)) return e;
3463
+ let i = Float32Array.of(0, r), a = (e, t, n, r) => {
3464
+ let a = new Float32Array(32);
3465
+ for (let i = 0; i < 16; i++) a[i] = e[t + i], a[16 + i] = n[r + i];
3466
+ return {
3467
+ times: i,
3468
+ matrices: a,
3469
+ visible: null
3470
+ };
3471
+ };
3472
+ for (let t of e.shapes) t.ctmEnd && (t.motion = a(t.ctm, 0, t.ctmEnd, 0));
3473
+ e.animatedInstances = [];
3474
+ for (let t of e.instances.values()) {
3475
+ if (!t.matricesEnd) continue;
3476
+ let n = [];
3477
+ for (let r = 0; r < t.count; r++) {
3478
+ let i = r * 16;
3479
+ kr(t.matrices, i, t.matricesEnd, i) || e.animatedInstances.length >= br ? n.push(r) : e.animatedInstances.push({
3480
+ name: t.name,
3481
+ motion: a(t.matrices, i, t.matricesEnd, i)
3482
+ });
3483
+ }
3484
+ if (n.length !== t.count) {
3485
+ let r = new Float32Array(n.length * 16);
3486
+ n.forEach((e, n) => r.set(t.matrices.subarray(e * 16, e * 16 + 16), n * 16)), e.instanceCount -= t.count - n.length, t.matrices = r, t.count = n.length;
3487
+ }
3488
+ t.matricesEnd = null;
3489
+ }
3490
+ return e.camera?.cameraToWorldEnd && (e.camera.motion = {
3491
+ ...a(e.camera.cameraToWorld, 0, e.camera.cameraToWorldEnd, 0),
3492
+ fov: null
3493
+ }), e.animation = {
3494
+ name: "Shutter motion",
3495
+ duration: r,
3496
+ frames: 2
3497
+ }, e;
3498
+ }
3499
+ //#endregion
3500
+ //#region src/Processor/PBRT/index.js
3501
+ var Pr = 2e6, Fr = new TextDecoder();
3502
+ function Ir(e) {
3503
+ let t = e.replace(/\\/g, "/").split("/"), n = [];
3504
+ for (let e of t) e === "" || e === "." || (e === ".." ? n.pop() : n.push(e));
3505
+ return n.join("/");
3506
+ }
3507
+ function Lr(e, t) {
3508
+ return !e || t.startsWith("/") ? Ir(t) : Ir(`${e}/${t}`);
3509
+ }
3510
+ var Rr = class {
3511
+ constructor(e, t = null) {
3512
+ this.entries = e, this.source = t, this.records = [], this.byPath = /* @__PURE__ */ new Map(), this.byBase = /* @__PURE__ */ new Map();
3513
+ let n = (e, t, n) => {
3514
+ let r = Ir(e).toLowerCase();
3515
+ if (this.byPath.has(r)) return;
3516
+ let i = {
3517
+ key: e,
3518
+ norm: r,
3519
+ bytes: t,
3520
+ lazy: n
3521
+ };
3522
+ this.records.push(i), this.byPath.set(r, i);
3523
+ let a = r.split("/").pop(), o = this.byBase.get(a);
3524
+ o ? o.push(i) : this.byBase.set(a, [i]);
3525
+ };
3526
+ for (let t in e) n(t, e[t], !1);
3527
+ for (let e of t?.listing ?? []) e.offset !== void 0 && n(e.path, null, !0);
3528
+ }
3529
+ async read(e) {
3530
+ return e ? e.bytes ? e.bytes : !e.lazy || !this.source ? null : this.source.read(e.key) : null;
3531
+ }
3532
+ async readHead(e, t) {
3533
+ return e ? e.bytes ? e.bytes.subarray(0, t) : !e.lazy || !this.source ? null : this.source.readHead ? this.source.readHead(e.key, t) : this.source.read(e.key) : null;
3534
+ }
3535
+ async readPath(e, t = null) {
3536
+ let n = t !== null && this.findRecord(Lr(t, e)) || this.findRecord(e);
3537
+ return this.read(n);
3538
+ }
3539
+ release(e) {
3540
+ e.bytes = null, delete this.entries[e.key];
3541
+ }
3542
+ releasePath(e, t = null, n = null) {
3543
+ let r = t !== null && this.findRecord(Lr(t, e)) || this.findRecord(e);
3544
+ r?.lazy && (this.release(r), H(n));
3545
+ }
3546
+ releaseScenes() {
3547
+ for (let e of this.records) e.norm.endsWith(".pbrt") && this.release(e);
3548
+ }
3549
+ findRecord(e) {
3550
+ let t = Ir(e).toLowerCase();
3551
+ if (this.byPath.has(t)) return this.byPath.get(t);
3552
+ let n = this.byBase.get(t.split("/").pop());
3553
+ return n ? n.find((e) => e.norm.endsWith("/" + t)) || (n.length === 1 ? n[0] : null) : null;
3554
+ }
3555
+ find(e) {
3556
+ return this.findRecord(e)?.bytes ?? null;
3557
+ }
3558
+ };
3559
+ function zr(e, t, n = 0) {
3560
+ let r = t[0], i = e.length - t.length;
3561
+ outer: for (let a = n; a <= i; a++) if (e[a] === r) {
3562
+ for (let n = 1; n < t.length; n++) if (e[a + n] !== t[n]) continue outer;
3563
+ return a;
3564
+ }
3565
+ return -1;
3566
+ }
3567
+ var Br = (e) => Uint8Array.from(e, (e) => e.charCodeAt(0)), Vr = 4 * 1024 * 1024, Hr = Br("WorldBegin"), Ur = [Br("Include"), Br("Import")];
3568
+ function Wr(e, t) {
3569
+ for (let n = t - 1; n >= 0; n--) {
3570
+ let t = e[n];
3571
+ if (t === 10) return !0;
3572
+ if (t !== 32 && t !== 9) return !1;
3573
+ }
3574
+ return !0;
3575
+ }
3576
+ function Gr(e) {
3577
+ for (let t = zr(e, Hr); t >= 0; t = zr(e, Hr, t + 1)) if (Wr(e, t)) return !0;
3578
+ return !1;
3579
+ }
3580
+ function Kr(e, t) {
3581
+ for (let n of Ur) for (let r = zr(e, n); r >= 0; r = zr(e, n, r + 1)) {
3582
+ if (!Wr(e, r)) continue;
3583
+ let i = r + n.length;
3584
+ for (; i < e.length && (e[i] === 32 || e[i] === 9);) i++;
3585
+ if (e[i] !== 34) continue;
3586
+ let a = ++i;
3587
+ for (; i < e.length && e[i] !== 34 && e[i] !== 10;) i++;
3588
+ if (e[i] !== 34) continue;
3589
+ let o = Fr.decode(e.subarray(a, i));
3590
+ t.add(Ir(o).toLowerCase().split("/").pop());
3591
+ }
3592
+ }
3593
+ function qr(e) {
3594
+ let t = Object.keys(e).filter((e) => e.toLowerCase().endsWith(".pbrt"));
3595
+ if (t.length <= 1) return t;
3596
+ let n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
3597
+ for (let i of t) {
3598
+ let t = e[i];
3599
+ r.set(i, Gr(t)), Kr(t, n);
3600
+ }
3601
+ return Jr(t, r, n);
3602
+ }
3603
+ function Jr(e, t, n) {
3604
+ let r = (e, t) => {
3605
+ let n = e.filter(t);
3606
+ return n.length ? n : e;
3607
+ }, i = r(e, (e) => t.get(e));
3608
+ return i = r(i, (e) => !n.has(e.toLowerCase().split("/").pop())), i = r(i, (e) => /(^|\/)(scene|main)\.pbrt$/i.test(e)), i.sort((e, t) => {
3609
+ let n = e.split("/").length, r = t.split("/").length;
3610
+ return n === r ? e.length === t.length ? e < t ? -1 : +(e > t) : e.length - t.length : n - r;
3611
+ });
3612
+ }
3613
+ function Yr(e) {
3614
+ return qr(e)[0] || null;
3615
+ }
3616
+ async function Xr(e) {
3617
+ let t = e.records.filter((e) => e.norm.endsWith(".pbrt"));
3618
+ if (t.length === 0) return [];
3619
+ if (t.length === 1) return [t[0].key];
3620
+ let n = async (n) => {
3621
+ let r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Map(), a = !1;
3622
+ for (let o of t) {
3623
+ let t = await n(o);
3624
+ t && (i.set(o.key, Gr(t)), Kr(t, r), t.length >= Vr && (a = !0), o.lazy && (e.release(o), H(t)));
3625
+ }
3626
+ return {
3627
+ included: r,
3628
+ worlds: i,
3629
+ cut: a
3630
+ };
3631
+ }, r = await n((t) => e.readHead(t, Vr)), { included: i, worlds: a } = [...r.worlds.values()].filter(Boolean).length === 1 || !r.cut ? r : await n((t) => e.read(t));
3632
+ return Jr(t.map((e) => e.key), a, i);
3633
+ }
3634
+ async function Zr(e) {
3635
+ let { vfs: t, source: n, plyParser: r, imageFromBytes: i, envFromBytes: a, convertHandedness: o } = e, s = new Rr(t, n), c = [], l = await Xr(s);
3636
+ if (l.length === 0) throw Error("PBRT loader: no .pbrt file found in archive");
3637
+ let u = e.entryPath && l.includes(e.entryPath) ? e.entryPath : null;
3638
+ e.entryPath && !u && c.push(`PBRT entry "${e.entryPath}" is not a scene in this archive — auto-detecting instead`);
3639
+ let d = u || l[0], f = async (t) => {
3640
+ let n = await s.readPath(t);
3641
+ if (!n) throw Error(`PBRT loader: entry "${t}" not readable`);
3642
+ let r = await new cn({
3643
+ resolveInclude: (e, t) => s.readPath(e, t),
3644
+ releaseInclude: (e, t, n) => s.releasePath(e, t, n),
3645
+ maxPlacements: e.maxPlacements,
3646
+ instanceIncludes: e.instanceIncludes
3647
+ }).parse(n, t.includes("/") ? t.slice(0, t.lastIndexOf("/")) : "");
3648
+ return s.releasePath(t, null, n), r;
3649
+ }, p = performance.now(), m = !u && e.animation !== !1 ? xr(l) : null, h;
3650
+ if (m) {
3651
+ let e = await f(m[0].path);
3652
+ if (e.shapes.length + e.instanceCount > Pr) c.push(`${m.length} animation frames found, but the scene is too large to parse once per frame — loaded "${m[0].path}" only`), m = null, h = Nr(e);
3653
+ else {
3654
+ let t = new Mr(m.map((e) => (e.number - m[0].number) / 30), Qr(m));
3655
+ t.addFrame(e);
3656
+ for (let e = 1; e < m.length; e++) t.addFrame(await f(m[e].path));
3657
+ h = t.finish();
3658
+ }
3659
+ } else h = Nr(await f(d));
3660
+ let g = performance.now() - p;
3661
+ s.releaseScenes();
3662
+ let _ = (e) => e.byteOffset === 0 && e.byteLength === e.buffer.byteLength ? e.buffer : e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength), v = /* @__PURE__ */ new Map(), y = async (e) => {
3663
+ let t = await s.read(e);
3664
+ return t ? (s.release(e), r(_(t))) : null;
3665
+ }, b = new _r({
3666
+ convertHandedness: o,
3667
+ maxTriangles: e.maxTriangles,
3668
+ maxPlacements: e.maxPlacements,
3669
+ mergeShapesAbove: e.mergeShapesAbove,
3670
+ curveSteps: e.curveSteps,
3671
+ curveSides: e.curveSides,
3672
+ curveTolerance: e.curveTolerance,
3673
+ resolvePLY: async (e) => {
3674
+ let t = s.findRecord(e);
3675
+ return t ? (v.has(t.norm) || v.set(t.norm, await y(t)), v.get(t.norm)) : null;
3676
+ },
3677
+ resolveImage: async (e) => {
3678
+ let t = await s.readPath(e);
3679
+ return t ? i(t, e) : null;
3680
+ },
3681
+ resolveEnvironment: async (e) => {
3682
+ let t = await s.readPath(e);
3683
+ return t ? (a || i)(t, e) : null;
3684
+ }
3685
+ }), x = performance.now(), S = await b.build(h);
3686
+ return {
3687
+ ...S,
3688
+ entryPath: m ? m[0].path : d,
3689
+ frames: m ? m.map((e) => e.path) : null,
3690
+ candidates: l,
3691
+ warnings: [...c, ...S.warnings ?? []],
3692
+ parseMs: g,
3693
+ buildMs: performance.now() - x
3694
+ };
3695
+ }
3696
+ function Qr(e) {
3697
+ let t = e[0].path.split("/");
3698
+ return `${t.length > 1 ? t[t.length - 2] : t[0].replace(/\d+\.pbrt$/i, "") || "frames"} · frames ${e[0].number}–${e[e.length - 1].number}`;
3699
+ }
3700
+ //#endregion
3701
+ //#region src/Processor/PBRT/PFM.js
3702
+ function $r(e) {
3703
+ let t = 0, n = () => {
3704
+ for (; t < e.length && e[t] <= 32;) t++;
3705
+ let n = t;
3706
+ for (; t < e.length && e[t] > 32;) t++;
3707
+ return String.fromCharCode(...e.subarray(n, t));
3708
+ }, r = n();
3709
+ if (r !== "PF" && r !== "Pf") throw Error("not a PFM image");
3710
+ let i = parseInt(n(), 10), a = parseInt(n(), 10), o = parseFloat(n());
3711
+ t++;
3712
+ let s = r === "PF" ? 3 : 1, c = i * a;
3713
+ if (!(i > 0 && a > 0) || !Number.isFinite(o) || o === 0) throw Error("bad PFM header");
3714
+ if (e.length - t < c * s * 4) throw Error("PFM image is truncated");
3715
+ let l = new DataView(e.buffer, e.byteOffset + t, c * s * 4), u = o < 0, d = Math.abs(o), f = new Float32Array(c * 4);
3716
+ for (let e = 0; e < c; e++) {
3717
+ for (let t = 0; t < 3; t++) f[e * 4 + t] = l.getFloat32((e * s + (s === 3 ? t : 0)) * 4, u) * d;
3718
+ f[e * 4 + 3] = 1;
3719
+ }
3720
+ return {
3721
+ data: f,
3722
+ width: i,
3723
+ height: a
3724
+ };
3725
+ }
3726
+ function ei(e) {
3727
+ let { data: t, width: n, height: r } = $r(e), i = new b(t, n, r, P, T);
3728
+ return i.minFilter = A, i.magFilter = A, i.needsUpdate = !0, i;
3729
+ }
3730
+ //#endregion
3731
+ //#region src/Processor/ArchiveImporter.js
3732
+ var ti = /* @__PURE__ */ e({
3733
+ ARCHIVE_ELEMENT_PROMPT_BYTES: () => ai,
3734
+ ArchiveImporter: () => oi
3735
+ }), ni = /\.(usd|usda|usdc)$/i, ri = /\.(png|jpg|jpeg|avif)$/i, ii = 3e4, ai = 4e9, oi = class e {
3736
+ static FORMATS = f;
3737
+ constructor(e) {
3738
+ this.loader = e, this._pendingGraph = null, this.lastPBRTStats = null;
3739
+ }
3740
+ release() {
3741
+ this._pendingGraph = null;
3742
+ }
3743
+ async _readHead(e, t = 512) {
3744
+ return e instanceof Uint8Array ? e.subarray(0, t) : typeof e.slice == "function" && typeof e.arrayBuffer == "function" ? new Uint8Array(await e.slice(0, t).arrayBuffer()) : new Uint8Array((await this.loader.readFileAsArrayBuffer(e)).slice(0, t));
3745
+ }
3746
+ async inspectArchive(e) {
3747
+ let n = Te(await this._readHead(e)), r = (e) => t({
3748
+ isLoading: !0,
3749
+ status: `Scanning archive… ${(e.bytes / 1e9).toFixed(1)} GB`,
3750
+ progress: 4
3751
+ }), i;
3752
+ if (n === "gzip") {
3753
+ let t = await this._unpackGzip(e, e.name);
3754
+ t ? (i = t.index.listing, t.release()) : {listing: i} = await Le(e, {
3755
+ filter: () => !1,
3756
+ onProgress: r
3757
+ });
3758
+ } else i = n === "tar" ? (await Je(e, this.loader.storage))?.listing ?? await ze(e, { onProgress: r }) : (await p(e)).map((e) => ({
3759
+ path: e.name,
3760
+ size: e.size
3761
+ }));
3762
+ let { root: a, elements: o } = Ue(i);
3763
+ return {
3764
+ kind: n ?? "zip",
3765
+ root: a,
3766
+ elements: o,
3767
+ entryCount: i.length,
3768
+ totalBytes: i.reduce((e, t) => e + t.size, 0)
3769
+ };
3770
+ }
3771
+ async _unpackGzip(e, n) {
3772
+ try {
3773
+ return await qe(e, {
3774
+ storage: this.loader.storage,
3775
+ label: n,
3776
+ onProgress: (e) => t({
3777
+ isLoading: !0,
3778
+ status: `Unpacking archive… ${(e / 1e9).toFixed(1)} GB`,
3779
+ progress: 4
3780
+ })
3781
+ });
3782
+ } catch (e) {
3783
+ return console.warn(`Unpacking "${n}" to storage failed, reading it in memory: ${e.message}`), null;
3784
+ }
3785
+ }
3786
+ async loadArchiveFromFile(e, t, { pbrtEntry: n = null, element: r = null, byteBudget: i, promptBytes: a, ...o } = {}) {
3787
+ try {
3788
+ let s = Te(await this._readHead(e)), c = this.loader.storage ? u(await l(e)) : null, d = {
3789
+ element: r,
3790
+ promptBytes: a,
3791
+ pbrtEntry: n,
3792
+ pbrt: o,
3793
+ archiveId: c
3794
+ };
3795
+ if (s === "tar") return await this._loadSeekable(e, t, d);
3796
+ if (s === "gzip") {
3797
+ let a = await this._unpackGzip(e, t);
3798
+ if (a) try {
3799
+ return await this._loadSeekable(a.file, t, {
3800
+ ...d,
3801
+ index: a.index
3802
+ });
3803
+ } finally {
3804
+ a.release();
3805
+ }
3806
+ let l = await this._readStreamedArchive(e, t, s, r, i);
3807
+ if (Yr(l)) return await this.loadPBRTFromZip(l, t, n, o);
3808
+ let u = await this._loadNonPBRTArchive(l, t);
3809
+ return this.loader._sourceKey = this.loader._keyed("archive", c), u;
3810
+ }
3811
+ return await this._loadSeekable(e, t, {
3812
+ ...d,
3813
+ zip: !0
3814
+ });
3815
+ } catch (e) {
3816
+ throw e?.code !== "ARCHIVE_NEEDS_ELEMENT" && console.error("Error loading archive:", e), e;
3817
+ }
3818
+ }
3819
+ async _loadNonPBRTArchive(e, t) {
3820
+ return await this.processObjMtlPairsInZip(e, t) || await this.findAndLoadModelFromZip(e, t);
3821
+ }
3822
+ async _loadSeekable(e, t, { element: n, promptBytes: r, pbrtEntry: i, pbrt: a, index: o = null, zip: s = !1, archiveId: c = null }) {
3823
+ let l = await this._openSeekableArchive(e, t, n, r, {
3824
+ index: o,
3825
+ zip: s
3826
+ });
3827
+ if (l.archiveId = c, l.elements = (Array.isArray(n) ? n : [n]).filter(Boolean), l.listing.some((e) => e.path.toLowerCase().endsWith(".pbrt"))) return await this.loadPBRTFromZip({}, t, i, a, l);
3828
+ for (let e of l.listing) e.offset !== void 0 && (l.entries[e.path] ??= await l.read(e.path));
3829
+ let u = await this._loadNonPBRTArchive(l.entries, t);
3830
+ return this.loader._sourceKey = this.loader._keyed("archive", c), u;
3831
+ }
3832
+ async _loadStoredGraph(e, n, { imageFromBytes: r, envFromBytes: i }) {
3833
+ let o = this.loader.storage?.area(s.SCENES), c = o ? await o.open(e) : null;
3834
+ if (!c) return null;
3835
+ let l = performance.now(), u = null;
3836
+ try {
3837
+ t({
3838
+ isLoading: !0,
3839
+ status: "Opening the stored scene...",
3840
+ progress: 6
3841
+ });
3842
+ let e = await c.json("graph.json"), a = await c.file("data.bin"), o = new Rr({}, n), s = await xt(e, a, { loadTexture: async (e, t) => {
3843
+ let n = await o.readPath(e);
3844
+ return n ? t === "environment" ? i(n, e) : r(n, e) : null;
3845
+ } });
3846
+ return {
3847
+ ...s.stats,
3848
+ group: s.root,
3849
+ environment: s.environment ? { texture: s.environment } : null,
3850
+ animations: s.animations,
3851
+ report: null,
3852
+ parseMs: 0,
3853
+ buildMs: performance.now() - l,
3854
+ fromStorage: !0
3855
+ };
3856
+ } catch (e) {
3857
+ return u = e, null;
3858
+ } finally {
3859
+ c.release(), u && (console.warn(`The stored scene could not be used, parsing the archive instead: ${u.message}`), this.loader._issues?.warn(a.STORAGE_ENTRY_CORRUPT, `stored scene unusable: ${u.message}`, { key: e }), o.remove(e).catch(() => {}));
3860
+ }
3861
+ }
3862
+ _storeGraph(e, t, n) {
3863
+ let i = this.loader.storage?.area(s.SCENES);
3864
+ if (!i) return;
3865
+ let a;
3866
+ try {
3867
+ a = mt(t.group, {
3868
+ environment: t.environment?.texture ?? null,
3869
+ animations: t.animations ?? [],
3870
+ stats: {
3871
+ meshCount: t.meshCount,
3872
+ entryPath: t.entryPath,
3873
+ candidates: t.candidates,
3874
+ frames: t.frames,
3875
+ warnings: t.warnings,
3876
+ triangleCount: t.triangleCount,
3877
+ placementCount: t.placementCount,
3878
+ mergedShapes: t.mergedShapes,
3879
+ skippedForBudget: t.skippedForBudget,
3880
+ droppedNoTemplate: t.droppedNoTemplate
3881
+ }
3882
+ });
3883
+ } catch (e) {
3884
+ e instanceof V ? console.info(`Scene not stored for next time: ${e.message}`) : console.warn("Scene not stored for next time:", e);
3885
+ return;
3886
+ }
3887
+ let o = {
3888
+ key: e,
3889
+ encoded: a,
3890
+ label: n,
3891
+ parseMs: (t.parseMs ?? 0) + (t.buildMs ?? 0),
3892
+ triangles: t.triangleCount ?? null
3893
+ };
3894
+ r(o.parseMs, a.byteLength) ? this._writeGraph(i, o) : this._pendingGraph = o;
3895
+ }
3896
+ flushPendingGraph(e) {
3897
+ let t = this._pendingGraph;
3898
+ this._pendingGraph = null;
3899
+ let n = this.loader.storage?.area(s.SCENES);
3900
+ !t || !n || !r(t.parseMs + e, t.encoded.byteLength) || this._writeGraph(n, t);
3901
+ }
3902
+ _writeGraph(e, t) {
3903
+ (async () => {
3904
+ let n = await e.create(t.key, {
3905
+ label: `${t.label} (scene)`,
3906
+ expectedBytes: t.encoded.byteLength
3907
+ });
3908
+ if (n) try {
3909
+ await ht(n, t.encoded, { release: !0 }), await n.commit({ triangles: t.triangles });
3910
+ } catch (e) {
3911
+ await n.abort(), console.warn("Storing the scene failed:", e);
3912
+ }
3913
+ })().catch((e) => console.warn("Storing the scene failed:", e));
3914
+ }
3915
+ async _openSeekableArchive(e, n, r, i, { index: a = null, zip: o = !1 } = {}) {
3916
+ let s = Array.isArray(r) ? r.filter(Boolean) : r ? [r] : [], c = s.length ? He(s) : null, l;
3917
+ if (o) l = await m(e, { filter: c });
3918
+ else {
3919
+ let n = a ?? await Je(e, this.loader.storage);
3920
+ l = await Ve(e, {
3921
+ index: n,
3922
+ headersOnly: !0,
3923
+ filter: c,
3924
+ onProgress: (e) => t({
3925
+ isLoading: !0,
3926
+ status: `Indexing archive… ${(e.bytes / 1e9).toFixed(1)} GB`,
3927
+ progress: 4
3928
+ })
3929
+ }), n || Ye(e, this.loader.storage, l.index).catch(() => {});
3930
+ }
3931
+ let u = l.listing.reduce((e, t) => e + t.size, 0);
3932
+ return s.length === 0 && this._requireElementChoice(n, l.listing, u, i), console.info(`Archive "${n}": indexed ${l.indexed} entries, ${(u / 1e9).toFixed(1)} GB, none resident` + (s.length ? `, ${s.length} part${s.length > 1 ? "s" : ""} selected.` : ".")), l;
3933
+ }
3934
+ _requireElementChoice(e, t, n, r = ai) {
3935
+ if (n < r) return;
3936
+ let { root: i, elements: s } = Ue(t);
3937
+ if (s.length < 2) return;
3938
+ let c = /* @__PURE__ */ Error(`"${e}" holds ${(n / 1e9).toFixed(1)} GB across ${s.length} parts. Choose which to load — loading all of them at once may exhaust memory.`);
3939
+ throw c.code = "ARCHIVE_NEEDS_ELEMENT", c.root = i, c.elements = s, c.totalBytes = n, this.loader._issues?.record(a.ASSET_ARCHIVE_TOO_LARGE, `archive holds ${(n / 1e9).toFixed(1)} GB across ${s.length} parts; choose which to load`, {
3940
+ root: i,
3941
+ elements: s.map((e) => e.prefix),
3942
+ totalBytes: n
3943
+ }, o.WARNING), c;
3944
+ }
3945
+ async _readStreamedArchive(e, n, r, i, s) {
3946
+ let c = Array.isArray(i) ? i.filter(Boolean) : i ? [i] : [], { entries: l, listing: u, retainedBytes: d, truncated: f } = await Le(e, {
3947
+ filter: c.length ? He(c) : null,
3948
+ ...s === void 0 ? {} : { byteBudget: s },
3949
+ onProgress: (e) => t({
3950
+ isLoading: !0,
3951
+ status: `Reading archive… ${(e.bytes / 1e9).toFixed(1)} GB`,
3952
+ progress: 4
3953
+ })
3954
+ });
3955
+ if (f && c.length === 0) {
3956
+ let { root: e, elements: t } = Ue(u), r = u.reduce((e, t) => e + t.size, 0), i = /* @__PURE__ */ Error(`"${n}" unpacks to ${(r / 1e9).toFixed(1)} GB, past the load budget. Load one of its ${t.length} parts instead.`);
3957
+ throw i.code = "ARCHIVE_NEEDS_ELEMENT", i.root = e, i.elements = t, i.totalBytes = r, this.loader._issues?.record(a.ASSET_ARCHIVE_TOO_LARGE, `archive unpacks to ${(r / 1e9).toFixed(1)} GB; choose one of ${t.length} parts`, {
3958
+ root: e,
3959
+ elements: t.map((e) => e.prefix),
3960
+ totalBytes: r
3961
+ }, o.WARNING), i;
3962
+ }
3963
+ if (f) {
3964
+ let e = He(c), t = u.filter((t) => e(t.path, t.size) && !l[t.path]).length;
3965
+ this.loader._issues?.record(a.ASSET_ARCHIVE_TOO_LARGE, `${t} files of the chosen parts were left out: they did not fit the ${((s ?? 15e8) / 1e9).toFixed(1)} GB in-memory read budget`, {
3966
+ elements: c,
3967
+ missing: t,
3968
+ retainedBytes: d
3969
+ }, o.WARNING);
3970
+ }
3971
+ return c.length && console.info(`Archive "${n}": loaded ${c.map((e) => `"${e}"`).join(", ")} — ${Object.keys(l).length} of ${u.length} files, ${(d / 1048576).toFixed(1)} MB.`), l;
3972
+ }
3973
+ async loadPBRTFromZip(e, r, s = null, l = {}, u = null) {
3974
+ if (t({
3975
+ isLoading: !0,
3976
+ status: "Parsing PBRT scene...",
3977
+ progress: 5
3978
+ }), !this.loader.loaderCache.ply) {
3979
+ let { PLYLoader: e } = await import("three/examples/jsm/loaders/PLYLoader.js");
3980
+ this.loader.loaderCache.ply = new e();
3981
+ }
3982
+ let d = (e) => this.loader.loaderCache.ply.parse(e), f = async (e, t) => {
3983
+ let n = await this._pbrtTextureFromBytes(e, t);
3984
+ return n && (n.userData[Ze] = t), n;
3985
+ }, p = async (e, t) => {
3986
+ let r = t.split(".").pop().toLowerCase(), i = new Blob([e]), a = r === "pfm" ? null : URL.createObjectURL(i);
3987
+ try {
3988
+ let o = a ? await this.loader.loadEnvironmentByExtension(a, r, { loader: r === "exr" ? this._exrLoader() : null }) : ei(e);
3989
+ return o.mapping = C, n(o, `bytes:${await c(i)}`), o.userData[Ze] = t, o.userData[Qe] = "environment", o;
3990
+ } finally {
3991
+ a && URL.revokeObjectURL(a);
3992
+ }
3993
+ }, m = performance.now(), h = {
3994
+ animation: l.animation,
3995
+ maxTriangles: l.maxTriangles,
3996
+ maxPlacements: l.maxPlacements,
3997
+ mergeShapesAbove: l.mergeShapesAbove,
3998
+ curveSteps: l.curveSteps,
3999
+ curveSides: l.curveSides,
4000
+ curveTolerance: l.curveTolerance,
4001
+ instanceIncludes: l.instanceIncludes
4002
+ }, g = u?.archiveId ? this.loader._keyed(`pbrt:2.8:${i}`, `${u.archiveId}|${s ?? ""}|${[...u.elements ?? []].sort().join(",")}|${JSON.stringify(h)}`) : null, _ = g ? await this._loadStoredGraph(g, u, {
4003
+ imageFromBytes: f,
4004
+ envFromBytes: p
4005
+ }) : null, v = _ ?? await Zr({
4006
+ vfs: e,
4007
+ source: u,
4008
+ entryPath: s,
4009
+ plyParser: d,
4010
+ imageFromBytes: f,
4011
+ envFromBytes: p,
4012
+ ...h
4013
+ }), { group: y, environment: b, animations: x, report: S, warnings: w, meshCount: T, entryPath: E, candidates: D, frames: O, parseMs: k, buildMs: A, triangleCount: j, placementCount: M, mergedShapes: N, skippedForBudget: ee, droppedNoTemplate: te } = v;
4014
+ _ && console.info(`PBRT scene "${E}" opened from storage in ${(A / 1e3).toFixed(1)} s, skipping the parse`), this.lastPBRTStats = {
4015
+ parseMs: k,
4016
+ buildMs: A,
4017
+ loaderMs: performance.now() - m,
4018
+ triangleCount: j,
4019
+ placementCount: M,
4020
+ mergedShapes: N,
4021
+ skippedForBudget: ee,
4022
+ droppedNoTemplate: te,
4023
+ meshCount: T,
4024
+ frames: O?.length ?? 1
4025
+ };
4026
+ let ne = new Set(O ?? [E]), re = D.filter((e) => !ne.has(e));
4027
+ if (re.length > 0 && (console.warn(`PBRT archive holds ${D.length} scenes; loaded "${E}". Others: ${re.join(", ")}`), this.loader._issues?.record(a.ASSET_AMBIGUOUS_ENTRY, `archive holds ${D.length} pbrt scenes; loaded "${E}"`, {
4028
+ loaded: E,
4029
+ alternatives: re
4030
+ }, o.WARNING)), S && S.length && typeof console.table == "function") {
4031
+ let e = S.length < T ? ` (first ${S.length} of ${T})` : "";
4032
+ console.groupCollapsed(`PBRT loader: ${T} mesh(es) from "${E}"${e}`), console.table(S), console.groupEnd();
4033
+ }
4034
+ return w && w.length && (console.warn(`PBRT loader: ${w.length} warning(s) parsing "${E}"`), w.forEach((e) => console.warn(" •", e))), b?.texture && (b.texture.generateMipmaps = !0, this.loader.applyEnvironmentToScene(b.texture)), y.name = E || r, this.loader.releaseTargetModel(), this.loader._sourceKey = g, g && !_ && this._storeGraph(g, v, r), this.loader.targetModel = y, this.loader.animations = x ?? [], this.loader.sceneMetadata = { environment: b?.texture ? {
4035
+ rotation: 0,
4036
+ intensity: 1
4037
+ } : { enabled: !1 } }, t({
4038
+ isLoading: !0,
4039
+ status: "Processing PBRT geometry...",
4040
+ progress: 10
4041
+ }), await this.loader.onModelLoad(this.loader.targetModel), this.loader.dispatchEvent({
4042
+ type: "load",
4043
+ model: y,
4044
+ filename: `${E} (from ZIP)`
4045
+ }), y;
4046
+ }
4047
+ _exrLoader() {
4048
+ return this.loader.loaderCache.exr ??= new ve().setDataType(T);
4049
+ }
4050
+ async _pbrtTextureFromBytes(e, t) {
4051
+ let n = t.split(".").pop().toLowerCase(), r;
4052
+ if (n === "exr" || n === "hdr") {
4053
+ let t = n === "hdr" ? this.loader.loaderCache.hdr || (this.loader.loaderCache.hdr = new he().setDataType(T)) : this._exrLoader();
4054
+ r = await this._loadViaObjectURL(t, e);
4055
+ } else if (n === "pfm") r = ei(e);
4056
+ else if (n === "tga") {
4057
+ if (!this.loader.loaderCache.tga) {
4058
+ let { TGALoader: e } = await import("three/examples/jsm/loaders/TGALoader.js");
4059
+ this.loader.loaderCache.tga = new e();
4060
+ }
4061
+ r = await this._loadViaObjectURL(this.loader.loaderCache.tga, e), r.colorSpace = F;
4062
+ } else r = new pe(await createImageBitmap(new Blob([e]))), r.colorSpace = F;
4063
+ return r.wrapS = r.wrapT = ue, r.needsUpdate = !0, r;
4064
+ }
4065
+ async _loadViaObjectURL(e, t) {
4066
+ let n = URL.createObjectURL(new Blob([t]));
4067
+ try {
4068
+ return await e.loadAsync(n);
4069
+ } finally {
4070
+ URL.revokeObjectURL(n);
4071
+ }
4072
+ }
4073
+ async processObjMtlPairsInZip(e, t) {
4074
+ let n = [], r = [];
4075
+ for (let t in e) {
4076
+ let i = t.toLowerCase();
4077
+ i.endsWith(".obj") ? n.push({
4078
+ path: t,
4079
+ content: e[t]
4080
+ }) : i.endsWith(".mtl") && r.push({
4081
+ path: t,
4082
+ content: e[t]
4083
+ });
4084
+ }
4085
+ if (n.length > 0 && r.length > 0) {
4086
+ console.log(`Found ${n.length} OBJ files and ${r.length} MTL files in ZIP`);
4087
+ let i = this.findMatchingObjMtlPairs(n, r);
4088
+ if (i.length > 0) return console.log(`Found ${i.length} matching OBJ+MTL pairs`), await this.loadOBJMTLPairFromZip(i[0].obj, i[0].mtl, e, t);
4089
+ if (i.length === 0) return console.log("No matching pairs by name, using first OBJ and MTL files"), await this.loadOBJMTLPairFromZip(n[0], r[0], e, t);
4090
+ }
4091
+ return null;
4092
+ }
4093
+ findMatchingObjMtlPairs(e, t) {
4094
+ let n = [];
4095
+ for (let r of e) {
4096
+ let e = r.path.split("/").pop().replace(/\.obj$/i, "").toLowerCase();
4097
+ for (let i of t) {
4098
+ let t = i.path.split("/").pop().replace(/\.mtl$/i, "").toLowerCase();
4099
+ if (e === t || e.includes(t) || t.includes(e)) {
4100
+ n.push({
4101
+ obj: r,
4102
+ mtl: i
4103
+ });
4104
+ break;
4105
+ }
4106
+ }
4107
+ }
4108
+ return n;
4109
+ }
4110
+ async findAndLoadModelFromZip(e) {
4111
+ for (let t of [
4112
+ "scene.gltf",
4113
+ "scene.glb",
4114
+ "model.gltf",
4115
+ "model.glb",
4116
+ "main.gltf",
4117
+ "main.glb",
4118
+ "asset.gltf",
4119
+ "asset.glb"
4120
+ ]) if (e[t]) {
4121
+ console.log(`Found main model file: ${t}`);
4122
+ let n = t.split(".").pop().toLowerCase();
4123
+ return await this.loadModelFromZipEntry(e[t], t, n, e);
4124
+ }
4125
+ for (let t in e) {
4126
+ let n = t.split(".").pop().toLowerCase();
4127
+ if (n === "obj" || this.loader.getFileFormat(t)?.type === "model") return ni.test(t) ? await this.loadUSDHierarchyFromZip(e) : (console.log(`Loading model file from ZIP: ${t}`), await this.loadModelFromZipEntry(e[t], t, n, e));
4128
+ }
4129
+ throw Error("No supported model files found in the ZIP archive");
4130
+ }
4131
+ async loadUSDHierarchyFromZip(t) {
4132
+ let n = Object.keys(t).filter((e) => ni.test(e));
4133
+ if (n.length === 0) throw Error("No USD layers found in the ZIP archive");
4134
+ let r = e._pickUSDRootLayer(n);
4135
+ console.log(`Loading USD scene from ZIP: ${r} (${n.length} layers)`);
4136
+ let i = { [r]: [t[r], { level: 0 }] };
4137
+ for (let e of Object.keys(t)) e !== r && (ni.test(e) || ri.test(e)) && (i[e] = [t[e], { level: 0 }]);
4138
+ return await this.loader._loadModelFileByExtension(new File([_e(i)], r), r);
4139
+ }
4140
+ static _pickUSDRootLayer(e) {
4141
+ let t = (e) => e.split("/").length, n = Math.min(...e.map(t)), r = e.filter((e) => t(e) === n);
4142
+ return r.find((e) => {
4143
+ let t = e.split("/");
4144
+ return t.length > 1 && t[t.length - 1].replace(ni, "") === t[t.length - 2];
4145
+ }) || r[0];
4146
+ }
4147
+ async loadModelFromZipEntry(e, n, r, i) {
4148
+ try {
4149
+ t({
4150
+ isLoading: !0,
4151
+ status: `Processing ${r.toUpperCase()} from ZIP...`,
4152
+ progress: 5
4153
+ });
4154
+ let a = new Blob([e.buffer], { type: "application/octet-stream" }), o = URL.createObjectURL(a), s;
4155
+ switch (r) {
4156
+ case "glb":
4157
+ case "gltf":
4158
+ s = await this.handleGltfFromZip(r, e, n, i);
4159
+ break;
4160
+ case "obj":
4161
+ s = await this.handleObjFromZip(e, n, i);
4162
+ break;
4163
+ default: s = await this.loader._loadModelFileByExtension(new File([e], n), n);
4164
+ }
4165
+ return URL.revokeObjectURL(o), this.loader.dispatchEvent({
4166
+ type: "load",
4167
+ model: this.loader.targetModel,
4168
+ filename: `${n} (from ZIP)`
4169
+ }), s;
4170
+ } catch (e) {
4171
+ throw console.error(`Error loading ${r} from ZIP:`, e), this.loader.dispatchEvent({
4172
+ type: "error",
4173
+ message: e.message,
4174
+ filename: n
4175
+ }), e;
4176
+ }
4177
+ }
4178
+ async handleGltfFromZip(e, t, n, r) {
4179
+ if (e === "gltf") {
4180
+ let e = R(t), i = new j(), a = n.split("/").slice(0, -1).join("/");
4181
+ i.setURLModifier((e) => this.resolveZipResource(e, a, r));
4182
+ let o = await this.loader.createGLTFLoader();
4183
+ o.manager = i;
4184
+ try {
4185
+ return await new Promise((t, n) => {
4186
+ o.parse(e, "", (e) => {
4187
+ try {
4188
+ this.loader._throwDeferred();
4189
+ } catch (e) {
4190
+ n(e);
4191
+ return;
4192
+ }
4193
+ this.loader.releaseTargetModel(), this.loader.targetModel = e.scene, this.loader.sceneMetadata = d(e), this.loader.onModelLoad(this.loader.targetModel).then(() => t(e));
4194
+ }, (e) => n(e));
4195
+ });
4196
+ } finally {
4197
+ this.loader._disposeGLTFLoader(o);
4198
+ }
4199
+ } else return await this.loader.loadGLBFromArrayBuffer(t.buffer, n);
4200
+ }
4201
+ async handleObjFromZip(e, t, n) {
4202
+ let r = R(e), i = r.match(/mtllib\s+([^\s]+)/), a = null;
4203
+ i && i[1] && (a = await this.loadMtlFromZip(i[1], t, n));
4204
+ let { OBJLoader: o } = await import("three/examples/jsm/loaders/OBJLoader.js"), s = new o();
4205
+ a && s.setMaterials(a);
4206
+ let c = s.parse(r);
4207
+ return c.name = t, this.loader.releaseTargetModel(), this.loader.targetModel = c, await this.loader.onModelLoad(this.loader.targetModel), c;
4208
+ }
4209
+ async preloadMtlTextures(e) {
4210
+ let t = [], n = e.loadTexture.bind(e);
4211
+ if (e.loadTexture = (e, r, i, a, o) => {
4212
+ let s;
4213
+ return t.push(new Promise((e) => s = e)), n(e, r, (e) => {
4214
+ s(), i?.(e);
4215
+ }, a, (e) => {
4216
+ s(), o?.(e);
4217
+ });
4218
+ }, e.preload(), t.length === 0) return e;
4219
+ let r, i = await Promise.race([Promise.all(t).then(() => !0), new Promise((e) => r = setTimeout(() => e(!1), ii))]);
4220
+ return clearTimeout(r), i || this.loader._issues?.record(a.TEXTURE_BUILD_FAILED, `MTL textures did not decode within ${ii}ms; affected materials render untextured`, { pending: t.length }), e;
4221
+ }
4222
+ async loadMtlFromZip(e, t, n) {
4223
+ let r = t.split("/").slice(0, -1).join("/"), i = [
4224
+ e,
4225
+ `${r}/${e}`,
4226
+ e.split("/").pop()
4227
+ ];
4228
+ for (let e of i) if (n[e]) {
4229
+ let { MTLLoader: t } = await import("three/examples/jsm/loaders/MTLLoader.js"), i = R(n[e]), a = new j();
4230
+ a.setURLModifier((e) => this.resolveZipResource(e, r, n));
4231
+ let o = new t(a).parse(i, r);
4232
+ return await this.preloadMtlTextures(o), o;
4233
+ }
4234
+ return null;
4235
+ }
4236
+ resolveZipResource(e, t, n) {
4237
+ let r = e.replace(/^\.\/|^\//, ""), i = [
4238
+ r,
4239
+ `${t}/${r}`,
4240
+ r.split("/").pop()
4241
+ ];
4242
+ for (let e of i) if (n[e]) {
4243
+ let t = new Blob([n[e].buffer], { type: "application/octet-stream" });
4244
+ return URL.createObjectURL(t);
4245
+ }
4246
+ return console.warn(`Resource not found in ZIP: ${e}`), e;
4247
+ }
4248
+ async loadOBJMTLPairFromZip(e, t, n, r) {
4249
+ let { MTLLoader: i } = await import("three/examples/jsm/loaders/MTLLoader.js"), { OBJLoader: a } = await import("three/examples/jsm/loaders/OBJLoader.js"), o = [], s = new j(), c = e.path.split("/").slice(0, -1).join("/"), l = t.path.split("/").slice(0, -1).join("/");
4250
+ s.setURLModifier((e) => this.resolveTextureInZip(e, c, l, t, n, o));
4251
+ let u = this.prepareFixedMtlContent(t), d = new i(s).parse(u, l);
4252
+ await this.preloadMtlTextures(d);
4253
+ let f = new a(s);
4254
+ f.setMaterials(d);
4255
+ let p = R(e.content), m = f.parse(p);
4256
+ return this.loader.releaseTargetModel(), this.loader.targetModel = m, await this.loader.onModelLoad(this.loader.targetModel), o.forEach((e) => URL.revokeObjectURL(e)), this.loader.dispatchEvent({
4257
+ type: "load",
4258
+ model: m,
4259
+ filename: `${e.path} (from ${r})`
4260
+ }), m;
4261
+ }
4262
+ prepareFixedMtlContent(e) {
4263
+ return R(e.content).replace(RegExp(`${e.path.split("/").pop()}\\s+`, "g"), " ").replace(/([a-zA-Z_]+)([\\/])/g, "$1 $2");
4264
+ }
4265
+ resolveTextureInZip(e, t, n, r, i, a) {
4266
+ let o = e.split("?")[0].split("#")[0].replace(/^\.\/|^\//, ""), s = r.path.split("/").pop();
4267
+ o.startsWith(s) && (o = o.substring(s.length).replace(/^\.\/|^\/|^\./, ""));
4268
+ let c = [
4269
+ o,
4270
+ `${t}/${o}`,
4271
+ `${n}/${o}`,
4272
+ `textures/${o}`,
4273
+ `texture/${o}`,
4274
+ `materials/${o}`,
4275
+ o.split("/").pop()
4276
+ ];
4277
+ for (let e of c) if (i[e]) {
4278
+ let t = new Blob([i[e].buffer], { type: "application/octet-stream" }), n = URL.createObjectURL(t);
4279
+ return a.push(n), n;
4280
+ }
4281
+ return this.findTextureWithFuzzyMatch(o, i, a) || e;
4282
+ }
4283
+ findTextureWithFuzzyMatch(e, t, n) {
4284
+ let r = e.split("/").pop();
4285
+ for (let e in t) if (e.endsWith(r)) {
4286
+ let r = new Blob([t[e].buffer], { type: "application/octet-stream" }), i = URL.createObjectURL(r);
4287
+ return n.push(i), i;
4288
+ }
4289
+ if (r && r.length > 5) for (let e in t) {
4290
+ let i = e.split("/").pop();
4291
+ if (i.includes(r) || r.includes(i)) {
4292
+ let r = new Blob([t[e].buffer], { type: "application/octet-stream" }), i = URL.createObjectURL(r);
4293
+ return n.push(i), i;
4294
+ }
4295
+ }
4296
+ return null;
4297
+ }
4298
+ };
4299
+ //#endregion
4300
+ export { oi as n, ti as r, ai as t };