@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
3 lines (2 loc) • 8.44 kB
JavaScript
import{b as e,C as t,a1 as a,T as s}from"./index-Bw2N6w1y.esm.min.js";function r(){const e=0,t=1,a=2,s=3,r=6,n=8,o=9,i=10,c=14;let l=null;function T(e,t,a,s,r){const n=e.getImageTranscodedSizeInBytes(t,a,s);let o=new Uint8Array(n);if(!e.transcodeImage(o,t,a,s,1,0))return null;if(r){o=function(e,t,a,s){const r=new Uint16Array(4),n=new Uint16Array(a*s),o=a/4,i=s/4;for(let s=0;s<i;s++)for(let i=0;i<o;i++){const c=t+8*(s*o+i);r[0]=e[c]|e[c+1]<<8,r[1]=e[c+2]|e[c+3]<<8,r[2]=(2*(31&r[0])+1*(31&r[1]))/3|(2*(2016&r[0])+1*(2016&r[1]))/3&2016|(2*(63488&r[0])+1*(63488&r[1]))/3&63488,r[3]=(2*(31&r[1])+1*(31&r[0]))/3|(2*(2016&r[1])+1*(2016&r[0]))/3&2016|(2*(63488&r[1])+1*(63488&r[0]))/3&63488;for(let t=0;t<4;t++){const o=e[c+4+t];let l=(4*s+t)*a+4*i;n[l++]=r[3&o],n[l++]=r[o>>2&3],n[l++]=r[o>>4&3],n[l++]=r[o>>6&3]}}return n}(o,0,e.getImageWidth(t,a)+3&-4,e.getImageHeight(t,a)+3&-4)}return o}onmessage=d=>{if("init"===d.data.action){if(d.data.url)try{importScripts(d.data.url)}catch(e){postMessage({action:"error",error:e})}l||(l=BASIS({wasmBinary:d.data.wasmBinary})),null!==l&&l.then((e=>{BASIS=e,e.initializeBasis(),postMessage({action:"init"})}))}else if("transcode"===d.data.action){const l=d.data.config,R=d.data.imageData,u=new BASIS.BasisFile(R),_=function(e){const t=e.getHasAlpha(),a=e.getNumImages(),s=[];for(let t=0;t<a;t++){const a={levels:[]},r=e.getNumLevels(t);for(let s=0;s<r;s++){const r={width:e.getImageWidth(t,s),height:e.getImageHeight(t,s)};a.levels.push(r)}s.push(a)}return{hasAlpha:t,images:s}}(u);let p=d.data.ignoreSupportedFormats?null:function(l,T){let d=null;l.supportedCompressionFormats&&(d=l.supportedCompressionFormats.astc?i:l.supportedCompressionFormats.bc7?r:l.supportedCompressionFormats.s3tc?T.hasAlpha?s:a:l.supportedCompressionFormats.pvrtc?T.hasAlpha?o:n:l.supportedCompressionFormats.etc2?t:l.supportedCompressionFormats.etc1?e:c);return d}(d.data.config,_),g=!1;null===p&&(g=!0,p=_.hasAlpha?s:a);let m=!0;u.startTranscoding()||(m=!1);const f=[];for(let e=0;e<_.images.length&&m;e++){const t=_.images[e];if(void 0===l.loadSingleImage||l.loadSingleImage===e){let a=t.levels.length;!1===l.loadMipmapLevels&&(a=1);for(let s=0;s<a;s++){const a=t.levels[s],r=T(u,e,s,p,g);if(!r){m=!1;break}a.transcodedPixels=r,f.push(a.transcodedPixels.buffer)}}}u.close(),u.delete(),g&&(p=-1),m?postMessage({action:"transcode",success:m,id:d.data.id,fileInfo:_,format:p},f):postMessage({action:"transcode",success:m,id:d.data.id})}}}var n;!function(e){e[e.cTFETC1=0]="cTFETC1",e[e.cTFETC2=1]="cTFETC2",e[e.cTFBC1=2]="cTFBC1",e[e.cTFBC3=3]="cTFBC3",e[e.cTFBC4=4]="cTFBC4",e[e.cTFBC5=5]="cTFBC5",e[e.cTFBC7=6]="cTFBC7",e[e.cTFPVRTC1_4_RGB=8]="cTFPVRTC1_4_RGB",e[e.cTFPVRTC1_4_RGBA=9]="cTFPVRTC1_4_RGBA",e[e.cTFASTC_4x4=10]="cTFASTC_4x4",e[e.cTFATC_RGB=11]="cTFATC_RGB",e[e.cTFATC_RGBA_INTERPOLATED_ALPHA=12]="cTFATC_RGBA_INTERPOLATED_ALPHA",e[e.cTFRGBA32=13]="cTFRGBA32",e[e.cTFRGB565=14]="cTFRGB565",e[e.cTFBGR565=15]="cTFBGR565",e[e.cTFRGBA4444=16]="cTFRGBA4444",e[e.cTFFXT1_RGB=17]="cTFFXT1_RGB",e[e.cTFPVRTC2_4_RGB=18]="cTFPVRTC2_4_RGB",e[e.cTFPVRTC2_4_RGBA=19]="cTFPVRTC2_4_RGBA",e[e.cTFETC2_EAC_R11=20]="cTFETC2_EAC_R11",e[e.cTFETC2_EAC_RG11=21]="cTFETC2_EAC_RG11"}(n||(n={}));const o={JSModuleURL:`${e._DefaultCdnUrl}/basisTranscoder/1/basis_transcoder.js`,WasmModuleURL:`${e._DefaultCdnUrl}/basisTranscoder/1/basis_transcoder.wasm`};let i=null,c=null,l=0;const T=async()=>(i||(i=new Promise(((t,a)=>{c?t(c):e.LoadFileAsync(e.GetBabylonScriptURL(o.WasmModuleURL)).then((s=>{if("function"!=typeof URL)return a("Basis transcoder requires an environment with a URL constructor");const n=URL.createObjectURL(new Blob([`(${r})()`],{type:"application/javascript"}));c=new Worker(n),async function(t,a,s){return await new Promise(((r,n)=>{const o=e=>{"init"===e.data.action?(t.removeEventListener("message",o),r(t)):"error"===e.data.action&&n(e.data.error||"error initializing worker")};t.addEventListener("message",o),t.postMessage({action:"init",url:s?e.GetBabylonScriptURL(s):void 0,wasmBinary:a},[a])}))}(c,s,o.JSModuleURL).then(t,a)})).catch(a)}))),await i),d=async(e,t)=>{const a=e instanceof ArrayBuffer?new Uint8Array(e):e;return await new Promise(((e,s)=>{T().then((()=>{const r=l++,n=t=>{"transcode"===t.data.action&&t.data.id===r&&(c.removeEventListener("message",n),t.data.success?e(t.data):s("Transcode is not supported on this device"))};c.addEventListener("message",n);const o=new Uint8Array(a.byteLength);o.set(new Uint8Array(a.buffer,a.byteOffset,a.byteLength)),c.postMessage({action:"transcode",id:r,imageData:o,config:t,ignoreSupportedFormats:false},[o.buffer])}),(e=>{s(e)}))}))},R=(e,t)=>{let a=t._gl?.TEXTURE_2D;e.isCube&&(a=t._gl?.TEXTURE_CUBE_MAP),t._bindTextureDirectly(a,e,!0)},u=(r,o)=>{const i=r.getEngine();for(let c=0;c<o.fileInfo.images.length;c++){const l=o.fileInfo.images[c].levels[0];if(r._invertVScale=r.invertY,-1===o.format||o.format===n.cTFRGB565)if(r.type=t.TEXTURETYPE_UNSIGNED_SHORT_5_6_5,r.format=t.TEXTUREFORMAT_RGB,!i._features.basisNeedsPOT||Math.log2(l.width)%1==0&&Math.log2(l.height)%1==0)r._invertVScale=!r.invertY,r.width=l.width+3&-4,r.height=l.height+3&-4,r.samplingMode=t.TEXTURE_LINEAR_LINEAR,R(r,i),i._uploadDataToTextureDirectly(r,new Uint16Array(l.transcodedPixels.buffer),c,0,t.TEXTUREFORMAT_RGB,!0);else{const e=new a(i,2);r._invertVScale=r.invertY,e.type=t.TEXTURETYPE_UNSIGNED_SHORT_5_6_5,e.format=t.TEXTUREFORMAT_RGB,e.width=l.width+3&-4,e.height=l.height+3&-4,R(e,i),i._uploadDataToTextureDirectly(e,new Uint16Array(l.transcodedPixels.buffer),c,0,t.TEXTUREFORMAT_RGB,!0),i._rescaleTexture(e,r,i.scenes[0],i._getInternalFormat(t.TEXTUREFORMAT_RGB),(()=>{i._releaseTexture(e),R(r,i)}))}else{r.width=l.width,r.height=l.height,r.generateMipMaps=o.fileInfo.images[c].levels.length>1;const t=_.GetInternalFormatFromBasisFormat(o.format,i);r.format=t,R(r,i);const a=o.fileInfo.images[c].levels;for(let e=0;e<a.length;e++){const s=a[e];i._uploadCompressedDataToTextureDirectly(r,t,s.width,s.height,s.transcodedPixels,c,e)}!i._features.basisNeedsPOT||Math.log2(r.width)%1==0&&Math.log2(r.height)%1==0||(e.Warn("Loaded .basis texture width and height are not a power of two. Texture wrapping will be set to Texture.CLAMP_ADDRESSMODE as other modes are not supported with non power of two dimensions in webGL 1."),r._cachedWrapU=s.CLAMP_ADDRESSMODE,r._cachedWrapV=s.CLAMP_ADDRESSMODE)}}},_={JSModuleURL:o.JSModuleURL,WasmModuleURL:o.WasmModuleURL,GetInternalFormatFromBasisFormat:(e,a)=>{let s;switch(e){case n.cTFETC1:s=t.TEXTUREFORMAT_COMPRESSED_RGB_ETC1_WEBGL;break;case n.cTFBC1:s=t.TEXTUREFORMAT_COMPRESSED_RGB_S3TC_DXT1;break;case n.cTFBC4:s=t.TEXTUREFORMAT_COMPRESSED_RGBA_S3TC_DXT5;break;case n.cTFASTC_4x4:s=t.TEXTUREFORMAT_COMPRESSED_RGBA_ASTC_4x4;break;case n.cTFETC2:s=t.TEXTUREFORMAT_COMPRESSED_RGBA8_ETC2_EAC;break;case n.cTFBC7:s=t.TEXTUREFORMAT_COMPRESSED_RGBA_BPTC_UNORM}if(void 0===s)throw"The chosen Basis transcoder format is not currently supported";return s},TranscodeAsync:d,LoadTextureFromTranscodeResult:u};Object.defineProperty(_,"JSModuleURL",{get:function(){return o.JSModuleURL},set:function(e){o.JSModuleURL=e}}),Object.defineProperty(_,"WasmModuleURL",{get:function(){return o.WasmModuleURL},set:function(e){o.WasmModuleURL=e}});class p{constructor(){this.supportCascades=!1}loadCubeData(t,a,s,r,n){if(Array.isArray(t))return;const o=a.getEngine().getCaps(),i={supportedCompressionFormats:{etc1:!!o.etc1,s3tc:!!o.s3tc,pvrtc:!!o.pvrtc,etc2:!!o.etc2,astc:!!o.astc,bc7:!!o.bptc}};d(t,i).then((e=>{const t=e.fileInfo.images[0].levels.length>1&&a.generateMipMaps;u(a,e),a.getEngine()._setCubeMapTextureParams(a,t),a.isReady=!0,a.onLoadedObservable.notifyObservers(a),a.onLoadedObservable.clear(),r&&r()})).catch((t=>{e.Warn("Failed to transcode Basis file, transcoding may not be supported on this device"),a.isReady=!0,n&&n(t)}))}loadData(t,a,s){const r=a.getEngine().getCaps(),n={supportedCompressionFormats:{etc1:!!r.etc1,s3tc:!!r.s3tc,pvrtc:!!r.pvrtc,etc2:!!r.etc2,astc:!!r.astc,bc7:!!r.bptc}};d(t,n).then((e=>{const t=e.fileInfo.images[0].levels[0],r=e.fileInfo.images[0].levels.length>1&&a.generateMipMaps;s(t.width,t.height,r,-1!==e.format,(()=>{u(a,e)}))})).catch((t=>{e.Warn("Failed to transcode Basis file, transcoding may not be supported on this device"),e.Warn(`Failed to transcode Basis file: ${t}`),s(0,0,!1,!1,(()=>{}),!0)}))}}export{p as _BasisTextureLoader};
//# sourceMappingURL=basisTextureLoader-yQzVMtZ8.esm.min.js.map