@needle-tools/gltf-progressive 1.2.5 → 1.2.6

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