@needle-tools/engine 4.3.2-beta.3 → 4.3.2-beta.4
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 +6 -0
- package/dist/gltf-progressive.js +91 -90
- package/dist/gltf-progressive.light.js +91 -90
- package/dist/gltf-progressive.light.min.js +6 -6
- package/dist/gltf-progressive.light.umd.cjs +4 -4
- package/dist/gltf-progressive.min.js +6 -6
- package/dist/gltf-progressive.umd.cjs +4 -4
- package/dist/needle-engine.bundle.js +2997 -2988
- package/dist/needle-engine.bundle.light.js +3036 -3027
- package/dist/needle-engine.bundle.light.min.js +110 -110
- package/dist/needle-engine.bundle.light.umd.cjs +94 -94
- package/dist/needle-engine.bundle.min.js +110 -110
- package/dist/needle-engine.bundle.umd.cjs +97 -97
- package/dist/needle-engine.light.d.ts +9 -9
- package/lib/engine/engine_context.d.ts +1 -0
- package/lib/engine/engine_context.js +3 -0
- package/lib/engine/engine_context.js.map +1 -1
- package/lib/engine/engine_context_registry.js +7 -0
- package/lib/engine/engine_context_registry.js.map +1 -1
- package/lib/engine/engine_utils_format.js +5 -1
- package/lib/engine/engine_utils_format.js.map +1 -1
- package/lib/engine-components/Skybox.js +9 -8
- package/lib/engine-components/Skybox.js.map +1 -1
- package/package.json +2 -2
- package/plugins/common/license.js +42 -32
- package/src/engine/engine_context.ts +4 -0
- package/src/engine/engine_context_registry.ts +5 -0
- package/src/engine/engine_utils_format.ts +5 -1
- package/src/engine-components/Skybox.ts +8 -7
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";const g=require("./three.light.umd.cjs"),Z=require("./three-examples.light.umd.cjs"),Ne="";globalThis.GLTF_PROGRESSIVE_VERSION=Ne;console.debug("[gltf-progressive] version -");let F="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",j="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Fe=F,Ue=j,Ve=new URL(F+"draco_decoder.js");fetch(Ve,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${F} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),F===Fe&&Pe("./include/draco/"),j===Ue&&Ce("./include/ktx2/")}).finally(()=>{Be()});function Pe(t){F=t,C&&C[me]!=F?(console.debug("Updating Draco decoder path to "+t),C[me]=F,C.setDecoderPath(F),C.preload()):console.debug("Setting Draco decoder path to "+t)}function Ce(t){j=t,$&&$.transcoderPath!=j?(console.debug("Updating KTX2 transcoder path to "+t),$.setTranscoderPath(j),$.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let C,de,$;function Be(){C||(C=new Z.DRACOLoader,C[me]=F,C.setDecoderPath(F),C.setDecoderConfig({type:"js"}),C.preload()),$||($=new Z.KTX2Loader,$.setTranscoderPath(j),$.init()),de||(de=Z.MeshoptDecoder)}function we(t){return Be(),t?$.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:C,ktx2Loader:$,meshoptDecoder:de}}function Oe(t){t.dracoLoader||t.setDRACOLoader(C),t.ktx2Loader||t.setKTX2Loader($),t.meshoptDecoder||t.setMeshoptDecoder(de)}const xe=new WeakMap;function Se(t,e){let s=xe.get(t);s?s=Object.assign(s,e):s=e,xe.set(t,s)}const pe=Z.GLTFLoader.prototype.load;function We(...t){const e=xe.get(this);let s=t[0];const o=new URL(s,window.location.href);if(o.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}`,s=o.toString()}return t[0]=s,pe==null?void 0:pe.call(this,...t)}Z.GLTFLoader.prototype.load=We;ne("debugprogressive");function ne(t){if(typeof window>"u")return!1;const s=new URL(window.location.href).searchParams.get(t);return s==null||s==="0"||s==="false"?!1:s===""?!0:s}function ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const s=t.lastIndexOf("/");if(s>=0){const o=t.substring(0,s+1);for(;o.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return o+e}return e}let te;function Xe(){return te!==void 0||(te=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ne("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",te)),te}const qe=typeof window>"u"&&typeof document>"u",De=Symbol("needle:raycast-mesh");function ee(t){return(t==null?void 0:t[De])instanceof g.BufferGeometry?t[De]:null}function Ke(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!ee(t)){const o=He(e);o.userData={isRaycastMesh:!0},t[De]=o}}function Ye(t=!0){if(t){if(se)return;const e=se=g.Mesh.prototype.raycast;g.Mesh.prototype.raycast=function(s,o){const i=this,r=ee(i);let n;r&&i.isMesh&&(n=i.geometry,i.geometry=r),e.call(this,s,o),n&&(i.geometry=n)}}else{if(!se)return;g.Mesh.prototype.raycast=se,se=null}}let se=null;function He(t){const e=new g.BufferGeometry;for(const s in t.attributes)e.setAttribute(s,t.getAttribute(s));return e.setIndex(t.getIndex()),e}const H=new Array,z="NEEDLE_progressive",y=ne("debugprogressive"),Me=Symbol("needle-progressive-texture"),oe=new Map,_e=new Set;if(y){let t=function(){e+=1,console.log("Toggle LOD level",e,oe),oe.forEach((i,r)=>{for(const n of i.keys){const a=r[n];if(a!=null)if(a.isBufferGeometry===!0){const c=E.getMeshLODInformation(a),l=c?Math.min(e,c.lods.length):0;r["DEBUG:LOD"]=l,c&&(s=Math.max(s,c.lods.length-1))}else r.isMaterial===!0&&(r["DEBUG:LOD"]=e)}}),e>=s&&(e=-1)},e=-1,s=2,o=!1;window.addEventListener("keyup",i=>{i.key==="p"&&t(),i.key==="w"&&(o=!o,_e&&_e.forEach(r=>{r.name!="BackgroundCubeMaterial"&&r.glyphMap==null&&"wireframe"in r&&(r.wireframe=o)}))})}function be(t,e,s){var i;if(!y)return;oe.has(t)||oe.set(t,{keys:[],sourceId:s});const o=oe.get(t);((i=o==null?void 0:o.keys)==null?void 0:i.includes(e))==!1&&o.keys.push(e)}const _=class{constructor(t,e){this.loadMesh=s=>{var i,r;if(this._isLoadingMesh)return null;const o=(r=(i=this.parser.json.meshes[s])==null?void 0:i.extensions)==null?void 0:r[z];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",s).then(n=>{var a;return this._isLoadingMesh=!1,n&&_.registerMesh(this.url,o.guid,n,(a=o.lods)==null?void 0:a.length,void 0,o),n})):null},y&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return z}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const s=this,o="LODS:minmax",i=t[o];if(i!=null)return i;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const n of t)this.getMaterialMinMaxLODsCount(n,e);return t[o]=e,e}if(y==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const n=t;for(const a of Object.keys(n.uniforms)){const c=n.uniforms[a].value;(c==null?void 0:c.isTexture)===!0&&r(c,e)}}else if(t.isMaterial)for(const n of Object.keys(t)){const a=t[n];(a==null?void 0:a.isTexture)===!0&&r(a,e)}return t[o]=e,e;function r(n,a){const c=s.getAssignedLODInformation(n);if(c){const l=s.lodInfos.get(c.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let d=0;d<l.lods.length;d++){const h=l.lods[d];h.width&&(a.lods[d]=a.lods[d]||{min_height:1/0,max_height:0},a.lods[d].min_height=Math.min(a.lods[d].min_height,h.height),a.lods[d].max_height=Math.max(a.lods[d].max_height,h.height))}}}}}static hasLODLevelAvailable(t,e){var i;if(Array.isArray(t)){for(const r of t)if(this.hasLODLevelAvailable(r,e))return!0;return!1}if(t.isMaterial===!0){for(const r of Object.keys(t)){const n=t[r];if(n&&n.isTexture&&this.hasLODLevelAvailable(n,e))return!0}return!1}else if(t.isGroup===!0){for(const r of t.children)if(r.isMesh===!0&&this.hasLODLevelAvailable(r,e))return!0}let s,o;if(t.isMesh?s=t.geometry:(t.isBufferGeometry||t.isTexture)&&(s=t),s&&(i=s==null?void 0:s.userData)!=null&&i.LODS){const r=s.userData.LODS;if(o=this.lodInfos.get(r.key),e===void 0)return o!=null;if(o)return Array.isArray(o.lods)?e<o.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof g.Mesh||t.isMesh===!0){const o=t.geometry,i=this.getAssignedLODInformation(o);if(!i)return Promise.resolve(null);for(const r of H)(s=r.onBeforeGetLODMesh)==null||s.call(r,t,e);return t["LOD:requested level"]=e,_.getOrLoadLOD(o,e).then(r=>{if(Array.isArray(r)){const n=i.index||0;r=r[n]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],r&&o!=r&&((r==null?void 0:r.isBufferGeometry)?(t.geometry=r,y&&be(t,"geometry",i.url)):y&&console.error("Invalid LOD geometry",r))),r}).catch(r=>(console.error("Error loading mesh LOD",t,r),null))}else y&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",t);return Promise.resolve(null)}static assignTextureLOD(t,e=0){if(!t)return Promise.resolve(null);if(t.isMesh===!0){const s=t;if(Array.isArray(s.material)){const o=new Array;for(const i of s.material){const r=this.assignTextureLOD(i,e);o.push(r)}return Promise.all(o).then(i=>{const r=new Array;for(const n of i)Array.isArray(n)&&r.push(...n);return r})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,o=[],i=new Array;if(y&&_e.add(s),s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const r=s;for(const n of Object.keys(r.uniforms)){const a=r.uniforms[n].value;if((a==null?void 0:a.isTexture)===!0){const c=this.assignTextureLODForSlot(a,e,s,n).then(l=>(l&&r.uniforms[n].value!=l&&(r.uniforms[n].value=l,r.uniformsNeedUpdate=!0),l));o.push(c),i.push(n)}}}else for(const r of Object.keys(s)){const n=s[r];if((n==null?void 0:n.isTexture)===!0){const a=this.assignTextureLODForSlot(n,e,s,r);o.push(a),i.push(r)}}return Promise.all(o).then(r=>{const n=new Array;for(let a=0;a<r.length;a++){const c=r[a],l=i[a];c&&c.isTexture===!0?n.push({material:s,slot:l,texture:c,level:e}):n.push({material:s,slot:l,texture:null,level:e})}return n})}if(t instanceof g.Texture||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,o){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):o==="glyphMap"?Promise.resolve(t):_.getOrLoadLOD(t,e).then(i=>{if(Array.isArray(i))return null;if((i==null?void 0:i.isTexture)===!0){if(i!=t){if(s&&o){const r=s[o];if(r&&!y){const n=this.getAssignedLODInformation(r);if(n&&(n==null?void 0:n.level)<e)return y==="verbose"&&console.warn("Assigned texture level is already higher: ",n.level,e,s,r,i),null}s[o]=i}if(y&&o&&s){const r=this.getAssignedLODInformation(t);r?be(s,o,r.url):console.warn("No LOD info for texture",t)}}return i}else y=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(i=>(console.error("Error loading LOD",t,i),null))}afterRoot(t){var e,s;return y&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((o,i)=>{var r;if(o!=null&&o.extensions){const n=o==null?void 0:o.extensions[z];if(n){if(!n.lods){y&&console.warn("Texture has no LODs",n);return}let a=!1;for(const c of this.parser.associations.keys())if(c.isTexture===!0){const l=this.parser.associations.get(c);(l==null?void 0:l.textures)===i&&(a=!0,_.registerTexture(this.url,c,(r=n.lods)==null?void 0:r.length,i,n))}a||this.parser.getDependency("texture",i).then(c=>{var l;c&&_.registerTexture(this.url,c,(l=n.lods)==null?void 0:l.length,i,n)})}}}),(s=this.parser.json.meshes)==null||s.forEach((o,i)=>{if(o!=null&&o.extensions){const r=o==null?void 0:o.extensions[z];if(r&&r.lods){for(const n of this.parser.associations.keys())if(n.isMesh){const a=this.parser.associations.get(n);(a==null?void 0:a.meshes)===i&&_.registerMesh(this.url,r.guid,n,r.lods.length,a.primitives,r)}}}}),null}static async getOrLoadLOD(t,e){var n,a,c,l;const s=y=="verbose",o=t.userData.LODS;if(!o)return null;const i=o==null?void 0:o.key;let r;if(t.isTexture===!0){const d=t;d.source&&d.source[Me]&&(r=d.source[Me])}if(r||(r=_.lodInfos.get(i)),r){if(e>0){let D=!1;const m=Array.isArray(r.lods);if(m&&e>=r.lods.length?D=!0:m||(D=!0),D)return this.lowresCache.get(i)}const d=Array.isArray(r.lods)?(n=r.lods[e])==null?void 0:n.path:r.lods;if(!d)return y&&!r["missing:uri"]&&(r["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,r)),null;const h=ze(o.url,d);if(h.endsWith(".glb")||h.endsWith(".gltf")){if(!r.guid)return console.warn("missing pointer for glb/gltf texture",r),null;const D=h+"_"+r.guid,m=this.previouslyLoaded.get(D);if(m!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${D}`);let f=await m.catch(N=>(console.error(`Error loading LOD ${e} from ${h}
|
|
1
|
+
"use strict";const g=require("./three.light.umd.cjs"),Z=require("./three-examples.light.umd.cjs"),Fe="";globalThis.GLTF_PROGRESSIVE_VERSION=Fe;console.debug("[gltf-progressive] version -");let F="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",j="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Ue=F,Ve=j,Pe=new URL(F+"draco_decoder.js");Pe.searchParams.append("range","true");fetch(Pe,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${F} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),F===Ue&&Ce("./include/draco/"),j===Ve&&Be("./include/ktx2/")}).finally(()=>{ke()});function Ce(t){F=t,C&&C[me]!=F?(console.debug("Updating Draco decoder path to "+t),C[me]=F,C.setDecoderPath(F),C.preload()):console.debug("Setting Draco decoder path to "+t)}function Be(t){j=t,$&&$.transcoderPath!=j?(console.debug("Updating KTX2 transcoder path to "+t),$.setTranscoderPath(j),$.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let C,de,$;function ke(){C||(C=new Z.DRACOLoader,C[me]=F,C.setDecoderPath(F),C.setDecoderConfig({type:"js"}),C.preload()),$||($=new Z.KTX2Loader,$.setTranscoderPath(j),$.init()),de||(de=Z.MeshoptDecoder)}function we(t){return ke(),t?$.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:C,ktx2Loader:$,meshoptDecoder:de}}function Oe(t){t.dracoLoader||t.setDRACOLoader(C),t.ktx2Loader||t.setKTX2Loader($),t.meshoptDecoder||t.setMeshoptDecoder(de)}const xe=new WeakMap;function Se(t,e){let s=xe.get(t);s?s=Object.assign(s,e):s=e,xe.set(t,s)}const pe=Z.GLTFLoader.prototype.load;function We(...t){const e=xe.get(this);let s=t[0];const o=new URL(s,window.location.href);if(o.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}`,s=o.toString()}return t[0]=s,pe==null?void 0:pe.call(this,...t)}Z.GLTFLoader.prototype.load=We;ne("debugprogressive");function ne(t){if(typeof window>"u")return!1;const s=new URL(window.location.href).searchParams.get(t);return s==null||s==="0"||s==="false"?!1:s===""?!0:s}function ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const s=t.lastIndexOf("/");if(s>=0){const o=t.substring(0,s+1);for(;o.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return o+e}return e}let te;function Xe(){return te!==void 0||(te=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ne("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",te)),te}const qe=typeof window>"u"&&typeof document>"u",De=Symbol("needle:raycast-mesh");function ee(t){return(t==null?void 0:t[De])instanceof g.BufferGeometry?t[De]:null}function Ke(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!ee(t)){const o=He(e);o.userData={isRaycastMesh:!0},t[De]=o}}function Ye(t=!0){if(t){if(se)return;const e=se=g.Mesh.prototype.raycast;g.Mesh.prototype.raycast=function(s,o){const i=this,r=ee(i);let n;r&&i.isMesh&&(n=i.geometry,i.geometry=r),e.call(this,s,o),n&&(i.geometry=n)}}else{if(!se)return;g.Mesh.prototype.raycast=se,se=null}}let se=null;function He(t){const e=new g.BufferGeometry;for(const s in t.attributes)e.setAttribute(s,t.getAttribute(s));return e.setIndex(t.getIndex()),e}const H=new Array,z="NEEDLE_progressive",y=ne("debugprogressive"),Me=Symbol("needle-progressive-texture"),oe=new Map,_e=new Set;if(y){let t=function(){e+=1,console.log("Toggle LOD level",e,oe),oe.forEach((i,r)=>{for(const n of i.keys){const a=r[n];if(a!=null)if(a.isBufferGeometry===!0){const c=E.getMeshLODInformation(a),l=c?Math.min(e,c.lods.length):0;r["DEBUG:LOD"]=l,c&&(s=Math.max(s,c.lods.length-1))}else r.isMaterial===!0&&(r["DEBUG:LOD"]=e)}}),e>=s&&(e=-1)},e=-1,s=2,o=!1;window.addEventListener("keyup",i=>{i.key==="p"&&t(),i.key==="w"&&(o=!o,_e&&_e.forEach(r=>{r.name!="BackgroundCubeMaterial"&&r.glyphMap==null&&"wireframe"in r&&(r.wireframe=o)}))})}function be(t,e,s){var i;if(!y)return;oe.has(t)||oe.set(t,{keys:[],sourceId:s});const o=oe.get(t);((i=o==null?void 0:o.keys)==null?void 0:i.includes(e))==!1&&o.keys.push(e)}const _=class{constructor(t,e){this.loadMesh=s=>{var i,r;if(this._isLoadingMesh)return null;const o=(r=(i=this.parser.json.meshes[s])==null?void 0:i.extensions)==null?void 0:r[z];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",s).then(n=>{var a;return this._isLoadingMesh=!1,n&&_.registerMesh(this.url,o.guid,n,(a=o.lods)==null?void 0:a.length,void 0,o),n})):null},y&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return z}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const s=this,o="LODS:minmax",i=t[o];if(i!=null)return i;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const n of t)this.getMaterialMinMaxLODsCount(n,e);return t[o]=e,e}if(y==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const n=t;for(const a of Object.keys(n.uniforms)){const c=n.uniforms[a].value;(c==null?void 0:c.isTexture)===!0&&r(c,e)}}else if(t.isMaterial)for(const n of Object.keys(t)){const a=t[n];(a==null?void 0:a.isTexture)===!0&&r(a,e)}return t[o]=e,e;function r(n,a){const c=s.getAssignedLODInformation(n);if(c){const l=s.lodInfos.get(c.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let d=0;d<l.lods.length;d++){const h=l.lods[d];h.width&&(a.lods[d]=a.lods[d]||{min_height:1/0,max_height:0},a.lods[d].min_height=Math.min(a.lods[d].min_height,h.height),a.lods[d].max_height=Math.max(a.lods[d].max_height,h.height))}}}}}static hasLODLevelAvailable(t,e){var i;if(Array.isArray(t)){for(const r of t)if(this.hasLODLevelAvailable(r,e))return!0;return!1}if(t.isMaterial===!0){for(const r of Object.keys(t)){const n=t[r];if(n&&n.isTexture&&this.hasLODLevelAvailable(n,e))return!0}return!1}else if(t.isGroup===!0){for(const r of t.children)if(r.isMesh===!0&&this.hasLODLevelAvailable(r,e))return!0}let s,o;if(t.isMesh?s=t.geometry:(t.isBufferGeometry||t.isTexture)&&(s=t),s&&(i=s==null?void 0:s.userData)!=null&&i.LODS){const r=s.userData.LODS;if(o=this.lodInfos.get(r.key),e===void 0)return o!=null;if(o)return Array.isArray(o.lods)?e<o.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof g.Mesh||t.isMesh===!0){const o=t.geometry,i=this.getAssignedLODInformation(o);if(!i)return Promise.resolve(null);for(const r of H)(s=r.onBeforeGetLODMesh)==null||s.call(r,t,e);return t["LOD:requested level"]=e,_.getOrLoadLOD(o,e).then(r=>{if(Array.isArray(r)){const n=i.index||0;r=r[n]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],r&&o!=r&&((r==null?void 0:r.isBufferGeometry)?(t.geometry=r,y&&be(t,"geometry",i.url)):y&&console.error("Invalid LOD geometry",r))),r}).catch(r=>(console.error("Error loading mesh LOD",t,r),null))}else y&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",t);return Promise.resolve(null)}static assignTextureLOD(t,e=0){if(!t)return Promise.resolve(null);if(t.isMesh===!0){const s=t;if(Array.isArray(s.material)){const o=new Array;for(const i of s.material){const r=this.assignTextureLOD(i,e);o.push(r)}return Promise.all(o).then(i=>{const r=new Array;for(const n of i)Array.isArray(n)&&r.push(...n);return r})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,o=[],i=new Array;if(y&&_e.add(s),s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const r=s;for(const n of Object.keys(r.uniforms)){const a=r.uniforms[n].value;if((a==null?void 0:a.isTexture)===!0){const c=this.assignTextureLODForSlot(a,e,s,n).then(l=>(l&&r.uniforms[n].value!=l&&(r.uniforms[n].value=l,r.uniformsNeedUpdate=!0),l));o.push(c),i.push(n)}}}else for(const r of Object.keys(s)){const n=s[r];if((n==null?void 0:n.isTexture)===!0){const a=this.assignTextureLODForSlot(n,e,s,r);o.push(a),i.push(r)}}return Promise.all(o).then(r=>{const n=new Array;for(let a=0;a<r.length;a++){const c=r[a],l=i[a];c&&c.isTexture===!0?n.push({material:s,slot:l,texture:c,level:e}):n.push({material:s,slot:l,texture:null,level:e})}return n})}if(t instanceof g.Texture||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,o){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):o==="glyphMap"?Promise.resolve(t):_.getOrLoadLOD(t,e).then(i=>{if(Array.isArray(i))return null;if((i==null?void 0:i.isTexture)===!0){if(i!=t){if(s&&o){const r=s[o];if(r&&!y){const n=this.getAssignedLODInformation(r);if(n&&(n==null?void 0:n.level)<e)return y==="verbose"&&console.warn("Assigned texture level is already higher: ",n.level,e,s,r,i),null}s[o]=i}if(y&&o&&s){const r=this.getAssignedLODInformation(t);r?be(s,o,r.url):console.warn("No LOD info for texture",t)}}return i}else y=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(i=>(console.error("Error loading LOD",t,i),null))}afterRoot(t){var e,s;return y&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((o,i)=>{var r;if(o!=null&&o.extensions){const n=o==null?void 0:o.extensions[z];if(n){if(!n.lods){y&&console.warn("Texture has no LODs",n);return}let a=!1;for(const c of this.parser.associations.keys())if(c.isTexture===!0){const l=this.parser.associations.get(c);(l==null?void 0:l.textures)===i&&(a=!0,_.registerTexture(this.url,c,(r=n.lods)==null?void 0:r.length,i,n))}a||this.parser.getDependency("texture",i).then(c=>{var l;c&&_.registerTexture(this.url,c,(l=n.lods)==null?void 0:l.length,i,n)})}}}),(s=this.parser.json.meshes)==null||s.forEach((o,i)=>{if(o!=null&&o.extensions){const r=o==null?void 0:o.extensions[z];if(r&&r.lods){for(const n of this.parser.associations.keys())if(n.isMesh){const a=this.parser.associations.get(n);(a==null?void 0:a.meshes)===i&&_.registerMesh(this.url,r.guid,n,r.lods.length,a.primitives,r)}}}}),null}static async getOrLoadLOD(t,e){var n,a,c,l;const s=y=="verbose",o=t.userData.LODS;if(!o)return null;const i=o==null?void 0:o.key;let r;if(t.isTexture===!0){const d=t;d.source&&d.source[Me]&&(r=d.source[Me])}if(r||(r=_.lodInfos.get(i)),r){if(e>0){let D=!1;const m=Array.isArray(r.lods);if(m&&e>=r.lods.length?D=!0:m||(D=!0),D)return this.lowresCache.get(i)}const d=Array.isArray(r.lods)?(n=r.lods[e])==null?void 0:n.path:r.lods;if(!d)return y&&!r["missing:uri"]&&(r["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,r)),null;const h=ze(o.url,d);if(h.endsWith(".glb")||h.endsWith(".gltf")){if(!r.guid)return console.warn("missing pointer for glb/gltf texture",r),null;const D=h+"_"+r.guid,m=this.previouslyLoaded.get(D);if(m!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${D}`);let f=await m.catch(N=>(console.error(`Error loading LOD ${e} from ${h}
|
|
2
2
|
`,N),null)),O=!1;if(f==null||(f instanceof g.Texture&&t instanceof g.Texture?(a=f.image)!=null&&a.data||(c=f.source)!=null&&c.data?f=this.copySettings(t,f):(O=!0,this.previouslyLoaded.delete(D)):f instanceof g.BufferGeometry&&t instanceof g.BufferGeometry&&((l=f.attributes.position)!=null&&l.array||(O=!0,this.previouslyLoaded.delete(D)))),!O)return f}const M=r,k=new Promise(async(f,O)=>{const N=new Z.GLTFLoader;Oe(N),y&&(await new Promise(S=>setTimeout(S,1e3)),s&&console.warn("Start loading (delayed) "+h,M.guid));let R=h;if(M&&Array.isArray(M.lods)){const S=M.lods[e];S.hash&&(R+="?v="+S.hash)}const A=await N.loadAsync(R).catch(S=>(console.error(`Error loading LOD ${e} from ${h}
|
|
3
3
|
`,S),null));if(!A)return null;const V=A.parser;s&&console.log("Loading finished "+h,M.guid);let w=0;if(A.parser.json.textures){let S=!1;for(const u of A.parser.json.textures){if(u!=null&&u.extensions){const p=u==null?void 0:u.extensions[z];if(p!=null&&p.guid&&p.guid===M.guid){S=!0;break}}w++}if(S){let u=await V.getDependency("texture",w);return u&&_.assignLODInformation(o.url,u,i,e,void 0,void 0),s&&console.log('change "'+t.name+'" → "'+u.name+'"',h,w,u,D),t instanceof g.Texture&&(u=this.copySettings(t,u)),u&&(u.guid=M.guid),f(u)}else y&&console.warn("Could not find texture with guid",M.guid,A.parser.json)}if(w=0,A.parser.json.meshes){let S=!1;for(const u of A.parser.json.meshes){if(u!=null&&u.extensions){const p=u==null?void 0:u.extensions[z];if(p!=null&&p.guid&&p.guid===M.guid){S=!0;break}}w++}if(S){const u=await V.getDependency("mesh",w),p=M;if(s&&console.log(`Loaded Mesh "${u.name}"`,h,w,u,D),u.isMesh===!0){const v=u.geometry;return _.assignLODInformation(o.url,v,i,e,void 0,p.density),f(v)}else{const v=new Array;for(let b=0;b<u.children.length;b++){const P=u.children[b];if(P.isMesh===!0){const X=P.geometry;_.assignLODInformation(o.url,X,i,e,b,p.density),v.push(X)}}return f(v)}}else y&&console.warn("Could not find mesh with guid",M.guid,A.parser.json)}return f(null)});return this.previouslyLoaded.set(D,k),await k}else if(t instanceof g.Texture){s&&console.log("Load texture from uri: "+h);const m=await new g.TextureLoader().loadAsync(h);return m?(m.guid=r.guid,m.flipY=!1,m.needsUpdate=!0,m.colorSpace=t.colorSpace,s&&console.log(r,m)):y&&console.warn("failed loading",h),m}}else y&&console.warn(`Can not load LOD ${e}: no LOD info found for "${i}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,s,o,i,r){if(!e)return;e.userData||(e.userData={});const n=new Je(t,s,o,i,r);e.userData.LODS=n}static getAssignedLODInformation(t){var e;return((e=t==null?void 0:t.userData)==null?void 0:e.LODS)||null}static copySettings(t,e){return e?(y&&console.warn(`Copy texture settings
|
|
4
4
|
`,t.uuid,`
|
|
5
|
-
`,e.uuid),e=e.clone(),e.offset=t.offset,e.repeat=t.repeat,e.colorSpace=t.colorSpace,e.magFilter=t.magFilter,e.minFilter=t.minFilter,e.wrapS=t.wrapS,e.wrapT=t.wrapT,e.flipY=t.flipY,e.anisotropy=t.anisotropy,e.mipmaps||(e.generateMipmaps=t.generateMipmaps),e):t}};let E=_;E.registerTexture=(t,e,s,o,i)=>{if(y&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,i),!e){y&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Me]=i);const r=i.guid;_.assignLODInformation(t,e,r,s,o,void 0),_.lodInfos.set(r,i),_.lowresCache.set(r,e)};E.registerMesh=(t,e,s,o,i,r)=>{var c;y&&console.log("> Progressive: register mesh",i,s.name,r,s.uuid,s);const n=s.geometry;if(!n){y&&console.warn("gltf-progressive: Register mesh without geometry");return}n.userData||(n.userData={}),_.assignLODInformation(t,n,e,o,i,r.density),_.lodInfos.set(e,r);let a=_.lowresCache.get(e);a?a.push(s.geometry):a=[s.geometry],_.lowresCache.set(e,a),o>0&&!ee(s)&&Ke(s,n);for(const l of H)(c=l.onRegisteredNewMesh)==null||c.call(l,s,r)};E.lodInfos=new Map;E.previouslyLoaded=new Map;E.lowresCache=new Map;class Je{constructor(e,s,o,i,r){this.url=e,this.key=s,this.level=o,i!=null&&(this.index=i),r!=null&&(this.density=r)}}var ke=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},L=(t,e,s)=>(ke(t,e,"read from private field"),s?s.call(t):e.get(t)),J=(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,o)=>(ke(t,e,"write to private field"),o?o.call(t,s):e.set(t,s),s),T,K,ve,Q,ie,fe,Y;const G=ne("debugprogressive"),Qe=ne("noprogressive"),ye=Symbol("Needle:LODSManager"),Le=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),I={mesh_lod:-1,texture_lod:-1},B=class{constructor(t,e){this.projectionScreenMatrix=new g.Matrix4,this.targetTriangleDensity=2e5,this.updateInterval="auto",J(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],J(this,K,void 0),J(this,ve,new g.Clock),J(this,Q,0),J(this,ie,0),J(this,fe,0),J(this,Y,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new g.Sphere,this._tempBox=new g.Box3,this._tempBox2=new g.Box3,this.tempMatrix=new g.Matrix4,this._tempWorldPosition=new g.Vector3,this._tempBoxSize=new g.Vector3,this._tempBox2Size=new g.Vector3,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Le]}static addPlugin(t){H.push(t)}static removePlugin(t){const e=H.indexOf(t);e>=0&&H.splice(e,1)}static get(t,e){if(t[ye])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ye];const s=new B(t,{engine:"unknown",...e});return t[ye]=s,s}get plugins(){return 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(L(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,o){const i=e.renderer.getRenderTarget();(i==null||"isXRRenderTarget"in i&&i.isXRRenderTarget)&&(t=0,W(e,Q,L(e,Q)+1),W(e,ie,L(e,ve).getDelta()),W(e,fe,L(e,fe)+L(e,ie)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/L(e,ie)),W(e,Y,e._fpsBuffer.reduce((n,a)=>n+a)/e._fpsBuffer.length),G&&L(e,Q)%200===0&&console.log("FPS",Math.round(L(e,Y)),"Interval:",L(e,T)));const r=t++;L(e,K).call(this,s,o),e.onAfterRender(s,o,r)}}disable(){L(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=L(this,K),W(this,K,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,s){if(this.pause)return;const i=this.renderer.renderLists.get(t,0).opaque;let r=!0;if(i.length===1){const n=i[0].material;(n.name==="EffectMaterial"||n.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(Qe||(this.updateInterval==="auto"?L(this,Y)<40&&L(this,T)<10?(W(this,T,L(this,T)+1),G&&console.warn("↓ Reducing LOD updates",L(this,T),L(this,Y).toFixed(0))):L(this,Y)>=60&&L(this,T)>1&&(W(this,T,L(this,T)-1),G&&console.warn("↑ Increasing LOD updates",L(this,T),L(this,Y).toFixed(0))):W(this,T,this.updateInterval),L(this,T)>0&&L(this,Q)%L(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var a,c;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((a=l.geometry)==null?void 0:a.type)==="BoxGeometry"||((c=l.geometry)==null?void 0:c.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){G&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const h=Math.random()*16777215,D=new g.MeshStandardMaterial({color:h});l.object.material=D}const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const r=s.transparent;for(const l of r){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const n=s.transmissive;for(const l of n){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}}updateLODs(t,e,s,o){var n,a;s.userData||(s.userData={});let i=s[Le];if(i||(i=new Ze,s[Le]=i),i.frames++<2)return;for(const c of H)(n=c.onBeforeUpdateLOD)==null||n.call(c,this.renderer,t,e,s);this.calculateLodLevel(e,s,i,o,I),I.mesh_lod=Math.round(I.mesh_lod),I.texture_lod=Math.round(I.texture_lod),I.mesh_lod>=0&&this.loadProgressiveMeshes(s,I.mesh_lod);let r=I.texture_lod;s.material&&r>=0&&this.loadProgressiveTextures(s.material,r);for(const c of H)(a=c.onAfterUpdatedLOD)==null||a.call(c,this.renderer,t,e,s,I);i.lastLodLevel_Mesh=I.mesh_lod,i.lastLodLevel_Texture=I.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const i of t)this.loadProgressiveTextures(i,e);return}let s=!1;(t[q]===void 0||e<t[q])&&(s=!0);const o=t["DEBUG:LOD"];o!=null&&(s=t[q]!=o,e=o),s&&(t[q]=e,E.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(r=>r({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[q]!==e;const o=t["DEBUG:LOD"];if(o!=null&&(s=t[q]!=o,e=o),s){t[q]=e;const i=t.geometry;return E.assignMeshLOD(t,e).then(r=>(r&&t[q]==e&&i!=t.geometry&&this._lodchangedlisteners.forEach(n=>n({type:"mesh",level:e,object:t})),r))}return Promise.resolve(null)}static isInside(t,e){const s=t.min,o=t.max,i=(s.x+o.x)*.5,r=(s.y+o.y)*.5;return this._tempPtInside.set(i,r,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,o,i){var k;if(!e){i.mesh_lod=-1,i.texture_lod=-1;return}if(!t){i.mesh_lod=-1,i.texture_lod=-1;return}let n=10+1,a=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const c=E.getMeshLODInformation(e.geometry),l=c==null?void 0:c.lods,d=l&&l.length>0,h=E.getMaterialMinMaxLODsCount(e.material),D=(h==null?void 0:h.min_count)!=1/0&&h.min_count>0&&h.max_count>0;if(!d&&!D){i.mesh_lod=0,i.texture_lod=0;return}d||(a=!0,n=0);const m=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let M=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const x=e;if(!x.boundingBox)x.computeBoundingBox();else if(s.frames%30===0){const f=ee(x),O=x.geometry;f&&(x.geometry=f),x.computeBoundingBox(),x.geometry=O}M=x.boundingBox}if(M){const x=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 u=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(u)){i.mesh_lod=0,i.texture_lod=0;return}}if(this._tempBox.copy(M),this._tempBox.applyMatrix4(e.matrixWorld),x.isPerspectiveCamera&&B.isInside(this._tempBox,this.projectionScreenMatrix)){i.mesh_lod=0,i.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&x.isPerspectiveCamera&&x.fov>70){const u=this._tempBox.min,p=this._tempBox.max;let v=u.x,b=u.y,P=p.x,X=p.y;const ae=2,he=1.5,le=(u.x+p.x)*.5,ce=(u.y+p.y)*.5;v=(v-le)*ae+le,b=(b-ce)*ae+ce,P=(P-le)*ae+le,X=(X-ce)*ae+ce;const Ie=v<0&&P>0?0:Math.min(Math.abs(u.x),Math.abs(p.x)),$e=b<0&&X>0?0:Math.min(Math.abs(u.y),Math.abs(p.y)),ge=Math.max(Ie,$e);s.lastCentrality=(he-ge)*(he-ge)*(he-ge)}else s.lastCentrality=1;const f=this._tempBox.getSize(this._tempBoxSize);f.multiplyScalar(.5),screen.availHeight>0&&m>0&&f.multiplyScalar(m/screen.availHeight),t.isPerspectiveCamera?f.x*=t.aspect:t.isOrthographicCamera;const O=t.matrixWorldInverse,N=this._tempBox2;N.copy(M),N.applyMatrix4(e.matrixWorld),N.applyMatrix4(O);const R=N.getSize(this._tempBox2Size),A=Math.max(R.x,R.y);if(Math.max(f.x,f.y)!=0&&A!=0&&(f.z=R.z/Math.max(R.x,R.y)*Math.max(f.x,f.y)),s.lastScreenCoverage=Math.max(f.x,f.y,f.z),s.lastScreenspaceVolume.copy(f),s.lastScreenCoverage*=s.lastCentrality,G&&B.debugDrawLine){const u=this.tempMatrix.copy(this.projectionScreenMatrix);u.invert();const p=B.corner0,v=B.corner1,b=B.corner2,P=B.corner3;p.copy(this._tempBox.min),v.copy(this._tempBox.max),v.x=p.x,b.copy(this._tempBox.max),b.y=p.y,P.copy(this._tempBox.max);const X=(p.z+P.z)*.5;p.z=v.z=b.z=P.z=X,p.applyMatrix4(u),v.applyMatrix4(u),b.applyMatrix4(u),P.applyMatrix4(u),B.debugDrawLine(p,v,255),B.debugDrawLine(p,b,255),B.debugDrawLine(v,P,255),B.debugDrawLine(b,P,255)}let w=999;if(l&&s.lastScreenCoverage>0){for(let u=0;u<l.length;u++)if(l[u].density/s.lastScreenCoverage<o){w=u;break}}w<n&&(n=w,a=!0)}if(a?i.mesh_lod=n:i.mesh_lod=s.lastLodLevel_Mesh,G&&i.mesh_lod!=s.lastLodLevel_Mesh){const f=l==null?void 0:l[i.mesh_lod];f&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${i.mesh_lod} (${f.density.toFixed(0)}) - ${e.name}`)}if(D){const x="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(i.texture_lod=h.max_count-1,G){const f=h.lods[h.max_count-1];G&&console.log(`First Texture LOD ${i.texture_lod} (${f.max_height}px) - ${e.name}`)}}else{const f=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let O=s.lastScreenCoverage*4;((k=this.context)==null?void 0:k.engine)==="model-viewer"&&(O*=1.5);const R=m/window.devicePixelRatio*O;let A=!1;for(let V=h.lods.length-1;V>=0;V--){let w=h.lods[V];if(!(x&&w.max_height>=2048)&&!(Xe()&&w.max_height>4096)&&(w.max_height>R||!A&&V===0)){if(A=!0,i.texture_lod=V,i.texture_lod<s.lastLodLevel_Texture){const S=w.max_height;G&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${i.texture_lod} = ${S}px
|
|
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 E=_;E.registerTexture=(t,e,s,o,i)=>{if(y&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,i),!e){y&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Me]=i);const r=i.guid;_.assignLODInformation(t,e,r,s,o,void 0),_.lodInfos.set(r,i),_.lowresCache.set(r,e)};E.registerMesh=(t,e,s,o,i,r)=>{var c;y&&console.log("> Progressive: register mesh",i,s.name,r,s.uuid,s);const n=s.geometry;if(!n){y&&console.warn("gltf-progressive: Register mesh without geometry");return}n.userData||(n.userData={}),_.assignLODInformation(t,n,e,o,i,r.density),_.lodInfos.set(e,r);let a=_.lowresCache.get(e);a?a.push(s.geometry):a=[s.geometry],_.lowresCache.set(e,a),o>0&&!ee(s)&&Ke(s,n);for(const l of H)(c=l.onRegisteredNewMesh)==null||c.call(l,s,r)};E.lodInfos=new Map;E.previouslyLoaded=new Map;E.lowresCache=new Map;class Je{constructor(e,s,o,i,r){this.url=e,this.key=s,this.level=o,i!=null&&(this.index=i),r!=null&&(this.density=r)}}var Re=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},L=(t,e,s)=>(Re(t,e,"read from private field"),s?s.call(t):e.get(t)),J=(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,o)=>(Re(t,e,"write to private field"),o?o.call(t,s):e.set(t,s),s),T,K,ve,Q,ie,fe,Y;const G=ne("debugprogressive"),Qe=ne("noprogressive"),ye=Symbol("Needle:LODSManager"),Le=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),I={mesh_lod:-1,texture_lod:-1},B=class{constructor(t,e){this.projectionScreenMatrix=new g.Matrix4,this.targetTriangleDensity=2e5,this.updateInterval="auto",J(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],J(this,K,void 0),J(this,ve,new g.Clock),J(this,Q,0),J(this,ie,0),J(this,fe,0),J(this,Y,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new g.Sphere,this._tempBox=new g.Box3,this._tempBox2=new g.Box3,this.tempMatrix=new g.Matrix4,this._tempWorldPosition=new g.Vector3,this._tempBoxSize=new g.Vector3,this._tempBox2Size=new g.Vector3,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Le]}static addPlugin(t){H.push(t)}static removePlugin(t){const e=H.indexOf(t);e>=0&&H.splice(e,1)}static get(t,e){if(t[ye])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ye];const s=new B(t,{engine:"unknown",...e});return t[ye]=s,s}get plugins(){return 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(L(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,o){const i=e.renderer.getRenderTarget();(i==null||"isXRRenderTarget"in i&&i.isXRRenderTarget)&&(t=0,W(e,Q,L(e,Q)+1),W(e,ie,L(e,ve).getDelta()),W(e,fe,L(e,fe)+L(e,ie)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/L(e,ie)),W(e,Y,e._fpsBuffer.reduce((n,a)=>n+a)/e._fpsBuffer.length),G&&L(e,Q)%200===0&&console.log("FPS",Math.round(L(e,Y)),"Interval:",L(e,T)));const r=t++;L(e,K).call(this,s,o),e.onAfterRender(s,o,r)}}disable(){L(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=L(this,K),W(this,K,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,s){if(this.pause)return;const i=this.renderer.renderLists.get(t,0).opaque;let r=!0;if(i.length===1){const n=i[0].material;(n.name==="EffectMaterial"||n.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(Qe||(this.updateInterval==="auto"?L(this,Y)<40&&L(this,T)<10?(W(this,T,L(this,T)+1),G&&console.warn("↓ Reducing LOD updates",L(this,T),L(this,Y).toFixed(0))):L(this,Y)>=60&&L(this,T)>1&&(W(this,T,L(this,T)-1),G&&console.warn("↑ Increasing LOD updates",L(this,T),L(this,Y).toFixed(0))):W(this,T,this.updateInterval),L(this,T)>0&&L(this,Q)%L(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var a,c;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((a=l.geometry)==null?void 0:a.type)==="BoxGeometry"||((c=l.geometry)==null?void 0:c.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){G&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const h=Math.random()*16777215,D=new g.MeshStandardMaterial({color:h});l.object.material=D}const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const r=s.transparent;for(const l of r){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const n=s.transmissive;for(const l of n){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}}updateLODs(t,e,s,o){var n,a;s.userData||(s.userData={});let i=s[Le];if(i||(i=new Ze,s[Le]=i),i.frames++<2)return;for(const c of H)(n=c.onBeforeUpdateLOD)==null||n.call(c,this.renderer,t,e,s);this.calculateLodLevel(e,s,i,o,I),I.mesh_lod=Math.round(I.mesh_lod),I.texture_lod=Math.round(I.texture_lod),I.mesh_lod>=0&&this.loadProgressiveMeshes(s,I.mesh_lod);let r=I.texture_lod;s.material&&r>=0&&this.loadProgressiveTextures(s.material,r);for(const c of H)(a=c.onAfterUpdatedLOD)==null||a.call(c,this.renderer,t,e,s,I);i.lastLodLevel_Mesh=I.mesh_lod,i.lastLodLevel_Texture=I.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const i of t)this.loadProgressiveTextures(i,e);return}let s=!1;(t[q]===void 0||e<t[q])&&(s=!0);const o=t["DEBUG:LOD"];o!=null&&(s=t[q]!=o,e=o),s&&(t[q]=e,E.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(r=>r({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[q]!==e;const o=t["DEBUG:LOD"];if(o!=null&&(s=t[q]!=o,e=o),s){t[q]=e;const i=t.geometry;return E.assignMeshLOD(t,e).then(r=>(r&&t[q]==e&&i!=t.geometry&&this._lodchangedlisteners.forEach(n=>n({type:"mesh",level:e,object:t})),r))}return Promise.resolve(null)}static isInside(t,e){const s=t.min,o=t.max,i=(s.x+o.x)*.5,r=(s.y+o.y)*.5;return this._tempPtInside.set(i,r,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,o,i){var k;if(!e){i.mesh_lod=-1,i.texture_lod=-1;return}if(!t){i.mesh_lod=-1,i.texture_lod=-1;return}let n=10+1,a=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const c=E.getMeshLODInformation(e.geometry),l=c==null?void 0:c.lods,d=l&&l.length>0,h=E.getMaterialMinMaxLODsCount(e.material),D=(h==null?void 0:h.min_count)!=1/0&&h.min_count>0&&h.max_count>0;if(!d&&!D){i.mesh_lod=0,i.texture_lod=0;return}d||(a=!0,n=0);const m=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let M=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const x=e;if(!x.boundingBox)x.computeBoundingBox();else if(s.frames%30===0){const f=ee(x),O=x.geometry;f&&(x.geometry=f),x.computeBoundingBox(),x.geometry=O}M=x.boundingBox}if(M){const x=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 u=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(u)){i.mesh_lod=0,i.texture_lod=0;return}}if(this._tempBox.copy(M),this._tempBox.applyMatrix4(e.matrixWorld),x.isPerspectiveCamera&&B.isInside(this._tempBox,this.projectionScreenMatrix)){i.mesh_lod=0,i.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&x.isPerspectiveCamera&&x.fov>70){const u=this._tempBox.min,p=this._tempBox.max;let v=u.x,b=u.y,P=p.x,X=p.y;const ae=2,he=1.5,le=(u.x+p.x)*.5,ce=(u.y+p.y)*.5;v=(v-le)*ae+le,b=(b-ce)*ae+ce,P=(P-le)*ae+le,X=(X-ce)*ae+ce;const $e=v<0&&P>0?0:Math.min(Math.abs(u.x),Math.abs(p.x)),Ne=b<0&&X>0?0:Math.min(Math.abs(u.y),Math.abs(p.y)),ge=Math.max($e,Ne);s.lastCentrality=(he-ge)*(he-ge)*(he-ge)}else s.lastCentrality=1;const f=this._tempBox.getSize(this._tempBoxSize);f.multiplyScalar(.5),screen.availHeight>0&&m>0&&f.multiplyScalar(m/screen.availHeight),t.isPerspectiveCamera?f.x*=t.aspect:t.isOrthographicCamera;const O=t.matrixWorldInverse,N=this._tempBox2;N.copy(M),N.applyMatrix4(e.matrixWorld),N.applyMatrix4(O);const R=N.getSize(this._tempBox2Size),A=Math.max(R.x,R.y);if(Math.max(f.x,f.y)!=0&&A!=0&&(f.z=R.z/Math.max(R.x,R.y)*Math.max(f.x,f.y)),s.lastScreenCoverage=Math.max(f.x,f.y,f.z),s.lastScreenspaceVolume.copy(f),s.lastScreenCoverage*=s.lastCentrality,G&&B.debugDrawLine){const u=this.tempMatrix.copy(this.projectionScreenMatrix);u.invert();const p=B.corner0,v=B.corner1,b=B.corner2,P=B.corner3;p.copy(this._tempBox.min),v.copy(this._tempBox.max),v.x=p.x,b.copy(this._tempBox.max),b.y=p.y,P.copy(this._tempBox.max);const X=(p.z+P.z)*.5;p.z=v.z=b.z=P.z=X,p.applyMatrix4(u),v.applyMatrix4(u),b.applyMatrix4(u),P.applyMatrix4(u),B.debugDrawLine(p,v,255),B.debugDrawLine(p,b,255),B.debugDrawLine(v,P,255),B.debugDrawLine(b,P,255)}let w=999;if(l&&s.lastScreenCoverage>0){for(let u=0;u<l.length;u++)if(l[u].density/s.lastScreenCoverage<o){w=u;break}}w<n&&(n=w,a=!0)}if(a?i.mesh_lod=n:i.mesh_lod=s.lastLodLevel_Mesh,G&&i.mesh_lod!=s.lastLodLevel_Mesh){const f=l==null?void 0:l[i.mesh_lod];f&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${i.mesh_lod} (${f.density.toFixed(0)}) - ${e.name}`)}if(D){const x="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(i.texture_lod=h.max_count-1,G){const f=h.lods[h.max_count-1];G&&console.log(`First Texture LOD ${i.texture_lod} (${f.max_height}px) - ${e.name}`)}}else{const f=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let O=s.lastScreenCoverage*4;((k=this.context)==null?void 0:k.engine)==="model-viewer"&&(O*=1.5);const R=m/window.devicePixelRatio*O;let A=!1;for(let V=h.lods.length-1;V>=0;V--){let w=h.lods[V];if(!(x&&w.max_height>=2048)&&!(Xe()&&w.max_height>4096)&&(w.max_height>R||!A&&V===0)){if(A=!0,i.texture_lod=V,i.texture_lod<s.lastLodLevel_Texture){const S=w.max_height;G&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${i.texture_lod} = ${S}px
|
|
6
6
|
Screensize: ${R.toFixed(0)}px, Coverage: ${(100*s.lastScreenCoverage).toFixed(2)}%, Volume ${f.toFixed(1)}
|
|
7
|
-
${e.name}`)}break}}}}else i.texture_lod=0}};let U=B;T=new WeakMap;K=new WeakMap;ve=new WeakMap;Q=new WeakMap;ie=new WeakMap;fe=new WeakMap;Y=new WeakMap;U.corner0=new g.Vector3;U.corner1=new g.Vector3;U.corner2=new g.Vector3;U.corner3=new g.Vector3;U._tempPtInside=new g.Vector3;class Ze{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new g.Vector3,this.lastCentrality=0}}const Te=Symbol("NEEDLE_mesh_lod"),ue=Symbol("NEEDLE_texture_lod");let re=null;function
|
|
8
|
-
`,t.getAttribute("src"));let e=null,s=null,o=null;for(let i=t;i!=null;i=Object.getPrototypeOf(i)){const r=Object.getOwnPropertySymbols(i),n=r.find(l=>l.toString()=="Symbol(renderer)"),a=r.find(l=>l.toString()=="Symbol(scene)"),c=r.find(l=>l.toString()=="Symbol(needsRender)");!e&&n!=null&&(e=t[n].threeRenderer),!s&&a!=null&&(s=t[a]),!o&&c!=null&&(o=t[c])}if(e&&s){let i=function(){if(o){let n=0,a=setInterval(()=>{if(n++>5){clearInterval(a);return}o==null||o.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const r=U.get(e,{engine:"model-viewer"});return U.addPlugin(new tt),r.enable(),r.addEventListener("changed",()=>{o==null||o.call(t)}),t.addEventListener("model-visibility",n=>{n.detail.visible&&(o==null||o.call(t))}),t.addEventListener("load",()=>{i()}),()=>{r.disable()}}return null}class tt{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,s,o,i){this.tryParseMeshLOD(s,i),this.tryParseTextureLOD(s,i)}getUrl(e){if(!e)return null;let s=e.getAttribute("src");return s||(s=e.src),s||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),s}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,s){if(s[ue]==!0)return;s[ue]=!0;const o=this.tryGetCurrentGLTF(e),i=this.tryGetCurrentModelViewer(e),r=this.getUrl(i);if(r&&o&&s.material){let n=function(c){var d,h,D;if(c[ue]==!0)return;c[ue]=!0,c.userData&&(c.userData.LOD=-1);const l=Object.keys(c);for(let m=0;m<l.length;m++){const M=l[m],k=c[M];if((k==null?void 0:k.isTexture)===!0){const x=(h=(d=k.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(x==null)continue;const f=o.parser.json.textures[x];if(!f){console.warn("Texture data not found for texture index "+x);continue}if((D=f==null?void 0:f.extensions)!=null&&D[z]){const O=f.extensions[z];O&&r&&E.registerTexture(r,k,O.lods.length,x,O)}}}};const a=s.material;if(Array.isArray(a))for(const c of a)n(c);else n(a)}}tryParseMeshLOD(e,s){var n,a;if(s[Te]==!0)return;s[Te]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const r=(a=(n=s.userData)==null?void 0:n.gltfExtensions)==null?void 0:a[z];if(r&&i){const c=s.uuid;E.registerMesh(i,c,s,0,r.lods.length,r)}}}function st(t,e,s,o){we(e),Oe(s),Se(s,{progressive:!0,...o==null?void 0:o.hints}),s.register(r=>new E(r,t));const i=U.get(e);return(o==null?void 0:o.enableLODsManager)!==!1&&i.enable(),i}
|
|
7
|
+
${e.name}`)}break}}}}else i.texture_lod=0}};let U=B;T=new WeakMap;K=new WeakMap;ve=new WeakMap;Q=new WeakMap;ie=new WeakMap;fe=new WeakMap;Y=new WeakMap;U.corner0=new g.Vector3;U.corner1=new g.Vector3;U.corner2=new g.Vector3;U.corner3=new g.Vector3;U._tempPtInside=new g.Vector3;class Ze{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new g.Vector3,this.lastCentrality=0}}const Te=Symbol("NEEDLE_mesh_lod"),ue=Symbol("NEEDLE_texture_lod");let re=null;function Ge(){const t=je();t&&(t.mapURLs(function(e){return Ee(),e}),Ee(),re==null||re.disconnect(),re=new MutationObserver(e=>{e.forEach(s=>{s.addedNodes.forEach(o=>{o instanceof HTMLElement&&o.tagName.toLowerCase()==="model-viewer"&&Ie(o)})})}),re.observe(document,{childList:!0,subtree:!0}))}function je(){if(typeof customElements>"u")return null;const t=customElements.get("model-viewer");return t||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Ge()}),null)}function Ee(){if(typeof document>"u")return;document.querySelectorAll("model-viewer").forEach(e=>{Ie(e)})}const Ae=new WeakSet;let et=0;function Ie(t){if(!t||Ae.has(t))return null;Ae.add(t),console.debug("[gltf-progressive] found new model-viewer..."+ ++et+`
|
|
8
|
+
`,t.getAttribute("src"));let e=null,s=null,o=null;for(let i=t;i!=null;i=Object.getPrototypeOf(i)){const r=Object.getOwnPropertySymbols(i),n=r.find(l=>l.toString()=="Symbol(renderer)"),a=r.find(l=>l.toString()=="Symbol(scene)"),c=r.find(l=>l.toString()=="Symbol(needsRender)");!e&&n!=null&&(e=t[n].threeRenderer),!s&&a!=null&&(s=t[a]),!o&&c!=null&&(o=t[c])}if(e&&s){let i=function(){if(o){let n=0,a=setInterval(()=>{if(n++>5){clearInterval(a);return}o==null||o.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const r=U.get(e,{engine:"model-viewer"});return U.addPlugin(new tt),r.enable(),r.addEventListener("changed",()=>{o==null||o.call(t)}),t.addEventListener("model-visibility",n=>{n.detail.visible&&(o==null||o.call(t))}),t.addEventListener("load",()=>{i()}),()=>{r.disable()}}return null}class tt{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,s,o,i){this.tryParseMeshLOD(s,i),this.tryParseTextureLOD(s,i)}getUrl(e){if(!e)return null;let s=e.getAttribute("src");return s||(s=e.src),s||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),s}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,s){if(s[ue]==!0)return;s[ue]=!0;const o=this.tryGetCurrentGLTF(e),i=this.tryGetCurrentModelViewer(e),r=this.getUrl(i);if(r&&o&&s.material){let n=function(c){var d,h,D;if(c[ue]==!0)return;c[ue]=!0,c.userData&&(c.userData.LOD=-1);const l=Object.keys(c);for(let m=0;m<l.length;m++){const M=l[m],k=c[M];if((k==null?void 0:k.isTexture)===!0){const x=(h=(d=k.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(x==null)continue;const f=o.parser.json.textures[x];if(!f){console.warn("Texture data not found for texture index "+x);continue}if((D=f==null?void 0:f.extensions)!=null&&D[z]){const O=f.extensions[z];O&&r&&E.registerTexture(r,k,O.lods.length,x,O)}}}};const a=s.material;if(Array.isArray(a))for(const c of a)n(c);else n(a)}}tryParseMeshLOD(e,s){var n,a;if(s[Te]==!0)return;s[Te]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const r=(a=(n=s.userData)==null?void 0:n.gltfExtensions)==null?void 0:a[z];if(r&&i){const c=s.uuid;E.registerMesh(i,c,s,0,r.lods.length,r)}}}function st(t,e,s,o){we(e),Oe(s),Se(s,{progressive:!0,...o==null?void 0:o.hints}),s.register(r=>new E(r,t));const i=U.get(e);return(o==null?void 0:o.enableLODsManager)!==!1&&i.enable(),i}Ge();if(!qe){const t={gltfProgressive:{useNeedleProgressive:st,LODsManager:U,configureLoader:Se,getRaycastMesh:ee,useRaycastMeshes:Ye}};if(!globalThis.Needle)globalThis.Needle=t;else for(const e in t)globalThis.Needle[e]=t[e]}exports.LODsManager=U;exports.NEEDLE_progressive=E;exports.addDracoAndKTX2Loaders=Oe;exports.configureLoader=Se;exports.createLoaders=we;exports.getRaycastMesh=ee;exports.setDracoDecoderLocation=Ce;exports.setKTX2TranscoderLocation=Be;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{B as le,M as H,T as ee,a as Ne,V as $,b as Oe,S as Ue,c as Se,d as Fe,C as ze}from"./three.min.js";import{D as Ve,K as qe,G as ge,M as Ke}from"./three-examples.min.js";const He="";globalThis.GLTF_PROGRESSIVE_VERSION=He,console.debug("[gltf-progressive] version -");let G="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",J="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Xe=G,Ye=J,Je=new URL(G+"draco_decoder.js");fetch(Je,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${G} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),G===Xe&&Te("./include/draco/"),J===Ye&&Ee("./include/ktx2/")}).finally(()=>{Ae()});function Te(t){G=t,A&&A[me]!=G?(console.debug("Updating Draco decoder path to "+t),A[me]=G,A.setDecoderPath(G),A.preload()):console.debug("Setting Draco decoder path to "+t)}function Ee(t){J=t,C&&C.transcoderPath!=J?(console.debug("Updating KTX2 transcoder path to "+t),C.setTranscoderPath(J),C.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let A,ae,C;function Ae(){A||(A=new Ve,A[me]=G,A.setDecoderPath(G),A.setDecoderConfig({type:"js"}),A.preload()),C||(C=new qe,C.setTranscoderPath(J),C.init()),ae||(ae=Ke)}function pe(t){return Ae(),t?C.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:A,ktx2Loader:C,meshoptDecoder:ae}}function ve(t){t.dracoLoader||t.setDRACOLoader(A),t.ktx2Loader||t.setKTX2Loader(C),t.meshoptDecoder||t.setMeshoptDecoder(ae)}const ye=new WeakMap;function xe(t,e){let r=ye.get(t);r?r=Object.assign(r,e):r=e,ye.set(t,r)}const Pe=ge.prototype.load;function Qe(...t){const e=ye.get(this);let r=t[0];const n=new URL(r,window.location.href);if(n.hostname.endsWith("needle.tools")){const s=e?.progressive!==void 0?e.progressive:!0,o=e!=null&&e.usecase?e.usecase:"default";s?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${o}`:this.requestHeader.Accept=`*/*;usecase=${o}`,r=n.toString()}return t[0]=r,Pe?.call(this,...t)}ge.prototype.load=Qe,te("debugprogressive");function te(t){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(t);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function Ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const r=t.lastIndexOf("/");if(r>=0){const n=t.substring(0,r+1);for(;n.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return n+e}return e}let ue;function et(){return ue!==void 0||(ue=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),te("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",ue)),ue}const tt=typeof window>"u"&&typeof document>"u",Le=Symbol("needle:raycast-mesh");function Q(t){return t?.[Le]instanceof le?t[Le]:null}function rt(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!Q(t)){const r=ot(e);r.userData={isRaycastMesh:!0},t[Le]=r}}function st(t=!0){if(t){if(re)return;const e=re=H.prototype.raycast;H.prototype.raycast=function(r,n){const s=this,o=Q(s);let i;o&&s.isMesh&&(i=s.geometry,s.geometry=o),e.call(this,r,n),i&&(s.geometry=i)}}else{if(!re)return;H.prototype.raycast=re,re=null}}let re=null;function ot(t){const e=new le;for(const r in t.attributes)e.setAttribute(r,t.getAttribute(r));return e.setIndex(t.getIndex()),e}const F=new Array,N="NEEDLE_progressive",v=te("debugprogressive"),De=Symbol("needle-progressive-texture"),se=new Map,we=new Set;if(v){let t=function(){e+=1,console.log("Toggle LOD level",e,se),se.forEach((s,o)=>{for(const i of s.keys){const a=o[i];if(a!=null)if(a.isBufferGeometry===!0){const u=S.getMeshLODInformation(a),l=u?Math.min(e,u.lods.length):0;o["DEBUG:LOD"]=l,u&&(r=Math.max(r,u.lods.length-1))}else o.isMaterial===!0&&(o["DEBUG:LOD"]=e)}}),e>=r&&(e=-1)},e=-1,r=2,n=!1;window.addEventListener("keyup",s=>{s.key==="p"&&t(),s.key==="w"&&(n=!n,we&&we.forEach(o=>{o.name!="BackgroundCubeMaterial"&&o.glyphMap==null&&"wireframe"in o&&(o.wireframe=n)}))})}function Be(t,e,r){var n;if(!v)return;se.has(t)||se.set(t,{keys:[],sourceId:r});const s=se.get(t);((n=s?.keys)==null?void 0:n.includes(e))==!1&&s.keys.push(e)}const w=class{constructor(t,e){this.loadMesh=r=>{var n,s;if(this._isLoadingMesh)return null;const o=(s=(n=this.parser.json.meshes[r])==null?void 0:n.extensions)==null?void 0:s[N];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",r).then(i=>{var a;return this._isLoadingMesh=!1,i&&w.registerMesh(this.url,o.guid,i,(a=o.lods)==null?void 0:a.length,void 0,o),i})):null},v&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return N}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const r=this,n="LODS:minmax",s=t[n];if(s!=null)return s;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const i of t)this.getMaterialMinMaxLODsCount(i,e);return t[n]=e,e}if(v==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const i=t;for(const a of Object.keys(i.uniforms)){const u=i.uniforms[a].value;u?.isTexture===!0&&o(u,e)}}else if(t.isMaterial)for(const i of Object.keys(t)){const a=t[i];a?.isTexture===!0&&o(a,e)}return t[n]=e,e;function o(i,a){const u=r.getAssignedLODInformation(i);if(u){const l=r.lodInfos.get(u.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let h=0;h<l.lods.length;h++){const f=l.lods[h];f.width&&(a.lods[h]=a.lods[h]||{min_height:1/0,max_height:0},a.lods[h].min_height=Math.min(a.lods[h].min_height,f.height),a.lods[h].max_height=Math.max(a.lods[h].max_height,f.height))}}}}}static hasLODLevelAvailable(t,e){var r;if(Array.isArray(t)){for(const o of t)if(this.hasLODLevelAvailable(o,e))return!0;return!1}if(t.isMaterial===!0){for(const o of Object.keys(t)){const i=t[o];if(i&&i.isTexture&&this.hasLODLevelAvailable(i,e))return!0}return!1}else if(t.isGroup===!0){for(const o of t.children)if(o.isMesh===!0&&this.hasLODLevelAvailable(o,e))return!0}let n,s;if(t.isMesh?n=t.geometry:(t.isBufferGeometry||t.isTexture)&&(n=t),n&&(r=n?.userData)!=null&&r.LODS){const o=n.userData.LODS;if(s=this.lodInfos.get(o.key),e===void 0)return s!=null;if(s)return Array.isArray(s.lods)?e<s.lods.length:e===0}return!1}static assignMeshLOD(t,e){var r;if(!t)return Promise.resolve(null);if(t instanceof H||t.isMesh===!0){const n=t.geometry,s=this.getAssignedLODInformation(n);if(!s)return Promise.resolve(null);for(const o of F)(r=o.onBeforeGetLODMesh)==null||r.call(o,t,e);return t["LOD:requested level"]=e,w.getOrLoadLOD(n,e).then(o=>{if(Array.isArray(o)){const i=s.index||0;o=o[i]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],o&&n!=o&&(o?.isBufferGeometry?(t.geometry=o,v&&Be(t,"geometry",s.url)):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 r=t;if(Array.isArray(r.material)){const n=new Array;for(const s of r.material){const o=this.assignTextureLOD(s,e);n.push(o)}return Promise.all(n).then(s=>{const o=new Array;for(const i of s)Array.isArray(i)&&o.push(...i);return o})}else return this.assignTextureLOD(r.material,e)}if(t.isMaterial===!0){const r=t,n=[],s=new Array;if(v&&we.add(r),r.uniforms&&(r.isRawShaderMaterial||r.isShaderMaterial===!0)){const o=r;for(const i of Object.keys(o.uniforms)){const a=o.uniforms[i].value;if(a?.isTexture===!0){const u=this.assignTextureLODForSlot(a,e,r,i).then(l=>(l&&o.uniforms[i].value!=l&&(o.uniforms[i].value=l,o.uniformsNeedUpdate=!0),l));n.push(u),s.push(i)}}}else for(const o of Object.keys(r)){const i=r[o];if(i?.isTexture===!0){const a=this.assignTextureLODForSlot(i,e,r,o);n.push(a),s.push(o)}}return Promise.all(n).then(o=>{const i=new Array;for(let a=0;a<o.length;a++){const u=o[a],l=s[a];u&&u.isTexture===!0?i.push({material:r,slot:l,texture:u,level:e}):i.push({material:r,slot:l,texture:null,level:e})}return i})}if(t instanceof ee||t.isTexture===!0){const r=t;return this.assignTextureLODForSlot(r,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,r,n){return t?.isTexture!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(t):w.getOrLoadLOD(t,e).then(s=>{if(Array.isArray(s))return null;if(s?.isTexture===!0){if(s!=t){if(r&&n){const o=r[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,r,o,s),null}r[n]=s}if(v&&n&&r){const o=this.getAssignedLODInformation(t);o?Be(r,n,o.url):console.warn("No LOD info for texture",t)}}return s}else v=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(s=>(console.error("Error loading LOD",t,s),null))}afterRoot(t){var e,r;return v&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((n,s)=>{var o;if(n!=null&&n.extensions){const i=n?.extensions[N];if(i){if(!i.lods){v&&console.warn("Texture has no LODs",i);return}let a=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const l=this.parser.associations.get(u);l?.textures===s&&(a=!0,w.registerTexture(this.url,u,(o=i.lods)==null?void 0:o.length,s,i))}a||this.parser.getDependency("texture",s).then(u=>{var l;u&&w.registerTexture(this.url,u,(l=i.lods)==null?void 0:l.length,s,i)})}}}),(r=this.parser.json.meshes)==null||r.forEach((n,s)=>{if(n!=null&&n.extensions){const o=n?.extensions[N];if(o&&o.lods){for(const i of this.parser.associations.keys())if(i.isMesh){const a=this.parser.associations.get(i);a?.meshes===s&&w.registerMesh(this.url,o.guid,i,o.lods.length,a.primitives,o)}}}}),null}static async getOrLoadLOD(t,e){var r,n,s,o;const i=v=="verbose",a=t.userData.LODS;if(!a)return null;const u=a?.key;let l;if(t.isTexture===!0){const h=t;h.source&&h.source[De]&&(l=h.source[De])}if(l||(l=w.lodInfos.get(u)),l){if(e>0){let p=!1;const E=Array.isArray(l.lods);if(E&&e>=l.lods.length?p=!0:E||(p=!0),p)return this.lowresCache.get(u)}const h=Array.isArray(l.lods)?(r=l.lods[e])==null?void 0:r.path:l.lods;if(!h)return v&&!l["missing:uri"]&&(l["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,l)),null;const f=Ze(a.url,h);if(f.endsWith(".glb")||f.endsWith(".gltf")){if(!l.guid)return console.warn("missing pointer for glb/gltf texture",l),null;const p=f+"_"+l.guid,E=this.previouslyLoaded.get(p);if(E!==void 0){i&&console.log(`LOD ${e} was already loading/loaded: ${p}`);let c=await E.catch(b=>(console.error(`Error loading LOD ${e} from ${f}
|
|
2
|
-
`,b),null)),M=!1;if(c==null||(c instanceof ee&&t instanceof ee?(n=c.image)!=null&&n.data||(s=c.source)!=null&&s.data?c=this.copySettings(t,c):(M=!0,this.previouslyLoaded.delete(p)):c instanceof le&&t instanceof le&&((o=c.attributes.position)!=null&&o.array||(M=!0,this.previouslyLoaded.delete(p)))),!M)return c}const D=l,y=new Promise(async(c,M)=>{const b=new
|
|
3
|
-
`,m),null));if(!_)return null;const I=_.parser;i&&console.log("Loading finished "+
|
|
1
|
+
import{B as le,M as H,T as ee,a as Ue,V as $,b as Oe,S as Fe,c as Se,d as ze,C as Ve}from"./three.min.js";import{D as qe,K as Ke,G as fe,M as He}from"./three-examples.min.js";const Xe="";globalThis.GLTF_PROGRESSIVE_VERSION=Xe,console.debug("[gltf-progressive] version -");let G="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",J="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Ye=G,Je=J,Te=new URL(G+"draco_decoder.js");Te.searchParams.append("range","true"),fetch(Te,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${G} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),G===Ye&&Ee("./include/draco/"),J===Je&&Ae("./include/ktx2/")}).finally(()=>{Pe()});function Ee(t){G=t,A&&A[me]!=G?(console.debug("Updating Draco decoder path to "+t),A[me]=G,A.setDecoderPath(G),A.preload()):console.debug("Setting Draco decoder path to "+t)}function Ae(t){J=t,C&&C.transcoderPath!=J?(console.debug("Updating KTX2 transcoder path to "+t),C.setTranscoderPath(J),C.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let A,ae,C;function Pe(){A||(A=new qe,A[me]=G,A.setDecoderPath(G),A.setDecoderConfig({type:"js"}),A.preload()),C||(C=new Ke,C.setTranscoderPath(J),C.init()),ae||(ae=He)}function pe(t){return Pe(),t?C.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:A,ktx2Loader:C,meshoptDecoder:ae}}function ve(t){t.dracoLoader||t.setDRACOLoader(A),t.ktx2Loader||t.setKTX2Loader(C),t.meshoptDecoder||t.setMeshoptDecoder(ae)}const ye=new WeakMap;function xe(t,e){let r=ye.get(t);r?r=Object.assign(r,e):r=e,ye.set(t,r)}const Be=fe.prototype.load;function Qe(...t){const e=ye.get(this);let r=t[0];const n=new URL(r,window.location.href);if(n.hostname.endsWith("needle.tools")){const s=e?.progressive!==void 0?e.progressive:!0,o=e!=null&&e.usecase?e.usecase:"default";s?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${o}`:this.requestHeader.Accept=`*/*;usecase=${o}`,r=n.toString()}return t[0]=r,Be?.call(this,...t)}fe.prototype.load=Qe,te("debugprogressive");function te(t){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(t);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function Ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const r=t.lastIndexOf("/");if(r>=0){const n=t.substring(0,r+1);for(;n.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return n+e}return e}let ue;function et(){return ue!==void 0||(ue=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),te("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",ue)),ue}const tt=typeof window>"u"&&typeof document>"u",Le=Symbol("needle:raycast-mesh");function Q(t){return t?.[Le]instanceof le?t[Le]:null}function rt(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!Q(t)){const r=ot(e);r.userData={isRaycastMesh:!0},t[Le]=r}}function st(t=!0){if(t){if(re)return;const e=re=H.prototype.raycast;H.prototype.raycast=function(r,n){const s=this,o=Q(s);let i;o&&s.isMesh&&(i=s.geometry,s.geometry=o),e.call(this,r,n),i&&(s.geometry=i)}}else{if(!re)return;H.prototype.raycast=re,re=null}}let re=null;function ot(t){const e=new le;for(const r in t.attributes)e.setAttribute(r,t.getAttribute(r));return e.setIndex(t.getIndex()),e}const F=new Array,N="NEEDLE_progressive",v=te("debugprogressive"),De=Symbol("needle-progressive-texture"),se=new Map,we=new Set;if(v){let t=function(){e+=1,console.log("Toggle LOD level",e,se),se.forEach((s,o)=>{for(const i of s.keys){const a=o[i];if(a!=null)if(a.isBufferGeometry===!0){const u=S.getMeshLODInformation(a),l=u?Math.min(e,u.lods.length):0;o["DEBUG:LOD"]=l,u&&(r=Math.max(r,u.lods.length-1))}else o.isMaterial===!0&&(o["DEBUG:LOD"]=e)}}),e>=r&&(e=-1)},e=-1,r=2,n=!1;window.addEventListener("keyup",s=>{s.key==="p"&&t(),s.key==="w"&&(n=!n,we&&we.forEach(o=>{o.name!="BackgroundCubeMaterial"&&o.glyphMap==null&&"wireframe"in o&&(o.wireframe=n)}))})}function Ie(t,e,r){var n;if(!v)return;se.has(t)||se.set(t,{keys:[],sourceId:r});const s=se.get(t);((n=s?.keys)==null?void 0:n.includes(e))==!1&&s.keys.push(e)}const w=class{constructor(t,e){this.loadMesh=r=>{var n,s;if(this._isLoadingMesh)return null;const o=(s=(n=this.parser.json.meshes[r])==null?void 0:n.extensions)==null?void 0:s[N];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",r).then(i=>{var a;return this._isLoadingMesh=!1,i&&w.registerMesh(this.url,o.guid,i,(a=o.lods)==null?void 0:a.length,void 0,o),i})):null},v&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return N}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const r=this,n="LODS:minmax",s=t[n];if(s!=null)return s;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const i of t)this.getMaterialMinMaxLODsCount(i,e);return t[n]=e,e}if(v==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const i=t;for(const a of Object.keys(i.uniforms)){const u=i.uniforms[a].value;u?.isTexture===!0&&o(u,e)}}else if(t.isMaterial)for(const i of Object.keys(t)){const a=t[i];a?.isTexture===!0&&o(a,e)}return t[n]=e,e;function o(i,a){const u=r.getAssignedLODInformation(i);if(u){const l=r.lodInfos.get(u.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let h=0;h<l.lods.length;h++){const g=l.lods[h];g.width&&(a.lods[h]=a.lods[h]||{min_height:1/0,max_height:0},a.lods[h].min_height=Math.min(a.lods[h].min_height,g.height),a.lods[h].max_height=Math.max(a.lods[h].max_height,g.height))}}}}}static hasLODLevelAvailable(t,e){var r;if(Array.isArray(t)){for(const o of t)if(this.hasLODLevelAvailable(o,e))return!0;return!1}if(t.isMaterial===!0){for(const o of Object.keys(t)){const i=t[o];if(i&&i.isTexture&&this.hasLODLevelAvailable(i,e))return!0}return!1}else if(t.isGroup===!0){for(const o of t.children)if(o.isMesh===!0&&this.hasLODLevelAvailable(o,e))return!0}let n,s;if(t.isMesh?n=t.geometry:(t.isBufferGeometry||t.isTexture)&&(n=t),n&&(r=n?.userData)!=null&&r.LODS){const o=n.userData.LODS;if(s=this.lodInfos.get(o.key),e===void 0)return s!=null;if(s)return Array.isArray(s.lods)?e<s.lods.length:e===0}return!1}static assignMeshLOD(t,e){var r;if(!t)return Promise.resolve(null);if(t instanceof H||t.isMesh===!0){const n=t.geometry,s=this.getAssignedLODInformation(n);if(!s)return Promise.resolve(null);for(const o of F)(r=o.onBeforeGetLODMesh)==null||r.call(o,t,e);return t["LOD:requested level"]=e,w.getOrLoadLOD(n,e).then(o=>{if(Array.isArray(o)){const i=s.index||0;o=o[i]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],o&&n!=o&&(o?.isBufferGeometry?(t.geometry=o,v&&Ie(t,"geometry",s.url)):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 r=t;if(Array.isArray(r.material)){const n=new Array;for(const s of r.material){const o=this.assignTextureLOD(s,e);n.push(o)}return Promise.all(n).then(s=>{const o=new Array;for(const i of s)Array.isArray(i)&&o.push(...i);return o})}else return this.assignTextureLOD(r.material,e)}if(t.isMaterial===!0){const r=t,n=[],s=new Array;if(v&&we.add(r),r.uniforms&&(r.isRawShaderMaterial||r.isShaderMaterial===!0)){const o=r;for(const i of Object.keys(o.uniforms)){const a=o.uniforms[i].value;if(a?.isTexture===!0){const u=this.assignTextureLODForSlot(a,e,r,i).then(l=>(l&&o.uniforms[i].value!=l&&(o.uniforms[i].value=l,o.uniformsNeedUpdate=!0),l));n.push(u),s.push(i)}}}else for(const o of Object.keys(r)){const i=r[o];if(i?.isTexture===!0){const a=this.assignTextureLODForSlot(i,e,r,o);n.push(a),s.push(o)}}return Promise.all(n).then(o=>{const i=new Array;for(let a=0;a<o.length;a++){const u=o[a],l=s[a];u&&u.isTexture===!0?i.push({material:r,slot:l,texture:u,level:e}):i.push({material:r,slot:l,texture:null,level:e})}return i})}if(t instanceof ee||t.isTexture===!0){const r=t;return this.assignTextureLODForSlot(r,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,r,n){return t?.isTexture!==!0?Promise.resolve(null):n==="glyphMap"?Promise.resolve(t):w.getOrLoadLOD(t,e).then(s=>{if(Array.isArray(s))return null;if(s?.isTexture===!0){if(s!=t){if(r&&n){const o=r[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,r,o,s),null}r[n]=s}if(v&&n&&r){const o=this.getAssignedLODInformation(t);o?Ie(r,n,o.url):console.warn("No LOD info for texture",t)}}return s}else v=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(s=>(console.error("Error loading LOD",t,s),null))}afterRoot(t){var e,r;return v&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((n,s)=>{var o;if(n!=null&&n.extensions){const i=n?.extensions[N];if(i){if(!i.lods){v&&console.warn("Texture has no LODs",i);return}let a=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const l=this.parser.associations.get(u);l?.textures===s&&(a=!0,w.registerTexture(this.url,u,(o=i.lods)==null?void 0:o.length,s,i))}a||this.parser.getDependency("texture",s).then(u=>{var l;u&&w.registerTexture(this.url,u,(l=i.lods)==null?void 0:l.length,s,i)})}}}),(r=this.parser.json.meshes)==null||r.forEach((n,s)=>{if(n!=null&&n.extensions){const o=n?.extensions[N];if(o&&o.lods){for(const i of this.parser.associations.keys())if(i.isMesh){const a=this.parser.associations.get(i);a?.meshes===s&&w.registerMesh(this.url,o.guid,i,o.lods.length,a.primitives,o)}}}}),null}static async getOrLoadLOD(t,e){var r,n,s,o;const i=v=="verbose",a=t.userData.LODS;if(!a)return null;const u=a?.key;let l;if(t.isTexture===!0){const h=t;h.source&&h.source[De]&&(l=h.source[De])}if(l||(l=w.lodInfos.get(u)),l){if(e>0){let p=!1;const E=Array.isArray(l.lods);if(E&&e>=l.lods.length?p=!0:E||(p=!0),p)return this.lowresCache.get(u)}const h=Array.isArray(l.lods)?(r=l.lods[e])==null?void 0:r.path:l.lods;if(!h)return v&&!l["missing:uri"]&&(l["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,l)),null;const g=Ze(a.url,h);if(g.endsWith(".glb")||g.endsWith(".gltf")){if(!l.guid)return console.warn("missing pointer for glb/gltf texture",l),null;const p=g+"_"+l.guid,E=this.previouslyLoaded.get(p);if(E!==void 0){i&&console.log(`LOD ${e} was already loading/loaded: ${p}`);let c=await E.catch(b=>(console.error(`Error loading LOD ${e} from ${g}
|
|
2
|
+
`,b),null)),M=!1;if(c==null||(c instanceof ee&&t instanceof ee?(n=c.image)!=null&&n.data||(s=c.source)!=null&&s.data?c=this.copySettings(t,c):(M=!0,this.previouslyLoaded.delete(p)):c instanceof le&&t instanceof le&&((o=c.attributes.position)!=null&&o.array||(M=!0,this.previouslyLoaded.delete(p)))),!M)return c}const D=l,y=new Promise(async(c,M)=>{const b=new fe;ve(b),v&&(await new Promise(m=>setTimeout(m,1e3)),i&&console.warn("Start loading (delayed) "+g,D.guid));let B=g;if(D&&Array.isArray(D.lods)){const m=D.lods[e];m.hash&&(B+="?v="+m.hash)}const _=await b.loadAsync(B).catch(m=>(console.error(`Error loading LOD ${e} from ${g}
|
|
3
|
+
`,m),null));if(!_)return null;const I=_.parser;i&&console.log("Loading finished "+g,D.guid);let f=0;if(_.parser.json.textures){let m=!1;for(const d of _.parser.json.textures){if(d!=null&&d.extensions){const L=d?.extensions[N];if(L!=null&&L.guid&&L.guid===D.guid){m=!0;break}}f++}if(m){let d=await I.getDependency("texture",f);return d&&w.assignLODInformation(a.url,d,u,e,void 0,void 0),i&&console.log('change "'+t.name+'" \u2192 "'+d.name+'"',g,f,d,p),t instanceof ee&&(d=this.copySettings(t,d)),d&&(d.guid=D.guid),c(d)}else v&&console.warn("Could not find texture with guid",D.guid,_.parser.json)}if(f=0,_.parser.json.meshes){let m=!1;for(const d of _.parser.json.meshes){if(d!=null&&d.extensions){const L=d?.extensions[N];if(L!=null&&L.guid&&L.guid===D.guid){m=!0;break}}f++}if(m){const d=await I.getDependency("mesh",f),L=D;if(i&&console.log(`Loaded Mesh "${d.name}"`,g,f,d,p),d.isMesh===!0){const O=d.geometry;return w.assignLODInformation(a.url,O,u,e,void 0,L.density),c(O)}else{const O=new Array;for(let j=0;j<d.children.length;j++){const K=d.children[j];if(K.isMesh===!0){const Y=K.geometry;w.assignLODInformation(a.url,Y,u,e,j,L.density),O.push(Y)}}return c(O)}}else v&&console.warn("Could not find mesh with guid",D.guid,_.parser.json)}return c(null)});return this.previouslyLoaded.set(p,y),await y}else if(t instanceof ee){i&&console.log("Load texture from uri: "+g);const p=await new Ue().loadAsync(g);return p?(p.guid=l.guid,p.flipY=!1,p.needsUpdate=!0,p.colorSpace=t.colorSpace,i&&console.log(l,p)):v&&console.warn("failed loading",g),p}}else v&&console.warn(`Can not load LOD ${e}: no LOD info found for "${u}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,r,n,s,o){if(!e)return;e.userData||(e.userData={});const i=new nt(t,r,n,s,o);e.userData.LODS=i}static getAssignedLODInformation(t){var e;return((e=t?.userData)==null?void 0:e.LODS)||null}static copySettings(t,e){return e?(v&&console.warn(`Copy texture settings
|
|
4
4
|
`,t.uuid,`
|
|
5
|
-
`,e.uuid),e=e.clone(),e.offset=t.offset,e.repeat=t.repeat,e.colorSpace=t.colorSpace,e.magFilter=t.magFilter,e.minFilter=t.minFilter,e.wrapS=t.wrapS,e.wrapT=t.wrapT,e.flipY=t.flipY,e.anisotropy=t.anisotropy,e.mipmaps||(e.generateMipmaps=t.generateMipmaps),e):t}};let S=w;S.registerTexture=(t,e,r,n,s)=>{if(v&&console.log("> Progressive: register texture",n,e.name,e.uuid,e,s),!e){v&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[De]=s);const o=s.guid;w.assignLODInformation(t,e,o,r,n,void 0),w.lodInfos.set(o,s),w.lowresCache.set(o,e)},S.registerMesh=(t,e,r,n,s,o)=>{var i;v&&console.log("> Progressive: register mesh",s,r.name,o,r.uuid,r);const a=r.geometry;if(!a){v&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),w.assignLODInformation(t,a,e,n,s,o.density),w.lodInfos.set(e,o);let u=w.lowresCache.get(e);u?u.push(r.geometry):u=[r.geometry],w.lowresCache.set(e,u),n>0&&!Q(r)&&rt(r,a);for(const l of F)(i=l.onRegisteredNewMesh)==null||i.call(l,r,o)},S.lodInfos=new Map,S.previouslyLoaded=new Map,S.lowresCache=new Map;class nt{constructor(e,r,n,s,o){this.url=e,this.key=r,this.level=n,s!=null&&(this.index=s),o!=null&&(this.density=o)}}var Ie=(t,e,r)=>{if(!e.has(t))throw TypeError("Cannot "+r)},x=(t,e,r)=>(Ie(t,e,"read from private field"),r?r.call(t):e.get(t)),X=(t,e,r)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,r)},U=(t,e,r,n)=>(Ie(t,e,"write to private field"),n?n.call(t,r):e.set(t,r),r),T,z,Me,Z,oe,ce,V;const k=te("debugprogressive"),it=te("noprogressive"),be=Symbol("Needle:LODSManager"),_e=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),R={mesh_lod:-1,texture_lod:-1},P=class{constructor(t,e){this.projectionScreenMatrix=new Oe,this.targetTriangleDensity=2e5,this.updateInterval="auto",X(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],X(this,z,void 0),X(this,Me,new ze),X(this,Z,0),X(this,oe,0),X(this,ce,0),X(this,V,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new Ue,this._tempBox=new Se,this._tempBox2=new Se,this.tempMatrix=new Oe,this._tempWorldPosition=new $,this._tempBoxSize=new $,this._tempBox2Size=new $,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[_e]}static addPlugin(t){F.push(t)}static removePlugin(t){const e=F.indexOf(t);e>=0&&F.splice(e,1)}static get(t,e){if(t[be])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[be];const r=new P(t,{engine:"unknown",...e});return t[be]=r,r}get plugins(){return F}addEventListener(t,e){t==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(t,e){if(t==="changed"){const r=this._lodchangedlisteners.indexOf(e);r>=0&&this._lodchangedlisteners.splice(r,1)}}enable(){if(x(this,z))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;U(this,z,this.renderer.render);const e=this;pe(this.renderer),this.renderer.render=function(r,n){const s=e.renderer.getRenderTarget();(s==null||"isXRRenderTarget"in s&&s.isXRRenderTarget)&&(t=0,U(e,Z,x(e,Z)+1),U(e,oe,x(e,Me).getDelta()),U(e,ce,x(e,ce)+x(e,oe)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/x(e,oe)),U(e,V,e._fpsBuffer.reduce((i,a)=>i+a)/e._fpsBuffer.length),k&&x(e,Z)%200===0&&console.log("FPS",Math.round(x(e,V)),"Interval:",x(e,T)));const o=t++;x(e,z).call(this,r,n),e.onAfterRender(r,n,o)}}disable(){x(this,z)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=x(this,z),U(this,z,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,r){if(this.pause)return;const n=this.renderer.renderLists.get(t,0).opaque;let s=!0;if(n.length===1){const o=n[0].material;(o.name==="EffectMaterial"||o.name==="CopyShader")&&(s=!1)}if((e.parent&&e.parent.type==="CubeCamera"||r>=1&&e.type==="OrthographicCamera")&&(s=!1),s){if(it||(this.updateInterval==="auto"?x(this,V)<40&&x(this,T)<10?(U(this,T,x(this,T)+1),k&&console.warn("\u2193 Reducing LOD updates",x(this,T),x(this,V).toFixed(0))):x(this,V)>=60&&x(this,T)>1&&(U(this,T,x(this,T)-1),k&&console.warn("\u2191 Increasing LOD updates",x(this,T),x(this,V).toFixed(0))):U(this,T,this.updateInterval),x(this,T)>0&&x(this,Z)%x(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var r,n;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((r=l.geometry)==null?void 0:r.type)==="BoxGeometry"||((n=l.geometry)==null?void 0:n.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){k&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(k==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const f=Math.random()*16777215,p=new Fe({color:f});l.object.material=p}const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}const a=s.transparent;for(const l of a){const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}const u=s.transmissive;for(const l of u){const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}}updateLODs(t,e,r,n){var s,o;r.userData||(r.userData={});let i=r[_e];if(i||(i=new lt,r[_e]=i),i.frames++<2)return;for(const u of F)(s=u.onBeforeUpdateLOD)==null||s.call(u,this.renderer,t,e,r);this.calculateLodLevel(e,r,i,n,R),R.mesh_lod=Math.round(R.mesh_lod),R.texture_lod=Math.round(R.texture_lod),R.mesh_lod>=0&&this.loadProgressiveMeshes(r,R.mesh_lod);let a=R.texture_lod;r.material&&a>=0&&this.loadProgressiveTextures(r.material,a);for(const u of F)(o=u.onAfterUpdatedLOD)==null||o.call(u,this.renderer,t,e,r,R);i.lastLodLevel_Mesh=R.mesh_lod,i.lastLodLevel_Texture=R.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const s of t)this.loadProgressiveTextures(s,e);return}let r=!1;(t[q]===void 0||e<t[q])&&(r=!0);const n=t["DEBUG:LOD"];n!=null&&(r=t[q]!=n,e=n),r&&(t[q]=e,S.assignTextureLOD(t,e).then(s=>{this._lodchangedlisteners.forEach(o=>o({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let r=t[q]!==e;const n=t["DEBUG:LOD"];if(n!=null&&(r=t[q]!=n,e=n),r){t[q]=e;const s=t.geometry;return S.assignMeshLOD(t,e).then(o=>(o&&t[q]==e&&s!=t.geometry&&this._lodchangedlisteners.forEach(i=>i({type:"mesh",level:e,object:t})),o))}return Promise.resolve(null)}static isInside(t,e){const r=t.min,n=t.max,s=(r.x+n.x)*.5,o=(r.y+n.y)*.5;return this._tempPtInside.set(s,o,r.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,r,n,s){var o;if(!e){s.mesh_lod=-1,s.texture_lod=-1;return}if(!t){s.mesh_lod=-1,s.texture_lod=-1;return}let i=10+1,a=!1;if(k&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const u=S.getMeshLODInformation(e.geometry),l=u?.lods,h=l&&l.length>0,f=S.getMaterialMinMaxLODsCount(e.material),p=f?.min_count!=1/0&&f.min_count>0&&f.max_count>0;if(!h&&!p){s.mesh_lod=0,s.texture_lod=0;return}h||(a=!0,i=0);const E=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let D=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const y=e;if(!y.boundingBox)y.computeBoundingBox();else if(r.frames%30===0){const c=Q(y),M=y.geometry;c&&(y.geometry=c),y.computeBoundingBox(),y.geometry=M}D=y.boundingBox}if(D){const y=t;if(e.geometry.attributes.color&&e.geometry.attributes.color.count<100&&e.geometry.boundingSphere){this._sphere.copy(e.geometry.boundingSphere),this._sphere.applyMatrix4(e.matrixWorld);const g=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(g)){s.mesh_lod=0,s.texture_lod=0;return}}if(this._tempBox.copy(D),this._tempBox.applyMatrix4(e.matrixWorld),y.isPerspectiveCamera&&P.isInside(this._tempBox,this.projectionScreenMatrix)){s.mesh_lod=0,s.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&y.isPerspectiveCamera&&y.fov>70){const g=this._tempBox.min,m=this._tempBox.max;let d=g.x,L=g.y,O=m.x,j=m.y;const K=2,Y=1.5,ne=(g.x+m.x)*.5,ie=(g.y+m.y)*.5;d=(d-ne)*K+ne,L=(L-ie)*K+ie,O=(O-ne)*K+ne,j=(j-ie)*K+ie;const We=d<0&&O>0?0:Math.min(Math.abs(g.x),Math.abs(m.x)),$e=L<0&&j>0?0:Math.min(Math.abs(g.y),Math.abs(m.y)),fe=Math.max(We,$e);r.lastCentrality=(Y-fe)*(Y-fe)*(Y-fe)}else r.lastCentrality=1;const c=this._tempBox.getSize(this._tempBoxSize);c.multiplyScalar(.5),screen.availHeight>0&&E>0&&c.multiplyScalar(E/screen.availHeight),t.isPerspectiveCamera?c.x*=t.aspect:t.isOrthographicCamera;const M=t.matrixWorldInverse,b=this._tempBox2;b.copy(D),b.applyMatrix4(e.matrixWorld),b.applyMatrix4(M);const B=b.getSize(this._tempBox2Size),_=Math.max(B.x,B.y);if(Math.max(c.x,c.y)!=0&&_!=0&&(c.z=B.z/Math.max(B.x,B.y)*Math.max(c.x,c.y)),r.lastScreenCoverage=Math.max(c.x,c.y,c.z),r.lastScreenspaceVolume.copy(c),r.lastScreenCoverage*=r.lastCentrality,k&&P.debugDrawLine){const g=this.tempMatrix.copy(this.projectionScreenMatrix);g.invert();const m=P.corner0,d=P.corner1,L=P.corner2,O=P.corner3;m.copy(this._tempBox.min),d.copy(this._tempBox.max),d.x=m.x,L.copy(this._tempBox.max),L.y=m.y,O.copy(this._tempBox.max);const j=(m.z+O.z)*.5;m.z=d.z=L.z=O.z=j,m.applyMatrix4(g),d.applyMatrix4(g),L.applyMatrix4(g),O.applyMatrix4(g),P.debugDrawLine(m,d,255),P.debugDrawLine(m,L,255),P.debugDrawLine(d,O,255),P.debugDrawLine(L,O,255)}let I=999;if(l&&r.lastScreenCoverage>0){for(let g=0;g<l.length;g++)if(l[g].density/r.lastScreenCoverage<n){I=g;break}}I<i&&(i=I,a=!0)}if(a?s.mesh_lod=i:s.mesh_lod=r.lastLodLevel_Mesh,k&&s.mesh_lod!=r.lastLodLevel_Mesh){const y=l?.[s.mesh_lod];y&&console.log(`Mesh LOD changed: ${r.lastLodLevel_Mesh} \u2192 ${s.mesh_lod} (${y.density.toFixed(0)}) - ${e.name}`)}if(p){const y="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(r.lastLodLevel_Texture<0){if(s.texture_lod=f.max_count-1,k){const c=f.lods[f.max_count-1];k&&console.log(`First Texture LOD ${s.texture_lod} (${c.max_height}px) - ${e.name}`)}}else{const c=r.lastScreenspaceVolume.x+r.lastScreenspaceVolume.y+r.lastScreenspaceVolume.z;let M=r.lastScreenCoverage*4;((o=this.context)==null?void 0:o.engine)==="model-viewer"&&(M*=1.5);const b=E/window.devicePixelRatio*M;let B=!1;for(let _=f.lods.length-1;_>=0;_--){let I=f.lods[_];if(!(y&&I.max_height>=2048)&&!(et()&&I.max_height>4096)&&(I.max_height>b||!B&&_===0)){if(B=!0,s.texture_lod=_,s.texture_lod<r.lastLodLevel_Texture){const g=I.max_height;k&&console.log(`Texture LOD changed: ${r.lastLodLevel_Texture} \u2192 ${s.texture_lod} = ${g}px
|
|
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 S=w;S.registerTexture=(t,e,r,n,s)=>{if(v&&console.log("> Progressive: register texture",n,e.name,e.uuid,e,s),!e){v&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[De]=s);const o=s.guid;w.assignLODInformation(t,e,o,r,n,void 0),w.lodInfos.set(o,s),w.lowresCache.set(o,e)},S.registerMesh=(t,e,r,n,s,o)=>{var i;v&&console.log("> Progressive: register mesh",s,r.name,o,r.uuid,r);const a=r.geometry;if(!a){v&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),w.assignLODInformation(t,a,e,n,s,o.density),w.lodInfos.set(e,o);let u=w.lowresCache.get(e);u?u.push(r.geometry):u=[r.geometry],w.lowresCache.set(e,u),n>0&&!Q(r)&&rt(r,a);for(const l of F)(i=l.onRegisteredNewMesh)==null||i.call(l,r,o)},S.lodInfos=new Map,S.previouslyLoaded=new Map,S.lowresCache=new Map;class nt{constructor(e,r,n,s,o){this.url=e,this.key=r,this.level=n,s!=null&&(this.index=s),o!=null&&(this.density=o)}}var Ce=(t,e,r)=>{if(!e.has(t))throw TypeError("Cannot "+r)},x=(t,e,r)=>(Ce(t,e,"read from private field"),r?r.call(t):e.get(t)),X=(t,e,r)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,r)},U=(t,e,r,n)=>(Ce(t,e,"write to private field"),n?n.call(t,r):e.set(t,r),r),T,z,Me,Z,oe,ce,V;const k=te("debugprogressive"),it=te("noprogressive"),be=Symbol("Needle:LODSManager"),_e=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),R={mesh_lod:-1,texture_lod:-1},P=class{constructor(t,e){this.projectionScreenMatrix=new Oe,this.targetTriangleDensity=2e5,this.updateInterval="auto",X(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],X(this,z,void 0),X(this,Me,new Ve),X(this,Z,0),X(this,oe,0),X(this,ce,0),X(this,V,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new Fe,this._tempBox=new Se,this._tempBox2=new Se,this.tempMatrix=new Oe,this._tempWorldPosition=new $,this._tempBoxSize=new $,this._tempBox2Size=new $,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[_e]}static addPlugin(t){F.push(t)}static removePlugin(t){const e=F.indexOf(t);e>=0&&F.splice(e,1)}static get(t,e){if(t[be])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[be];const r=new P(t,{engine:"unknown",...e});return t[be]=r,r}get plugins(){return F}addEventListener(t,e){t==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(t,e){if(t==="changed"){const r=this._lodchangedlisteners.indexOf(e);r>=0&&this._lodchangedlisteners.splice(r,1)}}enable(){if(x(this,z))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;U(this,z,this.renderer.render);const e=this;pe(this.renderer),this.renderer.render=function(r,n){const s=e.renderer.getRenderTarget();(s==null||"isXRRenderTarget"in s&&s.isXRRenderTarget)&&(t=0,U(e,Z,x(e,Z)+1),U(e,oe,x(e,Me).getDelta()),U(e,ce,x(e,ce)+x(e,oe)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/x(e,oe)),U(e,V,e._fpsBuffer.reduce((i,a)=>i+a)/e._fpsBuffer.length),k&&x(e,Z)%200===0&&console.log("FPS",Math.round(x(e,V)),"Interval:",x(e,T)));const o=t++;x(e,z).call(this,r,n),e.onAfterRender(r,n,o)}}disable(){x(this,z)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=x(this,z),U(this,z,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,r){if(this.pause)return;const n=this.renderer.renderLists.get(t,0).opaque;let s=!0;if(n.length===1){const o=n[0].material;(o.name==="EffectMaterial"||o.name==="CopyShader")&&(s=!1)}if((e.parent&&e.parent.type==="CubeCamera"||r>=1&&e.type==="OrthographicCamera")&&(s=!1),s){if(it||(this.updateInterval==="auto"?x(this,V)<40&&x(this,T)<10?(U(this,T,x(this,T)+1),k&&console.warn("\u2193 Reducing LOD updates",x(this,T),x(this,V).toFixed(0))):x(this,V)>=60&&x(this,T)>1&&(U(this,T,x(this,T)-1),k&&console.warn("\u2191 Increasing LOD updates",x(this,T),x(this,V).toFixed(0))):U(this,T,this.updateInterval),x(this,T)>0&&x(this,Z)%x(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var r,n;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((r=l.geometry)==null?void 0:r.type)==="BoxGeometry"||((n=l.geometry)==null?void 0:n.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){k&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(k==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const g=Math.random()*16777215,p=new ze({color:g});l.object.material=p}const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}const a=s.transparent;for(const l of a){const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}const u=s.transmissive;for(const l of u){const h=l.object;(h instanceof H||h.isMesh)&&this.updateLODs(t,e,h,i)}}updateLODs(t,e,r,n){var s,o;r.userData||(r.userData={});let i=r[_e];if(i||(i=new lt,r[_e]=i),i.frames++<2)return;for(const u of F)(s=u.onBeforeUpdateLOD)==null||s.call(u,this.renderer,t,e,r);this.calculateLodLevel(e,r,i,n,R),R.mesh_lod=Math.round(R.mesh_lod),R.texture_lod=Math.round(R.texture_lod),R.mesh_lod>=0&&this.loadProgressiveMeshes(r,R.mesh_lod);let a=R.texture_lod;r.material&&a>=0&&this.loadProgressiveTextures(r.material,a);for(const u of F)(o=u.onAfterUpdatedLOD)==null||o.call(u,this.renderer,t,e,r,R);i.lastLodLevel_Mesh=R.mesh_lod,i.lastLodLevel_Texture=R.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const s of t)this.loadProgressiveTextures(s,e);return}let r=!1;(t[q]===void 0||e<t[q])&&(r=!0);const n=t["DEBUG:LOD"];n!=null&&(r=t[q]!=n,e=n),r&&(t[q]=e,S.assignTextureLOD(t,e).then(s=>{this._lodchangedlisteners.forEach(o=>o({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let r=t[q]!==e;const n=t["DEBUG:LOD"];if(n!=null&&(r=t[q]!=n,e=n),r){t[q]=e;const s=t.geometry;return S.assignMeshLOD(t,e).then(o=>(o&&t[q]==e&&s!=t.geometry&&this._lodchangedlisteners.forEach(i=>i({type:"mesh",level:e,object:t})),o))}return Promise.resolve(null)}static isInside(t,e){const r=t.min,n=t.max,s=(r.x+n.x)*.5,o=(r.y+n.y)*.5;return this._tempPtInside.set(s,o,r.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,r,n,s){var o;if(!e){s.mesh_lod=-1,s.texture_lod=-1;return}if(!t){s.mesh_lod=-1,s.texture_lod=-1;return}let i=10+1,a=!1;if(k&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const u=S.getMeshLODInformation(e.geometry),l=u?.lods,h=l&&l.length>0,g=S.getMaterialMinMaxLODsCount(e.material),p=g?.min_count!=1/0&&g.min_count>0&&g.max_count>0;if(!h&&!p){s.mesh_lod=0,s.texture_lod=0;return}h||(a=!0,i=0);const E=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let D=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const y=e;if(!y.boundingBox)y.computeBoundingBox();else if(r.frames%30===0){const c=Q(y),M=y.geometry;c&&(y.geometry=c),y.computeBoundingBox(),y.geometry=M}D=y.boundingBox}if(D){const y=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)){s.mesh_lod=0,s.texture_lod=0;return}}if(this._tempBox.copy(D),this._tempBox.applyMatrix4(e.matrixWorld),y.isPerspectiveCamera&&P.isInside(this._tempBox,this.projectionScreenMatrix)){s.mesh_lod=0,s.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&y.isPerspectiveCamera&&y.fov>70){const f=this._tempBox.min,m=this._tempBox.max;let d=f.x,L=f.y,O=m.x,j=m.y;const K=2,Y=1.5,ne=(f.x+m.x)*.5,ie=(f.y+m.y)*.5;d=(d-ne)*K+ne,L=(L-ie)*K+ie,O=(O-ne)*K+ne,j=(j-ie)*K+ie;const $e=d<0&&O>0?0:Math.min(Math.abs(f.x),Math.abs(m.x)),Ne=L<0&&j>0?0:Math.min(Math.abs(f.y),Math.abs(m.y)),ge=Math.max($e,Ne);r.lastCentrality=(Y-ge)*(Y-ge)*(Y-ge)}else r.lastCentrality=1;const c=this._tempBox.getSize(this._tempBoxSize);c.multiplyScalar(.5),screen.availHeight>0&&E>0&&c.multiplyScalar(E/screen.availHeight),t.isPerspectiveCamera?c.x*=t.aspect:t.isOrthographicCamera;const M=t.matrixWorldInverse,b=this._tempBox2;b.copy(D),b.applyMatrix4(e.matrixWorld),b.applyMatrix4(M);const B=b.getSize(this._tempBox2Size),_=Math.max(B.x,B.y);if(Math.max(c.x,c.y)!=0&&_!=0&&(c.z=B.z/Math.max(B.x,B.y)*Math.max(c.x,c.y)),r.lastScreenCoverage=Math.max(c.x,c.y,c.z),r.lastScreenspaceVolume.copy(c),r.lastScreenCoverage*=r.lastCentrality,k&&P.debugDrawLine){const f=this.tempMatrix.copy(this.projectionScreenMatrix);f.invert();const m=P.corner0,d=P.corner1,L=P.corner2,O=P.corner3;m.copy(this._tempBox.min),d.copy(this._tempBox.max),d.x=m.x,L.copy(this._tempBox.max),L.y=m.y,O.copy(this._tempBox.max);const j=(m.z+O.z)*.5;m.z=d.z=L.z=O.z=j,m.applyMatrix4(f),d.applyMatrix4(f),L.applyMatrix4(f),O.applyMatrix4(f),P.debugDrawLine(m,d,255),P.debugDrawLine(m,L,255),P.debugDrawLine(d,O,255),P.debugDrawLine(L,O,255)}let I=999;if(l&&r.lastScreenCoverage>0){for(let f=0;f<l.length;f++)if(l[f].density/r.lastScreenCoverage<n){I=f;break}}I<i&&(i=I,a=!0)}if(a?s.mesh_lod=i:s.mesh_lod=r.lastLodLevel_Mesh,k&&s.mesh_lod!=r.lastLodLevel_Mesh){const y=l?.[s.mesh_lod];y&&console.log(`Mesh LOD changed: ${r.lastLodLevel_Mesh} \u2192 ${s.mesh_lod} (${y.density.toFixed(0)}) - ${e.name}`)}if(p){const y="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(r.lastLodLevel_Texture<0){if(s.texture_lod=g.max_count-1,k){const c=g.lods[g.max_count-1];k&&console.log(`First Texture LOD ${s.texture_lod} (${c.max_height}px) - ${e.name}`)}}else{const c=r.lastScreenspaceVolume.x+r.lastScreenspaceVolume.y+r.lastScreenspaceVolume.z;let M=r.lastScreenCoverage*4;((o=this.context)==null?void 0:o.engine)==="model-viewer"&&(M*=1.5);const b=E/window.devicePixelRatio*M;let B=!1;for(let _=g.lods.length-1;_>=0;_--){let I=g.lods[_];if(!(y&&I.max_height>=2048)&&!(et()&&I.max_height>4096)&&(I.max_height>b||!B&&_===0)){if(B=!0,s.texture_lod=_,s.texture_lod<r.lastLodLevel_Texture){const f=I.max_height;k&&console.log(`Texture LOD changed: ${r.lastLodLevel_Texture} \u2192 ${s.texture_lod} = ${f}px
|
|
6
6
|
Screensize: ${b.toFixed(0)}px, Coverage: ${(100*r.lastScreenCoverage).toFixed(2)}%, Volume ${c.toFixed(1)}
|
|
7
|
-
${e.name}`)}break}}}}else s.texture_lod=0}};let W=P;T=new WeakMap,z=new WeakMap,Me=new WeakMap,Z=new WeakMap,oe=new WeakMap,ce=new WeakMap,V=new WeakMap,W.corner0=new $,W.corner1=new $,W.corner2=new $,W.corner3=new $,W._tempPtInside=new $;class lt{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new $,this.lastCentrality=0}}const
|
|
8
|
-
`,t.getAttribute("src"));let e=null,r=null,n=null;for(let s=t;s!=null;s=Object.getPrototypeOf(s)){const o=Object.getOwnPropertySymbols(s),i=o.find(l=>l.toString()=="Symbol(renderer)"),a=o.find(l=>l.toString()=="Symbol(scene)"),u=o.find(l=>l.toString()=="Symbol(needsRender)");!e&&i!=null&&(e=t[i].threeRenderer),!r&&a!=null&&(r=t[a]),!n&&u!=null&&(n=t[u])}if(e&&r){let s=function(){if(n){let i=0,a=setInterval(()=>{if(i++>5){clearInterval(a);return}n?.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const o=W.get(e,{engine:"model-viewer"});return W.addPlugin(new ct),o.enable(),o.addEventListener("changed",()=>{n?.call(t)}),t.addEventListener("model-visibility",i=>{i.detail.visible&&n?.call(t)}),t.addEventListener("load",()=>{s()}),()=>{o.disable()}}return null}class ct{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,r,n,s){this.tryParseMeshLOD(r,s),this.tryParseTextureLOD(r,s)}getUrl(e){if(!e)return null;let r=e.getAttribute("src");return r||(r=e.src),r||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),r}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,r){if(r[de]==!0)return;r[de]=!0;const n=this.tryGetCurrentGLTF(e),s=this.tryGetCurrentModelViewer(e),o=this.getUrl(s);if(o&&n&&r.material){let i=function(u){var l,h,
|
|
7
|
+
${e.name}`)}break}}}}else s.texture_lod=0}};let W=P;T=new WeakMap,z=new WeakMap,Me=new WeakMap,Z=new WeakMap,oe=new WeakMap,ce=new WeakMap,V=new WeakMap,W.corner0=new $,W.corner1=new $,W.corner2=new $,W.corner3=new $,W._tempPtInside=new $;class lt{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new $,this.lastCentrality=0}}const ke=Symbol("NEEDLE_mesh_lod"),de=Symbol("NEEDLE_texture_lod");let he=null;function Re(){const t=at();t&&(t.mapURLs(function(e){return je(),e}),je(),he?.disconnect(),he=new MutationObserver(e=>{e.forEach(r=>{r.addedNodes.forEach(n=>{n instanceof HTMLElement&&n.tagName.toLowerCase()==="model-viewer"&&We(n)})})}),he.observe(document,{childList:!0,subtree:!0}))}function at(){return typeof customElements>"u"?null:customElements.get("model-viewer")||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Re()}),null)}function je(){typeof document>"u"||document.querySelectorAll("model-viewer").forEach(t=>{We(t)})}const Ge=new WeakSet;let ut=0;function We(t){if(!t||Ge.has(t))return null;Ge.add(t),console.debug("[gltf-progressive] found new model-viewer..."+ ++ut+`
|
|
8
|
+
`,t.getAttribute("src"));let e=null,r=null,n=null;for(let s=t;s!=null;s=Object.getPrototypeOf(s)){const o=Object.getOwnPropertySymbols(s),i=o.find(l=>l.toString()=="Symbol(renderer)"),a=o.find(l=>l.toString()=="Symbol(scene)"),u=o.find(l=>l.toString()=="Symbol(needsRender)");!e&&i!=null&&(e=t[i].threeRenderer),!r&&a!=null&&(r=t[a]),!n&&u!=null&&(n=t[u])}if(e&&r){let s=function(){if(n){let i=0,a=setInterval(()=>{if(i++>5){clearInterval(a);return}n?.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const o=W.get(e,{engine:"model-viewer"});return W.addPlugin(new ct),o.enable(),o.addEventListener("changed",()=>{n?.call(t)}),t.addEventListener("model-visibility",i=>{i.detail.visible&&n?.call(t)}),t.addEventListener("load",()=>{s()}),()=>{o.disable()}}return null}class ct{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,r,n,s){this.tryParseMeshLOD(r,s),this.tryParseTextureLOD(r,s)}getUrl(e){if(!e)return null;let r=e.getAttribute("src");return r||(r=e.src),r||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),r}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,r){if(r[de]==!0)return;r[de]=!0;const n=this.tryGetCurrentGLTF(e),s=this.tryGetCurrentModelViewer(e),o=this.getUrl(s);if(o&&n&&r.material){let i=function(u){var l,h,g;if(u[de]==!0)return;u[de]=!0,u.userData&&(u.userData.LOD=-1);const p=Object.keys(u);for(let E=0;E<p.length;E++){const D=p[E],y=u[D];if(y?.isTexture===!0){const c=(h=(l=y.userData)==null?void 0:l.associations)==null?void 0:h.textures;if(c==null)continue;const M=n.parser.json.textures[c];if(!M){console.warn("Texture data not found for texture index "+c);continue}if((g=M?.extensions)!=null&&g[N]){const b=M.extensions[N];b&&o&&S.registerTexture(o,y,b.lods.length,c,b)}}}};const a=r.material;if(Array.isArray(a))for(const u of a)i(u);else i(a)}}tryParseMeshLOD(e,r){var n,s;if(r[ke]==!0)return;r[ke]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const a=(s=(n=r.userData)==null?void 0:n.gltfExtensions)==null?void 0:s[N];if(a&&i){const u=r.uuid;S.registerMesh(i,u,r,0,a.lods.length,a)}}}function dt(t,e,r,n){pe(e),ve(r),xe(r,{progressive:!0,...n?.hints}),r.register(o=>new S(o,t));const s=W.get(e);return n?.enableLODsManager!==!1&&s.enable(),s}if(Re(),!tt){const t={gltfProgressive:{useNeedleProgressive:dt,LODsManager:W,configureLoader:xe,getRaycastMesh:Q,useRaycastMeshes:st}};if(!globalThis.Needle)globalThis.Needle=t;else for(const e in t)globalThis.Needle[e]=t[e]}export{W as L,S as N,ve as a,xe as b,pe as c,Ae as d,Q as g,Ee as s};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";const g=require("./three.umd.cjs"),Z=require("./three-examples.umd.cjs"),Ne="";globalThis.GLTF_PROGRESSIVE_VERSION=Ne;console.debug("[gltf-progressive] version -");let F="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",j="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Fe=F,Ue=j,Ve=new URL(F+"draco_decoder.js");fetch(Ve,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${F} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),F===Fe&&Pe("./include/draco/"),j===Ue&&Ce("./include/ktx2/")}).finally(()=>{Be()});function Pe(t){F=t,C&&C[me]!=F?(console.debug("Updating Draco decoder path to "+t),C[me]=F,C.setDecoderPath(F),C.preload()):console.debug("Setting Draco decoder path to "+t)}function Ce(t){j=t,$&&$.transcoderPath!=j?(console.debug("Updating KTX2 transcoder path to "+t),$.setTranscoderPath(j),$.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let C,de,$;function Be(){C||(C=new Z.DRACOLoader,C[me]=F,C.setDecoderPath(F),C.setDecoderConfig({type:"js"}),C.preload()),$||($=new Z.KTX2Loader,$.setTranscoderPath(j),$.init()),de||(de=Z.MeshoptDecoder)}function we(t){return Be(),t?$.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:C,ktx2Loader:$,meshoptDecoder:de}}function Oe(t){t.dracoLoader||t.setDRACOLoader(C),t.ktx2Loader||t.setKTX2Loader($),t.meshoptDecoder||t.setMeshoptDecoder(de)}const xe=new WeakMap;function Se(t,e){let s=xe.get(t);s?s=Object.assign(s,e):s=e,xe.set(t,s)}const pe=Z.GLTFLoader.prototype.load;function We(...t){const e=xe.get(this);let s=t[0];const o=new URL(s,window.location.href);if(o.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}`,s=o.toString()}return t[0]=s,pe==null?void 0:pe.call(this,...t)}Z.GLTFLoader.prototype.load=We;ne("debugprogressive");function ne(t){if(typeof window>"u")return!1;const s=new URL(window.location.href).searchParams.get(t);return s==null||s==="0"||s==="false"?!1:s===""?!0:s}function ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const s=t.lastIndexOf("/");if(s>=0){const o=t.substring(0,s+1);for(;o.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return o+e}return e}let te;function Xe(){return te!==void 0||(te=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ne("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",te)),te}const qe=typeof window>"u"&&typeof document>"u",De=Symbol("needle:raycast-mesh");function ee(t){return(t==null?void 0:t[De])instanceof g.BufferGeometry?t[De]:null}function Ke(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!ee(t)){const o=He(e);o.userData={isRaycastMesh:!0},t[De]=o}}function Ye(t=!0){if(t){if(se)return;const e=se=g.Mesh.prototype.raycast;g.Mesh.prototype.raycast=function(s,o){const i=this,r=ee(i);let n;r&&i.isMesh&&(n=i.geometry,i.geometry=r),e.call(this,s,o),n&&(i.geometry=n)}}else{if(!se)return;g.Mesh.prototype.raycast=se,se=null}}let se=null;function He(t){const e=new g.BufferGeometry;for(const s in t.attributes)e.setAttribute(s,t.getAttribute(s));return e.setIndex(t.getIndex()),e}const H=new Array,z="NEEDLE_progressive",y=ne("debugprogressive"),Me=Symbol("needle-progressive-texture"),oe=new Map,_e=new Set;if(y){let t=function(){e+=1,console.log("Toggle LOD level",e,oe),oe.forEach((i,r)=>{for(const n of i.keys){const a=r[n];if(a!=null)if(a.isBufferGeometry===!0){const c=E.getMeshLODInformation(a),l=c?Math.min(e,c.lods.length):0;r["DEBUG:LOD"]=l,c&&(s=Math.max(s,c.lods.length-1))}else r.isMaterial===!0&&(r["DEBUG:LOD"]=e)}}),e>=s&&(e=-1)},e=-1,s=2,o=!1;window.addEventListener("keyup",i=>{i.key==="p"&&t(),i.key==="w"&&(o=!o,_e&&_e.forEach(r=>{r.name!="BackgroundCubeMaterial"&&r.glyphMap==null&&"wireframe"in r&&(r.wireframe=o)}))})}function be(t,e,s){var i;if(!y)return;oe.has(t)||oe.set(t,{keys:[],sourceId:s});const o=oe.get(t);((i=o==null?void 0:o.keys)==null?void 0:i.includes(e))==!1&&o.keys.push(e)}const _=class{constructor(t,e){this.loadMesh=s=>{var i,r;if(this._isLoadingMesh)return null;const o=(r=(i=this.parser.json.meshes[s])==null?void 0:i.extensions)==null?void 0:r[z];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",s).then(n=>{var a;return this._isLoadingMesh=!1,n&&_.registerMesh(this.url,o.guid,n,(a=o.lods)==null?void 0:a.length,void 0,o),n})):null},y&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return z}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const s=this,o="LODS:minmax",i=t[o];if(i!=null)return i;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const n of t)this.getMaterialMinMaxLODsCount(n,e);return t[o]=e,e}if(y==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const n=t;for(const a of Object.keys(n.uniforms)){const c=n.uniforms[a].value;(c==null?void 0:c.isTexture)===!0&&r(c,e)}}else if(t.isMaterial)for(const n of Object.keys(t)){const a=t[n];(a==null?void 0:a.isTexture)===!0&&r(a,e)}return t[o]=e,e;function r(n,a){const c=s.getAssignedLODInformation(n);if(c){const l=s.lodInfos.get(c.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let d=0;d<l.lods.length;d++){const h=l.lods[d];h.width&&(a.lods[d]=a.lods[d]||{min_height:1/0,max_height:0},a.lods[d].min_height=Math.min(a.lods[d].min_height,h.height),a.lods[d].max_height=Math.max(a.lods[d].max_height,h.height))}}}}}static hasLODLevelAvailable(t,e){var i;if(Array.isArray(t)){for(const r of t)if(this.hasLODLevelAvailable(r,e))return!0;return!1}if(t.isMaterial===!0){for(const r of Object.keys(t)){const n=t[r];if(n&&n.isTexture&&this.hasLODLevelAvailable(n,e))return!0}return!1}else if(t.isGroup===!0){for(const r of t.children)if(r.isMesh===!0&&this.hasLODLevelAvailable(r,e))return!0}let s,o;if(t.isMesh?s=t.geometry:(t.isBufferGeometry||t.isTexture)&&(s=t),s&&(i=s==null?void 0:s.userData)!=null&&i.LODS){const r=s.userData.LODS;if(o=this.lodInfos.get(r.key),e===void 0)return o!=null;if(o)return Array.isArray(o.lods)?e<o.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof g.Mesh||t.isMesh===!0){const o=t.geometry,i=this.getAssignedLODInformation(o);if(!i)return Promise.resolve(null);for(const r of H)(s=r.onBeforeGetLODMesh)==null||s.call(r,t,e);return t["LOD:requested level"]=e,_.getOrLoadLOD(o,e).then(r=>{if(Array.isArray(r)){const n=i.index||0;r=r[n]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],r&&o!=r&&((r==null?void 0:r.isBufferGeometry)?(t.geometry=r,y&&be(t,"geometry",i.url)):y&&console.error("Invalid LOD geometry",r))),r}).catch(r=>(console.error("Error loading mesh LOD",t,r),null))}else y&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",t);return Promise.resolve(null)}static assignTextureLOD(t,e=0){if(!t)return Promise.resolve(null);if(t.isMesh===!0){const s=t;if(Array.isArray(s.material)){const o=new Array;for(const i of s.material){const r=this.assignTextureLOD(i,e);o.push(r)}return Promise.all(o).then(i=>{const r=new Array;for(const n of i)Array.isArray(n)&&r.push(...n);return r})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,o=[],i=new Array;if(y&&_e.add(s),s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const r=s;for(const n of Object.keys(r.uniforms)){const a=r.uniforms[n].value;if((a==null?void 0:a.isTexture)===!0){const c=this.assignTextureLODForSlot(a,e,s,n).then(l=>(l&&r.uniforms[n].value!=l&&(r.uniforms[n].value=l,r.uniformsNeedUpdate=!0),l));o.push(c),i.push(n)}}}else for(const r of Object.keys(s)){const n=s[r];if((n==null?void 0:n.isTexture)===!0){const a=this.assignTextureLODForSlot(n,e,s,r);o.push(a),i.push(r)}}return Promise.all(o).then(r=>{const n=new Array;for(let a=0;a<r.length;a++){const c=r[a],l=i[a];c&&c.isTexture===!0?n.push({material:s,slot:l,texture:c,level:e}):n.push({material:s,slot:l,texture:null,level:e})}return n})}if(t instanceof g.Texture||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,o){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):o==="glyphMap"?Promise.resolve(t):_.getOrLoadLOD(t,e).then(i=>{if(Array.isArray(i))return null;if((i==null?void 0:i.isTexture)===!0){if(i!=t){if(s&&o){const r=s[o];if(r&&!y){const n=this.getAssignedLODInformation(r);if(n&&(n==null?void 0:n.level)<e)return y==="verbose"&&console.warn("Assigned texture level is already higher: ",n.level,e,s,r,i),null}s[o]=i}if(y&&o&&s){const r=this.getAssignedLODInformation(t);r?be(s,o,r.url):console.warn("No LOD info for texture",t)}}return i}else y=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(i=>(console.error("Error loading LOD",t,i),null))}afterRoot(t){var e,s;return y&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((o,i)=>{var r;if(o!=null&&o.extensions){const n=o==null?void 0:o.extensions[z];if(n){if(!n.lods){y&&console.warn("Texture has no LODs",n);return}let a=!1;for(const c of this.parser.associations.keys())if(c.isTexture===!0){const l=this.parser.associations.get(c);(l==null?void 0:l.textures)===i&&(a=!0,_.registerTexture(this.url,c,(r=n.lods)==null?void 0:r.length,i,n))}a||this.parser.getDependency("texture",i).then(c=>{var l;c&&_.registerTexture(this.url,c,(l=n.lods)==null?void 0:l.length,i,n)})}}}),(s=this.parser.json.meshes)==null||s.forEach((o,i)=>{if(o!=null&&o.extensions){const r=o==null?void 0:o.extensions[z];if(r&&r.lods){for(const n of this.parser.associations.keys())if(n.isMesh){const a=this.parser.associations.get(n);(a==null?void 0:a.meshes)===i&&_.registerMesh(this.url,r.guid,n,r.lods.length,a.primitives,r)}}}}),null}static async getOrLoadLOD(t,e){var n,a,c,l;const s=y=="verbose",o=t.userData.LODS;if(!o)return null;const i=o==null?void 0:o.key;let r;if(t.isTexture===!0){const d=t;d.source&&d.source[Me]&&(r=d.source[Me])}if(r||(r=_.lodInfos.get(i)),r){if(e>0){let D=!1;const m=Array.isArray(r.lods);if(m&&e>=r.lods.length?D=!0:m||(D=!0),D)return this.lowresCache.get(i)}const d=Array.isArray(r.lods)?(n=r.lods[e])==null?void 0:n.path:r.lods;if(!d)return y&&!r["missing:uri"]&&(r["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,r)),null;const h=ze(o.url,d);if(h.endsWith(".glb")||h.endsWith(".gltf")){if(!r.guid)return console.warn("missing pointer for glb/gltf texture",r),null;const D=h+"_"+r.guid,m=this.previouslyLoaded.get(D);if(m!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${D}`);let f=await m.catch(N=>(console.error(`Error loading LOD ${e} from ${h}
|
|
1
|
+
"use strict";const g=require("./three.umd.cjs"),Z=require("./three-examples.umd.cjs"),Fe="";globalThis.GLTF_PROGRESSIVE_VERSION=Fe;console.debug("[gltf-progressive] version -");let F="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",j="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const Ue=F,Ve=j,Pe=new URL(F+"draco_decoder.js");Pe.searchParams.append("range","true");fetch(Pe,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(t=>{console.debug(`Failed to fetch remote Draco decoder from ${F} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),F===Ue&&Ce("./include/draco/"),j===Ve&&Be("./include/ktx2/")}).finally(()=>{ke()});function Ce(t){F=t,C&&C[me]!=F?(console.debug("Updating Draco decoder path to "+t),C[me]=F,C.setDecoderPath(F),C.preload()):console.debug("Setting Draco decoder path to "+t)}function Be(t){j=t,$&&$.transcoderPath!=j?(console.debug("Updating KTX2 transcoder path to "+t),$.setTranscoderPath(j),$.init()):console.debug("Setting KTX2 transcoder path to "+t)}const me=Symbol("dracoDecoderPath");let C,de,$;function ke(){C||(C=new Z.DRACOLoader,C[me]=F,C.setDecoderPath(F),C.setDecoderConfig({type:"js"}),C.preload()),$||($=new Z.KTX2Loader,$.setTranscoderPath(j),$.init()),de||(de=Z.MeshoptDecoder)}function we(t){return ke(),t?$.detectSupport(t):t!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:C,ktx2Loader:$,meshoptDecoder:de}}function Oe(t){t.dracoLoader||t.setDRACOLoader(C),t.ktx2Loader||t.setKTX2Loader($),t.meshoptDecoder||t.setMeshoptDecoder(de)}const xe=new WeakMap;function Se(t,e){let s=xe.get(t);s?s=Object.assign(s,e):s=e,xe.set(t,s)}const pe=Z.GLTFLoader.prototype.load;function We(...t){const e=xe.get(this);let s=t[0];const o=new URL(s,window.location.href);if(o.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}`,s=o.toString()}return t[0]=s,pe==null?void 0:pe.call(this,...t)}Z.GLTFLoader.prototype.load=We;ne("debugprogressive");function ne(t){if(typeof window>"u")return!1;const s=new URL(window.location.href).searchParams.get(t);return s==null||s==="0"||s==="false"?!1:s===""?!0:s}function ze(t,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||t===void 0)return e;const s=t.lastIndexOf("/");if(s>=0){const o=t.substring(0,s+1);for(;o.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return o+e}return e}let te;function Xe(){return te!==void 0||(te=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),ne("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",te)),te}const qe=typeof window>"u"&&typeof document>"u",De=Symbol("needle:raycast-mesh");function ee(t){return(t==null?void 0:t[De])instanceof g.BufferGeometry?t[De]:null}function Ke(t,e){if((t.type==="Mesh"||t.type==="SkinnedMesh")&&!ee(t)){const o=He(e);o.userData={isRaycastMesh:!0},t[De]=o}}function Ye(t=!0){if(t){if(se)return;const e=se=g.Mesh.prototype.raycast;g.Mesh.prototype.raycast=function(s,o){const i=this,r=ee(i);let n;r&&i.isMesh&&(n=i.geometry,i.geometry=r),e.call(this,s,o),n&&(i.geometry=n)}}else{if(!se)return;g.Mesh.prototype.raycast=se,se=null}}let se=null;function He(t){const e=new g.BufferGeometry;for(const s in t.attributes)e.setAttribute(s,t.getAttribute(s));return e.setIndex(t.getIndex()),e}const H=new Array,z="NEEDLE_progressive",y=ne("debugprogressive"),Me=Symbol("needle-progressive-texture"),oe=new Map,_e=new Set;if(y){let t=function(){e+=1,console.log("Toggle LOD level",e,oe),oe.forEach((i,r)=>{for(const n of i.keys){const a=r[n];if(a!=null)if(a.isBufferGeometry===!0){const c=E.getMeshLODInformation(a),l=c?Math.min(e,c.lods.length):0;r["DEBUG:LOD"]=l,c&&(s=Math.max(s,c.lods.length-1))}else r.isMaterial===!0&&(r["DEBUG:LOD"]=e)}}),e>=s&&(e=-1)},e=-1,s=2,o=!1;window.addEventListener("keyup",i=>{i.key==="p"&&t(),i.key==="w"&&(o=!o,_e&&_e.forEach(r=>{r.name!="BackgroundCubeMaterial"&&r.glyphMap==null&&"wireframe"in r&&(r.wireframe=o)}))})}function be(t,e,s){var i;if(!y)return;oe.has(t)||oe.set(t,{keys:[],sourceId:s});const o=oe.get(t);((i=o==null?void 0:o.keys)==null?void 0:i.includes(e))==!1&&o.keys.push(e)}const _=class{constructor(t,e){this.loadMesh=s=>{var i,r;if(this._isLoadingMesh)return null;const o=(r=(i=this.parser.json.meshes[s])==null?void 0:i.extensions)==null?void 0:r[z];return o?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",s).then(n=>{var a;return this._isLoadingMesh=!1,n&&_.registerMesh(this.url,o.guid,n,(a=o.lods)==null?void 0:a.length,void 0,o),n})):null},y&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return z}static getMeshLODInformation(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getMaterialMinMaxLODsCount(t,e){const s=this,o="LODS:minmax",i=t[o];if(i!=null)return i;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const n of t)this.getMaterialMinMaxLODsCount(n,e);return t[o]=e,e}if(y==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const n=t;for(const a of Object.keys(n.uniforms)){const c=n.uniforms[a].value;(c==null?void 0:c.isTexture)===!0&&r(c,e)}}else if(t.isMaterial)for(const n of Object.keys(t)){const a=t[n];(a==null?void 0:a.isTexture)===!0&&r(a,e)}return t[o]=e,e;function r(n,a){const c=s.getAssignedLODInformation(n);if(c){const l=s.lodInfos.get(c.key);if(l&&l.lods){a.min_count=Math.min(a.min_count,l.lods.length),a.max_count=Math.max(a.max_count,l.lods.length);for(let d=0;d<l.lods.length;d++){const h=l.lods[d];h.width&&(a.lods[d]=a.lods[d]||{min_height:1/0,max_height:0},a.lods[d].min_height=Math.min(a.lods[d].min_height,h.height),a.lods[d].max_height=Math.max(a.lods[d].max_height,h.height))}}}}}static hasLODLevelAvailable(t,e){var i;if(Array.isArray(t)){for(const r of t)if(this.hasLODLevelAvailable(r,e))return!0;return!1}if(t.isMaterial===!0){for(const r of Object.keys(t)){const n=t[r];if(n&&n.isTexture&&this.hasLODLevelAvailable(n,e))return!0}return!1}else if(t.isGroup===!0){for(const r of t.children)if(r.isMesh===!0&&this.hasLODLevelAvailable(r,e))return!0}let s,o;if(t.isMesh?s=t.geometry:(t.isBufferGeometry||t.isTexture)&&(s=t),s&&(i=s==null?void 0:s.userData)!=null&&i.LODS){const r=s.userData.LODS;if(o=this.lodInfos.get(r.key),e===void 0)return o!=null;if(o)return Array.isArray(o.lods)?e<o.lods.length:e===0}return!1}static assignMeshLOD(t,e){var s;if(!t)return Promise.resolve(null);if(t instanceof g.Mesh||t.isMesh===!0){const o=t.geometry,i=this.getAssignedLODInformation(o);if(!i)return Promise.resolve(null);for(const r of H)(s=r.onBeforeGetLODMesh)==null||s.call(r,t,e);return t["LOD:requested level"]=e,_.getOrLoadLOD(o,e).then(r=>{if(Array.isArray(r)){const n=i.index||0;r=r[n]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],r&&o!=r&&((r==null?void 0:r.isBufferGeometry)?(t.geometry=r,y&&be(t,"geometry",i.url)):y&&console.error("Invalid LOD geometry",r))),r}).catch(r=>(console.error("Error loading mesh LOD",t,r),null))}else y&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",t);return Promise.resolve(null)}static assignTextureLOD(t,e=0){if(!t)return Promise.resolve(null);if(t.isMesh===!0){const s=t;if(Array.isArray(s.material)){const o=new Array;for(const i of s.material){const r=this.assignTextureLOD(i,e);o.push(r)}return Promise.all(o).then(i=>{const r=new Array;for(const n of i)Array.isArray(n)&&r.push(...n);return r})}else return this.assignTextureLOD(s.material,e)}if(t.isMaterial===!0){const s=t,o=[],i=new Array;if(y&&_e.add(s),s.uniforms&&(s.isRawShaderMaterial||s.isShaderMaterial===!0)){const r=s;for(const n of Object.keys(r.uniforms)){const a=r.uniforms[n].value;if((a==null?void 0:a.isTexture)===!0){const c=this.assignTextureLODForSlot(a,e,s,n).then(l=>(l&&r.uniforms[n].value!=l&&(r.uniforms[n].value=l,r.uniformsNeedUpdate=!0),l));o.push(c),i.push(n)}}}else for(const r of Object.keys(s)){const n=s[r];if((n==null?void 0:n.isTexture)===!0){const a=this.assignTextureLODForSlot(n,e,s,r);o.push(a),i.push(r)}}return Promise.all(o).then(r=>{const n=new Array;for(let a=0;a<r.length;a++){const c=r[a],l=i[a];c&&c.isTexture===!0?n.push({material:s,slot:l,texture:c,level:e}):n.push({material:s,slot:l,texture:null,level:e})}return n})}if(t instanceof g.Texture||t.isTexture===!0){const s=t;return this.assignTextureLODForSlot(s,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,s,o){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):o==="glyphMap"?Promise.resolve(t):_.getOrLoadLOD(t,e).then(i=>{if(Array.isArray(i))return null;if((i==null?void 0:i.isTexture)===!0){if(i!=t){if(s&&o){const r=s[o];if(r&&!y){const n=this.getAssignedLODInformation(r);if(n&&(n==null?void 0:n.level)<e)return y==="verbose"&&console.warn("Assigned texture level is already higher: ",n.level,e,s,r,i),null}s[o]=i}if(y&&o&&s){const r=this.getAssignedLODInformation(t);r?be(s,o,r.url):console.warn("No LOD info for texture",t)}}return i}else y=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(i=>(console.error("Error loading LOD",t,i),null))}afterRoot(t){var e,s;return y&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((o,i)=>{var r;if(o!=null&&o.extensions){const n=o==null?void 0:o.extensions[z];if(n){if(!n.lods){y&&console.warn("Texture has no LODs",n);return}let a=!1;for(const c of this.parser.associations.keys())if(c.isTexture===!0){const l=this.parser.associations.get(c);(l==null?void 0:l.textures)===i&&(a=!0,_.registerTexture(this.url,c,(r=n.lods)==null?void 0:r.length,i,n))}a||this.parser.getDependency("texture",i).then(c=>{var l;c&&_.registerTexture(this.url,c,(l=n.lods)==null?void 0:l.length,i,n)})}}}),(s=this.parser.json.meshes)==null||s.forEach((o,i)=>{if(o!=null&&o.extensions){const r=o==null?void 0:o.extensions[z];if(r&&r.lods){for(const n of this.parser.associations.keys())if(n.isMesh){const a=this.parser.associations.get(n);(a==null?void 0:a.meshes)===i&&_.registerMesh(this.url,r.guid,n,r.lods.length,a.primitives,r)}}}}),null}static async getOrLoadLOD(t,e){var n,a,c,l;const s=y=="verbose",o=t.userData.LODS;if(!o)return null;const i=o==null?void 0:o.key;let r;if(t.isTexture===!0){const d=t;d.source&&d.source[Me]&&(r=d.source[Me])}if(r||(r=_.lodInfos.get(i)),r){if(e>0){let D=!1;const m=Array.isArray(r.lods);if(m&&e>=r.lods.length?D=!0:m||(D=!0),D)return this.lowresCache.get(i)}const d=Array.isArray(r.lods)?(n=r.lods[e])==null?void 0:n.path:r.lods;if(!d)return y&&!r["missing:uri"]&&(r["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,r)),null;const h=ze(o.url,d);if(h.endsWith(".glb")||h.endsWith(".gltf")){if(!r.guid)return console.warn("missing pointer for glb/gltf texture",r),null;const D=h+"_"+r.guid,m=this.previouslyLoaded.get(D);if(m!==void 0){s&&console.log(`LOD ${e} was already loading/loaded: ${D}`);let f=await m.catch(N=>(console.error(`Error loading LOD ${e} from ${h}
|
|
2
2
|
`,N),null)),O=!1;if(f==null||(f instanceof g.Texture&&t instanceof g.Texture?(a=f.image)!=null&&a.data||(c=f.source)!=null&&c.data?f=this.copySettings(t,f):(O=!0,this.previouslyLoaded.delete(D)):f instanceof g.BufferGeometry&&t instanceof g.BufferGeometry&&((l=f.attributes.position)!=null&&l.array||(O=!0,this.previouslyLoaded.delete(D)))),!O)return f}const M=r,k=new Promise(async(f,O)=>{const N=new Z.GLTFLoader;Oe(N),y&&(await new Promise(S=>setTimeout(S,1e3)),s&&console.warn("Start loading (delayed) "+h,M.guid));let R=h;if(M&&Array.isArray(M.lods)){const S=M.lods[e];S.hash&&(R+="?v="+S.hash)}const A=await N.loadAsync(R).catch(S=>(console.error(`Error loading LOD ${e} from ${h}
|
|
3
3
|
`,S),null));if(!A)return null;const V=A.parser;s&&console.log("Loading finished "+h,M.guid);let w=0;if(A.parser.json.textures){let S=!1;for(const u of A.parser.json.textures){if(u!=null&&u.extensions){const p=u==null?void 0:u.extensions[z];if(p!=null&&p.guid&&p.guid===M.guid){S=!0;break}}w++}if(S){let u=await V.getDependency("texture",w);return u&&_.assignLODInformation(o.url,u,i,e,void 0,void 0),s&&console.log('change "'+t.name+'" → "'+u.name+'"',h,w,u,D),t instanceof g.Texture&&(u=this.copySettings(t,u)),u&&(u.guid=M.guid),f(u)}else y&&console.warn("Could not find texture with guid",M.guid,A.parser.json)}if(w=0,A.parser.json.meshes){let S=!1;for(const u of A.parser.json.meshes){if(u!=null&&u.extensions){const p=u==null?void 0:u.extensions[z];if(p!=null&&p.guid&&p.guid===M.guid){S=!0;break}}w++}if(S){const u=await V.getDependency("mesh",w),p=M;if(s&&console.log(`Loaded Mesh "${u.name}"`,h,w,u,D),u.isMesh===!0){const v=u.geometry;return _.assignLODInformation(o.url,v,i,e,void 0,p.density),f(v)}else{const v=new Array;for(let b=0;b<u.children.length;b++){const P=u.children[b];if(P.isMesh===!0){const X=P.geometry;_.assignLODInformation(o.url,X,i,e,b,p.density),v.push(X)}}return f(v)}}else y&&console.warn("Could not find mesh with guid",M.guid,A.parser.json)}return f(null)});return this.previouslyLoaded.set(D,k),await k}else if(t instanceof g.Texture){s&&console.log("Load texture from uri: "+h);const m=await new g.TextureLoader().loadAsync(h);return m?(m.guid=r.guid,m.flipY=!1,m.needsUpdate=!0,m.colorSpace=t.colorSpace,s&&console.log(r,m)):y&&console.warn("failed loading",h),m}}else y&&console.warn(`Can not load LOD ${e}: no LOD info found for "${i}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,s,o,i,r){if(!e)return;e.userData||(e.userData={});const n=new Je(t,s,o,i,r);e.userData.LODS=n}static getAssignedLODInformation(t){var e;return((e=t==null?void 0:t.userData)==null?void 0:e.LODS)||null}static copySettings(t,e){return e?(y&&console.warn(`Copy texture settings
|
|
4
4
|
`,t.uuid,`
|
|
5
|
-
`,e.uuid),e=e.clone(),e.offset=t.offset,e.repeat=t.repeat,e.colorSpace=t.colorSpace,e.magFilter=t.magFilter,e.minFilter=t.minFilter,e.wrapS=t.wrapS,e.wrapT=t.wrapT,e.flipY=t.flipY,e.anisotropy=t.anisotropy,e.mipmaps||(e.generateMipmaps=t.generateMipmaps),e):t}};let E=_;E.registerTexture=(t,e,s,o,i)=>{if(y&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,i),!e){y&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Me]=i);const r=i.guid;_.assignLODInformation(t,e,r,s,o,void 0),_.lodInfos.set(r,i),_.lowresCache.set(r,e)};E.registerMesh=(t,e,s,o,i,r)=>{var c;y&&console.log("> Progressive: register mesh",i,s.name,r,s.uuid,s);const n=s.geometry;if(!n){y&&console.warn("gltf-progressive: Register mesh without geometry");return}n.userData||(n.userData={}),_.assignLODInformation(t,n,e,o,i,r.density),_.lodInfos.set(e,r);let a=_.lowresCache.get(e);a?a.push(s.geometry):a=[s.geometry],_.lowresCache.set(e,a),o>0&&!ee(s)&&Ke(s,n);for(const l of H)(c=l.onRegisteredNewMesh)==null||c.call(l,s,r)};E.lodInfos=new Map;E.previouslyLoaded=new Map;E.lowresCache=new Map;class Je{constructor(e,s,o,i,r){this.url=e,this.key=s,this.level=o,i!=null&&(this.index=i),r!=null&&(this.density=r)}}var ke=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},L=(t,e,s)=>(ke(t,e,"read from private field"),s?s.call(t):e.get(t)),J=(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,o)=>(ke(t,e,"write to private field"),o?o.call(t,s):e.set(t,s),s),T,K,ve,Q,ie,fe,Y;const G=ne("debugprogressive"),Qe=ne("noprogressive"),ye=Symbol("Needle:LODSManager"),Le=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),I={mesh_lod:-1,texture_lod:-1},B=class{constructor(t,e){this.projectionScreenMatrix=new g.Matrix4,this.targetTriangleDensity=2e5,this.updateInterval="auto",J(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],J(this,K,void 0),J(this,ve,new g.Clock),J(this,Q,0),J(this,ie,0),J(this,fe,0),J(this,Y,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new g.Sphere,this._tempBox=new g.Box3,this._tempBox2=new g.Box3,this.tempMatrix=new g.Matrix4,this._tempWorldPosition=new g.Vector3,this._tempBoxSize=new g.Vector3,this._tempBox2Size=new g.Vector3,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Le]}static addPlugin(t){H.push(t)}static removePlugin(t){const e=H.indexOf(t);e>=0&&H.splice(e,1)}static get(t,e){if(t[ye])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ye];const s=new B(t,{engine:"unknown",...e});return t[ye]=s,s}get plugins(){return 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(L(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,o){const i=e.renderer.getRenderTarget();(i==null||"isXRRenderTarget"in i&&i.isXRRenderTarget)&&(t=0,W(e,Q,L(e,Q)+1),W(e,ie,L(e,ve).getDelta()),W(e,fe,L(e,fe)+L(e,ie)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/L(e,ie)),W(e,Y,e._fpsBuffer.reduce((n,a)=>n+a)/e._fpsBuffer.length),G&&L(e,Q)%200===0&&console.log("FPS",Math.round(L(e,Y)),"Interval:",L(e,T)));const r=t++;L(e,K).call(this,s,o),e.onAfterRender(s,o,r)}}disable(){L(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=L(this,K),W(this,K,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,s){if(this.pause)return;const i=this.renderer.renderLists.get(t,0).opaque;let r=!0;if(i.length===1){const n=i[0].material;(n.name==="EffectMaterial"||n.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(Qe||(this.updateInterval==="auto"?L(this,Y)<40&&L(this,T)<10?(W(this,T,L(this,T)+1),G&&console.warn("↓ Reducing LOD updates",L(this,T),L(this,Y).toFixed(0))):L(this,Y)>=60&&L(this,T)>1&&(W(this,T,L(this,T)-1),G&&console.warn("↑ Increasing LOD updates",L(this,T),L(this,Y).toFixed(0))):W(this,T,this.updateInterval),L(this,T)>0&&L(this,Q)%L(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var a,c;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((a=l.geometry)==null?void 0:a.type)==="BoxGeometry"||((c=l.geometry)==null?void 0:c.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){G&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const h=Math.random()*16777215,D=new g.MeshStandardMaterial({color:h});l.object.material=D}const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const r=s.transparent;for(const l of r){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const n=s.transmissive;for(const l of n){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}}updateLODs(t,e,s,o){var n,a;s.userData||(s.userData={});let i=s[Le];if(i||(i=new Ze,s[Le]=i),i.frames++<2)return;for(const c of H)(n=c.onBeforeUpdateLOD)==null||n.call(c,this.renderer,t,e,s);this.calculateLodLevel(e,s,i,o,I),I.mesh_lod=Math.round(I.mesh_lod),I.texture_lod=Math.round(I.texture_lod),I.mesh_lod>=0&&this.loadProgressiveMeshes(s,I.mesh_lod);let r=I.texture_lod;s.material&&r>=0&&this.loadProgressiveTextures(s.material,r);for(const c of H)(a=c.onAfterUpdatedLOD)==null||a.call(c,this.renderer,t,e,s,I);i.lastLodLevel_Mesh=I.mesh_lod,i.lastLodLevel_Texture=I.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const i of t)this.loadProgressiveTextures(i,e);return}let s=!1;(t[q]===void 0||e<t[q])&&(s=!0);const o=t["DEBUG:LOD"];o!=null&&(s=t[q]!=o,e=o),s&&(t[q]=e,E.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(r=>r({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[q]!==e;const o=t["DEBUG:LOD"];if(o!=null&&(s=t[q]!=o,e=o),s){t[q]=e;const i=t.geometry;return E.assignMeshLOD(t,e).then(r=>(r&&t[q]==e&&i!=t.geometry&&this._lodchangedlisteners.forEach(n=>n({type:"mesh",level:e,object:t})),r))}return Promise.resolve(null)}static isInside(t,e){const s=t.min,o=t.max,i=(s.x+o.x)*.5,r=(s.y+o.y)*.5;return this._tempPtInside.set(i,r,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,o,i){var k;if(!e){i.mesh_lod=-1,i.texture_lod=-1;return}if(!t){i.mesh_lod=-1,i.texture_lod=-1;return}let n=10+1,a=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const c=E.getMeshLODInformation(e.geometry),l=c==null?void 0:c.lods,d=l&&l.length>0,h=E.getMaterialMinMaxLODsCount(e.material),D=(h==null?void 0:h.min_count)!=1/0&&h.min_count>0&&h.max_count>0;if(!d&&!D){i.mesh_lod=0,i.texture_lod=0;return}d||(a=!0,n=0);const m=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let M=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const x=e;if(!x.boundingBox)x.computeBoundingBox();else if(s.frames%30===0){const f=ee(x),O=x.geometry;f&&(x.geometry=f),x.computeBoundingBox(),x.geometry=O}M=x.boundingBox}if(M){const x=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 u=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(u)){i.mesh_lod=0,i.texture_lod=0;return}}if(this._tempBox.copy(M),this._tempBox.applyMatrix4(e.matrixWorld),x.isPerspectiveCamera&&B.isInside(this._tempBox,this.projectionScreenMatrix)){i.mesh_lod=0,i.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&x.isPerspectiveCamera&&x.fov>70){const u=this._tempBox.min,p=this._tempBox.max;let v=u.x,b=u.y,P=p.x,X=p.y;const ae=2,he=1.5,le=(u.x+p.x)*.5,ce=(u.y+p.y)*.5;v=(v-le)*ae+le,b=(b-ce)*ae+ce,P=(P-le)*ae+le,X=(X-ce)*ae+ce;const Ie=v<0&&P>0?0:Math.min(Math.abs(u.x),Math.abs(p.x)),$e=b<0&&X>0?0:Math.min(Math.abs(u.y),Math.abs(p.y)),ge=Math.max(Ie,$e);s.lastCentrality=(he-ge)*(he-ge)*(he-ge)}else s.lastCentrality=1;const f=this._tempBox.getSize(this._tempBoxSize);f.multiplyScalar(.5),screen.availHeight>0&&m>0&&f.multiplyScalar(m/screen.availHeight),t.isPerspectiveCamera?f.x*=t.aspect:t.isOrthographicCamera;const O=t.matrixWorldInverse,N=this._tempBox2;N.copy(M),N.applyMatrix4(e.matrixWorld),N.applyMatrix4(O);const R=N.getSize(this._tempBox2Size),A=Math.max(R.x,R.y);if(Math.max(f.x,f.y)!=0&&A!=0&&(f.z=R.z/Math.max(R.x,R.y)*Math.max(f.x,f.y)),s.lastScreenCoverage=Math.max(f.x,f.y,f.z),s.lastScreenspaceVolume.copy(f),s.lastScreenCoverage*=s.lastCentrality,G&&B.debugDrawLine){const u=this.tempMatrix.copy(this.projectionScreenMatrix);u.invert();const p=B.corner0,v=B.corner1,b=B.corner2,P=B.corner3;p.copy(this._tempBox.min),v.copy(this._tempBox.max),v.x=p.x,b.copy(this._tempBox.max),b.y=p.y,P.copy(this._tempBox.max);const X=(p.z+P.z)*.5;p.z=v.z=b.z=P.z=X,p.applyMatrix4(u),v.applyMatrix4(u),b.applyMatrix4(u),P.applyMatrix4(u),B.debugDrawLine(p,v,255),B.debugDrawLine(p,b,255),B.debugDrawLine(v,P,255),B.debugDrawLine(b,P,255)}let w=999;if(l&&s.lastScreenCoverage>0){for(let u=0;u<l.length;u++)if(l[u].density/s.lastScreenCoverage<o){w=u;break}}w<n&&(n=w,a=!0)}if(a?i.mesh_lod=n:i.mesh_lod=s.lastLodLevel_Mesh,G&&i.mesh_lod!=s.lastLodLevel_Mesh){const f=l==null?void 0:l[i.mesh_lod];f&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${i.mesh_lod} (${f.density.toFixed(0)}) - ${e.name}`)}if(D){const x="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(i.texture_lod=h.max_count-1,G){const f=h.lods[h.max_count-1];G&&console.log(`First Texture LOD ${i.texture_lod} (${f.max_height}px) - ${e.name}`)}}else{const f=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let O=s.lastScreenCoverage*4;((k=this.context)==null?void 0:k.engine)==="model-viewer"&&(O*=1.5);const R=m/window.devicePixelRatio*O;let A=!1;for(let V=h.lods.length-1;V>=0;V--){let w=h.lods[V];if(!(x&&w.max_height>=2048)&&!(Xe()&&w.max_height>4096)&&(w.max_height>R||!A&&V===0)){if(A=!0,i.texture_lod=V,i.texture_lod<s.lastLodLevel_Texture){const S=w.max_height;G&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${i.texture_lod} = ${S}px
|
|
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 E=_;E.registerTexture=(t,e,s,o,i)=>{if(y&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,i),!e){y&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Me]=i);const r=i.guid;_.assignLODInformation(t,e,r,s,o,void 0),_.lodInfos.set(r,i),_.lowresCache.set(r,e)};E.registerMesh=(t,e,s,o,i,r)=>{var c;y&&console.log("> Progressive: register mesh",i,s.name,r,s.uuid,s);const n=s.geometry;if(!n){y&&console.warn("gltf-progressive: Register mesh without geometry");return}n.userData||(n.userData={}),_.assignLODInformation(t,n,e,o,i,r.density),_.lodInfos.set(e,r);let a=_.lowresCache.get(e);a?a.push(s.geometry):a=[s.geometry],_.lowresCache.set(e,a),o>0&&!ee(s)&&Ke(s,n);for(const l of H)(c=l.onRegisteredNewMesh)==null||c.call(l,s,r)};E.lodInfos=new Map;E.previouslyLoaded=new Map;E.lowresCache=new Map;class Je{constructor(e,s,o,i,r){this.url=e,this.key=s,this.level=o,i!=null&&(this.index=i),r!=null&&(this.density=r)}}var Re=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},L=(t,e,s)=>(Re(t,e,"read from private field"),s?s.call(t):e.get(t)),J=(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,o)=>(Re(t,e,"write to private field"),o?o.call(t,s):e.set(t,s),s),T,K,ve,Q,ie,fe,Y;const G=ne("debugprogressive"),Qe=ne("noprogressive"),ye=Symbol("Needle:LODSManager"),Le=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),I={mesh_lod:-1,texture_lod:-1},B=class{constructor(t,e){this.projectionScreenMatrix=new g.Matrix4,this.targetTriangleDensity=2e5,this.updateInterval="auto",J(this,T,1),this.pause=!1,this.manual=!1,this._lodchangedlisteners=[],J(this,K,void 0),J(this,ve,new g.Clock),J(this,Q,0),J(this,ie,0),J(this,fe,0),J(this,Y,0),this._fpsBuffer=[60,60,60,60,60],this._sphere=new g.Sphere,this._tempBox=new g.Box3,this._tempBox2=new g.Box3,this.tempMatrix=new g.Matrix4,this._tempWorldPosition=new g.Vector3,this._tempBoxSize=new g.Vector3,this._tempBox2Size=new g.Vector3,this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Le]}static addPlugin(t){H.push(t)}static removePlugin(t){const e=H.indexOf(t);e>=0&&H.splice(e,1)}static get(t,e){if(t[ye])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[ye];const s=new B(t,{engine:"unknown",...e});return t[ye]=s,s}get plugins(){return 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(L(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,o){const i=e.renderer.getRenderTarget();(i==null||"isXRRenderTarget"in i&&i.isXRRenderTarget)&&(t=0,W(e,Q,L(e,Q)+1),W(e,ie,L(e,ve).getDelta()),W(e,fe,L(e,fe)+L(e,ie)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/L(e,ie)),W(e,Y,e._fpsBuffer.reduce((n,a)=>n+a)/e._fpsBuffer.length),G&&L(e,Q)%200===0&&console.log("FPS",Math.round(L(e,Y)),"Interval:",L(e,T)));const r=t++;L(e,K).call(this,s,o),e.onAfterRender(s,o,r)}}disable(){L(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=L(this,K),W(this,K,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,s){if(this.pause)return;const i=this.renderer.renderLists.get(t,0).opaque;let r=!0;if(i.length===1){const n=i[0].material;(n.name==="EffectMaterial"||n.name==="CopyShader")&&(r=!1)}if((e.parent&&e.parent.type==="CubeCamera"||s>=1&&e.type==="OrthographicCamera")&&(r=!1),r){if(Qe||(this.updateInterval==="auto"?L(this,Y)<40&&L(this,T)<10?(W(this,T,L(this,T)+1),G&&console.warn("↓ Reducing LOD updates",L(this,T),L(this,Y).toFixed(0))):L(this,Y)>=60&&L(this,T)>1&&(W(this,T,L(this,T)-1),G&&console.warn("↑ Increasing LOD updates",L(this,T),L(this,Y).toFixed(0))):W(this,T,this.updateInterval),L(this,T)>0&&L(this,Q)%L(this,T)!=0))return;this.internalUpdate(t,e)}}internalUpdate(t,e){var a,c;const s=this.renderer.renderLists.get(t,0),o=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const i=this.targetTriangleDensity;for(const l of o){if(l.material&&(((a=l.geometry)==null?void 0:a.type)==="BoxGeometry"||((c=l.geometry)==null?void 0:c.type)==="BufferGeometry")&&(l.material.name==="SphericalGaussianBlur"||l.material.name=="BackgroundCubeMaterial"||l.material.name==="CubemapFromEquirect"||l.material.name==="EquirectangularToCubeUV")){G&&(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(l.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",l,l.material.name,l.material.type)));continue}switch(l.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&l.material&&!l.object.progressive_debug_color){l.object.progressive_debug_color=!0;const h=Math.random()*16777215,D=new g.MeshStandardMaterial({color:h});l.object.material=D}const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const r=s.transparent;for(const l of r){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}const n=s.transmissive;for(const l of n){const d=l.object;(d instanceof g.Mesh||d.isMesh)&&this.updateLODs(t,e,d,i)}}updateLODs(t,e,s,o){var n,a;s.userData||(s.userData={});let i=s[Le];if(i||(i=new Ze,s[Le]=i),i.frames++<2)return;for(const c of H)(n=c.onBeforeUpdateLOD)==null||n.call(c,this.renderer,t,e,s);this.calculateLodLevel(e,s,i,o,I),I.mesh_lod=Math.round(I.mesh_lod),I.texture_lod=Math.round(I.texture_lod),I.mesh_lod>=0&&this.loadProgressiveMeshes(s,I.mesh_lod);let r=I.texture_lod;s.material&&r>=0&&this.loadProgressiveTextures(s.material,r);for(const c of H)(a=c.onAfterUpdatedLOD)==null||a.call(c,this.renderer,t,e,s,I);i.lastLodLevel_Mesh=I.mesh_lod,i.lastLodLevel_Texture=I.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const i of t)this.loadProgressiveTextures(i,e);return}let s=!1;(t[q]===void 0||e<t[q])&&(s=!0);const o=t["DEBUG:LOD"];o!=null&&(s=t[q]!=o,e=o),s&&(t[q]=e,E.assignTextureLOD(t,e).then(i=>{this._lodchangedlisteners.forEach(r=>r({type:"texture",level:e,object:t}))}))}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let s=t[q]!==e;const o=t["DEBUG:LOD"];if(o!=null&&(s=t[q]!=o,e=o),s){t[q]=e;const i=t.geometry;return E.assignMeshLOD(t,e).then(r=>(r&&t[q]==e&&i!=t.geometry&&this._lodchangedlisteners.forEach(n=>n({type:"mesh",level:e,object:t})),r))}return Promise.resolve(null)}static isInside(t,e){const s=t.min,o=t.max,i=(s.x+o.x)*.5,r=(s.y+o.y)*.5;return this._tempPtInside.set(i,r,s.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,s,o,i){var k;if(!e){i.mesh_lod=-1,i.texture_lod=-1;return}if(!t){i.mesh_lod=-1,i.texture_lod=-1;return}let n=10+1,a=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const c=E.getMeshLODInformation(e.geometry),l=c==null?void 0:c.lods,d=l&&l.length>0,h=E.getMaterialMinMaxLODsCount(e.material),D=(h==null?void 0:h.min_count)!=1/0&&h.min_count>0&&h.max_count>0;if(!d&&!D){i.mesh_lod=0,i.texture_lod=0;return}d||(a=!0,n=0);const m=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let M=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const x=e;if(!x.boundingBox)x.computeBoundingBox();else if(s.frames%30===0){const f=ee(x),O=x.geometry;f&&(x.geometry=f),x.computeBoundingBox(),x.geometry=O}M=x.boundingBox}if(M){const x=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 u=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(u)){i.mesh_lod=0,i.texture_lod=0;return}}if(this._tempBox.copy(M),this._tempBox.applyMatrix4(e.matrixWorld),x.isPerspectiveCamera&&B.isInside(this._tempBox,this.projectionScreenMatrix)){i.mesh_lod=0,i.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&x.isPerspectiveCamera&&x.fov>70){const u=this._tempBox.min,p=this._tempBox.max;let v=u.x,b=u.y,P=p.x,X=p.y;const ae=2,he=1.5,le=(u.x+p.x)*.5,ce=(u.y+p.y)*.5;v=(v-le)*ae+le,b=(b-ce)*ae+ce,P=(P-le)*ae+le,X=(X-ce)*ae+ce;const $e=v<0&&P>0?0:Math.min(Math.abs(u.x),Math.abs(p.x)),Ne=b<0&&X>0?0:Math.min(Math.abs(u.y),Math.abs(p.y)),ge=Math.max($e,Ne);s.lastCentrality=(he-ge)*(he-ge)*(he-ge)}else s.lastCentrality=1;const f=this._tempBox.getSize(this._tempBoxSize);f.multiplyScalar(.5),screen.availHeight>0&&m>0&&f.multiplyScalar(m/screen.availHeight),t.isPerspectiveCamera?f.x*=t.aspect:t.isOrthographicCamera;const O=t.matrixWorldInverse,N=this._tempBox2;N.copy(M),N.applyMatrix4(e.matrixWorld),N.applyMatrix4(O);const R=N.getSize(this._tempBox2Size),A=Math.max(R.x,R.y);if(Math.max(f.x,f.y)!=0&&A!=0&&(f.z=R.z/Math.max(R.x,R.y)*Math.max(f.x,f.y)),s.lastScreenCoverage=Math.max(f.x,f.y,f.z),s.lastScreenspaceVolume.copy(f),s.lastScreenCoverage*=s.lastCentrality,G&&B.debugDrawLine){const u=this.tempMatrix.copy(this.projectionScreenMatrix);u.invert();const p=B.corner0,v=B.corner1,b=B.corner2,P=B.corner3;p.copy(this._tempBox.min),v.copy(this._tempBox.max),v.x=p.x,b.copy(this._tempBox.max),b.y=p.y,P.copy(this._tempBox.max);const X=(p.z+P.z)*.5;p.z=v.z=b.z=P.z=X,p.applyMatrix4(u),v.applyMatrix4(u),b.applyMatrix4(u),P.applyMatrix4(u),B.debugDrawLine(p,v,255),B.debugDrawLine(p,b,255),B.debugDrawLine(v,P,255),B.debugDrawLine(b,P,255)}let w=999;if(l&&s.lastScreenCoverage>0){for(let u=0;u<l.length;u++)if(l[u].density/s.lastScreenCoverage<o){w=u;break}}w<n&&(n=w,a=!0)}if(a?i.mesh_lod=n:i.mesh_lod=s.lastLodLevel_Mesh,G&&i.mesh_lod!=s.lastLodLevel_Mesh){const f=l==null?void 0:l[i.mesh_lod];f&&console.log(`Mesh LOD changed: ${s.lastLodLevel_Mesh} → ${i.mesh_lod} (${f.density.toFixed(0)}) - ${e.name}`)}if(D){const x="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(s.lastLodLevel_Texture<0){if(i.texture_lod=h.max_count-1,G){const f=h.lods[h.max_count-1];G&&console.log(`First Texture LOD ${i.texture_lod} (${f.max_height}px) - ${e.name}`)}}else{const f=s.lastScreenspaceVolume.x+s.lastScreenspaceVolume.y+s.lastScreenspaceVolume.z;let O=s.lastScreenCoverage*4;((k=this.context)==null?void 0:k.engine)==="model-viewer"&&(O*=1.5);const R=m/window.devicePixelRatio*O;let A=!1;for(let V=h.lods.length-1;V>=0;V--){let w=h.lods[V];if(!(x&&w.max_height>=2048)&&!(Xe()&&w.max_height>4096)&&(w.max_height>R||!A&&V===0)){if(A=!0,i.texture_lod=V,i.texture_lod<s.lastLodLevel_Texture){const S=w.max_height;G&&console.log(`Texture LOD changed: ${s.lastLodLevel_Texture} → ${i.texture_lod} = ${S}px
|
|
6
6
|
Screensize: ${R.toFixed(0)}px, Coverage: ${(100*s.lastScreenCoverage).toFixed(2)}%, Volume ${f.toFixed(1)}
|
|
7
|
-
${e.name}`)}break}}}}else i.texture_lod=0}};let U=B;T=new WeakMap;K=new WeakMap;ve=new WeakMap;Q=new WeakMap;ie=new WeakMap;fe=new WeakMap;Y=new WeakMap;U.corner0=new g.Vector3;U.corner1=new g.Vector3;U.corner2=new g.Vector3;U.corner3=new g.Vector3;U._tempPtInside=new g.Vector3;class Ze{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new g.Vector3,this.lastCentrality=0}}const Te=Symbol("NEEDLE_mesh_lod"),ue=Symbol("NEEDLE_texture_lod");let re=null;function
|
|
8
|
-
`,t.getAttribute("src"));let e=null,s=null,o=null;for(let i=t;i!=null;i=Object.getPrototypeOf(i)){const r=Object.getOwnPropertySymbols(i),n=r.find(l=>l.toString()=="Symbol(renderer)"),a=r.find(l=>l.toString()=="Symbol(scene)"),c=r.find(l=>l.toString()=="Symbol(needsRender)");!e&&n!=null&&(e=t[n].threeRenderer),!s&&a!=null&&(s=t[a]),!o&&c!=null&&(o=t[c])}if(e&&s){let i=function(){if(o){let n=0,a=setInterval(()=>{if(n++>5){clearInterval(a);return}o==null||o.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const r=U.get(e,{engine:"model-viewer"});return U.addPlugin(new tt),r.enable(),r.addEventListener("changed",()=>{o==null||o.call(t)}),t.addEventListener("model-visibility",n=>{n.detail.visible&&(o==null||o.call(t))}),t.addEventListener("load",()=>{i()}),()=>{r.disable()}}return null}class tt{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,s,o,i){this.tryParseMeshLOD(s,i),this.tryParseTextureLOD(s,i)}getUrl(e){if(!e)return null;let s=e.getAttribute("src");return s||(s=e.src),s||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),s}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,s){if(s[ue]==!0)return;s[ue]=!0;const o=this.tryGetCurrentGLTF(e),i=this.tryGetCurrentModelViewer(e),r=this.getUrl(i);if(r&&o&&s.material){let n=function(c){var d,h,D;if(c[ue]==!0)return;c[ue]=!0,c.userData&&(c.userData.LOD=-1);const l=Object.keys(c);for(let m=0;m<l.length;m++){const M=l[m],k=c[M];if((k==null?void 0:k.isTexture)===!0){const x=(h=(d=k.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(x==null)continue;const f=o.parser.json.textures[x];if(!f){console.warn("Texture data not found for texture index "+x);continue}if((D=f==null?void 0:f.extensions)!=null&&D[z]){const O=f.extensions[z];O&&r&&E.registerTexture(r,k,O.lods.length,x,O)}}}};const a=s.material;if(Array.isArray(a))for(const c of a)n(c);else n(a)}}tryParseMeshLOD(e,s){var n,a;if(s[Te]==!0)return;s[Te]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const r=(a=(n=s.userData)==null?void 0:n.gltfExtensions)==null?void 0:a[z];if(r&&i){const c=s.uuid;E.registerMesh(i,c,s,0,r.lods.length,r)}}}function st(t,e,s,o){we(e),Oe(s),Se(s,{progressive:!0,...o==null?void 0:o.hints}),s.register(r=>new E(r,t));const i=U.get(e);return(o==null?void 0:o.enableLODsManager)!==!1&&i.enable(),i}
|
|
7
|
+
${e.name}`)}break}}}}else i.texture_lod=0}};let U=B;T=new WeakMap;K=new WeakMap;ve=new WeakMap;Q=new WeakMap;ie=new WeakMap;fe=new WeakMap;Y=new WeakMap;U.corner0=new g.Vector3;U.corner1=new g.Vector3;U.corner2=new g.Vector3;U.corner3=new g.Vector3;U._tempPtInside=new g.Vector3;class Ze{constructor(){this.frames=0,this.lastLodLevel_Mesh=-1,this.lastLodLevel_Texture=-1,this.lastScreenCoverage=0,this.lastScreenspaceVolume=new g.Vector3,this.lastCentrality=0}}const Te=Symbol("NEEDLE_mesh_lod"),ue=Symbol("NEEDLE_texture_lod");let re=null;function Ge(){const t=je();t&&(t.mapURLs(function(e){return Ee(),e}),Ee(),re==null||re.disconnect(),re=new MutationObserver(e=>{e.forEach(s=>{s.addedNodes.forEach(o=>{o instanceof HTMLElement&&o.tagName.toLowerCase()==="model-viewer"&&Ie(o)})})}),re.observe(document,{childList:!0,subtree:!0}))}function je(){if(typeof customElements>"u")return null;const t=customElements.get("model-viewer");return t||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Ge()}),null)}function Ee(){if(typeof document>"u")return;document.querySelectorAll("model-viewer").forEach(e=>{Ie(e)})}const Ae=new WeakSet;let et=0;function Ie(t){if(!t||Ae.has(t))return null;Ae.add(t),console.debug("[gltf-progressive] found new model-viewer..."+ ++et+`
|
|
8
|
+
`,t.getAttribute("src"));let e=null,s=null,o=null;for(let i=t;i!=null;i=Object.getPrototypeOf(i)){const r=Object.getOwnPropertySymbols(i),n=r.find(l=>l.toString()=="Symbol(renderer)"),a=r.find(l=>l.toString()=="Symbol(scene)"),c=r.find(l=>l.toString()=="Symbol(needsRender)");!e&&n!=null&&(e=t[n].threeRenderer),!s&&a!=null&&(s=t[a]),!o&&c!=null&&(o=t[c])}if(e&&s){let i=function(){if(o){let n=0,a=setInterval(()=>{if(n++>5){clearInterval(a);return}o==null||o.call(t)},300)}};console.debug("[gltf-progressive] setup model-viewer");const r=U.get(e,{engine:"model-viewer"});return U.addPlugin(new tt),r.enable(),r.addEventListener("changed",()=>{o==null||o.call(t)}),t.addEventListener("model-visibility",n=>{n.detail.visible&&(o==null||o.call(t))}),t.addEventListener("load",()=>{i()}),()=>{r.disable()}}return null}class tt{constructor(){this._didWarnAboutMissingUrl=!1}onBeforeUpdateLOD(e,s,o,i){this.tryParseMeshLOD(s,i),this.tryParseTextureLOD(s,i)}getUrl(e){if(!e)return null;let s=e.getAttribute("src");return s||(s=e.src),s||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),s}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,s){if(s[ue]==!0)return;s[ue]=!0;const o=this.tryGetCurrentGLTF(e),i=this.tryGetCurrentModelViewer(e),r=this.getUrl(i);if(r&&o&&s.material){let n=function(c){var d,h,D;if(c[ue]==!0)return;c[ue]=!0,c.userData&&(c.userData.LOD=-1);const l=Object.keys(c);for(let m=0;m<l.length;m++){const M=l[m],k=c[M];if((k==null?void 0:k.isTexture)===!0){const x=(h=(d=k.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(x==null)continue;const f=o.parser.json.textures[x];if(!f){console.warn("Texture data not found for texture index "+x);continue}if((D=f==null?void 0:f.extensions)!=null&&D[z]){const O=f.extensions[z];O&&r&&E.registerTexture(r,k,O.lods.length,x,O)}}}};const a=s.material;if(Array.isArray(a))for(const c of a)n(c);else n(a)}}tryParseMeshLOD(e,s){var n,a;if(s[Te]==!0)return;s[Te]=!0;const o=this.tryGetCurrentModelViewer(e),i=this.getUrl(o);if(!i)return;const r=(a=(n=s.userData)==null?void 0:n.gltfExtensions)==null?void 0:a[z];if(r&&i){const c=s.uuid;E.registerMesh(i,c,s,0,r.lods.length,r)}}}function st(t,e,s,o){we(e),Oe(s),Se(s,{progressive:!0,...o==null?void 0:o.hints}),s.register(r=>new E(r,t));const i=U.get(e);return(o==null?void 0:o.enableLODsManager)!==!1&&i.enable(),i}Ge();if(!qe){const t={gltfProgressive:{useNeedleProgressive:st,LODsManager:U,configureLoader:Se,getRaycastMesh:ee,useRaycastMeshes:Ye}};if(!globalThis.Needle)globalThis.Needle=t;else for(const e in t)globalThis.Needle[e]=t[e]}exports.LODsManager=U;exports.NEEDLE_progressive=E;exports.addDracoAndKTX2Loaders=Oe;exports.configureLoader=Se;exports.createLoaders=we;exports.getRaycastMesh=ee;exports.setDracoDecoderLocation=Ce;exports.setKTX2TranscoderLocation=Be;
|