@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
1,053 lines (1,052 loc) • 44 kB
JavaScript
var Ye = Object.defineProperty;
var Ce = (i) => {
throw TypeError(i);
};
var He = (i, t, e) => t in i ? Ye(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
var d = (i, t, e) => He(i, typeof t != "symbol" ? t + "" : t, e), Ee = (i, t, e) => t.has(i) || Ce("Cannot " + e);
var y = (i, t, e) => (Ee(i, t, "read from private field"), e ? e.call(i) : t.get(i)), j = (i, t, e) => t.has(i) ? Ce("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(i) : t.set(i, e), N = (i, t, e, s) => (Ee(i, t, "write to private field"), s ? s.call(i, e) : t.set(i, e), e);
import { BufferGeometry as ye, Mesh as ee, Texture as oe, TextureLoader as je, Matrix4 as Be, Clock as Je, MeshStandardMaterial as Qe, Sphere as Ze, Box3 as Ie, Vector3 as q } from "./three-Bz6X1mrw.js";
import { DRACOLoader as et, KTX2Loader as tt, MeshoptDecoder as st, GLTFLoader as Pe } from "./three-examples-GggCDHv0.js";
const rt = "";
globalThis.GLTF_PROGRESSIVE_VERSION = rt;
console.debug("[gltf-progressive] version -");
let V = "https://www.gstatic.com/draco/versioned/decoders/1.5.7/", te = "https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";
const nt = V, ot = te, $e = new URL(V + "draco_decoder.js");
$e.searchParams.append("range", "true");
fetch($e, {
method: "GET",
headers: {
Range: "bytes=0-1"
}
}).catch((i) => {
console.debug(`Failed to fetch remote Draco decoder from ${V} (offline: ${typeof navigator < "u" ? navigator.onLine : "unknown"})`), V === nt && it("./include/draco/"), te === ot && at("./include/ktx2/");
}).finally(() => {
Ue();
});
function it(i) {
V = i, B && B[Se] != V ? (console.debug("Updating Draco decoder path to " + i), B[Se] = V, B.setDecoderPath(V), B.preload()) : console.debug("Setting Draco decoder path to " + i);
}
function at(i) {
te = i, $ && $.transcoderPath != te ? (console.debug("Updating KTX2 transcoder path to " + i), $.setTranscoderPath(te), $.init()) : console.debug("Setting KTX2 transcoder path to " + i);
}
const Se = Symbol("dracoDecoderPath");
let B, me, $;
function Ue() {
B || (B = new et(), B[Se] = V, B.setDecoderPath(V), B.setDecoderConfig({ type: "js" }), B.preload()), $ || ($ = new tt(), $.setTranscoderPath(te), $.init()), me || (me = st);
}
function Fe(i) {
return Ue(), i ? $.detectSupport(i) : i !== null && console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"), { dracoLoader: B, ktx2Loader: $, meshoptDecoder: me };
}
function Ne(i) {
i.dracoLoader || i.setDRACOLoader(B), i.ktx2Loader || i.setKTX2Loader($), i.meshoptDecoder || i.setMeshoptDecoder(me);
}
const Te = /* @__PURE__ */ new WeakMap();
function ze(i, t) {
let e = Te.get(i);
e ? e = Object.assign(e, t) : e = t, Te.set(i, e);
}
const we = Pe.prototype.load;
function lt(...i) {
const t = Te.get(this);
let e = i[0];
const s = new URL(e, window.location.href);
if (s.hostname.endsWith("needle.tools")) {
const n = (t == null ? void 0 : t.progressive) !== void 0 ? t.progressive : !0, r = t != null && t.usecase ? t.usecase : "default";
n ? this.requestHeader.Accept = `*/*;progressive=allowed;usecase=${r}` : this.requestHeader.Accept = `*/*;usecase=${r}`, e = s.toString();
}
return i[0] = e, we == null ? void 0 : we.call(this, ...i);
}
Pe.prototype.load = lt;
ce("debugprogressive");
function ce(i) {
if (typeof window > "u") return !1;
const e = new URL(window.location.href).searchParams.get(i);
return e == null || e === "0" || e === "false" ? !1 : e === "" ? !0 : e;
}
function ct(i, t) {
if (t === void 0 || t.startsWith("./") || t.startsWith("http") || i === void 0)
return t;
const e = i.lastIndexOf("/");
if (e >= 0) {
const s = i.substring(0, e + 1);
for (; s.endsWith("/") && t.startsWith("/"); ) t = t.substring(1);
return s + t;
}
return t;
}
let ie;
function ut() {
return ie !== void 0 || (ie = /iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent), ce("debugprogressive") && console.log("[glTF Progressive]: isMobileDevice", ie)), ie;
}
const dt = typeof window > "u" && typeof document > "u", Ae = Symbol("needle:raycast-mesh");
function ue(i) {
return (i == null ? void 0 : i[Ae]) instanceof ye ? i[Ae] : null;
}
function ft(i, t) {
if ((i.type === "Mesh" || i.type === "SkinnedMesh") && !ue(i)) {
const s = gt(t);
s.userData = { isRaycastMesh: !0 }, i[Ae] = s;
}
}
function ht(i = !0) {
if (i) {
if (ae) return;
const t = ae = ee.prototype.raycast;
ee.prototype.raycast = function(e, s) {
const o = this, n = ue(o);
let r;
n && o.isMesh && (r = o.geometry, o.geometry = n), t.call(this, e, s), r && (o.geometry = r);
};
} else {
if (!ae) return;
ee.prototype.raycast = ae, ae = null;
}
}
let ae = null;
function gt(i) {
const t = new ye();
for (const e in i.attributes)
t.setAttribute(e, i.getAttribute(e));
return t.setIndex(i.getIndex()), t;
}
const Q = new Array(), D = ce("debugprogressive");
let pe, Z = -1;
if (D) {
let i = function() {
Z += 1, Z >= t && (Z = -1), console.log(`Toggle LOD level [${Z}]`);
}, t = 6;
window.addEventListener("keyup", (e) => {
e.key === "p" && i(), e.key === "w" && (pe = !pe, console.log(`Toggle wireframe [${pe}]`));
const s = parseInt(e.key);
!isNaN(s) && s >= 0 && (Z = s, console.log(`Set LOD level to [${Z}]`));
});
}
function Ve(i) {
if (D)
if (Array.isArray(i))
for (const t of i)
Ve(t);
else i && "wireframe" in i && (i.wireframe = pe === !0);
}
const K = "NEEDLE_progressive", Oe = Symbol("needle-progressive-texture"), x = class x {
constructor(t, e) {
d(this, "parser");
d(this, "url");
d(this, "_isLoadingMesh");
d(this, "loadMesh", (t) => {
var s, o;
if (this._isLoadingMesh) return null;
const e = (o = (s = this.parser.json.meshes[t]) == null ? void 0 : s.extensions) == null ? void 0 : o[K];
return e ? (this._isLoadingMesh = !0, this.parser.getDependency("mesh", t).then((n) => {
var r;
return this._isLoadingMesh = !1, n && x.registerMesh(this.url, e.guid, n, (r = e.lods) == null ? void 0 : r.length, 0, e), n;
})) : null;
});
D && console.log("Progressive extension registered for", e), this.parser = t, this.url = e;
}
/** The name of the extension */
get name() {
return K;
}
static getMeshLODExtension(t) {
const e = this.getAssignedLODInformation(t);
return e != null && e.key ? this.lodInfos.get(e.key) : null;
}
static getPrimitiveIndex(t) {
var s;
const e = (s = this.getAssignedLODInformation(t)) == null ? void 0 : s.index;
return e ?? -1;
}
static getMaterialMinMaxLODsCount(t, e) {
const s = this, o = "LODS:minmax", n = t[o];
if (n != null) return n;
if (e || (e = {
min_count: 1 / 0,
max_count: 0,
lods: []
}), Array.isArray(t)) {
for (const a of t)
this.getMaterialMinMaxLODsCount(a, e);
return t[o] = e, e;
}
if (D === "verbose" && console.log("getMaterialMinMaxLODsCount", t), t.type === "ShaderMaterial" || t.type === "RawShaderMaterial") {
const a = t;
for (const l of Object.keys(a.uniforms)) {
const u = a.uniforms[l].value;
(u == null ? void 0 : u.isTexture) === !0 && r(u, e);
}
} else if (t.isMaterial)
for (const a of Object.keys(t)) {
const l = t[a];
(l == null ? void 0 : l.isTexture) === !0 && r(l, e);
}
return t[o] = e, e;
function r(a, l) {
const u = s.getAssignedLODInformation(a);
if (u) {
const c = s.lodInfos.get(u.key);
if (c && c.lods) {
l.min_count = Math.min(l.min_count, c.lods.length), l.max_count = Math.max(l.max_count, c.lods.length);
for (let h = 0; h < c.lods.length; h++) {
const g = c.lods[h];
g.width && (l.lods[h] = l.lods[h] || { min_height: 1 / 0, max_height: 0 }, l.lods[h].min_height = Math.min(l.lods[h].min_height, g.height), l.lods[h].max_height = Math.max(l.lods[h].max_height, g.height));
}
}
}
}
}
/** Check if a LOD level is available for a mesh or a texture
* @param obj the mesh or texture to check
* @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
* @returns true if the LOD level is available (or if any LOD level is available if level is undefined)
*/
static hasLODLevelAvailable(t, e) {
var n;
if (Array.isArray(t)) {
for (const r of t)
if (this.hasLODLevelAvailable(r, e)) return !0;
return !1;
}
if (t.isMaterial === !0) {
for (const r of Object.keys(t)) {
const a = t[r];
if (a && a.isTexture && this.hasLODLevelAvailable(a, e))
return !0;
}
return !1;
} else if (t.isGroup === !0) {
for (const r of t.children)
if (r.isMesh === !0 && this.hasLODLevelAvailable(r, e))
return !0;
}
let s, o;
if (t.isMesh ? s = t.geometry : (t.isBufferGeometry || t.isTexture) && (s = t), s && (n = s == null ? void 0 : s.userData) != null && n.LODS) {
const r = s.userData.LODS;
if (o = this.lodInfos.get(r.key), e === void 0) return o != null;
if (o)
return Array.isArray(o.lods) ? e < o.lods.length : e === 0;
}
return !1;
}
/** Load a different resolution of a mesh (if available)
* @param context the context
* @param source the sourceid of the file from which the mesh is loaded (this is usually the component's sourceId)
* @param mesh the mesh to load the LOD for
* @param level the level of detail to load (0 is the highest resolution)
* @returns a promise that resolves to the mesh with the requested LOD level
* @example
* ```javascript
* const mesh = this.gameObject as Mesh;
* NEEDLE_progressive.assignMeshLOD(context, sourceId, mesh, 1).then(mesh => {
* console.log("Mesh with LOD level 1 loaded", mesh);
* });
* ```
*/
static assignMeshLOD(t, e) {
var s;
if (!t) return Promise.resolve(null);
if (t instanceof ee || t.isMesh === !0) {
const o = t.geometry, n = this.getAssignedLODInformation(o);
if (!n)
return Promise.resolve(null);
for (const r of Q)
(s = r.onBeforeGetLODMesh) == null || s.call(r, t, e);
return t["LOD:requested level"] = e, x.getOrLoadLOD(o, e).then((r) => {
if (Array.isArray(r)) {
const a = n.index || 0;
r = r[a];
}
return t["LOD:requested level"] === e && (delete t["LOD:requested level"], r && o != r && ((r == null ? void 0 : r.isBufferGeometry) ? t.geometry = r : D && console.error("Invalid LOD geometry", r))), r;
}).catch((r) => (console.error("Error loading mesh LOD", t, r), null));
} else D && console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh", t);
return Promise.resolve(null);
}
static assignTextureLOD(t, e = 0) {
if (!t) return Promise.resolve(null);
if (t.isMesh === !0) {
const s = t;
if (Array.isArray(s.material)) {
const o = new Array();
for (const n of s.material) {
const r = this.assignTextureLOD(n, e);
o.push(r);
}
return Promise.all(o).then((n) => {
const r = new Array();
for (const a of n)
Array.isArray(a) && r.push(...a);
return r;
});
} else
return this.assignTextureLOD(s.material, e);
}
if (t.isMaterial === !0) {
const s = t, o = [], n = new Array();
if (s.uniforms && (s.isRawShaderMaterial || s.isShaderMaterial === !0)) {
const r = s;
for (const a of Object.keys(r.uniforms)) {
const l = r.uniforms[a].value;
if ((l == null ? void 0 : l.isTexture) === !0) {
const u = this.assignTextureLODForSlot(l, e, s, a).then((c) => (c && r.uniforms[a].value != c && (r.uniforms[a].value = c, r.uniformsNeedUpdate = !0), c));
o.push(u), n.push(a);
}
}
} else
for (const r of Object.keys(s)) {
const a = s[r];
if ((a == null ? void 0 : a.isTexture) === !0) {
const l = this.assignTextureLODForSlot(a, e, s, r);
o.push(l), n.push(r);
}
}
return Promise.all(o).then((r) => {
const a = new Array();
for (let l = 0; l < r.length; l++) {
const u = r[l], c = n[l];
u && u.isTexture === !0 ? a.push({ material: s, slot: c, texture: u, level: e }) : a.push({ material: s, slot: c, texture: null, level: e });
}
return a;
});
}
if (t instanceof oe || t.isTexture === !0) {
const s = t;
return this.assignTextureLODForSlot(s, e, null, null);
}
return Promise.resolve(null);
}
static assignTextureLODForSlot(t, e, s, o) {
return (t == null ? void 0 : t.isTexture) !== !0 ? Promise.resolve(null) : o === "glyphMap" ? Promise.resolve(t) : x.getOrLoadLOD(t, e).then((n) => {
if (Array.isArray(n)) return null;
if ((n == null ? void 0 : n.isTexture) === !0) {
if (n != t && s && o) {
const r = s[o];
if (r && !D) {
const a = this.getAssignedLODInformation(r);
if (a && (a == null ? void 0 : a.level) < e)
return D === "verbose" && console.warn("Assigned texture level is already higher: ", a.level, e, s, r, n), null;
}
s[o] = n;
}
return n;
} else D == "verbose" && console.warn("No LOD found for", t, e);
return null;
}).catch((n) => (console.error("Error loading LOD", t, n), null));
}
afterRoot(t) {
var e, s;
return D && console.log("AFTER", this.url, t), (e = this.parser.json.textures) == null || e.forEach((o, n) => {
var r;
if (o != null && o.extensions) {
const a = o == null ? void 0 : o.extensions[K];
if (a) {
if (!a.lods) {
D && console.warn("Texture has no LODs", a);
return;
}
let l = !1;
for (const u of this.parser.associations.keys())
if (u.isTexture === !0) {
const c = this.parser.associations.get(u);
(c == null ? void 0 : c.textures) === n && (l = !0, x.registerTexture(this.url, u, (r = a.lods) == null ? void 0 : r.length, n, a));
}
l || this.parser.getDependency("texture", n).then((u) => {
var c;
u && x.registerTexture(this.url, u, (c = a.lods) == null ? void 0 : c.length, n, a);
});
}
}
}), (s = this.parser.json.meshes) == null || s.forEach((o, n) => {
if (o != null && o.extensions) {
const r = o == null ? void 0 : o.extensions[K];
if (r && r.lods) {
for (const a of this.parser.associations.keys())
if (a.isMesh) {
const l = this.parser.associations.get(a);
(l == null ? void 0 : l.meshes) === n && x.registerMesh(this.url, r.guid, a, r.lods.length, l.primitives, r);
}
}
}
}), null;
}
static async getOrLoadLOD(t, e) {
var a, l, u, c;
const s = D == "verbose", o = t.userData.LODS;
if (!o)
return null;
const n = o == null ? void 0 : o.key;
let r;
if (t.isTexture === !0) {
const h = t;
h.source && h.source[Oe] && (r = h.source[Oe]);
}
if (r || (r = x.lodInfos.get(n)), r) {
if (e > 0) {
let v = !1;
const b = Array.isArray(r.lods);
if (b && e >= r.lods.length ? v = !0 : b || (v = !0), v)
return this.lowresCache.get(n);
}
const h = Array.isArray(r.lods) ? (a = r.lods[e]) == null ? void 0 : a.path : r.lods;
if (!h)
return D && !r["missing:uri"] && (r["missing:uri"] = !0, console.warn("Missing uri for progressive asset for LOD " + e, r)), null;
const g = ct(o.url, h);
if (g.endsWith(".glb") || g.endsWith(".gltf")) {
if (!r.guid)
return console.warn("missing pointer for glb/gltf texture", r), null;
const v = g + "_" + r.guid, b = this.previouslyLoaded.get(v);
if (b !== void 0) {
s && console.log(`LOD ${e} was already loading/loaded: ${v}`);
let w = await b.catch((p) => (console.error(`Error loading LOD ${e} from ${g}
`, p), null)), _ = !1;
if (w == null || (w instanceof oe && t instanceof oe ? (l = w.image) != null && l.data || (u = w.source) != null && u.data ? w = this.copySettings(t, w) : (_ = !0, this.previouslyLoaded.delete(v)) : w instanceof ye && t instanceof ye && ((c = w.attributes.position) != null && c.array || (_ = !0, this.previouslyLoaded.delete(v)))), !_)
return w;
}
const M = r, U = new Promise(async (w, _) => {
const p = new Pe();
Ne(p), D && (await new Promise((S) => setTimeout(S, 1e3)), s && console.warn("Start loading (delayed) " + g, M.guid));
let X = g;
if (M && Array.isArray(M.lods)) {
const S = M.lods[e];
S.hash && (X += "?v=" + S.hash);
}
const A = await p.loadAsync(X).catch((S) => (console.error(`Error loading LOD ${e} from ${g}
`, S), null));
if (!A) return null;
const R = A.parser;
s && console.log("Loading finished " + g, M.guid);
let I = 0;
if (A.parser.json.textures) {
let S = !1;
for (const f of A.parser.json.textures) {
if (f != null && f.extensions) {
const T = f == null ? void 0 : f.extensions[K];
if (T != null && T.guid && T.guid === M.guid) {
S = !0;
break;
}
}
I++;
}
if (S) {
let f = await R.getDependency("texture", I);
return f && x.assignLODInformation(o.url, f, n, e, void 0), s && console.log('change "' + t.name + '" → "' + f.name + '"', g, I, f, v), t instanceof oe && (f = this.copySettings(t, f)), f && (f.guid = M.guid), w(f);
} else D && console.warn("Could not find texture with guid", M.guid, A.parser.json);
}
if (I = 0, A.parser.json.meshes) {
let S = !1;
for (const f of A.parser.json.meshes) {
if (f != null && f.extensions) {
const T = f == null ? void 0 : f.extensions[K];
if (T != null && T.guid && T.guid === M.guid) {
S = !0;
break;
}
}
I++;
}
if (S) {
const f = await R.getDependency("mesh", I);
if (s && console.log(`Loaded Mesh "${f.name}"`, g, I, f, v), f.isMesh === !0) {
const T = f.geometry;
return x.assignLODInformation(o.url, T, n, e, 0), w(T);
} else {
const T = new Array();
for (let L = 0; L < f.children.length; L++) {
const O = f.children[L];
if (O.isMesh === !0) {
const P = O.geometry;
x.assignLODInformation(o.url, P, n, e, L), T.push(P);
}
}
return w(T);
}
} else D && console.warn("Could not find mesh with guid", M.guid, A.parser.json);
}
return w(null);
});
return this.previouslyLoaded.set(v, U), await U;
} else if (t instanceof oe) {
s && console.log("Load texture from uri: " + g);
const b = await new je().loadAsync(g);
return b ? (b.guid = r.guid, b.flipY = !1, b.needsUpdate = !0, b.colorSpace = t.colorSpace, s && console.log(r, b)) : D && console.warn("failed loading", g), b;
}
} else
D && console.warn(`Can not load LOD ${e}: no LOD info found for "${n}" ${t.name}`, t.type);
return null;
}
static assignLODInformation(t, e, s, o, n) {
if (!e) return;
e.userData || (e.userData = {});
const r = new pt(t, s, o, n);
e.userData.LODS = r;
}
static getAssignedLODInformation(t) {
var e;
return ((e = t == null ? void 0 : t.userData) == null ? void 0 : e.LODS) || null;
}
// private static readonly _copiedTextures: WeakMap<Texture, Texture> = new Map();
static copySettings(t, e) {
return e ? (D && console.warn(`Copy texture settings
`, t.uuid, `
`, 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;
}
};
/**
* Register a texture with LOD information
*/
d(x, "registerTexture", (t, e, s, o, n) => {
if (D && console.log("> Progressive: register texture", o, e.name, e.uuid, e, n), !e) {
D && console.error("gltf-progressive: Register texture without texture");
return;
}
e.source && (e.source[Oe] = n);
const r = n.guid;
x.assignLODInformation(t, e, r, s, o), x.lodInfos.set(r, n), x.lowresCache.set(r, e);
}), /**
* Register a mesh with LOD information
*/
d(x, "registerMesh", (t, e, s, o, n, r) => {
var u;
const a = s.geometry;
if (!a) {
D && console.warn("gltf-progressive: Register mesh without geometry");
return;
}
a.userData || (a.userData = {}), D && console.log("> Progressive: register mesh " + s.name, { index: n, uuid: s.uuid }, r, s), x.assignLODInformation(t, a, e, o, n), x.lodInfos.set(e, r);
let l = x.lowresCache.get(e);
l ? l.push(s.geometry) : l = [s.geometry], x.lowresCache.set(e, l), o > 0 && !ue(s) && ft(s, a);
for (const c of Q)
(u = c.onRegisteredNewMesh) == null || u.call(c, s, r);
}), /** A map of key = asset uuid and value = LOD information */
d(x, "lodInfos", /* @__PURE__ */ new Map()), /** cache of already loaded mesh lods */
d(x, "previouslyLoaded", /* @__PURE__ */ new Map()), /** this contains the geometry/textures that were originally loaded */
d(x, "lowresCache", /* @__PURE__ */ new Map());
let z = x;
class pt {
constructor(t, e, s, o) {
d(this, "url");
/** the key to lookup the LOD information */
d(this, "key");
d(this, "level");
/** For multi objects (e.g. a group of meshes) this is the index of the object */
d(this, "index");
this.url = t, this.key = e, this.level = s, o != null && (this.index = o);
}
}
const G = ce("debugprogressive"), yt = ce("noprogressive"), _e = Symbol("Needle:LODSManager"), be = Symbol("Needle:LODState"), J = Symbol("Needle:CurrentLOD"), C = { mesh_lod: -1, texture_lod: -1 };
var m, E, Y, xe, se, re, De, H;
let Le = (m = class {
// readonly plugins: NEEDLE_progressive_plugin[] = [];
constructor(t, e) {
d(this, "context");
d(this, "renderer");
d(this, "projectionScreenMatrix", new Be());
/**
* The target triangle density is the desired max amount of triangles on screen when the mesh is filling the screen.
* @default 200_000
*/
d(this, "targetTriangleDensity", 2e5);
/**
* The interval in frames to automatically update the bounds of skinned meshes.
* Set to 0 or a negative value to disable automatic bounds updates.
* @default 30
*/
d(this, "skinnedMeshAutoUpdateBoundsInterval", 30);
/**
* 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.
* @default "auto"
*/
d(this, "updateInterval", "auto");
j(this, E, 1);
/**
* If set to true, the LODsManager will not update the LODs.
* @default false
*/
d(this, "pause", !1);
/**
* When set to true the LODsManager will not update the LODs. This can be used to manually update the LODs using the `update` method.
* Otherwise the LODs will be updated automatically when the renderer renders the scene.
* @default false
*/
d(this, "manual", !1);
d(this, "_lodchangedlisteners", []);
j(this, Y);
j(this, xe, new Je());
j(this, se, 0);
j(this, re, 0);
j(this, De, 0);
j(this, H, 0);
d(this, "_fpsBuffer", [60, 60, 60, 60, 60]);
// private testIfLODLevelsAreAvailable() {
d(this, "_sphere", new Ze());
d(this, "_tempBox", new Ie());
d(this, "_tempBox2", new Ie());
d(this, "tempMatrix", new Be());
d(this, "_tempWorldPosition", new q());
d(this, "_tempBoxSize", new q());
d(this, "_tempBox2Size", new q());
this.renderer = t, this.context = { ...e };
}
/** @internal */
static getObjectLODState(t) {
return t[be];
}
static addPlugin(t) {
Q.push(t);
}
static removePlugin(t) {
const e = Q.indexOf(t);
e >= 0 && Q.splice(e, 1);
}
/**
* Gets the LODsManager for the given renderer. If the LODsManager does not exist yet, it will be created.
* @param renderer The renderer to get the LODsManager for.
* @returns The LODsManager instance.
*/
static get(t, e) {
if (t[_e])
return console.debug("[gltf-progressive] LODsManager already exists for this renderer"), t[_e];
const s = new m(t, {
engine: "unknown",
...e
});
return t[_e] = s, s;
}
/** @deprecated use static `LODsManager.addPlugin()` method. This getter will be removed in later versions */
get plugins() {
return Q;
}
addEventListener(t, e) {
t === "changed" && this._lodchangedlisteners.push(e);
}
removeEventListener(t, e) {
if (t === "changed") {
const s = this._lodchangedlisteners.indexOf(e);
s >= 0 && this._lodchangedlisteners.splice(s, 1);
}
}
/**
* Enable the LODsManager. This will replace the render method of the renderer with a method that updates the LODs.
*/
enable() {
if (y(this, Y)) return;
console.debug("[gltf-progressive] Enabling LODsManager for renderer");
let t = 0;
N(this, Y, this.renderer.render);
const e = this;
Fe(this.renderer), this.renderer.render = function(s, o) {
const n = e.renderer.getRenderTarget();
(n == null || "isXRRenderTarget" in n && n.isXRRenderTarget) && (t = 0, N(e, se, y(e, se) + 1), N(e, re, y(e, xe).getDelta()), N(e, De, y(e, De) + y(e, re)), e._fpsBuffer.shift(), e._fpsBuffer.push(1 / y(e, re)), N(e, H, e._fpsBuffer.reduce((a, l) => a + l) / e._fpsBuffer.length), G && y(e, se) % 200 === 0 && console.log("FPS", Math.round(y(e, H)), "Interval:", y(e, E)));
const r = t++;
y(e, Y).call(this, s, o), e.onAfterRender(s, o, r);
};
}
disable() {
y(this, Y) && (console.debug("[gltf-progressive] Disabling LODsManager for renderer"), this.renderer.render = y(this, Y), N(this, Y, void 0));
}
update(t, e) {
this.internalUpdate(t, e);
}
onAfterRender(t, e, s) {
if (this.pause) return;
const n = this.renderer.renderLists.get(t, 0).opaque;
let r = !0;
if (n.length === 1) {
const a = n[0].material;
(a.name === "EffectMaterial" || a.name === "CopyShader") && (r = !1);
}
if ((e.parent && e.parent.type === "CubeCamera" || s >= 1 && e.type === "OrthographicCamera") && (r = !1), r) {
if (yt || (this.updateInterval === "auto" ? y(this, H) < 40 && y(this, E) < 10 ? (N(this, E, y(this, E) + 1), G && console.warn("↓ Reducing LOD updates", y(this, E), y(this, H).toFixed(0))) : y(this, H) >= 60 && y(this, E) > 1 && (N(this, E, y(this, E) - 1), G && console.warn("↑ Increasing LOD updates", y(this, E), y(this, H).toFixed(0))) : N(this, E, this.updateInterval), y(this, E) > 0 && y(this, se) % y(this, E) != 0))
return;
this.internalUpdate(t, e);
}
}
/**
* Update LODs in a scene
*/
internalUpdate(t, e) {
var l, u;
const s = this.renderer.renderLists.get(t, 0), o = s.opaque;
this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix, e.matrixWorldInverse);
const n = this.targetTriangleDensity;
for (const c of o) {
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")) {
G && (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)));
continue;
}
switch (c.material.type) {
case "LineBasicMaterial":
case "LineDashedMaterial":
case "PointsMaterial":
case "ShadowMaterial":
case "MeshDistanceMaterial":
case "MeshDepthMaterial":
continue;
}
if (G === "color" && c.material && !c.object.progressive_debug_color) {
c.object.progressive_debug_color = !0;
const g = Math.random() * 16777215, v = new Qe({ color: g });
c.object.material = v;
}
const h = c.object;
(h instanceof ee || h.isMesh) && this.updateLODs(t, e, h, n);
}
const r = s.transparent;
for (const c of r) {
const h = c.object;
(h instanceof ee || h.isMesh) && this.updateLODs(t, e, h, n);
}
const a = s.transmissive;
for (const c of a) {
const h = c.object;
(h instanceof ee || h.isMesh) && this.updateLODs(t, e, h, n);
}
}
/** Update the LOD levels for the renderer. */
updateLODs(t, e, s, o) {
var a, l;
s.userData || (s.userData = {});
let n = s[be];
if (n || (n = new mt(), s[be] = n), n.frames++ < 2)
return;
for (const u of Q)
(a = u.onBeforeUpdateLOD) == null || a.call(u, this.renderer, t, e, s);
const r = m.overrideGlobalLodLevel !== void 0 ? m.overrideGlobalLodLevel : Z;
r >= 0 ? (C.mesh_lod = r, C.texture_lod = r) : (this.calculateLodLevel(e, s, n, o, C), C.mesh_lod = Math.round(C.mesh_lod), C.texture_lod = Math.round(C.texture_lod)), C.mesh_lod >= 0 && this.loadProgressiveMeshes(s, C.mesh_lod), s.material && C.texture_lod >= 0 && this.loadProgressiveTextures(s.material, C.texture_lod), D && s.material && !s.isGizmo && Ve(s.material);
for (const u of Q)
(l = u.onAfterUpdatedLOD) == null || l.call(u, this.renderer, t, e, s, C);
n.lastLodLevel_Mesh = C.mesh_lod, n.lastLodLevel_Texture = C.texture_lod;
}
/** Load progressive textures for the given material
* @param material the material to load the textures for
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
* @returns Promise with true if the LOD was loaded, false if not
*/
loadProgressiveTextures(t, e) {
if (!t) return;
if (Array.isArray(t)) {
for (const n of t)
this.loadProgressiveTextures(n, e);
return;
}
let s = !1;
(t[J] === void 0 || e < t[J]) && (s = !0);
const o = t["DEBUG:LOD"];
o != null && (s = t[J] != o, e = o), s && (t[J] = e, z.assignTextureLOD(t, e).then((n) => {
this._lodchangedlisteners.forEach((r) => r({ type: "texture", level: e, object: t }));
}));
}
/** Load progressive meshes for the given mesh
* @param mesh the mesh to load the LOD for
* @param index the index of the mesh if it's part of a group
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
* @returns Promise with true if the LOD was loaded, false if not
*/
loadProgressiveMeshes(t, e) {
if (!t) return Promise.resolve(null);
let s = t[J] !== e;
const o = t["DEBUG:LOD"];
if (o != null && (s = t[J] != o, e = o), s) {
t[J] = e;
const n = t.geometry;
return z.assignMeshLOD(t, e).then((r) => (r && t[J] == e && n != t.geometry && this._lodchangedlisteners.forEach((a) => a({ type: "mesh", level: e, object: t })), r));
}
return Promise.resolve(null);
}
static isInside(t, e) {
const s = t.min, o = t.max, n = (s.x + o.x) * 0.5, r = (s.y + o.y) * 0.5;
return this._tempPtInside.set(n, r, s.z).applyMatrix4(e).z < 0;
}
calculateLodLevel(t, e, s, o, n) {
var U, W, w;
if (!e) {
n.mesh_lod = -1, n.texture_lod = -1;
return;
}
if (!t) {
n.mesh_lod = -1, n.texture_lod = -1;
return;
}
let a = 10 + 1, l = !1;
if (G && e["DEBUG:LOD"] != null)
return e["DEBUG:LOD"];
const u = (U = z.getMeshLODExtension(e.geometry)) == null ? void 0 : U.lods, c = z.getPrimitiveIndex(e.geometry), h = u && u.length > 0, g = z.getMaterialMinMaxLODsCount(e.material), v = (g == null ? void 0 : g.min_count) != 1 / 0 && g.min_count > 0 && g.max_count > 0;
if (!h && !v) {
n.mesh_lod = 0, n.texture_lod = 0;
return;
}
h || (l = !0, a = 0);
const b = this.renderer.domElement.clientHeight || this.renderer.domElement.height;
let M = e.geometry.boundingBox;
if (e.type === "SkinnedMesh") {
const _ = e;
if (!_.boundingBox)
_.computeBoundingBox();
else if (this.skinnedMeshAutoUpdateBoundsInterval > 0 && s.frames % this.skinnedMeshAutoUpdateBoundsInterval === 0) {
const p = ue(_), X = _.geometry;
p && (_.geometry = p), _.computeBoundingBox(), _.geometry = X;
}
M = _.boundingBox;
}
if (M) {
const _ = t;
if (e.geometry.attributes.color && e.geometry.attributes.color.count < 100 && e.geometry.boundingSphere) {
this._sphere.copy(e.geometry.boundingSphere), this._sphere.applyMatrix4(e.matrixWorld);
const L = t.getWorldPosition(this._tempWorldPosition);
if (this._sphere.containsPoint(L)) {
n.mesh_lod = 0, n.texture_lod = 0;
return;
}
}
if (this._tempBox.copy(M), this._tempBox.applyMatrix4(e.matrixWorld), _.isPerspectiveCamera && m.isInside(this._tempBox, this.projectionScreenMatrix)) {
n.mesh_lod = 0, n.texture_lod = 0;
return;
}
if (this._tempBox.applyMatrix4(this.projectionScreenMatrix), this.renderer.xr.enabled && _.isPerspectiveCamera && _.fov > 70) {
const L = this._tempBox.min, O = this._tempBox.max;
let P = L.x, k = L.y, F = O.x, ne = O.y;
const de = 2, Me = 1.5, fe = (L.x + O.x) * 0.5, he = (L.y + O.y) * 0.5;
P = (P - fe) * de + fe, k = (k - he) * de + he, F = (F - fe) * de + fe, ne = (ne - he) * de + he;
const qe = P < 0 && F > 0 ? 0 : Math.min(Math.abs(L.x), Math.abs(O.x)), Ke = k < 0 && ne > 0 ? 0 : Math.min(Math.abs(L.y), Math.abs(O.y)), ve = Math.max(qe, Ke);
s.lastCentrality = (Me - ve) * (Me - ve) * (Me - ve);
} else
s.lastCentrality = 1;
const p = this._tempBox.getSize(this._tempBoxSize);
p.multiplyScalar(0.5), screen.availHeight > 0 && b > 0 && p.multiplyScalar(b / screen.availHeight), t.isPerspectiveCamera ? p.x *= t.aspect : t.isOrthographicCamera;
const X = t.matrixWorldInverse, A = this._tempBox2;
A.copy(M), A.applyMatrix4(e.matrixWorld), A.applyMatrix4(X);
const R = A.getSize(this._tempBox2Size), I = Math.max(R.x, R.y);
if (Math.max(p.x, p.y) != 0 && I != 0 && (p.z = R.z / Math.max(R.x, R.y) * Math.max(p.x, p.y)), s.lastScreenCoverage = Math.max(p.x, p.y, p.z), s.lastScreenspaceVolume.copy(p), s.lastScreenCoverage *= s.lastCentrality, G && m.debugDrawLine) {
const L = this.tempMatrix.copy(this.projectionScreenMatrix);
L.invert();
const O = m.corner0, P = m.corner1, k = m.corner2, F = m.corner3;
O.copy(this._tempBox.min), P.copy(this._tempBox.max), P.x = O.x, k.copy(this._tempBox.max), k.y = O.y, F.copy(this._tempBox.max);
const ne = (O.z + F.z) * 0.5;
O.z = P.z = k.z = F.z = ne, O.applyMatrix4(L), P.applyMatrix4(L), k.applyMatrix4(L), F.applyMatrix4(L), m.debugDrawLine(O, P, 255), m.debugDrawLine(O, k, 255), m.debugDrawLine(P, F, 255), m.debugDrawLine(k, F, 255);
}
let f = 999;
if (u && s.lastScreenCoverage > 0)
for (let L = 0; L < u.length; L++) {
const O = u[L];
if ((((W = O.densities) == null ? void 0 : W[c]) || O.density || 1e-5) / s.lastScreenCoverage < o) {
f = L;
break;
}
}
f < a && (a = f, l = !0);
}
if (l ? n.mesh_lod = a : n.mesh_lod = s.lastLodLevel_Mesh, G && n.mesh_lod != s.lastLodLevel_Mesh) {
const p = u == null ? void 0 : u[n.mesh_lod];
p && console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${n.mesh_lod} (${p.density.toFixed(0)}) - ${e.name}`);
}
if (v) {
const _ = "saveData" in globalThis.navigator && globalThis.navigator.saveData === !0;
if (s.lastLodLevel_Texture < 0) {
if (n.texture_lod = g.max_count - 1, G) {
const p = g.lods[g.max_count - 1];
G && console.log(`First Texture LOD ${n.texture_lod} (${p.max_height}px) - ${e.name}`);
}
} else {
const p = s.lastScreenspaceVolume.x + s.lastScreenspaceVolume.y + s.lastScreenspaceVolume.z;
let X = s.lastScreenCoverage * 4;
((w = this.context) == null ? void 0 : w.engine) === "model-viewer" && (X *= 1.5);
const R = b / window.devicePixelRatio * X;
let I = !1;
for (let S = g.lods.length - 1; S >= 0; S--) {
const f = g.lods[S];
if (!(_ && f.max_height >= 2048) && !(ut() && f.max_height > 4096) && (f.max_height > R || !I && S === 0)) {
if (I = !0, n.texture_lod = S, n.texture_lod < s.lastLodLevel_Texture) {
const T = f.max_height;
G && console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${n.texture_lod} = ${T}px
Screensize: ${R.toFixed(0)}px, Coverage: ${(100 * s.lastScreenCoverage).toFixed(2)}%, Volume ${p.toFixed(1)}
${e.name}`);
}
break;
}
}
}
} else
n.texture_lod = 0;
}
}, E = new WeakMap(), Y = new WeakMap(), xe = new WeakMap(), se = new WeakMap(), re = new WeakMap(), De = new WeakMap(), H = 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.
*/
d(m, "debugDrawLine"), /**
* Force override the LOD level for all objects in the scene
*/
d(m, "overrideGlobalLodLevel"), d(m, "corner0", new q()), d(m, "corner1", new q()), d(m, "corner2", new q()), d(m, "corner3", new q()), d(m, "_tempPtInside", new q()), m);
class mt {
constructor() {
d(this, "frames", 0);
d(this, "lastLodLevel_Mesh", -1);
d(this, "lastLodLevel_Texture", -1);
d(this, "lastScreenCoverage", 0);
d(this, "lastScreenspaceVolume", new q());
d(this, "lastCentrality", 0);
}
}
const Re = Symbol("NEEDLE_mesh_lod"), ge = Symbol("NEEDLE_texture_lod");
let le = null;
function We() {
const i = Lt();
i && (i.mapURLs(function(t) {
return ke(), t;
}), ke(), le == null || le.disconnect(), le = new MutationObserver((t) => {
t.forEach((e) => {
e.addedNodes.forEach((s) => {
s instanceof HTMLElement && s.tagName.toLowerCase() === "model-viewer" && Xe(s);
});
});
}), le.observe(document, { childList: !0, subtree: !0 }));
}
function Lt() {
if (typeof customElements > "u") return null;
const i = customElements.get("model-viewer");
return i || (customElements.whenDefined("model-viewer").then(() => {
console.debug("[gltf-progressive] model-viewer defined"), We();
}), null);
}
function ke() {
if (typeof document > "u") return;
document.querySelectorAll("model-viewer").forEach((t) => {
Xe(t);
});
}
const Ge = /* @__PURE__ */ new WeakSet();
let xt = 0;
function Xe(i) {
if (!i || Ge.has(i))
return null;
Ge.add(i), console.debug("[gltf-progressive] found new model-viewer..." + ++xt + `
`, i.getAttribute("src"));
let t = null, e = null, s = null;
for (let o = i; o != null; o = Object.getPrototypeOf(o)) {
const n = Object.getOwnPropertySymbols(o), r = n.find((u) => u.toString() == "Symbol(renderer)"), a = n.find((u) => u.toString() == "Symbol(scene)"), l = n.find((u) => u.toString() == "Symbol(needsRender)");
!t && r != null && (t = i[r].threeRenderer), !e && a != null && (e = i[a]), !s && l != null && (s = i[l]);
}
if (t && e) {
let o = function() {
if (s) {
let r = 0, a = setInterval(() => {
if (r++ > 5) {
clearInterval(a);
return;
}
s == null || s.call(i);
}, 300);
}
};
console.debug("[gltf-progressive] setup model-viewer");
const n = Le.get(t, { engine: "model-viewer" });
return Le.addPlugin(new Dt()), n.enable(), n.addEventListener("changed", () => {
s == null || s.call(i);
}), i.addEventListener("model-visibility", (r) => {
r.detail.visible && (s == null || s.call(i));
}), i.addEventListener("load", () => {
o();
}), () => {
n.disable();
};
}
return null;
}
class Dt {
constructor() {
d(this, "_didWarnAboutMissingUrl", !1);
}
onBeforeUpdateLOD(t, e, s, o) {
this.tryParseMeshLOD(e, o), this.tryParseTextureLOD(e, o);
}
getUrl(t) {
if (!t)
return null;
let e = t.getAttribute("src");
return e || (e = t.src), e || (this._didWarnAboutMissingUrl || console.warn("No url found in modelviewer", t), this._didWarnAboutMissingUrl = !0), e;
}
tryGetCurrentGLTF(t) {
return t._currentGLTF;
}
tryGetCurrentModelViewer(t) {
return t.element;
}
tryParseTextureLOD(t, e) {
if (e[ge] == !0) return;
e[ge] = !0;
const s = this.tryGetCurrentGLTF(t), o = this.tryGetCurrentModelViewer(t), n = this.getUrl(o);
if (n && s && e.material) {
let r = function(l) {
var c, h, g;
if (l[ge] == !0) return;
l[ge] = !0, l.userData && (l.userData.LOD = -1);
const u = Object.keys(l);
for (let v = 0; v < u.length; v++) {
const b = u[v], M = l[b];
if ((M == null ? void 0 : M.isTexture) === !0) {
const U = (h = (c = M.userData) == null ? void 0 : c.associations) == null ? void 0 : h.textures;
if (U == null) continue;
const W = s.parser.json.textures[U];
if (!W) {
console.warn("Texture data not found for texture index " + U);
continue;
}
if ((g = W == null ? void 0 : W.extensions) != null && g[K]) {
const w = W.extensions[K];
w && n && z.registerTexture(n, M, w.lods.length, U, w);
}
}
}
};
const a = e.material;
if (Array.isArray(a)) for (const l of a) r(l);
else r(a);
}
}
tryParseMeshLOD(t, e) {
var r, a;
if (e[Re] == !0) return;
e[Re] = !0;
const s = this.tryGetCurrentModelViewer(t), o = this.getUrl(s);
if (!o)
return;
const n = (a = (r = e.userData) == null ? void 0 : r.gltfExtensions) == null ? void 0 : a[K];
if (n && o) {
const l = e.uuid;
z.registerMesh(o, l, e, 0, n.lods.length, n);
}
}
}
function Mt(i, t, e, s) {
Fe(t), Ne(e), ze(e, {
progressive: !0,
...s == null ? void 0 : s.hints
}), e.register((n) => new z(n, i));
const o = Le.get(t);
return (s == null ? void 0 : s.enableLODsManager) !== !1 && o.enable(), o;
}
We();
if (!dt) {
const i = {
gltfProgressive: {
useNeedleProgressive: Mt,
LODsManager: Le,
configureLoader: ze,
getRaycastMesh: ue,
useRaycastMeshes: ht
}
};
if (!globalThis.Needle)
globalThis.Needle = i;
else
for (const t in i)
globalThis.Needle[t] = i[t];
}
export {
Le as LODsManager,
z as NEEDLE_progressive,
Ne as addDracoAndKTX2Loaders,
ze as configureLoader,
Fe as createLoaders,
ue as getRaycastMesh,
it as setDracoDecoderLocation,
at as setKTX2TranscoderLocation
};