KTXLoader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {
  2. CompressedTextureLoader
  3. } from 'three';
  4. /**
  5. * A loader for the KTX texture compression format.
  6. *
  7. * References:
  8. * - [The KTX File Format and Tools]{@link https://www.khronos.org/opengles/sdk/tools/KTX/}
  9. * - [Babylon.JS khronosTextureContainer.ts]{@link https://github.com/BabylonJS/Babylon.js/blob/master/src/Misc/khronosTextureContainer.ts}
  10. *
  11. * ```js
  12. * const loader = new KTXLoader();
  13. *
  14. * const map = loader.load( 'textures/compressed/lensflare_ASTC8x8.ktx' )
  15. * map.colorSpace = THREE.SRGBColorSpace; // only for color textures
  16. * ```
  17. *
  18. * @augments CompressedTextureLoader
  19. */
  20. class KTXLoader extends CompressedTextureLoader {
  21. /**
  22. * Constructs a new KTX loader.
  23. *
  24. * @param {LoadingManager} [manager] - The loading manager.
  25. */
  26. constructor( manager ) {
  27. super( manager );
  28. }
  29. /**
  30. * Parses the given KTX texture data.
  31. *
  32. * @param {ArrayBuffer} buffer - The raw texture data.
  33. * @param {boolean} loadMipmaps - Whether to load mipmaps or not.
  34. * @return {CompressedTextureLoader~TexData} An object representing the parsed texture data.
  35. */
  36. parse( buffer, loadMipmaps ) {
  37. const ktx = new KhronosTextureContainer( buffer, 1 );
  38. return {
  39. mipmaps: ktx.mipmaps( loadMipmaps ),
  40. width: ktx.pixelWidth,
  41. height: ktx.pixelHeight,
  42. format: ktx.glInternalFormat,
  43. isCubemap: ktx.numberOfFaces === 6,
  44. mipmapCount: ktx.numberOfMipmapLevels
  45. };
  46. }
  47. }
  48. const HEADER_LEN = 12 + ( 13 * 4 ); // identifier + header elements (not including key value meta-data pairs)
  49. // load types
  50. const COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
  51. //const COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
  52. //const TEX_2D = 2; // uses a gl.texImage2D()
  53. //const TEX_3D = 3; // uses a gl.texImage3D()
  54. class KhronosTextureContainer {
  55. /**
  56. * @private
  57. * @param {ArrayBuffer} arrayBuffer - contents of the KTX container file
  58. * @param {number} facesExpected - should be either 1 or 6, based whether a cube texture or or
  59. * @param {boolean} threeDExpected - provision for indicating that data should be a 3D texture, not implemented
  60. * @param {boolean} textureArrayExpected - provision for indicating that data should be a texture array, not implemented
  61. */
  62. constructor( arrayBuffer, facesExpected /*, threeDExpected, textureArrayExpected */ ) {
  63. this.arrayBuffer = arrayBuffer;
  64. // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
  65. // '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\r', '\n', '\x1A', '\n'
  66. // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
  67. const identifier = new Uint8Array( this.arrayBuffer, 0, 12 );
  68. if ( identifier[ 0 ] !== 0xAB ||
  69. identifier[ 1 ] !== 0x4B ||
  70. identifier[ 2 ] !== 0x54 ||
  71. identifier[ 3 ] !== 0x58 ||
  72. identifier[ 4 ] !== 0x20 ||
  73. identifier[ 5 ] !== 0x31 ||
  74. identifier[ 6 ] !== 0x31 ||
  75. identifier[ 7 ] !== 0xBB ||
  76. identifier[ 8 ] !== 0x0D ||
  77. identifier[ 9 ] !== 0x0A ||
  78. identifier[ 10 ] !== 0x1A ||
  79. identifier[ 11 ] !== 0x0A ) {
  80. console.error( 'texture missing KTX identifier' );
  81. return;
  82. }
  83. // load the reset of the header in native 32 bit uint
  84. const dataSize = Uint32Array.BYTES_PER_ELEMENT;
  85. const headerDataView = new DataView( this.arrayBuffer, 12, 13 * dataSize );
  86. const endianness = headerDataView.getUint32( 0, true );
  87. const littleEndian = endianness === 0x04030201;
  88. this.glType = headerDataView.getUint32( 1 * dataSize, littleEndian ); // must be 0 for compressed textures
  89. this.glTypeSize = headerDataView.getUint32( 2 * dataSize, littleEndian ); // must be 1 for compressed textures
  90. this.glFormat = headerDataView.getUint32( 3 * dataSize, littleEndian ); // must be 0 for compressed textures
  91. this.glInternalFormat = headerDataView.getUint32( 4 * dataSize, littleEndian ); // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
  92. this.glBaseInternalFormat = headerDataView.getUint32( 5 * dataSize, littleEndian ); // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
  93. this.pixelWidth = headerDataView.getUint32( 6 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
  94. this.pixelHeight = headerDataView.getUint32( 7 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
  95. this.pixelDepth = headerDataView.getUint32( 8 * dataSize, littleEndian ); // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
  96. this.numberOfArrayElements = headerDataView.getUint32( 9 * dataSize, littleEndian ); // used for texture arrays
  97. this.numberOfFaces = headerDataView.getUint32( 10 * dataSize, littleEndian ); // used for cubemap textures, should either be 1 or 6
  98. this.numberOfMipmapLevels = headerDataView.getUint32( 11 * dataSize, littleEndian ); // number of levels; disregard possibility of 0 for compressed textures
  99. this.bytesOfKeyValueData = headerDataView.getUint32( 12 * dataSize, littleEndian ); // the amount of space after the header for meta-data
  100. // Make sure we have a compressed type. Not only reduces work, but probably better to let dev know they are not compressing.
  101. if ( this.glType !== 0 ) {
  102. console.warn( 'only compressed formats currently supported' );
  103. return;
  104. } else {
  105. // value of zero is an indication to generate mipmaps @ runtime. Not usually allowed for compressed, so disregard.
  106. this.numberOfMipmapLevels = Math.max( 1, this.numberOfMipmapLevels );
  107. }
  108. if ( this.pixelHeight === 0 || this.pixelDepth !== 0 ) {
  109. console.warn( 'only 2D textures currently supported' );
  110. return;
  111. }
  112. if ( this.numberOfArrayElements !== 0 ) {
  113. console.warn( 'texture arrays not currently supported' );
  114. return;
  115. }
  116. if ( this.numberOfFaces !== facesExpected ) {
  117. console.warn( 'number of faces expected' + facesExpected + ', but found ' + this.numberOfFaces );
  118. return;
  119. }
  120. // we now have a completely validated file, so could use existence of loadType as success
  121. // would need to make this more elaborate & adjust checks above to support more than one load type
  122. this.loadType = COMPRESSED_2D;
  123. }
  124. mipmaps( loadMipmaps ) {
  125. const mipmaps = [];
  126. // initialize width & height for level 1
  127. let dataOffset = HEADER_LEN + this.bytesOfKeyValueData;
  128. let width = this.pixelWidth;
  129. let height = this.pixelHeight;
  130. const mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
  131. for ( let level = 0; level < mipmapCount; level ++ ) {
  132. const imageSize = new Int32Array( this.arrayBuffer, dataOffset, 1 )[ 0 ]; // size per face, since not supporting array cubemaps
  133. dataOffset += 4; // size of the image + 4 for the imageSize field
  134. for ( let face = 0; face < this.numberOfFaces; face ++ ) {
  135. const byteArray = new Uint8Array( this.arrayBuffer, dataOffset, imageSize );
  136. mipmaps.push( { 'data': byteArray, 'width': width, 'height': height } );
  137. dataOffset += imageSize;
  138. dataOffset += 3 - ( ( imageSize + 3 ) % 4 ); // add padding for odd sized image
  139. }
  140. width = Math.max( 1.0, width * 0.5 );
  141. height = Math.max( 1.0, height * 0.5 );
  142. }
  143. return mipmaps;
  144. }
  145. }
  146. export { KTXLoader };
粤ICP备19079148号