@needle-tools/engine 4.7.1 → 4.7.2-next.099105f

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 (33) hide show
  1. package/dist/gltf-progressive-CNdBjvz6.js +1145 -0
  2. package/dist/gltf-progressive-C_oN6wCA.umd.cjs +8 -0
  3. package/dist/gltf-progressive-MgWOszRl.min.js +8 -0
  4. package/dist/{needle-engine.bundle-CN9UC6Ju.min.js → needle-engine.bundle-CL0uOHVo.min.js} +100 -99
  5. package/dist/{needle-engine.bundle-BpCihhaP.js → needle-engine.bundle-CMENyq1b.js} +1482 -1466
  6. package/dist/{needle-engine.bundle-B7M9iBIa.umd.cjs → needle-engine.bundle-CODH82Jn.umd.cjs} +93 -92
  7. package/dist/needle-engine.js +3 -3
  8. package/dist/needle-engine.min.js +1 -1
  9. package/dist/needle-engine.umd.cjs +1 -1
  10. package/lib/engine/engine_context.js +1 -2
  11. package/lib/engine/engine_context.js.map +1 -1
  12. package/lib/engine/engine_lods.d.ts +1 -1
  13. package/lib/engine/engine_lods.js +1 -1
  14. package/lib/engine/engine_lods.js.map +1 -1
  15. package/lib/engine/engine_tonemapping.js +1 -0
  16. package/lib/engine/engine_tonemapping.js.map +1 -1
  17. package/lib/engine/webcomponents/needle-engine.d.ts +0 -1
  18. package/lib/engine/webcomponents/needle-engine.js +27 -3
  19. package/lib/engine/webcomponents/needle-engine.js.map +1 -1
  20. package/lib/engine-components/RendererInstancing.js +1 -1
  21. package/lib/engine-components/RendererInstancing.js.map +1 -1
  22. package/lib/engine-components/postprocessing/Effects/Tonemapping.js +1 -1
  23. package/lib/engine-components/postprocessing/Effects/Tonemapping.js.map +1 -1
  24. package/package.json +3 -3
  25. package/src/engine/engine_context.ts +0 -2
  26. package/src/engine/engine_lods.ts +2 -3
  27. package/src/engine/engine_tonemapping.ts +1 -0
  28. package/src/engine/webcomponents/needle-engine.ts +34 -4
  29. package/src/engine-components/RendererInstancing.ts +1 -1
  30. package/src/engine-components/postprocessing/Effects/Tonemapping.ts +1 -1
  31. package/dist/gltf-progressive-60Qk5ebF.min.js +0 -8
  32. package/dist/gltf-progressive-DM5ZiecW.js +0 -1060
  33. package/dist/gltf-progressive-wxtaVmio.umd.cjs +0 -8
