KTX2Loader.js 17 KB

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