CompressedTextureLoader.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 to block based textures loader (dds, pvr, ...)
  7. *
  8. * Sub classes have to implement the parse() method which will be used in load().
  9. */
  10. class CompressedTextureLoader extends Loader {
  11. constructor( manager ) {
  12. super( manager );
  13. }
  14. load( url, onLoad, onProgress, onError ) {
  15. const scope = this;
  16. const images = [];
  17. const texture = new CompressedTexture();
  18. const loader = new FileLoader( this.manager );
  19. loader.setPath( this.path );
  20. loader.setResponseType( 'arraybuffer' );
  21. loader.setRequestHeader( this.requestHeader );
  22. loader.setWithCredentials( scope.withCredentials );
  23. let loaded = 0;
  24. function loadTexture( i ) {
  25. loader.load( url[ i ], function ( buffer ) {
  26. const texDatas = scope.parse( 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 ) texture.minFilter = LinearFilter;
  36. texture.image = images;
  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. for ( let i = 0, il = url.length; i < il; ++ i ) {
  45. loadTexture( i );
  46. }
  47. } else {
  48. // compressed cubemap texture stored in a single DDS file
  49. loader.load( url, function ( buffer ) {
  50. const texDatas = scope.parse( buffer, true );
  51. if ( texDatas.isCubemap ) {
  52. const faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  53. for ( let f = 0; f < faces; f ++ ) {
  54. images[ f ] = { mipmaps: [] };
  55. for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {
  56. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  57. images[ f ].format = texDatas.format;
  58. images[ f ].width = texDatas.width;
  59. images[ f ].height = texDatas.height;
  60. }
  61. }
  62. texture.image = images;
  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. }
  79. export { CompressedTextureLoader };
粤ICP备19079148号