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 13THREE.MTLLoader.prototype = { 14 15 constructor: THREE.MTLLoader, 16 17 crossOrigin: 'anonymous', 18 19 /** 20 * Loads and parses a MTL asset from a URL. 21 * 22 * @param {String} url - URL to the MTL file. 23 * @param {Function} [onLoad] - Callback invoked with the loaded object. 24 * @param {Function} [onProgress] - Callback for download progress. 25 * @param {Function} [onError] - Callback for download errors. 26 * 27 * @see setPath setResourcePath 28 * 29 * @note In order for relative texture references to resolve correctly 30 * you must call setResourcePath() explicitly prior to load. 31 */ 32 load: function ( url, onLoad, onProgress, onError ) { 33 34 var scope = this; 35 36 var path = ( this.path === undefined ) ? THREE.LoaderUtils.extractUrlBase( url ) : this.path; 37 38 var loader = new THREE.FileLoader( this.manager ); 39 loader.setPath( this.path ); 40 loader.load( url, function ( text ) { 41 42 onLoad( scope.parse( text, path ) ); 43 44 }, onProgress, onError ); 45 46 }, 47 48 /** 49 * Set base path for resolving references. 50 * If set this path will be prepended to each loaded and found reference. 51 * 52 * @see setResourcePath 53 * @param {String} path 54 * @return {THREE.MTLLoader} 55 * 56 * @example 57 * mtlLoader.setPath( 'assets/obj/' ); 58 * mtlLoader.load( 'my.mtl', ... ); 59 */ 60 setPath: function ( path ) { 61 62 this.path = path; 63 return this; 64 65 }, 66 67 /** 68 * Set base path for additional resources like textures. 69 * 70 * @see setPath 71 * @param {String} path 72 * @return {THREE.MTLLoader} 73 * 74 * @example 75 * mtlLoader.setPath( 'assets/obj/' ); 76 * mtlLoader.setResourcePath( 'assets/textures/' ); 77 * mtlLoader.load( 'my.mtl', ... ); 78 */
79 setResourcePath: function ( path ) { 80 81 this.resourcePath = path; 82 return this; 83 84 }, 85 86 setTexturePath: function ( path ) { 87 88 console.warn( 'THREE.MTLLoader: .setTexturePath() has been renamed to .setResourcePath().' ); 89 return this.setResourcePath( path ); 90 91 }, 92 93 setCrossOrigin: function ( value ) { 94 95 this.crossOrigin = value; 96 return this; 97 98 }, 99 100 setMaterialOptions: function ( value ) { 101 102 this.materialOptions = value; 103 return this; 104 105 }, 106 107 /** 108 * Parses a MTL file. 109 * 110 * @param {String} text - Content of MTL file 111 * @return {THREE.MTLLoader.MaterialCreator} 112 * 113 * @see setPath setResourcePath 114 * 115 * @note In order for relative texture references to resolve correctly 116 * you must call setResourcePath() explicitly prior to parse. 117 */ 118 parse: function ( text, path ) { 119 120 var lines = text.split( '\n' ); 121 var info = {}; 122 var delimiter_pattern = /\s+/; 123 var materialsInfo = {}; 124 125 for ( var i = 0; i < lines.length; i ++ ) { 126 127 var line = lines[ i ]; 128 line = line.trim(); 129 130 if ( line.length === 0 || line.charAt( 0 ) === '#' ) { 131 132 // Blank line or comment ignore 133 continue; 134 135 } 136 137 var pos = line.indexOf( ' ' ); 138 139 var key = ( pos >= 0 ) ? line.substring( 0, pos ) : line; 140 key = key.toLowerCase(); 141 142 var value = ( pos >= 0 ) ? line.substring( pos + 1 ) : ''; 143 value = value.trim(); 144 145 if ( key === 'newmtl' ) { 146 147 // New material 148 149 info = { name: value }; 150 materialsInfo[ value ] = info; 151 152 } else { 153 154 if ( key === 'ka' || key === 'kd' || key === 'ks' || key === 'ke' ) { 155 156 var ss = value.split( delimiter_pattern, 3 ); 157 info[ key ] = [ parseFloat( ss[ 0 ] ), parseFloat( ss[ 1 ] ), parseFloat( ss[ 2 ] ) ]; 158 159 } else { 160 161 info[ key ] = value; 162 163 } 164 165 } 166 167 } 168 169 var materialCreator = new THREE.MTLLoader.MaterialCreator( this.resourcePath || path, this.materialOptions ); 170 materialCreator.setCrossOrigin( this.crossOrigin ); 171 materialCreator.setManager( this.manager ); 172 materialCreator.setMaterials( materialsInfo ); 173 return materialCreator; 174 175 } 176 177}; 178 179/** 180 * Create a new THREE-MTLLoader.MaterialCreator 181 * @param baseUrl - Url relative to which textures are loaded 182 * @param options - Set of options on how to construct the materials 183 * side: Which side to apply the material 184 * THREE.FrontSide (default), THREE.BackSide, THREE.DoubleSide 185 * wrap: What type of wrapping to apply for textures 186 * THREE.RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping 187 * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255 188 * Default: false, assumed to be already normalized 189 * ignoreZeroRGBs: Ignore values of RGBs (Ka,Kd,Ks) that are all 0's 190 * Default: false 191 * @constructor 192 */ 193 194THREE.MTLLoader.MaterialCreator = function ( baseUrl, options ) { 195 196 this.baseUrl = baseUrl || ''; 197 this.options = options; 198 this.materialsInfo = {}; 199 this.materials = {}; 200 this.materialsArray = []; 201 this.nameLookup = {}; 202 203 this.side = ( this.options && this.options.side ) ? this.options.side : THREE.FrontSide; 204 this.wrap = ( this.options && this.options.wrap ) ? this.options.wrap : THREE.RepeatWrapping; 205 206}; 207 208THREE.MTLLoader.MaterialCreator.prototype = { 209 210 constructor: THREE.MTLLoader.MaterialCreator, 211 212 crossOrigin: 'anonymous', 213 214 setCrossOrigin: function ( value ) { 215 216 this.crossOrigin = value; 217 return this; 218 219 }, 220 221 setManager: function ( value ) { 222 223 this.manager = value; 224 225 }, 226 227 setMaterials: function ( materialsInfo ) { 228 229 this.materialsInfo = this.convert( materialsInfo ); 230 this.materials = {}; 231 this.materialsArray = []; 232 this.nameLookup = {}; 233 234 }, 235 236 convert: function ( materialsInfo ) { 237 238 if ( ! this.options ) return materialsInfo; 239 240 var converted = {}; 241 242 for ( var mn in materialsInfo ) { 243 244 // Convert materials info into normalized form based on options 245 246 var mat = materialsInfo[ mn ]; 247 248 var covmat = {}; 249 250 converted[ mn ] = covmat; 251 252 for ( var prop in mat ) { 253 254 var save = true; 255 var value = mat[ prop ]; 256 var lprop = prop.toLowerCase(); 257 258 switch ( lprop ) { 259 260 case 'kd': 261 case 'ka': 262 case 'ks': 263 264 // Diffuse color (color under white light) using RGB values 265 266 if ( this.options && this.options.normalizeRGB ) { 267 268 value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ]; 269 270 } 271 272 if ( this.options && this.options.ignoreZeroRGBs ) { 273 274 if ( value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 2 ] === 0 ) { 275 276 // ignore 277 278 save = false;
279 280 } 281 282 } 283 284 break; 285 286 default: 287 288 break; 289 290 } 291 292 if ( save ) { 293 294 covmat[ lprop ] = value; 295 296 } 297 298 } 299 300 } 301 302 return converted; 303 304 }, 305 306 preload: function () { 307 308 for ( var mn in this.materialsInfo ) { 309 310 this.create( mn ); 311 312 } 313 314 }, 315 316 getIndex: function ( materialName ) { 317 318 return this.nameLookup[ materialName ]; 319 320 }, 321 322 getAsArray: function () { 323 324 var index = 0; 325 326 for ( var mn in this.materialsInfo ) { 327 328 this.materialsArray[ index ] = this.create( mn ); 329 this.nameLookup[ mn ] = index; 330 index ++; 331 332 } 333 334 return this.materialsArray; 335 336 }, 337 338 create: function ( materialName ) { 339 340 if ( this.materials[ materialName ] === undefined ) { 341 342 this.createMaterial_( materialName ); 343 344 } 345 346 return this.materials[ materialName ]; 347 348 }, 349 350 createMaterial_: function ( materialName ) { 351 352 // Create material 353 354 var scope = this; 355 var mat = this.materialsInfo[ materialName ]; 356 var params = { 357 358 name: materialName, 359 side: this.side 360 361 }; 362 363 function resolveURL( baseUrl, url ) { 364 365 if ( typeof url !== 'string' || url === '' ) 366 return ''; 367 368 // Absolute URL 369 if ( /^https?:\/\//i.test( url ) ) return url; 370 371 return baseUrl + url; 372 373 } 374 375 function setMapForType( mapType, value ) { 376 377 if ( params[ mapType ] ) return; // Keep the first encountered texture 378 379 var texParams = scope.getTextureParams( value, params ); 380 var map = scope.loadTexture( resolveURL( scope.baseUrl, texParams.url ) ); 381 382 map.repeat.copy( texParams.scale ); 383 map.offset.copy( texParams.offset ); 384 385 map.wrapS = scope.wrap; 386 map.wrapT = scope.wrap; 387 388 params[ mapType ] = map; 389 390 } 391 392 for ( var prop in mat ) { 393 394 var value = mat[ prop ]; 395 var n; 396 397 if ( value === '' ) continue; 398 399 switch ( prop.toLowerCase() ) { 400 401 // Ns is material specular exponent 402 403 case 'kd': 404 405 // Diffuse color (color under white light) using RGB values 406 407 params.color = new THREE.Color().fromArray( value ); 408 409 break; 410 411 case 'ks': 412 413 // Specular color (color when light is reflected from shiny surface) using RGB values 414 params.specular = new THREE.Color().fromArray( value ); 415 416 break; 417 418 case 'ke': 419 420 // Emissive using RGB values 421 params.emissive = new THREE.Color().fromArray( value ); 422 423 break; 424 425 case 'map_kd': 426 427 // Diffuse texture map 428 429 setMapForType( "map", value ); 430 431 break; 432 433 case 'map_ks': 434 435 // Specular map 436 437 setMapForType( "specularMap", value ); 438 439 break; 440 441 case 'map_ke': 442 443 // Emissive map 444 445 setMapForType( "emissiveMap", value ); 446 447 break; 448 449 case 'norm': 450 451 setMapForType( "normalMap", value ); 452 453 break; 454 455 case 'map_bump': 456 case 'bump': 457 458 // Bump texture map 459 460 setMapForType( "bumpMap", value ); 461 462 break; 463 464 case 'map_d': 465 466 // Alpha map 467 468 setMapForType( "alphaMap", value ); 469 params.transparent = true; 470 471 break; 472 473 case 'ns': 474 475 // The specular exponent (defines the focus of the specular highlight) 476 // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000. 477 478 params.shininess = parseFloat( value ); 479 480 break; 481 482 case 'd': 483 n = parseFloat( value ); 484 485 if ( n < 1 ) { 486 487 params.opacity = n; 488 params.transparent = true; 489 490 } 491 492 break; 493 494 case 'tr': 495 n = parseFloat( value ); 496 497 if ( this.options && this.options.invertTrProperty ) n = 1 - n; 498 499 if ( n > 0 ) { 500 501 params.opacity = 1 - n; 502 params.transparent = true; 503 504 } 505 506 break; 507 508 default: 509 break; 510 511 } 512 513 } 514 515 this.materials[ materialName ] = new THREE.MeshPhongMaterial( params ); 516 return this.materials[ materialName ]; 517 518 }, 519 520 getTextureParams: function ( value, matParams ) { 521 522 var texParams = { 523 524 scale: new THREE.Vector2( 1, 1 ), 525 offset: new THREE.Vector2( 0, 0 ) 526 527 }; 528 529 var items = value.split( /\s+/ ); 530 var pos; 531 532 pos = items.indexOf( '-bm' ); 533 534 if ( pos >= 0 ) { 535 536 matParams.bumpScale = parseFloat( items[ pos + 1 ] ); 537 items.splice( pos, 2 ); 538 539 } 540 541 pos = items.indexOf( '-s' ); 542 543 if ( pos >= 0 ) { 544 545 texParams.scale.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) ); 546 items.splice( pos, 4 ); // we expect 3 parameters here! 547 548 } 549
550 pos = items.indexOf( '-o' ); 551 552 if ( pos >= 0 ) { 553 554 texParams.offset.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) ); 555 items.splice( pos, 4 ); // we expect 3 parameters here! 556 557 } 558 559 texParams.url = items.join( ' ' ).trim(); 560 return texParams; 561 562 }, 563 564 loadTexture: function ( url, mapping, onLoad, onProgress, onError ) { 565 566 var texture; 567 var loader = THREE.Loader.Handlers.get( url ); 568 var manager = ( this.manager !== undefined ) ? this.manager : THREE.DefaultLoadingManager; 569 570 if ( loader === null ) { 571 572 loader = new THREE.TextureLoader( manager ); 573 574 } 575 576 if ( loader.setCrossOrigin ) loader.setCrossOrigin( this.crossOrigin ); 577 texture = loader.load( url, onLoad, onProgress, onError ); 578 579 if ( mapping !== undefined ) texture.mapping = mapping; 580 581 return texture; 582 583 } 584 585};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.