@needle-tools/gltf-progressive 3.0.0-next.90239b7 → 3.0.0-next.d16d57f

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