Loader.js 8.2 KB

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