DataTextureLoader.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
  7. *
  8. * Sub classes have to implement the parse() method which will be used in load().
  9. */
  10. function DataTextureLoader( manager ) {
  11. Loader.call( this, manager );
  12. }
  13. DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  14. constructor: DataTextureLoader,
  15. load: function ( url, onLoad, onProgress, onError ) {
  16. const scope = this;
  17. const texture = new DataTexture();
  18. const loader = new FileLoader( this.manager );
  19. loader.setResponseType( 'arraybuffer' );
  20. loader.setRequestHeader( this.requestHeader );
  21. loader.setPath( this.path );
  22. loader.setWithCredentials( scope.withCredentials );
  23. loader.load( url, function ( buffer ) {
  24. const 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 : LinearFilter;
  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. texture.minFilter = LinearMipmapLinearFilter; // presumably...
  47. }
  48. if ( texData.mipmapCount === 1 ) {
  49. texture.minFilter = LinearFilter;
  50. }
  51. texture.needsUpdate = true;
  52. if ( onLoad ) onLoad( texture, texData );
  53. }, onProgress, onError );
  54. return texture;
  55. }
  56. } );
  57. export { DataTextureLoader };
粤ICP备19079148号