KTX2Loader.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /**
  2. * @author donmccurdy / https://www.donmccurdy.com
  3. * @author MarkCallow / https://github.com/MarkCallow
  4. *
  5. * References:
  6. * - KTX: http://github.khronos.org/KTX-Specification/
  7. * - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
  8. *
  9. * To do:
  10. * - [ ] Cross-platform testing
  11. * - [ ] Specify JS/WASM transcoder path
  12. * - [ ] High-quality demo
  13. * - [ ] Documentation
  14. * - [ ] TypeScript definitions
  15. * - [ ] (Optional) Include BC5
  16. * - [ ] (Optional) Include EAC RG on mobile (WEBGL_compressed_texture_etc)
  17. * - [ ] (Optional) Include two-texture output mode (see: clearcoat + clearcoatRoughness)
  18. * - [ ] (Optional) Support Web Workers, after #18234
  19. */
  20. import {
  21. CompressedTexture,
  22. CompressedTextureLoader,
  23. FileLoader,
  24. LinearEncoding,
  25. LinearFilter,
  26. LinearMipmapLinearFilter,
  27. MathUtils,
  28. RGBAFormat,
  29. RGBA_ASTC_4x4_Format,
  30. RGBA_BPTC_Format,
  31. RGBA_ETC2_EAC_Format,
  32. RGBA_PVRTC_4BPPV1_Format,
  33. RGBA_S3TC_DXT5_Format,
  34. RGB_ETC1_Format,
  35. RGB_ETC2_Format,
  36. RGB_PVRTC_4BPPV1_Format,
  37. RGB_S3TC_DXT1_Format,
  38. UnsignedByteType,
  39. sRGBEncoding,
  40. } from '../../../build/three.module.js';
  41. import { ZSTDDecoder } from '../libs/zstddec.module.js';
  42. // Data Format Descriptor (DFD) constants.
  43. const DFDModel = {
  44. ETC1S: 163,
  45. UASTC: 166,
  46. };
  47. const DFDChannel = {
  48. ETC1S: {
  49. RGB: 0,
  50. RRR: 3,
  51. GGG: 4,
  52. AAA: 15,
  53. },
  54. UASTC: {
  55. RGB: 0,
  56. RGBA: 3,
  57. RRR: 4,
  58. RRRG: 5
  59. },
  60. };
  61. //
  62. class KTX2Loader extends CompressedTextureLoader {
  63. constructor( manager ) {
  64. super( manager );
  65. this.basisModule = null;
  66. this.basisModulePending = null;
  67. this.transcoderConfig = {};
  68. }
  69. detectSupport( renderer ) {
  70. this.transcoderConfig = {
  71. astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
  72. etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
  73. etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
  74. dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
  75. bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
  76. pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
  77. || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
  78. };
  79. return this;
  80. }
  81. initModule() {
  82. if ( this.basisModulePending ) {
  83. return;
  84. }
  85. var scope = this;
  86. // The Emscripten wrapper returns a fake Promise, which can cause
  87. // infinite recursion when mixed with native Promises. Wrap the module
  88. // initialization to return a native Promise.
  89. scope.basisModulePending = new Promise( function ( resolve ) {
  90. MSC_TRANSCODER().then( function ( basisModule ) {
  91. scope.basisModule = basisModule;
  92. basisModule.initTranscoders();
  93. resolve();
  94. } );
  95. } );
  96. }
  97. load( url, onLoad, onProgress, onError ) {
  98. var scope = this;
  99. var texture = new CompressedTexture();
  100. var bufferPending = new Promise( function ( resolve, reject ) {
  101. new FileLoader( scope.manager )
  102. .setPath( scope.path )
  103. .setResponseType( 'arraybuffer' )
  104. .load( url, resolve, onProgress, reject );
  105. } );
  106. this.initModule();
  107. Promise.all( [ bufferPending, this.basisModulePending ] ).then( function ( [ buffer ] ) {
  108. scope.parse( buffer, function ( _texture ) {
  109. texture.copy( _texture );
  110. texture.needsUpdate = true;
  111. if ( onLoad ) onLoad( texture );
  112. }, onError );
  113. } );
  114. return texture;
  115. }
  116. parse( buffer, onLoad, onError ) {
  117. var BasisLzEtc1sImageTranscoder = this.basisModule.BasisLzEtc1sImageTranscoder;
  118. var UastcImageTranscoder = this.basisModule.UastcImageTranscoder;
  119. var TextureFormat = this.basisModule.TextureFormat;
  120. var ktx = new KTX2Container( this.basisModule, buffer );
  121. // TODO(donmccurdy): Should test if texture is transcodable before attempting
  122. // any transcoding. If supercompressionScheme is KTX_SS_BASIS_LZ and dfd
  123. // colorModel is ETC1S (163) or if dfd colorModel is UASTCF (166)
  124. // then texture must be transcoded.
  125. var transcoder = ktx.getTexFormat() === TextureFormat.UASTC4x4
  126. ? new UastcImageTranscoder()
  127. : new BasisLzEtc1sImageTranscoder();
  128. ktx.initMipmaps( transcoder, this.transcoderConfig )
  129. .then( function () {
  130. var texture = new CompressedTexture(
  131. ktx.mipmaps,
  132. ktx.getWidth(),
  133. ktx.getHeight(),
  134. ktx.transcodedFormat,
  135. UnsignedByteType
  136. );
  137. texture.encoding = ktx.getEncoding();
  138. texture.premultiplyAlpha = ktx.getPremultiplyAlpha();
  139. texture.minFilter = ktx.mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
  140. texture.magFilter = LinearFilter;
  141. onLoad( texture );
  142. } )
  143. .catch( onError );
  144. return this;
  145. }
  146. }
  147. class KTX2Container {
  148. constructor( basisModule, arrayBuffer ) {
  149. this.basisModule = basisModule;
  150. this.arrayBuffer = arrayBuffer;
  151. this.zstd = new ZSTDDecoder();
  152. this.zstd.init();
  153. this.mipmaps = null;
  154. this.transcodedFormat = null;
  155. // Confirm this is a KTX 2.0 file, based on the identifier in the first 12 bytes.
  156. var idByteLength = 12;
  157. var id = new Uint8Array( this.arrayBuffer, 0, idByteLength );
  158. if ( id[ 0 ] !== 0xAB || // '´'
  159. id[ 1 ] !== 0x4B || // 'K'
  160. id[ 2 ] !== 0x54 || // 'T'
  161. id[ 3 ] !== 0x58 || // 'X'
  162. id[ 4 ] !== 0x20 || // ' '
  163. id[ 5 ] !== 0x32 || // '2'
  164. id[ 6 ] !== 0x30 || // '0'
  165. id[ 7 ] !== 0xBB || // 'ª'
  166. id[ 8 ] !== 0x0D || // '\r'
  167. id[ 9 ] !== 0x0A || // '\n'
  168. id[ 10 ] !== 0x1A || // '\x1A'
  169. id[ 11 ] !== 0x0A // '\n'
  170. ) {
  171. throw new Error( 'THREE.KTX2Loader: Missing KTX 2.0 identifier.' );
  172. }
  173. // TODO(donmccurdy): If we need to support BE, derive this from typeSize.
  174. var littleEndian = true;
  175. ///////////////////////////////////////////////////
  176. // Header.
  177. ///////////////////////////////////////////////////
  178. var headerByteLength = 17 * Uint32Array.BYTES_PER_ELEMENT;
  179. var headerReader = new KTX2BufferReader( this.arrayBuffer, idByteLength, headerByteLength, littleEndian );
  180. this.header = {
  181. vkFormat: headerReader.nextUint32(),
  182. typeSize: headerReader.nextUint32(),
  183. pixelWidth: headerReader.nextUint32(),
  184. pixelHeight: headerReader.nextUint32(),
  185. pixelDepth: headerReader.nextUint32(),
  186. arrayElementCount: headerReader.nextUint32(),
  187. faceCount: headerReader.nextUint32(),
  188. levelCount: headerReader.nextUint32(),
  189. supercompressionScheme: headerReader.nextUint32(),
  190. dfdByteOffset: headerReader.nextUint32(),
  191. dfdByteLength: headerReader.nextUint32(),
  192. kvdByteOffset: headerReader.nextUint32(),
  193. kvdByteLength: headerReader.nextUint32(),
  194. sgdByteOffset: headerReader.nextUint64(),
  195. sgdByteLength: headerReader.nextUint64(),
  196. };
  197. if ( this.header.pixelDepth > 0 ) {
  198. throw new Error( 'THREE.KTX2Loader: Only 2D textures are currently supported.' );
  199. }
  200. if ( this.header.arrayElementCount > 1 ) {
  201. throw new Error( 'THREE.KTX2Loader: Array textures are not currently supported.' );
  202. }
  203. if ( this.header.faceCount > 1 ) {
  204. throw new Error( 'THREE.KTX2Loader: Cube textures are not currently supported.' );
  205. }
  206. ///////////////////////////////////////////////////
  207. // Level index
  208. ///////////////////////////////////////////////////
  209. var levelByteLength = this.header.levelCount * 3 * 8;
  210. var levelReader = new KTX2BufferReader( this.arrayBuffer, idByteLength + headerByteLength, levelByteLength, littleEndian );
  211. this.levels = [];
  212. for ( var i = 0; i < this.header.levelCount; i ++ ) {
  213. this.levels.push( {
  214. byteOffset: levelReader.nextUint64(),
  215. byteLength: levelReader.nextUint64(),
  216. uncompressedByteLength: levelReader.nextUint64(),
  217. } );
  218. }
  219. ///////////////////////////////////////////////////
  220. // Data Format Descriptor (DFD)
  221. ///////////////////////////////////////////////////
  222. var dfdReader = new KTX2BufferReader(
  223. this.arrayBuffer,
  224. this.header.dfdByteOffset,
  225. this.header.dfdByteLength,
  226. littleEndian
  227. );
  228. const sampleStart = 6;
  229. const sampleWords = 4;
  230. this.dfd = {
  231. vendorId: dfdReader.skip( 4 /* totalSize */ ).nextUint16(),
  232. versionNumber: dfdReader.skip( 2 /* descriptorType */ ).nextUint16(),
  233. descriptorBlockSize: dfdReader.nextUint16(),
  234. colorModel: dfdReader.nextUint8(),
  235. colorPrimaries: dfdReader.nextUint8(),
  236. transferFunction: dfdReader.nextUint8(),
  237. flags: dfdReader.nextUint8(),
  238. texelBlockDimension: {
  239. x: dfdReader.nextUint8() + 1,
  240. y: dfdReader.nextUint8() + 1,
  241. z: dfdReader.nextUint8() + 1,
  242. w: dfdReader.nextUint8() + 1,
  243. },
  244. bytesPlane0: dfdReader.nextUint8(),
  245. numSamples: 0,
  246. samples: [],
  247. };
  248. this.dfd.numSamples = ( this.dfd.descriptorBlockSize / 4 - sampleStart ) / sampleWords;
  249. dfdReader.skip( 7 /* bytesPlane[1-7] */ );
  250. for ( var i = 0; i < this.dfd.numSamples; i ++ ) {
  251. this.dfd.samples[ i ] = {
  252. channelID: dfdReader.skip( 3 /* bitOffset + bitLength */ ).nextUint8(),
  253. // ... remainder not implemented.
  254. };
  255. dfdReader.skip( 12 /* samplePosition[0-3], lower, upper */ );
  256. }
  257. if ( this.header.vkFormat !== 0x00 /* VK_FORMAT_UNDEFINED */ &&
  258. ! ( this.header.supercompressionScheme === 1 /* BasisLZ */ ||
  259. this.dfd.colorModel === DFDModel.UASTC ) ) {
  260. throw new Error( 'THREE.KTX2Loader: Only Basis Universal supercompression is currently supported.' );
  261. }
  262. ///////////////////////////////////////////////////
  263. // Key/Value Data (KVD)
  264. ///////////////////////////////////////////////////
  265. // Not implemented.
  266. this.kvd = {};
  267. ///////////////////////////////////////////////////
  268. // Supercompression Global Data (SGD)
  269. ///////////////////////////////////////////////////
  270. this.sgd = {};
  271. if ( this.header.sgdByteLength <= 0 ) return;
  272. var sgdReader = new KTX2BufferReader(
  273. this.arrayBuffer,
  274. this.header.sgdByteOffset,
  275. this.header.sgdByteLength,
  276. littleEndian
  277. );
  278. this.sgd.endpointCount = sgdReader.nextUint16();
  279. this.sgd.selectorCount = sgdReader.nextUint16();
  280. this.sgd.endpointsByteLength = sgdReader.nextUint32();
  281. this.sgd.selectorsByteLength = sgdReader.nextUint32();
  282. this.sgd.tablesByteLength = sgdReader.nextUint32();
  283. this.sgd.extendedByteLength = sgdReader.nextUint32();
  284. this.sgd.imageDescs = [];
  285. this.sgd.endpointsData = null;
  286. this.sgd.selectorsData = null;
  287. this.sgd.tablesData = null;
  288. this.sgd.extendedData = null;
  289. for ( var i = 0; i < this.header.levelCount; i ++ ) {
  290. this.sgd.imageDescs.push( {
  291. imageFlags: sgdReader.nextUint32(),
  292. rgbSliceByteOffset: sgdReader.nextUint32(),
  293. rgbSliceByteLength: sgdReader.nextUint32(),
  294. alphaSliceByteOffset: sgdReader.nextUint32(),
  295. alphaSliceByteLength: sgdReader.nextUint32(),
  296. } );
  297. }
  298. var endpointsByteOffset = this.header.sgdByteOffset + sgdReader.offset;
  299. var selectorsByteOffset = endpointsByteOffset + this.sgd.endpointsByteLength;
  300. var tablesByteOffset = selectorsByteOffset + this.sgd.selectorsByteLength;
  301. var extendedByteOffset = tablesByteOffset + this.sgd.tablesByteLength;
  302. this.sgd.endpointsData = new Uint8Array( this.arrayBuffer, endpointsByteOffset, this.sgd.endpointsByteLength );
  303. this.sgd.selectorsData = new Uint8Array( this.arrayBuffer, selectorsByteOffset, this.sgd.selectorsByteLength );
  304. this.sgd.tablesData = new Uint8Array( this.arrayBuffer, tablesByteOffset, this.sgd.tablesByteLength );
  305. this.sgd.extendedData = new Uint8Array( this.arrayBuffer, extendedByteOffset, this.sgd.extendedByteLength );
  306. }
  307. async initMipmaps( transcoder, config ) {
  308. await this.zstd.init();
  309. var TranscodeTarget = this.basisModule.TranscodeTarget;
  310. var TextureFormat = this.basisModule.TextureFormat;
  311. var ImageInfo = this.basisModule.ImageInfo;
  312. var scope = this;
  313. var mipmaps = [];
  314. var width = this.getWidth();
  315. var height = this.getHeight();
  316. var texFormat = this.getTexFormat();
  317. var hasAlpha = this.getAlpha();
  318. var isVideo = false;
  319. // PVRTC1 transcoders (from both ETC1S and UASTC) only support power of 2 dimensions.
  320. var pvrtcTranscodable = MathUtils.isPowerOfTwo( width ) && MathUtils.isPowerOfTwo( height );
  321. if ( texFormat === TextureFormat.ETC1S ) {
  322. var numEndpoints = this.sgd.endpointCount;
  323. var numSelectors = this.sgd.selectorCount;
  324. var endpoints = this.sgd.endpointsData;
  325. var selectors = this.sgd.selectorsData;
  326. var tables = this.sgd.tablesData;
  327. transcoder.decodePalettes( numEndpoints, endpoints, numSelectors, selectors );
  328. transcoder.decodeTables( tables );
  329. }
  330. var targetFormat;
  331. if ( config.astcSupported ) {
  332. targetFormat = TranscodeTarget.ASTC_4x4_RGBA;
  333. this.transcodedFormat = RGBA_ASTC_4x4_Format;
  334. } else if ( config.bptcSupported && texFormat === TextureFormat.UASTC4x4 ) {
  335. targetFormat = TranscodeTarget.BC7_M5_RGBA;
  336. this.transcodedFormat = RGBA_BPTC_Format;
  337. } else if ( config.dxtSupported ) {
  338. targetFormat = hasAlpha ? TranscodeTarget.BC3_RGBA : TranscodeTarget.BC1_RGB;
  339. this.transcodedFormat = hasAlpha ? RGBA_S3TC_DXT5_Format : RGB_S3TC_DXT1_Format;
  340. } else if ( config.pvrtcSupported && pvrtcTranscodable ) {
  341. targetFormat = hasAlpha ? TranscodeTarget.PVRTC1_4_RGBA : TranscodeTarget.PVRTC1_4_RGB;
  342. this.transcodedFormat = hasAlpha ? RGBA_PVRTC_4BPPV1_Format : RGB_PVRTC_4BPPV1_Format;
  343. } else if ( config.etc2Supported ) {
  344. targetFormat = hasAlpha ? TranscodeTarget.ETC2_RGBA : TranscodeTarget.ETC1_RGB/* subset of ETC2 */;
  345. this.transcodedFormat = hasAlpha ? RGBA_ETC2_EAC_Format : RGB_ETC2_Format;
  346. } else if ( config.etc1Supported ) {
  347. targetFormat = TranscodeTarget.ETC1_RGB;
  348. this.transcodedFormat = RGB_ETC1_Format;
  349. } else {
  350. console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' );
  351. targetFormat = TranscodeTarget.RGBA32;
  352. this.transcodedFormat = RGBAFormat;
  353. }
  354. if ( ! this.basisModule.isFormatSupported( targetFormat, texFormat ) ) {
  355. throw new Error( 'THREE.KTX2Loader: Selected texture format not supported by current transcoder build.' );
  356. }
  357. var imageDescIndex = 0;
  358. for ( var level = 0; level < this.header.levelCount; level ++ ) {
  359. var levelWidth = width / Math.pow( 2, level );
  360. var levelHeight = height / Math.pow( 2, level );
  361. var numImagesInLevel = 1; // TODO(donmccurdy): Support cubemaps, arrays and 3D.
  362. var imageOffsetInLevel = 0;
  363. var imageInfo = new ImageInfo( texFormat, levelWidth, levelHeight, level );
  364. var levelByteLength = this.levels[ level ].byteLength;
  365. var levelUncompressedByteLength = this.levels[ level ].uncompressedByteLength;
  366. for ( var imageIndex = 0; imageIndex < numImagesInLevel; imageIndex ++ ) {
  367. var result;
  368. var encodedData;
  369. if ( texFormat === TextureFormat.UASTC4x4 ) {
  370. // UASTC
  371. imageInfo.flags = 0;
  372. imageInfo.rgbByteOffset = 0;
  373. imageInfo.rgbByteLength = levelUncompressedByteLength;
  374. imageInfo.alphaByteOffset = 0;
  375. imageInfo.alphaByteLength = 0;
  376. encodedData = new Uint8Array( this.arrayBuffer, this.levels[ level ].byteOffset + imageOffsetInLevel, levelByteLength );
  377. if ( this.header.supercompressionScheme === 2 /* ZSTD */ ) {
  378. encodedData = this.zstd.decode( encodedData, levelUncompressedByteLength );
  379. }
  380. result = transcoder.transcodeImage( targetFormat, encodedData, imageInfo, 0, hasAlpha, isVideo );
  381. } else {
  382. // ETC1S
  383. var imageDesc = this.sgd.imageDescs[ imageDescIndex ++ ];
  384. imageInfo.flags = imageDesc.imageFlags;
  385. imageInfo.rgbByteOffset = 0;
  386. imageInfo.rgbByteLength = imageDesc.rgbSliceByteLength;
  387. imageInfo.alphaByteOffset = imageDesc.alphaSliceByteOffset > 0 ? imageDesc.rgbSliceByteLength : 0;
  388. imageInfo.alphaByteLength = imageDesc.alphaSliceByteLength;
  389. encodedData = new Uint8Array( this.arrayBuffer, this.levels[ level ].byteOffset + imageDesc.rgbSliceByteOffset, imageDesc.rgbSliceByteLength + imageDesc.alphaSliceByteLength );
  390. result = transcoder.transcodeImage( targetFormat, encodedData, imageInfo, 0, isVideo );
  391. }
  392. if ( result.transcodedImage === undefined ) {
  393. throw new Error( 'THREE.KTX2Loader: Unable to transcode image.' );
  394. }
  395. // Transcoded image is written in memory allocated by WASM. We could avoid copying
  396. // the image by waiting until the image is uploaded to the GPU, then calling
  397. // delete(). However, (1) we don't know if the user will later need to re-upload it
  398. // e.g. after calling texture.clone(), and (2) this code will eventually be in a
  399. // Web Worker, and transferring WASM's memory seems like a very bad idea.
  400. var levelData = result.transcodedImage.get_typed_memory_view().slice();
  401. result.transcodedImage.delete();
  402. mipmaps.push( { data: levelData, width: levelWidth, height: levelHeight } );
  403. imageOffsetInLevel += levelByteLength;
  404. }
  405. }
  406. scope.mipmaps = mipmaps;
  407. }
  408. getWidth() {
  409. return this.header.pixelWidth;
  410. }
  411. getHeight() {
  412. return this.header.pixelHeight;
  413. }
  414. getEncoding() {
  415. return this.dfd.transferFunction === 2 /* KHR_DF_TRANSFER_SRGB */
  416. ? sRGBEncoding
  417. : LinearEncoding;
  418. }
  419. getTexFormat() {
  420. var TextureFormat = this.basisModule.TextureFormat;
  421. return this.dfd.colorModel === DFDModel.UASTC ? TextureFormat.UASTC4x4 : TextureFormat.ETC1S;
  422. }
  423. getAlpha() {
  424. var TextureFormat = this.basisModule.TextureFormat;
  425. // TODO(donmccurdy): Handle all channelIDs (i.e. the R & R+G cases),
  426. // choosing appropriate transcode target formats or providing queries
  427. // for applications so they know what to do with the content.
  428. if ( this.getTexFormat() === TextureFormat.UASTC4x4 ) {
  429. // UASTC
  430. if ( ( this.dfd.samples[ 0 ].channelID & 0xF ) === DFDChannel.UASTC.RGBA ) {
  431. return true;
  432. }
  433. return false;
  434. }
  435. // ETC1S
  436. if ( this.dfd.numSamples === 2 && ( this.dfd.samples[ 1 ].channelID & 0xF ) === DFDChannel.ETC1S.AAA ) {
  437. return true;
  438. }
  439. return false;
  440. }
  441. getPremultiplyAlpha() {
  442. return !! ( this.dfd.flags & 1 /* KHR_DF_FLAG_ALPHA_PREMULTIPLIED */ );
  443. }
  444. }
  445. class KTX2BufferReader {
  446. constructor( arrayBuffer, byteOffset, byteLength, littleEndian ) {
  447. this.dataView = new DataView( arrayBuffer, byteOffset, byteLength );
  448. this.littleEndian = littleEndian;
  449. this.offset = 0;
  450. }
  451. nextUint8() {
  452. var value = this.dataView.getUint8( this.offset, this.littleEndian );
  453. this.offset += 1;
  454. return value;
  455. }
  456. nextUint16() {
  457. var value = this.dataView.getUint16( this.offset, this.littleEndian );
  458. this.offset += 2;
  459. return value;
  460. }
  461. nextUint32() {
  462. var value = this.dataView.getUint32( this.offset, this.littleEndian );
  463. this.offset += 4;
  464. return value;
  465. }
  466. nextUint64() {
  467. // https://stackoverflow.com/questions/53103695/
  468. var left = this.dataView.getUint32( this.offset, this.littleEndian );
  469. var right = this.dataView.getUint32( this.offset + 4, this.littleEndian );
  470. var value = this.littleEndian ? left + ( 2 ** 32 * right ) : ( 2 ** 32 * left ) + right;
  471. if ( ! Number.isSafeInteger( value ) ) {
  472. console.warn( 'THREE.KTX2Loader: ' + value + ' exceeds MAX_SAFE_INTEGER. Precision may be lost.' );
  473. }
  474. this.offset += 8;
  475. return value;
  476. }
  477. skip( bytes ) {
  478. this.offset += bytes;
  479. return this;
  480. }
  481. }
  482. export { KTX2Loader };