Loader.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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: '',
  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, texture_path ) {
  44. scope.materials = [];
  45. for ( var i = 0; i < materials.length; ++ i ) {
  46. scope.materials[ i ] = THREE.Loader.prototype.createMaterial( materials[ i ], texture_path );
  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, texture_path ) {
  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 ) {
  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. load_image( where[ name ], texture_path + "/" + sourceFile );
  105. }
  106. function rgb2hex( rgb ) {
  107. return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255;
  108. }
  109. var material, mtype, mpars,
  110. color, specular, ambient,
  111. vertexColors;
  112. // defaults
  113. mtype = "MeshLambertMaterial";
  114. // vertexColors
  115. mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, wireframe: m.wireframe };
  116. // parameters from model file
  117. if ( m.shading ) {
  118. if ( m.shading == "Phong" ) mtype = "MeshPhongMaterial";
  119. else if ( m.shading == "Basic" ) mtype = "MeshBasicMaterial";
  120. }
  121. if ( m.blending ) {
  122. if ( m.blending == "Additive" ) mpars.blending = THREE.AdditiveBlending;
  123. else if ( m.blending == "Subtractive" ) mpars.blending = THREE.SubtractiveBlending;
  124. else if ( m.blending == "Multiply" ) mpars.blending = THREE.MultiplyBlending;
  125. }
  126. if ( m.transparent !== undefined || m.opacity < 1.0 ) {
  127. mpars.transparent = m.transparent;
  128. }
  129. if ( m.depthTest !== undefined ) {
  130. mpars.depthTest = m.depthTest;
  131. }
  132. if ( m.vertexColors !== undefined ) {
  133. if ( m.vertexColors == "face" ) {
  134. mpars.vertexColors = THREE.FaceColors;
  135. } else if ( m.vertexColors ) {
  136. mpars.vertexColors = THREE.VertexColors;
  137. }
  138. }
  139. // colors
  140. if ( m.colorDiffuse ) {
  141. mpars.color = rgb2hex( m.colorDiffuse );
  142. } else if ( m.DbgColor ) {
  143. mpars.color = m.DbgColor;
  144. }
  145. if ( m.colorSpecular ) {
  146. mpars.specular = rgb2hex( m.colorSpecular );
  147. }
  148. if ( m.colorAmbient ) {
  149. mpars.ambient = rgb2hex( m.colorAmbient );
  150. }
  151. // modifiers
  152. if ( m.transparency ) {
  153. mpars.opacity = m.transparency;
  154. }
  155. if ( m.specularCoef ) {
  156. mpars.shininess = m.specularCoef;
  157. }
  158. // textures
  159. if ( m.mapDiffuse && texture_path ) {
  160. create_texture( mpars, "map", m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap );
  161. }
  162. if ( m.mapLight && texture_path ) {
  163. create_texture( mpars, "lightMap", m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap );
  164. }
  165. if ( m.mapNormal && texture_path ) {
  166. create_texture( mpars, "normalMap", m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap );
  167. }
  168. if ( m.mapSpecular && texture_path ) {
  169. create_texture( mpars, "specularMap", m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap );
  170. }
  171. // special case for normal mapped material
  172. if ( m.mapNormal ) {
  173. var shader = THREE.ShaderUtils.lib[ "normal" ];
  174. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  175. var diffuse = mpars.color;
  176. var specular = mpars.specular;
  177. var ambient = mpars.ambient;
  178. var shininess = mpars.shininess;
  179. uniforms[ "tNormal" ].texture = mpars.normalMap;
  180. if ( m.mapNormalFactor ) {
  181. uniforms[ "uNormalScale" ].value = m.mapNormalFactor;
  182. }
  183. if ( mpars.map ) {
  184. uniforms[ "tDiffuse" ].texture = mpars.map;
  185. uniforms[ "enableDiffuse" ].value = true;
  186. }
  187. if ( mpars.specularMap ) {
  188. uniforms[ "tSpecular" ].texture = mpars.specularMap;
  189. uniforms[ "enableSpecular" ].value = true;
  190. }
  191. if ( mpars.lightMap ) {
  192. uniforms[ "tAO" ].texture = mpars.lightMap;
  193. uniforms[ "enableAO" ].value = true;
  194. }
  195. // for the moment don't handle displacement texture
  196. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  197. uniforms[ "uSpecularColor" ].value.setHex( specular );
  198. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  199. uniforms[ "uShininess" ].value = shininess;
  200. if ( mpars.opacity ) {
  201. uniforms[ "uOpacity" ].value = mpars.opacity;
  202. }
  203. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  204. material = new THREE.ShaderMaterial( parameters );
  205. } else {
  206. material = new THREE[ mtype ]( mpars );
  207. }
  208. if ( m.DbgName !== undefined ) material.name = m.DbgName;
  209. return material;
  210. }
  211. };