@needle-tools/engine 4.7.3-next.24c33e7 → 4.7.3-next.3bba8e3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import path from "path";
1
2
 
2
3
  let isStringifying = false;
3
4
 
@@ -36,41 +37,80 @@ function sendLogToServer(level, ...message) {
36
37
  }
37
38
  }
38
39
 
39
- // const obj = {
40
- // hello: "world"
41
- // }
42
- // obj["test"] = obj;
43
- // sendLogToServer("internal", "Test circular reference", obj);
40
+ function logHelper(fn, args) {
41
+ const error = new Error();
42
+ const stack = error.stack;
43
+ const caller = stack?.split('\n')[3]; // Get the actual caller
44
+ const path = caller?.trim();
45
+ if (!path) {
46
+ return fn(...args);
47
+ }
48
+ const pathWithoutBrackets = path.replaceAll("(", "").replaceAll(")", "");
49
+ fn(...args, `\n» ${pathWithoutBrackets}`);
50
+ }
44
51
 
45
52
  if (import.meta && "hot" in import.meta) {
46
53
 
47
- const originalLog = console.log;
48
- const originalWarn = console.warn;
49
- const originalInfo = console.info;
50
- const originalDebug = console.debug;
51
- const originalError = console.error;
54
+ function patchLogs() {
55
+ const originalLog = console.log.bind(console);
56
+ const originalWarn = console.warn.bind(console);
57
+ const originalInfo = console.info.bind(console);
58
+ const originalDebug = console.debug.bind(console);
59
+ const originalError = console.error.bind(console);
52
60
 
53
- console.log = (...args) => {
54
- originalLog(...args);
55
- sendLogToServer("log", ...args);
56
- }
57
- console.warn = (...args) => {
58
- originalWarn(...args);
59
- sendLogToServer("warn", ...args);
60
- }
61
- console.info = (...args) => {
62
- originalInfo(...args);
63
- sendLogToServer("info", ...args);
61
+ console.log = function (...args) {
62
+ logHelper(originalLog, args);
63
+ sendLogToServer("log", ...args);
64
+ }
65
+ console.warn = (...args) => {
66
+ logHelper(originalWarn, args);
67
+ sendLogToServer("warn", ...args);
68
+ }
69
+ console.info = (...args) => {
70
+ logHelper(originalInfo, args);
71
+ sendLogToServer("info", ...args);
72
+ }
73
+ console.debug = (...args) => {
74
+ logHelper(originalDebug, args);
75
+ sendLogToServer("debug", ...args);
76
+ }
77
+ console.error = (...args) => {
78
+ logHelper(originalError, args);
79
+ sendLogToServer("error", ...args);
80
+ }
81
+ return () => {
82
+ console.log = originalLog;
83
+ console.warn = originalWarn;
84
+ console.info = originalInfo;
85
+ console.debug = originalDebug;
86
+ console.error = originalError;
87
+ }
64
88
  }
65
- console.debug = (...args) => {
66
- originalDebug(...args);
67
- sendLogToServer("debug", ...args);
89
+
90
+ const query = new URLSearchParams(window.location.search);
91
+ if (query.has("needle-debug")) {
92
+ patchLogs();
68
93
  }
69
- console.error = (...args) => {
70
- originalError(...args);
71
- sendLogToServer("error", ...args);
94
+ else {
95
+ // const unpatch = patchLogs();
96
+ // setTimeout(() => {
97
+ // sendLogToServer("internal", "Stop listening to console.log.");
98
+ // unpatch();
99
+ // }, 10_000);
100
+
101
+ const threshold = 100;
102
+ const devToolsArePotentiallyOpen = window.outerHeight - window.innerHeight > threshold || window.outerWidth - window.innerWidth > threshold;
103
+ if (devToolsArePotentiallyOpen) {
104
+ sendLogToServer("internal", "Console logging is disabled (devtools are open)");
105
+ }
106
+ else {
107
+ sendLogToServer("internal", "Console logging is enabled");
108
+ patchLogs();
109
+ }
72
110
  }
73
111
 
112
+
113
+
74
114
  try {
75
115
  sendLogToServer("internal", `Page loaded
76
116
  URL: ${window.location.href}
@@ -86,7 +126,6 @@ User Activation: ${"userActivation" in navigator ? JSON.stringify(navigator.user
86
126
  `);
87
127
 
88
128
  if ("gpu" in navigator) {
89
-
90
129
  // @ts-ignore
91
130
  navigator.gpu.requestAdapter()
92
131
  .then(adapter => adapter ? adapter.requestDevice() : null)
@@ -121,10 +160,9 @@ User Activation: ${"userActivation" in navigator ? JSON.stringify(navigator.user
121
160
  sendLogToServer("error", `Unhandled promise rejection: ${reason}`);
122
161
  });
123
162
  window.addEventListener('beforeunload', () => {
124
- sendLogToServer("internal", "Page is unloading");
163
+ sendLogToServer("internal", "Page is unloading\n\n");
125
164
  });
126
165
  document.addEventListener('visibilitychange', () => {
127
- console.log("Visibility changed:", document.visibilityState);
128
166
  if (document.visibilityState === 'hidden') {
129
167
  sendLogToServer("internal", "Page is hidden");
130
168
  }
@@ -92,10 +92,10 @@ function logRequests(server, log_http_requests = false) {
92
92
  });
93
93
  }
94
94
  // Log HTTP requests
95
- if (log_http_requests) {
96
- server.middlewares.use((req, res, next) => {
95
+ server.middlewares.use((req, res, next) => {
96
+ if (log_http_requests) {
97
97
  captureLogMessage("client-http", "info", [req.method, req.url], null);
98
- next();
99
- });
100
- }
98
+ }
99
+ next();
100
+ });
101
101
  }
@@ -58,8 +58,8 @@ export class LODsManager implements NEEDLE_progressive_plugin {
58
58
  }
59
59
 
60
60
  private applySettings() {
61
- if(this._lodsManager) {
62
- for(const key in this._settings) {
61
+ if (this._lodsManager) {
62
+ for (const key in this._settings) {
63
63
  this._lodsManager[key] = this._settings[key];
64
64
  }
65
65
  }
@@ -71,7 +71,7 @@ export class LODsManager implements NEEDLE_progressive_plugin {
71
71
  _LODsManager.removePlugin(this);
72
72
  _LODsManager.addPlugin(this);
73
73
  _LODsManager.debugDrawLine = Gizmos.DrawLine;
74
- this._lodsManager = _LODsManager.get(renderer);
74
+ this._lodsManager = _LODsManager.get(renderer, { engine: "needle-engine" });
75
75
  this.applySettings();
76
76
  this._lodsManager.enable();
77
77
  }
@@ -477,7 +477,7 @@ export class PostProcessingHandler {
477
477
  }
478
478
 
479
479
  // DEBUG LAND BELOW
480
- if (debug) console.log("[PostProcessing] Passes →", [...composer.passes], "\n---------------------------------\n• " + composer.passes.map(i => i.name).join("\n• ") + "\n");
480
+ if (debug) console.log("[PostProcessing] Passes →", [...composer.passes], "\n---------------------------------\n• " + composer.passes.map(i => i.name || (i.constructor.name + "*")).join("\n• ") + "\n");
481
481
  if (debug) this._onCreateEffectsDebug(this._composer!, cam);
482
482
  }
483
483
 
@@ -75,10 +75,11 @@ export class Volume extends Behaviour implements IEditorModificationReceiver, IP
75
75
  /**
76
76
  * Add a post processing effect to the stack and schedules the effect stack to be re-created.
77
77
  */
78
- addEffect<T extends PostProcessingEffect | Effect>(effect: T): T {
78
+ addEffect<T extends PostProcessingEffect | Effect>(effect: T & { order?: number }): T {
79
79
  let entry = effect as PostProcessingEffect;
80
80
  if (!(entry instanceof PostProcessingEffect)) {
81
81
  entry = new EffectWrapper(entry);
82
+ if (typeof effect.order === "number") entry.order = effect.order;
82
83
  }
83
84
  if (entry.gameObject === undefined) this.gameObject.addComponent(entry);
84
85
  if (this._effects.includes(entry)) return effect;
@@ -1,8 +0,0 @@
1
- var Ke=Object.defineProperty,Ee=s=>{throw TypeError(s)},He=(s,e,t)=>e in s?Ke(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,c=(s,e,t)=>He(s,typeof e!="symbol"?e+"":e,t),Ce=(s,e,t)=>e.has(s)||Ee("Cannot "+t),v=(s,e,t)=>(Ce(s,e,"read from private field"),t?t.call(s):e.get(s)),J=(s,e,t)=>e.has(s)?Ee("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t),$=(s,e,t,n)=>(Ce(s,e,"write to private field"),n?n.call(s,t):e.set(s,t),t);import{BufferGeometry as de,Mesh as Q,Texture as se,TextureLoader as Ye,Matrix4 as Ie,Clock as Je,MeshStandardMaterial as Qe,Sphere as Ze,Box3 as Be,Vector3 as F}from"./three-DuDKwKB8.min.js";import{DRACOLoader as et,KTX2Loader as tt,MeshoptDecoder as st,GLTFLoader as Le}from"./three-examples-BMOhDaYR.min.js";const rt="";globalThis.GLTF_PROGRESSIVE_VERSION=rt,console.debug("[gltf-progressive] version -");let W="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",ee="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const nt=W,ot=ee,Ge=new URL(W+"draco_decoder.js");Ge.searchParams.append("range","true"),fetch(Ge,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(s=>{console.debug(`Failed to fetch remote Draco decoder from ${W} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),W===nt&&ke("./include/draco/"),ee===ot&&je("./include/ktx2/")}).finally(()=>{Re()});function ke(s){W=s,A&&A[we]!=W?(console.debug("Updating Draco decoder path to "+s),A[we]=W,A.setDecoderPath(W),A.preload()):console.debug("Setting Draco decoder path to "+s)}function je(s){ee=s,B&&B.transcoderPath!=ee?(console.debug("Updating KTX2 transcoder path to "+s),B.setTranscoderPath(ee),B.init()):console.debug("Setting KTX2 transcoder path to "+s)}const we=Symbol("dracoDecoderPath");let A,ce,B;function Re(){A||(A=new et,A[we]=W,A.setDecoderPath(W),A.setDecoderConfig({type:"js"}),A.preload()),B||(B=new tt,B.setTranscoderPath(ee),B.init()),ce||(ce=st)}function _e(s){return Re(),s?B.detectSupport(s):s!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:A,ktx2Loader:B,meshoptDecoder:ce}}function Me(s){s.dracoLoader||s.setDRACOLoader(A),s.ktx2Loader||s.setKTX2Loader(B),s.meshoptDecoder||s.setMeshoptDecoder(ce)}const De=new WeakMap;function be(s,e){let t=De.get(s);t?t=Object.assign(t,e):t=e,De.set(s,t)}const Ne=Le.prototype.load;function it(...s){const e=De.get(this);let t=s[0];const n=new URL(t,window.location.href);if(n.hostname.endsWith("needle.tools")){const r=e?.progressive!==void 0?e.progressive:!0,i=e!=null&&e.usecase?e.usecase:"default";r?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${i}`:this.requestHeader.Accept=`*/*;usecase=${i}`,t=n.toString()}return s[0]=t,Ne?.call(this,...s)}Le.prototype.load=it,re("debugprogressive");function re(s){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(s);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function at(s,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||s===void 0)return e;const t=s.lastIndexOf("/");if(t>=0){const n=s.substring(0,t+1);for(;n.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return n+e}return e}let he;function lt(){return he!==void 0||(he=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),re("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",he)),he}function ut(){if(typeof window>"u")return!1;const s=new URL(window.location.href),e=s.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(s.hostname);return s.hostname==="127.0.0.1"||e}const dt=typeof window>"u"&&typeof document>"u",Oe=Symbol("needle:raycast-mesh");function te(s){return s?.[Oe]instanceof de?s[Oe]:null}function ct(s,e){if((s.type==="Mesh"||s.type==="SkinnedMesh")&&!te(s)){const t=gt(e);t.userData={isRaycastMesh:!0},s[Oe]=t}}function ht(s=!0){if(s){if(ne)return;const e=ne=Q.prototype.raycast;Q.prototype.raycast=function(t,n){const r=this,i=te(r);let o;i&&r.isMesh&&(o=r.geometry,r.geometry=i),e.call(this,t,n),o&&(r.geometry=o)}}else{if(!ne)return;Q.prototype.raycast=ne,ne=null}}let ne=null;function gt(s){const e=new de;for(const t in s.attributes)e.setAttribute(t,s.getAttribute(t));return e.setIndex(s.getIndex()),e}const X=new Array,x=re("debugprogressive");let ge,Z=-1;if(x){let s=function(){Z+=1,Z>=e&&(Z=-1),console.log(`Toggle LOD level [${Z}]`)},e=6;window.addEventListener("keyup",t=>{t.key==="p"&&s(),t.key==="w"&&(ge=!ge,console.log(`Toggle wireframe [${ge}]`));const n=parseInt(t.key);!isNaN(n)&&n>=0&&(Z=n,console.log(`Set LOD level to [${Z}]`))})}function We(s){if(x)if(Array.isArray(s))for(const e of s)We(e);else s&&"wireframe"in s&&(s.wireframe=ge===!0)}const U="NEEDLE_progressive",Se=Symbol("needle-progressive-texture"),E=class k{constructor(e,t){c(this,"parser"),c(this,"url"),c(this,"_isLoadingMesh"),c(this,"loadMesh",n=>{var r,i;if(this._isLoadingMesh)return null;const o=(i=(r=this.parser.json.meshes[n])==null?void 0:r.extensions)==null?void 0:i[U];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",n).then(a=>{var l;return this._isLoadingMesh=!1,a&&k.registerMesh(this.url,o.guid,a,(l=o.lods)==null?void 0:l.length,0,o),a})):null}),x&&console.log("Progressive extension registered for",t),this.parser=e,this.url=t}get name(){return U}static getMeshLODExtension(e){const t=this.getAssignedLODInformation(e);return t!=null&&t.key?this.lodInfos.get(t.key):null}static getPrimitiveIndex(e){var t;return((t=this.getAssignedLODInformation(e))==null?void 0:t.index)??-1}static getMaterialMinMaxLODsCount(e,t){const n=this,r="LODS:minmax",i=e[r];if(i!=null)return i;if(t||(t={min_count:1/0,max_count:0,lods:[]}),Array.isArray(e)){for(const a of e)this.getMaterialMinMaxLODsCount(a,t);return e[r]=t,t}if(x==="verbose"&&console.log("getMaterialMinMaxLODsCount",e),e.type==="ShaderMaterial"||e.type==="RawShaderMaterial"){const a=e;for(const l of Object.keys(a.uniforms)){const u=a.uniforms[l].value;u?.isTexture===!0&&o(u,t)}}else if(e.isMaterial)for(const a of Object.keys(e)){const l=e[a];l?.isTexture===!0&&o(l,t)}return e[r]=t,t;function o(a,l){const u=n.getAssignedLODInformation(a);if(u){const d=n.lodInfos.get(u.key);if(d&&d.lods){l.min_count=Math.min(l.min_count,d.lods.length),l.max_count=Math.max(l.max_count,d.lods.length);for(let w=0;w<d.lods.length;w++){const y=d.lods[w];y.width&&(l.lods[w]=l.lods[w]||{min_height:1/0,max_height:0},l.lods[w].min_height=Math.min(l.lods[w].min_height,y.height),l.lods[w].max_height=Math.max(l.lods[w].max_height,y.height))}}}}}static hasLODLevelAvailable(e,t){var n;if(Array.isArray(e)){for(const o of e)if(this.hasLODLevelAvailable(o,t))return!0;return!1}if(e.isMaterial===!0){for(const o of Object.keys(e)){const a=e[o];if(a&&a.isTexture&&this.hasLODLevelAvailable(a,t))return!0}return!1}else if(e.isGroup===!0){for(const o of e.children)if(o.isMesh===!0&&this.hasLODLevelAvailable(o,t))return!0}let r,i;if(e.isMesh?r=e.geometry:(e.isBufferGeometry||e.isTexture)&&(r=e),r&&(n=r?.userData)!=null&&n.LODS){const o=r.userData.LODS;if(i=this.lodInfos.get(o.key),t===void 0)return i!=null;if(i)return Array.isArray(i.lods)?t<i.lods.length:t===0}return!1}static assignMeshLOD(e,t){var n;if(!e)return Promise.resolve(null);if(e instanceof Q||e.isMesh===!0){const r=e.geometry,i=this.getAssignedLODInformation(r);if(!i)return Promise.resolve(null);for(const o of X)(n=o.onBeforeGetLODMesh)==null||n.call(o,e,t);return e["LOD:requested level"]=t,k.getOrLoadLOD(r,t).then(o=>{if(Array.isArray(o)){const a=i.index||0;o=o[a]}return e["LOD:requested level"]===t&&(delete e["LOD:requested level"],o&&r!=o&&(o?.isBufferGeometry?e.geometry=o:x&&console.error("Invalid LOD geometry",o))),o}).catch(o=>(console.error("Error loading mesh LOD",e,o),null))}else x&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",e);return Promise.resolve(null)}static assignTextureLOD(e,t=0){if(!e)return Promise.resolve(null);if(e.isMesh===!0){const n=e;if(Array.isArray(n.material)){const r=new Array;for(const i of n.material){const o=this.assignTextureLOD(i,t);r.push(o)}return Promise.all(r).then(i=>{const o=new Array;for(const a of i)Array.isArray(a)&&o.push(...a);return o})}else return this.assignTextureLOD(n.material,t)}if(e.isMaterial===!0){const n=e,r=[],i=new Array;if(n.uniforms&&(n.isRawShaderMaterial||n.isShaderMaterial===!0)){const o=n;for(const a of Object.keys(o.uniforms)){const l=o.uniforms[a].value;if(l?.isTexture===!0){const u=this.assignTextureLODForSlot(l,t,n,a).then(d=>(d&&o.uniforms[a].value!=d&&(o.uniforms[a].value=d,o.uniformsNeedUpdate=!0),d));r.push(u),i.push(a)}}}else for(const o of Object.keys(n)){const a=n[o];if(a?.isTexture===!0){const l=this.assignTextureLODForSlot(a,t,n,o);r.push(l),i.push(o)}}return Promise.all(r).then(o=>{const a=new Array;for(let l=0;l<o.length;l++){const u=o[l],d=i[l];u&&u.isTexture===!0?a.push({material:n,slot:d,texture:u,level:t}):a.push({material:n,slot:d,texture:null,level:t})}return a})}if(e instanceof se||e.isTexture===!0){const n=e;return this.assignTextureLODForSlot(n,t,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(e,t,n,r){return e?.isTexture!==!0?Promise.resolve(null):r==="glyphMap"?Promise.resolve(e):k.getOrLoadLOD(e,t).then(i=>{if(Array.isArray(i))return null;if(i?.isTexture===!0){if(i!=e&&n&&r){const o=n[r];if(o&&!x){const a=this.getAssignedLODInformation(o);if(a&&a?.level<t)return x==="verbose"&&console.warn("Assigned texture level is already higher: ",a.level,t,n,o,i),null}n[r]=i}return i}else x=="verbose"&&console.warn("No LOD found for",e,t);return null}).catch(i=>(console.error("Error loading LOD",e,i),null))}afterRoot(e){var t,n;return x&&console.log("AFTER",this.url,e),(t=this.parser.json.textures)==null||t.forEach((r,i)=>{var o;if(r!=null&&r.extensions){const a=r?.extensions[U];if(a){if(!a.lods){x&&console.warn("Texture has no LODs",a);return}let l=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const d=this.parser.associations.get(u);d?.textures===i&&(l=!0,k.registerTexture(this.url,u,(o=a.lods)==null?void 0:o.length,i,a))}l||this.parser.getDependency("texture",i).then(u=>{var d;u&&k.registerTexture(this.url,u,(d=a.lods)==null?void 0:d.length,i,a)})}}}),(n=this.parser.json.meshes)==null||n.forEach((r,i)=>{if(r!=null&&r.extensions){const o=r?.extensions[U];if(o&&o.lods){for(const a of this.parser.associations.keys())if(a.isMesh){const l=this.parser.associations.get(a);l?.meshes===i&&k.registerMesh(this.url,o.guid,a,o.lods.length,l.primitives,o)}}}}),null}static async getOrLoadLOD(e,t){var n,r,i,o;const a=x=="verbose",l=e.userData.LODS;if(!l)return null;const u=l?.key;let d;if(e.isTexture===!0){const w=e;w.source&&w.source[Se]&&(d=w.source[Se])}if(d||(d=k.lodInfos.get(u)),d){if(t>0){let m=!1;const j=Array.isArray(d.lods);if(j&&t>=d.lods.length?m=!0:j||(m=!0),m)return this.lowresCache.get(u)}const w=Array.isArray(d.lods)?(n=d.lods[t])==null?void 0:n.path:d.lods;if(!w)return x&&!d["missing:uri"]&&(d["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+t,d)),null;const y=at(l.url,w);if(y.endsWith(".glb")||y.endsWith(".gltf")){if(!d.guid)return console.warn("missing pointer for glb/gltf texture",d),null;const m=y+"_"+d.guid,j=this.previouslyLoaded.get(m);if(j!==void 0){a&&console.log(`LOD ${t} was already loading/loaded: ${m}`);let g=await j.catch(I=>(console.error(`Error loading LOD ${t} from ${y}
2
- `,I),null)),p=!1;if(g==null||(g instanceof se&&e instanceof se?(r=g.image)!=null&&r.data||(i=g.source)!=null&&i.data?g=this.copySettings(e,g):(p=!0,this.previouslyLoaded.delete(m)):g instanceof de&&e instanceof de&&((o=g.attributes.position)!=null&&o.array||(p=!0,this.previouslyLoaded.delete(m)))),!p)return g}const M=d,C=new Promise(async(g,p)=>{const I=new Le;Me(I),x&&(await new Promise(f=>setTimeout(f,1e3)),a&&console.warn("Start loading (delayed) "+y,M.guid));let R=y;if(M&&Array.isArray(M.lods)){const f=M.lods[t];f.hash&&(R+="?v="+f.hash)}const D=await I.loadAsync(R).catch(f=>(console.error(`Error loading LOD ${t} from ${y}
3
- `,f),null));if(!D)return null;const N=D.parser;a&&console.log("Loading finished "+y,M.guid);let b=0;if(D.parser.json.textures){let f=!1;for(const h of D.parser.json.textures){if(h!=null&&h.extensions){const L=h?.extensions[U];if(L!=null&&L.guid&&L.guid===M.guid){f=!0;break}}b++}if(f){let h=await N.getDependency("texture",b);return h&&k.assignLODInformation(l.url,h,u,t,void 0),a&&console.log('change "'+e.name+'" \u2192 "'+h.name+'"',y,b,h,m),e instanceof se&&(h=this.copySettings(e,h)),h&&(h.guid=M.guid),g(h)}else x&&console.warn("Could not find texture with guid",M.guid,D.parser.json)}if(b=0,D.parser.json.meshes){let f=!1;for(const h of D.parser.json.meshes){if(h!=null&&h.extensions){const L=h?.extensions[U];if(L!=null&&L.guid&&L.guid===M.guid){f=!0;break}}b++}if(f){const h=await N.getDependency("mesh",b);if(a&&console.log(`Loaded Mesh "${h.name}"`,y,b,h,m),h.isMesh===!0){const L=h.geometry;return k.assignLODInformation(l.url,L,u,t,0),g(L)}else{const L=new Array;for(let O=0;O<h.children.length;O++){const T=h.children[O];if(T.isMesh===!0){const V=T.geometry;k.assignLODInformation(l.url,V,u,t,O),L.push(V)}}return g(L)}}else x&&console.warn("Could not find mesh with guid",M.guid,D.parser.json)}return g(null)});return this.previouslyLoaded.set(m,C),await C}else if(e instanceof se){a&&console.log("Load texture from uri: "+y);const m=await new Ye().loadAsync(y);return m?(m.guid=d.guid,m.flipY=!1,m.needsUpdate=!0,m.colorSpace=e.colorSpace,a&&console.log(d,m)):x&&console.warn("failed loading",y),m}}else x&&console.warn(`Can not load LOD ${t}: no LOD info found for "${u}" ${e.name}`,e.type);return null}static assignLODInformation(e,t,n,r,i){if(!t)return;t.userData||(t.userData={});const o=new ft(e,n,r,i);t.userData.LODS=o}static getAssignedLODInformation(e){var t;return((t=e?.userData)==null?void 0:t.LODS)||null}static copySettings(e,t){return t?(x&&console.warn(`Copy texture settings
4
- `,e.uuid,`
5
- `,t.uuid),t=t.clone(),t.offset=e.offset,t.repeat=e.repeat,t.colorSpace=e.colorSpace,t.magFilter=e.magFilter,t.minFilter=e.minFilter,t.wrapS=e.wrapS,t.wrapT=e.wrapT,t.flipY=e.flipY,t.anisotropy=e.anisotropy,t.mipmaps||(t.generateMipmaps=e.generateMipmaps),t):e}};c(E,"registerTexture",(s,e,t,n,r)=>{if(x&&console.log("> Progressive: register texture",n,e.name,e.uuid,e,r),!e){x&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Se]=r);const i=r.guid;E.assignLODInformation(s,e,i,t,n),E.lodInfos.set(i,r),E.lowresCache.set(i,e)}),c(E,"registerMesh",(s,e,t,n,r,i)=>{var o;const a=t.geometry;if(!a){x&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),x&&console.log("> Progressive: register mesh "+t.name,{index:r,uuid:t.uuid},i,t),E.assignLODInformation(s,a,e,n,r),E.lodInfos.set(e,i);let l=E.lowresCache.get(e);l?l.push(t.geometry):l=[t.geometry],E.lowresCache.set(e,l),n>0&&!te(t)&&ct(t,a);for(const u of X)(o=u.onRegisteredNewMesh)==null||o.call(u,t,i)}),c(E,"lodInfos",new Map),c(E,"previouslyLoaded",new Map),c(E,"lowresCache",new Map);let z=E;class ft{constructor(e,t,n,r){c(this,"url"),c(this,"key"),c(this,"level"),c(this,"index"),this.url=e,this.key=t,this.level=n,r!=null&&(this.index=r)}}class fe{constructor(e,t){c(this,"frame_start"),c(this,"frame_capture_end"),c(this,"ready"),c(this,"_resolve"),c(this,"_signal"),c(this,"_resolved",!1),c(this,"_addedCount",0),c(this,"_resolvedCount",0),c(this,"_awaiting",[]),c(this,"_maxPromisesPerObject",1),c(this,"_currentFrame",0),c(this,"_seen",new WeakMap);var n;const r=Math.max(t.frames??2,2);this.frame_start=e,this.frame_capture_end=e+r,this.ready=new Promise(i=>{this._resolve=i}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=t.signal,(n=this._signal)==null||n.addEventListener("abort",()=>{this.resolveNow()}),this._maxPromisesPerObject=Math.max(1,t.maxPromisesPerObject??1)}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(e){var t;this._currentFrame=e,((t=this._signal)!=null&&t.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(e,t,n){if(this._resolved){x&&console.warn("PromiseGroup: Trying to add a promise to a resolved group, ignoring.");return}if(!(this._currentFrame>this.frame_capture_end)){if(this._maxPromisesPerObject>=1)if(this._seen.has(t)){let r=this._seen.get(t);if(r>=this._maxPromisesPerObject){x&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(t,r+1)}else this._seen.set(t,1);this._awaiting.push(n),this._addedCount++,n.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(n),1)})}}resolveNow(){var e,t;this._resolved||(t=this._resolve)==null||t.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((e=this._signal)==null?void 0:e.aborted)??!1})}}c(fe,"addPromise",(s,e,t,n)=>{n.forEach(r=>{r.add(s,e,t)})});const G=re("debugprogressive"),mt=re("noprogressive"),Pe=Symbol("Needle:LODSManager"),Te=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),S={mesh_lod:-1,texture_lod:-1};var _,P,K,Ae,H,oe,me,Y;let ie=(_=class{constructor(s,e){c(this,"renderer"),c(this,"context"),c(this,"projectionScreenMatrix",new Ie),c(this,"targetTriangleDensity",2e5),c(this,"skinnedMeshAutoUpdateBoundsInterval",30),c(this,"updateInterval","auto"),J(this,P,1),c(this,"pause",!1),c(this,"manual",!1),c(this,"_newPromiseGroups",[]),c(this,"_promiseGroupIds",0),c(this,"_lodchangedlisteners",[]),J(this,K),J(this,Ae,new Je),J(this,H,0),J(this,oe,0),J(this,me,0),J(this,Y,0),c(this,"_fpsBuffer",[60,60,60,60,60]),c(this,"_sphere",new Ze),c(this,"_tempBox",new Be),c(this,"_tempBox2",new Be),c(this,"tempMatrix",new Ie),c(this,"_tempWorldPosition",new F),c(this,"_tempBoxSize",new F),c(this,"_tempBox2Size",new F),this.renderer=s,this.context={...e}}static getObjectLODState(s){return s[Te]}static addPlugin(s){X.push(s)}static removePlugin(s){const e=X.indexOf(s);e>=0&&X.splice(e,1)}static get(s,e){if(s[Pe])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),s[Pe];const t=new _(s,{engine:"unknown",...e});return s[Pe]=t,t}get plugins(){return X}awaitLoading(s){const e=this._promiseGroupIds++,t=new fe(v(this,H),{...s});this._newPromiseGroups.push(t);const n=performance.now();return t.ready.finally(()=>{const r=this._newPromiseGroups.indexOf(t);r>=0&&(this._newPromiseGroups.splice(r,1),ut()&&performance.measure("LODsManager:awaitLoading",{start:n,detail:{id:e,name:s?.name,awaited:t.awaitedCount,resolved:t.resolvedCount}}))}),t.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let s=this._newPromiseGroups.length-1;s>=0;s--)this._newPromiseGroups[s].update(v(this,H))}addEventListener(s,e){s==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(s,e){if(s==="changed"){const t=this._lodchangedlisteners.indexOf(e);t>=0&&this._lodchangedlisteners.splice(t,1)}}enable(){if(v(this,K))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let s=0;$(this,K,this.renderer.render);const e=this;_e(this.renderer),this.renderer.render=function(t,n){const r=e.renderer.getRenderTarget();(r==null||"isXRRenderTarget"in r&&r.isXRRenderTarget)&&(s=0,$(e,H,v(e,H)+1),$(e,oe,v(e,Ae).getDelta()),$(e,me,v(e,me)+v(e,oe)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/v(e,oe)),$(e,Y,e._fpsBuffer.reduce((o,a)=>o+a)/e._fpsBuffer.length),G&&v(e,H)%200===0&&console.log("FPS",Math.round(v(e,Y)),"Interval:",v(e,P)));const i=s++;v(e,K).call(this,t,n),e.onAfterRender(t,n,i)}}disable(){v(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=v(this,K),$(this,K,void 0))}update(s,e){this.internalUpdate(s,e)}onAfterRender(s,e,t){if(this.pause)return;const n=this.renderer.renderLists.get(s,0).opaque;let r=!0;if(n.length===1){const i=n[0].material;(i.name==="EffectMaterial"||i.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||t>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(mt||(this.updateInterval==="auto"?v(this,Y)<40&&v(this,P)<10?($(this,P,v(this,P)+1),G&&console.warn("\u2193 Reducing LOD updates",v(this,P),v(this,Y).toFixed(0))):v(this,Y)>=60&&v(this,P)>1&&($(this,P,v(this,P)-1),G&&console.warn("\u2191 Increasing LOD updates",v(this,P),v(this,Y).toFixed(0))):$(this,P,this.updateInterval),v(this,P)>0&&v(this,H)%v(this,P)!=0))return;this.internalUpdate(s,e),this._postprocessPromiseGroups()}}internalUpdate(s,e){var t,n;const r=this.renderer.renderLists.get(s,0),i=r.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const o=this.targetTriangleDensity;for(const u of i){if(u.material&&(((t=u.geometry)==null?void 0:t.type)==="BoxGeometry"||((n=u.geometry)==null?void 0:n.type)==="BufferGeometry")&&(u.material.name==="SphericalGaussianBlur"||u.material.name=="BackgroundCubeMaterial"||u.material.name==="CubemapFromEquirect"||u.material.name==="EquirectangularToCubeUV")){G&&(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",u,u.material.name,u.material.type)));continue}switch(u.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&u.material&&!u.object.progressive_debug_color){u.object.progressive_debug_color=!0;const w=Math.random()*16777215,y=new Qe({color:w});u.object.material=y}const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(s,e,d,o)}const a=r.transparent;for(const u of a){const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(s,e,d,o)}const l=r.transmissive;for(const u of l){const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(s,e,d,o)}}updateLODs(s,e,t,n){var r,i;t.userData||(t.userData={});let o=t[Te];if(o||(o=new pt,t[Te]=o),o.frames++<2)return;for(const l of X)(r=l.onBeforeUpdateLOD)==null||r.call(l,this.renderer,s,e,t);const a=_.overrideGlobalLodLevel!==void 0?_.overrideGlobalLodLevel:Z;a>=0?(S.mesh_lod=a,S.texture_lod=a):(this.calculateLodLevel(e,t,o,n,S),S.mesh_lod=Math.round(S.mesh_lod),S.texture_lod=Math.round(S.texture_lod)),S.mesh_lod>=0&&this.loadProgressiveMeshes(t,S.mesh_lod),t.material&&S.texture_lod>=0&&this.loadProgressiveTextures(t.material,S.texture_lod),x&&t.material&&!t.isGizmo&&We(t.material);for(const l of X)(i=l.onAfterUpdatedLOD)==null||i.call(l,this.renderer,s,e,t,S);o.lastLodLevel_Mesh=S.mesh_lod,o.lastLodLevel_Texture=S.texture_lod}loadProgressiveTextures(s,e){if(!s)return;if(Array.isArray(s)){for(const r of s)this.loadProgressiveTextures(r,e);return}let t=!1;(s[q]===void 0||e<s[q])&&(t=!0);const n=s["DEBUG:LOD"];if(n!=null&&(t=s[q]!=n,e=n),t){s[q]=e;const r=z.assignTextureLOD(s,e).then(i=>{this._lodchangedlisteners.forEach(o=>o({type:"texture",level:e,object:s}))});fe.addPromise("texture",s,r,this._newPromiseGroups)}}loadProgressiveMeshes(s,e){if(!s)return Promise.resolve(null);let t=s[q]!==e;const n=s["DEBUG:LOD"];if(n!=null&&(t=s[q]!=n,e=n),t){s[q]=e;const r=s.geometry,i=z.assignMeshLOD(s,e).then(o=>(o&&s[q]==e&&r!=s.geometry&&this._lodchangedlisteners.forEach(a=>a({type:"mesh",level:e,object:s})),o));return fe.addPromise("mesh",s,i,this._newPromiseGroups),i}return Promise.resolve(null)}static isInside(s,e){const t=s.min,n=s.max,r=(t.x+n.x)*.5,i=(t.y+n.y)*.5;return this._tempPtInside.set(r,i,t.z).applyMatrix4(e).z<0}calculateLodLevel(s,e,t,n,r){var i,o,a;if(!e){r.mesh_lod=-1,r.texture_lod=-1;return}if(!s){r.mesh_lod=-1,r.texture_lod=-1;return}let l=10+1,u=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const d=(i=z.getMeshLODExtension(e.geometry))==null?void 0:i.lods,w=z.getPrimitiveIndex(e.geometry),y=d&&d.length>0,m=z.getMaterialMinMaxLODsCount(e.material),j=m?.min_count!=1/0&&m.min_count>0&&m.max_count>0;if(!y&&!j){r.mesh_lod=0,r.texture_lod=0;return}y||(u=!0,l=0);const M=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let C=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const g=e;if(!g.boundingBox)g.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0&&t.frames%this.skinnedMeshAutoUpdateBoundsInterval===0){const p=te(g),I=g.geometry;p&&(g.geometry=p),g.computeBoundingBox(),g.geometry=I}C=g.boundingBox}if(C){const g=s;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 f=s.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(f)){r.mesh_lod=0,r.texture_lod=0;return}}if(this._tempBox.copy(C),this._tempBox.applyMatrix4(e.matrixWorld),g.isPerspectiveCamera&&_.isInside(this._tempBox,this.projectionScreenMatrix)){r.mesh_lod=0,r.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&g.isPerspectiveCamera&&g.fov>70){const f=this._tempBox.min,h=this._tempBox.max;let L=f.x,O=f.y,T=h.x,V=h.y;const ae=2,xe=1.5,le=(f.x+h.x)*.5,ue=(f.y+h.y)*.5;L=(L-le)*ae+le,O=(O-ue)*ae+ue,T=(T-le)*ae+le,V=(V-ue)*ae+ue;const Xe=L<0&&T>0?0:Math.min(Math.abs(f.x),Math.abs(h.x)),qe=O<0&&V>0?0:Math.min(Math.abs(f.y),Math.abs(h.y)),ye=Math.max(Xe,qe);t.lastCentrality=(xe-ye)*(xe-ye)*(xe-ye)}else t.lastCentrality=1;const p=this._tempBox.getSize(this._tempBoxSize);p.multiplyScalar(.5),screen.availHeight>0&&M>0&&p.multiplyScalar(M/screen.availHeight),s.isPerspectiveCamera?p.x*=s.aspect:s.isOrthographicCamera;const I=s.matrixWorldInverse,R=this._tempBox2;R.copy(C),R.applyMatrix4(e.matrixWorld),R.applyMatrix4(I);const D=R.getSize(this._tempBox2Size),N=Math.max(D.x,D.y);if(Math.max(p.x,p.y)!=0&&N!=0&&(p.z=D.z/Math.max(D.x,D.y)*Math.max(p.x,p.y)),t.lastScreenCoverage=Math.max(p.x,p.y,p.z),t.lastScreenspaceVolume.copy(p),t.lastScreenCoverage*=t.lastCentrality,G&&_.debugDrawLine){const f=this.tempMatrix.copy(this.projectionScreenMatrix);f.invert();const h=_.corner0,L=_.corner1,O=_.corner2,T=_.corner3;h.copy(this._tempBox.min),L.copy(this._tempBox.max),L.x=h.x,O.copy(this._tempBox.max),O.y=h.y,T.copy(this._tempBox.max);const V=(h.z+T.z)*.5;h.z=L.z=O.z=T.z=V,h.applyMatrix4(f),L.applyMatrix4(f),O.applyMatrix4(f),T.applyMatrix4(f),_.debugDrawLine(h,L,255),_.debugDrawLine(h,O,255),_.debugDrawLine(L,T,255),_.debugDrawLine(O,T,255)}let b=999;if(d&&t.lastScreenCoverage>0)for(let f=0;f<d.length;f++){const h=d[f];if((((o=h.densities)==null?void 0:o[w])||h.density||1e-5)/t.lastScreenCoverage<n){b=f;break}}b<l&&(l=b,u=!0)}if(u?r.mesh_lod=l:r.mesh_lod=t.lastLodLevel_Mesh,G&&r.mesh_lod!=t.lastLodLevel_Mesh){const g=d?.[r.mesh_lod];g&&console.log(`Mesh LOD changed: ${t.lastLodLevel_Mesh} \u2192 ${r.mesh_lod} (${g.density.toFixed(0)}) - ${e.name}`)}if(j){const g="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(t.lastLodLevel_Texture<0){if(r.texture_lod=m.max_count-1,G){const p=m.lods[m.max_count-1];G&&console.log(`First Texture LOD ${r.texture_lod} (${p.max_height}px) - ${e.name}`)}}else{const p=t.lastScreenspaceVolume.x+t.lastScreenspaceVolume.y+t.lastScreenspaceVolume.z;let I=t.lastScreenCoverage*4;((a=this.context)==null?void 0:a.engine)==="model-viewer"&&(I*=1.5);const R=M/window.devicePixelRatio*I;let D=!1;for(let N=m.lods.length-1;N>=0;N--){const b=m.lods[N];if(!(g&&b.max_height>=2048)&&!(lt()&&b.max_height>4096)&&(b.max_height>R||!D&&N===0)){if(D=!0,r.texture_lod=N,r.texture_lod<t.lastLodLevel_Texture){const f=b.max_height;G&&console.log(`Texture LOD changed: ${t.lastLodLevel_Texture} \u2192 ${r.texture_lod} = ${f}px
6
- Screensize: ${R.toFixed(0)}px, Coverage: ${(100*t.lastScreenCoverage).toFixed(2)}%, Volume ${p.toFixed(1)}
7
- ${e.name}`)}break}}}}else r.texture_lod=0}},P=new WeakMap,K=new WeakMap,Ae=new WeakMap,H=new WeakMap,oe=new WeakMap,me=new WeakMap,Y=new WeakMap,c(_,"debugDrawLine"),c(_,"overrideGlobalLodLevel"),c(_,"corner0",new F),c(_,"corner1",new F),c(_,"corner2",new F),c(_,"corner3",new F),c(_,"_tempPtInside",new F),_);class pt{constructor(){c(this,"frames",0),c(this,"lastLodLevel_Mesh",-1),c(this,"lastLodLevel_Texture",-1),c(this,"lastScreenCoverage",0),c(this,"lastScreenspaceVolume",new F),c(this,"lastCentrality",0)}}const $e=Symbol("NEEDLE_mesh_lod"),pe=Symbol("NEEDLE_texture_lod");let ve=null;function Fe(){const s=vt();s&&(s.mapURLs(function(e){return Ue(),e}),Ue(),ve?.disconnect(),ve=new MutationObserver(e=>{e.forEach(t=>{t.addedNodes.forEach(n=>{n instanceof HTMLElement&&n.tagName.toLowerCase()==="model-viewer"&&Ve(n)})})}),ve.observe(document,{childList:!0,subtree:!0}))}function vt(){return typeof customElements>"u"?null:customElements.get("model-viewer")||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Fe()}),null)}function Ue(){typeof document>"u"||document.querySelectorAll("model-viewer").forEach(s=>{Ve(s)})}const ze=new WeakSet;let xt=0;function Ve(s){if(!s||ze.has(s))return null;ze.add(s),console.debug("[gltf-progressive] found new model-viewer..."+ ++xt+`
8
- `,s.getAttribute("src"));let e=null,t=null,n=null;for(let r=s;r!=null;r=Object.getPrototypeOf(r)){const i=Object.getOwnPropertySymbols(r),o=i.find(u=>u.toString()=="Symbol(renderer)"),a=i.find(u=>u.toString()=="Symbol(scene)"),l=i.find(u=>u.toString()=="Symbol(needsRender)");!e&&o!=null&&(e=s[o].threeRenderer),!t&&a!=null&&(t=s[a]),!n&&l!=null&&(n=s[l])}if(e&&t){let r=function(){if(n){let o=0,a=setInterval(()=>{if(o++>5){clearInterval(a);return}n?.call(s)},300)}};console.debug("[gltf-progressive] setup model-viewer");const i=ie.get(e,{engine:"model-viewer"});return ie.addPlugin(new yt),i.enable(),i.addEventListener("changed",()=>{n?.call(s)}),s.addEventListener("model-visibility",o=>{o.detail.visible&&n?.call(s)}),s.addEventListener("load",()=>{r()}),()=>{i.disable()}}return null}class yt{constructor(){c(this,"_didWarnAboutMissingUrl",!1)}onBeforeUpdateLOD(e,t,n,r){this.tryParseMeshLOD(t,r),this.tryParseTextureLOD(t,r)}getUrl(e){if(!e)return null;let t=e.getAttribute("src");return t||(t=e.src),t||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),t}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,t){if(t[pe]==!0)return;t[pe]=!0;const n=this.tryGetCurrentGLTF(e),r=this.tryGetCurrentModelViewer(e),i=this.getUrl(r);if(i&&n&&t.material){let o=function(l){var u,d,w;if(l[pe]==!0)return;l[pe]=!0,l.userData&&(l.userData.LOD=-1);const y=Object.keys(l);for(let m=0;m<y.length;m++){const j=y[m],M=l[j];if(M?.isTexture===!0){const C=(d=(u=M.userData)==null?void 0:u.associations)==null?void 0:d.textures;if(C==null)continue;const g=n.parser.json.textures[C];if(!g){console.warn("Texture data not found for texture index "+C);continue}if((w=g?.extensions)!=null&&w[U]){const p=g.extensions[U];p&&i&&z.registerTexture(i,M,p.lods.length,C,p)}}}};const a=t.material;if(Array.isArray(a))for(const l of a)o(l);else o(a)}}tryParseMeshLOD(e,t){var n,r;if(t[$e]==!0)return;t[$e]=!0;const i=this.tryGetCurrentModelViewer(e),o=this.getUrl(i);if(!o)return;const a=(r=(n=t.userData)==null?void 0:n.gltfExtensions)==null?void 0:r[U];if(a&&o){const l=t.uuid;z.registerMesh(o,l,t,0,a.lods.length,a)}}}function Lt(s,e,t,n){_e(e),Me(t),be(t,{progressive:!0,...n?.hints}),t.register(i=>new z(i,s));const r=ie.get(e);return n?.enableLODsManager!==!1&&r.enable(),r}if(Fe(),!dt){const s={gltfProgressive:{useNeedleProgressive:Lt,LODsManager:ie,configureLoader:be,getRaycastMesh:te,useRaycastMeshes:ht}};if(!globalThis.Needle)globalThis.Needle=s;else for(const e in s)globalThis.Needle[e]=s[e]}export{ie as LODsManager,z as NEEDLE_progressive,Me as addDracoAndKTX2Loaders,be as configureLoader,_e as createLoaders,te as getRaycastMesh,ke as setDracoDecoderLocation,je as setKTX2TranscoderLocation};
@@ -1,8 +0,0 @@
1
- "use strict";var qe=Object.defineProperty;var Ee=n=>{throw TypeError(n)};var Ke=(n,t,e)=>t in n?qe(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var c=(n,t,e)=>Ke(n,typeof t!="symbol"?t+"":t,e),Ge=(n,t,e)=>t.has(n)||Ee("Cannot "+e);var m=(n,t,e)=>(Ge(n,t,"read from private field"),e?e.call(n):t.get(n)),j=(n,t,e)=>t.has(n)?Ee("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(n):t.set(n,e),z=(n,t,e,s)=>(Ge(n,t,"write to private field"),s?s.call(n,e):t.set(n,e),e);const p=require("./three-B_hneGZr.umd.cjs"),te=require("./three-examples-DUcCNw9s.umd.cjs"),Ye="";globalThis.GLTF_PROGRESSIVE_VERSION=Ye;console.debug("[gltf-progressive] version -");let W="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",se="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const He=W,je=se,ke=new URL(W+"draco_decoder.js");ke.searchParams.append("range","true");fetch(ke,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(n=>{console.debug(`Failed to fetch remote Draco decoder from ${W} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),W===He&&$e("./include/draco/"),se===je&&Fe("./include/ktx2/")}).finally(()=>{Ue()});function $e(n){W=n,B&&B[be]!=W?(console.debug("Updating Draco decoder path to "+n),B[be]=W,B.setDecoderPath(W),B.preload()):console.debug("Setting Draco decoder path to "+n)}function Fe(n){se=n,U&&U.transcoderPath!=se?(console.debug("Updating KTX2 transcoder path to "+n),U.setTranscoderPath(se),U.init()):console.debug("Setting KTX2 transcoder path to "+n)}const be=Symbol("dracoDecoderPath");let B,ye,U;function Ue(){B||(B=new te.DRACOLoader,B[be]=W,B.setDecoderPath(W),B.setDecoderConfig({type:"js"}),B.preload()),U||(U=new te.KTX2Loader,U.setTranscoderPath(se),U.init()),ye||(ye=te.MeshoptDecoder)}function Te(n){return Ue(),n?U.detectSupport(n):n!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:B,ktx2Loader:U,meshoptDecoder:ye}}function Ae(n){n.dracoLoader||n.setDRACOLoader(B),n.ktx2Loader||n.setKTX2Loader(U),n.meshoptDecoder||n.setMeshoptDecoder(ye)}const Se=new WeakMap;function Ce(n,t){let e=Se.get(n);e?e=Object.assign(e,t):e=t,Se.set(n,e)}const Me=te.GLTFLoader.prototype.load;function Je(...n){const t=Se.get(this);let e=n[0];const s=new URL(e,window.location.href);if(s.hostname.endsWith("needle.tools")){const r=(t==null?void 0:t.progressive)!==void 0?t.progressive:!0,i=t!=null&&t.usecase?t.usecase:"default";r?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${i}`:this.requestHeader.Accept=`*/*;usecase=${i}`,e=s.toString()}return n[0]=e,Me==null?void 0:Me.call(this,...n)}te.GLTFLoader.prototype.load=Je;de("debugprogressive");function de(n){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(n);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function Qe(n,t){if(t===void 0||t.startsWith("./")||t.startsWith("http")||n===void 0)return t;const e=n.lastIndexOf("/");if(e>=0){const s=n.substring(0,e+1);for(;s.endsWith("/")&&t.startsWith("/");)t=t.substring(1);return s+t}return t}let ne;function Ze(){return ne!==void 0||(ne=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),de("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",ne)),ne}function et(){if(typeof window>"u")return!1;const n=new URL(window.location.href),t=n.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(n.hostname);return n.hostname==="127.0.0.1"||t}const tt=typeof window>"u"&&typeof document>"u",Pe=Symbol("needle:raycast-mesh");function ie(n){return(n==null?void 0:n[Pe])instanceof p.BufferGeometry?n[Pe]:null}function st(n,t){if((n.type==="Mesh"||n.type==="SkinnedMesh")&&!ie(n)){const s=it(t);s.userData={isRaycastMesh:!0},n[Pe]=s}}function rt(n=!0){if(n){if(ae)return;const t=ae=p.Mesh.prototype.raycast;p.Mesh.prototype.raycast=function(e,s){const o=this,r=ie(o);let i;r&&o.isMesh&&(i=o.geometry,o.geometry=r),t.call(this,e,s),i&&(o.geometry=i)}}else{if(!ae)return;p.Mesh.prototype.raycast=ae,ae=null}}let ae=null;function it(n){const t=new p.BufferGeometry;for(const e in n.attributes)t.setAttribute(e,n.getAttribute(e));return t.setIndex(n.getIndex()),t}const Q=new Array,L=de("debugprogressive");let pe,ee=-1;if(L){let n=function(){ee+=1,ee>=t&&(ee=-1),console.log(`Toggle LOD level [${ee}]`)},t=6;window.addEventListener("keyup",e=>{e.key==="p"&&n(),e.key==="w"&&(pe=!pe,console.log(`Toggle wireframe [${pe}]`));const s=parseInt(e.key);!isNaN(s)&&s>=0&&(ee=s,console.log(`Set LOD level to [${ee}]`))})}function Ne(n){if(L)if(Array.isArray(n))for(const t of n)Ne(t);else n&&"wireframe"in n&&(n.wireframe=pe===!0)}const K="NEEDLE_progressive",De=Symbol("needle-progressive-texture"),_=class _{constructor(t,e){c(this,"parser");c(this,"url");c(this,"_isLoadingMesh");c(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(r=>{var i;return this._isLoadingMesh=!1,r&&_.registerMesh(this.url,e.guid,r,(i=e.lods)==null?void 0:i.length,0,e),r})):null});L&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}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",r=t[o];if(r!=null)return r;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(L==="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&&i(u,e)}}else if(t.isMaterial)for(const a of Object.keys(t)){const l=t[a];(l==null?void 0:l.isTexture)===!0&&i(l,e)}return t[o]=e,e;function i(a,l){const u=s.getAssignedLODInformation(a);if(u){const d=s.lodInfos.get(u.key);if(d&&d.lods){l.min_count=Math.min(l.min_count,d.lods.length),l.max_count=Math.max(l.max_count,d.lods.length);for(let h=0;h<d.lods.length;h++){const g=d.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))}}}}}static hasLODLevelAvailable(t,e){var r;if(Array.isArray(t)){for(const i of t)if(this.hasLODLevelAvailable(i,e))return!0;return!1}if(t.isMaterial===!0){for(const i of Object.keys(t)){const a=t[i];if(a&&a.isTexture&&this.hasLODLevelAvailable(a,e))return!0}return!1}else if(t.isGroup===!0){for(const i of t.children)if(i.isMesh===!0&&this.hasLODLevelAvailable(i,e))return!0}let s,o;if(t.isMesh?s=t.geometry:(t.isBufferGeometry||t.isTexture)&&(s=t),s&&(r=s==null?void 0:s.userData)!=null&&r.LODS){const i=s.userData.LODS;if(o=this.lodInfos.get(i.key),e===void 0)return o!=null;if(o)return Array.isArray(o.lods)?e<o.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof p.Mesh||t.isMesh===!0){const o=t.geometry,r=this.getAssignedLODInformation(o);if(!r)return Promise.resolve(null);for(const i of Q)(s=i.onBeforeGetLODMesh)==null||s.call(i,t,e);return t["LOD:requested level"]=e,_.getOrLoadLOD(o,e).then(i=>{if(Array.isArray(i)){const a=r.index||0;i=i[a]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],i&&o!=i&&((i==null?void 0:i.isBufferGeometry)?t.geometry=i:L&&console.error("Invalid LOD geometry",i))),i}).catch(i=>(console.error("Error loading mesh LOD",t,i),null))}else L&&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 r of s.material){const i=this.assignTextureLOD(r,e);o.push(i)}return Promise.all(o).then(r=>{const i=new Array;for(const a of r)Array.isArray(a)&&i.push(...a);return i})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,o=[],r=new Array;if(s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const i=s;for(const a of Object.keys(i.uniforms)){const l=i.uniforms[a].value;if((l==null?void 0:l.isTexture)===!0){const u=this.assignTextureLODForSlot(l,e,s,a).then(d=>(d&&i.uniforms[a].value!=d&&(i.uniforms[a].value=d,i.uniformsNeedUpdate=!0),d));o.push(u),r.push(a)}}}else for(const i of Object.keys(s)){const a=s[i];if((a==null?void 0:a.isTexture)===!0){const l=this.assignTextureLODForSlot(a,e,s,i);o.push(l),r.push(i)}}return Promise.all(o).then(i=>{const a=new Array;for(let l=0;l<i.length;l++){const u=i[l],d=r[l];u&&u.isTexture===!0?a.push({material:s,slot:d,texture:u,level:e}):a.push({material:s,slot:d,texture:null,level:e})}return a})}if(t instanceof p.Texture||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):_.getOrLoadLOD(t,e).then(r=>{if(Array.isArray(r))return null;if((r==null?void 0:r.isTexture)===!0){if(r!=t&&s&&o){const i=s[o];if(i&&!L){const a=this.getAssignedLODInformation(i);if(a&&(a==null?void 0:a.level)<e)return L==="verbose"&&console.warn("Assigned texture level is already higher: ",a.level,e,s,i,r),null}s[o]=r}return r}else L=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(r=>(console.error("Error loading LOD",t,r),null))}afterRoot(t){var e,s;return L&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((o,r)=>{var i;if(o!=null&&o.extensions){const a=o==null?void 0:o.extensions[K];if(a){if(!a.lods){L&&console.warn("Texture has no LODs",a);return}let l=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const d=this.parser.associations.get(u);(d==null?void 0:d.textures)===r&&(l=!0,_.registerTexture(this.url,u,(i=a.lods)==null?void 0:i.length,r,a))}l||this.parser.getDependency("texture",r).then(u=>{var d;u&&_.registerTexture(this.url,u,(d=a.lods)==null?void 0:d.length,r,a)})}}}),(s=this.parser.json.meshes)==null||s.forEach((o,r)=>{if(o!=null&&o.extensions){const i=o==null?void 0:o.extensions[K];if(i&&i.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)===r&&_.registerMesh(this.url,i.guid,a,i.lods.length,l.primitives,i)}}}}),null}static async getOrLoadLOD(t,e){var a,l,u,d;const s=L=="verbose",o=t.userData.LODS;if(!o)return null;const r=o==null?void 0:o.key;let i;if(t.isTexture===!0){const h=t;h.source&&h.source[De]&&(i=h.source[De])}if(i||(i=_.lodInfos.get(r)),i){if(e>0){let D=!1;const S=Array.isArray(i.lods);if(S&&e>=i.lods.length?D=!0:S||(D=!0),D)return this.lowresCache.get(r)}const h=Array.isArray(i.lods)?(a=i.lods[e])==null?void 0:a.path:i.lods;if(!h)return L&&!i["missing:uri"]&&(i["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,i)),null;const g=Qe(o.url,h);if(g.endsWith(".glb")||g.endsWith(".gltf")){if(!i.guid)return console.warn("missing pointer for glb/gltf texture",i),null;const D=g+"_"+i.guid,S=this.previouslyLoaded.get(D);if(S!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${D}`);let v=await S.catch(y=>(console.error(`Error loading LOD ${e} from ${g}
2
- `,y),null)),b=!1;if(v==null||(v instanceof p.Texture&&t instanceof p.Texture?(l=v.image)!=null&&l.data||(u=v.source)!=null&&u.data?v=this.copySettings(t,v):(b=!0,this.previouslyLoaded.delete(D)):v instanceof p.BufferGeometry&&t instanceof p.BufferGeometry&&((d=v.attributes.position)!=null&&d.array||(b=!0,this.previouslyLoaded.delete(D)))),!b)return v}const M=i,N=new Promise(async(v,b)=>{const y=new te.GLTFLoader;Ae(y),L&&(await new Promise(P=>setTimeout(P,1e3)),s&&console.warn("Start loading (delayed) "+g,M.guid));let q=g;if(M&&Array.isArray(M.lods)){const P=M.lods[e];P.hash&&(q+="?v="+P.hash)}const A=await y.loadAsync(q).catch(P=>(console.error(`Error loading LOD ${e} from ${g}
3
- `,P),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 P=!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){P=!0;break}}I++}if(P){let f=await R.getDependency("texture",I);return f&&_.assignLODInformation(o.url,f,r,e,void 0),s&&console.log('change "'+t.name+'" → "'+f.name+'"',g,I,f,D),t instanceof p.Texture&&(f=this.copySettings(t,f)),f&&(f.guid=M.guid),v(f)}else L&&console.warn("Could not find texture with guid",M.guid,A.parser.json)}if(I=0,A.parser.json.meshes){let P=!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){P=!0;break}}I++}if(P){const f=await R.getDependency("mesh",I);if(s&&console.log(`Loaded Mesh "${f.name}"`,g,I,f,D),f.isMesh===!0){const T=f.geometry;return _.assignLODInformation(o.url,T,r,e,0),v(T)}else{const T=new Array;for(let w=0;w<f.children.length;w++){const O=f.children[w];if(O.isMesh===!0){const C=O.geometry;_.assignLODInformation(o.url,C,r,e,w),T.push(C)}}return v(T)}}else L&&console.warn("Could not find mesh with guid",M.guid,A.parser.json)}return v(null)});return this.previouslyLoaded.set(D,N),await N}else if(t instanceof p.Texture){s&&console.log("Load texture from uri: "+g);const S=await new p.TextureLoader().loadAsync(g);return S?(S.guid=i.guid,S.flipY=!1,S.needsUpdate=!0,S.colorSpace=t.colorSpace,s&&console.log(i,S)):L&&console.warn("failed loading",g),S}}else L&&console.warn(`Can not load LOD ${e}: no LOD info found for "${r}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,s,o,r){if(!e)return;e.userData||(e.userData={});const i=new ot(t,s,o,r);e.userData.LODS=i}static getAssignedLODInformation(t){var e;return((e=t==null?void 0:t.userData)==null?void 0:e.LODS)||null}static copySettings(t,e){return e?(L&&console.warn(`Copy texture settings
4
- `,t.uuid,`
5
- `,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}};c(_,"registerTexture",(t,e,s,o,r)=>{if(L&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,r),!e){L&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[De]=r);const i=r.guid;_.assignLODInformation(t,e,i,s,o),_.lodInfos.set(i,r),_.lowresCache.set(i,e)}),c(_,"registerMesh",(t,e,s,o,r,i)=>{var u;const a=s.geometry;if(!a){L&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),L&&console.log("> Progressive: register mesh "+s.name,{index:r,uuid:s.uuid},i,s),_.assignLODInformation(t,a,e,o,r),_.lodInfos.set(e,i);let l=_.lowresCache.get(e);l?l.push(s.geometry):l=[s.geometry],_.lowresCache.set(e,l),o>0&&!ie(s)&&st(s,a);for(const d of Q)(u=d.onRegisteredNewMesh)==null||u.call(d,s,i)}),c(_,"lodInfos",new Map),c(_,"previouslyLoaded",new Map),c(_,"lowresCache",new Map);let F=_;class ot{constructor(t,e,s,o){c(this,"url");c(this,"key");c(this,"level");c(this,"index");this.url=t,this.key=e,this.level=s,o!=null&&(this.index=o)}}class me{constructor(t,e){c(this,"frame_start");c(this,"frame_capture_end");c(this,"ready");c(this,"_resolve");c(this,"_signal");c(this,"_resolved",!1);c(this,"_addedCount",0);c(this,"_resolvedCount",0);c(this,"_awaiting",[]);c(this,"_maxPromisesPerObject",1);c(this,"_currentFrame",0);c(this,"_seen",new WeakMap);var r;const o=Math.max(e.frames??2,2);this.frame_start=t,this.frame_capture_end=t+o,this.ready=new Promise(i=>{this._resolve=i}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=e.signal,(r=this._signal)==null||r.addEventListener("abort",()=>{this.resolveNow()}),this._maxPromisesPerObject=Math.max(1,e.maxPromisesPerObject??1)}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(t){var e;this._currentFrame=t,((e=this._signal)!=null&&e.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(t,e,s){if(this._resolved){L&&console.warn("PromiseGroup: Trying to add a promise to a resolved group, ignoring.");return}if(!(this._currentFrame>this.frame_capture_end)){if(this._maxPromisesPerObject>=1)if(this._seen.has(e)){let o=this._seen.get(e);if(o>=this._maxPromisesPerObject){L&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(e,o+1)}else this._seen.set(e,1);this._awaiting.push(s),this._addedCount++,s.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(s),1)})}}resolveNow(){var t,e;this._resolved||(e=this._resolve)==null||e.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((t=this._signal)==null?void 0:t.aborted)??!1})}}c(me,"addPromise",(t,e,s,o)=>{o.forEach(r=>{r.add(t,e,s)})});const $=de("debugprogressive"),nt=de("noprogressive"),ve=Symbol("Needle:LODSManager"),Oe=Symbol("Needle:LODState"),J=Symbol("Needle:CurrentLOD"),E={mesh_lod:-1,texture_lod:-1};var x,G,Y,Le,Z,re,xe,H;let ce=(x=class{constructor(t,e){c(this,"renderer");c(this,"context");c(this,"projectionScreenMatrix",new p.Matrix4);c(this,"targetTriangleDensity",2e5);c(this,"skinnedMeshAutoUpdateBoundsInterval",30);c(this,"updateInterval","auto");j(this,G,1);c(this,"pause",!1);c(this,"manual",!1);c(this,"_newPromiseGroups",[]);c(this,"_promiseGroupIds",0);c(this,"_lodchangedlisteners",[]);j(this,Y);j(this,Le,new p.Clock);j(this,Z,0);j(this,re,0);j(this,xe,0);j(this,H,0);c(this,"_fpsBuffer",[60,60,60,60,60]);c(this,"_sphere",new p.Sphere);c(this,"_tempBox",new p.Box3);c(this,"_tempBox2",new p.Box3);c(this,"tempMatrix",new p.Matrix4);c(this,"_tempWorldPosition",new p.Vector3);c(this,"_tempBoxSize",new p.Vector3);c(this,"_tempBox2Size",new p.Vector3);this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Oe]}static addPlugin(t){Q.push(t)}static removePlugin(t){const e=Q.indexOf(t);e>=0&&Q.splice(e,1)}static get(t,e){if(t[ve])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ve];const s=new x(t,{engine:"unknown",...e});return t[ve]=s,s}get plugins(){return Q}awaitLoading(t){const e=this._promiseGroupIds++,s=new me(m(this,Z),{...t});this._newPromiseGroups.push(s);const o=performance.now();return s.ready.finally(()=>{const r=this._newPromiseGroups.indexOf(s);r>=0&&(this._newPromiseGroups.splice(r,1),et()&&performance.measure("LODsManager:awaitLoading",{start:o,detail:{id:e,name:t==null?void 0:t.name,awaited:s.awaitedCount,resolved:s.resolvedCount}}))}),s.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let t=this._newPromiseGroups.length-1;t>=0;t--)this._newPromiseGroups[t].update(m(this,Z))}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(){if(m(this,Y))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;z(this,Y,this.renderer.render);const e=this;Te(this.renderer),this.renderer.render=function(s,o){const r=e.renderer.getRenderTarget();(r==null||"isXRRenderTarget"in r&&r.isXRRenderTarget)&&(t=0,z(e,Z,m(e,Z)+1),z(e,re,m(e,Le).getDelta()),z(e,xe,m(e,xe)+m(e,re)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/m(e,re)),z(e,H,e._fpsBuffer.reduce((a,l)=>a+l)/e._fpsBuffer.length),$&&m(e,Z)%200===0&&console.log("FPS",Math.round(m(e,H)),"Interval:",m(e,G)));const i=t++;m(e,Y).call(this,s,o),e.onAfterRender(s,o,i)}}disable(){m(this,Y)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=m(this,Y),z(this,Y,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,s){if(this.pause)return;const r=this.renderer.renderLists.get(t,0).opaque;let i=!0;if(r.length===1){const a=r[0].material;(a.name==="EffectMaterial"||a.name==="CopyShader")&&(i=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(i=!1),i){if(nt||(this.updateInterval==="auto"?m(this,H)<40&&m(this,G)<10?(z(this,G,m(this,G)+1),$&&console.warn("↓ Reducing LOD updates",m(this,G),m(this,H).toFixed(0))):m(this,H)>=60&&m(this,G)>1&&(z(this,G,m(this,G)-1),$&&console.warn("↑ Increasing LOD updates",m(this,G),m(this,H).toFixed(0))):z(this,G,this.updateInterval),m(this,G)>0&&m(this,Z)%m(this,G)!=0))return;this.internalUpdate(t,e),this._postprocessPromiseGroups()}}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 r=this.targetTriangleDensity;for(const d of o){if(d.material&&(((l=d.geometry)==null?void 0:l.type)==="BoxGeometry"||((u=d.geometry)==null?void 0:u.type)==="BufferGeometry")&&(d.material.name==="SphericalGaussianBlur"||d.material.name=="BackgroundCubeMaterial"||d.material.name==="CubemapFromEquirect"||d.material.name==="EquirectangularToCubeUV")){$&&(d.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(d.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",d,d.material.name,d.material.type)));continue}switch(d.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if($==="color"&&d.material&&!d.object.progressive_debug_color){d.object.progressive_debug_color=!0;const g=Math.random()*16777215,D=new p.MeshStandardMaterial({color:g});d.object.material=D}const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,r)}const i=s.transparent;for(const d of i){const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,r)}const a=s.transmissive;for(const d of a){const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,r)}}updateLODs(t,e,s,o){var a,l;s.userData||(s.userData={});let r=s[Oe];if(r||(r=new at,s[Oe]=r),r.frames++<2)return;for(const u of Q)(a=u.onBeforeUpdateLOD)==null||a.call(u,this.renderer,t,e,s);const i=x.overrideGlobalLodLevel!==void 0?x.overrideGlobalLodLevel:ee;i>=0?(E.mesh_lod=i,E.texture_lod=i):(this.calculateLodLevel(e,s,r,o,E),E.mesh_lod=Math.round(E.mesh_lod),E.texture_lod=Math.round(E.texture_lod)),E.mesh_lod>=0&&this.loadProgressiveMeshes(s,E.mesh_lod),s.material&&E.texture_lod>=0&&this.loadProgressiveTextures(s.material,E.texture_lod),L&&s.material&&!s.isGizmo&&Ne(s.material);for(const u of Q)(l=u.onAfterUpdatedLOD)==null||l.call(u,this.renderer,t,e,s,E);r.lastLodLevel_Mesh=E.mesh_lod,r.lastLodLevel_Texture=E.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const r of t)this.loadProgressiveTextures(r,e);return}let s=!1;(t[J]===void 0||e<t[J])&&(s=!0);const o=t["DEBUG:LOD"];if(o!=null&&(s=t[J]!=o,e=o),s){t[J]=e;const r=F.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(a=>a({type:"texture",level:e,object:t}))});me.addPromise("texture",t,r,this._newPromiseGroups)}}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 r=t.geometry,i=F.assignMeshLOD(t,e).then(a=>(a&&t[J]==e&&r!=t.geometry&&this._lodchangedlisteners.forEach(l=>l({type:"mesh",level:e,object:t})),a));return me.addPromise("mesh",t,i,this._newPromiseGroups),i}return Promise.resolve(null)}static isInside(t,e){const s=t.min,o=t.max,r=(s.x+o.x)*.5,i=(s.y+o.y)*.5;return this._tempPtInside.set(r,i,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,o,r){var N,X,v;if(!e){r.mesh_lod=-1,r.texture_lod=-1;return}if(!t){r.mesh_lod=-1,r.texture_lod=-1;return}let a=10+1,l=!1;if($&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const u=(N=F.getMeshLODExtension(e.geometry))==null?void 0:N.lods,d=F.getPrimitiveIndex(e.geometry),h=u&&u.length>0,g=F.getMaterialMinMaxLODsCount(e.material),D=(g==null?void 0:g.min_count)!=1/0&&g.min_count>0&&g.max_count>0;if(!h&&!D){r.mesh_lod=0,r.texture_lod=0;return}h||(l=!0,a=0);const S=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let M=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const b=e;if(!b.boundingBox)b.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0&&s.frames%this.skinnedMeshAutoUpdateBoundsInterval===0){const y=ie(b),q=b.geometry;y&&(b.geometry=y),b.computeBoundingBox(),b.geometry=q}M=b.boundingBox}if(M){const b=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 w=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(w)){r.mesh_lod=0,r.texture_lod=0;return}}if(this._tempBox.copy(M),this._tempBox.applyMatrix4(e.matrixWorld),b.isPerspectiveCamera&&x.isInside(this._tempBox,this.projectionScreenMatrix)){r.mesh_lod=0,r.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&b.isPerspectiveCamera&&b.fov>70){const w=this._tempBox.min,O=this._tempBox.max;let C=w.x,k=w.y,V=O.x,oe=O.y;const ue=2,we=1.5,fe=(w.x+O.x)*.5,he=(w.y+O.y)*.5;C=(C-fe)*ue+fe,k=(k-he)*ue+he,V=(V-fe)*ue+fe,oe=(oe-he)*ue+he;const We=C<0&&V>0?0:Math.min(Math.abs(w.x),Math.abs(O.x)),Xe=k<0&&oe>0?0:Math.min(Math.abs(w.y),Math.abs(O.y)),_e=Math.max(We,Xe);s.lastCentrality=(we-_e)*(we-_e)*(we-_e)}else s.lastCentrality=1;const y=this._tempBox.getSize(this._tempBoxSize);y.multiplyScalar(.5),screen.availHeight>0&&S>0&&y.multiplyScalar(S/screen.availHeight),t.isPerspectiveCamera?y.x*=t.aspect:t.isOrthographicCamera;const q=t.matrixWorldInverse,A=this._tempBox2;A.copy(M),A.applyMatrix4(e.matrixWorld),A.applyMatrix4(q);const R=A.getSize(this._tempBox2Size),I=Math.max(R.x,R.y);if(Math.max(y.x,y.y)!=0&&I!=0&&(y.z=R.z/Math.max(R.x,R.y)*Math.max(y.x,y.y)),s.lastScreenCoverage=Math.max(y.x,y.y,y.z),s.lastScreenspaceVolume.copy(y),s.lastScreenCoverage*=s.lastCentrality,$&&x.debugDrawLine){const w=this.tempMatrix.copy(this.projectionScreenMatrix);w.invert();const O=x.corner0,C=x.corner1,k=x.corner2,V=x.corner3;O.copy(this._tempBox.min),C.copy(this._tempBox.max),C.x=O.x,k.copy(this._tempBox.max),k.y=O.y,V.copy(this._tempBox.max);const oe=(O.z+V.z)*.5;O.z=C.z=k.z=V.z=oe,O.applyMatrix4(w),C.applyMatrix4(w),k.applyMatrix4(w),V.applyMatrix4(w),x.debugDrawLine(O,C,255),x.debugDrawLine(O,k,255),x.debugDrawLine(C,V,255),x.debugDrawLine(k,V,255)}let f=999;if(u&&s.lastScreenCoverage>0)for(let w=0;w<u.length;w++){const O=u[w];if((((X=O.densities)==null?void 0:X[d])||O.density||1e-5)/s.lastScreenCoverage<o){f=w;break}}f<a&&(a=f,l=!0)}if(l?r.mesh_lod=a:r.mesh_lod=s.lastLodLevel_Mesh,$&&r.mesh_lod!=s.lastLodLevel_Mesh){const y=u==null?void 0:u[r.mesh_lod];y&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${r.mesh_lod} (${y.density.toFixed(0)}) - ${e.name}`)}if(D){const b="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(r.texture_lod=g.max_count-1,$){const y=g.lods[g.max_count-1];$&&console.log(`First Texture LOD ${r.texture_lod} (${y.max_height}px) - ${e.name}`)}}else{const y=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let q=s.lastScreenCoverage*4;((v=this.context)==null?void 0:v.engine)==="model-viewer"&&(q*=1.5);const R=S/window.devicePixelRatio*q;let I=!1;for(let P=g.lods.length-1;P>=0;P--){const f=g.lods[P];if(!(b&&f.max_height>=2048)&&!(Ze()&&f.max_height>4096)&&(f.max_height>R||!I&&P===0)){if(I=!0,r.texture_lod=P,r.texture_lod<s.lastLodLevel_Texture){const T=f.max_height;$&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${r.texture_lod} = ${T}px
6
- Screensize: ${R.toFixed(0)}px, Coverage: ${(100*s.lastScreenCoverage).toFixed(2)}%, Volume ${y.toFixed(1)}
7
- ${e.name}`)}break}}}}else r.texture_lod=0}},G=new WeakMap,Y=new WeakMap,Le=new WeakMap,Z=new WeakMap,re=new WeakMap,xe=new WeakMap,H=new WeakMap,c(x,"debugDrawLine"),c(x,"overrideGlobalLodLevel"),c(x,"corner0",new p.Vector3),c(x,"corner1",new p.Vector3),c(x,"corner2",new p.Vector3),c(x,"corner3",new p.Vector3),c(x,"_tempPtInside",new p.Vector3),x);class at{constructor(){c(this,"frames",0);c(this,"lastLodLevel_Mesh",-1);c(this,"lastLodLevel_Texture",-1);c(this,"lastScreenCoverage",0);c(this,"lastScreenspaceVolume",new p.Vector3);c(this,"lastCentrality",0)}}const Be=Symbol("NEEDLE_mesh_lod"),ge=Symbol("NEEDLE_texture_lod");let le=null;function Ve(){const n=lt();n&&(n.mapURLs(function(t){return Ie(),t}),Ie(),le==null||le.disconnect(),le=new MutationObserver(t=>{t.forEach(e=>{e.addedNodes.forEach(s=>{s instanceof HTMLElement&&s.tagName.toLowerCase()==="model-viewer"&&ze(s)})})}),le.observe(document,{childList:!0,subtree:!0}))}function lt(){if(typeof customElements>"u")return null;const n=customElements.get("model-viewer");return n||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Ve()}),null)}function Ie(){if(typeof document>"u")return;document.querySelectorAll("model-viewer").forEach(t=>{ze(t)})}const Re=new WeakSet;let ct=0;function ze(n){if(!n||Re.has(n))return null;Re.add(n),console.debug("[gltf-progressive] found new model-viewer..."+ ++ct+`
8
- `,n.getAttribute("src"));let t=null,e=null,s=null;for(let o=n;o!=null;o=Object.getPrototypeOf(o)){const r=Object.getOwnPropertySymbols(o),i=r.find(u=>u.toString()=="Symbol(renderer)"),a=r.find(u=>u.toString()=="Symbol(scene)"),l=r.find(u=>u.toString()=="Symbol(needsRender)");!t&&i!=null&&(t=n[i].threeRenderer),!e&&a!=null&&(e=n[a]),!s&&l!=null&&(s=n[l])}if(t&&e){let o=function(){if(s){let i=0,a=setInterval(()=>{if(i++>5){clearInterval(a);return}s==null||s.call(n)},300)}};console.debug("[gltf-progressive] setup model-viewer");const r=ce.get(t,{engine:"model-viewer"});return ce.addPlugin(new dt),r.enable(),r.addEventListener("changed",()=>{s==null||s.call(n)}),n.addEventListener("model-visibility",i=>{i.detail.visible&&(s==null||s.call(n))}),n.addEventListener("load",()=>{o()}),()=>{r.disable()}}return null}class dt{constructor(){c(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),r=this.getUrl(o);if(r&&s&&e.material){let i=function(l){var d,h,g;if(l[ge]==!0)return;l[ge]=!0,l.userData&&(l.userData.LOD=-1);const u=Object.keys(l);for(let D=0;D<u.length;D++){const S=u[D],M=l[S];if((M==null?void 0:M.isTexture)===!0){const N=(h=(d=M.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(N==null)continue;const X=s.parser.json.textures[N];if(!X){console.warn("Texture data not found for texture index "+N);continue}if((g=X==null?void 0:X.extensions)!=null&&g[K]){const v=X.extensions[K];v&&r&&F.registerTexture(r,M,v.lods.length,N,v)}}}};const a=e.material;if(Array.isArray(a))for(const l of a)i(l);else i(a)}}tryParseMeshLOD(t,e){var i,a;if(e[Be]==!0)return;e[Be]=!0;const s=this.tryGetCurrentModelViewer(t),o=this.getUrl(s);if(!o)return;const r=(a=(i=e.userData)==null?void 0:i.gltfExtensions)==null?void 0:a[K];if(r&&o){const l=e.uuid;F.registerMesh(o,l,e,0,r.lods.length,r)}}}function ut(n,t,e,s){Te(t),Ae(e),Ce(e,{progressive:!0,...s==null?void 0:s.hints}),e.register(r=>new F(r,n));const o=ce.get(t);return(s==null?void 0:s.enableLODsManager)!==!1&&o.enable(),o}Ve();if(!tt){const n={gltfProgressive:{useNeedleProgressive:ut,LODsManager:ce,configureLoader:Ce,getRaycastMesh:ie,useRaycastMeshes:rt}};if(!globalThis.Needle)globalThis.Needle=n;else for(const t in n)globalThis.Needle[t]=n[t]}exports.LODsManager=ce;exports.NEEDLE_progressive=F;exports.addDracoAndKTX2Loaders=Ae;exports.configureLoader=Ce;exports.createLoaders=Te;exports.getRaycastMesh=ie;exports.setDracoDecoderLocation=$e;exports.setKTX2TranscoderLocation=Fe;