@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.19 kB
JavaScript
import{aY as e,C as t,w as a,y as s,m as r}from"./index-BPURObVq.esm.min.js";function n(){const e=0,t=1,a=2,s=3,r=6,n=8,o=9,i=10,c=14;let T=null;function l(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 T=(4*s+t)*a+4*i;n[T++]=r[3&o],n[T++]=r[o>>2&3],n[T++]=r[o>>4&3],n[T]=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})}T||(T=BASIS({wasmBinary:d.data.wasmBinary})),null!==T&&T.then(e=>{BASIS=e,e.initializeBasis(),postMessage({action:"init"})})}else if("transcode"===d.data.action){const T=d.data.config,R=d.data.imageData,_=new BASIS.BasisFile(R),p=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}}(_);let g=d.data.ignoreSupportedFormats?null:function(T,l){let d=null;T.supportedCompressionFormats&&(d=T.supportedCompressionFormats.astc?i:T.supportedCompressionFormats.bc7?r:T.supportedCompressionFormats.s3tc?l.hasAlpha?s:a:T.supportedCompressionFormats.pvrtc?l.hasAlpha?o:n:T.supportedCompressionFormats.etc2?t:T.supportedCompressionFormats.etc1?e:c);return d}(d.data.config,p),u=!1;null===g&&(u=!0,g=p.hasAlpha?s:a);let m=!0;_.startTranscoding()||(m=!1);const h=[];for(let e=0;e<p.images.length&&m;e++){const t=p.images[e];if(void 0===T.loadSingleImage||T.loadSingleImage===e){let a=t.levels.length;!1===T.loadMipmapLevels&&(a=1);for(let s=0;s<a;s++){const a=t.levels[s],r=l(_,e,s,g,u);if(!r){m=!1;break}a.transcodedPixels=r,h.push(a.transcodedPixels.buffer)}}}_.close(),_.delete(),u&&(g=-1),m?postMessage({action:"transcode",success:m,id:d.data.id,fileInfo:p,format:g},h):postMessage({action:"transcode",success:m,id:d.data.id})}}}var o;!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"}(o||(o={}));const i={JSModuleURL:`${e._DefaultCdnUrl}/basisTranscoder/1/basis_transcoder.js`,WasmModuleURL:`${e._DefaultCdnUrl}/basisTranscoder/1/basis_transcoder.wasm`};let c=null,T=null,l=0;const d=async()=>(c||(c=new Promise((t,a)=>{T?t(T):e.LoadFileAsync(e.GetBabylonScriptURL(i.WasmModuleURL)).then(s=>{if("function"!=typeof URL)return a("Basis transcoder requires an environment with a URL constructor");const r=URL.createObjectURL(new Blob([`(${n})()`],{type:"application/javascript"}));T=new Worker(r),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])})}(T,s,i.JSModuleURL).then(t,a)}).catch(a)})),await c),R=async(e,t)=>{const a=e instanceof ArrayBuffer?new Uint8Array(e):e;return await new Promise((e,s)=>{d().then(()=>{const r=l++,n=t=>{"transcode"===t.data.action&&t.data.id===r&&(T.removeEventListener("message",n),t.data.success?e(t.data):s("Transcode is not supported on this device"))};T.addEventListener("message",n);const o=new Uint8Array(a.byteLength);o.set(new Uint8Array(a.buffer,a.byteOffset,a.byteLength)),T.postMessage({action:"transcode",id:r,imageData:o,config:t,ignoreSupportedFormats:false},[o.buffer])},e=>{s(e)})})},_=(e,t)=>{let a=t._gl?.TEXTURE_2D;e.isCube&&(a=t._gl?.TEXTURE_CUBE_MAP),t._bindTextureDirectly(a,e,!0)},p=(e,n)=>{const i=e.getEngine();for(let c=0;c<n.fileInfo.images.length;c++){const T=n.fileInfo.images[c].levels[0];if(e._invertVScale=e.invertY,-1===n.format||n.format===o.cTFRGB565)if(e.type=t.TEXTURETYPE_UNSIGNED_SHORT_5_6_5,e.format=t.TEXTUREFORMAT_RGB,!i._features.basisNeedsPOT||Math.log2(T.width)%1==0&&Math.log2(T.height)%1==0)e._invertVScale=!e.invertY,e.width=T.width+3&-4,e.height=T.height+3&-4,e.samplingMode=t.TEXTURE_LINEAR_LINEAR,_(e,i),i._uploadDataToTextureDirectly(e,new Uint16Array(T.transcodedPixels.buffer),c,0,t.TEXTUREFORMAT_RGB,!0);else{const s=new a(i,2);e._invertVScale=e.invertY,s.type=t.TEXTURETYPE_UNSIGNED_SHORT_5_6_5,s.format=t.TEXTUREFORMAT_RGB,s.width=T.width+3&-4,s.height=T.height+3&-4,_(s,i),i._uploadDataToTextureDirectly(s,new Uint16Array(T.transcodedPixels.buffer),c,0,t.TEXTUREFORMAT_RGB,!0),i._rescaleTexture(s,e,i.scenes[0],i._getInternalFormat(t.TEXTUREFORMAT_RGB),()=>{i._releaseTexture(s),_(e,i)})}else{e.width=T.width,e.height=T.height,e.generateMipMaps=n.fileInfo.images[c].levels.length>1;const t=g.GetInternalFormatFromBasisFormat(n.format,i);e.format=t,_(e,i);const a=n.fileInfo.images[c].levels;for(let s=0;s<a.length;s++){const r=a[s];i._uploadCompressedDataToTextureDirectly(e,t,r.width,r.height,r.transcodedPixels,c,s)}!i._features.basisNeedsPOT||Math.log2(e.width)%1==0&&Math.log2(e.height)%1==0||(s.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."),e._cachedWrapU=r.CLAMP_ADDRESSMODE,e._cachedWrapV=r.CLAMP_ADDRESSMODE)}}},g={JSModuleURL:i.JSModuleURL,WasmModuleURL:i.WasmModuleURL,GetInternalFormatFromBasisFormat:(e,a)=>{let s;switch(e){case o.cTFETC1:s=t.TEXTUREFORMAT_COMPRESSED_RGB_ETC1_WEBGL;break;case o.cTFBC1:s=t.TEXTUREFORMAT_COMPRESSED_RGB_S3TC_DXT1;break;case o.cTFBC4:s=t.TEXTUREFORMAT_COMPRESSED_RGBA_S3TC_DXT5;break;case o.cTFASTC_4x4:s=t.TEXTUREFORMAT_COMPRESSED_RGBA_ASTC_4x4;break;case o.cTFETC2:s=t.TEXTUREFORMAT_COMPRESSED_RGBA8_ETC2_EAC;break;case o.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:R,LoadTextureFromTranscodeResult:p};class u{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}};R(t,i).then(e=>{const t=e.fileInfo.images[0].levels.length>1&&a.generateMipMaps;p(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}};R(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,()=>{p(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{u as _BasisTextureLoader};
//# sourceMappingURL=basisTextureLoader-DbOD3nUX.esm.min.js.map