Loader.js 6.8 KB

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