Loader.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.Loader = function ( showStatus ) {
  5. this.showStatus = showStatus;
  6. this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null;
  7. this.imageLoader = new THREE.ImageLoader();
  8. this.onLoadStart = function () {};
  9. this.onLoadProgress = function () {};
  10. this.onLoadComplete = function () {};
  11. };
  12. THREE.Loader.prototype = {
  13. constructor: THREE.Loader,
  14. crossOrigin: undefined,
  15. addStatusElement: function () {
  16. var e = document.createElement( 'div' );
  17. e.style.position = 'absolute';
  18. e.style.right = '0px';
  19. e.style.top = '0px';
  20. e.style.fontSize = '0.8em';
  21. e.style.textAlign = 'left';
  22. e.style.background = 'rgba(0,0,0,0.25)';
  23. e.style.color = '#fff';
  24. e.style.width = '120px';
  25. e.style.padding = '0.5em 0.5em 0.5em 0.5em';
  26. e.style.zIndex = 1000;
  27. e.innerHTML = 'Loading ...';
  28. return e;
  29. },
  30. updateProgress: function ( progress ) {
  31. var message = 'Loaded ';
  32. if ( progress.total ) {
  33. message += ( 100 * progress.loaded / progress.total ).toFixed( 0 ) + '%';
  34. } else {
  35. message += ( progress.loaded / 1024 ).toFixed( 2 ) + ' KB';
  36. }
  37. this.statusDomElement.innerHTML = message;
  38. },
  39. extractUrlBase: function ( url ) {
  40. var parts = url.split( '/' );
  41. if ( parts.length === 1 ) return './';
  42. parts.pop();
  43. return parts.join( '/' ) + '/';
  44. },
  45. initMaterials: function ( materials, texturePath ) {
  46. var array = [];
  47. for ( var i = 0; i < materials.length; ++ i ) {
  48. array[ i ] = this.createMaterial( materials[ i ], texturePath );
  49. }
  50. return array;
  51. },
  52. needsTangents: function ( materials ) {
  53. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  54. var m = materials[ i ];
  55. if ( m instanceof THREE.ShaderMaterial ) return true;
  56. }
  57. return false;
  58. },
  59. createMaterial: function ( m, texturePath ) {
  60. var scope = this;
  61. function nearest_pow2( n ) {
  62. var l = Math.log( n ) / Math.LN2;
  63. return Math.pow( 2, Math.round( l ) );
  64. }
  65. function create_texture( where, name, sourceFile, repeat, offset, wrap, anisotropy ) {
  66. var fullPath = texturePath + sourceFile;
  67. var texture;
  68. var loader = THREE.Loader.Handlers.get( fullPath );
  69. if ( loader !== null ) {
  70. texture = loader.load( fullPath );
  71. } else {
  72. texture = new THREE.Texture();
  73. loader = scope.imageLoader;
  74. loader.crossOrigin = scope.crossOrigin;
  75. loader.load( fullPath, function ( image ) {
  76. if ( THREE.Math.isPowerOfTwo( image.width ) === false ||
  77. THREE.Math.isPowerOfTwo( image.height ) === false ) {
  78. var width = nearest_pow2( image.width );
  79. var height = nearest_pow2( image.height );
  80. var canvas = document.createElement( 'canvas' );
  81. canvas.width = width;
  82. canvas.height = height;
  83. var context = canvas.getContext( '2d' );
  84. context.drawImage( image, 0, 0, width, height );
  85. texture.image = canvas;
  86. } else {
  87. texture.image = image;
  88. }
  89. texture.needsUpdate = true;
  90. } );
  91. }
  92. texture.sourceFile = sourceFile;
  93. if ( repeat ) {
  94. texture.repeat.set( repeat[ 0 ], repeat[ 1 ] );
  95. if ( repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  96. if ( repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  97. }
  98. if ( offset ) {
  99. texture.offset.set( offset[ 0 ], offset[ 1 ] );
  100. }
  101. if ( wrap ) {
  102. var wrapMap = {
  103. 'repeat': THREE.RepeatWrapping,
  104. 'mirror': THREE.MirroredRepeatWrapping
  105. }
  106. if ( wrapMap[ wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ wrap[ 0 ] ];
  107. if ( wrapMap[ wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ wrap[ 1 ] ];
  108. }
  109. if ( anisotropy ) {
  110. texture.anisotropy = anisotropy;
  111. }
  112. where[ name ] = texture;
  113. }
  114. function rgb2hex( rgb ) {
  115. return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255;
  116. }
  117. // defaults
  118. var mtype = 'MeshLambertMaterial';
  119. var mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, bumpMap: null, wireframe: false };
  120. // parameters from model file
  121. if ( m.shading ) {
  122. var shading = m.shading.toLowerCase();
  123. if ( shading === 'phong' ) mtype = 'MeshPhongMaterial';
  124. else if ( shading === 'basic' ) mtype = 'MeshBasicMaterial';
  125. }
  126. if ( m.blending !== undefined && THREE[ m.blending ] !== undefined ) {
  127. mpars.blending = THREE[ m.blending ];
  128. }
  129. if ( m.transparent !== undefined ) {
  130. mpars.transparent = m.transparent;
  131. }
  132. if ( m.opacity !== undefined && m.opacity < 1.0 ) {
  133. mpars.transparent = true;
  134. }
  135. if ( m.depthTest !== undefined ) {
  136. mpars.depthTest = m.depthTest;
  137. }
  138. if ( m.depthWrite !== undefined ) {
  139. mpars.depthWrite = m.depthWrite;
  140. }
  141. if ( m.visible !== undefined ) {
  142. mpars.visible = m.visible;
  143. }
  144. if ( m.flipSided !== undefined ) {
  145. mpars.side = THREE.BackSide;
  146. }
  147. if ( m.doubleSided !== undefined ) {
  148. mpars.side = THREE.DoubleSide;
  149. }
  150. if ( m.wireframe !== undefined ) {
  151. mpars.wireframe = m.wireframe;
  152. }
  153. if ( m.vertexColors !== undefined ) {
  154. if ( m.vertexColors === 'face' ) {
  155. mpars.vertexColors = THREE.FaceColors;
  156. } else if ( m.vertexColors ) {
  157. mpars.vertexColors = THREE.VertexColors;
  158. }
  159. }
  160. // colors
  161. if ( m.colorDiffuse ) {
  162. mpars.color = rgb2hex( m.colorDiffuse );
  163. } else if ( m.DbgColor ) {
  164. mpars.color = m.DbgColor;
  165. }
  166. if ( m.colorSpecular ) {
  167. mpars.specular = rgb2hex( m.colorSpecular );
  168. }
  169. if ( m.colorEmissive ) {
  170. mpars.emissive = rgb2hex( m.colorEmissive );
  171. }
  172. // modifiers
  173. if ( m.transparency !== undefined ) {
  174. THREE.warn( 'THREE.Loader: transparency has been renamed to opacity' );
  175. m.opacity = m.transparency;
  176. }
  177. if ( m.opacity !== undefined ) {
  178. mpars.opacity = m.opacity;
  179. }
  180. if ( m.specularCoef ) {
  181. mpars.shininess = m.specularCoef;
  182. }
  183. // textures
  184. if ( m.mapDiffuse && texturePath ) {
  185. create_texture( mpars, 'map', m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
  186. }
  187. if ( m.mapLight && texturePath ) {
  188. create_texture( mpars, 'lightMap', m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
  189. }
  190. if ( m.mapBump && texturePath ) {
  191. create_texture( mpars, 'bumpMap', m.mapBump, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
  192. }
  193. if ( m.mapNormal && texturePath ) {
  194. create_texture( mpars, 'normalMap', m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
  195. }
  196. if ( m.mapSpecular && texturePath ) {
  197. create_texture( mpars, 'specularMap', m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
  198. }
  199. if ( m.mapAlpha && texturePath ) {
  200. create_texture( mpars, 'alphaMap', m.mapAlpha, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
  201. }
  202. //
  203. if ( m.mapBumpScale ) {
  204. mpars.bumpScale = m.mapBumpScale;
  205. }
  206. if ( m.mapNormalFactor ) {
  207. mpars.normalScale = new THREE.Vector2( m.mapNormalFactor, m.mapNormalFactor );
  208. }
  209. var material = new THREE[ mtype ]( mpars );
  210. if ( m.DbgName !== undefined ) material.name = m.DbgName;
  211. return material;
  212. }
  213. };
  214. THREE.Loader.Handlers = {
  215. handlers: [],
  216. add: function ( regex, loader ) {
  217. this.handlers.push( regex, loader );
  218. },
  219. get: function ( file ) {
  220. for ( var i = 0, l = this.handlers.length; i < l; i += 2 ) {
  221. var regex = this.handlers[ i ];
  222. var loader = this.handlers[ i + 1 ];
  223. if ( regex.test( file ) ) {
  224. return loader;
  225. }
  226. }
  227. return null;
  228. }
  229. };