DataTextureLoader.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { LinearFilter, LinearMipmapLinearFilter, ClampToEdgeWrapping } from '../constants.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { DataTexture } from '../textures/DataTexture.js';
  4. import { Loader } from './Loader.js';
  5. /**
  6. * @author Nikos M. / https://github.com/foo123/
  7. *
  8. * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
  9. *
  10. * Sub classes have to implement the parse() method which will be used in load().
  11. */
  12. function DataTextureLoader( manager ) {
  13. Loader.call( this, manager );
  14. }
  15. DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  16. constructor: DataTextureLoader,
  17. load: function ( url, onLoad, onProgress, onError ) {
  18. var scope = this;
  19. var texture = new DataTexture();
  20. var loader = new FileLoader( this.manager );
  21. loader.setResponseType( 'arraybuffer' );
  22. loader.setPath( this.path );
  23. loader.load( url, function ( buffer ) {
  24. var texData = scope.parse( buffer );
  25. if ( ! texData ) return;
  26. if ( texData.image !== undefined ) {
  27. texture.image = texData.image;
  28. } else if ( texData.data !== undefined ) {
  29. texture.image.width = texData.width;
  30. texture.image.height = texData.height;
  31. texture.image.data = texData.data;
  32. }
  33. texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
  34. texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
  35. texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
  36. texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearMipmapLinearFilter;
  37. texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
  38. if ( texData.format !== undefined ) {
  39. texture.format = texData.format;
  40. }
  41. if ( texData.type !== undefined ) {
  42. texture.type = texData.type;
  43. }
  44. if ( texData.mipmaps !== undefined ) {
  45. texture.mipmaps = texData.mipmaps;
  46. }
  47. if ( texData.mipmapCount === 1 ) {
  48. texture.minFilter = LinearFilter;
  49. }
  50. texture.needsUpdate = true;
  51. if ( onLoad ) onLoad( texture, texData );
  52. }, onProgress, onError );
  53. return texture;
  54. }
  55. } );
  56. export { DataTextureLoader };
粤ICP备19079148号