Loader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. addStatusElement: function () {
  13. var e = document.createElement( "div" );
  14. e.style.position = "absolute";
  15. e.style.right = "0px";
  16. e.style.top = "0px";
  17. e.style.fontSize = "0.8em";
  18. e.style.textAlign = "left";
  19. e.style.background = "rgba(0,0,0,0.25)";
  20. e.style.color = "#fff";
  21. e.style.width = "120px";
  22. e.style.padding = "0.5em 0.5em 0.5em 0.5em";
  23. e.style.zIndex = 1000;
  24. e.innerHTML = "Loading ...";
  25. return e;
  26. },
  27. updateProgress: function ( progress ) {
  28. var message = "Loaded ";
  29. if ( progress.total ) {
  30. message += ( 100 * progress.loaded / progress.total ).toFixed(0) + "%";
  31. } else {
  32. message += ( progress.loaded / 1000 ).toFixed(2) + " KB";
  33. }
  34. this.statusDomElement.innerHTML = message;
  35. },
  36. extractUrlbase: function( url ) {
  37. var chunks = url.split( "/" );
  38. chunks.pop();
  39. return chunks.join( "/" );
  40. },
  41. init_materials: function( scope, materials, texture_path ) {
  42. scope.materials = [];
  43. for ( var i = 0; i < materials.length; ++i ) {
  44. scope.materials[ i ] = [ THREE.Loader.prototype.createMaterial( materials[ i ], texture_path ) ];
  45. }
  46. },
  47. createMaterial: function ( m, texture_path ) {
  48. function is_pow2( n ) {
  49. var l = Math.log( n ) / Math.LN2;
  50. return Math.floor( l ) == l;
  51. }
  52. function nearest_pow2( n ) {
  53. var l = Math.log( n ) / Math.LN2;
  54. return Math.pow( 2, Math.round( l ) );
  55. }
  56. function load_image( where, url ) {
  57. var image = new Image();
  58. image.onload = function () {
  59. if ( !is_pow2( this.width ) || !is_pow2( this.height ) ) {
  60. var w = nearest_pow2( this.width ),
  61. h = nearest_pow2( this.height );
  62. where.image.width = w;
  63. where.image.height = h;
  64. where.image.getContext("2d").drawImage( this, 0, 0, w, h );
  65. } else {
  66. where.image = this;
  67. }
  68. where.needsUpdate = true;
  69. };
  70. image.src = url;
  71. }
  72. var material, mtype, mpars, texture, color, vertexColors;
  73. // defaults
  74. mtype = "MeshLambertMaterial";
  75. // vertexColors
  76. mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, wireframe: m.wireframe };
  77. // parameters from model file
  78. if ( m.shading ) {
  79. if ( m.shading == "Phong" ) mtype = "MeshPhongMaterial";
  80. else if ( m.shading == "Basic" ) mtype = "MeshBasicMaterial";
  81. }
  82. if ( m.blending ) {
  83. if ( m.blending == "Additive" ) mpars.blending = THREE.AdditiveBlending;
  84. else if ( m.blending == "Subtractive" ) mpars.blending = THREE.SubtractiveBlending;
  85. else if ( m.blending == "Multiply" ) mpars.blending = THREE.MultiplyBlending;
  86. }
  87. if ( m.transparent !== undefined || m.opacity < 1.0 ) {
  88. mpars.transparent = m.transparent;
  89. }
  90. if ( m.depthTest !== undefined ) {
  91. mpars.depthTest = m.depthTest;
  92. }
  93. if ( m.vertexColors !== undefined ) {
  94. if ( m.vertexColors == "face" ) {
  95. mpars.vertexColors = THREE.FaceColors;
  96. } else if ( m.vertexColors ) {
  97. mpars.vertexColors = THREE.VertexColors;
  98. }
  99. }
  100. if ( m.mapDiffuse && texture_path ) {
  101. texture = document.createElement( 'canvas' );
  102. mpars.map = new THREE.Texture( texture );
  103. mpars.map.sourceFile = m.mapDiffuse;
  104. if( m.mapDiffuseRepeat ) {
  105. mpars.map.repeat.set( m.mapDiffuseRepeat[ 0 ], m.mapDiffuseRepeat[ 1 ] );
  106. mpars.map.wrapS = mpars.map.wrapT = THREE.RepeatWrapping;
  107. }
  108. if( m.mapDiffuseOffset ) {
  109. mpars.map.offset.set( m.mapDiffuseOffset[ 0 ], m.mapDiffuseOffset[ 1 ] );
  110. }
  111. load_image( mpars.map, texture_path + "/" + m.mapDiffuse );
  112. } else if ( m.colorDiffuse ) {
  113. color = ( m.colorDiffuse[0] * 255 << 16 ) + ( m.colorDiffuse[1] * 255 << 8 ) + m.colorDiffuse[2] * 255;
  114. mpars.color = color;
  115. mpars.opacity = m.transparency;
  116. } else if ( m.DbgColor ) {
  117. mpars.color = m.DbgColor;
  118. }
  119. if ( m.mapLight && texture_path ) {
  120. texture = document.createElement( 'canvas' );
  121. mpars.lightMap = new THREE.Texture( texture );
  122. mpars.lightMap.sourceFile = m.mapLight;
  123. if( m.mapLightmapRepeat ) {
  124. mpars.lightMap.repeat.set( m.mapLightRepeat[ 0 ], m.mapLightRepeat[ 1 ] );
  125. mpars.lightMap.wrapS = mpars.lightMap.wrapT = THREE.RepeatWrapping;
  126. }
  127. if( m.mapLightmapOffset ) {
  128. mpars.lightMap.offset.set( m.mapLightmapOffset[ 0 ], m.mapLightmapOffset[ 1 ] );
  129. }
  130. load_image( mpars.lightMap, texture_path + "/" + m.mapLightmap );
  131. }
  132. material = new THREE[ mtype ]( mpars );
  133. return material;
  134. }
  135. };