KTX2Loader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Loader for KTX 2.0 GPU Texture containers.
  3. *
  4. * KTX 2.0 is a container format for various GPU texture formats. The loader
  5. * supports Basis Universal GPU textures, which can be quickly transcoded to
  6. * a wide variety of GPU texture compression formats. While KTX 2.0 also allows
  7. * other hardware-specific formats, this loader does not yet parse them.
  8. *
  9. * This loader parses the KTX 2.0 container and then relies on
  10. * THREE.BasisTextureLoader to complete the transcoding process.
  11. *
  12. * References:
  13. * - KTX: http://github.khronos.org/KTX-Specification/
  14. * - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
  15. */
  16. import {
  17. CompressedTexture,
  18. CompressedTextureLoader,
  19. FileLoader,
  20. LinearEncoding,
  21. sRGBEncoding,
  22. } from '../../../build/three.module.js';
  23. import {
  24. read as readKTX,
  25. KTX2ChannelETC1S,
  26. KTX2ChannelUASTC,
  27. KTX2Flags,
  28. KTX2Model,
  29. KTX2SupercompressionScheme,
  30. KTX2Transfer
  31. } from '../libs/ktx-parse.module.js';
  32. import { BasisTextureLoader } from './BasisTextureLoader.js';
  33. import { ZSTDDecoder } from '../libs/zstddec.module.js';
  34. class KTX2Loader extends CompressedTextureLoader {
  35. constructor( manager ) {
  36. super( manager );
  37. this.basisLoader = new BasisTextureLoader( manager );
  38. this.zstd = new ZSTDDecoder();
  39. this.zstd.init();
  40. if ( typeof MSC_TRANSCODER !== 'undefined' ) {
  41. console.warn(
  42. 'THREE.KTX2Loader: Please update to latest "basis_transcoder".'
  43. + ' "msc_basis_transcoder" is no longer supported in three.js r125+.'
  44. );
  45. }
  46. }
  47. setTranscoderPath( path ) {
  48. this.basisLoader.setTranscoderPath( path );
  49. return this;
  50. }
  51. setWorkerLimit( path ) {
  52. this.basisLoader.setWorkerLimit( path );
  53. return this;
  54. }
  55. detectSupport( renderer ) {
  56. this.basisLoader.detectSupport( renderer );
  57. return this;
  58. }
  59. dispose() {
  60. this.basisLoader.dispose();
  61. return this;
  62. }
  63. load( url, onLoad, onProgress, onError ) {
  64. var scope = this;
  65. var texture = new CompressedTexture();
  66. var bufferPending = new Promise( function ( resolve, reject ) {
  67. new FileLoader( scope.manager )
  68. .setPath( scope.path )
  69. .setResponseType( 'arraybuffer' )
  70. .load( url, resolve, onProgress, reject );
  71. } );
  72. bufferPending
  73. .then( function ( buffer ) {
  74. scope.parse( buffer, function ( _texture ) {
  75. texture.copy( _texture );
  76. texture.needsUpdate = true;
  77. if ( onLoad ) onLoad( texture );
  78. }, onError );
  79. } )
  80. .catch( onError );
  81. return texture;
  82. }
  83. parse( buffer, onLoad, onError ) {
  84. var scope = this;
  85. var ktx = readKTX( new Uint8Array( buffer ) );
  86. if ( ktx.pixelDepth > 0 ) {
  87. throw new Error( 'THREE.KTX2Loader: Only 2D textures are currently supported.' );
  88. }
  89. if ( ktx.layerCount > 1 ) {
  90. throw new Error( 'THREE.KTX2Loader: Array textures are not currently supported.' );
  91. }
  92. if ( ktx.faceCount > 1 ) {
  93. throw new Error( 'THREE.KTX2Loader: Cube textures are not currently supported.' );
  94. }
  95. var dfd = KTX2Utils.getBasicDFD( ktx );
  96. KTX2Utils.createLevels( ktx, this.zstd ).then( function ( levels ) {
  97. var basisFormat = dfd.colorModel === KTX2Model.UASTC
  98. ? BasisTextureLoader.BasisFormat.UASTC_4x4
  99. : BasisTextureLoader.BasisFormat.ETC1S;
  100. var parseConfig = {
  101. levels: levels,
  102. width: ktx.pixelWidth,
  103. height: ktx.pixelHeight,
  104. basisFormat: basisFormat,
  105. hasAlpha: KTX2Utils.getAlpha( ktx ),
  106. };
  107. if ( basisFormat === BasisTextureLoader.BasisFormat.ETC1S ) {
  108. parseConfig.globalData = ktx.globalData;
  109. }
  110. return scope.basisLoader.parseInternalAsync( parseConfig );
  111. } ).then( function ( texture ) {
  112. texture.encoding = dfd.transferFunction === KTX2Transfer.SRGB
  113. ? sRGBEncoding
  114. : LinearEncoding;
  115. texture.premultiplyAlpha = KTX2Utils.getPremultiplyAlpha( ktx );
  116. onLoad( texture );
  117. } ).catch( onError );
  118. return this;
  119. }
  120. }
  121. var KTX2Utils = {
  122. createLevels: async function ( ktx, zstd ) {
  123. if ( ktx.supercompressionScheme === KTX2SupercompressionScheme.ZSTD ) {
  124. await zstd.init();
  125. }
  126. var levels = [];
  127. var width = ktx.pixelWidth;
  128. var height = ktx.pixelHeight;
  129. for ( var levelIndex = 0; levelIndex < ktx.levels.length; levelIndex ++ ) {
  130. var levelWidth = Math.max( 1, Math.floor( width / Math.pow( 2, levelIndex ) ) );
  131. var levelHeight = Math.max( 1, Math.floor( height / Math.pow( 2, levelIndex ) ) );
  132. var levelData = ktx.levels[ levelIndex ].levelData;
  133. if ( ktx.supercompressionScheme === KTX2SupercompressionScheme.ZSTD ) {
  134. levelData = zstd.decode( levelData, ktx.levels[ levelIndex ].uncompressedByteLength );
  135. }
  136. levels.push( {
  137. index: levelIndex,
  138. width: levelWidth,
  139. height: levelHeight,
  140. data: levelData,
  141. } );
  142. }
  143. return levels;
  144. },
  145. getBasicDFD: function ( ktx ) {
  146. // Basic Data Format Descriptor Block is always the first DFD.
  147. return ktx.dataFormatDescriptor[ 0 ];
  148. },
  149. getAlpha: function ( ktx ) {
  150. var dfd = this.getBasicDFD( ktx );
  151. // UASTC
  152. if ( dfd.colorModel === KTX2Model.UASTC ) {
  153. if ( ( dfd.samples[ 0 ].channelID & 0xF ) === KTX2ChannelUASTC.RGBA ) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. // ETC1S
  159. if ( dfd.samples.length === 2
  160. && ( dfd.samples[ 1 ].channelID & 0xF ) === KTX2ChannelETC1S.AAA ) {
  161. return true;
  162. }
  163. return false;
  164. },
  165. getPremultiplyAlpha: function ( ktx ) {
  166. var dfd = this.getBasicDFD( ktx );
  167. return !! ( dfd.flags & KTX2Flags.ALPHA_PREMULTIPLIED );
  168. },
  169. };
  170. export { KTX2Loader };