UNPKG

2.15 kBJavaScriptView Raw
1import { LinearFilter, LinearMipMapLinearFilter, ClampToEdgeWrapping } from '../constants.js';
2import { FileLoader } from './FileLoader.js';
3import { DataTexture } from '../textures/DataTexture.js';
4import { DefaultLoadingManager } from './LoadingManager.js';
5
6/**
7 * @author Nikos M. / https://github.com/foo123/
8 *
9 * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
10 */
11
12function DataTextureLoader( manager ) {
13
14 this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
15
16 // override in sub classes
17 this._parser = null;
18
19}
20
21Object.assign( DataTextureLoader.prototype, {
22
23 load: function ( url, onLoad, onProgress, onError ) {
24
25 var scope = this;
26
27 var texture = new DataTexture();
28
29 var loader = new FileLoader( this.manager );
30 loader.setResponseType( 'arraybuffer' );
31
32 loader.load( url, function ( buffer ) {
33
34 var texData = scope._parser( buffer );
35
36 if ( ! texData ) return;
37
38 if ( undefined !== texData.image ) {
39
40 texture.image = texData.image;
41
42 } else if ( undefined !== texData.data ) {
43
44 texture.image.width = texData.width;
45 texture.image.height = texData.height;
46 texture.image.data = texData.data;
47
48 }
49
50 texture.wrapS = undefined !== texData.wrapS ? texData.wrapS : ClampToEdgeWrapping;
51 texture.wrapT = undefined !== texData.wrapT ? texData.wrapT : ClampToEdgeWrapping;
52
53 texture.magFilter = undefined !== texData.magFilter ? texData.magFilter : LinearFilter;
54 texture.minFilter = undefined !== texData.minFilter ? texData.minFilter : LinearMipMapLinearFilter;
55
56 texture.anisotropy = undefined !== texData.anisotropy ? texData.anisotropy : 1;
57
58 if ( undefined !== texData.format ) {
59
60 texture.format = texData.format;
61
62 }
63 if ( undefined !== texData.type ) {
64
65 texture.type = texData.type;
66
67 }
68
69 if ( undefined !== texData.mipmaps ) {
70
71 texture.mipmaps = texData.mipmaps;
72
73 }
74
75 if ( 1 === texData.mipmapCount ) {
76
77 texture.minFilter = LinearFilter;
78
79 }
80
81 texture.needsUpdate = true;
82
83 if ( onLoad ) onLoad( texture, texData );
84
85 }, onProgress, onError );
86
87
88 return texture;
89
90 }
91
92} );
93
94
95export { DataTextureLoader };