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