Loader.js 7.5 KB

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