TextureLoader.js 1.2 KB

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