three-stdlib 2.21.12 → 2.22.0
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/index.cjs.js +1 -1
- package/loaders/KTX2Loader.cjs.js +1 -1
- package/loaders/KTX2Loader.js +56 -16
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three"),r=require("../utils/WorkerPool.cjs.js"),t=require("ktx-parse"),o=require("zstddec");const s=new WeakMap;let a,n=0;class i extends e.Loader{constructor(e){super(e),this.transcoderPath="",this.transcoderBinary=null,this.transcoderPending=null,this.workerPool=new r.WorkerPool,this.workerSourceURL="",this.workerConfig=null,"undefined"!=typeof MSC_TRANSCODER&&console.warn('THREE.KTX2Loader: Please update to latest "basis_transcoder". "msc_basis_transcoder" is no longer supported in three.js r125+.')}setTranscoderPath(e){return this.transcoderPath=e,this}setWorkerLimit(e){return this.workerPool.setWorkerLimit(e),this}detectSupport(e){return this.workerConfig={astcSupported:e.extensions.has("WEBGL_compressed_texture_astc"),etc1Supported:e.extensions.has("WEBGL_compressed_texture_etc1"),etc2Supported:e.extensions.has("WEBGL_compressed_texture_etc"),dxtSupported:e.extensions.has("WEBGL_compressed_texture_s3tc"),bptcSupported:e.extensions.has("EXT_texture_compression_bptc"),pvrtcSupported:e.extensions.has("WEBGL_compressed_texture_pvrtc")||e.extensions.has("WEBKIT_WEBGL_compressed_texture_pvrtc")},e.capabilities.isWebGL2&&(this.workerConfig.etc1Supported=!1),this}init(){if(!this.transcoderPending){const r=new e.FileLoader(this.manager);r.setPath(this.transcoderPath),r.setWithCredentials(this.withCredentials);const t=r.loadAsync("basis_transcoder.js"),o=new e.FileLoader(this.manager);o.setPath(this.transcoderPath),o.setResponseType("arraybuffer"),o.setWithCredentials(this.withCredentials);const s=o.loadAsync("basis_transcoder.wasm");this.transcoderPending=Promise.all([t,s]).then((([e,r])=>{const t=i.BasisWorker.toString(),o=["/* constants */","let _EngineFormat = "+JSON.stringify(i.EngineFormat),"let _TranscoderFormat = "+JSON.stringify(i.TranscoderFormat),"let _BasisFormat = "+JSON.stringify(i.BasisFormat),"/* basis_transcoder.js */",e,"/* worker */",t.substring(t.indexOf("{")+1,t.lastIndexOf("}"))].join("\n");this.workerSourceURL=URL.createObjectURL(new Blob([o])),this.transcoderBinary=r,this.workerPool.setWorkerCreator((()=>{const e=new Worker(this.workerSourceURL),r=this.transcoderBinary.slice(0);return e.postMessage({type:"init",config:this.workerConfig,transcoderBinary:r},[r]),e}))})),n>0&&console.warn("THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues. Use a single KTX2Loader instance, or call .dispose() on old instances."),n++}return this.transcoderPending}load(r,t,o,a){if(null===this.workerConfig)throw new Error("THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.");const n=new e.FileLoader(this.manager);n.setResponseType("arraybuffer"),n.setWithCredentials(this.withCredentials),n.load(r,(e=>{if(s.has(e)){return s.get(e).promise.then(t).catch(a)}this._createTexture(e).then((e=>t?t(e):null)).catch(a)}),o,a)}_createTextureFrom(r){const{mipmaps:o,width:s,height:a,format:n,type:i,error:_,dfdTransferFn:T,dfdFlags:d}=r;if("error"===i)return Promise.reject(_);const R=new e.CompressedTexture(o,s,a,n,e.UnsignedByteType);return R.minFilter=1===o.length?e.LinearFilter:e.LinearMipmapLinearFilter,R.magFilter=e.LinearFilter,R.generateMipmaps=!1,R.needsUpdate=!0,R.encoding=T===t.KHR_DF_TRANSFER_SRGB?e.sRGBEncoding:e.LinearEncoding,R.premultiplyAlpha=!!(d&t.KHR_DF_FLAG_ALPHA_PREMULTIPLIED),R}_createTexture(r,n={}){const i=t.read(new Uint8Array(r));if(i.vkFormat!==t.VK_FORMAT_UNDEFINED)return async function(r){const{vkFormat:s,pixelWidth:n,pixelHeight:i,pixelDepth:R}=r;if(void 0===_[s])throw new Error("THREE.KTX2Loader: Unsupported vkFormat.");const c=r.levels[0];let F,p;if(r.supercompressionScheme===t.KHR_SUPERCOMPRESSION_NONE)F=c.levelData;else{if(r.supercompressionScheme!==t.KHR_SUPERCOMPRESSION_ZSTD)throw new Error("THREE.KTX2Loader: Unsupported supercompressionScheme.");a||(a=new Promise((async e=>{const r=new o.ZSTDDecoder;await r.init(),e(r)}))),F=(await a).decode(c.levelData,c.uncompressedByteLength)}p=T[s]===e.FloatType?new Float32Array(F.buffer,F.byteOffset,F.byteLength/Float32Array.BYTES_PER_ELEMENT):T[s]===e.HalfFloatType?new Uint16Array(F.buffer,F.byteOffset,F.byteLength/Uint16Array.BYTES_PER_ELEMENT):F;const m=0===R?new e.DataTexture(p,n,i):new e.Data3DTexture(p,n,i,R);return m.type=T[s],m.format=_[s],m.encoding=d[s]||e.LinearEncoding,m.needsUpdate=!0,Promise.resolve(m)}(i);const R=n,c=this.init().then((()=>this.workerPool.postMessage({type:"transcode",buffer:r,taskConfig:R},[r]))).then((e=>this._createTextureFrom(e.data)));return s.set(r,{promise:c}),c}dispose(){return this.workerPool.dispose(),this.workerSourceURL&&URL.revokeObjectURL(this.workerSourceURL),n--,this}}i.BasisFormat={ETC1S:0,UASTC_4x4:1},i.TranscoderFormat={ETC1:0,ETC2:1,BC1:2,BC3:3,BC4:4,BC5:5,BC7_M6_OPAQUE_ONLY:6,BC7_M5:7,PVRTC1_4_RGB:8,PVRTC1_4_RGBA:9,ASTC_4x4:10,ATC_RGB:11,ATC_RGBA_INTERPOLATED_ALPHA:12,RGBA32:13,RGB565:14,BGR565:15,RGBA4444:16},i.EngineFormat={RGBAFormat:e.RGBAFormat,RGBA_ASTC_4x4_Format:e.RGBA_ASTC_4x4_Format,RGBA_BPTC_Format:e.RGBA_BPTC_Format,RGBA_ETC2_EAC_Format:e.RGBA_ETC2_EAC_Format,RGBA_PVRTC_4BPPV1_Format:e.RGBA_PVRTC_4BPPV1_Format,RGBA_S3TC_DXT5_Format:e.RGBA_S3TC_DXT5_Format,RGB_ETC1_Format:e.RGB_ETC1_Format,RGB_ETC2_Format:e.RGB_ETC2_Format,RGB_PVRTC_4BPPV1_Format:e.RGB_PVRTC_4BPPV1_Format,RGB_S3TC_DXT1_Format:e.RGB_S3TC_DXT1_Format},i.BasisWorker=function(){let e,r,t;const o=_EngineFormat,s=_TranscoderFormat,a=_BasisFormat;self.addEventListener("message",(function(n){const d=n.data;switch(d.type){case"init":e=d.config,R=d.transcoderBinary,r=new Promise((e=>{t={wasmBinary:R,onRuntimeInitialized:e},BASIS(t)})).then((()=>{t.initializeBasis(),void 0===t.KTX2File&&console.warn("THREE.KTX2Loader: Please update Basis Universal transcoder.")}));break;case"transcode":r.then((()=>{try{const{width:r,height:n,hasAlpha:R,mipmaps:c,format:F,dfdTransferFn:p,dfdFlags:m}=function(r){const n=new t.KTX2File(new Uint8Array(r));function d(){n.close(),n.delete()}if(!n.isValid())throw d(),new Error("THREE.KTX2Loader:\tInvalid or unsupported .ktx2 file");const R=n.isUASTC()?a.UASTC_4x4:a.ETC1S,c=n.getWidth(),F=n.getHeight(),p=n.getLevels(),m=n.getHasAlpha(),l=n.getDFDTransferFunc(),A=n.getDFDFlags(),{transcoderFormat:B,engineFormat:h}=function(r,t,n,d){let R,c;const F=r===a.ETC1S?i:_;for(let o=0;o<F.length;o++){const s=F[o];if(e[s.if]&&(s.basisFormat.includes(r)&&!(d&&s.transcoderFormat.length<2)&&(!s.needsPowerOfTwo||T(t)&&T(n))))return R=s.transcoderFormat[d?1:0],c=s.engineFormat[d?1:0],{transcoderFormat:R,engineFormat:c}}return console.warn("THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32."),R=s.RGBA32,c=o.RGBAFormat,{transcoderFormat:R,engineFormat:c}}(R,c,F,m);if(!c||!F||!p)throw d(),new Error("THREE.KTX2Loader:\tInvalid texture");if(!n.startTranscoding())throw d(),new Error("THREE.KTX2Loader: .startTranscoding failed");const S=[];for(let e=0;e<p;e++){const r=n.getImageLevelInfo(e,0,0),t=r.origWidth,o=r.origHeight,s=new Uint8Array(n.getImageTranscodedSizeInBytes(e,0,0,B));if(!n.transcodeImage(s,e,0,0,B,0,-1,-1))throw d(),new Error("THREE.KTX2Loader: .transcodeImage failed.");S.push({data:s,width:t,height:o})}return d(),{width:c,height:F,hasAlpha:m,mipmaps:S,format:h,dfdTransferFn:l,dfdFlags:A}}(d.buffer),l=[];for(let e=0;e<c.length;++e)l.push(c[e].data.buffer);self.postMessage({type:"transcode",id:d.id,width:r,height:n,hasAlpha:R,mipmaps:c,format:F,dfdTransferFn:p,dfdFlags:m},l)}catch(e){console.error(e),self.postMessage({type:"error",id:d.id,error:e.message})}}))}var R}));const n=[{if:"astcSupported",basisFormat:[a.UASTC_4x4],transcoderFormat:[s.ASTC_4x4,s.ASTC_4x4],engineFormat:[o.RGBA_ASTC_4x4_Format,o.RGBA_ASTC_4x4_Format],priorityETC1S:1/0,priorityUASTC:1,needsPowerOfTwo:!1},{if:"bptcSupported",basisFormat:[a.ETC1S,a.UASTC_4x4],transcoderFormat:[s.BC7_M5,s.BC7_M5],engineFormat:[o.RGBA_BPTC_Format,o.RGBA_BPTC_Format],priorityETC1S:3,priorityUASTC:2,needsPowerOfTwo:!1},{if:"dxtSupported",basisFormat:[a.ETC1S,a.UASTC_4x4],transcoderFormat:[s.BC1,s.BC3],engineFormat:[o.RGB_S3TC_DXT1_Format,o.RGBA_S3TC_DXT5_Format],priorityETC1S:4,priorityUASTC:5,needsPowerOfTwo:!1},{if:"etc2Supported",basisFormat:[a.ETC1S,a.UASTC_4x4],transcoderFormat:[s.ETC1,s.ETC2],engineFormat:[o.RGB_ETC2_Format,o.RGBA_ETC2_EAC_Format],priorityETC1S:1,priorityUASTC:3,needsPowerOfTwo:!1},{if:"etc1Supported",basisFormat:[a.ETC1S,a.UASTC_4x4],transcoderFormat:[s.ETC1],engineFormat:[o.RGB_ETC1_Format],priorityETC1S:2,priorityUASTC:4,needsPowerOfTwo:!1},{if:"pvrtcSupported",basisFormat:[a.ETC1S,a.UASTC_4x4],transcoderFormat:[s.PVRTC1_4_RGB,s.PVRTC1_4_RGBA],engineFormat:[o.RGB_PVRTC_4BPPV1_Format,o.RGBA_PVRTC_4BPPV1_Format],priorityETC1S:5,priorityUASTC:6,needsPowerOfTwo:!0}],i=n.sort((function(e,r){return e.priorityETC1S-r.priorityETC1S})),_=n.sort((function(e,r){return e.priorityUASTC-r.priorityUASTC}));function T(e){return e<=2||0==(e&e-1)&&0!==e}};const _={[t.VK_FORMAT_R32G32B32A32_SFLOAT]:e.RGBAFormat,[t.VK_FORMAT_R16G16B16A16_SFLOAT]:e.RGBAFormat,[t.VK_FORMAT_R8G8B8A8_UNORM]:e.RGBAFormat,[t.VK_FORMAT_R8G8B8A8_SRGB]:e.RGBAFormat,[t.VK_FORMAT_R32G32_SFLOAT]:e.RGFormat,[t.VK_FORMAT_R16G16_SFLOAT]:e.RGFormat,[t.VK_FORMAT_R8G8_UNORM]:e.RGFormat,[t.VK_FORMAT_R8G8_SRGB]:e.RGFormat,[t.VK_FORMAT_R32_SFLOAT]:e.RedFormat,[t.VK_FORMAT_R16_SFLOAT]:e.RedFormat,[t.VK_FORMAT_R8_SRGB]:e.RedFormat,[t.VK_FORMAT_R8_UNORM]:e.RedFormat},T={[t.VK_FORMAT_R32G32B32A32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16G16B16A16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8G8B8A8_UNORM]:e.UnsignedByteType,[t.VK_FORMAT_R8G8B8A8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R32G32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16G16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8G8_UNORM]:e.UnsignedByteType,[t.VK_FORMAT_R8G8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R8_UNORM]:e.UnsignedByteType},d={[t.VK_FORMAT_R8G8B8A8_SRGB]:e.sRGBEncoding,[t.VK_FORMAT_R8G8_SRGB]:e.sRGBEncoding,[t.VK_FORMAT_R8_SRGB]:e.sRGBEncoding};exports.KTX2Loader=i;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three"),r=require("../utils/WorkerPool.cjs.js"),t=require("ktx-parse"),o=require("zstddec");class s extends e.CompressedTexture{constructor(r,t,o,s,n,a){super(r,t,o,n,a),this.isCompressedArrayTexture=!0,this.image.depth=s,this.wrapR=e.ClampToEdgeWrapping}}const n=new WeakMap;let a,i=0;class _ extends e.Loader{constructor(e){super(e),this.transcoderPath="",this.transcoderBinary=null,this.transcoderPending=null,this.workerPool=new r.WorkerPool,this.workerSourceURL="",this.workerConfig=null,"undefined"!=typeof MSC_TRANSCODER&&console.warn('THREE.KTX2Loader: Please update to latest "basis_transcoder". "msc_basis_transcoder" is no longer supported in three.js r125+.')}setTranscoderPath(e){return this.transcoderPath=e,this}setWorkerLimit(e){return this.workerPool.setWorkerLimit(e),this}detectSupport(e){return this.workerConfig={astcSupported:e.extensions.has("WEBGL_compressed_texture_astc"),etc1Supported:e.extensions.has("WEBGL_compressed_texture_etc1"),etc2Supported:e.extensions.has("WEBGL_compressed_texture_etc"),dxtSupported:e.extensions.has("WEBGL_compressed_texture_s3tc"),bptcSupported:e.extensions.has("EXT_texture_compression_bptc"),pvrtcSupported:e.extensions.has("WEBGL_compressed_texture_pvrtc")||e.extensions.has("WEBKIT_WEBGL_compressed_texture_pvrtc")},e.capabilities.isWebGL2&&(this.workerConfig.etc1Supported=!1),this}init(){if(!this.transcoderPending){const r=new e.FileLoader(this.manager);r.setPath(this.transcoderPath),r.setWithCredentials(this.withCredentials);const t=r.loadAsync("basis_transcoder.js"),o=new e.FileLoader(this.manager);o.setPath(this.transcoderPath),o.setResponseType("arraybuffer"),o.setWithCredentials(this.withCredentials);const s=o.loadAsync("basis_transcoder.wasm");this.transcoderPending=Promise.all([t,s]).then((([e,r])=>{const t=_.BasisWorker.toString(),o=["/* constants */","let _EngineFormat = "+JSON.stringify(_.EngineFormat),"let _TranscoderFormat = "+JSON.stringify(_.TranscoderFormat),"let _BasisFormat = "+JSON.stringify(_.BasisFormat),"/* basis_transcoder.js */",e,"/* worker */",t.substring(t.indexOf("{")+1,t.lastIndexOf("}"))].join("\n");this.workerSourceURL=URL.createObjectURL(new Blob([o])),this.transcoderBinary=r,this.workerPool.setWorkerCreator((()=>{const e=new Worker(this.workerSourceURL),r=this.transcoderBinary.slice(0);return e.postMessage({type:"init",config:this.workerConfig,transcoderBinary:r},[r]),e}))})),i>0&&console.warn("THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues. Use a single KTX2Loader instance, or call .dispose() on old instances."),i++}return this.transcoderPending}load(r,t,o,s){if(null===this.workerConfig)throw new Error("THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.");const a=new e.FileLoader(this.manager);a.setResponseType("arraybuffer"),a.setWithCredentials(this.withCredentials),a.load(r,(e=>{if(n.has(e)){return n.get(e).promise.then(t).catch(s)}this._createTexture(e).then((e=>t?t(e):null)).catch(s)}),o,s)}_createTextureFrom(r,o){const{mipmaps:n,width:a,height:i,format:_,type:d,error:T,dfdTransferFn:c,dfdFlags:R}=r;if("error"===d)return Promise.reject(T);const F=o.layerCount>1?new s(n,a,i,o.layerCount,_,e.UnsignedByteType):new e.CompressedTexture(n,a,i,_,e.UnsignedByteType);return F.minFilter=1===n.length?e.LinearFilter:e.LinearMipmapLinearFilter,F.magFilter=e.LinearFilter,F.generateMipmaps=!1,F.needsUpdate=!0,F.encoding=c===t.KHR_DF_TRANSFER_SRGB?e.sRGBEncoding:e.LinearEncoding,F.premultiplyAlpha=!!(R&t.KHR_DF_FLAG_ALPHA_PREMULTIPLIED),F}async _createTexture(r,s={}){const i=t.read(new Uint8Array(r));if(i.vkFormat!==t.VK_FORMAT_UNDEFINED)return async function(r){const{vkFormat:s,pixelWidth:n,pixelHeight:i,pixelDepth:_}=r;if(void 0===d[s])throw new Error("THREE.KTX2Loader: Unsupported vkFormat.");const R=r.levels[0];let F,p;if(r.supercompressionScheme===t.KHR_SUPERCOMPRESSION_NONE)F=R.levelData;else{if(r.supercompressionScheme!==t.KHR_SUPERCOMPRESSION_ZSTD)throw new Error("THREE.KTX2Loader: Unsupported supercompressionScheme.");a||(a=new Promise((async e=>{const r=new o.ZSTDDecoder;await r.init(),e(r)}))),F=(await a).decode(R.levelData,R.uncompressedByteLength)}p=T[s]===e.FloatType?new Float32Array(F.buffer,F.byteOffset,F.byteLength/Float32Array.BYTES_PER_ELEMENT):T[s]===e.HalfFloatType?new Uint16Array(F.buffer,F.byteOffset,F.byteLength/Uint16Array.BYTES_PER_ELEMENT):F;const m=0===_?new e.DataTexture(p,n,i):new e.Data3DTexture(p,n,i,_);return m.type=T[s],m.format=d[s],m.encoding=c[s]||e.LinearEncoding,m.needsUpdate=!0,Promise.resolve(m)}(i);const _=s,R=this.init().then((()=>this.workerPool.postMessage({type:"transcode",buffer:r,taskConfig:_},[r]))).then((e=>this._createTextureFrom(e.data,i)));return n.set(r,{promise:R}),R}dispose(){return this.workerPool.dispose(),this.workerSourceURL&&URL.revokeObjectURL(this.workerSourceURL),i--,this}}_.BasisFormat={ETC1S:0,UASTC_4x4:1},_.TranscoderFormat={ETC1:0,ETC2:1,BC1:2,BC3:3,BC4:4,BC5:5,BC7_M6_OPAQUE_ONLY:6,BC7_M5:7,PVRTC1_4_RGB:8,PVRTC1_4_RGBA:9,ASTC_4x4:10,ATC_RGB:11,ATC_RGBA_INTERPOLATED_ALPHA:12,RGBA32:13,RGB565:14,BGR565:15,RGBA4444:16},_.EngineFormat={RGBAFormat:e.RGBAFormat,RGBA_ASTC_4x4_Format:e.RGBA_ASTC_4x4_Format,RGBA_BPTC_Format:e.RGBA_BPTC_Format,RGBA_ETC2_EAC_Format:e.RGBA_ETC2_EAC_Format,RGBA_PVRTC_4BPPV1_Format:e.RGBA_PVRTC_4BPPV1_Format,RGBA_S3TC_DXT5_Format:e.RGBA_S3TC_DXT5_Format,RGB_ETC1_Format:e.RGB_ETC1_Format,RGB_ETC2_Format:e.RGB_ETC2_Format,RGB_PVRTC_4BPPV1_Format:e.RGB_PVRTC_4BPPV1_Format,RGB_S3TC_DXT1_Format:e.RGB_S3TC_DXT1_Format},_.BasisWorker=function(){let e,r,t;const o=_EngineFormat,s=_TranscoderFormat,n=_BasisFormat;self.addEventListener("message",(function(a){const c=a.data;switch(c.type){case"init":e=c.config,R=c.transcoderBinary,r=new Promise((e=>{t={wasmBinary:R,onRuntimeInitialized:e},BASIS(t)})).then((()=>{t.initializeBasis(),void 0===t.KTX2File&&console.warn("THREE.KTX2Loader: Please update Basis Universal transcoder.")}));break;case"transcode":r.then((()=>{try{const{width:r,height:a,hasAlpha:R,mipmaps:F,format:p,dfdTransferFn:m,dfdFlags:l}=function(r){const a=new t.KTX2File(new Uint8Array(r));function c(){a.close(),a.delete()}if(!a.isValid())throw c(),new Error("THREE.KTX2Loader:\tInvalid or unsupported .ktx2 file");const R=a.isUASTC()?n.UASTC_4x4:n.ETC1S,F=a.getWidth(),p=a.getHeight(),m=a.getLayers()||1,l=a.getLevels(),h=a.getHasAlpha(),A=a.getDFDTransferFunc(),B=a.getDFDFlags(),{transcoderFormat:u,engineFormat:C}=function(r,t,a,T){let c,R;const F=r===n.ETC1S?i:_;for(let o=0;o<F.length;o++){const s=F[o];if(e[s.if]&&(s.basisFormat.includes(r)&&!(T&&s.transcoderFormat.length<2)&&(!s.needsPowerOfTwo||d(t)&&d(a))))return c=s.transcoderFormat[T?1:0],R=s.engineFormat[T?1:0],{transcoderFormat:c,engineFormat:R}}return console.warn("THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32."),c=s.RGBA32,R=o.RGBAFormat,{transcoderFormat:c,engineFormat:R}}(R,F,p,h);if(!F||!p||!l)throw c(),new Error("THREE.KTX2Loader:\tInvalid texture");if(!a.startTranscoding())throw c(),new Error("THREE.KTX2Loader: .startTranscoding failed");const g=[];for(let e=0;e<l;e++){const r=[];let t,o;for(let s=0;s<m;s++){const n=a.getImageLevelInfo(e,s,0);t=n.origWidth<4?n.origWidth:n.width,o=n.origHeight<4?n.origHeight:n.height;const i=new Uint8Array(a.getImageTranscodedSizeInBytes(e,s,0,u));if(!a.transcodeImage(i,e,s,0,u,0,-1,-1))throw c(),new Error("THREE.KTX2Loader: .transcodeImage failed.");r.push(i)}g.push({data:T(r),width:t,height:o})}return c(),{width:F,height:p,hasAlpha:h,mipmaps:g,format:C,dfdTransferFn:A,dfdFlags:B}}(c.buffer),h=[];for(let e=0;e<F.length;++e)h.push(F[e].data.buffer);self.postMessage({type:"transcode",id:c.id,width:r,height:a,hasAlpha:R,mipmaps:F,format:p,dfdTransferFn:m,dfdFlags:l},h)}catch(e){console.error(e),self.postMessage({type:"error",id:c.id,error:e.message})}}))}var R}));const a=[{if:"astcSupported",basisFormat:[n.UASTC_4x4],transcoderFormat:[s.ASTC_4x4,s.ASTC_4x4],engineFormat:[o.RGBA_ASTC_4x4_Format,o.RGBA_ASTC_4x4_Format],priorityETC1S:1/0,priorityUASTC:1,needsPowerOfTwo:!1},{if:"bptcSupported",basisFormat:[n.ETC1S,n.UASTC_4x4],transcoderFormat:[s.BC7_M5,s.BC7_M5],engineFormat:[o.RGBA_BPTC_Format,o.RGBA_BPTC_Format],priorityETC1S:3,priorityUASTC:2,needsPowerOfTwo:!1},{if:"dxtSupported",basisFormat:[n.ETC1S,n.UASTC_4x4],transcoderFormat:[s.BC1,s.BC3],engineFormat:[o.RGB_S3TC_DXT1_Format,o.RGBA_S3TC_DXT5_Format],priorityETC1S:4,priorityUASTC:5,needsPowerOfTwo:!1},{if:"etc2Supported",basisFormat:[n.ETC1S,n.UASTC_4x4],transcoderFormat:[s.ETC1,s.ETC2],engineFormat:[o.RGB_ETC2_Format,o.RGBA_ETC2_EAC_Format],priorityETC1S:1,priorityUASTC:3,needsPowerOfTwo:!1},{if:"etc1Supported",basisFormat:[n.ETC1S,n.UASTC_4x4],transcoderFormat:[s.ETC1],engineFormat:[o.RGB_ETC1_Format],priorityETC1S:2,priorityUASTC:4,needsPowerOfTwo:!1},{if:"pvrtcSupported",basisFormat:[n.ETC1S,n.UASTC_4x4],transcoderFormat:[s.PVRTC1_4_RGB,s.PVRTC1_4_RGBA],engineFormat:[o.RGB_PVRTC_4BPPV1_Format,o.RGBA_PVRTC_4BPPV1_Format],priorityETC1S:5,priorityUASTC:6,needsPowerOfTwo:!0}],i=a.sort((function(e,r){return e.priorityETC1S-r.priorityETC1S})),_=a.sort((function(e,r){return e.priorityUASTC-r.priorityUASTC}));function d(e){return e<=2||0==(e&e-1)&&0!==e}function T(e){let r=0;for(let t=0;t<e.length;t++){r+=e[t].byteLength}const t=new Uint8Array(r);let o=0;for(let r=0;r<e.length;r++){const s=e[r];t.set(s,o),o+=s.byteLength}return t}};const d={[t.VK_FORMAT_R32G32B32A32_SFLOAT]:e.RGBAFormat,[t.VK_FORMAT_R16G16B16A16_SFLOAT]:e.RGBAFormat,[t.VK_FORMAT_R8G8B8A8_UNORM]:e.RGBAFormat,[t.VK_FORMAT_R8G8B8A8_SRGB]:e.RGBAFormat,[t.VK_FORMAT_R32G32_SFLOAT]:e.RGFormat,[t.VK_FORMAT_R16G16_SFLOAT]:e.RGFormat,[t.VK_FORMAT_R8G8_UNORM]:e.RGFormat,[t.VK_FORMAT_R8G8_SRGB]:e.RGFormat,[t.VK_FORMAT_R32_SFLOAT]:e.RedFormat,[t.VK_FORMAT_R16_SFLOAT]:e.RedFormat,[t.VK_FORMAT_R8_SRGB]:e.RedFormat,[t.VK_FORMAT_R8_UNORM]:e.RedFormat},T={[t.VK_FORMAT_R32G32B32A32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16G16B16A16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8G8B8A8_UNORM]:e.UnsignedByteType,[t.VK_FORMAT_R8G8B8A8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R32G32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16G16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8G8_UNORM]:e.UnsignedByteType,[t.VK_FORMAT_R8G8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R32_SFLOAT]:e.FloatType,[t.VK_FORMAT_R16_SFLOAT]:e.HalfFloatType,[t.VK_FORMAT_R8_SRGB]:e.UnsignedByteType,[t.VK_FORMAT_R8_UNORM]:e.UnsignedByteType},c={[t.VK_FORMAT_R8G8B8A8_SRGB]:e.sRGBEncoding,[t.VK_FORMAT_R8G8_SRGB]:e.sRGBEncoding,[t.VK_FORMAT_R8_SRGB]:e.sRGBEncoding};exports.KTX2Loader=_;
|
package/loaders/KTX2Loader.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Loader, FileLoader,
|
|
1
|
+
import { Loader, FileLoader, UnsignedByteType, CompressedTexture, LinearFilter, LinearMipmapLinearFilter, sRGBEncoding, LinearEncoding, RGBAFormat, RGBA_ASTC_4x4_Format, RGBA_BPTC_Format, RGBA_ETC2_EAC_Format, RGBA_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGB_PVRTC_4BPPV1_Format, RGB_S3TC_DXT1_Format, FloatType, HalfFloatType, DataTexture, Data3DTexture, ClampToEdgeWrapping, RGFormat, RedFormat } from 'three';
|
|
2
2
|
import { WorkerPool } from '../utils/WorkerPool.js';
|
|
3
3
|
import { KHR_DF_TRANSFER_SRGB, KHR_DF_FLAG_ALPHA_PREMULTIPLIED, read, VK_FORMAT_UNDEFINED, KHR_SUPERCOMPRESSION_NONE, KHR_SUPERCOMPRESSION_ZSTD, VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R16G16_SFLOAT, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8_SRGB, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R16_SFLOAT, VK_FORMAT_R8_SRGB, VK_FORMAT_R8_UNORM } from 'ktx-parse';
|
|
4
4
|
import { ZSTDDecoder } from 'zstddec';
|
|
@@ -16,6 +16,16 @@ import { ZSTDDecoder } from 'zstddec';
|
|
|
16
16
|
* - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
class CompressedArrayTexture extends CompressedTexture {
|
|
20
|
+
constructor(mipmaps, width, height, depth, format, type) {
|
|
21
|
+
super(mipmaps, width, height, format, type);
|
|
22
|
+
this.isCompressedArrayTexture = true;
|
|
23
|
+
this.image.depth = depth;
|
|
24
|
+
this.wrapR = ClampToEdgeWrapping;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
19
29
|
const _taskCache = new WeakMap();
|
|
20
30
|
|
|
21
31
|
let _activeLoaders = 0;
|
|
@@ -127,7 +137,7 @@ class KTX2Loader extends Loader {
|
|
|
127
137
|
}, onProgress, onError);
|
|
128
138
|
}
|
|
129
139
|
|
|
130
|
-
_createTextureFrom(transcodeResult) {
|
|
140
|
+
_createTextureFrom(transcodeResult, container) {
|
|
131
141
|
const {
|
|
132
142
|
mipmaps,
|
|
133
143
|
width,
|
|
@@ -139,7 +149,7 @@ class KTX2Loader extends Loader {
|
|
|
139
149
|
dfdFlags
|
|
140
150
|
} = transcodeResult;
|
|
141
151
|
if (type === 'error') return Promise.reject(error);
|
|
142
|
-
const texture = new CompressedTexture(mipmaps, width, height, format, UnsignedByteType);
|
|
152
|
+
const texture = container.layerCount > 1 ? new CompressedArrayTexture(mipmaps, width, height, container.layerCount, format, UnsignedByteType) : new CompressedTexture(mipmaps, width, height, format, UnsignedByteType);
|
|
143
153
|
texture.minFilter = mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
|
|
144
154
|
texture.magFilter = LinearFilter;
|
|
145
155
|
texture.generateMipmaps = false;
|
|
@@ -151,11 +161,11 @@ class KTX2Loader extends Loader {
|
|
|
151
161
|
/**
|
|
152
162
|
* @param {ArrayBuffer} buffer
|
|
153
163
|
* @param {object?} config
|
|
154
|
-
* @return {Promise<CompressedTexture|DataTexture|Data3DTexture>}
|
|
164
|
+
* @return {Promise<CompressedTexture|CompressedArrayTexture|DataTexture|Data3DTexture>}
|
|
155
165
|
*/
|
|
156
166
|
|
|
157
167
|
|
|
158
|
-
_createTexture(buffer, config = {}) {
|
|
168
|
+
async _createTexture(buffer, config = {}) {
|
|
159
169
|
const container = read(new Uint8Array(buffer));
|
|
160
170
|
|
|
161
171
|
if (container.vkFormat !== VK_FORMAT_UNDEFINED) {
|
|
@@ -170,7 +180,7 @@ class KTX2Loader extends Loader {
|
|
|
170
180
|
buffer,
|
|
171
181
|
taskConfig: taskConfig
|
|
172
182
|
}, [buffer]);
|
|
173
|
-
}).then(e => this._createTextureFrom(e.data)); // Cache the task result.
|
|
183
|
+
}).then(e => this._createTextureFrom(e.data, container)); // Cache the task result.
|
|
174
184
|
|
|
175
185
|
_taskCache.set(buffer, {
|
|
176
186
|
promise: texturePending
|
|
@@ -320,6 +330,7 @@ KTX2Loader.BasisWorker = function () {
|
|
|
320
330
|
const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
|
|
321
331
|
const width = ktx2File.getWidth();
|
|
322
332
|
const height = ktx2File.getHeight();
|
|
333
|
+
const layers = ktx2File.getLayers() || 1;
|
|
323
334
|
const levels = ktx2File.getLevels();
|
|
324
335
|
const hasAlpha = ktx2File.getHasAlpha();
|
|
325
336
|
const dfdTransferFn = ktx2File.getDFDTransferFunc();
|
|
@@ -342,19 +353,26 @@ KTX2Loader.BasisWorker = function () {
|
|
|
342
353
|
const mipmaps = [];
|
|
343
354
|
|
|
344
355
|
for (let mip = 0; mip < levels; mip++) {
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
356
|
+
const layerMips = [];
|
|
357
|
+
let mipWidth, mipHeight;
|
|
358
|
+
|
|
359
|
+
for (let layer = 0; layer < layers; layer++) {
|
|
360
|
+
const levelInfo = ktx2File.getImageLevelInfo(mip, layer, 0);
|
|
361
|
+
mipWidth = levelInfo.origWidth < 4 ? levelInfo.origWidth : levelInfo.width;
|
|
362
|
+
mipHeight = levelInfo.origHeight < 4 ? levelInfo.origHeight : levelInfo.height;
|
|
363
|
+
const dst = new Uint8Array(ktx2File.getImageTranscodedSizeInBytes(mip, layer, 0, transcoderFormat));
|
|
364
|
+
const status = ktx2File.transcodeImage(dst, mip, layer, 0, transcoderFormat, 0, -1, -1);
|
|
365
|
+
|
|
366
|
+
if (!status) {
|
|
367
|
+
cleanup();
|
|
368
|
+
throw new Error('THREE.KTX2Loader: .transcodeImage failed.');
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
layerMips.push(dst);
|
|
354
372
|
}
|
|
355
373
|
|
|
356
374
|
mipmaps.push({
|
|
357
|
-
data:
|
|
375
|
+
data: concat(layerMips),
|
|
358
376
|
width: mipWidth,
|
|
359
377
|
height: mipHeight
|
|
360
378
|
});
|
|
@@ -468,6 +486,28 @@ KTX2Loader.BasisWorker = function () {
|
|
|
468
486
|
if (value <= 2) return true;
|
|
469
487
|
return (value & value - 1) === 0 && value !== 0;
|
|
470
488
|
}
|
|
489
|
+
/** Concatenates N byte arrays. */
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
function concat(arrays) {
|
|
493
|
+
let totalByteLength = 0;
|
|
494
|
+
|
|
495
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
496
|
+
const array = arrays[i];
|
|
497
|
+
totalByteLength += array.byteLength;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const result = new Uint8Array(totalByteLength);
|
|
501
|
+
let byteOffset = 0;
|
|
502
|
+
|
|
503
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
504
|
+
const array = arrays[i];
|
|
505
|
+
result.set(array, byteOffset);
|
|
506
|
+
byteOffset += array.byteLength;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
471
511
|
}; //
|
|
472
512
|
// DataTexture and Data3DTexture parsing.
|
|
473
513
|
|