CompressedTextureLoader.js 2.7 KB

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