CompressedTextureLoader.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * @author mrdoob / http://mrdoob.com/
  7. *
  8. * Abstract Base class to block based textures loader (dds, pvr, ...)
  9. */
  10. function CompressedTextureLoader( manager ) {
  11. Loader.call( this, manager );
  12. // override in sub classes
  13. this._parser = null;
  14. }
  15. CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  16. constructor: CompressedTextureLoader,
  17. load: function ( url, onLoad, onProgress, onError ) {
  18. var scope = this;
  19. var images = [];
  20. var texture = new CompressedTexture();
  21. texture.image = images;
  22. var loader = new FileLoader( this.manager );
  23. loader.setPath( this.path );
  24. loader.setResponseType( 'arraybuffer' );
  25. function loadTexture( i ) {
  26. loader.load( url[ i ], function ( buffer ) {
  27. var texDatas = scope._parser( buffer, true );
  28. images[ i ] = {
  29. width: texDatas.width,
  30. height: texDatas.height,
  31. format: texDatas.format,
  32. mipmaps: texDatas.mipmaps
  33. };
  34. loaded += 1;
  35. if ( loaded === 6 ) {
  36. if ( texDatas.mipmapCount === 1 )
  37. texture.minFilter = LinearFilter;
  38. texture.format = texDatas.format;
  39. texture.needsUpdate = true;
  40. if ( onLoad ) onLoad( texture );
  41. }
  42. }, onProgress, onError );
  43. }
  44. if ( Array.isArray( url ) ) {
  45. var loaded = 0;
  46. for ( var i = 0, il = url.length; i < il; ++ i ) {
  47. loadTexture( i );
  48. }
  49. } else {
  50. // compressed cubemap texture stored in a single DDS file
  51. loader.load( url, function ( buffer ) {
  52. var texDatas = scope._parser( buffer, true );
  53. if ( texDatas.isCubemap ) {
  54. var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  55. for ( var f = 0; f < faces; f ++ ) {
  56. images[ f ] = { mipmaps: [] };
  57. for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
  58. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  59. images[ f ].format = texDatas.format;
  60. images[ f ].width = texDatas.width;
  61. images[ f ].height = texDatas.height;
  62. }
  63. }
  64. } else {
  65. texture.image.width = texDatas.width;
  66. texture.image.height = texDatas.height;
  67. texture.mipmaps = texDatas.mipmaps;
  68. }
  69. if ( texDatas.mipmapCount === 1 ) {
  70. texture.minFilter = LinearFilter;
  71. }
  72. texture.format = texDatas.format;
  73. texture.needsUpdate = true;
  74. if ( onLoad ) onLoad( texture );
  75. }, onProgress, onError );
  76. }
  77. return texture;
  78. }
  79. } );
  80. export { CompressedTextureLoader };