@needle-tools/gltf-progressive 3.0.0 → 3.1.0-next.f550970

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