@needle-tools/gltf-progressive 3.1.0 → 3.1.1-next.50e45f8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +79 -39
- package/gltf-progressive.js +916 -720
- package/gltf-progressive.min.js +7 -7
- package/gltf-progressive.umd.cjs +8 -8
- package/lib/extension.d.ts +3 -0
- package/lib/extension.js +96 -14
- package/lib/loaders.d.ts +8 -0
- package/lib/loaders.js +33 -22
- package/lib/lods.manager.js +24 -11
- package/lib/utils.internal.d.ts +4 -5
- package/lib/utils.internal.js +2 -2
- package/lib/version.js +1 -1
- package/lib/worker/loader.mainthread.d.ts +45 -0
- package/lib/worker/loader.mainthread.js +193 -0
- package/package.json +2 -2
package/gltf-progressive.min.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var Qe=Object.defineProperty,Ye=(t,e,s)=>e in t?Qe(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,u=(t,e,s)=>(Ye(t,typeof e!="symbol"?e+"":e,s),s),Ee=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},y=(t,e,s)=>(Ee(t,e,"read from private field"),s?s.call(t):e.get(t)),Y=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},U=(t,e,s,n)=>(Ee(t,e,"write to private field"),n?n.call(t,s):e.set(t,s),s);import{BufferGeometry as ue,Mesh as Z,Texture as se,TextureLoader as Ze,Matrix4 as Ce,Clock as Je,MeshStandardMaterial as et,Sphere as tt,Box3 as ke,Vector3 as W}from"three";import{GLTFLoader as xe}from"three/examples/jsm/loaders/GLTFLoader.js";import{MeshoptDecoder as st}from"three/examples/jsm/libs/meshopt_decoder.module.js";import{DRACOLoader as rt}from"three/examples/jsm/loaders/DRACOLoader.js";import{KTX2Loader as nt}from"three/examples/jsm/loaders/KTX2Loader.js";const Be="";globalThis.GLTF_PROGRESSIVE_VERSION=Be,console.debug("[gltf-progressive] version -");let N="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",ee="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const ot=N,it=ee,Ie=new URL(N+"draco_decoder.js");Ie.searchParams.append("range","true"),fetch(Ie,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${N} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),N===ot&&Ge("./include/draco/"),ee===it&&Re("./include/ktx2/")}).finally(()=>{$e()});function Ge(t){N=t,I&&I[_e]!=N?(console.debug("Updating Draco decoder path to "+t),I[_e]=N,I.setDecoderPath(N),I.preload()):console.debug("Setting Draco decoder path to "+t)}function Re(t){ee=t,R&&R.transcoderPath!=ee?(console.debug("Updating KTX2 transcoder path to "+t),R.setTranscoderPath(ee),R.init()):console.debug("Setting KTX2 transcoder path to "+t)}const _e=Symbol("dracoDecoderPath");let I,de,R;function $e(){I||(I=new rt,I[_e]=N,I.setDecoderPath(N),I.setDecoderConfig({type:"js"}),I.preload()),R||(R=new nt,R.setTranscoderPath(ee),R.init()),de||(de=st)}function Le(t){return $e(),t?R.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:I,ktx2Loader:R,meshoptDecoder:de}}function we(t){t.dracoLoader||t.setDRACOLoader(I),t.ktx2Loader||t.setKTX2Loader(R),t.meshoptDecoder||t.setMeshoptDecoder(de)}const Me=new WeakMap;function be(t,e){let s=Me.get(t);s?s=Object.assign(s,e):s=e,Me.set(t,s)}const je=xe.prototype.load;function at(...t){const e=Me.get(this);let s=t[0];const n=new URL(s,window.location.href);if(n.hostname.endsWith("needle.tools")){const r=e?.progressive!==void 0?e.progressive:!0,o=e!=null&&e.usecase?e.usecase:"default";r?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${o}`:this.requestHeader.Accept=`*/*;usecase=${o}`,s=n.toString()}return t[0]=s,je?.call(this,...t)}xe.prototype.load=at,re("debugprogressive");function re(t){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(t);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function lt(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const s=t.lastIndexOf("/");if(s>=0){const n=t.substring(0,s+1);for(;n.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return n+e}return e}let ce;function ut(){return ce!==void 0||(ce=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),re("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",ce)),ce}function dt(){if(typeof window>"u")return!1;const t=new URL(window.location.href),e=t.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(t.hostname);return t.hostname==="127.0.0.1"||e}class ct{constructor(e=100,s={}){u(this,"_running",new Map),u(this,"_queue",[]),u(this,"debug",!1),u(this,"tick",()=>{this.internalUpdate(),setTimeout(this.tick,10)}),this.maxConcurrent=e,this.debug=s.debug??!1,window.requestAnimationFrame(this.tick)}slot(e){return this.debug&&console.debug(`[PromiseQueue]: Requesting slot for key ${e}, running: ${this._running.size}, waiting: ${this._queue.length}`),new Promise(s=>{this._queue.push({key:e,resolve:s})})}add(e,s){this._running.has(e)||(this._running.set(e,s),s.finally(()=>{this._running.delete(e),this.debug&&console.debug(`[PromiseQueue]: Promise for key ${e} finished, running: ${this._running.size}, waiting: ${this._queue.length}`)}),this.debug&&console.debug(`[PromiseQueue]: Adding promise for key ${e}, running: ${this._running.size}, waiting: ${this._queue.length}`))}internalUpdate(){const e=this.maxConcurrent-this._running.size;for(let s=0;s<e&&this._queue.length>0;s++){this.debug&&console.debug(`[PromiseQueue]: Running ${this._running.size} promises, waiting for ${this._queue.length} more.`);const{key:n,resolve:r}=this._queue.shift();r({use:o=>this.add(n,o)})}}}const ht=typeof window>"u"&&typeof document>"u",De=Symbol("needle:raycast-mesh");function te(t){return t?.[De]instanceof ue?t[De]:null}function Ne(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!te(t)){const s=gt(e);s.userData={isRaycastMesh:!0},t[De]=s}}function Fe(t=!0){if(t){if(ne)return;const e=ne=Z.prototype.raycast;Z.prototype.raycast=function(s,n){const r=this,o=te(r);let i;o&&r.isMesh&&(i=r.geometry,r.geometry=o),e.call(this,s,n),i&&(r.geometry=i)}}else{if(!ne)return;Z.prototype.raycast=ne,ne=null}}let ne=null;function gt(t){const e=new ue;for(const s in t.attributes)e.setAttribute(s,t.getAttribute(s));return e.setIndex(t.getIndex()),e}const q=new Array,m=re("debugprogressive");let he,J=-1;if(m){let t=function(){J+=1,J>=e&&(J=-1),console.log(`Toggle LOD level [${J}]`)},e=6;window.addEventListener("keyup",s=>{s.key==="p"&&t(),s.key==="w"&&(he=!he,console.log(`Toggle wireframe [${he}]`));const n=parseInt(s.key);!isNaN(n)&&n>=0&&(J=n,console.log(`Set LOD level to [${J}]`))})}function Ue(t){if(m)if(Array.isArray(t))for(const e of t)Ue(e);else t&&"wireframe"in t&&(t.wireframe=he===!0)}const F="NEEDLE_progressive",Oe=Symbol("needle-progressive-texture"),M=class{constructor(t,e){u(this,"parser"),u(this,"url"),u(this,"_isLoadingMesh"),u(this,"loadMesh",s=>{var n,r;if(this._isLoadingMesh)return null;const o=(r=(n=this.parser.json.meshes[s])==null?void 0:n.extensions)==null?void 0:r[F];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",s).then(i=>{var l;return this._isLoadingMesh=!1,i&&M.registerMesh(this.url,o.guid,i,(l=o.lods)==null?void 0:l.length,0,o),i})):null}),m&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return F}static getMeshLODExtension(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getPrimitiveIndex(t){var e;return((e=this.getAssignedLODInformation(t))==null?void 0:e.index)??-1}static getMaterialMinMaxLODsCount(t,e){const s=this,n="LODS:minmax",r=t[n];if(r!=null)return r;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const i of t)this.getMaterialMinMaxLODsCount(i,e);return t[n]=e,e}if(m==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const i=t;for(const l of Object.keys(i.uniforms)){const d=i.uniforms[l].value;d?.isTexture===!0&&o(d,e)}}else if(t.isMaterial)for(const i of Object.keys(t)){const l=t[i];l?.isTexture===!0&&o(l,e)}return t[n]=e,e;function o(i,l){const d=s.getAssignedLODInformation(i);if(d){const a=s.lodInfos.get(d.key);if(a&&a.lods){l.min_count=Math.min(l.min_count,a.lods.length),l.max_count=Math.max(l.max_count,a.lods.length);for(let g=0;g<a.lods.length;g++){const x=a.lods[g];x.width&&(l.lods[g]=l.lods[g]||{min_height:1/0,max_height:0},l.lods[g].min_height=Math.min(l.lods[g].min_height,x.height),l.lods[g].max_height=Math.max(l.lods[g].max_height,x.height))}}}}}static hasLODLevelAvailable(t,e){var s;if(Array.isArray(t)){for(const o of t)if(this.hasLODLevelAvailable(o,e))return!0;return!1}if(t.isMaterial===!0){for(const o of Object.keys(t)){const i=t[o];if(i&&i.isTexture&&this.hasLODLevelAvailable(i,e))return!0}return!1}else if(t.isGroup===!0){for(const o of t.children)if(o.isMesh===!0&&this.hasLODLevelAvailable(o,e))return!0}let n,r;if(t.isMesh?n=t.geometry:(t.isBufferGeometry||t.isTexture)&&(n=t),n&&(s=n?.userData)!=null&&s.LODS){const o=n.userData.LODS;if(r=this.lodInfos.get(o.key),e===void 0)return r!=null;if(r)return Array.isArray(r.lods)?e<r.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof Z||t.isMesh===!0){const n=t.geometry,r=this.getAssignedLODInformation(n);if(!r)return Promise.resolve(null);for(const o of q)(s=o.onBeforeGetLODMesh)==null||s.call(o,t,e);return t["LOD:requested level"]=e,M.getOrLoadLOD(n,e).then(o=>{if(Array.isArray(o)){const i=r.index||0;o=o[i]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],o&&n!=o&&(o?.isBufferGeometry?t.geometry=o:m&&console.error("Invalid LOD geometry",o))),o}).catch(o=>(console.error("Error loading mesh LOD",t,o),null))}else m&&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 n=new Array;for(const r of s.material){const o=this.assignTextureLOD(r,e);n.push(o)}return Promise.all(n).then(r=>{const o=new Array;for(const i of r)Array.isArray(i)&&o.push(...i);return o})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,n=[],r=new Array;if(s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const o=s;for(const i of Object.keys(o.uniforms)){const l=o.uniforms[i].value;if(l?.isTexture===!0){const d=this.assignTextureLODForSlot(l,e,s,i).then(a=>(a&&o.uniforms[i].value!=a&&(o.uniforms[i].value=a,o.uniformsNeedUpdate=!0),a));n.push(d),r.push(i)}}}else for(const o of Object.keys(s)){const i=s[o];if(i?.isTexture===!0){const l=this.assignTextureLODForSlot(i,e,s,o);n.push(l),r.push(o)}}return Promise.all(n).then(o=>{const i=new Array;for(let l=0;l<o.length;l++){const d=o[l],a=r[l];d&&d.isTexture===!0?i.push({material:s,slot:a,texture:d,level:e}):i.push({material:s,slot:a,texture:null,level:e})}return i})}if(t instanceof se||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,n){return t?.isTexture!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(t):M.getOrLoadLOD(t,e).then(r=>{if(Array.isArray(r))return null;if(r?.isTexture===!0){if(r!=t&&s&&n){const o=s[n];if(o&&!m){const i=this.getAssignedLODInformation(o);if(i&&i?.level<e)return m==="verbose"&&console.warn("Assigned texture level is already higher: ",i.level,e,s,o,r),null}s[n]=r}return r}else m=="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 m&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((n,r)=>{var o;if(n!=null&&n.extensions){const i=n?.extensions[F];if(i){if(!i.lods){m&&console.warn("Texture has no LODs",i);return}let l=!1;for(const d of this.parser.associations.keys())if(d.isTexture===!0){const a=this.parser.associations.get(d);a?.textures===r&&(l=!0,M.registerTexture(this.url,d,(o=i.lods)==null?void 0:o.length,r,i))}l||this.parser.getDependency("texture",r).then(d=>{var a;d&&M.registerTexture(this.url,d,(a=i.lods)==null?void 0:a.length,r,i)})}}}),(s=this.parser.json.meshes)==null||s.forEach((n,r)=>{if(n!=null&&n.extensions){const o=n?.extensions[F];if(o&&o.lods){for(const i of this.parser.associations.keys())if(i.isMesh){const l=this.parser.associations.get(i);l?.meshes===r&&M.registerMesh(this.url,o.guid,i,o.lods.length,l.primitives,o)}}}}),null}static async getOrLoadLOD(t,e){var s,n,r,o;const i=m=="verbose",l=t.userData.LODS;if(!l)return null;const d=l?.key;let a;if(t.isTexture===!0){const g=t;g.source&&g.source[Oe]&&(a=g.source[Oe])}if(a||(a=M.lodInfos.get(d)),a){if(e>0){let p=!1;const L=Array.isArray(a.lods);if(L&&e>=a.lods.length?p=!0:L||(p=!0),p)return this.lowresCache.get(d)}const g=Array.isArray(a.lods)?(s=a.lods[e])==null?void 0:s.path:a.lods;if(!g)return m&&!a["missing:uri"]&&(a["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,a)),null;const x=lt(l.url,g);if(x.endsWith(".glb")||x.endsWith(".gltf")){if(!a.guid)return console.warn("missing pointer for glb/gltf texture",a),null;const p=x+"_"+a.guid,L=this.previouslyLoaded.get(p);if(L!==void 0){i&&console.log(`LOD ${e} was already loading/loaded: ${p}`);let h=await L.catch(T=>(console.error(`Error loading LOD ${e} from ${x}
|
|
2
|
-
`,
|
|
3
|
-
`,
|
|
1
|
+
var st=Object.defineProperty,nt=(t,e,r)=>e in t?st(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,c=(t,e,r)=>(nt(t,typeof e!="symbol"?e+"":e,r),r),Re=(t,e,r)=>{if(!e.has(t))throw TypeError("Cannot "+r)},w=(t,e,r)=>(Re(t,e,"read from private field"),r?r.call(t):e.get(t)),Q=(t,e,r)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,r)},q=(t,e,r,n)=>(Re(t,e,"write to private field"),n?n.call(t,r):e.set(t,r),r);import{BufferGeometry as oe,Mesh as J,Box3 as Le,Vector3 as N,Sphere as $e,CompressedTexture as ot,Texture as Z,Matrix3 as it,InterleavedBuffer as at,InterleavedBufferAttribute as lt,BufferAttribute as ut,TextureLoader as dt,Matrix4 as Ge,Clock as ct,MeshStandardMaterial as ht}from"three";import{GLTFLoader as be}from"three/examples/jsm/loaders/GLTFLoader.js";import{MeshoptDecoder as gt}from"three/examples/jsm/libs/meshopt_decoder.module.js";import{DRACOLoader as ft}from"three/examples/jsm/loaders/DRACOLoader.js";import{KTX2Loader as mt}from"three/examples/jsm/loaders/KTX2Loader.js";const je="";globalThis.GLTF_PROGRESSIVE_VERSION=je,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 pt=W,vt=ee,Ne=new URL(W+"draco_decoder.js");Ne.searchParams.append("range","true"),fetch(Ne,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${W} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),W===pt&&We("./include/draco/"),ee===vt&&Ue("./include/ktx2/")}).finally(()=>{Fe()});const xt=()=>({dracoDecoderPath:W,ktx2TranscoderPath:ee});function We(t){W=t,j&&j[De]!=W?(console.debug("Updating Draco decoder path to "+t),j[De]=W,j.setDecoderPath(W),j.preload()):console.debug("Setting Draco decoder path to "+t)}function Ue(t){ee=t,U&&U.transcoderPath!=ee?(console.debug("Updating KTX2 transcoder path to "+t),U.setTranscoderPath(ee),U.init()):console.debug("Setting KTX2 transcoder path to "+t)}function he(t){return Fe(),t?U.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:j,ktx2Loader:U,meshoptDecoder:ge}}function Me(t){t.dracoLoader||t.setDRACOLoader(j),t.ktx2Loader||t.setKTX2Loader(U),t.meshoptDecoder||t.setMeshoptDecoder(ge)}const De=Symbol("dracoDecoderPath");let j,ge,U;function Fe(){j||(j=new ft,j[De]=W,j.setDecoderPath(W),j.setDecoderConfig({type:"js"}),j.preload()),U||(U=new mt,U.setTranscoderPath(ee),U.init()),ge||(ge=gt)}const Oe=new WeakMap;function Se(t,e){let r=Oe.get(t);r?r=Object.assign(r,e):r=e,Oe.set(t,r)}const ze=be.prototype.load;function yt(...t){const e=Oe.get(this);let r=t[0];const n=new URL(r,window.location.href);if(n.hostname.endsWith("needle.tools")){const s=e?.progressive!==void 0?e.progressive:!0,o=e!=null&&e.usecase?e.usecase:"default";s?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${o}`:this.requestHeader.Accept=`*/*;usecase=${o}`,r=n.toString()}return t[0]=r,ze?.call(this,...t)}be.prototype.load=yt,te("debugprogressive");function te(t){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(t);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function wt(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const r=t.lastIndexOf("/");if(r>=0){const n=t.substring(0,r+1);for(;n.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return n+e}return e}let fe;function qe(){return fe!==void 0||(fe=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),te("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",fe)),fe}function Ve(){if(typeof window>"u")return!1;const t=new URL(window.location.href),e=t.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(t.hostname);return t.hostname==="127.0.0.1"||e}class _t{constructor(e=100,r={}){c(this,"_running",new Map),c(this,"_queue",[]),c(this,"debug",!1),c(this,"tick",()=>{this.internalUpdate(),setTimeout(this.tick,10)}),this.maxConcurrent=e,this.debug=r.debug??!1,window.requestAnimationFrame(this.tick)}slot(e){return this.debug&&console.debug(`[PromiseQueue]: Requesting slot for key ${e}, running: ${this._running.size}, waiting: ${this._queue.length}`),new Promise(r=>{this._queue.push({key:e,resolve:r})})}add(e,r){this._running.has(e)||(this._running.set(e,r),r.finally(()=>{this._running.delete(e),this.debug&&console.debug(`[PromiseQueue]: Promise finished now running: ${this._running.size}, waiting: ${this._queue.length}. (finished ${e})`)}),this.debug&&console.debug(`[PromiseQueue]: Added new promise, now running: ${this._running.size}, waiting: ${this._queue.length}. (added ${e})`))}internalUpdate(){const e=this.maxConcurrent-this._running.size;for(let r=0;r<e&&this._queue.length>0;r++){this.debug&&console.debug(`[PromiseQueue]: Running ${this._running.size} promises, waiting for ${this._queue.length} more.`);const{key:n,resolve:s}=this._queue.shift();s({use:o=>this.add(n,o)})}}}const Lt=typeof window>"u"&&typeof document>"u",Te=Symbol("needle:raycast-mesh");function se(t){return t?.[Te]instanceof oe?t[Te]:null}function Xe(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!se(t)){const r=bt(e);r.userData={isRaycastMesh:!0},t[Te]=r}}function Ke(t=!0){if(t){if(ie)return;const e=ie=J.prototype.raycast;J.prototype.raycast=function(r,n){const s=this,o=se(s);let i;o&&s.isMesh&&(i=s.geometry,s.geometry=o),e.call(this,r,n),i&&(s.geometry=i)}}else{if(!ie)return;J.prototype.raycast=ie,ie=null}}let ie=null;function bt(t){const e=new oe;for(const r in t.attributes)e.setAttribute(r,t.getAttribute(r));return e.setIndex(t.getIndex()),e}const V=new Array,y=te("debugprogressive");let me,re=-1;if(y){let t=function(){re+=1,re>=e&&(re=-1),console.log(`Toggle LOD level [${re}]`)},e=6;window.addEventListener("keyup",r=>{r.key==="p"&&t(),r.key==="w"&&(me=!me,console.log(`Toggle wireframe [${me}]`));const n=parseInt(r.key);!isNaN(n)&&n>=0&&(re=n,console.log(`Set LOD level to [${re}]`))})}function He(t){if(y)if(Array.isArray(t))for(const e of t)He(e);else t&&"wireframe"in t&&(t.wireframe=me===!0)}const ae=new Array;let Mt=0;const Dt=qe()?2:10;function Ot(t){if(ae.length<Dt){const r=ae.length;y&&console.warn(`[Worker] Creating new worker #${r}`);const n=Be.createWorker(t||{});return ae.push(n),n}const e=Mt++%ae.length;return ae[e]}class Be{constructor(e,r){c(this,"_running",[]),c(this,"_webglRenderer",null),c(this,"_debug",!1),this.worker=e,this._debug=r.debug??!1,e.onmessage=n=>{const s=n.data;switch(this._debug&&console.log("[Worker] EVENT",s),s.type){case"loaded-gltf":for(const o of this._running)if(o.url===s.result.url){St(s.result),o.resolve(s.result);const i=o.url;i.startsWith("blob:")&&URL.revokeObjectURL(i)}}},e.onerror=n=>{console.error("[Worker] Error in gltf-progressive worker:",n)},e.postMessage({type:"init"})}static async createWorker(e){const r=new Worker(new URL("/assets/loader.worker-c528e7f2.js",self.location),{type:"module"});return new Be(r,e)}async load(e,r){const n=xt();let s=r?.renderer;s||(this._webglRenderer??(this._webglRenderer=(async()=>{const{WebGLRenderer:a}=await import("three");return new a})()),s=await this._webglRenderer);const o=he(s).ktx2Loader.workerConfig;e instanceof URL?e=e.toString():e.startsWith("file:")?e=URL.createObjectURL(new Blob([e])):!e.startsWith("blob:")&&!e.startsWith("http:")&&!e.startsWith("https:")&&(e=new URL(e,window.location.href).toString());const i={type:"load",url:e,dracoDecoderPath:n.dracoDecoderPath,ktx2TranscoderPath:n.ktx2TranscoderPath,ktx2LoaderConfig:o};return this._debug&&console.debug("[Worker] Sending load request",i),this.worker.postMessage(i),new Promise(a=>{this._running.push({url:e.toString(),resolve:a})})}}function St(t){var e,r,n,s,o,i,a,d,u,m,p,x,S,T,b,I;for(const v of t.geometries){const l=v.geometry,g=new oe;if(g.name=l.name||"",l.index){const _=l.index;g.setIndex(Pe(_))}for(const _ in l.attributes){const M=l.attributes[_],k=Pe(M);g.setAttribute(_,k)}if(l.morphAttributes)for(const _ in l.morphAttributes){const M=l.morphAttributes[_].map(k=>Pe(k));g.morphAttributes[_]=M}if(g.morphTargetsRelative=l.morphTargetsRelative??!1,g.boundingBox=new Le,g.boundingBox.min=new N((e=l.boundingBox)==null?void 0:e.min.x,(r=l.boundingBox)==null?void 0:r.min.y,(n=l.boundingBox)==null?void 0:n.min.z),g.boundingBox.max=new N((s=l.boundingBox)==null?void 0:s.max.x,(o=l.boundingBox)==null?void 0:o.max.y,(i=l.boundingBox)==null?void 0:i.max.z),g.boundingSphere=new $e(new N((a=l.boundingSphere)==null?void 0:a.center.x,(d=l.boundingSphere)==null?void 0:d.center.y,(u=l.boundingSphere)==null?void 0:u.center.z),(m=l.boundingSphere)==null?void 0:m.radius),l.groups)for(const _ of l.groups)g.addGroup(_.start,_.count,_.materialIndex);l.userData&&(g.userData=l.userData),v.geometry=g}for(const v of t.textures){const l=v.texture;let g=null;if(l.isCompressedTexture){const _=l.mipmaps,M=((p=l.image)==null?void 0:p.width)||((S=(x=l.source)==null?void 0:x.data)==null?void 0:S.width)||-1,k=((T=l.image)==null?void 0:T.height)||((I=(b=l.source)==null?void 0:b.data)==null?void 0:I.height)||-1;g=new ot(_,M,k,l.format,l.type,l.mapping,l.wrapS,l.wrapT,l.magFilter,l.minFilter,l.anisotropy,l.colorSpace)}else g=new Z(l.image,l.mapping,l.wrapS,l.wrapT,l.magFilter,l.minFilter,l.format,l.type,l.anisotropy,l.colorSpace),g.mipmaps=l.mipmaps,g.channel=l.channel,g.source.data=l.source.data,g.flipY=l.flipY,g.premultiplyAlpha=l.premultiplyAlpha,g.unpackAlignment=l.unpackAlignment,g.matrix=new it(...l.matrix.elements);if(!g){console.error("[Worker] Failed to create new texture from received data. Texture is not a CompressedTexture or Texture.");continue}v.texture=g}return t}function Pe(t){let e=t;if("isInterleavedBufferAttribute"in t&&t.isInterleavedBufferAttribute){const r=t.data,n=r.array,s=new at(n,r.stride);e=new lt(s,t.itemSize,n.byteOffset,t.normalized),e.offset=t.offset}else"isBufferAttribute"in t&&t.isBufferAttribute&&(e=new ut(t.array,t.itemSize,t.normalized),e.usage=t.usage,e.gpuType=t.gpuType,e.updateRanges=t.updateRanges);return e}const Tt=te("gltf-progressive-worker"),Pt=te("gltf-progressive-reduce-mipmaps"),Ae=Symbol("needle-progressive-texture"),z="NEEDLE_progressive",D=class{constructor(t,e){c(this,"parser"),c(this,"url"),c(this,"_isLoadingMesh"),c(this,"loadMesh",r=>{var n,s;if(this._isLoadingMesh)return null;const o=(s=(n=this.parser.json.meshes[r])==null?void 0:n.extensions)==null?void 0:s[z];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",r).then(i=>{var a;return this._isLoadingMesh=!1,i&&D.registerMesh(this.url,o.guid,i,(a=o.lods)==null?void 0:a.length,0,o),i})):null}),y&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return z}static getMeshLODExtension(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getPrimitiveIndex(t){var e;return((e=this.getAssignedLODInformation(t))==null?void 0:e.index)??-1}static getMaterialMinMaxLODsCount(t,e){const r=this,n="LODS:minmax",s=t[n];if(s!=null)return s;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const i of t)this.getMaterialMinMaxLODsCount(i,e);return t[n]=e,e}if(y==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const i=t;for(const a of Object.keys(i.uniforms)){const d=i.uniforms[a].value;d?.isTexture===!0&&o(d,e)}}else if(t.isMaterial)for(const i of Object.keys(t)){const a=t[i];a?.isTexture===!0&&o(a,e)}else y&&console.warn(`[getMaterialMinMaxLODsCount] Unsupported material type: ${t.type}`);return t[n]=e,e;function o(i,a){const d=r.getAssignedLODInformation(i);if(d){const u=r.lodInfos.get(d.key);if(u&&u.lods){a.min_count=Math.min(a.min_count,u.lods.length),a.max_count=Math.max(a.max_count,u.lods.length);for(let m=0;m<u.lods.length;m++){const p=u.lods[m];p.width&&(a.lods[m]=a.lods[m]||{min_height:1/0,max_height:0},a.lods[m].min_height=Math.min(a.lods[m].min_height,p.height),a.lods[m].max_height=Math.max(a.lods[m].max_height,p.height))}}}}}static hasLODLevelAvailable(t,e){var r;if(Array.isArray(t)){for(const o of t)if(this.hasLODLevelAvailable(o,e))return!0;return!1}if(t.isMaterial===!0){for(const o of Object.keys(t)){const i=t[o];if(i&&i.isTexture&&this.hasLODLevelAvailable(i,e))return!0}return!1}else if(t.isGroup===!0){for(const o of t.children)if(o.isMesh===!0&&this.hasLODLevelAvailable(o,e))return!0}let n,s;if(t.isMesh?n=t.geometry:(t.isBufferGeometry||t.isTexture)&&(n=t),n&&(r=n?.userData)!=null&&r.LODS){const o=n.userData.LODS;if(s=this.lodInfos.get(o.key),e===void 0)return s!=null;if(s)return Array.isArray(s.lods)?e<s.lods.length:e===0}return!1}static assignMeshLOD(t,e){var r;if(!t)return Promise.resolve(null);if(t instanceof J||t.isMesh===!0){const n=t.geometry,s=this.getAssignedLODInformation(n);if(!s)return Promise.resolve(null);for(const o of V)(r=o.onBeforeGetLODMesh)==null||r.call(o,t,e);return t["LOD:requested level"]=e,D.getOrLoadLOD(n,e).then(o=>{if(Array.isArray(o)){const i=s.index||0;o=o[i]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],o&&n!=o&&(o?.isBufferGeometry?t.geometry=o:y&&console.error("Invalid LOD geometry",o))),o}).catch(o=>(console.error("Error loading mesh LOD",t,o),null))}else y&&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 r=t;if(Array.isArray(r.material)){const n=new Array;for(const s of r.material){const o=this.assignTextureLOD(s,e);n.push(o)}return Promise.all(n).then(s=>{const o=new Array;for(const i of s)Array.isArray(i)&&o.push(...i);return o})}else return this.assignTextureLOD(r.material,e)}if(t.isMaterial===!0){const r=t,n=[],s=new Array;if(r.uniforms&&(r.isRawShaderMaterial||r.isShaderMaterial===!0)){const o=r;for(const i of Object.keys(o.uniforms)){const a=o.uniforms[i].value;if(a?.isTexture===!0){const d=this.assignTextureLODForSlot(a,e,r,i).then(u=>(u&&o.uniforms[i].value!=u&&(o.uniforms[i].value=u,o.uniformsNeedUpdate=!0),u));n.push(d),s.push(i)}}}else for(const o of Object.keys(r)){const i=r[o];if(i?.isTexture===!0){const a=this.assignTextureLODForSlot(i,e,r,o);n.push(a),s.push(o)}}return Promise.all(n).then(o=>{const i=new Array;for(let a=0;a<o.length;a++){const d=o[a],u=s[a];d&&d.isTexture===!0?i.push({material:r,slot:u,texture:d,level:e}):i.push({material:r,slot:u,texture:null,level:e})}return i})}if(t instanceof Z||t.isTexture===!0){const r=t;return this.assignTextureLODForSlot(r,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,r,n){return t?.isTexture!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(t):D.getOrLoadLOD(t,e).then(s=>{var o,i;if(Array.isArray(s))return console.warn("Progressive: Got an array of textures for a texture slot, this should not happen..."),null;if(s?.isTexture===!0){if(s!=t&&r&&n){const a=r[n];if(a&&!y){const d=this.getAssignedLODInformation(a);if(d&&d?.level<e)return y==="verbose"&&console.warn("Assigned texture level is already higher: ",d.level,e,r,a,s),null}if(Pt&&s.mipmaps){const d=s.mipmaps.length;s.mipmaps.length=Math.min(s.mipmaps.length,3),d!==s.mipmaps.length&&y&&console.debug(`Reduced mipmap count from ${d} to ${s.mipmaps.length} for ${s.uuid}: ${(o=s.image)==null?void 0:o.width}x${(i=s.image)==null?void 0:i.height}.`)}r[n]=s}return s}else y=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(s=>(console.error("Error loading LOD",t,s),null))}afterRoot(t){var e,r;return y&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((n,s)=>{var o;if(n!=null&&n.extensions){const i=n?.extensions[z];if(i){if(!i.lods){y&&console.warn("Texture has no LODs",i);return}let a=!1;for(const d of this.parser.associations.keys())if(d.isTexture===!0){const u=this.parser.associations.get(d);u?.textures===s&&(a=!0,D.registerTexture(this.url,d,(o=i.lods)==null?void 0:o.length,s,i))}a||this.parser.getDependency("texture",s).then(d=>{var u;d&&D.registerTexture(this.url,d,(u=i.lods)==null?void 0:u.length,s,i)})}}}),(r=this.parser.json.meshes)==null||r.forEach((n,s)=>{if(n!=null&&n.extensions){const o=n?.extensions[z];if(o&&o.lods){for(const i of this.parser.associations.keys())if(i.isMesh){const a=this.parser.associations.get(i);a?.meshes===s&&D.registerMesh(this.url,o.guid,i,o.lods.length,a.primitives,o)}}}}),null}static async getOrLoadLOD(t,e){var r,n,s,o;const i=y=="verbose",a=this.getAssignedLODInformation(t);if(!a)return y&&console.warn(`[gltf-progressive] No LOD information found: ${t.name}, uuid: ${t.uuid}, type: ${t.type}`,t),null;const d=a?.key;let u;if(t.isTexture===!0){const m=t;m.source&&m.source[Ae]&&(u=m.source[Ae])}if(u||(u=D.lodInfos.get(d)),u){if(e>0){let x=!1;const S=Array.isArray(u.lods);if(S&&e>=u.lods.length?x=!0:S||(x=!0),x)return this.lowresCache.get(d)}const m=Array.isArray(u.lods)?(r=u.lods[e])==null?void 0:r.path:u.lods;if(!m)return y&&!u["missing:uri"]&&(u["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,u)),null;const p=wt(a.url,m);if(p.endsWith(".glb")||p.endsWith(".gltf")){if(!u.guid)return console.warn("missing pointer for glb/gltf texture",u),null;const x=p+"_"+u.guid,S=await this.queue.slot(p),T=this.previouslyLoaded.get(x);if(T!==void 0){i&&console.log(`LOD ${e} was already loading/loaded: ${x}`);let v=await T.catch(g=>(console.error(`Error loading LOD ${e} from ${p}
|
|
2
|
+
`,g),null)),l=!1;if(v==null||(v instanceof Z&&t instanceof Z?(n=v.image)!=null&&n.data||(s=v.source)!=null&&s.data?v=this.copySettings(t,v):(l=!0,this.previouslyLoaded.delete(x)):v instanceof oe&&t instanceof oe&&((o=v.attributes.position)!=null&&o.array||(l=!0,this.previouslyLoaded.delete(x)))),!l)return v}if(!S.use)return y&&console.log(`LOD ${e} was aborted: ${p}`),null;const b=u,I=new Promise(async(v,l)=>{if(Tt){const L=await(await Ot({})).load(p);if(L.textures.length>0)for(const h of L.textures){let f=h.texture;return D.assignLODInformation(a.url,f,d,e,void 0),t instanceof Z&&(f=this.copySettings(t,f)),f&&(f.guid=b.guid),v(f)}if(L.geometries.length>0){const h=new Array;for(const f of L.geometries){const O=f.geometry;D.assignLODInformation(a.url,O,d,e,f.primitiveIndex),h.push(O)}return v(h)}return v(null)}const g=new be;Me(g),y&&(await new Promise(L=>setTimeout(L,1e3)),i&&console.warn("Start loading (delayed) "+p,b.guid));let _=p;if(b&&Array.isArray(b.lods)){const L=b.lods[e];L.hash&&(_+="?v="+L.hash)}const M=await g.loadAsync(_).catch(L=>(console.error(`Error loading LOD ${e} from ${p}
|
|
3
|
+
`,L),v(null)));if(!M)return v(null);const k=M.parser;i&&console.log("Loading finished "+p,b.guid);let E=0;if(M.parser.json.textures){let L=!1;for(const h of M.parser.json.textures){if(h!=null&&h.extensions){const f=h?.extensions[z];if(f!=null&&f.guid&&f.guid===b.guid){L=!0;break}}E++}if(L){let h=await k.getDependency("texture",E);return h&&D.assignLODInformation(a.url,h,d,e,void 0),i&&console.log('change "'+t.name+'" \u2192 "'+h.name+'"',p,E,h,x),t instanceof Z&&(h=this.copySettings(t,h)),h&&(h.guid=b.guid),v(h)}else y&&console.warn("Could not find texture with guid",b.guid,M.parser.json)}if(E=0,M.parser.json.meshes){let L=!1;for(const h of M.parser.json.meshes){if(h!=null&&h.extensions){const f=h?.extensions[z];if(f!=null&&f.guid&&f.guid===b.guid){L=!0;break}}E++}if(L){const h=await k.getDependency("mesh",E);if(i&&console.log(`Loaded Mesh "${h.name}"`,p,E,h,x),h.isMesh===!0){const f=h.geometry;return D.assignLODInformation(a.url,f,d,e,0),v(f)}else{const f=new Array;for(let O=0;O<h.children.length;O++){const $=h.children[O];if($.isMesh===!0){const G=$.geometry;D.assignLODInformation(a.url,G,d,e,O),f.push(G)}}return v(f)}}else y&&console.warn("Could not find mesh with guid",b.guid,M.parser.json)}return v(null)});return this.previouslyLoaded.set(x,I),S.use(I),await I}else if(t instanceof Z){i&&console.log("Load texture from uri: "+p);const x=await new dt().loadAsync(p);return x?(x.guid=u.guid,x.flipY=!1,x.needsUpdate=!0,x.colorSpace=t.colorSpace,i&&console.log(u,x)):y&&console.warn("failed loading",p),x}}else y&&console.warn(`Can not load LOD ${e}: no LOD info found for "${d}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,r,n,s){if(!e)return;e.userData||(e.userData={});const o=new At(t,r,n,s);e.userData.LODS=o,"source"in e&&typeof e.source=="object"&&(e.source.LODS=o)}static getAssignedLODInformation(t){var e,r;return t?(e=t.userData)!=null&&e.LODS?t.userData.LODS:"source"in t&&(r=t.source)!=null&&r.LODS?t.source.LODS:null:null}static copySettings(t,e){return e?(y==="verbose"&&console.debug(`Copy texture settings
|
|
4
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}};let P=M;u(P,"registerTexture",(t,e,s,n,r)=>{if(m&&console.log("> Progressive: register texture",n,e.name,e.uuid,e,r),!e){m&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Oe]=r);const o=r.guid;M.assignLODInformation(t,e,o,s,n),M.lodInfos.set(o,r),M.lowresCache.set(o,e)}),u(P,"registerMesh",(t,e,s,n,r,o)=>{var i;const l=s.geometry;if(!l){m&&console.warn("gltf-progressive: Register mesh without geometry");return}l.userData||(l.userData={}),m&&console.log("> Progressive: register mesh "+s.name,{index:r,uuid:s.uuid},o,s),M.assignLODInformation(t,l,e,n,r),M.lodInfos.set(e,o);let d=M.lowresCache.get(e);d?d.push(s.geometry):d=[s.geometry],M.lowresCache.set(e,d),n>0&&!te(s)&&Ne(s,l);for(const a of q)(i=a.onRegisteredNewMesh)==null||i.call(a,s,o)}),u(P,"lodInfos",new Map),u(P,"previouslyLoaded",new Map),u(P,"lowresCache",new Map),u(P,"queue",new ct(100,{debug:m!=!1}));class ft{constructor(e,s,n,r){u(this,"url"),u(this,"key"),u(this,"level"),u(this,"index"),this.url=e,this.key=s,this.level=n,r!=null&&(this.index=r)}}class ge{constructor(e,s){u(this,"frame_start"),u(this,"frame_capture_end"),u(this,"ready"),u(this,"_resolve"),u(this,"_signal"),u(this,"_resolved",!1),u(this,"_addedCount",0),u(this,"_resolvedCount",0),u(this,"_awaiting",[]),u(this,"_maxPromisesPerObject",1),u(this,"_currentFrame",0),u(this,"_seen",new WeakMap);var n;const r=Math.max(s.frames??2,2);this.frame_start=e,this.frame_capture_end=e+r,this.ready=new Promise(o=>{this._resolve=o}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=s.signal,(n=this._signal)==null||n.addEventListener("abort",()=>{this.resolveNow()}),this._maxPromisesPerObject=Math.max(1,s.maxPromisesPerObject??1)}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(e){var s;this._currentFrame=e,((s=this._signal)!=null&&s.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(e,s,n){if(this._resolved){m&&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(s)){let r=this._seen.get(s);if(r>=this._maxPromisesPerObject){m&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(s,r+1)}else this._seen.set(s,1);this._awaiting.push(n),this._addedCount++,n.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(n),1)})}}resolveNow(){var e,s;this._resolved||(s=this._resolve)==null||s.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((e=this._signal)==null?void 0:e.aborted)??!1})}}u(ge,"addPromise",(t,e,s,n)=>{n.forEach(r=>{r.add(t,e,s)})});const $=re("debugprogressive"),mt=re("noprogressive"),Se=Symbol("Needle:LODSManager"),Pe=Symbol("Needle:LODState"),V=Symbol("Needle:CurrentLOD"),A={mesh_lod:-1,texture_lod:-1};var E,X,Te,K,oe,fe,H;const b=class{constructor(t,e){u(this,"renderer"),u(this,"context"),u(this,"projectionScreenMatrix",new Ce),u(this,"targetTriangleDensity",2e5),u(this,"skinnedMeshAutoUpdateBoundsInterval",30),u(this,"updateInterval","auto"),Y(this,E,1),u(this,"pause",!1),u(this,"manual",!1),u(this,"_newPromiseGroups",[]),u(this,"_promiseGroupIds",0),u(this,"_lodchangedlisteners",[]),Y(this,X,void 0),Y(this,Te,new Je),Y(this,K,0),Y(this,oe,0),Y(this,fe,0),Y(this,H,0),u(this,"_fpsBuffer",[60,60,60,60,60]),u(this,"_sphere",new tt),u(this,"_tempBox",new ke),u(this,"_tempBox2",new ke),u(this,"tempMatrix",new Ce),u(this,"_tempWorldPosition",new W),u(this,"_tempBoxSize",new W),u(this,"_tempBox2Size",new W),this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Pe]}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[Se])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[Se];const s=new b(t,{engine:"unknown",...e});return t[Se]=s,s}get plugins(){return q}awaitLoading(t){const e=this._promiseGroupIds++,s=new ge(y(this,K),{...t});this._newPromiseGroups.push(s);const n=performance.now();return s.ready.finally(()=>{const r=this._newPromiseGroups.indexOf(s);r>=0&&(this._newPromiseGroups.splice(r,1),dt()&&performance.measure("LODsManager:awaitLoading",{start:n,detail:{id:e,name: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(y(this,K))}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(y(this,X))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;U(this,X,this.renderer.render);const e=this;Le(this.renderer),this.renderer.render=function(s,n){const r=e.renderer.getRenderTarget();(r==null||"isXRRenderTarget"in r&&r.isXRRenderTarget)&&(t=0,U(e,K,y(e,K)+1),U(e,oe,y(e,Te).getDelta()),U(e,fe,y(e,fe)+y(e,oe)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/y(e,oe)),U(e,H,e._fpsBuffer.reduce((i,l)=>i+l)/e._fpsBuffer.length),$&&y(e,K)%200===0&&console.log("FPS",Math.round(y(e,H)),"Interval:",y(e,E)));const o=t++;y(e,X).call(this,s,n),e.onAfterRender(s,n,o)}}disable(){y(this,X)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=y(this,X),U(this,X,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 o=n[0].material;(o.name==="EffectMaterial"||o.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(mt||(this.updateInterval==="auto"?y(this,H)<40&&y(this,E)<10?(U(this,E,y(this,E)+1),$&&console.warn("\u2193 Reducing LOD updates",y(this,E),y(this,H).toFixed(0))):y(this,H)>=60&&y(this,E)>1&&(U(this,E,y(this,E)-1),$&&console.warn("\u2191 Increasing LOD updates",y(this,E),y(this,H).toFixed(0))):U(this,E,this.updateInterval),y(this,E)>0&&y(this,K)%y(this,E)!=0))return;this.internalUpdate(t,e),this._postprocessPromiseGroups()}}internalUpdate(t,e){var s,n;const r=this.renderer.renderLists.get(t,0),o=r.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const a of o){if(a.material&&(((s=a.geometry)==null?void 0:s.type)==="BoxGeometry"||((n=a.geometry)==null?void 0:n.type)==="BufferGeometry")&&(a.material.name==="SphericalGaussianBlur"||a.material.name=="BackgroundCubeMaterial"||a.material.name==="CubemapFromEquirect"||a.material.name==="EquirectangularToCubeUV")){$&&(a.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(a.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",a,a.material.name,a.material.type)));continue}switch(a.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if($==="color"&&a.material&&!a.object.progressive_debug_color){a.object.progressive_debug_color=!0;const x=Math.random()*16777215,p=new et({color:x});a.object.material=p}const g=a.object;(g instanceof Z||g.isMesh)&&this.updateLODs(t,e,g,i)}const l=r.transparent;for(const a of l){const g=a.object;(g instanceof Z||g.isMesh)&&this.updateLODs(t,e,g,i)}const d=r.transmissive;for(const a of d){const g=a.object;(g instanceof Z||g.isMesh)&&this.updateLODs(t,e,g,i)}}updateLODs(t,e,s,n){var r,o;s.userData||(s.userData={});let i=s[Pe];if(i||(i=new pt,s[Pe]=i),i.frames++<2)return;for(const d of q)(r=d.onBeforeUpdateLOD)==null||r.call(d,this.renderer,t,e,s);const l=b.overrideGlobalLodLevel!==void 0?b.overrideGlobalLodLevel:J;l>=0?(A.mesh_lod=l,A.texture_lod=l):(this.calculateLodLevel(e,s,i,n,A),A.mesh_lod=Math.round(A.mesh_lod),A.texture_lod=Math.round(A.texture_lod)),A.mesh_lod>=0&&this.loadProgressiveMeshes(s,A.mesh_lod),s.material&&A.texture_lod>=0&&this.loadProgressiveTextures(s.material,A.texture_lod),m&&s.material&&!s.isGizmo&&Ue(s.material);for(const d of q)(o=d.onAfterUpdatedLOD)==null||o.call(d,this.renderer,t,e,s,A);i.lastLodLevel_Mesh=A.mesh_lod,i.lastLodLevel_Texture=A.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[V]===void 0||e<t[V])&&(s=!0);const n=t["DEBUG:LOD"];if(n!=null&&(s=t[V]!=n,e=n),s){t[V]=e;const r=P.assignTextureLOD(t,e).then(o=>{this._lodchangedlisteners.forEach(i=>i({type:"texture",level:e,object:t}))});ge.addPromise("texture",t,r,this._newPromiseGroups)}}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[V]!==e;const n=t["DEBUG:LOD"];if(n!=null&&(s=t[V]!=n,e=n),s){t[V]=e;const r=t.geometry,o=P.assignMeshLOD(t,e).then(i=>(i&&t[V]==e&&r!=t.geometry&&this._lodchangedlisteners.forEach(l=>l({type:"mesh",level:e,object:t})),i));return ge.addPromise("mesh",t,o,this._newPromiseGroups),o}return Promise.resolve(null)}static isInside(t,e){const s=t.min,n=t.max,r=(s.x+n.x)*.5,o=(s.y+n.y)*.5;return this._tempPtInside.set(r,o,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,n,r){var o,i,l;if(!e){r.mesh_lod=-1,r.texture_lod=-1;return}if(!t){r.mesh_lod=-1,r.texture_lod=-1;return}let d=10+1,a=!1;if($&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const g=(o=P.getMeshLODExtension(e.geometry))==null?void 0:o.lods,x=P.getPrimitiveIndex(e.geometry),p=g&&g.length>0,L=P.getMaterialMinMaxLODsCount(e.material),Q=L?.min_count!=1/0&&L.min_count>0&&L.max_count>0;if(!p&&!Q){r.mesh_lod=0,r.texture_lod=0;return}p||(a=!0,d=0);const w=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let k=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const h=e;if(!h.boundingBox)h.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0){if(!h[b.$skinnedMeshBoundsOffset]){const T=b.skinnedMeshBoundsFrameOffsetCounter++;h[b.$skinnedMeshBoundsOffset]=T}const v=h[b.$skinnedMeshBoundsOffset];if((s.frames+v)%this.skinnedMeshAutoUpdateBoundsInterval===0){const T=te(h),G=h.geometry;T&&(h.geometry=T),h.computeBoundingBox(),h.geometry=G}}k=h.boundingBox}if(k){const h=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 f=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(f)){r.mesh_lod=0,r.texture_lod=0;return}}if(this._tempBox.copy(k),this._tempBox.applyMatrix4(e.matrixWorld),h.isPerspectiveCamera&&b.isInside(this._tempBox,this.projectionScreenMatrix)){r.mesh_lod=0,r.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&h.isPerspectiveCamera&&h.fov>70){const f=this._tempBox.min,c=this._tempBox.max;let _=f.x,S=f.y,B=c.x,z=c.y;const ie=2,ve=1.5,ae=(f.x+c.x)*.5,le=(f.y+c.y)*.5;_=(_-ae)*ie+ae,S=(S-le)*ie+le,B=(B-ae)*ie+ae,z=(z-le)*ie+le;const Ke=_<0&&B>0?0:Math.min(Math.abs(f.x),Math.abs(c.x)),He=S<0&&z>0?0:Math.min(Math.abs(f.y),Math.abs(c.y)),ye=Math.max(Ke,He);s.lastCentrality=(ve-ye)*(ve-ye)*(ve-ye)}else s.lastCentrality=1;const v=this._tempBox.getSize(this._tempBoxSize);v.multiplyScalar(.5),screen.availHeight>0&&w>0&&v.multiplyScalar(w/screen.availHeight),t.isPerspectiveCamera?v.x*=t.aspect:t.isOrthographicCamera;const T=t.matrixWorldInverse,G=this._tempBox2;G.copy(k),G.applyMatrix4(e.matrixWorld),G.applyMatrix4(T);const D=G.getSize(this._tempBox2Size),j=Math.max(D.x,D.y);if(Math.max(v.x,v.y)!=0&&j!=0&&(v.z=D.z/Math.max(D.x,D.y)*Math.max(v.x,v.y)),s.lastScreenCoverage=Math.max(v.x,v.y,v.z),s.lastScreenspaceVolume.copy(v),s.lastScreenCoverage*=s.lastCentrality,$&&b.debugDrawLine){const f=this.tempMatrix.copy(this.projectionScreenMatrix);f.invert();const c=b.corner0,_=b.corner1,S=b.corner2,B=b.corner3;c.copy(this._tempBox.min),_.copy(this._tempBox.max),_.x=c.x,S.copy(this._tempBox.max),S.y=c.y,B.copy(this._tempBox.max);const z=(c.z+B.z)*.5;c.z=_.z=S.z=B.z=z,c.applyMatrix4(f),_.applyMatrix4(f),S.applyMatrix4(f),B.applyMatrix4(f),b.debugDrawLine(c,_,255),b.debugDrawLine(c,S,255),b.debugDrawLine(_,B,255),b.debugDrawLine(S,B,255)}let O=999;if(g&&s.lastScreenCoverage>0)for(let f=0;f<g.length;f++){const c=g[f];if((((i=c.densities)==null?void 0:i[x])||c.density||1e-5)/s.lastScreenCoverage<n){O=f;break}}O<d&&(d=O,a=!0)}if(a?r.mesh_lod=d:r.mesh_lod=s.lastLodLevel_Mesh,$&&r.mesh_lod!=s.lastLodLevel_Mesh){const h=g?.[r.mesh_lod];h&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} \u2192 ${r.mesh_lod} (${h.density.toFixed(0)}) - ${e.name}`)}if(Q){const h="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(r.texture_lod=L.max_count-1,$){const v=L.lods[L.max_count-1];$&&console.log(`First Texture LOD ${r.texture_lod} (${v.max_height}px) - ${e.name}`)}}else{const v=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let T=s.lastScreenCoverage*4;((l=this.context)==null?void 0:l.engine)==="model-viewer"&&(T*=1.5);const G=w/window.devicePixelRatio*T;let D=!1;for(let j=L.lods.length-1;j>=0;j--){const O=L.lods[j];if(!(h&&O.max_height>=2048)&&!(ut()&&O.max_height>4096)&&(O.max_height>G||!D&&j===0)){if(D=!0,r.texture_lod=j,r.texture_lod<s.lastLodLevel_Texture){const f=O.max_height;$&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} \u2192 ${r.texture_lod} = ${f}px
|
|
6
|
-
Screensize: ${
|
|
7
|
-
${e.name}`)}break}}}}else
|
|
8
|
-
`,t.getAttribute("src"));let e=null,
|
|
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}};let P=D;c(P,"registerTexture",(t,e,r,n,s)=>{var o,i,a,d,u,m,p,x;if(!e){y&&console.error("gltf-progressive: Called register texture without texture");return}if(y){const T=((o=e.image)==null?void 0:o.width)||((a=(i=e.source)==null?void 0:i.data)==null?void 0:a.width)||0,b=((d=e.image)==null?void 0:d.height)||((m=(u=e.source)==null?void 0:u.data)==null?void 0:m.height)||0;console.log(`> Progressive: register texture[${n}] "${e.name||e.uuid}", Current: ${T}x${b}, Max: ${(p=s.lods[0])==null?void 0:p.width}x${(x=s.lods[0])==null?void 0:x.height}, uuid: ${e.uuid}`,s,e)}e.source&&(e.source[Ae]=s);const S=s.guid;D.assignLODInformation(t,e,S,r,n),D.lodInfos.set(S,s),D.lowresCache.set(S,e)}),c(P,"registerMesh",(t,e,r,n,s,o)=>{var i;const a=r.geometry;if(!a){y&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),y&&console.log("> Progressive: register mesh "+r.name,{index:s,uuid:r.uuid},o,r),D.assignLODInformation(t,a,e,n,s),D.lodInfos.set(e,o);let d=D.lowresCache.get(e);d?d.push(r.geometry):d=[r.geometry],D.lowresCache.set(e,d),n>0&&!se(r)&&Xe(r,a);for(const u of V)(i=u.onRegisteredNewMesh)==null||i.call(u,r,o)}),c(P,"lodInfos",new Map),c(P,"previouslyLoaded",new Map),c(P,"lowresCache",new Map),c(P,"workers",[]),c(P,"_workersIndex",0),c(P,"maxConcurrent",50),c(P,"queue",new _t(D.maxConcurrent,{debug:y!=!1}));class At{constructor(e,r,n,s){c(this,"url"),c(this,"key"),c(this,"level"),c(this,"index"),this.url=e,this.key=r,this.level=n,s!=null&&(this.index=s)}}class pe{constructor(e,r){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 s=Math.max(r.frames??2,2);this.frame_start=e,this.frame_capture_end=e+s,this.ready=new Promise(o=>{this._resolve=o}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=r.signal,(n=this._signal)==null||n.addEventListener("abort",()=>{this.resolveNow()}),this._maxPromisesPerObject=Math.max(1,r.maxPromisesPerObject??1)}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(e){var r;this._currentFrame=e,((r=this._signal)!=null&&r.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(e,r,n){if(this._resolved){y&&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(r)){let s=this._seen.get(r);if(s>=this._maxPromisesPerObject){y&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(r,s+1)}else this._seen.set(r,1);this._awaiting.push(n),this._addedCount++,n.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(n),1)})}}resolveNow(){var e,r;this._resolved||(r=this._resolve)==null||r.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((e=this._signal)==null?void 0:e.aborted)??!1})}}c(pe,"addPromise",(t,e,r,n)=>{n.forEach(s=>{s.add(t,e,r)})});const F=te("debugprogressive"),kt=te("noprogressive"),ke=Symbol("Needle:LODSManager"),Ie=Symbol("Needle:LODState"),X=Symbol("Needle:CurrentLOD"),C={mesh_lod:-1,texture_lod:-1};var B,K,Ee,H,le,ve,Y;const A=class{constructor(t,e){c(this,"renderer"),c(this,"context"),c(this,"projectionScreenMatrix",new Ge),c(this,"targetTriangleDensity",2e5),c(this,"skinnedMeshAutoUpdateBoundsInterval",30),c(this,"updateInterval","auto"),Q(this,B,1),c(this,"pause",!1),c(this,"manual",!1),c(this,"_newPromiseGroups",[]),c(this,"_promiseGroupIds",0),c(this,"_lodchangedlisteners",[]),Q(this,K,void 0),Q(this,Ee,new ct),Q(this,H,0),Q(this,le,0),Q(this,ve,0),Q(this,Y,0),c(this,"_fpsBuffer",[60,60,60,60,60]),c(this,"_sphere",new $e),c(this,"_tempBox",new Le),c(this,"_tempBox2",new Le),c(this,"tempMatrix",new Ge),c(this,"_tempWorldPosition",new N),c(this,"_tempBoxSize",new N),c(this,"_tempBox2Size",new N),this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Ie]}static addPlugin(t){V.push(t)}static removePlugin(t){const e=V.indexOf(t);e>=0&&V.splice(e,1)}static get(t,e){if(t[ke])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ke];const r=new A(t,{engine:"unknown",...e});return t[ke]=r,r}get plugins(){return V}awaitLoading(t){const e=this._promiseGroupIds++,r=new pe(w(this,H),{...t});this._newPromiseGroups.push(r);const n=performance.now();return r.ready.finally(()=>{const s=this._newPromiseGroups.indexOf(r);s>=0&&(this._newPromiseGroups.splice(s,1),Ve()&&performance.measure("LODsManager:awaitLoading",{start:n,detail:{id:e,name:t?.name,awaited:r.awaitedCount,resolved:r.resolvedCount}}))}),r.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let t=this._newPromiseGroups.length-1;t>=0;t--)this._newPromiseGroups[t].update(w(this,H))}addEventListener(t,e){t==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(t,e){if(t==="changed"){const r=this._lodchangedlisteners.indexOf(e);r>=0&&this._lodchangedlisteners.splice(r,1)}}enable(){if(w(this,K))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;q(this,K,this.renderer.render);const e=this;he(this.renderer),this.renderer.render=function(r,n){const s=e.renderer.getRenderTarget();(s==null||"isXRRenderTarget"in s&&s.isXRRenderTarget)&&(t=0,q(e,H,w(e,H)+1),q(e,le,w(e,Ee).getDelta()),q(e,ve,w(e,ve)+w(e,le)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/w(e,le)),q(e,Y,e._fpsBuffer.reduce((i,a)=>i+a)/e._fpsBuffer.length),F&&w(e,H)%200===0&&console.log("FPS",Math.round(w(e,Y)),"Interval:",w(e,B)));const o=t++;w(e,K).call(this,r,n),e.onAfterRender(r,n,o)}}disable(){w(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=w(this,K),q(this,K,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,r){if(this.pause)return;const n=this.renderer.renderLists.get(t,0).opaque;let s=!0;if(n.length===1){const o=n[0].material;(o.name==="EffectMaterial"||o.name==="CopyShader")&&(s=!1)}if((e.parent&&e.parent.type==="CubeCamera"||r>=1&&e.type==="OrthographicCamera")&&(s=!1),s){if(kt||(this.updateInterval==="auto"?w(this,Y)<40&&w(this,B)<10?(q(this,B,w(this,B)+1),F&&console.warn("\u2193 Reducing LOD updates",w(this,B),w(this,Y).toFixed(0))):w(this,Y)>=60&&w(this,B)>1&&(q(this,B,w(this,B)-1),F&&console.warn("\u2191 Increasing LOD updates",w(this,B),w(this,Y).toFixed(0))):q(this,B,this.updateInterval),w(this,B)>0&&w(this,H)%w(this,B)!=0))return;this.internalUpdate(t,e),this._postprocessPromiseGroups()}}internalUpdate(t,e){var r,n;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const u of o){if(u.material&&(((r=u.geometry)==null?void 0:r.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")){F&&(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",u,u.material.name,u.material.type)));continue}switch(u.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(F==="color"&&u.material&&!u.object.progressive_debug_color){u.object.progressive_debug_color=!0;const p=Math.random()*16777215,x=new ht({color:p});u.object.material=x}const m=u.object;(m instanceof J||m.isMesh)&&this.updateLODs(t,e,m,i)}const a=s.transparent;for(const u of a){const m=u.object;(m instanceof J||m.isMesh)&&this.updateLODs(t,e,m,i)}const d=s.transmissive;for(const u of d){const m=u.object;(m instanceof J||m.isMesh)&&this.updateLODs(t,e,m,i)}}updateLODs(t,e,r,n){var s,o;r.userData||(r.userData={});let i=r[Ie];if(i||(i=new It,r[Ie]=i),i.frames++<2)return;for(const d of V)(s=d.onBeforeUpdateLOD)==null||s.call(d,this.renderer,t,e,r);const a=A.overrideGlobalLodLevel!==void 0?A.overrideGlobalLodLevel:re;a>=0?(C.mesh_lod=a,C.texture_lod=a):(this.calculateLodLevel(e,r,i,n,C),C.mesh_lod=Math.round(C.mesh_lod),C.texture_lod=Math.round(C.texture_lod)),C.mesh_lod>=0&&this.loadProgressiveMeshes(r,C.mesh_lod),r.material&&C.texture_lod>=0&&this.loadProgressiveTextures(r.material,C.texture_lod,a),y&&r.material&&!r.isGizmo&&He(r.material);for(const d of V)(o=d.onAfterUpdatedLOD)==null||o.call(d,this.renderer,t,e,r,C);i.lastLodLevel_Mesh=C.mesh_lod,i.lastLodLevel_Texture=C.texture_lod}loadProgressiveTextures(t,e,r){if(!t)return;if(Array.isArray(t)){for(const s of t)this.loadProgressiveTextures(s,e);return}let n=!1;if((t[X]===void 0||e<t[X])&&(n=!0),r!==void 0&&r>=0&&(n=t[X]!=r,e=r),n){t[X]=e;const s=P.assignTextureLOD(t,e).then(o=>{this._lodchangedlisteners.forEach(i=>i({type:"texture",level:e,object:t}))});pe.addPromise("texture",t,s,this._newPromiseGroups)}}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let r=t[X]!==e;const n=t["DEBUG:LOD"];if(n!=null&&(r=t[X]!=n,e=n),r){t[X]=e;const s=t.geometry,o=P.assignMeshLOD(t,e).then(i=>(i&&t[X]==e&&s!=t.geometry&&this._lodchangedlisteners.forEach(a=>a({type:"mesh",level:e,object:t})),i));return pe.addPromise("mesh",t,o,this._newPromiseGroups),o}return Promise.resolve(null)}static isInside(t,e){const r=t.min,n=t.max,s=(r.x+n.x)*.5,o=(r.y+n.y)*.5;return this._tempPtInside.set(s,o,r.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,r,n,s){var o,i,a,d;if(!e){s.mesh_lod=-1,s.texture_lod=-1;return}if(!t){s.mesh_lod=-1,s.texture_lod=-1;return}let u=10+1,m=!1;if(F&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const p=(o=P.getMeshLODExtension(e.geometry))==null?void 0:o.lods,x=P.getPrimitiveIndex(e.geometry),S=p&&p.length>0,T=P.getMaterialMinMaxLODsCount(e.material),b=T.min_count!==1/0&&T.min_count>=0&&T.max_count>=0;if(!S&&!b){s.mesh_lod=0,s.texture_lod=0;return}S||(m=!0,u=0);const I=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let v=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const l=e;if(!l.boundingBox)l.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0){if(!l[A.$skinnedMeshBoundsOffset]){const _=A.skinnedMeshBoundsFrameOffsetCounter++;l[A.$skinnedMeshBoundsOffset]=_}const g=l[A.$skinnedMeshBoundsOffset];if((r.frames+g)%this.skinnedMeshAutoUpdateBoundsInterval===0){const _=se(l),M=l.geometry;_&&(l.geometry=_),l.computeBoundingBox(),l.geometry=M}}v=l.boundingBox}if(v){const l=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 h=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(h)){s.mesh_lod=0,s.texture_lod=0;return}}if(this._tempBox.copy(v),this._tempBox.applyMatrix4(e.matrixWorld),l.isPerspectiveCamera&&A.isInside(this._tempBox,this.projectionScreenMatrix)){s.mesh_lod=0,s.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&l.isPerspectiveCamera&&l.fov>70){const h=this._tempBox.min,f=this._tempBox.max;let O=h.x,$=h.y,G=f.x,ne=f.y;const ue=2,we=1.5,de=(h.x+f.x)*.5,ce=(h.y+f.y)*.5;O=(O-de)*ue+de,$=($-ce)*ue+ce,G=(G-de)*ue+de,ne=(ne-ce)*ue+ce;const tt=O<0&&G>0?0:Math.min(Math.abs(h.x),Math.abs(f.x)),rt=$<0&&ne>0?0:Math.min(Math.abs(h.y),Math.abs(f.y)),_e=Math.max(tt,rt);r.lastCentrality=(we-_e)*(we-_e)*(we-_e)}else r.lastCentrality=1;const g=this._tempBox.getSize(this._tempBoxSize);g.multiplyScalar(.5),screen.availHeight>0&&I>0&&g.multiplyScalar(I/screen.availHeight),t.isPerspectiveCamera?g.x*=t.aspect:t.isOrthographicCamera;const _=t.matrixWorldInverse,M=this._tempBox2;M.copy(v),M.applyMatrix4(e.matrixWorld),M.applyMatrix4(_);const k=M.getSize(this._tempBox2Size),E=Math.max(k.x,k.y);if(Math.max(g.x,g.y)!=0&&E!=0&&(g.z=k.z/Math.max(k.x,k.y)*Math.max(g.x,g.y)),r.lastScreenCoverage=Math.max(g.x,g.y,g.z),r.lastScreenspaceVolume.copy(g),r.lastScreenCoverage*=r.lastCentrality,F&&A.debugDrawLine){const h=this.tempMatrix.copy(this.projectionScreenMatrix);h.invert();const f=A.corner0,O=A.corner1,$=A.corner2,G=A.corner3;f.copy(this._tempBox.min),O.copy(this._tempBox.max),O.x=f.x,$.copy(this._tempBox.max),$.y=f.y,G.copy(this._tempBox.max);const ne=(f.z+G.z)*.5;f.z=O.z=$.z=G.z=ne,f.applyMatrix4(h),O.applyMatrix4(h),$.applyMatrix4(h),G.applyMatrix4(h),A.debugDrawLine(f,O,255),A.debugDrawLine(f,$,255),A.debugDrawLine(O,G,255),A.debugDrawLine($,G,255)}let L=999;if(p&&r.lastScreenCoverage>0)for(let h=0;h<p.length;h++){const f=p[h],O=(((i=f.densities)==null?void 0:i[x])||f.density||1e-5)/r.lastScreenCoverage;if(x>0&&Ve()&&!f.densities&&!globalThis["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"]&&(window["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"]=!0,console.warn("[Needle Progressive] Detected usage of mesh without primitive densities. This might cause incorrect LOD level selection: Consider re-optimizing your model by updating your Needle Integration, Needle glTF Pipeline or running optimization again on Needle Cloud.")),O<n){L=h;break}}L<u&&(u=L,m=!0)}if(m?s.mesh_lod=u:s.mesh_lod=r.lastLodLevel_Mesh,F&&s.mesh_lod!=r.lastLodLevel_Mesh){const l=p?.[s.mesh_lod];l&&console.debug(`Mesh LOD changed: ${r.lastLodLevel_Mesh} \u2192 ${s.mesh_lod} (density: ${(a=l.densities)==null?void 0:a[x].toFixed(0)}) | ${e.name}`)}if(b){const l="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(r.lastLodLevel_Texture<0){if(s.texture_lod=T.max_count-1,F){const g=T.lods[T.max_count-1];F&&console.log(`First Texture LOD ${s.texture_lod} (${g.max_height}px) - ${e.name}`)}}else{const g=r.lastScreenspaceVolume.x+r.lastScreenspaceVolume.y+r.lastScreenspaceVolume.z;let _=r.lastScreenCoverage*4;((d=this.context)==null?void 0:d.engine)==="model-viewer"&&(_*=1.5);const M=I/window.devicePixelRatio*_;let k=!1;for(let E=T.lods.length-1;E>=0;E--){const L=T.lods[E];if(!(l&&L.max_height>=2048)&&!(qe()&&L.max_height>4096)&&(L.max_height>M||!k&&E===0)){if(k=!0,s.texture_lod=E,F&&s.texture_lod<r.lastLodLevel_Texture){const h=L.max_height;console.log(`Texture LOD changed: ${r.lastLodLevel_Texture} \u2192 ${s.texture_lod} = ${h}px
|
|
6
|
+
Screensize: ${M.toFixed(0)}px, Coverage: ${(100*r.lastScreenCoverage).toFixed(2)}%, Volume ${g.toFixed(1)}
|
|
7
|
+
${e.name}`)}break}}}}else s.texture_lod=0}};let R=A;B=new WeakMap,K=new WeakMap,Ee=new WeakMap,H=new WeakMap,le=new WeakMap,ve=new WeakMap,Y=new WeakMap,c(R,"debugDrawLine"),c(R,"overrideGlobalLodLevel"),c(R,"corner0",new N),c(R,"corner1",new N),c(R,"corner2",new N),c(R,"corner3",new N),c(R,"_tempPtInside",new N),c(R,"skinnedMeshBoundsFrameOffsetCounter",0),c(R,"$skinnedMeshBoundsOffset",Symbol("gltf-progressive-skinnedMeshBoundsOffset"));class It{constructor(){c(this,"frames",0),c(this,"lastLodLevel_Mesh",-1),c(this,"lastLodLevel_Texture",-1),c(this,"lastScreenCoverage",0),c(this,"lastScreenspaceVolume",new N),c(this,"lastCentrality",0)}}const Ye=Symbol("NEEDLE_mesh_lod"),xe=Symbol("NEEDLE_texture_lod");let ye=null;function Ce(){const t=Et();t&&(t.mapURLs(function(e){return Qe(),e}),Qe(),ye?.disconnect(),ye=new MutationObserver(e=>{e.forEach(r=>{r.addedNodes.forEach(n=>{n instanceof HTMLElement&&n.tagName.toLowerCase()==="model-viewer"&&Ze(n)})})}),ye.observe(document,{childList:!0,subtree:!0}))}function Et(){return typeof customElements>"u"?null:customElements.get("model-viewer")||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Ce()}),null)}function Qe(){typeof document>"u"||document.querySelectorAll("model-viewer").forEach(t=>{Ze(t)})}const Je=new WeakSet;let Ct=0;function Ze(t){if(!t||Je.has(t))return null;Je.add(t),console.debug("[gltf-progressive] found new model-viewer..."+ ++Ct+`
|
|
8
|
+
`,t.getAttribute("src"));let e=null,r=null,n=null;for(let s=t;s!=null;s=Object.getPrototypeOf(s)){const o=Object.getOwnPropertySymbols(s),i=o.find(u=>u.toString()=="Symbol(renderer)"),a=o.find(u=>u.toString()=="Symbol(scene)"),d=o.find(u=>u.toString()=="Symbol(needsRender)");!e&&i!=null&&(e=t[i].threeRenderer),!r&&a!=null&&(r=t[a]),!n&&d!=null&&(n=t[d])}if(e&&r){let s=function(){if(n){let i=0,a=setInterval(()=>{if(i++>5){clearInterval(a);return}n?.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const o=R.get(e,{engine:"model-viewer"});return R.addPlugin(new Bt),o.enable(),o.addEventListener("changed",()=>{n?.call(t)}),t.addEventListener("model-visibility",i=>{i.detail.visible&&n?.call(t)}),t.addEventListener("load",()=>{s()}),()=>{o.disable()}}return null}class Bt{constructor(){c(this,"_didWarnAboutMissingUrl",!1)}onBeforeUpdateLOD(e,r,n,s){this.tryParseMeshLOD(r,s),this.tryParseTextureLOD(r,s)}getUrl(e){if(!e)return null;let r=e.getAttribute("src");return r||(r=e.src),r||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),r}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,r){if(r[xe]==!0)return;r[xe]=!0;const n=this.tryGetCurrentGLTF(e),s=this.tryGetCurrentModelViewer(e),o=this.getUrl(s);if(o&&n&&r.material){let i=function(d){var u,m,p;if(d[xe]==!0)return;d[xe]=!0,d.userData&&(d.userData.LOD=-1);const x=Object.keys(d);for(let S=0;S<x.length;S++){const T=x[S],b=d[T];if(b?.isTexture===!0){const I=(m=(u=b.userData)==null?void 0:u.associations)==null?void 0:m.textures;if(I==null)continue;const v=n.parser.json.textures[I];if(!v){console.warn("Texture data not found for texture index "+I);continue}if((p=v?.extensions)!=null&&p[z]){const l=v.extensions[z];l&&o&&P.registerTexture(o,b,l.lods.length,I,l)}}}};const a=r.material;if(Array.isArray(a))for(const d of a)i(d);else i(a)}}tryParseMeshLOD(e,r){var n,s;if(r[Ye]==!0)return;r[Ye]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const a=(s=(n=r.userData)==null?void 0:n.gltfExtensions)==null?void 0:s[z];if(a&&i){const d=r.uuid;P.registerMesh(i,d,r,0,a.lods.length,a)}}}function et(t,e,r,n){he(e),Me(r),Se(r,{progressive:!0,...n?.hints}),r.register(o=>new P(o,t));const s=R.get(e);return n?.enableLODsManager!==!1&&s.enable(),s}if(Ce(),!Lt){const t={gltfProgressive:{useNeedleProgressive:et,LODsManager:R,configureLoader:Se,getRaycastMesh:se,useRaycastMeshes:Ke}};if(!globalThis.Needle)globalThis.Needle=t;else for(const e in t)globalThis.Needle[e]=t[e]}export{z as EXTENSION_NAME,R as LODsManager,P as NEEDLE_progressive,je as VERSION,Me as addDracoAndKTX2Loaders,Se as configureLoader,he as createLoaders,se as getRaycastMesh,Ce as patchModelViewer,Xe as registerRaycastMesh,We as setDracoDecoderLocation,Ue as setKTX2TranscoderLocation,et as useNeedleProgressive,Ke as useRaycastMeshes};
|
package/gltf-progressive.umd.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var je=Object.defineProperty;var He=(o,t,e)=>t in o?je(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var u=(o,t,e)=>(He(o,typeof t!="symbol"?t+"":t,e),e),Ee=(o,t,e)=>{if(!t.has(o))throw TypeError("Cannot "+e)};var L=(o,t,e)=>(Ee(o,t,"read from private field"),e?e.call(o):t.get(o)),j=(o,t,e)=>{if(t.has(o))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(o):t.set(o,e)},z=(o,t,e,s)=>(Ee(o,t,"write to private field"),s?s.call(o,e):t.set(o,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("three"),Pe=require("three/examples/jsm/loaders/GLTFLoader.js"),Qe=require("three/examples/jsm/libs/meshopt_decoder.module.js"),Je=require("three/examples/jsm/loaders/DRACOLoader.js"),Ze=require("three/examples/jsm/loaders/KTX2Loader.js"),Re="";globalThis.GLTF_PROGRESSIVE_VERSION=Re;console.debug("[gltf-progressive] version -");let X="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",se="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const et=X,tt=se,Ie=new URL(X+"draco_decoder.js");Ie.searchParams.append("range","true");fetch(Ie,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(o=>{console.debug(`Failed to fetch remote Draco decoder from ${X} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),X===et&&Fe("./include/draco/"),se===tt&&Ne("./include/ktx2/")}).finally(()=>{Ue()});function Fe(o){X=o,I&&I[Oe]!=X?(console.debug("Updating Draco decoder path to "+o),I[Oe]=X,I.setDecoderPath(X),I.preload()):console.debug("Setting Draco decoder path to "+o)}function Ne(o){se=o,U&&U.transcoderPath!=se?(console.debug("Updating KTX2 transcoder path to "+o),U.setTranscoderPath(se),U.init()):console.debug("Setting KTX2 transcoder path to "+o)}const Oe=Symbol("dracoDecoderPath");let I,me,U;function Ue(){I||(I=new Je.DRACOLoader,I[Oe]=X,I.setDecoderPath(X),I.setDecoderConfig({type:"js"}),I.preload()),U||(U=new Ze.KTX2Loader,U.setTranscoderPath(se),U.init()),me||(me=Qe.MeshoptDecoder)}function Te(o){return Ue(),o?U.detectSupport(o):o!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:I,ktx2Loader:U,meshoptDecoder:me}}function Ae(o){o.dracoLoader||o.setDRACOLoader(I),o.ktx2Loader||o.setKTX2Loader(U),o.meshoptDecoder||o.setMeshoptDecoder(me)}const be=new WeakMap;function Ce(o,t){let e=be.get(o);e?e=Object.assign(e,t):e=t,be.set(o,e)}const we=Pe.GLTFLoader.prototype.load;function st(...o){const t=be.get(this);let e=o[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 o[0]=e,we==null?void 0:we.call(this,...o)}Pe.GLTFLoader.prototype.load=st;ue("debugprogressive");function ue(o){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(o);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function rt(o,t){if(t===void 0||t.startsWith("./")||t.startsWith("http")||o===void 0)return t;const e=o.lastIndexOf("/");if(e>=0){const s=o.substring(0,e+1);for(;s.endsWith("/")&&t.startsWith("/");)t=t.substring(1);return s+t}return t}let oe;function it(){return oe!==void 0||(oe=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ue("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",oe)),oe}function nt(){if(typeof window>"u")return!1;const o=new URL(window.location.href),t=o.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(o.hostname);return o.hostname==="127.0.0.1"||t}class ot{constructor(t=100,e={}){u(this,"_running",new Map);u(this,"_queue",[]);u(this,"debug",!1);u(this,"tick",()=>{this.internalUpdate(),setTimeout(this.tick,10)});this.maxConcurrent=t,this.debug=e.debug??!1,window.requestAnimationFrame(this.tick)}slot(t){return this.debug&&console.debug(`[PromiseQueue]: Requesting slot for key ${t}, running: ${this._running.size}, waiting: ${this._queue.length}`),new Promise(e=>{this._queue.push({key:t,resolve:e})})}add(t,e){this._running.has(t)||(this._running.set(t,e),e.finally(()=>{this._running.delete(t),this.debug&&console.debug(`[PromiseQueue]: Promise for key ${t} finished, running: ${this._running.size}, waiting: ${this._queue.length}`)}),this.debug&&console.debug(`[PromiseQueue]: Adding promise for key ${t}, running: ${this._running.size}, waiting: ${this._queue.length}`))}internalUpdate(){const t=this.maxConcurrent-this._running.size;for(let e=0;e<t&&this._queue.length>0;e++){this.debug&&console.debug(`[PromiseQueue]: Running ${this._running.size} promises, waiting for ${this._queue.length} more.`);const{key:s,resolve:n}=this._queue.shift();n({use:r=>this.add(s,r)})}}}const at=typeof window>"u"&&typeof document>"u",Se=Symbol("needle:raycast-mesh");function ie(o){return(o==null?void 0:o[Se])instanceof y.BufferGeometry?o[Se]:null}function Ve(o,t){if((o.type==="Mesh"||o.type==="SkinnedMesh")&&!ie(o)){const s=lt(t);s.userData={isRaycastMesh:!0},o[Se]=s}}function qe(o=!0){if(o){if(ae)return;const t=ae=y.Mesh.prototype.raycast;y.Mesh.prototype.raycast=function(e,s){const n=this,r=ie(n);let i;r&&n.isMesh&&(i=n.geometry,n.geometry=r),t.call(this,e,s),i&&(n.geometry=i)}}else{if(!ae)return;y.Mesh.prototype.raycast=ae,ae=null}}let ae=null;function lt(o){const t=new y.BufferGeometry;for(const e in o.attributes)t.setAttribute(e,o.getAttribute(e));return t.setIndex(o.getIndex()),t}const Q=new Array,x=ue("debugprogressive");let ge,te=-1;if(x){let o=function(){te+=1,te>=t&&(te=-1),console.log(`Toggle LOD level [${te}]`)},t=6;window.addEventListener("keyup",e=>{e.key==="p"&&o(),e.key==="w"&&(ge=!ge,console.log(`Toggle wireframe [${ge}]`));const s=parseInt(e.key);!isNaN(s)&&s>=0&&(te=s,console.log(`Set LOD level to [${te}]`))})}function ze(o){if(x)if(Array.isArray(o))for(const t of o)ze(t);else o&&"wireframe"in o&&(o.wireframe=ge===!0)}const W="NEEDLE_progressive",Me=Symbol("needle-progressive-texture"),O=class{constructor(t,e){u(this,"parser");u(this,"url");u(this,"_isLoadingMesh");u(this,"loadMesh",t=>{var s,n;if(this._isLoadingMesh)return null;const e=(n=(s=this.parser.json.meshes[t])==null?void 0:s.extensions)==null?void 0:n[W];return e?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",t).then(r=>{var i;return this._isLoadingMesh=!1,r&&O.registerMesh(this.url,e.guid,r,(i=e.lods)==null?void 0:i.length,0,e),r})):null});x&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return W}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,n="LODS:minmax",r=t[n];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[n]=e,e}if(x==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const a=t;for(const l of Object.keys(a.uniforms)){const c=a.uniforms[l].value;(c==null?void 0:c.isTexture)===!0&&i(c,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[n]=e,e;function i(a,l){const c=s.getAssignedLODInformation(a);if(c){const d=s.lodInfos.get(c.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 p=0;p<d.lods.length;p++){const f=d.lods[p];f.width&&(l.lods[p]=l.lods[p]||{min_height:1/0,max_height:0},l.lods[p].min_height=Math.min(l.lods[p].min_height,f.height),l.lods[p].max_height=Math.max(l.lods[p].max_height,f.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,n;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(n=this.lodInfos.get(i.key),e===void 0)return n!=null;if(n)return Array.isArray(n.lods)?e<n.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof y.Mesh||t.isMesh===!0){const n=t.geometry,r=this.getAssignedLODInformation(n);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,O.getOrLoadLOD(n,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&&n!=i&&((i==null?void 0:i.isBufferGeometry)?t.geometry=i:x&&console.error("Invalid LOD geometry",i))),i}).catch(i=>(console.error("Error loading mesh LOD",t,i),null))}else x&&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 n=new Array;for(const r of s.material){const i=this.assignTextureLOD(r,e);n.push(i)}return Promise.all(n).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,n=[],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 c=this.assignTextureLODForSlot(l,e,s,a).then(d=>(d&&i.uniforms[a].value!=d&&(i.uniforms[a].value=d,i.uniformsNeedUpdate=!0),d));n.push(c),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);n.push(l),r.push(i)}}return Promise.all(n).then(i=>{const a=new Array;for(let l=0;l<i.length;l++){const c=i[l],d=r[l];c&&c.isTexture===!0?a.push({material:s,slot:d,texture:c,level:e}):a.push({material:s,slot:d,texture:null,level:e})}return a})}if(t instanceof y.Texture||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,n){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(t):O.getOrLoadLOD(t,e).then(r=>{if(Array.isArray(r))return null;if((r==null?void 0:r.isTexture)===!0){if(r!=t&&s&&n){const i=s[n];if(i&&!x){const a=this.getAssignedLODInformation(i);if(a&&(a==null?void 0:a.level)<e)return x==="verbose"&&console.warn("Assigned texture level is already higher: ",a.level,e,s,i,r),null}s[n]=r}return r}else x=="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 x&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((n,r)=>{var i;if(n!=null&&n.extensions){const a=n==null?void 0:n.extensions[W];if(a){if(!a.lods){x&&console.warn("Texture has no LODs",a);return}let l=!1;for(const c of this.parser.associations.keys())if(c.isTexture===!0){const d=this.parser.associations.get(c);(d==null?void 0:d.textures)===r&&(l=!0,O.registerTexture(this.url,c,(i=a.lods)==null?void 0:i.length,r,a))}l||this.parser.getDependency("texture",r).then(c=>{var d;c&&O.registerTexture(this.url,c,(d=a.lods)==null?void 0:d.length,r,a)})}}}),(s=this.parser.json.meshes)==null||s.forEach((n,r)=>{if(n!=null&&n.extensions){const i=n==null?void 0:n.extensions[W];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&&O.registerMesh(this.url,i.guid,a,i.lods.length,l.primitives,i)}}}}),null}static async getOrLoadLOD(t,e){var a,l,c,d;const s=x=="verbose",n=t.userData.LODS;if(!n)return null;const r=n==null?void 0:n.key;let i;if(t.isTexture===!0){const p=t;p.source&&p.source[Me]&&(i=p.source[Me])}if(i||(i=O.lodInfos.get(r)),i){if(e>0){let M=!1;const D=Array.isArray(i.lods);if(D&&e>=i.lods.length?M=!0:D||(M=!0),M)return this.lowresCache.get(r)}const p=Array.isArray(i.lods)?(a=i.lods[e])==null?void 0:a.path:i.lods;if(!p)return x&&!i["missing:uri"]&&(i["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,i)),null;const f=rt(n.url,p);if(f.endsWith(".glb")||f.endsWith(".gltf")){if(!i.guid)return console.warn("missing pointer for glb/gltf texture",i),null;const M=f+"_"+i.guid,D=this.previouslyLoaded.get(M);if(D!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${M}`);let h=await D.catch(G=>(console.error(`Error loading LOD ${e} from ${f}
|
|
2
|
-
`,
|
|
3
|
-
`,
|
|
4
|
-
`,
|
|
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}};let T=O;u(T,"registerTexture",(t,e,s,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[Me]=r);const i=r.guid;O.assignLODInformation(t,e,i,s,n),O.lodInfos.set(i,r),O.lowresCache.set(i,e)}),u(T,"registerMesh",(t,e,s,n,r,i)=>{var c;const a=s.geometry;if(!a){x&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),x&&console.log("> Progressive: register mesh "+s.name,{index:r,uuid:s.uuid},i,s),O.assignLODInformation(t,a,e,n,r),O.lodInfos.set(e,i);let l=O.lowresCache.get(e);l?l.push(s.geometry):l=[s.geometry],O.lowresCache.set(e,l),n>0&&!ie(s)&&Ve(s,a);for(const d of Q)(c=d.onRegisteredNewMesh)==null||c.call(d,s,i)}),u(T,"lodInfos",new Map),u(T,"previouslyLoaded",new Map),u(T,"lowresCache",new Map),u(T,"queue",new ot(100,{debug:x!=!1}));class ut{constructor(t,e,s,n){u(this,"url");u(this,"key");u(this,"level");u(this,"index");this.url=t,this.key=e,this.level=s,n!=null&&(this.index=n)}}class pe{constructor(t,e){u(this,"frame_start");u(this,"frame_capture_end");u(this,"ready");u(this,"_resolve");u(this,"_signal");u(this,"_resolved",!1);u(this,"_addedCount",0);u(this,"_resolvedCount",0);u(this,"_awaiting",[]);u(this,"_maxPromisesPerObject",1);u(this,"_currentFrame",0);u(this,"_seen",new WeakMap);var r;const n=Math.max(e.frames??2,2);this.frame_start=t,this.frame_capture_end=t+n,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){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(e)){let n=this._seen.get(e);if(n>=this._maxPromisesPerObject){x&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(e,n+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})}}u(pe,"addPromise",(t,e,s,n)=>{n.forEach(r=>{r.add(t,e,s)})});const N=ue("debugprogressive"),dt=ue("noprogressive"),ve=Symbol("Needle:LODSManager"),De=Symbol("Needle:LODState"),H=Symbol("Needle:CurrentLOD"),k={mesh_lod:-1,texture_lod:-1};var R,K,ye,J,re,Le,Y;const b=class{constructor(t,e){u(this,"renderer");u(this,"context");u(this,"projectionScreenMatrix",new y.Matrix4);u(this,"targetTriangleDensity",2e5);u(this,"skinnedMeshAutoUpdateBoundsInterval",30);u(this,"updateInterval","auto");j(this,R,1);u(this,"pause",!1);u(this,"manual",!1);u(this,"_newPromiseGroups",[]);u(this,"_promiseGroupIds",0);u(this,"_lodchangedlisteners",[]);j(this,K,void 0);j(this,ye,new y.Clock);j(this,J,0);j(this,re,0);j(this,Le,0);j(this,Y,0);u(this,"_fpsBuffer",[60,60,60,60,60]);u(this,"_sphere",new y.Sphere);u(this,"_tempBox",new y.Box3);u(this,"_tempBox2",new y.Box3);u(this,"tempMatrix",new y.Matrix4);u(this,"_tempWorldPosition",new y.Vector3);u(this,"_tempBoxSize",new y.Vector3);u(this,"_tempBox2Size",new y.Vector3);this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[De]}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 b(t,{engine:"unknown",...e});return t[ve]=s,s}get plugins(){return Q}awaitLoading(t){const e=this._promiseGroupIds++,s=new pe(L(this,J),{...t});this._newPromiseGroups.push(s);const n=performance.now();return s.ready.finally(()=>{const r=this._newPromiseGroups.indexOf(s);r>=0&&(this._newPromiseGroups.splice(r,1),nt()&&performance.measure("LODsManager:awaitLoading",{start:n,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(L(this,J))}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(L(this,K))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;z(this,K,this.renderer.render);const e=this;Te(this.renderer),this.renderer.render=function(s,n){const r=e.renderer.getRenderTarget();(r==null||"isXRRenderTarget"in r&&r.isXRRenderTarget)&&(t=0,z(e,J,L(e,J)+1),z(e,re,L(e,ye).getDelta()),z(e,Le,L(e,Le)+L(e,re)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/L(e,re)),z(e,Y,e._fpsBuffer.reduce((a,l)=>a+l)/e._fpsBuffer.length),N&&L(e,J)%200===0&&console.log("FPS",Math.round(L(e,Y)),"Interval:",L(e,R)));const i=t++;L(e,K).call(this,s,n),e.onAfterRender(s,n,i)}}disable(){L(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=L(this,K),z(this,K,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(dt||(this.updateInterval==="auto"?L(this,Y)<40&&L(this,R)<10?(z(this,R,L(this,R)+1),N&&console.warn("↓ Reducing LOD updates",L(this,R),L(this,Y).toFixed(0))):L(this,Y)>=60&&L(this,R)>1&&(z(this,R,L(this,R)-1),N&&console.warn("↑ Increasing LOD updates",L(this,R),L(this,Y).toFixed(0))):z(this,R,this.updateInterval),L(this,R)>0&&L(this,J)%L(this,R)!=0))return;this.internalUpdate(t,e),this._postprocessPromiseGroups()}}internalUpdate(t,e){var l,c;const s=this.renderer.renderLists.get(t,0),n=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const r=this.targetTriangleDensity;for(const d of n){if(d.material&&(((l=d.geometry)==null?void 0:l.type)==="BoxGeometry"||((c=d.geometry)==null?void 0:c.type)==="BufferGeometry")&&(d.material.name==="SphericalGaussianBlur"||d.material.name=="BackgroundCubeMaterial"||d.material.name==="CubemapFromEquirect"||d.material.name==="EquirectangularToCubeUV")){N&&(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(N==="color"&&d.material&&!d.object.progressive_debug_color){d.object.progressive_debug_color=!0;const f=Math.random()*16777215,M=new y.MeshStandardMaterial({color:f});d.object.material=M}const p=d.object;(p instanceof y.Mesh||p.isMesh)&&this.updateLODs(t,e,p,r)}const i=s.transparent;for(const d of i){const p=d.object;(p instanceof y.Mesh||p.isMesh)&&this.updateLODs(t,e,p,r)}const a=s.transmissive;for(const d of a){const p=d.object;(p instanceof y.Mesh||p.isMesh)&&this.updateLODs(t,e,p,r)}}updateLODs(t,e,s,n){var a,l;s.userData||(s.userData={});let r=s[De];if(r||(r=new ct,s[De]=r),r.frames++<2)return;for(const c of Q)(a=c.onBeforeUpdateLOD)==null||a.call(c,this.renderer,t,e,s);const i=b.overrideGlobalLodLevel!==void 0?b.overrideGlobalLodLevel:te;i>=0?(k.mesh_lod=i,k.texture_lod=i):(this.calculateLodLevel(e,s,r,n,k),k.mesh_lod=Math.round(k.mesh_lod),k.texture_lod=Math.round(k.texture_lod)),k.mesh_lod>=0&&this.loadProgressiveMeshes(s,k.mesh_lod),s.material&&k.texture_lod>=0&&this.loadProgressiveTextures(s.material,k.texture_lod),x&&s.material&&!s.isGizmo&&ze(s.material);for(const c of Q)(l=c.onAfterUpdatedLOD)==null||l.call(c,this.renderer,t,e,s,k);r.lastLodLevel_Mesh=k.mesh_lod,r.lastLodLevel_Texture=k.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[H]===void 0||e<t[H])&&(s=!0);const n=t["DEBUG:LOD"];if(n!=null&&(s=t[H]!=n,e=n),s){t[H]=e;const r=T.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(a=>a({type:"texture",level:e,object:t}))});pe.addPromise("texture",t,r,this._newPromiseGroups)}}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[H]!==e;const n=t["DEBUG:LOD"];if(n!=null&&(s=t[H]!=n,e=n),s){t[H]=e;const r=t.geometry,i=T.assignMeshLOD(t,e).then(a=>(a&&t[H]==e&&r!=t.geometry&&this._lodchangedlisteners.forEach(l=>l({type:"mesh",level:e,object:t})),a));return pe.addPromise("mesh",t,i,this._newPromiseGroups),i}return Promise.resolve(null)}static isInside(t,e){const s=t.min,n=t.max,r=(s.x+n.x)*.5,i=(s.y+n.y)*.5;return this._tempPtInside.set(r,i,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,n,r){var S,F,Z;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(N&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const c=(S=T.getMeshLODExtension(e.geometry))==null?void 0:S.lods,d=T.getPrimitiveIndex(e.geometry),p=c&&c.length>0,f=T.getMaterialMinMaxLODsCount(e.material),M=(f==null?void 0:f.min_count)!=1/0&&f.min_count>0&&f.max_count>0;if(!p&&!M){r.mesh_lod=0,r.texture_lod=0;return}p||(l=!0,a=0);const D=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let C=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const h=e;if(!h.boundingBox)h.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0){if(!h[b.$skinnedMeshBoundsOffset]){const G=b.skinnedMeshBoundsFrameOffsetCounter++;h[b.$skinnedMeshBoundsOffset]=G}const _=h[b.$skinnedMeshBoundsOffset];if((s.frames+_)%this.skinnedMeshAutoUpdateBoundsInterval===0){const G=ie(h),V=h.geometry;G&&(h.geometry=G),h.computeBoundingBox(),h.geometry=V}}C=h.boundingBox}if(C){const h=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 g=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(g)){r.mesh_lod=0,r.texture_lod=0;return}}if(this._tempBox.copy(C),this._tempBox.applyMatrix4(e.matrixWorld),h.isPerspectiveCamera&&b.isInside(this._tempBox,this.projectionScreenMatrix)){r.mesh_lod=0,r.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&h.isPerspectiveCamera&&h.fov>70){const g=this._tempBox.min,w=this._tempBox.max;let B=g.x,$=g.y,q=w.x,ne=w.y;const de=2,xe=1.5,ce=(g.x+w.x)*.5,fe=(g.y+w.y)*.5;B=(B-ce)*de+ce,$=($-fe)*de+fe,q=(q-ce)*de+ce,ne=(ne-fe)*de+fe;const Ke=B<0&&q>0?0:Math.min(Math.abs(g.x),Math.abs(w.x)),Ye=$<0&&ne>0?0:Math.min(Math.abs(g.y),Math.abs(w.y)),_e=Math.max(Ke,Ye);s.lastCentrality=(xe-_e)*(xe-_e)*(xe-_e)}else s.lastCentrality=1;const _=this._tempBox.getSize(this._tempBoxSize);_.multiplyScalar(.5),screen.availHeight>0&&D>0&&_.multiplyScalar(D/screen.availHeight),t.isPerspectiveCamera?_.x*=t.aspect:t.isOrthographicCamera;const G=t.matrixWorldInverse,V=this._tempBox2;V.copy(C),V.applyMatrix4(e.matrixWorld),V.applyMatrix4(G);const P=V.getSize(this._tempBox2Size),ee=Math.max(P.x,P.y);if(Math.max(_.x,_.y)!=0&&ee!=0&&(_.z=P.z/Math.max(P.x,P.y)*Math.max(_.x,_.y)),s.lastScreenCoverage=Math.max(_.x,_.y,_.z),s.lastScreenspaceVolume.copy(_),s.lastScreenCoverage*=s.lastCentrality,N&&b.debugDrawLine){const g=this.tempMatrix.copy(this.projectionScreenMatrix);g.invert();const w=b.corner0,B=b.corner1,$=b.corner2,q=b.corner3;w.copy(this._tempBox.min),B.copy(this._tempBox.max),B.x=w.x,$.copy(this._tempBox.max),$.y=w.y,q.copy(this._tempBox.max);const ne=(w.z+q.z)*.5;w.z=B.z=$.z=q.z=ne,w.applyMatrix4(g),B.applyMatrix4(g),$.applyMatrix4(g),q.applyMatrix4(g),b.debugDrawLine(w,B,255),b.debugDrawLine(w,$,255),b.debugDrawLine(B,q,255),b.debugDrawLine($,q,255)}let v=999;if(c&&s.lastScreenCoverage>0)for(let g=0;g<c.length;g++){const w=c[g];if((((F=w.densities)==null?void 0:F[d])||w.density||1e-5)/s.lastScreenCoverage<n){v=g;break}}v<a&&(a=v,l=!0)}if(l?r.mesh_lod=a:r.mesh_lod=s.lastLodLevel_Mesh,N&&r.mesh_lod!=s.lastLodLevel_Mesh){const _=c==null?void 0:c[r.mesh_lod];_&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${r.mesh_lod} (${_.density.toFixed(0)}) - ${e.name}`)}if(M){const h="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(r.texture_lod=f.max_count-1,N){const _=f.lods[f.max_count-1];N&&console.log(`First Texture LOD ${r.texture_lod} (${_.max_height}px) - ${e.name}`)}}else{const _=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let G=s.lastScreenCoverage*4;((Z=this.context)==null?void 0:Z.engine)==="model-viewer"&&(G*=1.5);const P=D/window.devicePixelRatio*G;let ee=!1;for(let E=f.lods.length-1;E>=0;E--){const v=f.lods[E];if(!(h&&v.max_height>=2048)&&!(it()&&v.max_height>4096)&&(v.max_height>P||!ee&&E===0)){if(ee=!0,r.texture_lod=E,r.texture_lod<s.lastLodLevel_Texture){const m=v.max_height;N&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${r.texture_lod} = ${m}px
|
|
6
|
-
Screensize: ${
|
|
7
|
-
${
|
|
8
|
-
`,
|
|
1
|
+
"use strict";var rt=Object.create;var ve=Object.defineProperty;var st=Object.getOwnPropertyDescriptor;var nt=Object.getOwnPropertyNames;var it=Object.getPrototypeOf,ot=Object.prototype.hasOwnProperty;var at=(i,e,t)=>e in i?ve(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var lt=(i,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of nt(e))!ot.call(i,n)&&n!==t&&ve(i,n,{get:()=>e[n],enumerable:!(r=st(e,n))||r.enumerable});return i};var ut=(i,e,t)=>(t=i!=null?rt(it(i)):{},lt(e||!i||!i.__esModule?ve(t,"default",{value:i,enumerable:!0}):t,i));var d=(i,e,t)=>(at(i,typeof e!="symbol"?e+"":e,t),t),Ee=(i,e,t)=>{if(!e.has(i))throw TypeError("Cannot "+t)};var b=(i,e,t)=>(Ee(i,e,"read from private field"),t?t.call(i):e.get(i)),Q=(i,e,t)=>{if(e.has(i))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(i):e.set(i,t)},K=(i,e,t,r)=>(Ee(i,e,"write to private field"),r?r.call(i,t):e.set(i,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("three"),Ie=require("three/examples/jsm/loaders/GLTFLoader.js"),dt=require("three/examples/jsm/libs/meshopt_decoder.module.js"),ct=require("three/examples/jsm/loaders/DRACOLoader.js"),ft=require("three/examples/jsm/loaders/KTX2Loader.js"),ze="";globalThis.GLTF_PROGRESSIVE_VERSION=ze;console.debug("[gltf-progressive] version -");let V="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",se="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const ht=V,gt=se,Ve=new URL(V+"draco_decoder.js");Ve.searchParams.append("range","true");fetch(Ve,{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===ht&&qe("./include/draco/"),se===gt&&Xe("./include/ktx2/")}).finally(()=>{Ke()});const pt=()=>({dracoDecoderPath:V,ktx2TranscoderPath:se});function qe(i){V=i,U&&U[Ae]!=V?(console.debug("Updating Draco decoder path to "+i),U[Ae]=V,U.setDecoderPath(V),U.preload()):console.debug("Setting Draco decoder path to "+i)}function Xe(i){se=i,q&&q.transcoderPath!=se?(console.debug("Updating KTX2 transcoder path to "+i),q.setTranscoderPath(se),q.init()):console.debug("Setting KTX2 transcoder path to "+i)}function _e(i){return Ke(),i?q.detectSupport(i):i!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:U,ktx2Loader:q,meshoptDecoder:xe}}function Be(i){i.dracoLoader||i.setDRACOLoader(U),i.ktx2Loader||i.setKTX2Loader(q),i.meshoptDecoder||i.setMeshoptDecoder(xe)}const Ae=Symbol("dracoDecoderPath");let U,xe,q;function Ke(){U||(U=new ct.DRACOLoader,U[Ae]=V,U.setDecoderPath(V),U.setDecoderConfig({type:"js"}),U.preload()),q||(q=new ft.KTX2Loader,q.setTranscoderPath(se),q.init()),xe||(xe=dt.MeshoptDecoder)}const Ce=new WeakMap;function Re(i,e){let t=Ce.get(i);t?t=Object.assign(t,e):t=e,Ce.set(i,t)}const be=Ie.GLTFLoader.prototype.load;function mt(...i){const e=Ce.get(this);let t=i[0];const r=new URL(t,window.location.href);if(r.hostname.endsWith("needle.tools")){const s=(e==null?void 0:e.progressive)!==void 0?e.progressive:!0,o=e!=null&&e.usecase?e.usecase:"default";s?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${o}`:this.requestHeader.Accept=`*/*;usecase=${o}`,t=r.toString()}return i[0]=t,be==null?void 0:be.call(this,...i)}Ie.GLTFLoader.prototype.load=mt;ne("debugprogressive");function ne(i){if(typeof window>"u")return!1;const t=new URL(window.location.href).searchParams.get(i);return t==null||t==="0"||t==="false"?!1:t===""?!0:t}function yt(i,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||i===void 0)return e;const t=i.lastIndexOf("/");if(t>=0){const r=i.substring(0,t+1);for(;r.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return r+e}return e}let le;function je(){return le!==void 0||(le=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ne("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",le)),le}function Fe(){if(typeof window>"u")return!1;const i=new URL(window.location.href),e=i.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(i.hostname);return i.hostname==="127.0.0.1"||e}class xt{constructor(e=100,t={}){d(this,"_running",new Map);d(this,"_queue",[]);d(this,"debug",!1);d(this,"tick",()=>{this.internalUpdate(),setTimeout(this.tick,10)});this.maxConcurrent=e,this.debug=t.debug??!1,window.requestAnimationFrame(this.tick)}slot(e){return this.debug&&console.debug(`[PromiseQueue]: Requesting slot for key ${e}, running: ${this._running.size}, waiting: ${this._queue.length}`),new Promise(t=>{this._queue.push({key:e,resolve:t})})}add(e,t){this._running.has(e)||(this._running.set(e,t),t.finally(()=>{this._running.delete(e),this.debug&&console.debug(`[PromiseQueue]: Promise finished now running: ${this._running.size}, waiting: ${this._queue.length}. (finished ${e})`)}),this.debug&&console.debug(`[PromiseQueue]: Added new promise, now running: ${this._running.size}, waiting: ${this._queue.length}. (added ${e})`))}internalUpdate(){const e=this.maxConcurrent-this._running.size;for(let t=0;t<e&&this._queue.length>0;t++){this.debug&&console.debug(`[PromiseQueue]: Running ${this._running.size} promises, waiting for ${this._queue.length} more.`);const{key:r,resolve:n}=this._queue.shift();n({use:s=>this.add(r,s)})}}}const wt=typeof window>"u"&&typeof document>"u",ke=Symbol("needle:raycast-mesh");function oe(i){return(i==null?void 0:i[ke])instanceof g.BufferGeometry?i[ke]:null}function Ye(i,e){if((i.type==="Mesh"||i.type==="SkinnedMesh")&&!oe(i)){const r=Lt(e);r.userData={isRaycastMesh:!0},i[ke]=r}}function He(i=!0){if(i){if(ue)return;const e=ue=g.Mesh.prototype.raycast;g.Mesh.prototype.raycast=function(t,r){const n=this,s=oe(n);let o;s&&n.isMesh&&(o=n.geometry,n.geometry=s),e.call(this,t,r),o&&(n.geometry=o)}}else{if(!ue)return;g.Mesh.prototype.raycast=ue,ue=null}}let ue=null;function Lt(i){const e=new g.BufferGeometry;for(const t in i.attributes)e.setAttribute(t,i.getAttribute(t));return e.setIndex(i.getIndex()),e}const Z=new Array,w=ne("debugprogressive");let me,re=-1;if(w){let i=function(){re+=1,re>=e&&(re=-1),console.log(`Toggle LOD level [${re}]`)},e=6;window.addEventListener("keyup",t=>{t.key==="p"&&i(),t.key==="w"&&(me=!me,console.log(`Toggle wireframe [${me}]`));const r=parseInt(t.key);!isNaN(r)&&r>=0&&(re=r,console.log(`Set LOD level to [${re}]`))})}function Qe(i){if(w)if(Array.isArray(i))for(const e of i)Qe(e);else i&&"wireframe"in i&&(i.wireframe=me===!0)}const de=new Array;let _t=0;const Mt=je()?2:10;function Dt(i){if(de.length<Mt){const r=de.length;w&&console.warn(`[Worker] Creating new worker #${r}`);const n=$e.createWorker(i||{});return de.push(n),n}const e=_t++%de.length;return de[e]}class $e{constructor(e,t){d(this,"_running",[]);d(this,"_webglRenderer",null);d(this,"_debug",!1);this.worker=e,this._debug=t.debug??!1,e.onmessage=r=>{const n=r.data;switch(this._debug&&console.log("[Worker] EVENT",n),n.type){case"loaded-gltf":for(const s of this._running)if(s.url===n.result.url){vt(n.result),s.resolve(n.result);const o=s.url;o.startsWith("blob:")&&URL.revokeObjectURL(o)}}},e.onerror=r=>{console.error("[Worker] Error in gltf-progressive worker:",r)},e.postMessage({type:"init"})}static async createWorker(e){const t=new Worker(new URL("/assets/loader.worker-c528e7f2.js",self.location),{type:"module"});return new $e(t,e)}async load(e,t){const r=pt();let n=t==null?void 0:t.renderer;n||(this._webglRenderer??(this._webglRenderer=(async()=>{const{WebGLRenderer:u}=await import("three");return new u})()),n=await this._webglRenderer);const a=_e(n).ktx2Loader.workerConfig;e instanceof URL?e=e.toString():e.startsWith("file:")?e=URL.createObjectURL(new Blob([e])):!e.startsWith("blob:")&&!e.startsWith("http:")&&!e.startsWith("https:")&&(e=new URL(e,window.location.href).toString());const l={type:"load",url:e,dracoDecoderPath:r.dracoDecoderPath,ktx2TranscoderPath:r.ktx2TranscoderPath,ktx2LoaderConfig:a};return this._debug&&console.debug("[Worker] Sending load request",l),this.worker.postMessage(l),new Promise(u=>{this._running.push({url:e.toString(),resolve:u})})}}function vt(i){var e,t,r,n,s,o,a,l,u,f,y,M,L,S,D,R;for(const O of i.geometries){const c=O.geometry,v=new g.BufferGeometry;if(v.name=c.name||"",c.index){const h=c.index;v.setIndex(Oe(h))}for(const h in c.attributes){const _=c.attributes[h],A=Oe(_);v.setAttribute(h,A)}if(c.morphAttributes)for(const h in c.morphAttributes){const A=c.morphAttributes[h].map(W=>Oe(W));v.morphAttributes[h]=A}if(v.morphTargetsRelative=c.morphTargetsRelative??!1,v.boundingBox=new g.Box3,v.boundingBox.min=new g.Vector3((e=c.boundingBox)==null?void 0:e.min.x,(t=c.boundingBox)==null?void 0:t.min.y,(r=c.boundingBox)==null?void 0:r.min.z),v.boundingBox.max=new g.Vector3((n=c.boundingBox)==null?void 0:n.max.x,(s=c.boundingBox)==null?void 0:s.max.y,(o=c.boundingBox)==null?void 0:o.max.z),v.boundingSphere=new g.Sphere(new g.Vector3((a=c.boundingSphere)==null?void 0:a.center.x,(l=c.boundingSphere)==null?void 0:l.center.y,(u=c.boundingSphere)==null?void 0:u.center.z),(f=c.boundingSphere)==null?void 0:f.radius),c.groups)for(const h of c.groups)v.addGroup(h.start,h.count,h.materialIndex);c.userData&&(v.userData=c.userData),O.geometry=v}for(const O of i.textures){const c=O.texture;let v=null;if(c.isCompressedTexture){const h=c.mipmaps,_=((y=c.image)==null?void 0:y.width)||((L=(M=c.source)==null?void 0:M.data)==null?void 0:L.width)||-1,A=((S=c.image)==null?void 0:S.height)||((R=(D=c.source)==null?void 0:D.data)==null?void 0:R.height)||-1;v=new g.CompressedTexture(h,_,A,c.format,c.type,c.mapping,c.wrapS,c.wrapT,c.magFilter,c.minFilter,c.anisotropy,c.colorSpace)}else v=new g.Texture(c.image,c.mapping,c.wrapS,c.wrapT,c.magFilter,c.minFilter,c.format,c.type,c.anisotropy,c.colorSpace),v.mipmaps=c.mipmaps,v.channel=c.channel,v.source.data=c.source.data,v.flipY=c.flipY,v.premultiplyAlpha=c.premultiplyAlpha,v.unpackAlignment=c.unpackAlignment,v.matrix=new g.Matrix3(...c.matrix.elements);if(!v){console.error("[Worker] Failed to create new texture from received data. Texture is not a CompressedTexture or Texture.");continue}O.texture=v}return i}function Oe(i){let e=i;if("isInterleavedBufferAttribute"in i&&i.isInterleavedBufferAttribute){const t=i.data,r=t.array,n=new g.InterleavedBuffer(r,t.stride);e=new g.InterleavedBufferAttribute(n,i.itemSize,r.byteOffset,i.normalized),e.offset=i.offset}else"isBufferAttribute"in i&&i.isBufferAttribute&&(e=new g.BufferAttribute(i.array,i.itemSize,i.normalized),e.usage=i.usage,e.gpuType=i.gpuType,e.updateRanges=i.updateRanges);return e}const bt=ne("gltf-progressive-worker"),Ot=ne("gltf-progressive-reduce-mipmaps"),Se=Symbol("needle-progressive-texture"),j="NEEDLE_progressive",P=class{constructor(e,t){d(this,"parser");d(this,"url");d(this,"_isLoadingMesh");d(this,"loadMesh",e=>{var r,n;if(this._isLoadingMesh)return null;const t=(n=(r=this.parser.json.meshes[e])==null?void 0:r.extensions)==null?void 0:n[j];return t?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",e).then(s=>{var o;return this._isLoadingMesh=!1,s&&P.registerMesh(this.url,t.guid,s,(o=t.lods)==null?void 0:o.length,0,t),s})):null});w&&console.log("Progressive extension registered for",t),this.parser=e,this.url=t}get name(){return j}static getMeshLODExtension(e){const t=this.getAssignedLODInformation(e);return t!=null&&t.key?this.lodInfos.get(t.key):null}static getPrimitiveIndex(e){var r;const t=(r=this.getAssignedLODInformation(e))==null?void 0:r.index;return t??-1}static getMaterialMinMaxLODsCount(e,t){const r=this,n="LODS:minmax",s=e[n];if(s!=null)return s;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[n]=t,t}if(w==="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==null?void 0:u.isTexture)===!0&&o(u,t)}}else if(e.isMaterial)for(const a of Object.keys(e)){const l=e[a];(l==null?void 0:l.isTexture)===!0&&o(l,t)}else w&&console.warn(`[getMaterialMinMaxLODsCount] Unsupported material type: ${e.type}`);return e[n]=t,t;function o(a,l){const u=r.getAssignedLODInformation(a);if(u){const f=r.lodInfos.get(u.key);if(f&&f.lods){l.min_count=Math.min(l.min_count,f.lods.length),l.max_count=Math.max(l.max_count,f.lods.length);for(let y=0;y<f.lods.length;y++){const M=f.lods[y];M.width&&(l.lods[y]=l.lods[y]||{min_height:1/0,max_height:0},l.lods[y].min_height=Math.min(l.lods[y].min_height,M.height),l.lods[y].max_height=Math.max(l.lods[y].max_height,M.height))}}}}}static hasLODLevelAvailable(e,t){var s;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,n;if(e.isMesh?r=e.geometry:(e.isBufferGeometry||e.isTexture)&&(r=e),r&&(s=r==null?void 0:r.userData)!=null&&s.LODS){const o=r.userData.LODS;if(n=this.lodInfos.get(o.key),t===void 0)return n!=null;if(n)return Array.isArray(n.lods)?t<n.lods.length:t===0}return!1}static assignMeshLOD(e,t){var r;if(!e)return Promise.resolve(null);if(e instanceof g.Mesh||e.isMesh===!0){const n=e.geometry,s=this.getAssignedLODInformation(n);if(!s)return Promise.resolve(null);for(const o of Z)(r=o.onBeforeGetLODMesh)==null||r.call(o,e,t);return e["LOD:requested level"]=t,P.getOrLoadLOD(n,t).then(o=>{if(Array.isArray(o)){const a=s.index||0;o=o[a]}return e["LOD:requested level"]===t&&(delete e["LOD:requested level"],o&&n!=o&&((o==null?void 0:o.isBufferGeometry)?e.geometry=o:w&&console.error("Invalid LOD geometry",o))),o}).catch(o=>(console.error("Error loading mesh LOD",e,o),null))}else w&&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 r=e;if(Array.isArray(r.material)){const n=new Array;for(const s of r.material){const o=this.assignTextureLOD(s,t);n.push(o)}return Promise.all(n).then(s=>{const o=new Array;for(const a of s)Array.isArray(a)&&o.push(...a);return o})}else return this.assignTextureLOD(r.material,t)}if(e.isMaterial===!0){const r=e,n=[],s=new Array;if(r.uniforms&&(r.isRawShaderMaterial||r.isShaderMaterial===!0)){const o=r;for(const a of Object.keys(o.uniforms)){const l=o.uniforms[a].value;if((l==null?void 0:l.isTexture)===!0){const u=this.assignTextureLODForSlot(l,t,r,a).then(f=>(f&&o.uniforms[a].value!=f&&(o.uniforms[a].value=f,o.uniformsNeedUpdate=!0),f));n.push(u),s.push(a)}}}else for(const o of Object.keys(r)){const a=r[o];if((a==null?void 0:a.isTexture)===!0){const l=this.assignTextureLODForSlot(a,t,r,o);n.push(l),s.push(o)}}return Promise.all(n).then(o=>{const a=new Array;for(let l=0;l<o.length;l++){const u=o[l],f=s[l];u&&u.isTexture===!0?a.push({material:r,slot:f,texture:u,level:t}):a.push({material:r,slot:f,texture:null,level:t})}return a})}if(e instanceof g.Texture||e.isTexture===!0){const r=e;return this.assignTextureLODForSlot(r,t,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(e,t,r,n){return(e==null?void 0:e.isTexture)!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(e):P.getOrLoadLOD(e,t).then(s=>{var o,a;if(Array.isArray(s))return console.warn("Progressive: Got an array of textures for a texture slot, this should not happen..."),null;if((s==null?void 0:s.isTexture)===!0){if(s!=e&&r&&n){const l=r[n];if(l&&!w){const u=this.getAssignedLODInformation(l);if(u&&(u==null?void 0:u.level)<t)return w==="verbose"&&console.warn("Assigned texture level is already higher: ",u.level,t,r,l,s),null}if(Ot&&s.mipmaps){const u=s.mipmaps.length;s.mipmaps.length=Math.min(s.mipmaps.length,3),u!==s.mipmaps.length&&w&&console.debug(`Reduced mipmap count from ${u} to ${s.mipmaps.length} for ${s.uuid}: ${(o=s.image)==null?void 0:o.width}x${(a=s.image)==null?void 0:a.height}.`)}r[n]=s}return s}else w=="verbose"&&console.warn("No LOD found for",e,t);return null}).catch(s=>(console.error("Error loading LOD",e,s),null))}afterRoot(e){var t,r;return w&&console.log("AFTER",this.url,e),(t=this.parser.json.textures)==null||t.forEach((n,s)=>{var o;if(n!=null&&n.extensions){const a=n==null?void 0:n.extensions[j];if(a){if(!a.lods){w&&console.warn("Texture has no LODs",a);return}let l=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const f=this.parser.associations.get(u);(f==null?void 0:f.textures)===s&&(l=!0,P.registerTexture(this.url,u,(o=a.lods)==null?void 0:o.length,s,a))}l||this.parser.getDependency("texture",s).then(u=>{var f;u&&P.registerTexture(this.url,u,(f=a.lods)==null?void 0:f.length,s,a)})}}}),(r=this.parser.json.meshes)==null||r.forEach((n,s)=>{if(n!=null&&n.extensions){const o=n==null?void 0:n.extensions[j];if(o&&o.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)===s&&P.registerMesh(this.url,o.guid,a,o.lods.length,l.primitives,o)}}}}),null}static async getOrLoadLOD(e,t){var l,u,f,y;const r=w=="verbose",n=this.getAssignedLODInformation(e);if(!n)return w&&console.warn(`[gltf-progressive] No LOD information found: ${e.name}, uuid: ${e.uuid}, type: ${e.type}`,e),null;const s=n==null?void 0:n.key;let o;if(e.isTexture===!0){const M=e;M.source&&M.source[Se]&&(o=M.source[Se])}if(o||(o=P.lodInfos.get(s)),o){if(t>0){let S=!1;const D=Array.isArray(o.lods);if(D&&t>=o.lods.length?S=!0:D||(S=!0),S)return this.lowresCache.get(s)}const M=Array.isArray(o.lods)?(l=o.lods[t])==null?void 0:l.path:o.lods;if(!M)return w&&!o["missing:uri"]&&(o["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+t,o)),null;const L=yt(n.url,M);if(L.endsWith(".glb")||L.endsWith(".gltf")){if(!o.guid)return console.warn("missing pointer for glb/gltf texture",o),null;const S=L+"_"+o.guid,D=await this.queue.slot(L),R=this.previouslyLoaded.get(S);if(R!==void 0){r&&console.log(`LOD ${t} was already loading/loaded: ${S}`);let h=await R.catch(A=>(console.error(`Error loading LOD ${t} from ${L}
|
|
2
|
+
`,A),null)),_=!1;if(h==null||(h instanceof g.Texture&&e instanceof g.Texture?(u=h.image)!=null&&u.data||(f=h.source)!=null&&f.data?h=this.copySettings(e,h):(_=!0,this.previouslyLoaded.delete(S)):h instanceof g.BufferGeometry&&e instanceof g.BufferGeometry&&((y=h.attributes.position)!=null&&y.array||(_=!0,this.previouslyLoaded.delete(S)))),!_)return h}if(!D.use)return w&&console.log(`LOD ${t} was aborted: ${L}`),null;const O=o,c=new Promise(async(h,_)=>{if(bt){const m=await(await Dt({})).load(L);if(m.textures.length>0)for(const p of m.textures){let x=p.texture;return P.assignLODInformation(n.url,x,s,t,void 0),e instanceof g.Texture&&(x=this.copySettings(e,x)),x&&(x.guid=O.guid),h(x)}if(m.geometries.length>0){const p=new Array;for(const x of m.geometries){const B=x.geometry;P.assignLODInformation(n.url,B,s,t,x.primitiveIndex),p.push(B)}return h(p)}return h(null)}const A=new Ie.GLTFLoader;Be(A),w&&(await new Promise(T=>setTimeout(T,1e3)),r&&console.warn("Start loading (delayed) "+L,O.guid));let W=L;if(O&&Array.isArray(O.lods)){const T=O.lods[t];T.hash&&(W+="?v="+T.hash)}const I=await A.loadAsync(W).catch(T=>(console.error(`Error loading LOD ${t} from ${L}
|
|
3
|
+
`,T),h(null)));if(!I)return h(null);const te=I.parser;r&&console.log("Loading finished "+L,O.guid);let E=0;if(I.parser.json.textures){let T=!1;for(const m of I.parser.json.textures){if(m!=null&&m.extensions){const p=m==null?void 0:m.extensions[j];if(p!=null&&p.guid&&p.guid===O.guid){T=!0;break}}E++}if(T){let m=await te.getDependency("texture",E);return m&&P.assignLODInformation(n.url,m,s,t,void 0),r&&console.log('change "'+e.name+'" → "'+m.name+'"',L,E,m,S),e instanceof g.Texture&&(m=this.copySettings(e,m)),m&&(m.guid=O.guid),h(m)}else w&&console.warn("Could not find texture with guid",O.guid,I.parser.json)}if(E=0,I.parser.json.meshes){let T=!1;for(const m of I.parser.json.meshes){if(m!=null&&m.extensions){const p=m==null?void 0:m.extensions[j];if(p!=null&&p.guid&&p.guid===O.guid){T=!0;break}}E++}if(T){const m=await te.getDependency("mesh",E);if(r&&console.log(`Loaded Mesh "${m.name}"`,L,E,m,S),m.isMesh===!0){const p=m.geometry;return P.assignLODInformation(n.url,p,s,t,0),h(p)}else{const p=new Array;for(let x=0;x<m.children.length;x++){const B=m.children[x];if(B.isMesh===!0){const G=B.geometry;P.assignLODInformation(n.url,G,s,t,x),p.push(G)}}return h(p)}}else w&&console.warn("Could not find mesh with guid",O.guid,I.parser.json)}return h(null)});return this.previouslyLoaded.set(S,c),D.use(c),await c}else if(e instanceof g.Texture){r&&console.log("Load texture from uri: "+L);const D=await new g.TextureLoader().loadAsync(L);return D?(D.guid=o.guid,D.flipY=!1,D.needsUpdate=!0,D.colorSpace=e.colorSpace,r&&console.log(o,D)):w&&console.warn("failed loading",L),D}}else w&&console.warn(`Can not load LOD ${t}: no LOD info found for "${s}" ${e.name}`,e.type);return null}static assignLODInformation(e,t,r,n,s){if(!t)return;t.userData||(t.userData={});const o=new St(e,r,n,s);t.userData.LODS=o,"source"in t&&typeof t.source=="object"&&(t.source.LODS=o)}static getAssignedLODInformation(e){var t,r;return e?(t=e.userData)!=null&&t.LODS?e.userData.LODS:"source"in e&&((r=e.source)!=null&&r.LODS)?e.source.LODS:null:null}static copySettings(e,t){return t?(w==="verbose"&&console.debug(`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}};let C=P;d(C,"registerTexture",(e,t,r,n,s)=>{var a,l,u,f,y,M,L,S;if(!t){w&&console.error("gltf-progressive: Called register texture without texture");return}if(w){const D=((a=t.image)==null?void 0:a.width)||((u=(l=t.source)==null?void 0:l.data)==null?void 0:u.width)||0,R=((f=t.image)==null?void 0:f.height)||((M=(y=t.source)==null?void 0:y.data)==null?void 0:M.height)||0;console.log(`> Progressive: register texture[${n}] "${t.name||t.uuid}", Current: ${D}x${R}, Max: ${(L=s.lods[0])==null?void 0:L.width}x${(S=s.lods[0])==null?void 0:S.height}, uuid: ${t.uuid}`,s,t)}t.source&&(t.source[Se]=s);const o=s.guid;P.assignLODInformation(e,t,o,r,n),P.lodInfos.set(o,s),P.lowresCache.set(o,t)}),d(C,"registerMesh",(e,t,r,n,s,o)=>{var u;const a=r.geometry;if(!a){w&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),w&&console.log("> Progressive: register mesh "+r.name,{index:s,uuid:r.uuid},o,r),P.assignLODInformation(e,a,t,n,s),P.lodInfos.set(t,o);let l=P.lowresCache.get(t);l?l.push(r.geometry):l=[r.geometry],P.lowresCache.set(t,l),n>0&&!oe(r)&&Ye(r,a);for(const f of Z)(u=f.onRegisteredNewMesh)==null||u.call(f,r,o)}),d(C,"lodInfos",new Map),d(C,"previouslyLoaded",new Map),d(C,"lowresCache",new Map),d(C,"workers",[]),d(C,"_workersIndex",0),d(C,"maxConcurrent",50),d(C,"queue",new xt(P.maxConcurrent,{debug:w!=!1}));class St{constructor(e,t,r,n){d(this,"url");d(this,"key");d(this,"level");d(this,"index");this.url=e,this.key=t,this.level=r,n!=null&&(this.index=n)}}class ye{constructor(e,t){d(this,"frame_start");d(this,"frame_capture_end");d(this,"ready");d(this,"_resolve");d(this,"_signal");d(this,"_resolved",!1);d(this,"_addedCount",0);d(this,"_resolvedCount",0);d(this,"_awaiting",[]);d(this,"_maxPromisesPerObject",1);d(this,"_currentFrame",0);d(this,"_seen",new WeakMap);var s;const n=Math.max(t.frames??2,2);this.frame_start=e,this.frame_capture_end=e+n,this.ready=new Promise(o=>{this._resolve=o}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=t.signal,(s=this._signal)==null||s.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,r){if(this._resolved){w&&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 n=this._seen.get(t);if(n>=this._maxPromisesPerObject){w&&console.warn("PromiseGroup: Already awaiting object ignoring new promise for it.");return}this._seen.set(t,n+1)}else this._seen.set(t,1);this._awaiting.push(r),this._addedCount++,r.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(r),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})}}d(ye,"addPromise",(e,t,r,n)=>{n.forEach(s=>{s.add(e,t,r)})});const z=ne("debugprogressive"),Tt=ne("noprogressive"),Te=Symbol("Needle:LODSManager"),Pe=Symbol("Needle:LODState"),J=Symbol("Needle:CurrentLOD"),F={mesh_lod:-1,texture_lod:-1};var N,Y,we,ee,ie,Le,H;const k=class{constructor(e,t){d(this,"renderer");d(this,"context");d(this,"projectionScreenMatrix",new g.Matrix4);d(this,"targetTriangleDensity",2e5);d(this,"skinnedMeshAutoUpdateBoundsInterval",30);d(this,"updateInterval","auto");Q(this,N,1);d(this,"pause",!1);d(this,"manual",!1);d(this,"_newPromiseGroups",[]);d(this,"_promiseGroupIds",0);d(this,"_lodchangedlisteners",[]);Q(this,Y,void 0);Q(this,we,new g.Clock);Q(this,ee,0);Q(this,ie,0);Q(this,Le,0);Q(this,H,0);d(this,"_fpsBuffer",[60,60,60,60,60]);d(this,"_sphere",new g.Sphere);d(this,"_tempBox",new g.Box3);d(this,"_tempBox2",new g.Box3);d(this,"tempMatrix",new g.Matrix4);d(this,"_tempWorldPosition",new g.Vector3);d(this,"_tempBoxSize",new g.Vector3);d(this,"_tempBox2Size",new g.Vector3);this.renderer=e,this.context={...t}}static getObjectLODState(e){return e[Pe]}static addPlugin(e){Z.push(e)}static removePlugin(e){const t=Z.indexOf(e);t>=0&&Z.splice(t,1)}static get(e,t){if(e[Te])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),e[Te];const r=new k(e,{engine:"unknown",...t});return e[Te]=r,r}get plugins(){return Z}awaitLoading(e){const t=this._promiseGroupIds++,r=new ye(b(this,ee),{...e});this._newPromiseGroups.push(r);const n=performance.now();return r.ready.finally(()=>{const s=this._newPromiseGroups.indexOf(r);s>=0&&(this._newPromiseGroups.splice(s,1),Fe()&&performance.measure("LODsManager:awaitLoading",{start:n,detail:{id:t,name:e==null?void 0:e.name,awaited:r.awaitedCount,resolved:r.resolvedCount}}))}),r.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let e=this._newPromiseGroups.length-1;e>=0;e--)this._newPromiseGroups[e].update(b(this,ee))}addEventListener(e,t){e==="changed"&&this._lodchangedlisteners.push(t)}removeEventListener(e,t){if(e==="changed"){const r=this._lodchangedlisteners.indexOf(t);r>=0&&this._lodchangedlisteners.splice(r,1)}}enable(){if(b(this,Y))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let e=0;K(this,Y,this.renderer.render);const t=this;_e(this.renderer),this.renderer.render=function(r,n){const s=t.renderer.getRenderTarget();(s==null||"isXRRenderTarget"in s&&s.isXRRenderTarget)&&(e=0,K(t,ee,b(t,ee)+1),K(t,ie,b(t,we).getDelta()),K(t,Le,b(t,Le)+b(t,ie)),t._fpsBuffer.shift(),t._fpsBuffer.push(1/b(t,ie)),K(t,H,t._fpsBuffer.reduce((a,l)=>a+l)/t._fpsBuffer.length),z&&b(t,ee)%200===0&&console.log("FPS",Math.round(b(t,H)),"Interval:",b(t,N)));const o=e++;b(t,Y).call(this,r,n),t.onAfterRender(r,n,o)}}disable(){b(this,Y)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=b(this,Y),K(this,Y,void 0))}update(e,t){this.internalUpdate(e,t)}onAfterRender(e,t,r){if(this.pause)return;const s=this.renderer.renderLists.get(e,0).opaque;let o=!0;if(s.length===1){const a=s[0].material;(a.name==="EffectMaterial"||a.name==="CopyShader")&&(o=!1)}if((t.parent&&t.parent.type==="CubeCamera"||r>=1&&t.type==="OrthographicCamera")&&(o=!1),o){if(Tt||(this.updateInterval==="auto"?b(this,H)<40&&b(this,N)<10?(K(this,N,b(this,N)+1),z&&console.warn("↓ Reducing LOD updates",b(this,N),b(this,H).toFixed(0))):b(this,H)>=60&&b(this,N)>1&&(K(this,N,b(this,N)-1),z&&console.warn("↑ Increasing LOD updates",b(this,N),b(this,H).toFixed(0))):K(this,N,this.updateInterval),b(this,N)>0&&b(this,ee)%b(this,N)!=0))return;this.internalUpdate(e,t),this._postprocessPromiseGroups()}}internalUpdate(e,t){var l,u;const r=this.renderer.renderLists.get(e,0),n=r.opaque;this.projectionScreenMatrix.multiplyMatrices(t.projectionMatrix,t.matrixWorldInverse);const s=this.targetTriangleDensity;for(const f of n){if(f.material&&(((l=f.geometry)==null?void 0:l.type)==="BoxGeometry"||((u=f.geometry)==null?void 0:u.type)==="BufferGeometry")&&(f.material.name==="SphericalGaussianBlur"||f.material.name=="BackgroundCubeMaterial"||f.material.name==="CubemapFromEquirect"||f.material.name==="EquirectangularToCubeUV")){z&&(f.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(f.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",f,f.material.name,f.material.type)));continue}switch(f.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(z==="color"&&f.material&&!f.object.progressive_debug_color){f.object.progressive_debug_color=!0;const M=Math.random()*16777215,L=new g.MeshStandardMaterial({color:M});f.object.material=L}const y=f.object;(y instanceof g.Mesh||y.isMesh)&&this.updateLODs(e,t,y,s)}const o=r.transparent;for(const f of o){const y=f.object;(y instanceof g.Mesh||y.isMesh)&&this.updateLODs(e,t,y,s)}const a=r.transmissive;for(const f of a){const y=f.object;(y instanceof g.Mesh||y.isMesh)&&this.updateLODs(e,t,y,s)}}updateLODs(e,t,r,n){var a,l;r.userData||(r.userData={});let s=r[Pe];if(s||(s=new Pt,r[Pe]=s),s.frames++<2)return;for(const u of Z)(a=u.onBeforeUpdateLOD)==null||a.call(u,this.renderer,e,t,r);const o=k.overrideGlobalLodLevel!==void 0?k.overrideGlobalLodLevel:re;o>=0?(F.mesh_lod=o,F.texture_lod=o):(this.calculateLodLevel(t,r,s,n,F),F.mesh_lod=Math.round(F.mesh_lod),F.texture_lod=Math.round(F.texture_lod)),F.mesh_lod>=0&&this.loadProgressiveMeshes(r,F.mesh_lod),r.material&&F.texture_lod>=0&&this.loadProgressiveTextures(r.material,F.texture_lod,o),w&&r.material&&!r.isGizmo&&Qe(r.material);for(const u of Z)(l=u.onAfterUpdatedLOD)==null||l.call(u,this.renderer,e,t,r,F);s.lastLodLevel_Mesh=F.mesh_lod,s.lastLodLevel_Texture=F.texture_lod}loadProgressiveTextures(e,t,r){if(!e)return;if(Array.isArray(e)){for(const s of e)this.loadProgressiveTextures(s,t);return}let n=!1;if((e[J]===void 0||t<e[J])&&(n=!0),r!==void 0&&r>=0&&(n=e[J]!=r,t=r),n){e[J]=t;const s=C.assignTextureLOD(e,t).then(o=>{this._lodchangedlisteners.forEach(a=>a({type:"texture",level:t,object:e}))});ye.addPromise("texture",e,s,this._newPromiseGroups)}}loadProgressiveMeshes(e,t){if(!e)return Promise.resolve(null);let r=e[J]!==t;const n=e["DEBUG:LOD"];if(n!=null&&(r=e[J]!=n,t=n),r){e[J]=t;const s=e.geometry,o=C.assignMeshLOD(e,t).then(a=>(a&&e[J]==t&&s!=e.geometry&&this._lodchangedlisteners.forEach(l=>l({type:"mesh",level:t,object:e})),a));return ye.addPromise("mesh",e,o,this._newPromiseGroups),o}return Promise.resolve(null)}static isInside(e,t){const r=e.min,n=e.max,s=(r.x+n.x)*.5,o=(r.y+n.y)*.5;return this._tempPtInside.set(s,o,r.z).applyMatrix4(t).z<0}calculateLodLevel(e,t,r,n,s){var R,O,c,v;if(!t){s.mesh_lod=-1,s.texture_lod=-1;return}if(!e){s.mesh_lod=-1,s.texture_lod=-1;return}let a=10+1,l=!1;if(z&&t["DEBUG:LOD"]!=null)return t["DEBUG:LOD"];const u=(R=C.getMeshLODExtension(t.geometry))==null?void 0:R.lods,f=C.getPrimitiveIndex(t.geometry),y=u&&u.length>0,M=C.getMaterialMinMaxLODsCount(t.material),L=M.min_count!==1/0&&M.min_count>=0&&M.max_count>=0;if(!y&&!L){s.mesh_lod=0,s.texture_lod=0;return}y||(l=!0,a=0);const S=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let D=t.geometry.boundingBox;if(t.type==="SkinnedMesh"){const h=t;if(!h.boundingBox)h.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0){if(!h[k.$skinnedMeshBoundsOffset]){const A=k.skinnedMeshBoundsFrameOffsetCounter++;h[k.$skinnedMeshBoundsOffset]=A}const _=h[k.$skinnedMeshBoundsOffset];if((r.frames+_)%this.skinnedMeshAutoUpdateBoundsInterval===0){const A=oe(h),W=h.geometry;A&&(h.geometry=A),h.computeBoundingBox(),h.geometry=W}}D=h.boundingBox}if(D){const h=e;if(t.geometry.attributes.color&&t.geometry.attributes.color.count<100&&t.geometry.boundingSphere){this._sphere.copy(t.geometry.boundingSphere),this._sphere.applyMatrix4(t.matrixWorld);const p=e.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(p)){s.mesh_lod=0,s.texture_lod=0;return}}if(this._tempBox.copy(D),this._tempBox.applyMatrix4(t.matrixWorld),h.isPerspectiveCamera&&k.isInside(this._tempBox,this.projectionScreenMatrix)){s.mesh_lod=0,s.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&h.isPerspectiveCamera&&h.fov>70){const p=this._tempBox.min,x=this._tempBox.max;let B=p.x,G=p.y,X=x.x,ae=x.y;const fe=2,Me=1.5,he=(p.x+x.x)*.5,ge=(p.y+x.y)*.5;B=(B-he)*fe+he,G=(G-ge)*fe+ge,X=(X-he)*fe+he,ae=(ae-ge)*fe+ge;const et=B<0&&X>0?0:Math.min(Math.abs(p.x),Math.abs(x.x)),tt=G<0&&ae>0?0:Math.min(Math.abs(p.y),Math.abs(x.y)),De=Math.max(et,tt);r.lastCentrality=(Me-De)*(Me-De)*(Me-De)}else r.lastCentrality=1;const _=this._tempBox.getSize(this._tempBoxSize);_.multiplyScalar(.5),screen.availHeight>0&&S>0&&_.multiplyScalar(S/screen.availHeight),e.isPerspectiveCamera?_.x*=e.aspect:e.isOrthographicCamera;const A=e.matrixWorldInverse,W=this._tempBox2;W.copy(D),W.applyMatrix4(t.matrixWorld),W.applyMatrix4(A);const I=W.getSize(this._tempBox2Size),te=Math.max(I.x,I.y);if(Math.max(_.x,_.y)!=0&&te!=0&&(_.z=I.z/Math.max(I.x,I.y)*Math.max(_.x,_.y)),r.lastScreenCoverage=Math.max(_.x,_.y,_.z),r.lastScreenspaceVolume.copy(_),r.lastScreenCoverage*=r.lastCentrality,z&&k.debugDrawLine){const p=this.tempMatrix.copy(this.projectionScreenMatrix);p.invert();const x=k.corner0,B=k.corner1,G=k.corner2,X=k.corner3;x.copy(this._tempBox.min),B.copy(this._tempBox.max),B.x=x.x,G.copy(this._tempBox.max),G.y=x.y,X.copy(this._tempBox.max);const ae=(x.z+X.z)*.5;x.z=B.z=G.z=X.z=ae,x.applyMatrix4(p),B.applyMatrix4(p),G.applyMatrix4(p),X.applyMatrix4(p),k.debugDrawLine(x,B,255),k.debugDrawLine(x,G,255),k.debugDrawLine(B,X,255),k.debugDrawLine(G,X,255)}let T=999;if(u&&r.lastScreenCoverage>0)for(let p=0;p<u.length;p++){const x=u[p],G=(((O=x.densities)==null?void 0:O[f])||x.density||1e-5)/r.lastScreenCoverage;if(f>0&&Fe()&&!x.densities&&!globalThis["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"]&&(window["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"]=!0,console.warn("[Needle Progressive] Detected usage of mesh without primitive densities. This might cause incorrect LOD level selection: Consider re-optimizing your model by updating your Needle Integration, Needle glTF Pipeline or running optimization again on Needle Cloud.")),G<n){T=p;break}}T<a&&(a=T,l=!0)}if(l?s.mesh_lod=a:s.mesh_lod=r.lastLodLevel_Mesh,z&&s.mesh_lod!=r.lastLodLevel_Mesh){const _=u==null?void 0:u[s.mesh_lod];_&&console.debug(`Mesh LOD changed: ${r.lastLodLevel_Mesh} → ${s.mesh_lod} (density: ${(c=_.densities)==null?void 0:c[f].toFixed(0)}) | ${t.name}`)}if(L){const h="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(r.lastLodLevel_Texture<0){if(s.texture_lod=M.max_count-1,z){const _=M.lods[M.max_count-1];z&&console.log(`First Texture LOD ${s.texture_lod} (${_.max_height}px) - ${t.name}`)}}else{const _=r.lastScreenspaceVolume.x+r.lastScreenspaceVolume.y+r.lastScreenspaceVolume.z;let A=r.lastScreenCoverage*4;((v=this.context)==null?void 0:v.engine)==="model-viewer"&&(A*=1.5);const I=S/window.devicePixelRatio*A;let te=!1;for(let E=M.lods.length-1;E>=0;E--){const T=M.lods[E];if(!(h&&T.max_height>=2048)&&!(je()&&T.max_height>4096)&&(T.max_height>I||!te&&E===0)){if(te=!0,s.texture_lod=E,z&&s.texture_lod<r.lastLodLevel_Texture){const m=T.max_height;console.log(`Texture LOD changed: ${r.lastLodLevel_Texture} → ${s.texture_lod} = ${m}px
|
|
6
|
+
Screensize: ${I.toFixed(0)}px, Coverage: ${(100*r.lastScreenCoverage).toFixed(2)}%, Volume ${_.toFixed(1)}
|
|
7
|
+
${t.name}`)}break}}}}else s.texture_lod=0}};let $=k;N=new WeakMap,Y=new WeakMap,we=new WeakMap,ee=new WeakMap,ie=new WeakMap,Le=new WeakMap,H=new WeakMap,d($,"debugDrawLine"),d($,"overrideGlobalLodLevel"),d($,"corner0",new g.Vector3),d($,"corner1",new g.Vector3),d($,"corner2",new g.Vector3),d($,"corner3",new g.Vector3),d($,"_tempPtInside",new g.Vector3),d($,"skinnedMeshBoundsFrameOffsetCounter",0),d($,"$skinnedMeshBoundsOffset",Symbol("gltf-progressive-skinnedMeshBoundsOffset"));class Pt{constructor(){d(this,"frames",0);d(this,"lastLodLevel_Mesh",-1);d(this,"lastLodLevel_Texture",-1);d(this,"lastScreenCoverage",0);d(this,"lastScreenspaceVolume",new g.Vector3);d(this,"lastCentrality",0)}}const Ne=Symbol("NEEDLE_mesh_lod"),pe=Symbol("NEEDLE_texture_lod");let ce=null;function Ge(){const i=At();i&&(i.mapURLs(function(e){return Ue(),e}),Ue(),ce==null||ce.disconnect(),ce=new MutationObserver(e=>{e.forEach(t=>{t.addedNodes.forEach(r=>{r instanceof HTMLElement&&r.tagName.toLowerCase()==="model-viewer"&&Je(r)})})}),ce.observe(document,{childList:!0,subtree:!0}))}function At(){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"),Ge()}),null)}function Ue(){if(typeof document>"u")return;document.querySelectorAll("model-viewer").forEach(e=>{Je(e)})}const We=new WeakSet;let Ct=0;function Je(i){if(!i||We.has(i))return null;We.add(i),console.debug("[gltf-progressive] found new model-viewer..."+ ++Ct+`
|
|
8
|
+
`,i.getAttribute("src"));let e=null,t=null,r=null;for(let n=i;n!=null;n=Object.getPrototypeOf(n)){const s=Object.getOwnPropertySymbols(n),o=s.find(u=>u.toString()=="Symbol(renderer)"),a=s.find(u=>u.toString()=="Symbol(scene)"),l=s.find(u=>u.toString()=="Symbol(needsRender)");!e&&o!=null&&(e=i[o].threeRenderer),!t&&a!=null&&(t=i[a]),!r&&l!=null&&(r=i[l])}if(e&&t){let n=function(){if(r){let o=0,a=setInterval(()=>{if(o++>5){clearInterval(a);return}r==null||r.call(i)},300)}};console.debug("[gltf-progressive] setup model-viewer");const s=$.get(e,{engine:"model-viewer"});return $.addPlugin(new kt),s.enable(),s.addEventListener("changed",()=>{r==null||r.call(i)}),i.addEventListener("model-visibility",o=>{o.detail.visible&&(r==null||r.call(i))}),i.addEventListener("load",()=>{n()}),()=>{s.disable()}}return null}class kt{constructor(){d(this,"_didWarnAboutMissingUrl",!1)}onBeforeUpdateLOD(e,t,r,n){this.tryParseMeshLOD(t,n),this.tryParseTextureLOD(t,n)}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 r=this.tryGetCurrentGLTF(e),n=this.tryGetCurrentModelViewer(e),s=this.getUrl(n);if(s&&r&&t.material){let o=function(l){var f,y,M;if(l[pe]==!0)return;l[pe]=!0,l.userData&&(l.userData.LOD=-1);const u=Object.keys(l);for(let L=0;L<u.length;L++){const S=u[L],D=l[S];if((D==null?void 0:D.isTexture)===!0){const R=(y=(f=D.userData)==null?void 0:f.associations)==null?void 0:y.textures;if(R==null)continue;const O=r.parser.json.textures[R];if(!O){console.warn("Texture data not found for texture index "+R);continue}if((M=O==null?void 0:O.extensions)!=null&&M[j]){const c=O.extensions[j];c&&s&&C.registerTexture(s,D,c.lods.length,R,c)}}}};const a=t.material;if(Array.isArray(a))for(const l of a)o(l);else o(a)}}tryParseMeshLOD(e,t){var o,a;if(t[Ne]==!0)return;t[Ne]=!0;const r=this.tryGetCurrentModelViewer(e),n=this.getUrl(r);if(!n)return;const s=(a=(o=t.userData)==null?void 0:o.gltfExtensions)==null?void 0:a[j];if(s&&n){const l=t.uuid;C.registerMesh(n,l,t,0,s.lods.length,s)}}}function Ze(i,e,t,r){_e(e),Be(t),Re(t,{progressive:!0,...r==null?void 0:r.hints}),t.register(s=>new C(s,i));const n=$.get(e);return(r==null?void 0:r.enableLODsManager)!==!1&&n.enable(),n}Ge();if(!wt){const i={gltfProgressive:{useNeedleProgressive:Ze,LODsManager:$,configureLoader:Re,getRaycastMesh:oe,useRaycastMeshes:He}};if(!globalThis.Needle)globalThis.Needle=i;else for(const e in i)globalThis.Needle[e]=i[e]}exports.EXTENSION_NAME=j;exports.LODsManager=$;exports.NEEDLE_progressive=C;exports.VERSION=ze;exports.addDracoAndKTX2Loaders=Be;exports.configureLoader=Re;exports.createLoaders=_e;exports.getRaycastMesh=oe;exports.patchModelViewer=Ge;exports.registerRaycastMesh=Ye;exports.setDracoDecoderLocation=qe;exports.setKTX2TranscoderLocation=Xe;exports.useNeedleProgressive=Ze;exports.useRaycastMeshes=He;
|
package/lib/extension.d.ts
CHANGED
|
@@ -122,7 +122,10 @@ export declare class NEEDLE_progressive implements GLTFLoaderPlugin {
|
|
|
122
122
|
private static readonly previouslyLoaded;
|
|
123
123
|
/** this contains the geometry/textures that were originally loaded */
|
|
124
124
|
private static readonly lowresCache;
|
|
125
|
+
private static readonly workers;
|
|
126
|
+
private static _workersIndex;
|
|
125
127
|
private static getOrLoadLOD;
|
|
128
|
+
private static maxConcurrent;
|
|
126
129
|
private static queue;
|
|
127
130
|
private static assignLODInformation;
|
|
128
131
|
private static getAssignedLODInformation;
|