Loader.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. (function(){
  5. var globalImageLoader = null;
  6. THREE.Loader = function ( showStatus ) {
  7. this.showStatus = showStatus;
  8. this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null;
  9. this.onLoadStart = function () {};
  10. this.onLoadProgress = function () {};
  11. this.onLoadComplete = function () {};
  12. };
  13. THREE.Loader.prototype = {
  14. constructor: THREE.Loader,
  15. crossOrigin: undefined,
  16. addStatusElement: function () {
  17. var e = document.createElement( 'div' );
  18. e.style.position = 'absolute';
  19. e.style.right = '0px';
  20. e.style.top = '0px';
  21. e.style.fontSize = '0.8em';
  22. e.style.textAlign = 'left';
  23. e.style.background = 'rgba(0,0,0,0.25)';
  24. e.style.color = '#fff';
  25. e.style.width = '120px';
  26. e.style.padding = '0.5em 0.5em 0.5em 0.5em';
  27. e.style.zIndex = 1000;
  28. e.innerHTML = 'Loading ...';
  29. return e;
  30. },
  31. updateProgress: function ( progress ) {
  32. var message = 'Loaded ';
  33. if ( progress.total ) {
  34. message += ( 100 * progress.loaded / progress.total ).toFixed( 0 ) + '%';
  35. } else {
  36. message += ( progress.loaded / 1024 ).toFixed( 2 ) + ' KB';
  37. }
  38. this.statusDomElement.innerHTML = message;
  39. },
  40. extractUrlBase: function ( url ) {
  41. var parts = url.split( '/' );
  42. if ( parts.length === 1 ) return './';
  43. parts.pop();
  44. return parts.join( '/' ) + '/';
  45. },
  46. initMaterials: function ( materials, texturePath, crossOrigin ) {
  47. var array = [];
  48. for ( var i = 0; i < materials.length; ++ i ) {
  49. array[ i ] = this.createMaterial( materials[ i ], texturePath, crossOrigin );
  50. }
  51. return array;
  52. },
  53. needsTangents: function ( materials ) {
  54. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  55. var m = materials[ i ];
  56. if ( m instanceof THREE.ShaderMaterial ) return true;
  57. }
  58. return false;
  59. },
  60. createMaterial: function ( m, texturePath, crossOrigin ) {
  61. var scope = this;
  62. if ( crossOrigin === undefined && scope.crossOrigin !== undefined ) crossOrigin = scope.crossOrigin;
  63. if ( globalImageLoader === null ) globalImageLoader = new THREE.ImageLoader();
  64. function nearest_pow2( n ) {
  65. var l = Math.log( n ) / Math.LN2;
  66. return Math.pow( 2, Math.round( l ) );
  67. }
  68. function create_texture( where, name, sourceFile, repeat, offset, wrap, anisotropy ) {
  69. var fullPath = texturePath + sourceFile;
  70. var texture;
  71. var loader = THREE.Loader.Handlers.get( fullPath );
  72. if ( loader !== null ) {
  73. texture = loader.load( fullPath );
  74. } else {
  75. texture = new THREE.Texture();
  76. loader = globalImageLoader;
  77. loader.setCrossOrigin( crossOrigin );
  78. loader.load( fullPath, function ( image ) {
  79. if ( THREE.Math.isPowerOfTwo( image.width ) === false ||
  80. THREE.Math.isPowerOfTwo( image.height ) === false ) {
  81. var width = nearest_pow2( image.width );
  82. var height = nearest_pow2( image.height );
  83. var canvas = document.createElement( 'canvas' );
  84. canvas.width = width;
  85. canvas.height = height;
  86. var context = canvas.getContext( '2d' );
  87. context.drawImage( image, 0, 0, width, height );
  88. texture.image = canvas;
  89. } else {
  90. texture.image = image;
  91. }
  92. texture.needsUpdate = true;
  93. } );
  94. }
  95. texture.sourceFile = sourceFile;
  96. if ( repeat ) {
  97. texture.repeat.set( repeat[ 0 ], repeat[ 1 ] );
  98. if ( repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  99. if ( repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  100. }
  101. if ( offset ) {
  102. texture.offset.set( offset[ 0 ], offset[ 1 ] );
  103. }
  104. if ( wrap ) {
  105. var wrapMap = {
  106. 'repeat': THREE.RepeatWrapping,
  107. 'mirror': THREE.MirroredRepeatWrapping
  108. }
  109. if ( wrapMap[ wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ wrap[ 0 ] ];
  110. if ( wrapMap[ wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ wrap[ 1 ] ];
  111. }
  112. if ( anisotropy ) {
  113. texture.anisotropy = anisotropy;
  114. }
  115. where[ name ] = texture;
  116. }
  117. function rgb2hex( rgb ) {
  118. return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255;
  119. }
  120. // defaults
  121. var mtype = 'MeshLambertMaterial';
  122. var mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, bumpMap: null, wireframe: false };
  123. // parameters from model file
  124. if ( m.shading ) {
  125. var shading = m.shading.toLowerCase();
  126. if ( shading === 'phong' ) mtype = 'MeshPhongMaterial';
  127. else if ( shading === 'basic' ) mtype = 'MeshBasicMaterial';
  128. }
  129. if ( m.blending !== undefined && THREE[ m.blending ] !== undefined ) {
  130. mpars.blending = THREE[ m.blending ];
  131. }
  132. if ( m.transparent !== undefined || m.opacity < 1.0 ) {
  133. mpars.transparent = m.transparent;
  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.colorAmbient ) {
  170. mpars.ambient = rgb2hex( m.colorAmbient );
  171. }
  172. if ( m.colorEmissive ) {
  173. mpars.emissive = rgb2hex( m.colorEmissive );
  174. }
  175. // modifiers
  176. if ( m.transparency ) {
  177. mpars.opacity = m.transparency;
  178. }
  179. if ( m.specularCoef ) {
  180. mpars.shininess = m.specularCoef;
  181. }
  182. // textures
  183. if ( m.mapDiffuse && texturePath ) {
  184. create_texture( mpars, 'map', m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
  185. }
  186. if ( m.mapLight && texturePath ) {
  187. create_texture( mpars, 'lightMap', m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
  188. }
  189. if ( m.mapBump && texturePath ) {
  190. create_texture( mpars, 'bumpMap', m.mapBump, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
  191. }
  192. if ( m.mapNormal && texturePath ) {
  193. create_texture( mpars, 'normalMap', m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
  194. }
  195. if ( m.mapSpecular && texturePath ) {
  196. create_texture( mpars, 'specularMap', m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
  197. }
  198. if ( m.mapAlpha && texturePath ) {
  199. create_texture( mpars, 'alphaMap', m.mapAlpha, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
  200. }
  201. //
  202. if ( m.mapBumpScale ) {
  203. mpars.bumpScale = m.mapBumpScale;
  204. }
  205. // special case for normal mapped material
  206. if ( m.mapNormal ) {
  207. var shader = THREE.ShaderLib[ 'normalmap' ];
  208. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  209. uniforms[ 'tNormal' ].value = mpars.normalMap;
  210. if ( m.mapNormalFactor ) {
  211. uniforms[ 'uNormalScale' ].value.set( m.mapNormalFactor, m.mapNormalFactor );
  212. }
  213. if ( mpars.map ) {
  214. uniforms[ 'tDiffuse' ].value = mpars.map;
  215. uniforms[ 'enableDiffuse' ].value = true;
  216. }
  217. if ( mpars.specularMap ) {
  218. uniforms[ 'tSpecular' ].value = mpars.specularMap;
  219. uniforms[ 'enableSpecular' ].value = true;
  220. }
  221. if ( mpars.lightMap ) {
  222. uniforms[ 'tAO' ].value = mpars.lightMap;
  223. uniforms[ 'enableAO' ].value = true;
  224. }
  225. // for the moment don't handle displacement texture
  226. uniforms[ 'diffuse' ].value.setHex( mpars.color );
  227. uniforms[ 'specular' ].value.setHex( mpars.specular );
  228. uniforms[ 'ambient' ].value.setHex( mpars.ambient );
  229. uniforms[ 'shininess' ].value = mpars.shininess;
  230. if ( mpars.opacity !== undefined ) {
  231. uniforms[ 'opacity' ].value = mpars.opacity;
  232. }
  233. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  234. var material = new THREE.ShaderMaterial( parameters );
  235. if ( mpars.transparent ) {
  236. material.transparent = true;
  237. }
  238. } else {
  239. var material = new THREE[ mtype ]( mpars );
  240. }
  241. if ( m.DbgName !== undefined ) material.name = m.DbgName;
  242. return material;
  243. }
  244. };
  245. THREE.Loader.Handlers = {
  246. handlers: [],
  247. add: function ( regex, loader ) {
  248. this.handlers.push( regex, loader );
  249. },
  250. get: function ( file ) {
  251. for ( var i = 0, l = this.handlers.length; i < l; i += 2 ) {
  252. var regex = this.handlers[ i ];
  253. var loader = this.handlers[ i + 1 ];
  254. if ( regex.test( file ) ) {
  255. return loader;
  256. }
  257. }
  258. return null;
  259. }
  260. };
  261. })();