Loader.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import {
  2. NoBlending,
  3. NormalBlending,
  4. AdditiveBlending,
  5. SubtractiveBlending,
  6. MultiplyBlending,
  7. CustomBlending,
  8. FaceColors,
  9. VertexColors,
  10. DoubleSide,
  11. BackSide,
  12. MirroredRepeatWrapping,
  13. RepeatWrapping
  14. } from '../constants';
  15. import { _Math } from '../math/Math';
  16. import { MaterialLoader } from './MaterialLoader';
  17. import { TextureLoader } from './TextureLoader';
  18. import { Color } from '../math/Color';
  19. /**
  20. * @author alteredq / http://alteredqualia.com/
  21. */
  22. function Loader() {
  23. this.onLoadStart = function () {};
  24. this.onLoadProgress = function () {};
  25. this.onLoadComplete = function () {};
  26. }
  27. Loader.prototype = {
  28. constructor: Loader,
  29. crossOrigin: undefined,
  30. extractUrlBase: function ( url ) {
  31. var parts = url.split( '/' );
  32. if ( parts.length === 1 ) return './';
  33. parts.pop();
  34. return parts.join( '/' ) + '/';
  35. },
  36. initMaterials: function ( materials, texturePath, crossOrigin ) {
  37. var array = [];
  38. for ( var i = 0; i < materials.length; ++ i ) {
  39. array[ i ] = this.createMaterial( materials[ i ], texturePath, crossOrigin );
  40. }
  41. return array;
  42. },
  43. createMaterial: ( function () {
  44. var BlendingMode = {
  45. NoBlending: NoBlending,
  46. NormalBlending: NormalBlending,
  47. AdditiveBlending: AdditiveBlending,
  48. SubtractiveBlending: SubtractiveBlending,
  49. MultiplyBlending: MultiplyBlending,
  50. CustomBlending: CustomBlending
  51. };
  52. var color, textureLoader, materialLoader;
  53. return function createMaterial( m, texturePath, crossOrigin ) {
  54. if ( color === undefined ) color = new Color();
  55. if ( textureLoader === undefined ) textureLoader = new TextureLoader();
  56. if ( materialLoader === undefined ) materialLoader = new MaterialLoader();
  57. // convert from old material format
  58. var textures = {};
  59. function loadTexture( path, repeat, offset, wrap, anisotropy ) {
  60. var fullPath = texturePath + path;
  61. var loader = Loader.Handlers.get( fullPath );
  62. var texture;
  63. if ( loader !== null ) {
  64. texture = loader.load( fullPath );
  65. } else {
  66. textureLoader.setCrossOrigin( crossOrigin );
  67. texture = textureLoader.load( fullPath );
  68. }
  69. if ( repeat !== undefined ) {
  70. texture.repeat.fromArray( repeat );
  71. if ( repeat[ 0 ] !== 1 ) texture.wrapS = RepeatWrapping;
  72. if ( repeat[ 1 ] !== 1 ) texture.wrapT = RepeatWrapping;
  73. }
  74. if ( offset !== undefined ) {
  75. texture.offset.fromArray( offset );
  76. }
  77. if ( wrap !== undefined ) {
  78. if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = RepeatWrapping;
  79. if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = MirroredRepeatWrapping;
  80. if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = RepeatWrapping;
  81. if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = MirroredRepeatWrapping;
  82. }
  83. if ( anisotropy !== undefined ) {
  84. texture.anisotropy = anisotropy;
  85. }
  86. var uuid = _Math.generateUUID();
  87. textures[ uuid ] = texture;
  88. return uuid;
  89. }
  90. //
  91. var json = {
  92. uuid: _Math.generateUUID(),
  93. type: 'MeshLambertMaterial'
  94. };
  95. for ( var name in m ) {
  96. var value = m[ name ];
  97. switch ( name ) {
  98. case 'DbgColor':
  99. case 'DbgIndex':
  100. case 'opticalDensity':
  101. case 'illumination':
  102. break;
  103. case 'DbgName':
  104. json.name = value;
  105. break;
  106. case 'blending':
  107. json.blending = BlendingMode[ value ];
  108. break;
  109. case 'colorAmbient':
  110. case 'mapAmbient':
  111. console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
  112. break;
  113. case 'colorDiffuse':
  114. json.color = color.fromArray( value ).getHex();
  115. break;
  116. case 'colorSpecular':
  117. json.specular = color.fromArray( value ).getHex();
  118. break;
  119. case 'colorEmissive':
  120. json.emissive = color.fromArray( value ).getHex();
  121. break;
  122. case 'specularCoef':
  123. json.shininess = value;
  124. break;
  125. case 'shading':
  126. if ( value.toLowerCase() === 'basic' ) json.type = 'MeshBasicMaterial';
  127. if ( value.toLowerCase() === 'phong' ) json.type = 'MeshPhongMaterial';
  128. if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
  129. break;
  130. case 'mapDiffuse':
  131. json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
  132. break;
  133. case 'mapDiffuseRepeat':
  134. case 'mapDiffuseOffset':
  135. case 'mapDiffuseWrap':
  136. case 'mapDiffuseAnisotropy':
  137. break;
  138. case 'mapEmissive':
  139. json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy );
  140. break;
  141. case 'mapEmissiveRepeat':
  142. case 'mapEmissiveOffset':
  143. case 'mapEmissiveWrap':
  144. case 'mapEmissiveAnisotropy':
  145. break;
  146. case 'mapLight':
  147. json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
  148. break;
  149. case 'mapLightRepeat':
  150. case 'mapLightOffset':
  151. case 'mapLightWrap':
  152. case 'mapLightAnisotropy':
  153. break;
  154. case 'mapAO':
  155. json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy );
  156. break;
  157. case 'mapAORepeat':
  158. case 'mapAOOffset':
  159. case 'mapAOWrap':
  160. case 'mapAOAnisotropy':
  161. break;
  162. case 'mapBump':
  163. json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
  164. break;
  165. case 'mapBumpScale':
  166. json.bumpScale = value;
  167. break;
  168. case 'mapBumpRepeat':
  169. case 'mapBumpOffset':
  170. case 'mapBumpWrap':
  171. case 'mapBumpAnisotropy':
  172. break;
  173. case 'mapNormal':
  174. json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
  175. break;
  176. case 'mapNormalFactor':
  177. json.normalScale = [ value, value ];
  178. break;
  179. case 'mapNormalRepeat':
  180. case 'mapNormalOffset':
  181. case 'mapNormalWrap':
  182. case 'mapNormalAnisotropy':
  183. break;
  184. case 'mapSpecular':
  185. json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
  186. break;
  187. case 'mapSpecularRepeat':
  188. case 'mapSpecularOffset':
  189. case 'mapSpecularWrap':
  190. case 'mapSpecularAnisotropy':
  191. break;
  192. case 'mapMetalness':
  193. json.metalnessMap = loadTexture( value, m.mapMetalnessRepeat, m.mapMetalnessOffset, m.mapMetalnessWrap, m.mapMetalnessAnisotropy );
  194. break;
  195. case 'mapMetalnessRepeat':
  196. case 'mapMetalnessOffset':
  197. case 'mapMetalnessWrap':
  198. case 'mapMetalnessAnisotropy':
  199. break;
  200. case 'mapRoughness':
  201. json.roughnessMap = loadTexture( value, m.mapRoughnessRepeat, m.mapRoughnessOffset, m.mapRoughnessWrap, m.mapRoughnessAnisotropy );
  202. break;
  203. case 'mapRoughnessRepeat':
  204. case 'mapRoughnessOffset':
  205. case 'mapRoughnessWrap':
  206. case 'mapRoughnessAnisotropy':
  207. break;
  208. case 'mapAlpha':
  209. json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
  210. break;
  211. case 'mapAlphaRepeat':
  212. case 'mapAlphaOffset':
  213. case 'mapAlphaWrap':
  214. case 'mapAlphaAnisotropy':
  215. break;
  216. case 'flipSided':
  217. json.side = BackSide;
  218. break;
  219. case 'doubleSided':
  220. json.side = DoubleSide;
  221. break;
  222. case 'transparency':
  223. console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
  224. json.opacity = value;
  225. break;
  226. case 'depthTest':
  227. case 'depthWrite':
  228. case 'colorWrite':
  229. case 'opacity':
  230. case 'reflectivity':
  231. case 'transparent':
  232. case 'visible':
  233. case 'wireframe':
  234. json[ name ] = value;
  235. break;
  236. case 'vertexColors':
  237. if ( value === true ) json.vertexColors = VertexColors;
  238. if ( value === 'face' ) json.vertexColors = FaceColors;
  239. break;
  240. default:
  241. console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
  242. break;
  243. }
  244. }
  245. if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
  246. if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
  247. if ( json.opacity < 1 ) json.transparent = true;
  248. materialLoader.setTextures( textures );
  249. return materialLoader.parse( json );
  250. };
  251. } )()
  252. };
  253. Loader.Handlers = {
  254. handlers: [],
  255. add: function ( regex, loader ) {
  256. this.handlers.push( regex, loader );
  257. },
  258. get: function ( file ) {
  259. var handlers = this.handlers;
  260. for ( var i = 0, l = handlers.length; i < l; i += 2 ) {
  261. var regex = handlers[ i ];
  262. var loader = handlers[ i + 1 ];
  263. if ( regex.test( file ) ) {
  264. return loader;
  265. }
  266. }
  267. return null;
  268. }
  269. };
  270. export { Loader };