1
0

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