CompressedTextureLoader.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { LinearFilter } from '../constants.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { CompressedTexture } from '../textures/CompressedTexture.js';
  4. import { Loader } from './Loader.js';
  5. /**
  6. * Abstract base class for loading compressed texture formats S3TC, ASTC or ETC.
  7. * Textures are internally loaded via {@link FileLoader}.
  8. *
  9. * Derived classes have to implement the `parse()` method which holds the parsing
  10. * for the respective format.
  11. *
  12. * @abstract
  13. * @augments Loader
  14. */
  15. class CompressedTextureLoader extends Loader {
  16. /**
  17. * Constructs a new compressed texture loader.
  18. *
  19. * @param {LoadingManager} [manager] - The loading manager.
  20. */
  21. constructor( manager ) {
  22. super( manager );
  23. }
  24. /**
  25. * Starts loading from the given URL and passes the loaded compressed texture
  26. * to the `onLoad()` callback. The method also returns a new texture object which can
  27. * directly be used for material creation. If you do it this way, the texture
  28. * may pop up in your scene once the respective loading process is finished.
  29. *
  30. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  31. * @param {function(CompressedTexture)} onLoad - Executed when the loading process has been finished.
  32. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  33. * @param {onErrorCallback} onError - Executed when errors occur.
  34. * @return {CompressedTexture} The compressed texture.
  35. */
  36. load( url, onLoad, onProgress, onError ) {
  37. const scope = this;
  38. const images = [];
  39. const texture = new CompressedTexture();
  40. const loader = new FileLoader( this.manager );
  41. loader.setPath( this.path );
  42. loader.setResponseType( 'arraybuffer' );
  43. loader.setRequestHeader( this.requestHeader );
  44. loader.setWithCredentials( scope.withCredentials );
  45. let loaded = 0;
  46. function loadTexture( i ) {
  47. loader.load( url[ i ], function ( buffer ) {
  48. const texDatas = scope.parse( buffer, true );
  49. images[ i ] = {
  50. width: texDatas.width,
  51. height: texDatas.height,
  52. format: texDatas.format,
  53. mipmaps: texDatas.mipmaps
  54. };
  55. loaded += 1;
  56. if ( loaded === 6 ) {
  57. if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;
  58. texture.image = images;
  59. texture.format = texDatas.format;
  60. texture.needsUpdate = true;
  61. if ( onLoad ) onLoad( texture );
  62. }
  63. }, onProgress, onError );
  64. }
  65. if ( Array.isArray( url ) ) {
  66. for ( let i = 0, il = url.length; i < il; ++ i ) {
  67. loadTexture( i );
  68. }
  69. } else {
  70. // compressed cubemap texture stored in a single DDS file
  71. loader.load( url, function ( buffer ) {
  72. const texDatas = scope.parse( buffer, true );
  73. if ( texDatas.isCubemap ) {
  74. const faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  75. for ( let f = 0; f < faces; f ++ ) {
  76. images[ f ] = { mipmaps: [] };
  77. for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {
  78. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  79. images[ f ].format = texDatas.format;
  80. images[ f ].width = texDatas.width;
  81. images[ f ].height = texDatas.height;
  82. }
  83. }
  84. texture.image = images;
  85. } else {
  86. texture.image.width = texDatas.width;
  87. texture.image.height = texDatas.height;
  88. texture.mipmaps = texDatas.mipmaps;
  89. }
  90. if ( texDatas.mipmapCount === 1 ) {
  91. texture.minFilter = LinearFilter;
  92. }
  93. texture.format = texDatas.format;
  94. texture.needsUpdate = true;
  95. if ( onLoad ) onLoad( texture );
  96. }, onProgress, onError );
  97. }
  98. return texture;
  99. }
  100. }
  101. /**
  102. * Represents the result object type of the `parse()` method.
  103. *
  104. * @typedef {Object} CompressedTextureLoader~TexData
  105. * @property {number} width - The width of the base mip.
  106. * @property {number} height - The width of the base mip.
  107. * @property {boolean} isCubemap - Whether the data represent a cubemap or not.
  108. * @property {number} mipmapCount - The mipmap count.
  109. * @property {Array<{data:TypedArray,width:number,height:number}>} mipmaps - An array holding the mipmaps.
  110. * Each entry holds the data and the dimensions for each level.
  111. * @property {number} format - The texture format.
  112. **/
  113. export { CompressedTextureLoader };
粤ICP备19079148号