KTXLoader.js 6.8 KB

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