KTXLoader.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. THREE.KTXLoader = function () {
  10. this._parser = THREE.KTXLoader.parse;
  11. };
  12. THREE.KTXLoader.prototype = Object.create( THREE.CompressedTextureLoader.prototype );
  13. THREE.KTXLoader.prototype.constructor = THREE.KTXLoader;
  14. THREE.KTXLoader.parse = function ( buffer, loadMipmaps ) {
  15. var ktx = new KhronosTextureContainer( buffer, 1 );
  16. return {
  17. mipmaps: ktx.mipmaps( loadMipmaps ),
  18. width: ktx.pixelWidth,
  19. height: ktx.pixelHeight,
  20. format: ktx.glInternalFormat,
  21. isCubemap: ktx.numberOfFaces === 6,
  22. mipmapCount: ktx.numberOfMipmapLevels
  23. };
  24. };
  25. var KhronosTextureContainer = ( function () {
  26. /**
  27. * @param {ArrayBuffer} arrayBuffer- contents of the KTX container file
  28. * @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or
  29. * @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented
  30. * @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented
  31. */
  32. function KhronosTextureContainer( arrayBuffer, facesExpected, threeDExpected, textureArrayExpected ) {
  33. this.arrayBuffer = arrayBuffer;
  34. // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
  35. // '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\r', '\n', '\x1A', '\n'
  36. // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
  37. var identifier = new Uint8Array( this.arrayBuffer, 0, 12 );
  38. if ( identifier[ 0 ] !== 0xAB ||
  39. identifier[ 1 ] !== 0x4B ||
  40. identifier[ 2 ] !== 0x54 ||
  41. identifier[ 3 ] !== 0x58 ||
  42. identifier[ 4 ] !== 0x20 ||
  43. identifier[ 5 ] !== 0x31 ||
  44. identifier[ 6 ] !== 0x31 ||
  45. identifier[ 7 ] !== 0xBB ||
  46. identifier[ 8 ] !== 0x0D ||
  47. identifier[ 9 ] !== 0x0A ||
  48. identifier[ 10 ] !== 0x1A ||
  49. identifier[ 11 ] !== 0x0A ) {
  50. console.error( 'texture missing KTX identifier' );
  51. return;
  52. }
  53. // load the reset of the header in native 32 bit int
  54. var header = new Int32Array( this.arrayBuffer, 12, 13 );
  55. // determine of the remaining header values are recorded in the opposite endianness & require conversion
  56. var oppositeEndianess = header[ 0 ] === 0x01020304;
  57. // read all the header elements in order they exist in the file, without modification (sans endainness)
  58. this.glType = oppositeEndianess ? this.switchEndainness( header[ 1 ] ) : header[ 1 ]; // must be 0 for compressed textures
  59. this.glTypeSize = oppositeEndianess ? this.switchEndainness( header[ 2 ] ) : header[ 2 ]; // must be 1 for compressed textures
  60. this.glFormat = oppositeEndianess ? this.switchEndainness( header[ 3 ] ) : header[ 3 ]; // must be 0 for compressed textures
  61. this.glInternalFormat = oppositeEndianess ? this.switchEndainness( header[ 4 ] ) : header[ 4 ]; // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)
  62. this.glBaseInternalFormat = oppositeEndianess ? this.switchEndainness( header[ 5 ] ) : header[ 5 ]; // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)
  63. this.pixelWidth = oppositeEndianess ? this.switchEndainness( header[ 6 ] ) : header[ 6 ]; // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)
  64. this.pixelHeight = oppositeEndianess ? this.switchEndainness( header[ 7 ] ) : header[ 7 ]; // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)
  65. this.pixelDepth = oppositeEndianess ? this.switchEndainness( header[ 8 ] ) : header[ 8 ]; // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)
  66. this.numberOfArrayElements = oppositeEndianess ? this.switchEndainness( header[ 9 ] ) : header[ 9 ]; // used for texture arrays
  67. this.numberOfFaces = oppositeEndianess ? this.switchEndainness( header[ 10 ] ) : header[ 10 ]; // used for cubemap textures, should either be 1 or 6
  68. this.numberOfMipmapLevels = oppositeEndianess ? this.switchEndainness( header[ 11 ] ) : header[ 11 ]; // number of levels; disregard possibility of 0 for compressed textures
  69. this.bytesOfKeyValueData = oppositeEndianess ? this.switchEndainness( header[ 12 ] ) : header[ 12 ]; // the amount of space after the header for meta-data
  70. // Make sure we have a compressed type. Not only reduces work, but probably better to let dev know they are not compressing.
  71. if ( this.glType !== 0 ) {
  72. console.warn( 'only compressed formats currently supported' );
  73. return;
  74. } else {
  75. // value of zero is an indication to generate mipmaps @ runtime. Not usually allowed for compressed, so disregard.
  76. this.numberOfMipmapLevels = Math.max( 1, this.numberOfMipmapLevels );
  77. }
  78. if ( this.pixelHeight === 0 || this.pixelDepth !== 0 ) {
  79. console.warn( 'only 2D textures currently supported' );
  80. return;
  81. }
  82. if ( this.numberOfArrayElements !== 0 ) {
  83. console.warn( 'texture arrays not currently supported' );
  84. return;
  85. }
  86. if ( this.numberOfFaces !== facesExpected ) {
  87. console.warn( 'number of faces expected' + facesExpected + ', but found ' + this.numberOfFaces );
  88. return;
  89. }
  90. // we now have a completely validated file, so could use existence of loadType as success
  91. // would need to make this more elaborate & adjust checks above to support more than one load type
  92. this.loadType = KhronosTextureContainer.COMPRESSED_2D;
  93. }
  94. // not as fast hardware based, but will probably never need to use
  95. KhronosTextureContainer.prototype.switchEndainness = function ( val ) {
  96. return ( ( val & 0xFF ) << 24 ) | ( ( val & 0xFF00 ) << 8 ) | ( ( val >> 8 ) & 0xFF00 ) | ( ( val >> 24 ) & 0xFF );
  97. };
  98. // return mipmaps for THREE.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. for ( var face = 0; face < this.numberOfFaces; face ++ ) {
  109. var byteArray = new Uint8Array( this.arrayBuffer, dataOffset + 4, imageSize );
  110. mipmaps.push( { "data": byteArray, "width": width, "height": height } );
  111. dataOffset += imageSize + 4; // size of the image + 4 for the imageSize field
  112. dataOffset += 3 - ( ( imageSize + 3 ) % 4 ); // add padding for odd sized image
  113. }
  114. width = Math.max( 1.0, width * 0.5 );
  115. height = Math.max( 1.0, height * 0.5 );
  116. }
  117. return mipmaps;
  118. };
  119. KhronosTextureContainer.HEADER_LEN = 12 + ( 13 * 4 ); // identifier + header elements (not including key value meta-data pairs)
  120. // load types
  121. KhronosTextureContainer.COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
  122. KhronosTextureContainer.COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
  123. KhronosTextureContainer.TEX_2D = 2; // uses a gl.texImage2D()
  124. KhronosTextureContainer.TEX_3D = 3; // uses a gl.texImage3D()
  125. return KhronosTextureContainer;
  126. }() );