MTLLoader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. console.warn( "THREE.MTLLoader: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * Loads a Wavefront .mtl file specifying materials
  4. *
  5. * @author angelxuanchang
  6. */
  7. THREE.MTLLoader = function ( manager ) {
  8. THREE.Loader.call( this, manager );
  9. };
  10. THREE.MTLLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  11. constructor: THREE.MTLLoader,
  12. /**
  13. * Loads and parses a MTL asset from a URL.
  14. *
  15. * @param {String} url - URL to the MTL file.
  16. * @param {Function} [onLoad] - Callback invoked with the loaded object.
  17. * @param {Function} [onProgress] - Callback for download progress.
  18. * @param {Function} [onError] - Callback for download errors.
  19. *
  20. * @see setPath setResourcePath
  21. *
  22. * @note In order for relative texture references to resolve correctly
  23. * you must call setResourcePath() explicitly prior to load.
  24. */
  25. load: function ( url, onLoad, onProgress, onError ) {
  26. var scope = this;
  27. var path = ( this.path === '' ) ? THREE.LoaderUtils.extractUrlBase( url ) : this.path;
  28. var loader = new THREE.FileLoader( this.manager );
  29. loader.setPath( this.path );
  30. loader.load( url, function ( text ) {
  31. onLoad( scope.parse( text, path ) );
  32. }, onProgress, onError );
  33. },
  34. setMaterialOptions: function ( value ) {
  35. this.materialOptions = value;
  36. return this;
  37. },
  38. /**
  39. * Parses a MTL file.
  40. *
  41. * @param {String} text - Content of MTL file
  42. * @return {THREE.MTLLoader.MaterialCreator}
  43. *
  44. * @see setPath setResourcePath
  45. *
  46. * @note In order for relative texture references to resolve correctly
  47. * you must call setResourcePath() explicitly prior to parse.
  48. */
  49. parse: function ( text, path ) {
  50. var lines = text.split( '\n' );
  51. var info = {};
  52. var delimiter_pattern = /\s+/;
  53. var materialsInfo = {};
  54. for ( var i = 0; i < lines.length; i ++ ) {
  55. var line = lines[ i ];
  56. line = line.trim();
  57. if ( line.length === 0 || line.charAt( 0 ) === '#' ) {
  58. // Blank line or comment ignore
  59. continue;
  60. }
  61. var pos = line.indexOf( ' ' );
  62. var key = ( pos >= 0 ) ? line.substring( 0, pos ) : line;
  63. key = key.toLowerCase();
  64. var value = ( pos >= 0 ) ? line.substring( pos + 1 ) : '';
  65. value = value.trim();
  66. if ( key === 'newmtl' ) {
  67. // New material
  68. info = { name: value };
  69. materialsInfo[ value ] = info;
  70. } else {
  71. if ( key === 'ka' || key === 'kd' || key === 'ks' || key === 'ke' ) {
  72. var ss = value.split( delimiter_pattern, 3 );
  73. info[ key ] = [ parseFloat( ss[ 0 ] ), parseFloat( ss[ 1 ] ), parseFloat( ss[ 2 ] ) ];
  74. } else {
  75. info[ key ] = value;
  76. }
  77. }
  78. }
  79. var materialCreator = new THREE.MTLLoader.MaterialCreator( this.resourcePath || path, this.materialOptions );
  80. materialCreator.setCrossOrigin( this.crossOrigin );
  81. materialCreator.setManager( this.manager );
  82. materialCreator.setMaterials( materialsInfo );
  83. return materialCreator;
  84. }
  85. } );
  86. /**
  87. * Create a new THREE.MTLLoader.MaterialCreator
  88. * @param baseUrl - Url relative to which textures are loaded
  89. * @param options - Set of options on how to construct the materials
  90. * side: Which side to apply the material
  91. * THREE.FrontSide (default), THREE.BackSide, THREE.DoubleSide
  92. * wrap: What type of wrapping to apply for textures
  93. * THREE.RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping
  94. * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255
  95. * Default: false, assumed to be already normalized
  96. * ignoreZeroRGBs: Ignore values of RGBs (Ka,Kd,Ks) that are all 0's
  97. * Default: false
  98. * @constructor
  99. */
  100. THREE.MTLLoader.MaterialCreator = function ( baseUrl, options ) {
  101. this.baseUrl = baseUrl || '';
  102. this.options = options;
  103. this.materialsInfo = {};
  104. this.materials = {};
  105. this.materialsArray = [];
  106. this.nameLookup = {};
  107. this.side = ( this.options && this.options.side ) ? this.options.side : THREE.FrontSide;
  108. this.wrap = ( this.options && this.options.wrap ) ? this.options.wrap : THREE.RepeatWrapping;
  109. };
  110. THREE.MTLLoader.MaterialCreator.prototype = {
  111. constructor: THREE.MTLLoader.MaterialCreator,
  112. crossOrigin: 'anonymous',
  113. setCrossOrigin: function ( value ) {
  114. this.crossOrigin = value;
  115. return this;
  116. },
  117. setManager: function ( value ) {
  118. this.manager = value;
  119. },
  120. setMaterials: function ( materialsInfo ) {
  121. this.materialsInfo = this.convert( materialsInfo );
  122. this.materials = {};
  123. this.materialsArray = [];
  124. this.nameLookup = {};
  125. },
  126. convert: function ( materialsInfo ) {
  127. if ( ! this.options ) return materialsInfo;
  128. var converted = {};
  129. for ( var mn in materialsInfo ) {
  130. // Convert materials info into normalized form based on options
  131. var mat = materialsInfo[ mn ];
  132. var covmat = {};
  133. converted[ mn ] = covmat;
  134. for ( var prop in mat ) {
  135. var save = true;
  136. var value = mat[ prop ];
  137. var lprop = prop.toLowerCase();
  138. switch ( lprop ) {
  139. case 'kd':
  140. case 'ka':
  141. case 'ks':
  142. // Diffuse color (color under white light) using RGB values
  143. if ( this.options && this.options.normalizeRGB ) {
  144. value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ];
  145. }
  146. if ( this.options && this.options.ignoreZeroRGBs ) {
  147. if ( value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 2 ] === 0 ) {
  148. // ignore
  149. save = false;
  150. }
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. if ( save ) {
  157. covmat[ lprop ] = value;
  158. }
  159. }
  160. }
  161. return converted;
  162. },
  163. preload: function () {
  164. for ( var mn in this.materialsInfo ) {
  165. this.create( mn );
  166. }
  167. },
  168. getIndex: function ( materialName ) {
  169. return this.nameLookup[ materialName ];
  170. },
  171. getAsArray: function () {
  172. var index = 0;
  173. for ( var mn in this.materialsInfo ) {
  174. this.materialsArray[ index ] = this.create( mn );
  175. this.nameLookup[ mn ] = index;
  176. index ++;
  177. }
  178. return this.materialsArray;
  179. },
  180. create: function ( materialName ) {
  181. if ( this.materials[ materialName ] === undefined ) {
  182. this.createMaterial_( materialName );
  183. }
  184. return this.materials[ materialName ];
  185. },
  186. createMaterial_: function ( materialName ) {
  187. // Create material
  188. var scope = this;
  189. var mat = this.materialsInfo[ materialName ];
  190. var params = {
  191. name: materialName,
  192. side: this.side
  193. };
  194. function resolveURL( baseUrl, url ) {
  195. if ( typeof url !== 'string' || url === '' )
  196. return '';
  197. // Absolute URL
  198. if ( /^https?:\/\//i.test( url ) ) return url;
  199. return baseUrl + url;
  200. }
  201. function setMapForType( mapType, value ) {
  202. if ( params[ mapType ] ) return; // Keep the first encountered texture
  203. var texParams = scope.getTextureParams( value, params );
  204. var map = scope.loadTexture( resolveURL( scope.baseUrl, texParams.url ) );
  205. map.repeat.copy( texParams.scale );
  206. map.offset.copy( texParams.offset );
  207. map.wrapS = scope.wrap;
  208. map.wrapT = scope.wrap;
  209. params[ mapType ] = map;
  210. }
  211. for ( var prop in mat ) {
  212. var value = mat[ prop ];
  213. var n;
  214. if ( value === '' ) continue;
  215. switch ( prop.toLowerCase() ) {
  216. // Ns is material specular exponent
  217. case 'kd':
  218. // Diffuse color (color under white light) using RGB values
  219. params.color = new THREE.Color().fromArray( value );
  220. break;
  221. case 'ks':
  222. // Specular color (color when light is reflected from shiny surface) using RGB values
  223. params.specular = new THREE.Color().fromArray( value );
  224. break;
  225. case 'ke':
  226. // Emissive using RGB values
  227. params.emissive = new THREE.Color().fromArray( value );
  228. break;
  229. case 'map_kd':
  230. // Diffuse texture map
  231. setMapForType( "map", value );
  232. break;
  233. case 'map_ks':
  234. // Specular map
  235. setMapForType( "specularMap", value );
  236. break;
  237. case 'map_ke':
  238. // Emissive map
  239. setMapForType( "emissiveMap", value );
  240. break;
  241. case 'norm':
  242. setMapForType( "normalMap", value );
  243. break;
  244. case 'map_bump':
  245. case 'bump':
  246. // Bump texture map
  247. setMapForType( "bumpMap", value );
  248. break;
  249. case 'map_d':
  250. // Alpha map
  251. setMapForType( "alphaMap", value );
  252. params.transparent = true;
  253. break;
  254. case 'ns':
  255. // The specular exponent (defines the focus of the specular highlight)
  256. // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000.
  257. params.shininess = parseFloat( value );
  258. break;
  259. case 'd':
  260. n = parseFloat( value );
  261. if ( n < 1 ) {
  262. params.opacity = n;
  263. params.transparent = true;
  264. }
  265. break;
  266. case 'tr':
  267. n = parseFloat( value );
  268. if ( this.options && this.options.invertTrProperty ) n = 1 - n;
  269. if ( n > 0 ) {
  270. params.opacity = 1 - n;
  271. params.transparent = true;
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. this.materials[ materialName ] = new THREE.MeshPhongMaterial( params );
  279. return this.materials[ materialName ];
  280. },
  281. getTextureParams: function ( value, matParams ) {
  282. var texParams = {
  283. scale: new THREE.Vector2( 1, 1 ),
  284. offset: new THREE.Vector2( 0, 0 )
  285. };
  286. var items = value.split( /\s+/ );
  287. var pos;
  288. pos = items.indexOf( '-bm' );
  289. if ( pos >= 0 ) {
  290. matParams.bumpScale = parseFloat( items[ pos + 1 ] );
  291. items.splice( pos, 2 );
  292. }
  293. pos = items.indexOf( '-s' );
  294. if ( pos >= 0 ) {
  295. texParams.scale.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) );
  296. items.splice( pos, 4 ); // we expect 3 parameters here!
  297. }
  298. pos = items.indexOf( '-o' );
  299. if ( pos >= 0 ) {
  300. texParams.offset.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) );
  301. items.splice( pos, 4 ); // we expect 3 parameters here!
  302. }
  303. texParams.url = items.join( ' ' ).trim();
  304. return texParams;
  305. },
  306. loadTexture: function ( url, mapping, onLoad, onProgress, onError ) {
  307. var texture;
  308. var manager = ( this.manager !== undefined ) ? this.manager : THREE.DefaultLoadingManager;
  309. var loader = manager.getHandler( url );
  310. if ( loader === null ) {
  311. loader = new THREE.TextureLoader( manager );
  312. }
  313. if ( loader.setCrossOrigin ) loader.setCrossOrigin( this.crossOrigin );
  314. texture = loader.load( url, onLoad, onProgress, onError );
  315. if ( mapping !== undefined ) texture.mapping = mapping;
  316. return texture;
  317. }
  318. };