UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

2 lines (1 loc) 9.98 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var e=require("../../../build/three.module.js"),t=require("../utils/WorkerPool.js"),r=require("../libs/ktx-parse.module.js"),o=require("../libs/zstddec.module.js");const s=new WeakMap;let n;class a extends e.Loader{constructor(e){super(e),this.transcoderPath="",this.transcoderBinary=null,this.transcoderPending=null,this.workerPool=new t.WorkerPool,this.workerSourceURL="",this.workerConfig=null}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 t=new e.FileLoader(this.manager);t.setPath(this.transcoderPath),t.setWithCredentials(this.withCredentials);const r=t.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([r,s]).then((([e,t])=>{const r=a.BasisWorker.toString(),o=["/* constants */","let _EngineFormat = "+JSON.stringify(a.EngineFormat),"let _TranscoderFormat = "+JSON.stringify(a.TranscoderFormat),"let _BasisFormat = "+JSON.stringify(a.BasisFormat),"/* basis_transcoder.js */",e,"/* worker */",r.substring(r.indexOf("{")+1,r.lastIndexOf("}"))].join("\n");this.workerSourceURL=URL.createObjectURL(new Blob([o])),this.transcoderBinary=t,this.workerPool.setWorkerCreator((()=>{const e=new Worker(this.workerSourceURL),t=this.transcoderBinary.slice(0);return e.postMessage({type:"init",config:this.workerConfig,transcoderBinary:t},[t]),e}))}))}return this.transcoderPending}load(t,r,o,n){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(t,(e=>{if(s.has(e)){return s.get(e).promise.then(r).catch(n)}this._createTexture(e).then((e=>r?r(e):null)).catch(n)}),o,n)}_createTextureFrom(t,o){const{mipmaps:s,width:n,height:a,format:i,type:_,error:T,dfdTransferFn:d,dfdFlags:R}=t;if("error"===_)return Promise.reject(T);const c=o.layerCount>1?new e.CompressedArrayTexture(s,n,a,o.layerCount,i,e.UnsignedByteType):new e.CompressedTexture(s,n,a,i,e.UnsignedByteType);return c.minFilter=1===s.length?e.LinearFilter:e.LinearMipmapLinearFilter,c.magFilter=e.LinearFilter,c.generateMipmaps=!1,c.needsUpdate=!0,c.encoding=d===r.KHR_DF_TRANSFER_SRGB?e.sRGBEncoding:e.LinearEncoding,c.premultiplyAlpha=!!(R&r.KHR_DF_FLAG_ALPHA_PREMULTIPLIED),c}async _createTexture(t,a={}){const d=r.read(new Uint8Array(t));if(d.vkFormat!==r.VK_FORMAT_UNDEFINED)return async function(t){const{vkFormat:s,pixelWidth:a,pixelHeight:d,pixelDepth:R}=t;if(void 0===i[s])throw new Error("THREE.KTX2Loader: Unsupported vkFormat.");const c=t.levels[0];let F,m;if(t.supercompressionScheme===r.KHR_SUPERCOMPRESSION_NONE)F=c.levelData;else{if(t.supercompressionScheme!==r.KHR_SUPERCOMPRESSION_ZSTD)throw new Error("THREE.KTX2Loader: Unsupported supercompressionScheme.");n||(n=new Promise((async e=>{const t=new o.ZSTDDecoder;await t.init(),e(t)}))),F=(await n).decode(c.levelData,c.uncompressedByteLength)}m=_[s]===e.FloatType?new Float32Array(F.buffer,F.byteOffset,F.byteLength/Float32Array.BYTES_PER_ELEMENT):_[s]===e.HalfFloatType?new Uint16Array(F.buffer,F.byteOffset,F.byteLength/Uint16Array.BYTES_PER_ELEMENT):F;const p=0===R?new e.DataTexture(m,a,d):new e.Data3DTexture(m,a,d,R);return p.type=_[s],p.format=i[s],p.encoding=T[s]||e.LinearEncoding,p.needsUpdate=!0,Promise.resolve(p)}(d);const R=a,c=this.init().then((()=>this.workerPool.postMessage({type:"transcode",buffer:t,taskConfig:R},[t]))).then((e=>this._createTextureFrom(e.data,d)));return s.set(t,{promise:c}),c}dispose(){return this.workerPool.dispose(),this.workerSourceURL&&URL.revokeObjectURL(this.workerSourceURL),this}}a.BasisFormat={ETC1S:0,UASTC_4x4:1},a.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},a.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},a.BasisWorker=function(){let e,t,r;const o=_EngineFormat,s=_TranscoderFormat,n=_BasisFormat;self.addEventListener("message",(function(a){const R=a.data;switch(R.type){case"init":e=R.config,c=R.transcoderBinary,t=new Promise((e=>{r={wasmBinary:c,onRuntimeInitialized:e},BASIS(r)})).then((()=>{r.initializeBasis(),r.KTX2File}));break;case"transcode":t.then((()=>{try{const{width:t,height:a,hasAlpha:c,mipmaps:F,format:m,dfdTransferFn:p,dfdFlags:A}=function(t){const a=new r.KTX2File(new Uint8Array(t));function R(){a.close(),a.delete()}if(!a.isValid())throw R(),new Error("THREE.KTX2Loader:\tInvalid or unsupported .ktx2 file");const c=a.isUASTC()?n.UASTC_4x4:n.ETC1S,F=a.getWidth(),m=a.getHeight(),p=a.getLayers()||1,A=a.getLevels(),h=a.getHasAlpha(),l=a.getDFDTransferFunc(),B=a.getDFDFlags(),{transcoderFormat:u,engineFormat:S}=function(t,r,a,d){let R,c;const F=t===n.ETC1S?i:_;for(let o=0;o<F.length;o++){const s=F[o];if(e[s.if]&&(s.basisFormat.includes(t)&&!(d&&s.transcoderFormat.length<2)&&(!s.needsPowerOfTwo||T(r)&&T(a))))return R=s.transcoderFormat[d?1:0],c=s.engineFormat[d?1:0],{transcoderFormat:R,engineFormat:c}}return R=s.RGBA32,c=o.RGBAFormat,{transcoderFormat:R,engineFormat:c}}(c,F,m,h);if(!F||!m||!A)throw R(),new Error("THREE.KTX2Loader:\tInvalid texture");if(!a.startTranscoding())throw R(),new Error("THREE.KTX2Loader: .startTranscoding failed");const C=[];for(let e=0;e<A;e++){const t=[];let r,o;for(let s=0;s<p;s++){const n=a.getImageLevelInfo(e,s,0);r=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 R(),new Error("THREE.KTX2Loader: .transcodeImage failed.");t.push(i)}C.push({data:d(t),width:r,height:o})}return R(),{width:F,height:m,hasAlpha:h,mipmaps:C,format:S,dfdTransferFn:l,dfdFlags:B}}(R.buffer),h=[];for(let e=0;e<F.length;++e)h.push(F[e].data.buffer);self.postMessage({type:"transcode",id:R.id,width:t,height:a,hasAlpha:c,mipmaps:F,format:m,dfdTransferFn:p,dfdFlags:A},h)}catch(t){self.postMessage({type:"error",id:R.id,error:t.message})}}))}var c}));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,t){return e.priorityETC1S-t.priorityETC1S})),_=a.sort((function(e,t){return e.priorityUASTC-t.priorityUASTC}));function T(e){return e<=2||0==(e&e-1)&&0!==e}function d(e){let t=0;for(const s of e)t+=s.byteLength;const r=new Uint8Array(t);let o=0;for(const s of e)r.set(s,o),o+=s.byteLength;return r}};const i={[r.VK_FORMAT_R32G32B32A32_SFLOAT]:e.RGBAFormat,[r.VK_FORMAT_R16G16B16A16_SFLOAT]:e.RGBAFormat,[r.VK_FORMAT_R8G8B8A8_UNORM]:e.RGBAFormat,[r.VK_FORMAT_R8G8B8A8_SRGB]:e.RGBAFormat,[r.VK_FORMAT_R32G32_SFLOAT]:e.RGFormat,[r.VK_FORMAT_R16G16_SFLOAT]:e.RGFormat,[r.VK_FORMAT_R8G8_UNORM]:e.RGFormat,[r.VK_FORMAT_R8G8_SRGB]:e.RGFormat,[r.VK_FORMAT_R32_SFLOAT]:e.RedFormat,[r.VK_FORMAT_R16_SFLOAT]:e.RedFormat,[r.VK_FORMAT_R8_SRGB]:e.RedFormat,[r.VK_FORMAT_R8_UNORM]:e.RedFormat},_={[r.VK_FORMAT_R32G32B32A32_SFLOAT]:e.FloatType,[r.VK_FORMAT_R16G16B16A16_SFLOAT]:e.HalfFloatType,[r.VK_FORMAT_R8G8B8A8_UNORM]:e.UnsignedByteType,[r.VK_FORMAT_R8G8B8A8_SRGB]:e.UnsignedByteType,[r.VK_FORMAT_R32G32_SFLOAT]:e.FloatType,[r.VK_FORMAT_R16G16_SFLOAT]:e.HalfFloatType,[r.VK_FORMAT_R8G8_UNORM]:e.UnsignedByteType,[r.VK_FORMAT_R8G8_SRGB]:e.UnsignedByteType,[r.VK_FORMAT_R32_SFLOAT]:e.FloatType,[r.VK_FORMAT_R16_SFLOAT]:e.HalfFloatType,[r.VK_FORMAT_R8_SRGB]:e.UnsignedByteType,[r.VK_FORMAT_R8_UNORM]:e.UnsignedByteType},T={[r.VK_FORMAT_R8G8B8A8_SRGB]:e.sRGBEncoding,[r.VK_FORMAT_R8G8_SRGB]:e.sRGBEncoding,[r.VK_FORMAT_R8_SRGB]:e.sRGBEncoding};exports.KTX2Loader=a;