@@ -1,1060 +0,0 @@
1
- var qe = Object.defineProperty;
2
- var Ae = (i) => {
3
- throw TypeError(i);
4
- };
5
- var Ke = (i, e, t) => e in i ? qe(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
6
- var d = (i, e, t) => Ke(i, typeof e != "symbol" ? e + "" : e, t), Pe = (i, e, t) => e.has(i) || Ae("Cannot " + t);
7
- var m = (i, e, t) => (Pe(i, e, "read from private field"), t ? t.call(i) : e.get(i)), H = (i, e, t) => e.has(i) ? Ae("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(i) : e.set(i, t), F = (i, e, t, s) => (Pe(i, e, "write to private field"), s ? s.call(i, t) : e.set(i, t), t);
8
- import { BufferGeometry as he, Mesh as Z, Texture as se, TextureLoader as Ye, Matrix4 as Ee, Clock as He, MeshStandardMaterial as Je, Sphere as Qe, Box3 as Ce, Vector3 as W } from "./three-DrqIzZTH.js";
9
- import { DRACOLoader as Ze, KTX2Loader as je, MeshoptDecoder as et, GLTFLoader as Te } from "./three-examples-B50TT3Iu.js";
10
- const tt = "";
11
- globalThis.GLTF_PROGRESSIVE_VERSION = tt;
12
- console.debug("[gltf-progressive] version -");
13
- let N = "https://www.gstatic.com/draco/versioned/decoders/1.5.7/", j = "https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";
14
- const st = N, rt = j, Ge = new URL(N + "draco_decoder.js");
15
- Ge.searchParams.append("range", "true");
16
- fetch(Ge, {
17
- method: "GET",
18
- headers: {
19
- Range: "bytes=0-1"
20
- }
21
- }).catch((i) => {
22
- console.debug(`Failed to fetch remote Draco decoder from ${N} (offline: ${typeof navigator < "u" ? navigator.onLine : "unknown"})`), N === st && nt("./include/draco/"), j === rt && ot("./include/ktx2/");
23
- }).finally(() => {
24
- $e();
25
- });
26
- function nt(i) {
27
- N = i, C && C[Oe] != N ? (console.debug("Updating Draco decoder path to " + i), C[Oe] = N, C.setDecoderPath(N), C.preload()) : console.debug("Setting Draco decoder path to " + i);
28
- }
29
- function ot(i) {
30
- j = i, G && G.transcoderPath != j ? (console.debug("Updating KTX2 transcoder path to " + i), G.setTranscoderPath(j), G.init()) : console.debug("Setting KTX2 transcoder path to " + i);
31
- }
32
- const Oe = Symbol("dracoDecoderPath");
33
- let C, ge, G;
34
- function $e() {
35
- C || (C = new Ze(), C[Oe] = N, C.setDecoderPath(N), C.setDecoderConfig({ type: "js" }), C.preload()), G || (G = new je(), G.setTranscoderPath(j), G.init()), ge || (ge = et);
36
- }
37
- function Ue(i) {
38
- return $e(), i ? G.detectSupport(i) : i !== null && console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"), { dracoLoader: C, ktx2Loader: G, meshoptDecoder: ge };
39
- }
40
- function Fe(i) {
41
- i.dracoLoader || i.setDRACOLoader(C), i.ktx2Loader || i.setKTX2Loader(G), i.meshoptDecoder || i.setMeshoptDecoder(ge);
42
- }
43
- const _e = /* @__PURE__ */ new WeakMap();
44
- function Ne(i, e) {
45
- let t = _e.get(i);
46
- t ? t = Object.assign(t, e) : t = e, _e.set(i, t);
47
- }
48
- const De = Te.prototype.load;
49
- function it(...i) {
50
- const e = _e.get(this);
51
- let t = i[0];
52
- const s = new URL(t, window.location.href);
53
- if (s.hostname.endsWith("needle.tools")) {
54
- const r = (e == null ? void 0 : e.progressive) !== void 0 ? e.progressive : !0, n = e != null && e.usecase ? e.usecase : "default";
55
- r ? this.requestHeader.Accept = `*/*;progressive=allowed;usecase=${n}` : this.requestHeader.Accept = `*/*;usecase=${n}`, t = s.toString();
56
- }
57
- return i[0] = t, De == null ? void 0 : De.call(this, ...i);
58
- }
59
- Te.prototype.load = it;
60
- ae("debugprogressive");
61
- function ae(i) {
62
- if (typeof window > "u") return !1;
63
- const t = new URL(window.location.href).searchParams.get(i);
64
- return t == null || t === "0" || t === "false" ? !1 : t === "" ? !0 : t;
65
- }
66
- function at(i, e) {
67
- if (e === void 0 || e.startsWith("./") || e.startsWith("http") || i === void 0)
68
- return e;
69
- const t = i.lastIndexOf("/");
70
- if (t >= 0) {
71
- const s = i.substring(0, t + 1);
72
- for (; s.endsWith("/") && e.startsWith("/"); ) e = e.substring(1);
73
- return s + e;
74
- }
75
- return e;
76
- }
77
- let re;
78
- function lt() {
79
- return re !== void 0 || (re = /iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent), ae("debugprogressive") && console.log("[glTF Progressive]: isMobileDevice", re)), re;
80
- }
81
- const ct = typeof window > "u" && typeof document > "u", be = Symbol("needle:raycast-mesh");
82
- function le(i) {
83
- return (i == null ? void 0 : i[be]) instanceof he ? i[be] : null;
84
- }
85
- function ut(i, e) {
86
- if ((i.type === "Mesh" || i.type === "SkinnedMesh") && !le(i)) {
87
- const s = dt(e);
88
- s.userData = { isRaycastMesh: !0 }, i[be] = s;
89
- }
90
- }
91
- function ft(i = !0) {
92
- if (i) {
93
- if (ne) return;
94
- const e = ne = Z.prototype.raycast;
95
- Z.prototype.raycast = function(t, s) {
96
- const o = this, r = le(o);
97
- let n;
98
- r && o.isMesh && (n = o.geometry, o.geometry = r), e.call(this, t, s), n && (o.geometry = n);
99
- };
100
- } else {
101
- if (!ne) return;
102
- Z.prototype.raycast = ne, ne = null;
103
- }
104
- }
105
- let ne = null;
106
- function dt(i) {
107
- const e = new he();
108
- for (const t in i.attributes)
109
- e.setAttribute(t, i.getAttribute(t));
110
- return e.setIndex(i.getIndex()), e;
111
- }
112
- const Q = new Array(), X = "NEEDLE_progressive", L = ae("debugprogressive"), Me = Symbol("needle-progressive-texture"), ie = /* @__PURE__ */ new Map(), Se = /* @__PURE__ */ new Set();
113
- if (L) {
114
- let i = function() {
115
- e += 1, console.log("Toggle LOD level", e, ie), ie.forEach((o, r) => {
116
- for (const n of o.keys) {
117
- const a = r[n];
118
- if (a != null)
119
- if (a.isBufferGeometry === !0) {
120
- const l = z.getMeshLODInformation(a), u = l ? Math.min(e, l.lods.length) : 0;
121
- r["DEBUG:LOD"] = u, l && (t = Math.max(t, l.lods.length - 1));
122
- } else r.isMaterial === !0 && (r["DEBUG:LOD"] = e);
123
- }
124
- }), e >= t && (e = -1);
125
- }, e = -1, t = 2, s = !1;
126
- window.addEventListener("keyup", (o) => {
127
- o.key === "p" && i(), o.key === "w" && (s = !s, Se && Se.forEach((r) => {
128
- r.name != "BackgroundCubeMaterial" && r.glyphMap == null && "wireframe" in r && (r.wireframe = s);
129
- }));
130
- });
131
- }
132
- function Be(i, e, t) {
133
- var o;
134
- if (!L) return;
135
- ie.has(i) || ie.set(i, { keys: [], sourceId: t });
136
- const s = ie.get(i);
137
- ((o = s == null ? void 0 : s.keys) == null ? void 0 : o.includes(e)) == !1 && s.keys.push(e);
138
- }
139
- const x = class x {
140
- constructor(e, t) {
141
- d(this, "parser");
142
- d(this, "url");
143
- d(this, "_isLoadingMesh");
144
- d(this, "loadMesh", (e) => {
145
- var s, o;
146
- if (this._isLoadingMesh) return null;
147
- const t = (o = (s = this.parser.json.meshes[e]) == null ? void 0 : s.extensions) == null ? void 0 : o[X];
148
- return t ? (this._isLoadingMesh = !0, this.parser.getDependency("mesh", e).then((r) => {
149
- var n;
150
- return this._isLoadingMesh = !1, r && x.registerMesh(this.url, t.guid, r, (n = t.lods) == null ? void 0 : n.length, void 0, t), r;
151
- })) : null;
152
- });
153
- L && console.log("Progressive extension registered for", t), this.parser = e, this.url = t;
154
- }
155
- /** The name of the extension */
156
- get name() {
157
- return X;
158
- }
159
- static getMeshLODInformation(e) {
160
- const t = this.getAssignedLODInformation(e);
161
- return t != null && t.key ? this.lodInfos.get(t.key) : null;
162
- }
163
- static getMaterialMinMaxLODsCount(e, t) {
164
- const s = this, o = "LODS:minmax", r = e[o];
165
- if (r != null) return r;
166
- if (t || (t = {
167
- min_count: 1 / 0,
168
- max_count: 0,
169
- lods: []
170
- }), Array.isArray(e)) {
171
- for (const a of e)
172
- this.getMaterialMinMaxLODsCount(a, t);
173
- return e[o] = t, t;
174
- }
175
- if (L === "verbose" && console.log("getMaterialMinMaxLODsCount", e), e.type === "ShaderMaterial" || e.type === "RawShaderMaterial") {
176
- const a = e;
177
- for (const l of Object.keys(a.uniforms)) {
178
- const u = a.uniforms[l].value;
179
- (u == null ? void 0 : u.isTexture) === !0 && n(u, t);
180
- }
181
- } else if (e.isMaterial)
182
- for (const a of Object.keys(e)) {
183
- const l = e[a];
184
- (l == null ? void 0 : l.isTexture) === !0 && n(l, t);
185
- }
186
- return e[o] = t, t;
187
- function n(a, l) {
188
- const u = s.getAssignedLODInformation(a);
189
- if (u) {
190
- const c = s.lodInfos.get(u.key);
191
- if (c && c.lods) {
192
- l.min_count = Math.min(l.min_count, c.lods.length), l.max_count = Math.max(l.max_count, c.lods.length);
193
- for (let g = 0; g < c.lods.length; g++) {
194
- const p = c.lods[g];
195
- p.width && (l.lods[g] = l.lods[g] || { min_height: 1 / 0, max_height: 0 }, l.lods[g].min_height = Math.min(l.lods[g].min_height, p.height), l.lods[g].max_height = Math.max(l.lods[g].max_height, p.height));
196
- }
197
- }
198
- }
199
- }
200
- }
201
- /** Check if a LOD level is available for a mesh or a texture
202
- * @param obj the mesh or texture to check
203
- * @param level the level of detail to check for (0 is the highest resolution). If undefined, the function checks if any LOD level is available
204
- * @returns true if the LOD level is available (or if any LOD level is available if level is undefined)
205
- */
206
- static hasLODLevelAvailable(e, t) {
207
- var r;
208
- if (Array.isArray(e)) {
209
- for (const n of e)
210
- if (this.hasLODLevelAvailable(n, t)) return !0;
211
- return !1;
212
- }
213
- if (e.isMaterial === !0) {
214
- for (const n of Object.keys(e)) {
215
- const a = e[n];
216
- if (a && a.isTexture && this.hasLODLevelAvailable(a, t))
217
- return !0;
218
- }
219
- return !1;
220
- } else if (e.isGroup === !0) {
221
- for (const n of e.children)
222
- if (n.isMesh === !0 && this.hasLODLevelAvailable(n, t))
223
- return !0;
224
- }
225
- let s, o;
226
- if (e.isMesh ? s = e.geometry : (e.isBufferGeometry || e.isTexture) && (s = e), s && (r = s == null ? void 0 : s.userData) != null && r.LODS) {
227
- const n = s.userData.LODS;
228
- if (o = this.lodInfos.get(n.key), t === void 0) return o != null;
229
- if (o)
230
- return Array.isArray(o.lods) ? t < o.lods.length : t === 0;
231
- }
232
- return !1;
233
- }
234
- /** Load a different resolution of a mesh (if available)
235
- * @param context the context
236
- * @param source the sourceid of the file from which the mesh is loaded (this is usually the component's sourceId)
237
- * @param mesh the mesh to load the LOD for
238
- * @param level the level of detail to load (0 is the highest resolution)
239
- * @returns a promise that resolves to the mesh with the requested LOD level
240
- * @example
241
- * ```javascript
242
- * const mesh = this.gameObject as Mesh;
243
- * NEEDLE_progressive.assignMeshLOD(context, sourceId, mesh, 1).then(mesh => {
244
- * console.log("Mesh with LOD level 1 loaded", mesh);
245
- * });
246
- * ```
247
- */
248
- static assignMeshLOD(e, t) {
249
- var s;
250
- if (!e) return Promise.resolve(null);
251
- if (e instanceof Z || e.isMesh === !0) {
252
- const o = e.geometry, r = this.getAssignedLODInformation(o);
253
- if (!r)
254
- return Promise.resolve(null);
255
- for (const n of Q)
256
- (s = n.onBeforeGetLODMesh) == null || s.call(n, e, t);
257
- return e["LOD:requested level"] = t, x.getOrLoadLOD(o, t).then((n) => {
258
- if (Array.isArray(n)) {
259
- const a = r.index || 0;
260
- n = n[a];
261
- }
262
- return e["LOD:requested level"] === t && (delete e["LOD:requested level"], n && o != n && ((n == null ? void 0 : n.isBufferGeometry) ? (e.geometry = n, L && Be(e, "geometry", r.url)) : L && console.error("Invalid LOD geometry", n))), n;
263
- }).catch((n) => (console.error("Error loading mesh LOD", e, n), null));
264
- } else L && console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh", e);
265
- return Promise.resolve(null);
266
- }
267
- static assignTextureLOD(e, t = 0) {
268
- if (!e) return Promise.resolve(null);
269
- if (e.isMesh === !0) {
270
- const s = e;
271
- if (Array.isArray(s.material)) {
272
- const o = new Array();
273
- for (const r of s.material) {
274
- const n = this.assignTextureLOD(r, t);
275
- o.push(n);
276
- }
277
- return Promise.all(o).then((r) => {
278
- const n = new Array();
279
- for (const a of r)
280
- Array.isArray(a) && n.push(...a);
281
- return n;
282
- });
283
- } else
284
- return this.assignTextureLOD(s.material, t);
285
- }
286
- if (e.isMaterial === !0) {
287
- const s = e, o = [], r = new Array();
288
- if (L && Se.add(s), s.uniforms && (s.isRawShaderMaterial || s.isShaderMaterial === !0)) {
289
- const n = s;
290
- for (const a of Object.keys(n.uniforms)) {
291
- const l = n.uniforms[a].value;
292
- if ((l == null ? void 0 : l.isTexture) === !0) {
293
- const u = this.assignTextureLODForSlot(l, t, s, a).then((c) => (c && n.uniforms[a].value != c && (n.uniforms[a].value = c, n.uniformsNeedUpdate = !0), c));
294
- o.push(u), r.push(a);
295
- }
296
- }
297
- } else
298
- for (const n of Object.keys(s)) {
299
- const a = s[n];
300
- if ((a == null ? void 0 : a.isTexture) === !0) {
301
- const l = this.assignTextureLODForSlot(a, t, s, n);
302
- o.push(l), r.push(n);
303
- }
304
- }
305
- return Promise.all(o).then((n) => {
306
- const a = new Array();
307
- for (let l = 0; l < n.length; l++) {
308
- const u = n[l], c = r[l];
309
- u && u.isTexture === !0 ? a.push({ material: s, slot: c, texture: u, level: t }) : a.push({ material: s, slot: c, texture: null, level: t });
310
- }
311
- return a;
312
- });
313
- }
314
- if (e instanceof se || e.isTexture === !0) {
315
- const s = e;
316
- return this.assignTextureLODForSlot(s, t, null, null);
317
- }
318
- return Promise.resolve(null);
319
- }
320
- static assignTextureLODForSlot(e, t, s, o) {
321
- return (e == null ? void 0 : e.isTexture) !== !0 ? Promise.resolve(null) : o === "glyphMap" ? Promise.resolve(e) : x.getOrLoadLOD(e, t).then((r) => {
322
- if (Array.isArray(r)) return null;
323
- if ((r == null ? void 0 : r.isTexture) === !0) {
324
- if (r != e) {
325
- if (s && o) {
326
- const n = s[o];
327
- if (n && !L) {
328
- const a = this.getAssignedLODInformation(n);
329
- if (a && (a == null ? void 0 : a.level) < t)
330
- return L === "verbose" && console.warn("Assigned texture level is already higher: ", a.level, t, s, n, r), null;
331
- }
332
- s[o] = r;
333
- }
334
- if (L && o && s) {
335
- const n = this.getAssignedLODInformation(e);
336
- n ? Be(s, o, n.url) : console.warn("No LOD info for texture", e);
337
- }
338
- }
339
- return r;
340
- } else L == "verbose" && console.warn("No LOD found for", e, t);
341
- return null;
342
- }).catch((r) => (console.error("Error loading LOD", e, r), null));
343
- }
344
- afterRoot(e) {
345
- var t, s;
346
- return L && console.log("AFTER", this.url, e), (t = this.parser.json.textures) == null || t.forEach((o, r) => {
347
- var n;
348
- if (o != null && o.extensions) {
349
- const a = o == null ? void 0 : o.extensions[X];
350
- if (a) {
351
- if (!a.lods) {
352
- L && console.warn("Texture has no LODs", a);
353
- return;
354
- }
355
- let l = !1;
356
- for (const u of this.parser.associations.keys())
357
- if (u.isTexture === !0) {
358
- const c = this.parser.associations.get(u);
359
- (c == null ? void 0 : c.textures) === r && (l = !0, x.registerTexture(this.url, u, (n = a.lods) == null ? void 0 : n.length, r, a));
360
- }
361
- l || this.parser.getDependency("texture", r).then((u) => {
362
- var c;
363
- u && x.registerTexture(this.url, u, (c = a.lods) == null ? void 0 : c.length, r, a);
364
- });
365
- }
366
- }
367
- }), (s = this.parser.json.meshes) == null || s.forEach((o, r) => {
368
- if (o != null && o.extensions) {
369
- const n = o == null ? void 0 : o.extensions[X];
370
- if (n && n.lods) {
371
- for (const a of this.parser.associations.keys())
372
- if (a.isMesh) {
373
- const l = this.parser.associations.get(a);
374
- (l == null ? void 0 : l.meshes) === r && x.registerMesh(this.url, n.guid, a, n.lods.length, l.primitives, n);
375
- }
376
- }
377
- }
378
- }), null;
379
- }
380
- static async getOrLoadLOD(e, t) {
381
- var a, l, u, c;
382
- const s = L == "verbose", o = e.userData.LODS;
383
- if (!o)
384
- return null;
385
- const r = o == null ? void 0 : o.key;
386
- let n;
387
- if (e.isTexture === !0) {
388
- const g = e;
389
- g.source && g.source[Me] && (n = g.source[Me]);
390
- }
391
- if (n || (n = x.lodInfos.get(r)), n) {
392
- if (t > 0) {
393
- let v = !1;
394
- const O = Array.isArray(n.lods);
395
- if (O && t >= n.lods.length ? v = !0 : O || (v = !0), v)
396
- return this.lowresCache.get(r);
397
- }
398
- const g = Array.isArray(n.lods) ? (a = n.lods[t]) == null ? void 0 : a.path : n.lods;
399
- if (!g)
400
- return L && !n["missing:uri"] && (n["missing:uri"] = !0, console.warn("Missing uri for progressive asset for LOD " + t, n)), null;
401
- const p = at(o.url, g);
402
- if (p.endsWith(".glb") || p.endsWith(".gltf")) {
403
- if (!n.guid)
404
- return console.warn("missing pointer for glb/gltf texture", n), null;
405
- const v = p + "_" + n.guid, O = this.previouslyLoaded.get(v);
406
- if (O !== void 0) {
407
- s && console.log(`LOD ${t} was already loading/loaded: ${v}`);
408
- let h = await O.catch((U) => (console.error(`Error loading LOD ${t} from ${p}
409
- `, U), null)), B = !1;
410
- if (h == null || (h instanceof se && e instanceof se ? (l = h.image) != null && l.data || (u = h.source) != null && u.data ? h = this.copySettings(e, h) : (B = !0, this.previouslyLoaded.delete(v)) : h instanceof he && e instanceof he && ((c = h.attributes.position) != null && c.array || (B = !0, this.previouslyLoaded.delete(v)))), !B)
411
- return h;
412
- }
413
- const D = n, $ = new Promise(async (h, B) => {
414
- const U = new Te();
415
- Fe(U), L && (await new Promise((S) => setTimeout(S, 1e3)), s && console.warn("Start loading (delayed) " + p, D.guid));
416
- let k = p;
417
- if (D && Array.isArray(D.lods)) {
418
- const S = D.lods[t];
419
- S.hash && (k += "?v=" + S.hash);
420
- }
421
- const A = await U.loadAsync(k).catch((S) => (console.error(`Error loading LOD ${t} from ${p}
422
- `, S), null));
423
- if (!A) return null;
424
- const V = A.parser;
425
- s && console.log("Loading finished " + p, D.guid);
426
- let b = 0;
427
- if (A.parser.json.textures) {
428
- let S = !1;
429
- for (const f of A.parser.json.textures) {
430
- if (f != null && f.extensions) {
431
- const y = f == null ? void 0 : f.extensions[X];
432
- if (y != null && y.guid && y.guid === D.guid) {
433
- S = !0;
434
- break;
435
- }
436
- }
437
- b++;
438
- }
439
- if (S) {
440
- let f = await V.getDependency("texture", b);
441
- return f && x.assignLODInformation(o.url, f, r, t, void 0, void 0), s && console.log('change "' + e.name + '" → "' + f.name + '"', p, b, f, v), e instanceof se && (f = this.copySettings(e, f)), f && (f.guid = D.guid), h(f);
442
- } else L && console.warn("Could not find texture with guid", D.guid, A.parser.json);
443
- }
444
- if (b = 0, A.parser.json.meshes) {
445
- let S = !1;
446
- for (const f of A.parser.json.meshes) {
447
- if (f != null && f.extensions) {
448
- const y = f == null ? void 0 : f.extensions[X];
449
- if (y != null && y.guid && y.guid === D.guid) {
450
- S = !0;
451
- break;
452
- }
453
- }
454
- b++;
455
- }
456
- if (S) {
457
- const f = await V.getDependency("mesh", b), y = D;
458
- if (s && console.log(`Loaded Mesh "${f.name}"`, p, b, f, v), f.isMesh === !0) {
459
- const _ = f.geometry;
460
- return x.assignLODInformation(o.url, _, r, t, void 0, y.density), h(_);
461
- } else {
462
- const _ = new Array();
463
- for (let T = 0; T < f.children.length; T++) {
464
- const P = f.children[T];
465
- if (P.isMesh === !0) {
466
- const Y = P.geometry;
467
- x.assignLODInformation(o.url, Y, r, t, T, y.density), _.push(Y);
468
- }
469
- }
470
- return h(_);
471
- }
472
- } else L && console.warn("Could not find mesh with guid", D.guid, A.parser.json);
473
- }
474
- return h(null);
475
- });
476
- return this.previouslyLoaded.set(v, $), await $;
477
- } else if (e instanceof se) {
478
- s && console.log("Load texture from uri: " + p);
479
- const O = await new Ye().loadAsync(p);
480
- return O ? (O.guid = n.guid, O.flipY = !1, O.needsUpdate = !0, O.colorSpace = e.colorSpace, s && console.log(n, O)) : L && console.warn("failed loading", p), O;
481
- }
482
- } else
483
- L && console.warn(`Can not load LOD ${t}: no LOD info found for "${r}" ${e.name}`, e.type);
484
- return null;
485
- }
486
- static assignLODInformation(e, t, s, o, r, n) {
487
- if (!t) return;
488
- t.userData || (t.userData = {});
489
- const a = new ht(e, s, o, r, n);
490
- t.userData.LODS = a;
491
- }
492
- static getAssignedLODInformation(e) {
493
- var t;
494
- return ((t = e == null ? void 0 : e.userData) == null ? void 0 : t.LODS) || null;
495
- }
496
- // private static readonly _copiedTextures: WeakMap<Texture, Texture> = new Map();
497
- static copySettings(e, t) {
498
- return t ? (L && console.warn(`Copy texture settings
499
- `, e.uuid, `
500
- `, t.uuid), t = t.clone(), t.offset = e.offset, t.repeat = e.repeat, t.colorSpace = e.colorSpace, t.magFilter = e.magFilter, t.minFilter = e.minFilter, t.wrapS = e.wrapS, t.wrapT = e.wrapT, t.flipY = e.flipY, t.anisotropy = e.anisotropy, t.mipmaps || (t.generateMipmaps = e.generateMipmaps), t) : e;
501
- }
502
- };
503
- /**
504
- * Register a texture with LOD information
505
- */
506
- d(x, "registerTexture", (e, t, s, o, r) => {
507
- if (L && console.log("> Progressive: register texture", o, t.name, t.uuid, t, r), !t) {
508
- L && console.error("gltf-progressive: Register texture without texture");
509
- return;
510
- }
511
- t.source && (t.source[Me] = r);
512
- const n = r.guid;
513
- x.assignLODInformation(e, t, n, s, o, void 0), x.lodInfos.set(n, r), x.lowresCache.set(n, t);
514
- }), /**
515
- * Register a mesh with LOD information
516
- */
517
- d(x, "registerMesh", (e, t, s, o, r, n) => {
518
- var u;
519
- L && console.log("> Progressive: register mesh", r, s.name, n, s.uuid, s);
520
- const a = s.geometry;
521
- if (!a) {
522
- L && console.warn("gltf-progressive: Register mesh without geometry");
523
- return;
524
- }
525
- a.userData || (a.userData = {}), x.assignLODInformation(e, a, t, o, r, n.density), x.lodInfos.set(t, n);
526
- let l = x.lowresCache.get(t);
527
- l ? l.push(s.geometry) : l = [s.geometry], x.lowresCache.set(t, l), o > 0 && !le(s) && ut(s, a);
528
- for (const c of Q)
529
- (u = c.onRegisteredNewMesh) == null || u.call(c, s, n);
530
- }), /** A map of key = asset uuid and value = LOD information */
531
- d(x, "lodInfos", /* @__PURE__ */ new Map()), /** cache of already loaded mesh lods */
532
- d(x, "previouslyLoaded", /* @__PURE__ */ new Map()), /** this contains the geometry/textures that were originally loaded */
533
- d(x, "lowresCache", /* @__PURE__ */ new Map());
534
- let z = x;
535
- class ht {
536
- constructor(e, t, s, o, r) {
537
- d(this, "url");
538
- /** the key to lookup the LOD information */
539
- d(this, "key");
540
- d(this, "level");
541
- /** For multi objects (e.g. a group of meshes) this is the index of the object */
542
- d(this, "index");
543
- /** the mesh density */
544
- d(this, "density");
545
- this.url = e, this.key = t, this.level = s, o != null && (this.index = o), r != null && (this.density = r);
546
- }
547
- }
548
- const I = ae("debugprogressive"), gt = ae("noprogressive"), we = Symbol("Needle:LODSManager"), ve = Symbol("Needle:LODState"), J = Symbol("Needle:CurrentLOD"), R = { mesh_lod: -1, texture_lod: -1 };
549
- var w, E, q, ye, ee, te, me, K;
550
- let pe = (w = class {
551
- // readonly plugins: NEEDLE_progressive_plugin[] = [];
552
- constructor(e, t) {
553
- d(this, "context");
554
- d(this, "renderer");
555
- d(this, "projectionScreenMatrix", new Ee());
556
- /**
557
- * The target triangle density is the desired max amount of triangles on screen when the mesh is filling the screen.
558
- * @default 200_000
559
- */
560
- d(this, "targetTriangleDensity", 2e5);
561
- /**
562
- * The interval in frames to automatically update the bounds of skinned meshes.
563
- * Set to 0 or a negative value to disable automatic bounds updates.
564
- * @default 30
565
- */
566
- d(this, "skinnedMeshAutoUpdateBoundsInterval", 30);
567
- /**
568
- * The update interval in frames. If set to 0, the LODs will be updated every frame. If set to 2, the LODs will be updated every second frame, etc.
569
- * @default "auto"
570
- */
571
- d(this, "updateInterval", "auto");
572
- H(this, E, 1);
573
- /**
574
- * If set to true, the LODsManager will not update the LODs.
575
- * @default false
576
- */
577
- d(this, "pause", !1);
578
- /**
579
- * When set to true the LODsManager will not update the LODs. This can be used to manually update the LODs using the `update` method.
580
- * Otherwise the LODs will be updated automatically when the renderer renders the scene.
581
- * @default false
582
- */
583
- d(this, "manual", !1);
584
- d(this, "_lodchangedlisteners", []);
585
- H(this, q);
586
- H(this, ye, new He());
587
- H(this, ee, 0);
588
- H(this, te, 0);
589
- H(this, me, 0);
590
- H(this, K, 0);
591
- d(this, "_fpsBuffer", [60, 60, 60, 60, 60]);
592
- // private testIfLODLevelsAreAvailable() {
593
- d(this, "_sphere", new Qe());
594
- d(this, "_tempBox", new Ce());
595
- d(this, "_tempBox2", new Ce());
596
- d(this, "tempMatrix", new Ee());
597
- d(this, "_tempWorldPosition", new W());
598
- d(this, "_tempBoxSize", new W());
599
- d(this, "_tempBox2Size", new W());
600
- this.renderer = e, this.context = { ...t };
601
- }
602
- /** @internal */
603
- static getObjectLODState(e) {
604
- return e[ve];
605
- }
606
- static addPlugin(e) {
607
- Q.push(e);
608
- }
609
- static removePlugin(e) {
610
- const t = Q.indexOf(e);
611
- t >= 0 && Q.splice(t, 1);
612
- }
613
- /**
614
- * Gets the LODsManager for the given renderer. If the LODsManager does not exist yet, it will be created.
615
- * @param renderer The renderer to get the LODsManager for.
616
- * @returns The LODsManager instance.
617
- */
618
- static get(e, t) {
619
- if (e[we])
620
- return console.debug("[gltf-progressive] LODsManager already exists for this renderer"), e[we];
621
- const s = new w(e, {
622
- engine: "unknown",
623
- ...t
624
- });
625
- return e[we] = s, s;
626
- }
627
- /** @deprecated use static `LODsManager.addPlugin()` method. This getter will be removed in later versions */
628
- get plugins() {
629
- return Q;
630
- }
631
- addEventListener(e, t) {
632
- e === "changed" && this._lodchangedlisteners.push(t);
633
- }
634
- removeEventListener(e, t) {
635
- if (e === "changed") {
636
- const s = this._lodchangedlisteners.indexOf(t);
637
- s >= 0 && this._lodchangedlisteners.splice(s, 1);
638
- }
639
- }
640
- /**
641
- * Enable the LODsManager. This will replace the render method of the renderer with a method that updates the LODs.
642
- */
643
- enable() {
644
- if (m(this, q)) return;
645
- console.debug("[gltf-progressive] Enabling LODsManager for renderer");
646
- let e = 0;
647
- F(this, q, this.renderer.render);
648
- const t = this;
649
- Ue(this.renderer), this.renderer.render = function(s, o) {
650
- const r = t.renderer.getRenderTarget();
651
- (r == null || "isXRRenderTarget" in r && r.isXRRenderTarget) && (e = 0, F(t, ee, m(t, ee) + 1), F(t, te, m(t, ye).getDelta()), F(t, me, m(t, me) + m(t, te)), t._fpsBuffer.shift(), t._fpsBuffer.push(1 / m(t, te)), F(t, K, t._fpsBuffer.reduce((a, l) => a + l) / t._fpsBuffer.length), I && m(t, ee) % 200 === 0 && console.log("FPS", Math.round(m(t, K)), "Interval:", m(t, E)));
652
- const n = e++;
653
- m(t, q).call(this, s, o), t.onAfterRender(s, o, n);
654
- };
655
- }
656
- disable() {
657
- m(this, q) && (console.debug("[gltf-progressive] Disabling LODsManager for renderer"), this.renderer.render = m(this, q), F(this, q, void 0));
658
- }
659
- update(e, t) {
660
- this.internalUpdate(e, t);
661
- }
662
- onAfterRender(e, t, s) {
663
- if (this.pause) return;
664
- const r = this.renderer.renderLists.get(e, 0).opaque;
665
- let n = !0;
666
- if (r.length === 1) {
667
- const a = r[0].material;
668
- (a.name === "EffectMaterial" || a.name === "CopyShader") && (n = !1);
669
- }
670
- if ((t.parent && t.parent.type === "CubeCamera" || s >= 1 && t.type === "OrthographicCamera") && (n = !1), n) {
671
- if (gt || (this.updateInterval === "auto" ? m(this, K) < 40 && m(this, E) < 10 ? (F(this, E, m(this, E) + 1), I && console.warn("↓ Reducing LOD updates", m(this, E), m(this, K).toFixed(0))) : m(this, K) >= 60 && m(this, E) > 1 && (F(this, E, m(this, E) - 1), I && console.warn("↑ Increasing LOD updates", m(this, E), m(this, K).toFixed(0))) : F(this, E, this.updateInterval), m(this, E) > 0 && m(this, ee) % m(this, E) != 0))
672
- return;
673
- this.internalUpdate(e, t);
674
- }
675
- }
676
- /**
677
- * Update LODs in a scene
678
- */
679
- internalUpdate(e, t) {
680
- var l, u;
681
- const s = this.renderer.renderLists.get(e, 0), o = s.opaque;
682
- this.projectionScreenMatrix.multiplyMatrices(t.projectionMatrix, t.matrixWorldInverse);
683
- const r = this.targetTriangleDensity;
684
- for (const c of o) {
685
- if (c.material && (((l = c.geometry) == null ? void 0 : l.type) === "BoxGeometry" || ((u = c.geometry) == null ? void 0 : u.type) === "BufferGeometry") && (c.material.name === "SphericalGaussianBlur" || c.material.name == "BackgroundCubeMaterial" || c.material.name === "CubemapFromEquirect" || c.material.name === "EquirectangularToCubeUV")) {
686
- I && (c.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"] || (c.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"] = !0, console.warn("Ignoring skybox or BLIT object", c, c.material.name, c.material.type)));
687
- continue;
688
- }
689
- switch (c.material.type) {
690
- case "LineBasicMaterial":
691
- case "LineDashedMaterial":
692
- case "PointsMaterial":
693
- case "ShadowMaterial":
694
- case "MeshDistanceMaterial":
695
- case "MeshDepthMaterial":
696
- continue;
697
- }
698
- if (I === "color" && c.material && !c.object.progressive_debug_color) {
699
- c.object.progressive_debug_color = !0;
700
- const p = Math.random() * 16777215, v = new Je({ color: p });
701
- c.object.material = v;
702
- }
703
- const g = c.object;
704
- (g instanceof Z || g.isMesh) && this.updateLODs(e, t, g, r);
705
- }
706
- const n = s.transparent;
707
- for (const c of n) {
708
- const g = c.object;
709
- (g instanceof Z || g.isMesh) && this.updateLODs(e, t, g, r);
710
- }
711
- const a = s.transmissive;
712
- for (const c of a) {
713
- const g = c.object;
714
- (g instanceof Z || g.isMesh) && this.updateLODs(e, t, g, r);
715
- }
716
- }
717
- /** Update the LOD levels for the renderer. */
718
- updateLODs(e, t, s, o) {
719
- var a, l;
720
- s.userData || (s.userData = {});
721
- let r = s[ve];
722
- if (r || (r = new pt(), s[ve] = r), r.frames++ < 2)
723
- return;
724
- for (const u of Q)
725
- (a = u.onBeforeUpdateLOD) == null || a.call(u, this.renderer, e, t, s);
726
- this.calculateLodLevel(t, s, r, o, R), R.mesh_lod = Math.round(R.mesh_lod), R.texture_lod = Math.round(R.texture_lod), R.mesh_lod >= 0 && this.loadProgressiveMeshes(s, R.mesh_lod);
727
- let n = R.texture_lod;
728
- s.material && n >= 0 && this.loadProgressiveTextures(s.material, n);
729
- for (const u of Q)
730
- (l = u.onAfterUpdatedLOD) == null || l.call(u, this.renderer, e, t, s, R);
731
- r.lastLodLevel_Mesh = R.mesh_lod, r.lastLodLevel_Texture = R.texture_lod;
732
- }
733
- /** Load progressive textures for the given material
734
- * @param material the material to load the textures for
735
- * @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
736
- * @returns Promise with true if the LOD was loaded, false if not
737
- */
738
- loadProgressiveTextures(e, t) {
739
- if (!e) return;
740
- if (Array.isArray(e)) {
741
- for (const r of e)
742
- this.loadProgressiveTextures(r, t);
743
- return;
744
- }
745
- let s = !1;
746
- (e[J] === void 0 || t < e[J]) && (s = !0);
747
- const o = e["DEBUG:LOD"];
748
- o != null && (s = e[J] != o, t = o), s && (e[J] = t, z.assignTextureLOD(e, t).then((r) => {
749
- this._lodchangedlisteners.forEach((n) => n({ type: "texture", level: t, object: e }));
750
- }));
751
- }
752
- /** Load progressive meshes for the given mesh
753
- * @param mesh the mesh to load the LOD for
754
- * @param index the index of the mesh if it's part of a group
755
- * @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
756
- * @returns Promise with true if the LOD was loaded, false if not
757
- */
758
- loadProgressiveMeshes(e, t) {
759
- if (!e) return Promise.resolve(null);
760
- let s = e[J] !== t;
761
- const o = e["DEBUG:LOD"];
762
- if (o != null && (s = e[J] != o, t = o), s) {
763
- e[J] = t;
764
- const r = e.geometry;
765
- return z.assignMeshLOD(e, t).then((n) => (n && e[J] == t && r != e.geometry && this._lodchangedlisteners.forEach((a) => a({ type: "mesh", level: t, object: e })), n));
766
- }
767
- return Promise.resolve(null);
768
- }
769
- static isInside(e, t) {
770
- const s = e.min, o = e.max, r = (s.x + o.x) * 0.5, n = (s.y + o.y) * 0.5;
771
- return this._tempPtInside.set(r, n, s.z).applyMatrix4(t).z < 0;
772
- }
773
- calculateLodLevel(e, t, s, o, r) {
774
- var $;
775
- if (!t) {
776
- r.mesh_lod = -1, r.texture_lod = -1;
777
- return;
778
- }
779
- if (!e) {
780
- r.mesh_lod = -1, r.texture_lod = -1;
781
- return;
782
- }
783
- let a = 10 + 1, l = !1;
784
- if (I && t["DEBUG:LOD"] != null)
785
- return t["DEBUG:LOD"];
786
- const u = z.getMeshLODInformation(t.geometry), c = u == null ? void 0 : u.lods, g = c && c.length > 0, p = z.getMaterialMinMaxLODsCount(t.material), v = (p == null ? void 0 : p.min_count) != 1 / 0 && p.min_count > 0 && p.max_count > 0;
787
- if (!g && !v) {
788
- r.mesh_lod = 0, r.texture_lod = 0;
789
- return;
790
- }
791
- g || (l = !0, a = 0);
792
- const O = this.renderer.domElement.clientHeight || this.renderer.domElement.height;
793
- let D = t.geometry.boundingBox;
794
- if (t.type === "SkinnedMesh") {
795
- const M = t;
796
- if (!M.boundingBox)
797
- M.computeBoundingBox();
798
- else if (this.skinnedMeshAutoUpdateBoundsInterval > 0 && s.frames % this.skinnedMeshAutoUpdateBoundsInterval === 0) {
799
- const h = le(M), B = M.geometry;
800
- h && (M.geometry = h), M.computeBoundingBox(), M.geometry = B;
801
- }
802
- D = M.boundingBox;
803
- }
804
- if (D) {
805
- const M = e;
806
- if (t.geometry.attributes.color && t.geometry.attributes.color.count < 100 && t.geometry.boundingSphere) {
807
- this._sphere.copy(t.geometry.boundingSphere), this._sphere.applyMatrix4(t.matrixWorld);
808
- const f = e.getWorldPosition(this._tempWorldPosition);
809
- if (this._sphere.containsPoint(f)) {
810
- r.mesh_lod = 0, r.texture_lod = 0;
811
- return;
812
- }
813
- }
814
- if (this._tempBox.copy(D), this._tempBox.applyMatrix4(t.matrixWorld), M.isPerspectiveCamera && w.isInside(this._tempBox, this.projectionScreenMatrix)) {
815
- r.mesh_lod = 0, r.texture_lod = 0;
816
- return;
817
- }
818
- if (this._tempBox.applyMatrix4(this.projectionScreenMatrix), this.renderer.xr.enabled && M.isPerspectiveCamera && M.fov > 70) {
819
- const f = this._tempBox.min, y = this._tempBox.max;
820
- let _ = f.x, T = f.y, P = y.x, Y = y.y;
821
- const ce = 2, Le = 1.5, ue = (f.x + y.x) * 0.5, fe = (f.y + y.y) * 0.5;
822
- _ = (_ - ue) * ce + ue, T = (T - fe) * ce + fe, P = (P - ue) * ce + ue, Y = (Y - fe) * ce + fe;
823
- const We = _ < 0 && P > 0 ? 0 : Math.min(Math.abs(f.x), Math.abs(y.x)), Xe = T < 0 && Y > 0 ? 0 : Math.min(Math.abs(f.y), Math.abs(y.y)), xe = Math.max(We, Xe);
824
- s.lastCentrality = (Le - xe) * (Le - xe) * (Le - xe);
825
- } else
826
- s.lastCentrality = 1;
827
- const h = this._tempBox.getSize(this._tempBoxSize);
828
- h.multiplyScalar(0.5), screen.availHeight > 0 && O > 0 && h.multiplyScalar(O / screen.availHeight), e.isPerspectiveCamera ? h.x *= e.aspect : e.isOrthographicCamera;
829
- const B = e.matrixWorldInverse, U = this._tempBox2;
830
- U.copy(D), U.applyMatrix4(t.matrixWorld), U.applyMatrix4(B);
831
- const k = U.getSize(this._tempBox2Size), A = Math.max(k.x, k.y);
832
- if (Math.max(h.x, h.y) != 0 && A != 0 && (h.z = k.z / Math.max(k.x, k.y) * Math.max(h.x, h.y)), s.lastScreenCoverage = Math.max(h.x, h.y, h.z), s.lastScreenspaceVolume.copy(h), s.lastScreenCoverage *= s.lastCentrality, I && w.debugDrawLine) {
833
- const f = this.tempMatrix.copy(this.projectionScreenMatrix);
834
- f.invert();
835
- const y = w.corner0, _ = w.corner1, T = w.corner2, P = w.corner3;
836
- y.copy(this._tempBox.min), _.copy(this._tempBox.max), _.x = y.x, T.copy(this._tempBox.max), T.y = y.y, P.copy(this._tempBox.max);
837
- const Y = (y.z + P.z) * 0.5;
838
- y.z = _.z = T.z = P.z = Y, y.applyMatrix4(f), _.applyMatrix4(f), T.applyMatrix4(f), P.applyMatrix4(f), w.debugDrawLine(y, _, 255), w.debugDrawLine(y, T, 255), w.debugDrawLine(_, P, 255), w.debugDrawLine(T, P, 255);
839
- }
840
- let b = 999;
841
- if (c && s.lastScreenCoverage > 0) {
842
- for (let f = 0; f < c.length; f++)
843
- if (c[f].density / s.lastScreenCoverage < o) {
844
- b = f;
845
- break;
846
- }
847
- }
848
- b < a && (a = b, l = !0);
849
- }
850
- if (l ? r.mesh_lod = a : r.mesh_lod = s.lastLodLevel_Mesh, I && r.mesh_lod != s.lastLodLevel_Mesh) {
851
- const h = c == null ? void 0 : c[r.mesh_lod];
852
- h && console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${r.mesh_lod} (${h.density.toFixed(0)}) - ${t.name}`);
853
- }
854
- if (v) {
855
- const M = "saveData" in globalThis.navigator && globalThis.navigator.saveData === !0;
856
- if (s.lastLodLevel_Texture < 0) {
857
- if (r.texture_lod = p.max_count - 1, I) {
858
- const h = p.lods[p.max_count - 1];
859
- I && console.log(`First Texture LOD ${r.texture_lod} (${h.max_height}px) - ${t.name}`);
860
- }
861
- } else {
862
- const h = s.lastScreenspaceVolume.x + s.lastScreenspaceVolume.y + s.lastScreenspaceVolume.z;
863
- let B = s.lastScreenCoverage * 4;
864
- (($ = this.context) == null ? void 0 : $.engine) === "model-viewer" && (B *= 1.5);
865
- const k = O / window.devicePixelRatio * B;
866
- let A = !1;
867
- for (let V = p.lods.length - 1; V >= 0; V--) {
868
- const b = p.lods[V];
869
- if (!(M && b.max_height >= 2048) && !(lt() && b.max_height > 4096) && (b.max_height > k || !A && V === 0)) {
870
- if (A = !0, r.texture_lod = V, r.texture_lod < s.lastLodLevel_Texture) {
871
- const S = b.max_height;
872
- I && console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${r.texture_lod} = ${S}px
873
- Screensize: ${k.toFixed(0)}px, Coverage: ${(100 * s.lastScreenCoverage).toFixed(2)}%, Volume ${h.toFixed(1)}
874
- ${t.name}`);
875
- }
876
- break;
877
- }
878
- }
879
- }
880
- } else
881
- r.texture_lod = 0;
882
- }
883
- }, E = new WeakMap(), q = new WeakMap(), ye = new WeakMap(), ee = new WeakMap(), te = new WeakMap(), me = new WeakMap(), K = new WeakMap(), /** Assign a function to draw debug lines for the LODs. This function will be called with the start and end position of the line and the color of the line when the `debugprogressive` query parameter is set.
884
- */
885
- d(w, "debugDrawLine"), d(w, "corner0", new W()), d(w, "corner1", new W()), d(w, "corner2", new W()), d(w, "corner3", new W()), d(w, "_tempPtInside", new W()), w);
886
- class pt {
887
- constructor() {
888
- d(this, "frames", 0);
889
- d(this, "lastLodLevel_Mesh", -1);
890
- d(this, "lastLodLevel_Texture", -1);
891
- d(this, "lastScreenCoverage", 0);
892
- d(this, "lastScreenspaceVolume", new W());
893
- d(this, "lastCentrality", 0);
894
- }
895
- }
896
- const ke = Symbol("NEEDLE_mesh_lod"), de = Symbol("NEEDLE_texture_lod");
897
- let oe = null;
898
- function ze() {
899
- const i = yt();
900
- i && (i.mapURLs(function(e) {
901
- return Ie(), e;
902
- }), Ie(), oe == null || oe.disconnect(), oe = new MutationObserver((e) => {
903
- e.forEach((t) => {
904
- t.addedNodes.forEach((s) => {
905
- s instanceof HTMLElement && s.tagName.toLowerCase() === "model-viewer" && Ve(s);
906
- });
907
- });
908
- }), oe.observe(document, { childList: !0, subtree: !0 }));
909
- }
910
- function yt() {
911
- if (typeof customElements > "u") return null;
912
- const i = customElements.get("model-viewer");
913
- return i || (customElements.whenDefined("model-viewer").then(() => {
914
- console.debug("[gltf-progressive] model-viewer defined"), ze();
915
- }), null);
916
- }
917
- function Ie() {
918
- if (typeof document > "u") return;
919
- document.querySelectorAll("model-viewer").forEach((e) => {
920
- Ve(e);
921
- });
922
- }
923
- const Re = /* @__PURE__ */ new WeakSet();
924
- let mt = 0;
925
- function Ve(i) {
926
- if (!i || Re.has(i))
927
- return null;
928
- Re.add(i), console.debug("[gltf-progressive] found new model-viewer..." + ++mt + `
929
- `, i.getAttribute("src"));
930
- let e = null, t = null, s = null;
931
- for (let o = i; o != null; o = Object.getPrototypeOf(o)) {
932
- const r = Object.getOwnPropertySymbols(o), n = r.find((u) => u.toString() == "Symbol(renderer)"), a = r.find((u) => u.toString() == "Symbol(scene)"), l = r.find((u) => u.toString() == "Symbol(needsRender)");
933
- !e && n != null && (e = i[n].threeRenderer), !t && a != null && (t = i[a]), !s && l != null && (s = i[l]);
934
- }
935
- if (e && t) {
936
- let o = function() {
937
- if (s) {
938
- let n = 0, a = setInterval(() => {
939
- if (n++ > 5) {
940
- clearInterval(a);
941
- return;
942
- }
943
- s == null || s.call(i);
944
- }, 300);
945
- }
946
- };
947
- console.debug("[gltf-progressive] setup model-viewer");
948
- const r = pe.get(e, { engine: "model-viewer" });
949
- return pe.addPlugin(new Lt()), r.enable(), r.addEventListener("changed", () => {
950
- s == null || s.call(i);
951
- }), i.addEventListener("model-visibility", (n) => {
952
- n.detail.visible && (s == null || s.call(i));
953
- }), i.addEventListener("load", () => {
954
- o();
955
- }), () => {
956
- r.disable();
957
- };
958
- }
959
- return null;
960
- }
961
- class Lt {
962
- constructor() {
963
- d(this, "_didWarnAboutMissingUrl", !1);
964
- }
965
- onBeforeUpdateLOD(e, t, s, o) {
966
- this.tryParseMeshLOD(t, o), this.tryParseTextureLOD(t, o);
967
- }
968
- getUrl(e) {
969
- if (!e)
970
- return null;
971
- let t = e.getAttribute("src");
972
- return t || (t = e.src), t || (this._didWarnAboutMissingUrl || console.warn("No url found in modelviewer", e), this._didWarnAboutMissingUrl = !0), t;
973
- }
974
- tryGetCurrentGLTF(e) {
975
- return e._currentGLTF;
976
- }
977
- tryGetCurrentModelViewer(e) {
978
- return e.element;
979
- }
980
- tryParseTextureLOD(e, t) {
981
- if (t[de] == !0) return;
982
- t[de] = !0;
983
- const s = this.tryGetCurrentGLTF(e), o = this.tryGetCurrentModelViewer(e), r = this.getUrl(o);
984
- if (r && s && t.material) {
985
- let n = function(l) {
986
- var c, g, p;
987
- if (l[de] == !0) return;
988
- l[de] = !0, l.userData && (l.userData.LOD = -1);
989
- const u = Object.keys(l);
990
- for (let v = 0; v < u.length; v++) {
991
- const O = u[v], D = l[O];
992
- if ((D == null ? void 0 : D.isTexture) === !0) {
993
- const $ = (g = (c = D.userData) == null ? void 0 : c.associations) == null ? void 0 : g.textures;
994
- if ($ == null) continue;
995
- const M = s.parser.json.textures[$];
996
- if (!M) {
997
- console.warn("Texture data not found for texture index " + $);
998
- continue;
999
- }
1000
- if ((p = M == null ? void 0 : M.extensions) != null && p[X]) {
1001
- const h = M.extensions[X];
1002
- h && r && z.registerTexture(r, D, h.lods.length, $, h);
1003
- }
1004
- }
1005
- }
1006
- };
1007
- const a = t.material;
1008
- if (Array.isArray(a)) for (const l of a) n(l);
1009
- else n(a);
1010
- }
1011
- }
1012
- tryParseMeshLOD(e, t) {
1013
- var n, a;
1014
- if (t[ke] == !0) return;
1015
- t[ke] = !0;
1016
- const s = this.tryGetCurrentModelViewer(e), o = this.getUrl(s);
1017
- if (!o)
1018
- return;
1019
- const r = (a = (n = t.userData) == null ? void 0 : n.gltfExtensions) == null ? void 0 : a[X];
1020
- if (r && o) {
1021
- const l = t.uuid;
1022
- z.registerMesh(o, l, t, 0, r.lods.length, r);
1023
- }
1024
- }
1025
- }
1026
- function xt(i, e, t, s) {
1027
- Ue(e), Fe(t), Ne(t, {
1028
- progressive: !0,
1029
- ...s == null ? void 0 : s.hints
1030
- }), t.register((r) => new z(r, i));
1031
- const o = pe.get(e);
1032
- return (s == null ? void 0 : s.enableLODsManager) !== !1 && o.enable(), o;
1033
- }
1034
- ze();
1035
- if (!ct) {
1036
- const i = {
1037
- gltfProgressive: {
1038
- useNeedleProgressive: xt,
1039
- LODsManager: pe,
1040
- configureLoader: Ne,
1041
- getRaycastMesh: le,
1042
- useRaycastMeshes: ft
1043
- }
1044
- };
1045
- if (!globalThis.Needle)
1046
- globalThis.Needle = i;
1047
- else
1048
- for (const e in i)
1049
- globalThis.Needle[e] = i[e];
1050
- }
1051
- export {
1052
- pe as LODsManager,
1053
- z as NEEDLE_progressive,
1054
- Fe as addDracoAndKTX2Loaders,
1055
- Ne as configureLoader,
1056
- Ue as createLoaders,
1057
- le as getRaycastMesh,
1058
- nt as setDracoDecoderLocation,
1059
- ot as setKTX2TranscoderLocation
1060
- };