KTXLoader.js 6.8 KB

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