UNPKG

1.32 kBJavaScriptView Raw
1/**
2 * @author mrdoob / http://mrdoob.com/
3 */
4
5import { RGBAFormat, RGBFormat } from '../constants.js';
6import { ImageLoader } from './ImageLoader.js';
7import { Texture } from '../textures/Texture.js';
8import { DefaultLoadingManager } from './LoadingManager.js';
9
10
11function TextureLoader( manager ) {
12
13 this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
14
15}
16
17Object.assign( TextureLoader.prototype, {
18
19 crossOrigin: 'Anonymous',
20
21 load: function ( url, onLoad, onProgress, onError ) {
22
23 var texture = new Texture();
24
25 var loader = new ImageLoader( this.manager );
26 loader.setCrossOrigin( this.crossOrigin );
27 loader.setPath( this.path );
28
29 loader.load( url, function ( image ) {
30
31 texture.image = image;
32
33 // JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
34 var isJPEG = url.search( /\.(jpg|jpeg)$/ ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;
35
36 texture.format = isJPEG ? RGBFormat : RGBAFormat;
37 texture.needsUpdate = true;
38
39 if ( onLoad !== undefined ) {
40
41 onLoad( texture );
42
43 }
44
45 }, onProgress, onError );
46
47 return texture;
48
49 },
50
51 setCrossOrigin: function ( value ) {
52
53 this.crossOrigin = value;
54 return this;
55
56 },
57
58 setPath: function ( value ) {
59
60 this.path = value;
61 return this;
62
63 }
64
65} );
66
67
68export { TextureLoader };