MTLLoader.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. THREE.MTLLoader.prototype = {
  10. constructor: THREE.MTLLoader,
  11. load: function ( url, onLoad, onProgress, onError ) {
  12. var scope = this;
  13. var loader = new THREE.XHRLoader( this.manager );
  14. loader.setCrossOrigin( this.crossOrigin );
  15. loader.load( url, function ( text ) {
  16. onLoad( scope.parse( text ) );
  17. }, onProgress, onError );
  18. },
  19. setBaseUrl: function( value ) {
  20. this.baseUrl = value;
  21. },
  22. setCrossOrigin: function ( value ) {
  23. this.crossOrigin = value;
  24. },
  25. setMaterialOptions: function ( value ) {
  26. this.materialOptions = value;
  27. },
  28. /**
  29. * Parses loaded MTL file
  30. * @param text - Content of MTL file
  31. * @return {THREE.MTLLoader.MaterialCreator}
  32. */
  33. parse: function ( text ) {
  34. var lines = text.split( "\n" );
  35. var info = {};
  36. var delimiter_pattern = /\s+/;
  37. var materialsInfo = {};
  38. for ( var i = 0; i < lines.length; i ++ ) {
  39. var line = lines[ i ];
  40. line = line.trim();
  41. if ( line.length === 0 || line.charAt( 0 ) === '#' ) {
  42. // Blank line or comment ignore
  43. continue;
  44. }
  45. var pos = line.indexOf( ' ' );
  46. var key = ( pos >= 0 ) ? line.substring( 0, pos ) : line;
  47. key = key.toLowerCase();
  48. var value = ( pos >= 0 ) ? line.substring( pos + 1 ) : "";
  49. value = value.trim();
  50. if ( key === "newmtl" ) {
  51. // New material
  52. info = { name: value };
  53. materialsInfo[ value ] = info;
  54. } else if ( info ) {
  55. if ( key === "ka" || key === "kd" || key === "ks" ) {
  56. var ss = value.split( delimiter_pattern, 3 );
  57. info[ key ] = [ parseFloat( ss[ 0 ] ), parseFloat( ss[ 1 ] ), parseFloat( ss[ 2 ] ) ];
  58. } else {
  59. info[ key ] = value;
  60. }
  61. }
  62. }
  63. var materialCreator = new THREE.MTLLoader.MaterialCreator( this.baseUrl, this.materialOptions );
  64. materialCreator.setCrossOrigin( this.crossOrigin );
  65. materialCreator.setManager( this.manager );
  66. materialCreator.setMaterials( materialsInfo );
  67. return materialCreator;
  68. }
  69. };
  70. /**
  71. * Create a new THREE-MTLLoader.MaterialCreator
  72. * @param baseUrl - Url relative to which textures are loaded
  73. * @param options - Set of options on how to construct the materials
  74. * side: Which side to apply the material
  75. * THREE.FrontSide (default), THREE.BackSide, THREE.DoubleSide
  76. * wrap: What type of wrapping to apply for textures
  77. * THREE.RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping
  78. * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255
  79. * Default: false, assumed to be already normalized
  80. * ignoreZeroRGBs: Ignore values of RGBs (Ka,Kd,Ks) that are all 0's
  81. * Default: false
  82. * invertTransparency: If transparency need to be inverted (inversion is needed if d = 0 is fully opaque)
  83. * Default: false (d = 1 is fully opaque)
  84. * @constructor
  85. */
  86. THREE.MTLLoader.MaterialCreator = function( baseUrl, options ) {
  87. this.baseUrl = baseUrl;
  88. this.options = options;
  89. this.materialsInfo = {};
  90. this.materials = {};
  91. this.materialsArray = [];
  92. this.nameLookup = {};
  93. this.side = ( this.options && this.options.side ) ? this.options.side : THREE.FrontSide;
  94. this.wrap = ( this.options && this.options.wrap ) ? this.options.wrap : THREE.RepeatWrapping;
  95. };
  96. THREE.MTLLoader.MaterialCreator.prototype = {
  97. constructor: THREE.MTLLoader.MaterialCreator,
  98. setCrossOrigin: function ( value ) {
  99. this.crossOrigin = value;
  100. },
  101. setManager: function ( value ) {
  102. this.manager = value;
  103. },
  104. setMaterials: function( materialsInfo ) {
  105. this.materialsInfo = this.convert( materialsInfo );
  106. this.materials = {};
  107. this.materialsArray = [];
  108. this.nameLookup = {};
  109. },
  110. convert: function( materialsInfo ) {
  111. if ( ! this.options ) return materialsInfo;
  112. var converted = {};
  113. for ( var mn in materialsInfo ) {
  114. // Convert materials info into normalized form based on options
  115. var mat = materialsInfo[ mn ];
  116. var covmat = {};
  117. converted[ mn ] = covmat;
  118. for ( var prop in mat ) {
  119. var save = true;
  120. var value = mat[ prop ];
  121. var lprop = prop.toLowerCase();
  122. switch ( lprop ) {
  123. case 'kd':
  124. case 'ka':
  125. case 'ks':
  126. // Diffuse color (color under white light) using RGB values
  127. if ( this.options && this.options.normalizeRGB ) {
  128. value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ];
  129. }
  130. if ( this.options && this.options.ignoreZeroRGBs ) {
  131. if ( value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 1 ] === 0 ) {
  132. // ignore
  133. save = false;
  134. }
  135. }
  136. break;
  137. case 'd':
  138. // According to MTL format (http://paulbourke.net/dataformats/mtl/):
  139. // d is dissolve for current material
  140. // factor of 1.0 is fully opaque, a factor of 0 is fully dissolved (completely transparent)
  141. if ( this.options && this.options.invertTransparency ) {
  142. value = 1 - value;
  143. }
  144. break;
  145. default:
  146. break;
  147. }
  148. if ( save ) {
  149. covmat[ lprop ] = value;
  150. }
  151. }
  152. }
  153. return converted;
  154. },
  155. preload: function () {
  156. for ( var mn in this.materialsInfo ) {
  157. this.create( mn );
  158. }
  159. },
  160. getIndex: function( materialName ) {
  161. return this.nameLookup[ materialName ];
  162. },
  163. getAsArray: function() {
  164. var index = 0;
  165. for ( var mn in this.materialsInfo ) {
  166. this.materialsArray[ index ] = this.create( mn );
  167. this.nameLookup[ mn ] = index;
  168. index ++;
  169. }
  170. return this.materialsArray;
  171. },
  172. create: function ( materialName ) {
  173. if ( this.materials[ materialName ] === undefined ) {
  174. this.createMaterial_( materialName );
  175. }
  176. return this.materials[ materialName ];
  177. },
  178. createMaterial_: function ( materialName ) {
  179. // Create material
  180. var mat = this.materialsInfo[ materialName ];
  181. var params = {
  182. name: materialName,
  183. side: this.side
  184. };
  185. for ( var prop in mat ) {
  186. var value = mat[ prop ];
  187. switch ( prop.toLowerCase() ) {
  188. // Ns is material specular exponent
  189. case 'kd':
  190. // Diffuse color (color under white light) using RGB values
  191. params[ 'color' ] = new THREE.Color().fromArray( value );
  192. break;
  193. case 'ka':
  194. // Ambient color (color under shadow) using RGB values
  195. break;
  196. case 'ks':
  197. // Specular color (color when light is reflected from shiny surface) using RGB values
  198. params[ 'specular' ] = new THREE.Color().fromArray( value );
  199. break;
  200. case 'map_kd':
  201. // Diffuse texture map
  202. params[ 'map' ] = this.loadTexture( this.baseUrl + value );
  203. params[ 'map' ].wrapS = this.wrap;
  204. params[ 'map' ].wrapT = this.wrap;
  205. break;
  206. case 'ns':
  207. // The specular exponent (defines the focus of the specular highlight)
  208. // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000.
  209. params[ 'shininess' ] = parseFloat( value );
  210. break;
  211. case 'd':
  212. // According to MTL format (http://paulbourke.net/dataformats/mtl/):
  213. // d is dissolve for current material
  214. // factor of 1.0 is fully opaque, a factor of 0 is fully dissolved (completely transparent)
  215. if ( value < 1 ) {
  216. params[ 'transparent' ] = true;
  217. params[ 'opacity' ] = value;
  218. }
  219. break;
  220. case 'map_bump':
  221. case 'bump':
  222. // Bump texture map
  223. if ( params[ 'bumpMap' ] ) break; // Avoid loading twice.
  224. params[ 'bumpMap' ] = this.loadTexture( this.baseUrl + value );
  225. params[ 'bumpMap' ].wrapS = this.wrap;
  226. params[ 'bumpMap' ].wrapT = this.wrap;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. this.materials[ materialName ] = new THREE.MeshPhongMaterial( params );
  233. return this.materials[ materialName ];
  234. },
  235. loadTexture: function ( url, mapping, onLoad, onProgress, onError ) {
  236. var texture;
  237. var loader = THREE.Loader.Handlers.get( url );
  238. var manager = ( this.manager !== undefined ) ? this.manager : THREE.DefaultLoadingManager;
  239. if ( loader !== null ) {
  240. texture = loader.load( url, onLoad );
  241. } else {
  242. texture = new THREE.Texture();
  243. loader = new THREE.ImageLoader( manager );
  244. loader.setCrossOrigin( this.crossOrigin );
  245. loader.load( url, function ( image ) {
  246. texture.image = THREE.MTLLoader.ensurePowerOfTwo_( image );
  247. texture.needsUpdate = true;
  248. if ( onLoad ) onLoad( texture );
  249. }, onProgress, onError );
  250. }
  251. if ( mapping !== undefined ) texture.mapping = mapping;
  252. return texture;
  253. }
  254. };
  255. THREE.MTLLoader.ensurePowerOfTwo_ = function ( image ) {
  256. if ( ! THREE.Math.isPowerOfTwo( image.width ) || ! THREE.Math.isPowerOfTwo( image.height ) ) {
  257. var canvas = document.createElement( "canvas" );
  258. canvas.width = THREE.MTLLoader.nextHighestPowerOfTwo_( image.width );
  259. canvas.height = THREE.MTLLoader.nextHighestPowerOfTwo_( image.height );
  260. var ctx = canvas.getContext( "2d" );
  261. ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
  262. return canvas;
  263. }
  264. return image;
  265. };
  266. THREE.MTLLoader.nextHighestPowerOfTwo_ = function( x ) {
  267. -- x;
  268. for ( var i = 1; i < 32; i <<= 1 ) {
  269. x = x | x >> i;
  270. }
  271. return x + 1;
  272. };
  273. THREE.EventDispatcher.prototype.apply( THREE.MTLLoader.prototype );