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