UNPKG

@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) 15.1 kB
import{L as e,b as t,C as s}from"./index-Bw2N6w1y.esm.min.js";import{A as r}from"./workerPool-BWHiDmEZ.esm.min.js";class a{constructor(t,s){if(this.data=t,this.isInvalid=!1,!a.IsValid(t))return this.isInvalid=!0,void e.Error("texture missing KTX identifier");const r=Uint32Array.BYTES_PER_ELEMENT,o=new DataView(this.data.buffer,this.data.byteOffset+12,13*r),i=67305985===o.getUint32(0,!0);return this.glType=o.getUint32(1*r,i),this.glTypeSize=o.getUint32(2*r,i),this.glFormat=o.getUint32(3*r,i),this.glInternalFormat=o.getUint32(4*r,i),this.glBaseInternalFormat=o.getUint32(5*r,i),this.pixelWidth=o.getUint32(6*r,i),this.pixelHeight=o.getUint32(7*r,i),this.pixelDepth=o.getUint32(8*r,i),this.numberOfArrayElements=o.getUint32(9*r,i),this.numberOfFaces=o.getUint32(10*r,i),this.numberOfMipmapLevels=o.getUint32(11*r,i),this.bytesOfKeyValueData=o.getUint32(12*r,i),0!==this.glType?(e.Error("only compressed formats currently supported"),void(this.isInvalid=!0)):(this.numberOfMipmapLevels=Math.max(1,this.numberOfMipmapLevels),0===this.pixelHeight||0!==this.pixelDepth?(e.Error("only 2D textures currently supported"),void(this.isInvalid=!0)):0!==this.numberOfArrayElements?(e.Error("texture arrays not currently supported"),void(this.isInvalid=!0)):this.numberOfFaces!==s?(e.Error("number of faces expected"+s+", but found "+this.numberOfFaces),void(this.isInvalid=!0)):void(this.loadType=a.COMPRESSED_2D))}uploadLevels(e,t){if(this.loadType===a.COMPRESSED_2D)this._upload2DCompressedLevels(e,t)}_upload2DCompressedLevels(e,t){let s=a.HEADER_LEN+this.bytesOfKeyValueData,r=this.pixelWidth,o=this.pixelHeight;const i=t?this.numberOfMipmapLevels:1;for(let t=0;t<i;t++){const a=new Int32Array(this.data.buffer,this.data.byteOffset+s,1)[0];s+=4;for(let i=0;i<this.numberOfFaces;i++){const n=new Uint8Array(this.data.buffer,this.data.byteOffset+s,a);e.getEngine()._uploadCompressedDataToTextureDirectly(e,e.format,r,o,n,i,t),s+=a,s+=3-(a+3)%4}r=Math.max(1,.5*r),o=Math.max(1,.5*o)}}static IsValid(e){if(e.byteLength>=12){const t=new Uint8Array(e.buffer,e.byteOffset,12);if(171===t[0]&&75===t[1]&&84===t[2]&&88===t[3]&&32===t[4]&&49===t[5]&&49===t[6]&&187===t[7]&&13===t[8]&&10===t[9]&&26===t[10]&&10===t[11])return!0}return!1}}var o,i,n;function T(e,t){const s=t?.jsDecoderModule||KTX2DECODER;e&&(e.wasmUASTCToASTC&&(s.LiteTranscoder_UASTC_ASTC.WasmModuleURL=e.wasmUASTCToASTC),e.wasmUASTCToBC7&&(s.LiteTranscoder_UASTC_BC7.WasmModuleURL=e.wasmUASTCToBC7),e.wasmUASTCToRGBA_UNORM&&(s.LiteTranscoder_UASTC_RGBA_UNORM.WasmModuleURL=e.wasmUASTCToRGBA_UNORM),e.wasmUASTCToRGBA_SRGB&&(s.LiteTranscoder_UASTC_RGBA_SRGB.WasmModuleURL=e.wasmUASTCToRGBA_SRGB),e.wasmUASTCToR8_UNORM&&(s.LiteTranscoder_UASTC_R8_UNORM.WasmModuleURL=e.wasmUASTCToR8_UNORM),e.wasmUASTCToRG8_UNORM&&(s.LiteTranscoder_UASTC_RG8_UNORM.WasmModuleURL=e.wasmUASTCToRG8_UNORM),e.jsMSCTranscoder&&(s.MSCTranscoder.JSModuleURL=e.jsMSCTranscoder),e.wasmMSCTranscoder&&(s.MSCTranscoder.WasmModuleURL=e.wasmMSCTranscoder),e.wasmZSTDDecoder&&(s.ZSTDDecoder.WasmModuleURL=e.wasmZSTDDecoder)),t&&(t.wasmUASTCToASTC&&(s.LiteTranscoder_UASTC_ASTC.WasmBinary=t.wasmUASTCToASTC),t.wasmUASTCToBC7&&(s.LiteTranscoder_UASTC_BC7.WasmBinary=t.wasmUASTCToBC7),t.wasmUASTCToRGBA_UNORM&&(s.LiteTranscoder_UASTC_RGBA_UNORM.WasmBinary=t.wasmUASTCToRGBA_UNORM),t.wasmUASTCToRGBA_SRGB&&(s.LiteTranscoder_UASTC_RGBA_SRGB.WasmBinary=t.wasmUASTCToRGBA_SRGB),t.wasmUASTCToR8_UNORM&&(s.LiteTranscoder_UASTC_R8_UNORM.WasmBinary=t.wasmUASTCToR8_UNORM),t.wasmUASTCToRG8_UNORM&&(s.LiteTranscoder_UASTC_RG8_UNORM.WasmBinary=t.wasmUASTCToRG8_UNORM),t.jsMSCTranscoder&&(s.MSCTranscoder.JSModule=t.jsMSCTranscoder),t.wasmMSCTranscoder&&(s.MSCTranscoder.WasmBinary=t.wasmMSCTranscoder),t.wasmZSTDDecoder&&(s.ZSTDDecoder.WasmBinary=t.wasmZSTDDecoder))}function _(e){let t;void 0===e&&"undefined"!=typeof KTX2DECODER&&(e=KTX2DECODER),onmessage=s=>{if(s.data)switch(s.data.action){case"init":{const r=s.data.urls;r&&(r.jsDecoderModule&&void 0===e&&(importScripts(r.jsDecoderModule),e=KTX2DECODER),T(r)),s.data.wasmBinaries&&T(void 0,{...s.data.wasmBinaries,jsDecoderModule:e}),t=new e.KTX2Decoder,postMessage({action:"init"});break}case"setDefaultDecoderOptions":e.KTX2Decoder.DefaultDecoderOptions=s.data.options;break;case"decode":t.decode(s.data.data,s.data.caps,s.data.options).then((e=>{const t=[];for(let s=0;s<e.mipmaps.length;++s){const r=e.mipmaps[s];r&&r.data&&t.push(r.data.buffer)}postMessage({action:"decoded",success:!0,decodedData:e},t)})).catch((e=>{postMessage({action:"decoded",success:!1,msg:e})}))}}}a.HEADER_LEN=64,a.COMPRESSED_2D=0,a.COMPRESSED_3D=1,a.TEX_2D=2,a.TEX_3D=3,function(e){e[e.ETC1S=0]="ETC1S",e[e.UASTC4x4=1]="UASTC4x4"}(o||(o={})),function(e){e[e.ASTC_4X4_RGBA=0]="ASTC_4X4_RGBA",e[e.BC7_RGBA=1]="BC7_RGBA",e[e.BC3_RGBA=2]="BC3_RGBA",e[e.BC1_RGB=3]="BC1_RGB",e[e.PVRTC1_4_RGBA=4]="PVRTC1_4_RGBA",e[e.PVRTC1_4_RGB=5]="PVRTC1_4_RGB",e[e.ETC2_RGBA=6]="ETC2_RGBA",e[e.ETC1_RGB=7]="ETC1_RGB",e[e.RGBA32=8]="RGBA32",e[e.R8=9]="R8",e[e.RG8=10]="RG8"}(i||(i={})),function(e){e[e.COMPRESSED_RGBA_BPTC_UNORM_EXT=36492]="COMPRESSED_RGBA_BPTC_UNORM_EXT",e[e.COMPRESSED_RGBA_ASTC_4X4_KHR=37808]="COMPRESSED_RGBA_ASTC_4X4_KHR",e[e.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",e[e.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",e[e.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",e[e.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",e[e.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",e[e.RGBA8Format=32856]="RGBA8Format",e[e.R8Format=33321]="R8Format",e[e.RG8Format=33323]="RG8Format"}(n||(n={}));class R{static GetDefaultNumWorkers(){return"object"==typeof navigator&&navigator.hardwareConcurrency?Math.min(Math.floor(.5*navigator.hardwareConcurrency),4):1}static _Initialize(e){if(R._WorkerPoolPromise||R._DecoderModulePromise)return;const s={jsDecoderModule:t.GetBabylonScriptURL(this.URLConfig.jsDecoderModule,!0),wasmUASTCToASTC:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToASTC,!0),wasmUASTCToBC7:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToBC7,!0),wasmUASTCToRGBA_UNORM:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_UNORM,!0),wasmUASTCToRGBA_SRGB:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_SRGB,!0),wasmUASTCToR8_UNORM:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToR8_UNORM,!0),wasmUASTCToRG8_UNORM:t.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRG8_UNORM,!0),jsMSCTranscoder:t.GetBabylonScriptURL(this.URLConfig.jsMSCTranscoder,!0),wasmMSCTranscoder:t.GetBabylonScriptURL(this.URLConfig.wasmMSCTranscoder,!0),wasmZSTDDecoder:t.GetBabylonScriptURL(this.URLConfig.wasmZSTDDecoder,!0)};e&&"function"==typeof Worker&&"undefined"!=typeof URL?R._WorkerPoolPromise=new Promise((t=>{const a=`${T}(${_})()`,o=URL.createObjectURL(new Blob([a],{type:"application/javascript"}));t(new r(e,(async()=>await async function(e,t,s){return await new Promise(((r,a)=>{const o=t=>{e.removeEventListener("error",o),e.removeEventListener("message",i),a(t)},i=t=>{"init"===t.data.action&&(e.removeEventListener("error",o),e.removeEventListener("message",i),r(e))};e.addEventListener("error",o),e.addEventListener("message",i),e.postMessage({action:"init",urls:s,wasmBinaries:t})}))}(new Worker(o),void 0,s))))})):void 0===R._KTX2DecoderModule?R._DecoderModulePromise=t.LoadBabylonScriptAsync(s.jsDecoderModule).then((()=>(R._KTX2DecoderModule=KTX2DECODER,R._KTX2DecoderModule.MSCTranscoder.UseFromWorkerThread=!1,R._KTX2DecoderModule.WASMMemoryManager.LoadBinariesFromCurrentThread=!0,T(s,R._KTX2DecoderModule),new R._KTX2DecoderModule.KTX2Decoder))):(R._KTX2DecoderModule.MSCTranscoder.UseFromWorkerThread=!1,R._KTX2DecoderModule.WASMMemoryManager.LoadBinariesFromCurrentThread=!0,R._DecoderModulePromise=Promise.resolve(new R._KTX2DecoderModule.KTX2Decoder))}constructor(e,t=R.DefaultNumWorkers){this._engine=e;const s="object"==typeof t&&t.workerPool||R.WorkerPool;if(s)R._WorkerPoolPromise=Promise.resolve(s);else{"object"==typeof t?R._KTX2DecoderModule=t?.binariesAndModulesContainer?.jsDecoderModule:"undefined"!=typeof KTX2DECODER&&(R._KTX2DecoderModule=KTX2DECODER);const e="number"==typeof t?t:t.numWorkers??R.DefaultNumWorkers;R._Initialize(e)}}async _uploadAsync(e,t,s){const r=this._engine.getCaps(),a={astc:!!r.astc,bptc:!!r.bptc,s3tc:!!r.s3tc,pvrtc:!!r.pvrtc,etc2:!!r.etc2,etc1:!!r.etc1};if(R._WorkerPoolPromise){const r=await R._WorkerPoolPromise;return await new Promise(((o,i)=>{r.push(((r,n)=>{const T=e=>{r.removeEventListener("error",T),r.removeEventListener("message",_),i(e),n()},_=e=>{if("decoded"===e.data.action){if(r.removeEventListener("error",T),r.removeEventListener("message",_),e.data.success)try{this._createTexture(e.data.decodedData,t,s),o()}catch(e){i({message:e})}else i({message:e.data.msg});n()}};r.addEventListener("error",T),r.addEventListener("message",_),r.postMessage({action:"setDefaultDecoderOptions",options:R.DefaultDecoderOptions._getKTX2DecoderOptions()});const d=new Uint8Array(e.byteLength);d.set(new Uint8Array(e.buffer,e.byteOffset,e.byteLength)),r.postMessage({action:"decode",data:d,caps:a,options:s},[d.buffer])}))}))}if(R._DecoderModulePromise){const s=await R._DecoderModulePromise;return R.DefaultDecoderOptions.isDirty&&(R._KTX2DecoderModule.KTX2Decoder.DefaultDecoderOptions=R.DefaultDecoderOptions._getKTX2DecoderOptions()),await new Promise(((a,o)=>{s.decode(e,r).then((e=>{this._createTexture(e,t),a()})).catch((e=>{o({message:e})}))}))}throw new Error("KTX2 decoder module is not available")}_createTexture(e,t,r){this._engine._bindTextureDirectly(3553,t),r&&(r.transcodedFormat=e.transcodedFormat,r.isInGammaSpace=e.isInGammaSpace,r.hasAlpha=e.hasAlpha,r.transcoderName=e.transcoderName);let a=!0;switch(e.transcodedFormat){case 32856:t.type=s.TEXTURETYPE_UNSIGNED_BYTE,t.format=s.TEXTUREFORMAT_RGBA;break;case 33321:t.type=s.TEXTURETYPE_UNSIGNED_BYTE,t.format=s.TEXTUREFORMAT_R;break;case 33323:t.type=s.TEXTURETYPE_UNSIGNED_BYTE,t.format=s.TEXTUREFORMAT_RG;break;default:t.format=e.transcodedFormat,a=!1}if(t._gammaSpace=e.isInGammaSpace,t.generateMipMaps=e.mipmaps.length>1,e.errors)throw new Error("KTX2 container - could not transcode the data. "+e.errors);for(let s=0;s<e.mipmaps.length;++s){const r=e.mipmaps[s];if(!r||!r.data)throw new Error("KTX2 container - could not transcode one of the image");a?(t.width=r.width,t.height=r.height,this._engine._uploadDataToTextureDirectly(t,r.data,0,s,void 0,!0)):this._engine._uploadCompressedDataToTextureDirectly(t,e.transcodedFormat,r.width,r.height,r.data,0,s)}t._extension=".ktx2",t.width=e.mipmaps[0].width,t.height=e.mipmaps[0].height,t.isReady=!0,this._engine._bindTextureDirectly(3553,null)}static IsValid(e){if(e.byteLength>=12){const t=new Uint8Array(e.buffer,e.byteOffset,12);if(171===t[0]&&75===t[1]&&84===t[2]&&88===t[3]&&32===t[4]&&50===t[5]&&48===t[6]&&187===t[7]&&13===t[8]&&10===t[9]&&26===t[10]&&10===t[11])return!0}return!1}}R.URLConfig={jsDecoderModule:"https://cdn.babylonjs.com/babylon.ktx2Decoder.js",wasmUASTCToASTC:null,wasmUASTCToBC7:null,wasmUASTCToRGBA_UNORM:null,wasmUASTCToRGBA_SRGB:null,wasmUASTCToR8_UNORM:null,wasmUASTCToRG8_UNORM:null,jsMSCTranscoder:null,wasmMSCTranscoder:null,wasmZSTDDecoder:null},R.DefaultNumWorkers=R.GetDefaultNumWorkers(),R.DefaultDecoderOptions=new class{constructor(){this._isDirty=!0,this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC=!0,this._ktx2DecoderOptions={}}get isDirty(){return this._isDirty}get useRGBAIfASTCBC7NotAvailableWhenUASTC(){return this._useRGBAIfASTCBC7NotAvailableWhenUASTC}set useRGBAIfASTCBC7NotAvailableWhenUASTC(e){this._useRGBAIfASTCBC7NotAvailableWhenUASTC!==e&&(this._useRGBAIfASTCBC7NotAvailableWhenUASTC=e,this._isDirty=!0)}get useRGBAIfOnlyBC1BC3AvailableWhenUASTC(){return this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC}set useRGBAIfOnlyBC1BC3AvailableWhenUASTC(e){this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC!==e&&(this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC=e,this._isDirty=!0)}get forceRGBA(){return this._forceRGBA}set forceRGBA(e){this._forceRGBA!==e&&(this._forceRGBA=e,this._isDirty=!0)}get forceR8(){return this._forceR8}set forceR8(e){this._forceR8!==e&&(this._forceR8=e,this._isDirty=!0)}get forceRG8(){return this._forceRG8}set forceRG8(e){this._forceRG8!==e&&(this._forceRG8=e,this._isDirty=!0)}get bypassTranscoders(){return this._bypassTranscoders}set bypassTranscoders(e){this._bypassTranscoders!==e&&(this._bypassTranscoders=e,this._isDirty=!0)}_getKTX2DecoderOptions(){if(!this._isDirty)return this._ktx2DecoderOptions;this._isDirty=!1;const e={useRGBAIfASTCBC7NotAvailableWhenUASTC:this._useRGBAIfASTCBC7NotAvailableWhenUASTC,forceRGBA:this._forceRGBA,forceR8:this._forceR8,forceRG8:this._forceRG8,bypassTranscoders:this._bypassTranscoders};return this.useRGBAIfOnlyBC1BC3AvailableWhenUASTC&&(e.transcodeFormatDecisionTree={UASTC:{transcodeFormat:[i.BC1_RGB,i.BC3_RGBA],yes:{transcodeFormat:i.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}),this._ktx2DecoderOptions=e,e}};class d{constructor(){this.supportCascades=!1}loadCubeData(e,t,s,r){if(Array.isArray(e))return;t._invertVScale=!t.invertY;const o=t.getEngine(),i=new a(e,6),n=i.numberOfMipmapLevels>1&&t.generateMipMaps;o._unpackFlipY(!0),i.uploadLevels(t,t.generateMipMaps),t.width=i.pixelWidth,t.height=i.pixelHeight,o._setCubeMapTextureParams(t,n,i.numberOfMipmapLevels-1),t.isReady=!0,t.onLoadedObservable.notifyObservers(t),t.onLoadedObservable.clear(),r&&r()}loadData(t,r,o,i){if(a.IsValid(t)){r._invertVScale=!r.invertY;const e=new a(t,1),i=function(e){switch(e){case s.TEXTUREFORMAT_COMPRESSED_SRGB_S3TC_DXT1_EXT:return s.TEXTUREFORMAT_COMPRESSED_RGB_S3TC_DXT1;case s.TEXTUREFORMAT_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:return s.TEXTUREFORMAT_COMPRESSED_RGBA_S3TC_DXT3;case s.TEXTUREFORMAT_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:return s.TEXTUREFORMAT_COMPRESSED_RGBA_S3TC_DXT5;case s.TEXTUREFORMAT_COMPRESSED_SRGB8_ETC2:return s.TEXTUREFORMAT_COMPRESSED_RGB8_ETC2;case s.TEXTUREFORMAT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:return s.TEXTUREFORMAT_COMPRESSED_RGBA8_ETC2_EAC;case s.TEXTUREFORMAT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:return s.TEXTUREFORMAT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;case s.TEXTUREFORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:return s.TEXTUREFORMAT_COMPRESSED_RGBA_ASTC_4x4;case s.TEXTUREFORMAT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:return s.TEXTUREFORMAT_COMPRESSED_RGBA_BPTC_UNORM}return null}(e.glInternalFormat);i?(r.format=i,r._useSRGBBuffer=r.getEngine()._getUseSRGBBuffer(!0,r.generateMipMaps),r._gammaSpace=!0):r.format=e.glInternalFormat,o(e.pixelWidth,e.pixelHeight,r.generateMipMaps,!0,(()=>{e.uploadLevels(r,r.generateMipMaps)}),e.isInvalid)}else if(R.IsValid(t)){new R(r.getEngine())._uploadAsync(t,r,i).then((()=>{o(r.width,r.height,r.generateMipMaps,!0,(()=>{}),!1)}),(t=>{e.Warn(`Failed to load KTX2 texture data: ${t.message}`),o(0,0,!1,!1,(()=>{}),!0)}))}else e.Error("texture missing KTX identifier"),o(0,0,!1,!1,(()=>{}),!0)}}export{d as _KTXTextureLoader}; //# sourceMappingURL=ktxTextureLoader-B7DT5Tim.esm.min.js.map