KTX2Loader.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. ( function () {
  2. /**
  3. * THREE.Loader for KTX 2.0 GPU Texture containers.
  4. *
  5. * KTX 2.0 is a container format for various GPU texture formats. The loader
  6. * supports Basis Universal GPU textures, which can be quickly transcoded to
  7. * a wide variety of GPU texture compression formats, as well as some
  8. * uncompressed THREE.DataTexture and THREE.Data3DTexture formats.
  9. *
  10. * References:
  11. * - KTX: http://github.khronos.org/KTX-Specification/
  12. * - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
  13. */
  14. const _taskCache = new WeakMap();
  15. let _activeLoaders = 0;
  16. let _zstd;
  17. class KTX2Loader extends THREE.Loader {
  18. constructor( manager ) {
  19. super( manager );
  20. this.transcoderPath = '';
  21. this.transcoderBinary = null;
  22. this.transcoderPending = null;
  23. this.workerPool = new THREE.WorkerPool();
  24. this.workerSourceURL = '';
  25. this.workerConfig = null;
  26. if ( typeof MSC_TRANSCODER !== 'undefined' ) {
  27. console.warn( 'THREE.KTX2Loader: Please update to latest "basis_transcoder".' + ' "msc_basis_transcoder" is no longer supported in three.js r125+.' );
  28. }
  29. }
  30. setTranscoderPath( path ) {
  31. this.transcoderPath = path;
  32. return this;
  33. }
  34. setWorkerLimit( num ) {
  35. this.workerPool.setWorkerLimit( num );
  36. return this;
  37. }
  38. detectSupport( renderer ) {
  39. this.workerConfig = {
  40. astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
  41. etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
  42. etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
  43. dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
  44. bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
  45. pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' ) || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
  46. };
  47. if ( renderer.capabilities.isWebGL2 ) {
  48. // https://github.com/mrdoob/three.js/pull/22928
  49. this.workerConfig.etc1Supported = false;
  50. }
  51. return this;
  52. }
  53. init() {
  54. if ( ! this.transcoderPending ) {
  55. // Load transcoder wrapper.
  56. const jsLoader = new THREE.FileLoader( this.manager );
  57. jsLoader.setPath( this.transcoderPath );
  58. jsLoader.setWithCredentials( this.withCredentials );
  59. const jsContent = jsLoader.loadAsync( 'basis_transcoder.js' ); // Load transcoder WASM binary.
  60. const binaryLoader = new THREE.FileLoader( this.manager );
  61. binaryLoader.setPath( this.transcoderPath );
  62. binaryLoader.setResponseType( 'arraybuffer' );
  63. binaryLoader.setWithCredentials( this.withCredentials );
  64. const binaryContent = binaryLoader.loadAsync( 'basis_transcoder.wasm' );
  65. this.transcoderPending = Promise.all( [ jsContent, binaryContent ] ).then( ( [ jsContent, binaryContent ] ) => {
  66. const fn = KTX2Loader.BasisWorker.toString();
  67. const body = [ '/* constants */', 'let _EngineFormat = ' + JSON.stringify( KTX2Loader.EngineFormat ), 'let _TranscoderFormat = ' + JSON.stringify( KTX2Loader.TranscoderFormat ), 'let _BasisFormat = ' + JSON.stringify( KTX2Loader.BasisFormat ), '/* basis_transcoder.js */', jsContent, '/* worker */', fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) ) ].join( '\n' );
  68. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  69. this.transcoderBinary = binaryContent;
  70. this.workerPool.setWorkerCreator( () => {
  71. const worker = new Worker( this.workerSourceURL );
  72. const transcoderBinary = this.transcoderBinary.slice( 0 );
  73. worker.postMessage( {
  74. type: 'init',
  75. config: this.workerConfig,
  76. transcoderBinary
  77. }, [ transcoderBinary ] );
  78. return worker;
  79. } );
  80. } );
  81. if ( _activeLoaders > 0 ) {
  82. // Each instance loads a transcoder and allocates workers, increasing network and memory cost.
  83. console.warn( 'THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues.' + ' Use a single KTX2Loader instance, or call .dispose() on old instances.' );
  84. }
  85. _activeLoaders ++;
  86. }
  87. return this.transcoderPending;
  88. }
  89. load( url, onLoad, onProgress, onError ) {
  90. if ( this.workerConfig === null ) {
  91. throw new Error( 'THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.' );
  92. }
  93. const loader = new THREE.FileLoader( this.manager );
  94. loader.setResponseType( 'arraybuffer' );
  95. loader.setWithCredentials( this.withCredentials );
  96. loader.load( url, buffer => {
  97. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  98. // again from this thread.
  99. if ( _taskCache.has( buffer ) ) {
  100. const cachedTask = _taskCache.get( buffer );
  101. return cachedTask.promise.then( onLoad ).catch( onError );
  102. }
  103. this._createTexture( buffer ).then( texture => onLoad ? onLoad( texture ) : null ).catch( onError );
  104. }, onProgress, onError );
  105. }
  106. _createTextureFrom( transcodeResult ) {
  107. const {
  108. mipmaps,
  109. width,
  110. height,
  111. format,
  112. type,
  113. error,
  114. dfdTransferFn,
  115. dfdFlags
  116. } = transcodeResult;
  117. if ( type === 'error' ) return Promise.reject( error );
  118. const texture = new THREE.CompressedTexture( mipmaps, width, height, format, THREE.UnsignedByteType );
  119. texture.minFilter = mipmaps.length === 1 ? THREE.LinearFilter : THREE.LinearMipmapLinearFilter;
  120. texture.magFilter = THREE.LinearFilter;
  121. texture.generateMipmaps = false;
  122. texture.needsUpdate = true;
  123. texture.encoding = dfdTransferFn === KHR_DF_TRANSFER_SRGB ? THREE.sRGBEncoding : THREE.LinearEncoding;
  124. texture.premultiplyAlpha = !! ( dfdFlags & KHR_DF_FLAG_ALPHA_PREMULTIPLIED );
  125. return texture;
  126. }
  127. /**
  128. * @param {ArrayBuffer} buffer
  129. * @param {object?} config
  130. * @return {Promise<CompressedTexture|DataTexture|Data3DTexture>}
  131. */
  132. _createTexture( buffer, config = {} ) {
  133. const container = read( new Uint8Array( buffer ) );
  134. if ( container.vkFormat !== VK_FORMAT_UNDEFINED ) {
  135. return createDataTexture( container );
  136. } //
  137. const taskConfig = config;
  138. const texturePending = this.init().then( () => {
  139. return this.workerPool.postMessage( {
  140. type: 'transcode',
  141. buffer,
  142. taskConfig: taskConfig
  143. }, [ buffer ] );
  144. } ).then( e => this._createTextureFrom( e.data ) ); // Cache the task result.
  145. _taskCache.set( buffer, {
  146. promise: texturePending
  147. } );
  148. return texturePending;
  149. }
  150. dispose() {
  151. this.workerPool.dispose();
  152. if ( this.workerSourceURL ) URL.revokeObjectURL( this.workerSourceURL );
  153. _activeLoaders --;
  154. return this;
  155. }
  156. }
  157. /* CONSTANTS */
  158. KTX2Loader.BasisFormat = {
  159. ETC1S: 0,
  160. UASTC_4x4: 1
  161. };
  162. KTX2Loader.TranscoderFormat = {
  163. ETC1: 0,
  164. ETC2: 1,
  165. BC1: 2,
  166. BC3: 3,
  167. BC4: 4,
  168. BC5: 5,
  169. BC7_M6_OPAQUE_ONLY: 6,
  170. BC7_M5: 7,
  171. PVRTC1_4_RGB: 8,
  172. PVRTC1_4_RGBA: 9,
  173. ASTC_4x4: 10,
  174. ATC_RGB: 11,
  175. ATC_RGBA_INTERPOLATED_ALPHA: 12,
  176. RGBA32: 13,
  177. RGB565: 14,
  178. BGR565: 15,
  179. RGBA4444: 16
  180. };
  181. KTX2Loader.EngineFormat = {
  182. RGBAFormat: THREE.RGBAFormat,
  183. RGBA_ASTC_4x4_Format: THREE.RGBA_ASTC_4x4_Format,
  184. RGBA_BPTC_Format: THREE.RGBA_BPTC_Format,
  185. RGBA_ETC2_EAC_Format: THREE.RGBA_ETC2_EAC_Format,
  186. RGBA_PVRTC_4BPPV1_Format: THREE.RGBA_PVRTC_4BPPV1_Format,
  187. RGBA_S3TC_DXT5_Format: THREE.RGBA_S3TC_DXT5_Format,
  188. RGB_ETC1_Format: THREE.RGB_ETC1_Format,
  189. RGB_ETC2_Format: THREE.RGB_ETC2_Format,
  190. RGB_PVRTC_4BPPV1_Format: THREE.RGB_PVRTC_4BPPV1_Format,
  191. RGB_S3TC_DXT1_Format: THREE.RGB_S3TC_DXT1_Format
  192. };
  193. /* WEB WORKER */
  194. KTX2Loader.BasisWorker = function () {
  195. let config;
  196. let transcoderPending;
  197. let BasisModule;
  198. const EngineFormat = _EngineFormat; // eslint-disable-line no-undef
  199. const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
  200. const BasisFormat = _BasisFormat; // eslint-disable-line no-undef
  201. self.addEventListener( 'message', function ( e ) {
  202. const message = e.data;
  203. switch ( message.type ) {
  204. case 'init':
  205. config = message.config;
  206. init( message.transcoderBinary );
  207. break;
  208. case 'transcode':
  209. transcoderPending.then( () => {
  210. try {
  211. const {
  212. width,
  213. height,
  214. hasAlpha,
  215. mipmaps,
  216. format,
  217. dfdTransferFn,
  218. dfdFlags
  219. } = transcode( message.buffer );
  220. const buffers = [];
  221. for ( let i = 0; i < mipmaps.length; ++ i ) {
  222. buffers.push( mipmaps[ i ].data.buffer );
  223. }
  224. self.postMessage( {
  225. type: 'transcode',
  226. id: message.id,
  227. width,
  228. height,
  229. hasAlpha,
  230. mipmaps,
  231. format,
  232. dfdTransferFn,
  233. dfdFlags
  234. }, buffers );
  235. } catch ( error ) {
  236. console.error( error );
  237. self.postMessage( {
  238. type: 'error',
  239. id: message.id,
  240. error: error.message
  241. } );
  242. }
  243. } );
  244. break;
  245. }
  246. } );
  247. function init( wasmBinary ) {
  248. transcoderPending = new Promise( resolve => {
  249. BasisModule = {
  250. wasmBinary,
  251. onRuntimeInitialized: resolve
  252. };
  253. BASIS( BasisModule ); // eslint-disable-line no-undef
  254. } ).then( () => {
  255. BasisModule.initializeBasis();
  256. if ( BasisModule.KTX2File === undefined ) {
  257. console.warn( 'THREE.KTX2Loader: Please update Basis Universal transcoder.' );
  258. }
  259. } );
  260. }
  261. function transcode( buffer ) {
  262. const ktx2File = new BasisModule.KTX2File( new Uint8Array( buffer ) );
  263. function cleanup() {
  264. ktx2File.close();
  265. ktx2File.delete();
  266. }
  267. if ( ! ktx2File.isValid() ) {
  268. cleanup();
  269. throw new Error( 'THREE.KTX2Loader: Invalid or unsupported .ktx2 file' );
  270. }
  271. const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
  272. const width = ktx2File.getWidth();
  273. const height = ktx2File.getHeight();
  274. const levels = ktx2File.getLevels();
  275. const hasAlpha = ktx2File.getHasAlpha();
  276. const dfdTransferFn = ktx2File.getDFDTransferFunc();
  277. const dfdFlags = ktx2File.getDFDFlags();
  278. const {
  279. transcoderFormat,
  280. engineFormat
  281. } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  282. if ( ! width || ! height || ! levels ) {
  283. cleanup();
  284. throw new Error( 'THREE.KTX2Loader: Invalid texture' );
  285. }
  286. if ( ! ktx2File.startTranscoding() ) {
  287. cleanup();
  288. throw new Error( 'THREE.KTX2Loader: .startTranscoding failed' );
  289. }
  290. const mipmaps = [];
  291. for ( let mip = 0; mip < levels; mip ++ ) {
  292. const levelInfo = ktx2File.getImageLevelInfo( mip, 0, 0 );
  293. const mipWidth = levelInfo.origWidth;
  294. const mipHeight = levelInfo.origHeight;
  295. const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, 0, 0, transcoderFormat ) );
  296. const status = ktx2File.transcodeImage( dst, mip, 0, 0, transcoderFormat, 0, - 1, - 1 );
  297. if ( ! status ) {
  298. cleanup();
  299. throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' );
  300. }
  301. mipmaps.push( {
  302. data: dst,
  303. width: mipWidth,
  304. height: mipHeight
  305. } );
  306. }
  307. cleanup();
  308. return {
  309. width,
  310. height,
  311. hasAlpha,
  312. mipmaps,
  313. format: engineFormat,
  314. dfdTransferFn,
  315. dfdFlags
  316. };
  317. } //
  318. // Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC),
  319. // device capabilities, and texture dimensions. The list below ranks the formats separately
  320. // for ETC1S and UASTC.
  321. //
  322. // In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at
  323. // significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently
  324. // chooses RGBA32 only as a last resort and does not expose that option to the caller.
  325. const FORMAT_OPTIONS = [ {
  326. if: 'astcSupported',
  327. basisFormat: [ BasisFormat.UASTC_4x4 ],
  328. transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
  329. engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
  330. priorityETC1S: Infinity,
  331. priorityUASTC: 1,
  332. needsPowerOfTwo: false
  333. }, {
  334. if: 'bptcSupported',
  335. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  336. transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
  337. engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
  338. priorityETC1S: 3,
  339. priorityUASTC: 2,
  340. needsPowerOfTwo: false
  341. }, {
  342. if: 'dxtSupported',
  343. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  344. transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
  345. engineFormat: [ EngineFormat.RGB_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
  346. priorityETC1S: 4,
  347. priorityUASTC: 5,
  348. needsPowerOfTwo: false
  349. }, {
  350. if: 'etc2Supported',
  351. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  352. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
  353. engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
  354. priorityETC1S: 1,
  355. priorityUASTC: 3,
  356. needsPowerOfTwo: false
  357. }, {
  358. if: 'etc1Supported',
  359. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  360. transcoderFormat: [ TranscoderFormat.ETC1 ],
  361. engineFormat: [ EngineFormat.RGB_ETC1_Format ],
  362. priorityETC1S: 2,
  363. priorityUASTC: 4,
  364. needsPowerOfTwo: false
  365. }, {
  366. if: 'pvrtcSupported',
  367. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  368. transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
  369. engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
  370. priorityETC1S: 5,
  371. priorityUASTC: 6,
  372. needsPowerOfTwo: true
  373. } ];
  374. const ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  375. return a.priorityETC1S - b.priorityETC1S;
  376. } );
  377. const UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  378. return a.priorityUASTC - b.priorityUASTC;
  379. } );
  380. function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
  381. let transcoderFormat;
  382. let engineFormat;
  383. const options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS;
  384. for ( let i = 0; i < options.length; i ++ ) {
  385. const opt = options[ i ];
  386. if ( ! config[ opt.if ] ) continue;
  387. if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
  388. if ( hasAlpha && opt.transcoderFormat.length < 2 ) continue;
  389. if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
  390. transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
  391. engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
  392. return {
  393. transcoderFormat,
  394. engineFormat
  395. };
  396. }
  397. console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' );
  398. transcoderFormat = TranscoderFormat.RGBA32;
  399. engineFormat = EngineFormat.RGBAFormat;
  400. return {
  401. transcoderFormat,
  402. engineFormat
  403. };
  404. }
  405. function isPowerOfTwo( value ) {
  406. if ( value <= 2 ) return true;
  407. return ( value & value - 1 ) === 0 && value !== 0;
  408. }
  409. }; //
  410. // THREE.DataTexture and THREE.Data3DTexture parsing.
  411. const FORMAT_MAP = {
  412. [ VK_FORMAT_R32G32B32A32_SFLOAT ]: THREE.RGBAFormat,
  413. [ VK_FORMAT_R16G16B16A16_SFLOAT ]: THREE.RGBAFormat,
  414. [ VK_FORMAT_R8G8B8A8_UNORM ]: THREE.RGBAFormat,
  415. [ VK_FORMAT_R8G8B8A8_SRGB ]: THREE.RGBAFormat,
  416. [ VK_FORMAT_R32G32_SFLOAT ]: THREE.RGFormat,
  417. [ VK_FORMAT_R16G16_SFLOAT ]: THREE.RGFormat,
  418. [ VK_FORMAT_R8G8_UNORM ]: THREE.RGFormat,
  419. [ VK_FORMAT_R8G8_SRGB ]: THREE.RGFormat,
  420. [ VK_FORMAT_R32_SFLOAT ]: THREE.RedFormat,
  421. [ VK_FORMAT_R16_SFLOAT ]: THREE.RedFormat,
  422. [ VK_FORMAT_R8_SRGB ]: THREE.RedFormat,
  423. [ VK_FORMAT_R8_UNORM ]: THREE.RedFormat
  424. };
  425. const TYPE_MAP = {
  426. [ VK_FORMAT_R32G32B32A32_SFLOAT ]: THREE.FloatType,
  427. [ VK_FORMAT_R16G16B16A16_SFLOAT ]: THREE.HalfFloatType,
  428. [ VK_FORMAT_R8G8B8A8_UNORM ]: THREE.UnsignedByteType,
  429. [ VK_FORMAT_R8G8B8A8_SRGB ]: THREE.UnsignedByteType,
  430. [ VK_FORMAT_R32G32_SFLOAT ]: THREE.FloatType,
  431. [ VK_FORMAT_R16G16_SFLOAT ]: THREE.HalfFloatType,
  432. [ VK_FORMAT_R8G8_UNORM ]: THREE.UnsignedByteType,
  433. [ VK_FORMAT_R8G8_SRGB ]: THREE.UnsignedByteType,
  434. [ VK_FORMAT_R32_SFLOAT ]: THREE.FloatType,
  435. [ VK_FORMAT_R16_SFLOAT ]: THREE.HalfFloatType,
  436. [ VK_FORMAT_R8_SRGB ]: THREE.UnsignedByteType,
  437. [ VK_FORMAT_R8_UNORM ]: THREE.UnsignedByteType
  438. };
  439. const ENCODING_MAP = {
  440. [ VK_FORMAT_R8G8B8A8_SRGB ]: THREE.sRGBEncoding,
  441. [ VK_FORMAT_R8G8_SRGB ]: THREE.sRGBEncoding,
  442. [ VK_FORMAT_R8_SRGB ]: THREE.sRGBEncoding
  443. };
  444. async function createDataTexture( container ) {
  445. const {
  446. vkFormat,
  447. pixelWidth,
  448. pixelHeight,
  449. pixelDepth
  450. } = container;
  451. if ( FORMAT_MAP[ vkFormat ] === undefined ) {
  452. throw new Error( 'THREE.KTX2Loader: Unsupported vkFormat.' );
  453. } //
  454. const level = container.levels[ 0 ];
  455. let levelData;
  456. let view;
  457. if ( container.supercompressionScheme === KHR_SUPERCOMPRESSION_NONE ) {
  458. levelData = level.levelData;
  459. } else if ( container.supercompressionScheme === KHR_SUPERCOMPRESSION_ZSTD ) {
  460. if ( ! _zstd ) {
  461. _zstd = new Promise( async resolve => {
  462. const zstd = new ZSTDDecoder();
  463. await zstd.init();
  464. resolve( zstd );
  465. } );
  466. }
  467. levelData = ( await _zstd ).decode( level.levelData, level.uncompressedByteLength );
  468. } else {
  469. throw new Error( 'THREE.KTX2Loader: Unsupported supercompressionScheme.' );
  470. }
  471. if ( TYPE_MAP[ vkFormat ] === THREE.FloatType ) {
  472. view = new Float32Array( levelData.buffer, levelData.byteOffset, levelData.byteLength / Float32Array.BYTES_PER_ELEMENT );
  473. } else if ( TYPE_MAP[ vkFormat ] === THREE.HalfFloatType ) {
  474. view = new Uint16Array( levelData.buffer, levelData.byteOffset, levelData.byteLength / Uint16Array.BYTES_PER_ELEMENT );
  475. } else {
  476. view = levelData;
  477. } //
  478. const texture = pixelDepth === 0 ? new THREE.DataTexture( view, pixelWidth, pixelHeight ) : new THREE.Data3DTexture( view, pixelWidth, pixelHeight, pixelDepth );
  479. texture.type = TYPE_MAP[ vkFormat ];
  480. texture.format = FORMAT_MAP[ vkFormat ];
  481. texture.encoding = ENCODING_MAP[ vkFormat ] || THREE.LinearEncoding;
  482. texture.needsUpdate = true; //
  483. return Promise.resolve( texture );
  484. }
  485. THREE.KTX2Loader = KTX2Loader;
  486. } )();