Loader.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. addStatusElement: function () {
  14. var e = document.createElement( "div" );
  15. e.style.position = "absolute";
  16. e.style.right = "0px";
  17. e.style.top = "0px";
  18. e.style.fontSize = "0.8em";
  19. e.style.textAlign = "left";
  20. e.style.background = "rgba(0,0,0,0.25)";
  21. e.style.color = "#fff";
  22. e.style.width = "120px";
  23. e.style.padding = "0.5em 0.5em 0.5em 0.5em";
  24. e.style.zIndex = 1000;
  25. e.innerHTML = "Loading ...";
  26. return e;
  27. },
  28. updateProgress: function ( progress ) {
  29. var message = "Loaded ";
  30. if ( progress.total ) {
  31. message += ( 100 * progress.loaded / progress.total ).toFixed(0) + "%";
  32. } else {
  33. message += ( progress.loaded / 1000 ).toFixed(2) + " KB";
  34. }
  35. this.statusDomElement.innerHTML = message;
  36. },
  37. extractUrlbase: function ( url ) {
  38. var parts = url.split( '/' );
  39. parts.pop();
  40. return parts.length < 1 ? '' : parts.join( '/' ) + '/';
  41. },
  42. initMaterials: function ( scope, materials, texture_path ) {
  43. scope.materials = [];
  44. for ( var i = 0; i < materials.length; ++ i ) {
  45. scope.materials[ i ] = THREE.Loader.prototype.createMaterial( materials[ i ], texture_path );
  46. }
  47. },
  48. hasNormals: function ( scope ) {
  49. var m, i, il = scope.materials.length;
  50. for( i = 0; i < il; i ++ ) {
  51. m = scope.materials[ i ];
  52. if ( m instanceof THREE.ShaderMaterial ) return true;
  53. }
  54. return false;
  55. },
  56. createMaterial: function ( m, texture_path ) {
  57. function is_pow2( n ) {
  58. var l = Math.log( n ) / Math.LN2;
  59. return Math.floor( l ) == l;
  60. }
  61. function nearest_pow2( n ) {
  62. var l = Math.log( n ) / Math.LN2;
  63. return Math.pow( 2, Math.round( l ) );
  64. }
  65. function load_image( where, url ) {
  66. var image = new Image();
  67. image.onload = function () {
  68. if ( !is_pow2( this.width ) || !is_pow2( this.height ) ) {
  69. var w = nearest_pow2( this.width ),
  70. h = nearest_pow2( this.height );
  71. where.image.width = w;
  72. where.image.height = h;
  73. where.image.getContext("2d").drawImage( this, 0, 0, w, h );
  74. } else {
  75. where.image = this;
  76. }
  77. where.needsUpdate = true;
  78. };
  79. image.src = url;
  80. }
  81. function create_texture( where, name, sourceFile, repeat, offset, wrap ) {
  82. var texture = document.createElement( 'canvas' );
  83. where[ name ] = new THREE.Texture( texture );
  84. where[ name ].sourceFile = sourceFile;
  85. if( repeat ) {
  86. where[ name ].repeat.set( repeat[ 0 ], repeat[ 1 ] );
  87. if ( repeat[ 0 ] != 1 ) where[ name ].wrapS = THREE.RepeatWrapping;
  88. if ( repeat[ 1 ] != 1 ) where[ name ].wrapT = THREE.RepeatWrapping;
  89. }
  90. if( offset ) {
  91. where[ name ].offset.set( offset[ 0 ], offset[ 1 ] );
  92. }
  93. if( wrap ) {
  94. var wrapMap = {
  95. "repeat" : THREE.RepeatWrapping,
  96. "mirror" : THREE.MirroredRepeatWrapping
  97. }
  98. if ( wrapMap[ wrap[ 0 ] ] !== undefined ) where[ name ].wrapS = wrapMap[ wrap[ 0 ] ];
  99. if ( wrapMap[ wrap[ 1 ] ] !== undefined ) where[ name ].wrapT = wrapMap[ wrap[ 1 ] ];
  100. }
  101. load_image( where[ name ], texture_path + "/" + sourceFile );
  102. }
  103. function rgb2hex( rgb ) {
  104. return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255;
  105. }
  106. var material, mtype, mpars,
  107. color, specular, ambient,
  108. vertexColors;
  109. // defaults
  110. mtype = "MeshLambertMaterial";
  111. // vertexColors
  112. mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, wireframe: m.wireframe };
  113. // parameters from model file
  114. if ( m.shading ) {
  115. if ( m.shading == "Phong" ) mtype = "MeshPhongMaterial";
  116. else if ( m.shading == "Basic" ) mtype = "MeshBasicMaterial";
  117. }
  118. if ( m.blending ) {
  119. if ( m.blending == "Additive" ) mpars.blending = THREE.AdditiveBlending;
  120. else if ( m.blending == "Subtractive" ) mpars.blending = THREE.SubtractiveBlending;
  121. else if ( m.blending == "Multiply" ) mpars.blending = THREE.MultiplyBlending;
  122. }
  123. if ( m.transparent !== undefined || m.opacity < 1.0 ) {
  124. mpars.transparent = m.transparent;
  125. }
  126. if ( m.depthTest !== undefined ) {
  127. mpars.depthTest = m.depthTest;
  128. }
  129. if ( m.vertexColors !== undefined ) {
  130. if ( m.vertexColors == "face" ) {
  131. mpars.vertexColors = THREE.FaceColors;
  132. } else if ( m.vertexColors ) {
  133. mpars.vertexColors = THREE.VertexColors;
  134. }
  135. }
  136. // colors
  137. if ( m.colorDiffuse ) {
  138. mpars.color = rgb2hex( m.colorDiffuse );
  139. } else if ( m.DbgColor ) {
  140. mpars.color = m.DbgColor;
  141. }
  142. if ( m.colorSpecular ) {
  143. mpars.specular = rgb2hex( m.colorSpecular );
  144. }
  145. if ( m.colorAmbient ) {
  146. mpars.ambient = rgb2hex( m.colorAmbient );
  147. }
  148. // modifiers
  149. if ( m.transparency ) {
  150. mpars.opacity = m.transparency;
  151. }
  152. if ( m.specularCoef ) {
  153. mpars.shininess = m.specularCoef;
  154. }
  155. // textures
  156. if ( m.mapDiffuse && texture_path ) {
  157. create_texture( mpars, "map", m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap );
  158. }
  159. if ( m.mapLight && texture_path ) {
  160. create_texture( mpars, "lightMap", m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap );
  161. }
  162. if ( m.mapNormal && texture_path ) {
  163. create_texture( mpars, "normalMap", m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap );
  164. }
  165. if ( m.mapSpecular && texture_path ) {
  166. create_texture( mpars, "specularMap", m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap );
  167. }
  168. // special case for normal mapped material
  169. if ( m.mapNormal ) {
  170. var shader = THREE.ShaderUtils.lib[ "normal" ];
  171. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  172. var diffuse = mpars.color;
  173. var specular = mpars.specular;
  174. var ambient = mpars.ambient;
  175. var shininess = mpars.shininess;
  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( diffuse );
  194. uniforms[ "uSpecularColor" ].value.setHex( specular );
  195. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  196. uniforms[ "uShininess" ].value = shininess;
  197. if ( mpars.opacity ) {
  198. uniforms[ "uOpacity" ].value = mpars.opacity;
  199. }
  200. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  201. material = new THREE.ShaderMaterial( parameters );
  202. } else {
  203. material = new THREE[ mtype ]( mpars );
  204. }
  205. return material;
  206. }
  207. };
粤ICP备19079148号