comunicationLeap/clientLeap/libraries/MTLLoader.js
clientLeap/libraries/MTLLoader.jsRaw
1/**
2 * Loads a Wavefront .mtl file specifying materials
3 *
4 * @author angelxuanchang
5 */
6
7THREE.MTLLoader = function( manager ) {
8
9 this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
10
11};
12
13Object.assign( THREE.MTLLoader.prototype, THREE.EventDispatcher.prototype, {
14
15 /**
16 * Loads and parses a MTL asset from a URL.
17 *
18 * @param {String} url - URL to the MTL file.
19 * @param {Function} [onLoad] - Callback invoked with the loaded object.
20 * @param {Function} [onProgress] - Callback for download progress.
21 * @param {Function} [onError] - Callback for download errors.
22 *
23 * @see setPath setTexturePath
24 *
25 * @note In order for relative texture references to resolve correctly
26 * you must call setPath and/or setTexturePath explicitly prior to load.
27 */
28 load: function ( url, onLoad, onProgress, onError ) {
29
30 var scope = this;
31
32 var loader = new THREE.XHRLoader( this.manager );
33 loader.setPath( this.path );
34 loader.load( url, function ( text ) {
35
36 onLoad( scope.parse( text ) );
37
38 }, onProgress, onError );
39
40 },
41
42 /**
43 * Set base path for resolving references.
44 * If set this path will be prepended to each loaded and found reference.
45 *
46 * @see setTexturePath
47 * @param {String} path
48 *
49 * @example
50 * mtlLoader.setPath( 'assets/obj/' );
51 * mtlLoader.load( 'my.mtl', ... );
52 */
53 setPath: function ( path ) {
54
55 this.path = path;
56
57 },
58
59 /**
60 * Set base path for resolving texture references.
61 * If set this path will be prepended found texture reference.
62 * If not set and setPath is, it will be used as texture base path.
63 *
64 * @see setPath
65 * @param {String} path
66 *
67 * @example
68 * mtlLoader.setPath( 'assets/obj/' );
69 * mtlLoader.setTexturePath( 'assets/textures/' );
70 * mtlLoader.load( 'my.mtl', ... );
71 */
72 setTexturePath: function( path ) {
73
74 this.texturePath = path;
75
76 },
77
78 setBaseUrl: function( path ) {
79
80 console.warn( 'THREE.MTLLoader: .setBaseUrl() is deprecated. Use .setTexturePath( path ) for texture path or .setPath( path ) for general base path instead.' );
81
82 this.setTexturePath( path );
83
84 },
85
86 setCrossOrigin: function ( value ) {
87
88 this.crossOrigin = value;
89
90 },
91
92 setMaterialOptions: function ( value ) {
93
94 this.materialOptions = value;
95
96 },
97
98 /**
99 * Parses a MTL file.
100 *
101 * @param {String} text - Content of MTL file
102 * @return {THREE.MTLLoader.MaterialCreator}
103 *
104 * @see setPath setTexturePath
105 *
106 * @note In order for relative texture references to resolve correctly
107 * you must call setPath and/or setTexturePath explicitly prior to parse.
108 */
109 parse: function ( text ) {
110
111 var lines = text.split( '\n' );
112 var info = {};
113 var delimiter_pattern = /\s+/;
114 var materialsInfo = {};
115
116 for ( var i = 0; i < lines.length; i ++ ) {
117
118 var line = lines[ i ];
119 line = line.trim();
120
121 if ( line.length === 0 || line.charAt( 0 ) === '#' ) {
122
123 // Blank line or comment ignore
124 continue;
125
126 }
127
128 var pos = line.indexOf( ' ' );
129
130 var key = ( pos >= 0 ) ? line.substring( 0, pos ) : line;
131 key = key.toLowerCase();
132
133 var value = ( pos >= 0 ) ? line.substring( pos + 1 ) : '';
134 value = value.trim();
135
136 if ( key === 'newmtl' ) {
137
138 // New material
139
140 info = { name: value };
141 materialsInfo[ value ] = info;
142
143 } else if ( info ) {
144
145 if ( key === 'ka' || key === 'kd' || key === 'ks' ) {
146
147 var ss = value.split( delimiter_pattern, 3 );
148 info[ key ] = [ parseFloat( ss[ 0 ] ), parseFloat( ss[ 1 ] ), parseFloat( ss[ 2 ] ) ];
149
150 } else {
151
152 info[ key ] = value;
153
154 }
155
156 }
157
158 }
159
160 var materialCreator = new THREE.MTLLoader.MaterialCreator( this.texturePath || this.path, this.materialOptions );
161 materialCreator.setCrossOrigin( this.crossOrigin );
162 materialCreator.setManager( this.manager );
163 materialCreator.setMaterials( materialsInfo );
164 return materialCreator;
165
166 }
167
168} );
169
170/**
171 * Create a new THREE-MTLLoader.MaterialCreator
172 * @param baseUrl - Url relative to which textures are loaded
173 * @param options - Set of options on how to construct the materials
174 * side: Which side to apply the material
175 * THREE.FrontSide (default), THREE.BackSide, THREE.DoubleSide
176 * wrap: What type of wrapping to apply for textures
177 * THREE.RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping
178 * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255
179 * Default: false, assumed to be already normalized
180 * ignoreZeroRGBs: Ignore values of RGBs (Ka,Kd,Ks) that are all 0's
181 * Default: false
182 * @constructor
183 */
184
185THREE.MTLLoader.MaterialCreator = function( baseUrl, options ) {
186
187 this.baseUrl = baseUrl || '';
188 this.options = options;
189 this.materialsInfo = {};
190 this.materials = {};
191 this.materialsArray = [];
192 this.nameLookup = {};
193
194 this.side = ( this.options && this.options.side ) ? this.options.side : THREE.FrontSide;
195 this.wrap = ( this.options && this.options.wrap ) ? this.options.wrap : THREE.RepeatWrapping;
196
197};
198
199THREE.MTLLoader.MaterialCreator.prototype = {
200
201 constructor: THREE.MTLLoader.MaterialCreator,
202
203 setCrossOrigin: function ( value ) {
204
205 this.crossOrigin = value;
206
207 },
208
209 setManager: function ( value ) {
210
211 this.manager = value;
212
213 },
214
215 setMaterials: function( materialsInfo ) {
216
217 this.materialsInfo = this.convert( materialsInfo );
218 this.materials = {};
219 this.materialsArray = [];
220 this.nameLookup = {};
221
222 },
223
224 convert: function( materialsInfo ) {
225
226 if ( ! this.options ) return materialsInfo;
227
228 var converted = {};
229
230 for ( var mn in materialsInfo ) {
231
232 // Convert materials info into normalized form based on options
233
234 var mat = materialsInfo[ mn ];
235
236 var covmat = {};
237
238 converted[ mn ] = covmat;
239
240 for ( var prop in mat ) {
241
242 var save = true;
243 var value = mat[ prop ];
244 var lprop = prop.toLowerCase();
245
246 switch ( lprop ) {
247
248 case 'kd':
249 case 'ka':
250 case 'ks':
251
252 // Diffuse color (color under white light) using RGB values
253
254 if ( this.options && this.options.normalizeRGB ) {
255
256 value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ];
257
258 }
259
260 if ( this.options && this.options.ignoreZeroRGBs ) {
261
262 if ( value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 1 ] === 0 ) {
263
264 // ignore
265
266 save = false;
267
268 }
269
270 }
271
272 break;
273
274 default:
275
276 break;
277 }
278
279 if ( save ) {
280
281 covmat[ lprop ] = value;
282
283 }
284
285 }
286
287 }
288
289 return converted;
290
291 },
292
293 preload: function () {
294
295 for ( var mn in this.materialsInfo ) {
296
297 this.create( mn );
298
299 }
300
301 },
302
303 getIndex: function( materialName ) {
304
305 return this.nameLookup[ materialName ];
306
307 },
308
309 getAsArray: function() {
310
311 var index = 0;
312
313 for ( var mn in this.materialsInfo ) {
314
315 this.materialsArray[ index ] = this.create( mn );
316 this.nameLookup[ mn ] = index;
317 index ++;
318
319 }
320
321 return this.materialsArray;
322
323 },
324
325 create: function ( materialName ) {
326
327 if ( this.materials[ materialName ] === undefined ) {
328
329 this.createMaterial_( materialName );
330
331 }
332
333 return this.materials[ materialName ];
334
335 },
336
337 createMaterial_: function ( materialName ) {
338
339 // Create material
340
341 var mat = this.materialsInfo[ materialName ];
342 var params = {
343
344 name: materialName,
345 side: this.side
346
347 };
348
349 var resolveURL = function ( baseUrl, url ) {
350
351 if ( typeof url !== 'string' || url === '' )
352 return '';
353
354 // Absolute URL
355 if ( /^https?:\/\//i.test( url ) ) {
356 return url;
357 }
358
359 return baseUrl + url;
360 };
361
362 for ( var prop in mat ) {
363
364 var value = mat[ prop ];
365
366 if ( value === '' ) continue;
367
368 switch ( prop.toLowerCase() ) {
369
370 // Ns is material specular exponent
371
372 case 'kd':
373
374 // Diffuse color (color under white light) using RGB values
375
376 params.color = new THREE.Color().fromArray( value );
377
378 break;
379
380 case 'ks':
381
382 // Specular color (color when light is reflected from shiny surface) using RGB values
383 params.specular = new THREE.Color().fromArray( value );
384
385 break;
386
387 case 'map_kd':
388
389 // Diffuse texture map
390
391 if ( params.map ) break; // Keep the first encountered texture
392
393 params.map = this.loadTexture( resolveURL( this.baseUrl, value ) );
394 params.map.wrapS = this.wrap;
395 params.map.wrapT = this.wrap;
396
397 break;
398
399 case 'ns':
400
401 // The specular exponent (defines the focus of the specular highlight)
402 // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000.
403
404 params.shininess = parseFloat( value );
405
406 break;
407
408 case 'd':
409
410 if ( value < 1 ) {
411
412 params.opacity = value;
413 params.transparent = true;
414
415 }
416
417 break;
418
419 case 'Tr':
420
421 if ( value > 0 ) {
422
423 params.opacity = 1 - value;
424 params.transparent = true;
425
426 }
427
428 break;
429
430 case 'map_bump':
431 case 'bump':
432
433 // Bump texture map
434
435 if ( params.bumpMap ) break; // Keep the first encountered texture
436
437 params.bumpMap = this.loadTexture( resolveURL( this.baseUrl, value ) );
438 params.bumpMap.wrapS = this.wrap;
439 params.bumpMap.wrapT = this.wrap;
440
441 break;
442
443 default:
444 break;
445
446 }
447
448 }
449
450 this.materials[ materialName ] = new THREE.MeshPhongMaterial( params );
451 return this.materials[ materialName ];
452
453 },
454
455 loadTexture: function ( url, mapping, onLoad, onProgress, onError ) {
456
457 var texture;
458 var loader = THREE.Loader.Handlers.get( url );
459 var manager = ( this.manager !== undefined ) ? this.manager : THREE.DefaultLoadingManager;
460
461 if ( loader === null ) {
462
463 loader = new THREE.TextureLoader( manager );
464
465 }
466
467 if ( loader.setCrossOrigin ) loader.setCrossOrigin( this.crossOrigin );
468 texture = loader.load( url, onLoad, onProgress, onError );
469
470 if ( mapping !== undefined ) texture.mapping = mapping;
471
472 return texture;
473
474 }
475
476};