1import{L as _}from"./fileTools.pure-O3hflNMy.js";import{T as l}from"./tools.pure-PAKPbutq.js";class d{constructor(s,e){if(this.data=s,this.isInvalid=!1,!d.IsValid(s)){this.isInvalid=!0,_.Error("texture missing KTX identifier");return}const i=Uint32Array.BYTES_PER_ELEMENT,a=new DataView(this.data.buffer,this.data.byteOffset+12,13*i),r=a.getUint32(0,!0)===67305985;if(this.glType=a.getUint32(1*i,r),this.glTypeSize=a.getUint32(2*i,r),this.glFormat=a.getUint32(3*i,r),this.glInternalFormat=a.getUint32(4*i,r),this.glBaseInternalFormat=a.getUint32(5*i,r),this.pixelWidth=a.getUint32(6*i,r),this.pixelHeight=a.getUint32(7*i,r),this.pixelDepth=a.getUint32(8*i,r),this.numberOfArrayElements=a.getUint32(9*i,r),this.numberOfFaces=a.getUint32(10*i,r),this.numberOfMipmapLevels=a.getUint32(11*i,r),this.bytesOfKeyValueData=a.getUint32(12*i,r),this.glType!==0){_.Error("only compressed formats currently supported"),this.isInvalid=!0;return}else this.numberOfMipmapLevels=Math.max(1,this.numberOfMipmapLevels);if(this.pixelHeight===0||this.pixelDepth!==0){_.Error("only 2D textures currently supported"),this.isInvalid=!0;return}if(this.numberOfArrayElements!==0){_.Error("texture arrays not currently supported"),this.isInvalid=!0;return}if(this.numberOfFaces!==e){_.Error("number of faces expected"+e+", but found "+this.numberOfFaces),this.isInvalid=!0;return}this.loadType=d.COMPRESSED_2D}uploadLevels(s,e){switch(this.loadType){case d.COMPRESSED_2D:this._upload2DCompressedLevels(s,e);break}}_upload2DCompressedLevels(s,e){let i=d.HEADER_LEN+this.bytesOfKeyValueData,a=this.pixelWidth,o=this.pixelHeight;const r=e?this.numberOfMipmapLevels:1;for(let n=0;n<r;n++){const f=new Int32Array(this.data.buffer,this.data.byteOffset+i,1)[0];i+=4;for(let R=0;R<this.numberOfFaces;R++){const S=new Uint8Array(this.data.buffer,this.data.byteOffset+i,f);s.getEngine()._uploadCompressedDataToTextureDirectly(s,s.format,a,o,S,R,n),i+=f,i+=3-(f+3)%4}a=Math.max(1,a*.5),o=Math.max(1,o*.5)}}static IsValid(s){if(s.byteLength>=12){const e=new Uint8Array(s.buffer,s.byteOffset,12);if(e[0]===171&&e[1]===75&&e[2]===84&&e[3]===88&&e[4]===32&&e[5]===49&&e[6]===49&&e[7]===187&&e[8]===13&&e[9]===10&&e[10]===26&&e[11]===10)return!0}return!1}}d.HEADER_LEN=64;d.COMPRESSED_2D=0;d.COMPRESSED_3D=1;d.TEX_2D=2;d.TEX_3D=3;class D{constructor(s){this._pendingActions=new Array,this._workerInfos=s.map(e=>({workerPromise:Promise.resolve(e),idle:!0}))}dispose(){for(const s of this._workerInfos)s.workerPromise.then(e=>{e.terminate()});this._workerInfos.length=0,this._pendingActions.length=0}push(s){this._executeOnIdleWorker(s)||this._pendingActions.push(s)}_executeOnIdleWorker(s){for(const e of this._workerInfos)if(e.idle)return this._execute(e,s),!0;return!1}_execute(s,e){s.idle=!1,s.workerPromise.then(i=>{e(i,()=>{const a=this._pendingActions.shift();a?this._execute(s,a):s.idle=!0})})}}class T extends D{constructor(s,e,i=T.DefaultOptions){super([]),this._maxWorkers=s,this._createWorkerAsync=e,this._options=i}push(s){if(!this._executeOnIdleWorker(s))if(this._workerInfos.length<this._maxWorkers){const e={workerPromise:this._createWorkerAsync(),idle:!1};this._workerInfos.push(e),this._execute(e,s)}else this._pendingActions.push(s)}_execute(s,e){s.timeoutId&&(clearTimeout(s.timeoutId),delete s.timeoutId),super._execute(s,(i,a)=>{e(i,()=>{a(),s.idle&&(s.timeoutId=setTimeout(()=>{s.workerPromise.then(r=>{r.terminate()});const o=this._workerInfos.indexOf(s);o!==-1&&this._workerInfos.splice(o,1)},this._options.idleTimeElapsedBeforeRelease))})})}}T.DefaultOptions={idleTimeElapsedBeforeRelease:1e3};var u;(function(t){t[t.ETC1S=0]="ETC1S",t[t.UASTC4x4=1]="UASTC4x4",t[t.RGBA32=2]="RGBA32"})(u||(u={}));var p;(function(t){t[t.ASTC_4X4_RGBA=0]="ASTC_4X4_RGBA",t[t.ASTC_4x4_RGBA=0]="ASTC_4x4_RGBA",t[t.BC7_RGBA=1]="BC7_RGBA",t[t.BC3_RGBA=2]="BC3_RGBA",t[t.BC1_RGB=3]="BC1_RGB",t[t.PVRTC1_4_RGBA=4]="PVRTC1_4_RGBA",t[t.PVRTC1_4_RGB=5]="PVRTC1_4_RGB",t[t.ETC2_RGBA=6]="ETC2_RGBA",t[t.ETC1_RGB=7]="ETC1_RGB",t[t.RGBA32=8]="RGBA32",t[t.R8=9]="R8",t[t.RG8=10]="RG8"})(p||(p={}));var B;(function(t){t[t.COMPRESSED_RGBA_BPTC_UNORM_EXT=36492]="COMPRESSED_RGBA_BPTC_UNORM_EXT",t[t.COMPRESSED_RGBA_ASTC_4X4_KHR=37808]="COMPRESSED_RGBA_ASTC_4X4_KHR",t[t.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",t[t.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",t[t.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",t[t.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",t[t.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",t[t.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",t[t.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",t[t.RGBA8Format=32856]="RGBA8Format",t[t.R8Format=33321]="R8Format",t[t.RG8Format=33323]="RG8Format"})(B||(B={}));function A(t,s){const e=s?.jsDecoderModule||KTX2DECODER;t&&(t.wasmBaseUrl&&(e.Transcoder.WasmBaseUrl=t.wasmBaseUrl),t.wasmUASTCToASTC&&(e.LiteTranscoder_UASTC_ASTC.WasmModuleURL=t.wasmUASTCToASTC),t.wasmUASTCToBC7&&(e.LiteTranscoder_UASTC_BC7.WasmModuleURL=t.wasmUASTCToBC7),t.wasmUASTCToRGBA_UNORM&&(e.LiteTranscoder_UASTC_RGBA_UNORM.WasmModuleURL=t.wasmUASTCToRGBA_UNORM),t.wasmUASTCToRGBA_SRGB&&(e.LiteTranscoder_UASTC_RGBA_SRGB.WasmModuleURL=t.wasmUASTCToRGBA_SRGB),t.wasmUASTCToR8_UNORM&&(e.LiteTranscoder_UASTC_R8_UNORM.WasmModuleURL=t.wasmUASTCToR8_UNORM),t.wasmUASTCToRG8_UNORM&&(e.LiteTranscoder_UASTC_RG8_UNORM.WasmModuleURL=t.wasmUASTCToRG8_UNORM),t.jsMSCTranscoder&&(e.MSCTranscoder.JSModuleURL=t.jsMSCTranscoder),t.wasmMSCTranscoder&&(e.MSCTranscoder.WasmModuleURL=t.wasmMSCTranscoder),t.wasmZSTDDecoder&&(e.ZSTDDecoder.WasmModuleURL=t.wasmZSTDDecoder)),s&&(s.wasmUASTCToASTC&&(e.LiteTranscoder_UASTC_ASTC.WasmBinary=s.wasmUASTCToASTC),s.wasmUASTCToBC7&&(e.LiteTranscoder_UASTC_BC7.WasmBinary=s.wasmUASTCToBC7),s.wasmUASTCToRGBA_UNORM&&(e.LiteTranscoder_UASTC_RGBA_UNORM.WasmBinary=s.wasmUASTCToRGBA_UNORM),s.wasmUASTCToRGBA_SRGB&&(e.LiteTranscoder_UASTC_RGBA_SRGB.WasmBinary=s.wasmUASTCToRGBA_SRGB),s.wasmUASTCToR8_UNORM&&(e.LiteTranscoder_UASTC_R8_UNORM.WasmBinary=s.wasmUASTCToR8_UNORM),s.wasmUASTCToRG8_UNORM&&(e.LiteTranscoder_UASTC_RG8_UNORM.WasmBinary=s.wasmUASTCToRG8_UNORM),s.jsMSCTranscoder&&(e.MSCTranscoder.JSModule=s.jsMSCTranscoder),s.wasmMSCTranscoder&&(e.MSCTranscoder.WasmBinary=s.wasmMSCTranscoder),s.wasmZSTDDecoder&&(e.ZSTDDecoder.WasmBinary=s.wasmZSTDDecoder))}function G(t){typeof t>"u"&&typeof KTX2DECODER<"u"&&(t=KTX2DECODER);let s;onmessage=e=>{if(e.data)switch(e.data.action){case"init":{const i=e.data.urls;i&&(i.jsDecoderModule&&typeof t>"u"&&(importScripts(i.jsDecoderModule),t=KTX2DECODER),A(i)),e.data.wasmBinaries&&A(void 0,{...e.data.wasmBinaries,jsDecoderModule:t}),s=new t.KTX2Decoder,postMessage({action:"init"});break}case"setDefaultDecoderOptions":{t.KTX2Decoder.DefaultDecoderOptions=e.data.options;break}case"decode":s.decode(e.data.data,e.data.caps,e.data.options).then(i=>{const a=[];for(let o=0;o<i.mipmaps.length;++o){const r=i.mipmaps[o];r&&r.data&&a.push(r.data.buffer)}postMessage({action:"decoded",success:!0,decodedData:i},a)}).catch(i=>{postMessage({action:"decoded",success:!1,msg:i})});break}}}async function M(t,s,e){return await new Promise((i,a)=>{const o=n=>{t.removeEventListener("error",o),t.removeEventListener("message",r),a(n)},r=n=>{n.data.action==="init"&&(t.removeEventListener("error",o),t.removeEventListener("message",r),i(t))};t.addEventListener("error",o),t.addEventListener("message",r),t.postMessage({action:"init",urls:e,wasmBinaries:s})})}class y{constructor(){this._isDirty=!0,this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC=!0,this._ktx2DecoderOptions={}}get isDirty(){return this._isDirty}get useRGBAIfASTCBC7NotAvailableWhenUASTC(){return this._useRGBAIfASTCBC7NotAvailableWhenUASTC}set useRGBAIfASTCBC7NotAvailableWhenUASTC(s){this._useRGBAIfASTCBC7NotAvailableWhenUASTC!==s&&(this._useRGBAIfASTCBC7NotAvailableWhenUASTC=s,this._isDirty=!0)}get useRGBAIfOnlyBC1BC3AvailableWhenUASTC(){return this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC}set useRGBAIfOnlyBC1BC3AvailableWhenUASTC(s){this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC!==s&&(this._useRGBAIfOnlyBC1BC3AvailableWhenUASTC=s,this._isDirty=!0)}get forceRGBA(){return this._forceRGBA}set forceRGBA(s){this._forceRGBA!==s&&(this._forceRGBA=s,this._isDirty=!0)}get forceR8(){return this._forceR8}set forceR8(s){this._forceR8!==s&&(this._forceR8=s,this._isDirty=!0)}get forceRG8(){return this._forceRG8}set forceRG8(s){this._forceRG8!==s&&(this._forceRG8=s,this._isDirty=!0)}get bypassTranscoders(){return this._bypassTranscoders}set bypassTranscoders(s){this._bypassTranscoders!==s&&(this._bypassTranscoders=s,this._isDirty=!0)}_getKTX2DecoderOptions(){if(!this._isDirty)return this._ktx2DecoderOptions;this._isDirty=!1;const s={};return this._useRGBAIfASTCBC7NotAvailableWhenUASTC!==void 0&&(s.useRGBAIfASTCBC7NotAvailableWhenUASTC=this._useRGBAIfASTCBC7NotAvailableWhenUASTC),this._forceRGBA!==void 0&&(s.forceRGBA=this._forceRGBA),this._forceR8!==void 0&&(s.forceR8=this._forceR8),this._forceRG8!==void 0&&(s.forceRG8=this._forceRG8),this._bypassTranscoders!==void 0&&(s.bypassTranscoders=this._bypassTranscoders),this.useRGBAIfOnlyBC1BC3AvailableWhenUASTC&&(s.transcodeFormatDecisionTree={UASTC:{transcodeFormat:[p.BC1_RGB,p.BC3_RGBA],yes:{transcodeFormat:p.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}),this._ktx2DecoderOptions=s,s}}class c{static GetDefaultNumWorkers(){return typeof navigator!="object"||!navigator.hardwareConcurrency?1:Math.min(Math.floor(navigator.hardwareConcurrency*.5),4)}static _Initialize(s){if(c._WorkerPoolPromise||c._DecoderModulePromise)return;const e={wasmBaseUrl:l.ScriptBaseUrl,jsDecoderModule:l.GetBabylonScriptURL(this.URLConfig.jsDecoderModule,!0),wasmUASTCToASTC:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToASTC,!0),wasmUASTCToBC7:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToBC7,!0),wasmUASTCToRGBA_UNORM:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_UNORM,!0),wasmUASTCToRGBA_SRGB:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_SRGB,!0),wasmUASTCToR8_UNORM:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToR8_UNORM,!0),wasmUASTCToRG8_UNORM:l.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRG8_UNORM,!0),jsMSCTranscoder:l.GetBabylonScriptURL(this.URLConfig.jsMSCTranscoder,!0),wasmMSCTranscoder:l.GetBabylonScriptURL(this.URLConfig.wasmMSCTranscoder,!0),wasmZSTDDecoder:l.GetBabylonScriptURL(this.URLConfig.wasmZSTDDecoder,!0)};s&&typeof Worker=="function"&&typeof URL<"u"?c._WorkerPoolPromise=new Promise(i=>{const a=`${A}(${G})()`,o=URL.createObjectURL(new Blob([a],{type:"application/javascript"}));i(new T(s,async()=>await M(new Worker(o),void 0,e)))}):typeof c._KTX2DecoderModule>"u"?c._DecoderModulePromise=l.LoadBabylonScriptAsync(e.jsDecoderModule).then(()=>(c._KTX2DecoderModule=KTX2DECODER,c._KTX2DecoderModule.MSCTranscoder.UseFromWorkerThread=!1,c._KTX2DecoderModule.WASMMemoryManager.LoadBinariesFromCurrentThread=!0,A(e,c._KTX2DecoderModule),new c._KTX2DecoderModule.KTX2Decoder)):(c._KTX2DecoderModule.MSCTranscoder.UseFromWorkerThread=!1,c._KTX2DecoderModule.WASMMemoryManager.LoadBinariesFromCurrentThread=!0,c._DecoderModulePromise=Promise.resolve(new c._KTX2DecoderModule.KTX2Decoder))}constructor(s,e=c.DefaultNumWorkers){this._engine=s;const i=typeof e=="object"&&e.workerPool||c.WorkerPool;if(i)c._WorkerPoolPromise=Promise.resolve(i);else{typeof e=="object"?c._KTX2DecoderModule=e?.binariesAndModulesContainer?.jsDecoderModule:typeof KTX2DECODER<"u"&&(c._KTX2DecoderModule=KTX2DECODER);const a=typeof e=="number"?e:e.numWorkers??c.DefaultNumWorkers;c._Initialize(a)}}async _uploadAsync(s,e,i){const a=await this._decodeAsync(s,i);this._createTexture(a,e,i)}async _decodeAsync(s,e){const i=this._engine.getCaps(),a={astc:!!i.astc,bptc:!!i.bptc,s3tc:!!i.s3tc,pvrtc:!!i.pvrtc,etc2:!!i.etc2,etc1:!!i.etc1};if(c._WorkerPoolPromise){const o=await c._WorkerPoolPromise;return await new Promise((r,n)=>{o.push((f,R)=>{const S=m=>{f.removeEventListener("error",S),f.removeEventListener("message",h),n(m),R()},h=m=>{m.data.action==="decoded"&&(f.removeEventListener("error",S),f.removeEventListener("message",h),m.data.success?r(m.data.decodedData):n({message:m.data.msg}),R())};f.addEventListener("error",S),f.addEventListener("message",h),f.postMessage({action:"setDefaultDecoderOptions",options:c.DefaultDecoderOptions._getKTX2DecoderOptions()});const C=new Uint8Array(s.byteLength);C.set(new Uint8Array(s.buffer,s.byteOffset,s.byteLength)),f.postMessage({action:"decode",data:C,caps:a,options:e},[C.buffer])})})}else if(c._DecoderModulePromise){const o=await c._DecoderModulePromise;return c.DefaultDecoderOptions.isDirty&&(c._KTX2DecoderModule.KTX2Decoder.DefaultDecoderOptions=c.DefaultDecoderOptions._getKTX2DecoderOptions()),await new Promise((r,n)=>{o.decode(s,i,e).then(f=>{r(f)}).catch(f=>{n({message:f})})})}throw new Error("KTX2 decoder module is not available")}_createTexture(s,e,i){if((s.layerCount??1)>1)throw new Error("KTX2 container - array textures are not supported by this texture loader.");const a=3553;this._engine._bindTextureDirectly(a,e),i&&(i.transcodedFormat=s.transcodedFormat,i.isInGammaSpace=s.isInGammaSpace,i.hasAlpha=s.hasAlpha,i.transcoderName=s.transcoderName);let o=!0;switch(s.transcodedFormat){case 32856:e.type=0,e.format=5;break;case 33321:e.type=0,e.format=6;break;case 33323:e.type=0,e.format=7;break;default:e.format=s.transcodedFormat,o=!1;break}if(e._gammaSpace=s.isInGammaSpace,e.generateMipMaps=s.mipmaps.length>1,e.width=s.mipmaps[0].width,e.height=s.mipmaps[0].height,s.errors)throw new Error("KTX2 container - could not transcode the data. "+s.errors);for(let r=0;r<s.mipmaps.length;++r){const n=s.mipmaps[r];if(!n||!n.data)throw new Error("KTX2 container - could not transcode one of the image");o?(e.width=n.width,e.height=n.height,this._engine._uploadDataToTextureDirectly(e,n.data,0,r,void 0,!0)):this._engine._uploadCompressedDataToTextureDirectly(e,s.transcodedFormat,n.width,n.height,n.data,0,r)}e._extension=".ktx2",e.isReady=!0,this._engine._bindTextureDirectly(a,null)}static IsValid(s){if(s.byteLength>=12){const e=new Uint8Array(s.buffer,s.byteOffset,12);if(e[0]===171&&e[1]===75&&e[2]===84&&e[3]===88&&e[4]===32&&e[5]===50&&e[6]===48&&e[7]===187&&e[8]===13&&e[9]===10&&e[10
1]===26&&e[11]===10)return!0}return!1}}c.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};c.DefaultNumWorkers=c.GetDefaultNumWorkers();c.DefaultDecoderOptions=new y;function U(t){switch(t){case 35916:return 33776;case 35918:return 33778;case 35919:return 33779;case 37493:return 37492;case 37497:return 37496;case 37495:return 37494;case 37840:return 37808;case 37841:return 37809;case 37842:return 37810;case 37843:return 37811;case 37844:return 37812;case 37845:return 37813;case 37846:return 37814;case 37847:return 37815;case 37848:return 37816;case 37849:return 37817;case 37850:return 37818;case 37851:return 37819;case 37852:return 37820;case 37853:return 37821;case 36493:return 36492}return null}class O{constructor(){this.supportCascades=!1}loadCubeData(s,e,i,a){if(Array.isArray(s))return;e._invertVScale=!e.invertY;const o=e.getEngine(),r=new d(s,6),n=U(r.glInternalFormat);n!==null?(e.format=n,e._useSRGBBuffer=o._getUseSRGBBuffer(!0,!e.generateMipMaps),e._gammaSpace=!0):e.format=r.glInternalFormat;const f=r.numberOfMipmapLevels>1&&e.generateMipMaps;o._unpackFlipY(!0),r.uploadLevels(e,e.generateMipMaps),e.width=r.pixelWidth,e.height=r.pixelHeight,o._setCubeMapTextureParams(e,f,r.numberOfMipmapLevels-1),e.isReady=!0,e.onLoadedObservable.notifyObservers(e),e.onLoadedObservable.clear(),a&&a()}loadData(s,e,i,a){if(d.IsValid(s)){e._invertVScale=!e.invertY;const o=new d(s,1),r=U(o.glInternalFormat);r!==null?(e.format=r,e._useSRGBBuffer=e.getEngine()._getUseSRGBBuffer(!0,!e.generateMipMaps),e._gammaSpace=!0):e.format=o.glInternalFormat,i(o.pixelWidth,o.pixelHeight,e.generateMipMaps,!0,()=>{o.uploadLevels(e,e.generateMipMaps)},o.isInvalid)}else c.IsValid(s)?new c(e.getEngine())._uploadAsync(s,e,a).then(()=>{i(e.width,e.height,e.generateMipMaps,!0,()=>{},!1)},r=>{_.Warn(`Failed to load KTX2 texture data: ${r.message}`),i(0,0,!1,!1,()=>{},!0)}):(_.Error("texture missing KTX identifier"),i(0,0,!1,!1,()=>{},!0))}}export{O as _KTXTextureLoader};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.