2
0

KTX2Loader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 { BasisTextureLoader } from './BasisTextureLoader.js';
  24. import { ZSTDDecoder } from '../libs/zstddec.module.js';
  25. import { read as readKTX } from '../libs/ktx-parse.module.js';
  26. // KTX 2.0 constants.
  27. var DFDModel = {
  28. ETC1S: 163,
  29. UASTC: 166,
  30. };
  31. var DFDChannel = {
  32. ETC1S: {
  33. RGB: 0,
  34. RRR: 3,
  35. GGG: 4,
  36. AAA: 15,
  37. },
  38. UASTC: {
  39. RGB: 0,
  40. RGBA: 3,
  41. RRR: 4,
  42. RRRG: 5
  43. },
  44. };
  45. var SupercompressionScheme = {
  46. ZSTD: 2
  47. };
  48. var Transfer = {
  49. SRGB: 2
  50. };
  51. //
  52. class KTX2Loader extends CompressedTextureLoader {
  53. constructor( manager ) {
  54. super( manager );
  55. this.basisLoader = new BasisTextureLoader( manager );
  56. this.zstd = new ZSTDDecoder();
  57. this.zstd.init();
  58. if ( typeof MSC_TRANSCODER !== 'undefined' ) {
  59. console.warn(
  60. 'THREE.KTX2Loader: Please update to latest "basis_transcoder".'
  61. + ' "msc_basis_transcoder" is no longer supported in three.js r125+.'
  62. );
  63. }
  64. }
  65. setTranscoderPath( path ) {
  66. this.basisLoader.setTranscoderPath( path );
  67. return this;
  68. }
  69. setWorkerLimit( path ) {
  70. this.basisLoader.setWorkerLimit( path );
  71. return this;
  72. }
  73. detectSupport( renderer ) {
  74. this.basisLoader.detectSupport( renderer );
  75. return this;
  76. }
  77. dispose() {
  78. this.basisLoader.dispose();
  79. return this;
  80. }
  81. load( url, onLoad, onProgress, onError ) {
  82. var scope = this;
  83. var texture = new CompressedTexture();
  84. var bufferPending = new Promise( function ( resolve, reject ) {
  85. new FileLoader( scope.manager )
  86. .setPath( scope.path )
  87. .setResponseType( 'arraybuffer' )
  88. .load( url, resolve, onProgress, reject );
  89. } );
  90. bufferPending
  91. .then( function ( buffer ) {
  92. scope.parse( buffer, function ( _texture ) {
  93. texture.copy( _texture );
  94. texture.needsUpdate = true;
  95. if ( onLoad ) onLoad( texture );
  96. }, onError );
  97. } )
  98. .catch( onError );
  99. return texture;
  100. }
  101. parse( buffer, onLoad, onError ) {
  102. var scope = this;
  103. var ktx = readKTX( new Uint8Array( buffer ) );
  104. if ( ktx.pixelDepth > 0 ) {
  105. throw new Error( 'THREE.KTX2Loader: Only 2D textures are currently supported.' );
  106. }
  107. if ( ktx.layerCount > 1 ) {
  108. throw new Error( 'THREE.KTX2Loader: Array textures are not currently supported.' );
  109. }
  110. if ( ktx.faceCount > 1 ) {
  111. throw new Error( 'THREE.KTX2Loader: Cube textures are not currently supported.' );
  112. }
  113. var dfd = KTX2Utils.getBasicDFD( ktx );
  114. KTX2Utils.createLevels( ktx, this.zstd ).then( function ( levels ) {
  115. var basisFormat = dfd.colorModel === DFDModel.UASTC
  116. ? BasisTextureLoader.BasisFormat.UASTC_4x4
  117. : BasisTextureLoader.BasisFormat.ETC1S;
  118. var parseConfig = {
  119. levels: levels,
  120. width: ktx.pixelWidth,
  121. height: ktx.pixelHeight,
  122. basisFormat: basisFormat,
  123. hasAlpha: KTX2Utils.getAlpha( ktx ),
  124. };
  125. if ( basisFormat === BasisTextureLoader.BasisFormat.ETC1S ) {
  126. parseConfig.globalData = ktx.globalData;
  127. }
  128. return scope.basisLoader.parseInternalAsync( parseConfig );
  129. } ).then( function ( texture ) {
  130. texture.encoding = dfd.transferFunction === Transfer.SRGB
  131. ? sRGBEncoding
  132. : LinearEncoding;
  133. texture.premultiplyAlpha = KTX2Utils.getPremultiplyAlpha( ktx );
  134. onLoad( texture );
  135. } ).catch( onError );
  136. return this;
  137. }
  138. }
  139. var KTX2Utils = {
  140. createLevels: async function ( ktx, zstd ) {
  141. if ( ktx.supercompressionScheme === SupercompressionScheme.ZSTD ) {
  142. await zstd.init();
  143. }
  144. var levels = [];
  145. var width = ktx.pixelWidth;
  146. var height = ktx.pixelHeight;
  147. for ( var levelIndex = 0; levelIndex < ktx.levels.length; levelIndex ++ ) {
  148. var levelWidth = Math.max( 1, Math.floor( width / Math.pow( 2, levelIndex ) ) );
  149. var levelHeight = Math.max( 1, Math.floor( height / Math.pow( 2, levelIndex ) ) );
  150. var levelData = ktx.levels[ levelIndex ].levelData;
  151. if ( ktx.supercompressionScheme === SupercompressionScheme.ZSTD ) {
  152. levelData = zstd.decode( levelData, ktx.levels[ levelIndex ].uncompressedByteLength );
  153. }
  154. levels.push( {
  155. index: levelIndex,
  156. width: levelWidth,
  157. height: levelHeight,
  158. data: levelData,
  159. } );
  160. }
  161. return levels;
  162. },
  163. getBasicDFD: function ( ktx ) {
  164. // Basic Data Format Descriptor Block is always the first DFD.
  165. return ktx.dataFormatDescriptor[ 0 ];
  166. },
  167. getAlpha: function ( ktx ) {
  168. var dfd = this.getBasicDFD( ktx );
  169. // UASTC
  170. if ( dfd.colorModel === DFDModel.UASTC ) {
  171. if ( ( dfd.samples[ 0 ].channelID & 0xF ) === DFDChannel.UASTC.RGBA ) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. // ETC1S
  177. if ( dfd.samples.length === 2
  178. && ( dfd.samples[ 1 ].channelID & 0xF ) === DFDChannel.ETC1S.AAA ) {
  179. return true;
  180. }
  181. return false;
  182. },
  183. getPremultiplyAlpha: function ( ktx ) {
  184. var dfd = this.getBasicDFD( ktx );
  185. return !! ( dfd.flags & 1 /* KHR_DF_FLAG_ALPHA_PREMULTIPLIED */ );
  186. },
  187. };
  188. export { KTX2Loader };