KTX2Loader.js 18 KB

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