DataTextureLoader.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 : 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 };