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