You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 lines
9.6 KiB

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