TextureLoader.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { RGBAFormat, RGBFormat } from '../constants.js';
  5. import { ImageLoader } from './ImageLoader.js';
  6. import { Texture } from '../textures/Texture.js';
  7. import { DefaultLoadingManager } from './LoadingManager.js';
  8. function TextureLoader( manager ) {
  9. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  10. }
  11. Object.assign( TextureLoader.prototype, {
  12. crossOrigin: 'anonymous',
  13. load: function ( url, onLoad, onProgress, onError ) {
  14. var texture = new Texture();
  15. var loader = new ImageLoader( this.manager );
  16. loader.setCrossOrigin( this.crossOrigin );
  17. loader.setPath( this.path );
  18. loader.load( url, function ( image ) {
  19. texture.image = image;
  20. // JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
  21. var isJPEG = url.search( /\.jpe?g$/i ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;
  22. texture.format = isJPEG ? RGBFormat : RGBAFormat;
  23. texture.needsUpdate = true;
  24. if ( onLoad !== undefined ) {
  25. onLoad( texture );
  26. }
  27. }, onProgress, onError );
  28. return texture;
  29. },
  30. setCrossOrigin: function ( value ) {
  31. this.crossOrigin = value;
  32. return this;
  33. },
  34. setPath: function ( value ) {
  35. this.path = value;
  36. return this;
  37. }
  38. } );
  39. export { TextureLoader };