KTX2Loader.js 19 KB

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