scene_test.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - scene loader test</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#000;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family:georgia;
  14. text-align:center;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: center;
  23. z-index:100;
  24. }
  25. #progress {
  26. color:red;
  27. top:7em;
  28. width: 100%;
  29. font-size:3em;
  30. font-variant:small-caps;
  31. font-weight:bold;
  32. position:absolute;
  33. z-index:100;
  34. text-align: center;
  35. text-shadow: #000 0px 0px 10px;
  36. display:none;
  37. }
  38. #start {
  39. color:#fff;
  40. text-shadow: #000 0px 0px 2px;
  41. padding:0.1em 0.3em;
  42. width:3em;
  43. text-align: center;
  44. display:none;
  45. }
  46. .shadow {
  47. -moz-box-shadow: 0px 0px 5px #000;
  48. -webkit-box-shadow: 0px 0px 5px #000;
  49. box-shadow: 0px 0px 5px #000;
  50. }
  51. #progressbar {
  52. text-align: center;
  53. background: white;
  54. width: 250px;
  55. height: 10px;
  56. }
  57. #bar {
  58. background:#d00;
  59. width:50px;
  60. height:10px;
  61. }
  62. .enabled {
  63. color: lime!important;
  64. cursor:pointer;
  65. }
  66. .enabled:hover {
  67. text-shadow: #0f0 0px 0px 5px !important;
  68. }
  69. .disabled {
  70. background:gray;
  71. cursor:default;
  72. }
  73. a { color:red }
  74. canvas { pointer-events:none; z-index:10; }
  75. #log { position:absolute; top:0; display:block; text-align:left; z-index:1000; pointer-events:none; }
  76. #scene_explorer {
  77. background:transparent;
  78. color:#fff;
  79. width:200px;
  80. position:absolute;
  81. text-align:left;
  82. top:0px;
  83. z-index:200;
  84. overflow:auto;
  85. }
  86. #section_exp {
  87. background:rgba(0,0,50,0.5);
  88. padding:0.5em 0;
  89. display:none;
  90. }
  91. #scene_explorer h3 {
  92. font-size:1em;
  93. padding:0;
  94. margin:0;
  95. color:orange;
  96. }
  97. #scene_explorer a {
  98. color:#555;
  99. font-weight:bold;
  100. text-decoration:none;
  101. font-size:1.2em;
  102. font-family:Monospace;
  103. }
  104. #scene_explorer a:hover {
  105. background:#555;
  106. color:rgba(0,0,50,1);
  107. }
  108. .part {
  109. display:none;
  110. padding:0 0 0.5em 2em;
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div id="info">
  116. <a href="http://github.com/mrdoob/three.js">three.js</a> - scene loader test
  117. </div>
  118. <div id="scene_explorer">
  119. <a id="plus_exp" href="#">[+]</a>
  120. <div id="section_exp"></div>
  121. </div>
  122. <div id="progress">
  123. <span id="message">Loading ...</span>
  124. <center>
  125. <div id="progressbar" class="shadow"><div id="bar" class="shadow"></div></div>
  126. <div id="start" class="disabled">Start</div>
  127. </center>
  128. </div>
  129. <pre id="log"></pre>
  130. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  131. <script type="text/javascript" src="js/Stats.js"></script>
  132. <script type="text/javascript">
  133. var SCREEN_WIDTH = window.innerWidth;
  134. var SCREEN_HEIGHT = window.innerHeight;
  135. var container,stats;
  136. var camera, scene, loaded;
  137. var renderer;
  138. var mesh, zmesh, geometry;
  139. var mouseX = 0, mouseY = 0;
  140. var windowHalfX = window.innerWidth / 2;
  141. var windowHalfY = window.innerHeight / 2;
  142. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  143. init();
  144. function $( id ) {
  145. return document.getElementById( id );
  146. }
  147. function handle_update( result, pieces ) {
  148. refreshSceneView( result );
  149. renderer.initWebGLObjects( result.scene );
  150. var m, material, count = 0;
  151. for ( m in result.materials ) {
  152. material = result.materials[ m ];
  153. if ( ! ( material instanceof THREE.MeshFaceMaterial ) ) {
  154. if( !material.program ) {
  155. console.log(m);
  156. renderer.initMaterial( material, result.scene.lights, result.scene.fog );
  157. count += 1;
  158. if( count > pieces ) {
  159. //console.log("xxxxxxxxx");
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. function init() {
  167. container = document.createElement( 'div' );
  168. document.body.appendChild( container );
  169. var loadScene = createLoadScene();
  170. scene = loadScene.scene;
  171. camera = loadScene.camera;
  172. renderer = new THREE.WebGLRenderer();
  173. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  174. renderer.domElement.style.position = "relative";
  175. container.appendChild( renderer.domElement );
  176. $( "start" ).addEventListener( 'click', onStartClick, false );
  177. setInterval( loop, 1000/60 );
  178. var callback_progress = function( progress, result ) {
  179. var bar = 250,
  180. total = progress.total_models + progress.total_textures,
  181. loaded = progress.loaded_models + progress.loaded_textures;
  182. if ( total )
  183. bar = Math.floor( bar * loaded / total );
  184. $( "bar" ).style.width = bar + "px";
  185. count = 0;
  186. for ( var m in result.materials ) count++;
  187. handle_update( result, Math.floor( count/total ) );
  188. }
  189. var callback_sync = function( result ) {
  190. /*
  191. // uncomment to see progressive scene loading
  192. scene = result.scene;
  193. camera = result.currentCamera;
  194. camera.aspect = window.innerWidth / window.innerHeight;
  195. camera.updateProjectionMatrix();
  196. renderer.setClearColor( result.bgColor.hex, result.bgAlpha );
  197. */
  198. //handle_update( result, 1 );
  199. }
  200. var callback_async = function( result ) {
  201. loaded = result;
  202. var mat_veyron = result.geometries[ "veyron" ].materials;
  203. mat_veyron[ 0 ][ 0 ] = result.materials[ "interior" ];
  204. mat_veyron[ 1 ][ 0 ] = result.materials[ "chrome" ];
  205. mat_veyron[ 2 ][ 0 ] = result.materials[ "darkerchrome" ];
  206. mat_veyron[ 3 ][ 0 ] = result.materials[ "glass" ];
  207. mat_veyron[ 4 ][ 0 ] = result.materials[ "chrome" ];
  208. mat_veyron[ 5 ][ 0 ] = result.materials[ "chrome" ];
  209. mat_veyron[ 6 ][ 0 ] = result.materials[ "backlights" ];
  210. mat_veyron[ 7 ][ 0 ] = result.materials[ "backsignals" ];
  211. $( "message" ).style.display = "none";
  212. $( "progressbar" ).style.display = "none";
  213. $( "start" ).style.display = "block";
  214. $( "start" ).className = "enabled";
  215. handle_update( result, 1 );
  216. }
  217. $( "progress" ).style.display = "block";
  218. SceneUtils.loadScene( "scenes/test_scene.js", callback_sync, callback_async, callback_progress );
  219. stats = new Stats();
  220. stats.domElement.style.position = 'absolute';
  221. stats.domElement.style.top = '0px';
  222. stats.domElement.style.right = '0px';
  223. stats.domElement.style.zIndex = 100;
  224. container.appendChild( stats.domElement );
  225. $( "plus_exp" ).addEventListener( 'click', createToggle( "exp" ), false );
  226. }
  227. function setButtonActive( id ) {
  228. $( "start" ).style.backgroundColor = "green";
  229. }
  230. function onStartClick() {
  231. $( "progress" ).style.display = "none";
  232. scene = loaded.scene;
  233. camera = loaded.currentCamera;
  234. camera.aspect = window.innerWidth / window.innerHeight;
  235. camera.updateProjectionMatrix();
  236. renderer.setClearColor( loaded.bgColor.hex, loaded.bgAlpha );
  237. }
  238. function onDocumentMouseMove(event) {
  239. mouseX = ( event.clientX - windowHalfX );
  240. mouseY = ( event.clientY - windowHalfY );
  241. }
  242. function createLoadScene() {
  243. var result = {
  244. scene: new THREE.Scene(),
  245. camera: new THREE.Camera( 65, window.innerWidth / window.innerHeight, 1, 1000 )
  246. };
  247. result.camera.position.z = 100;
  248. var object, geometry, material, light, count = 100, range = 150;
  249. material = new THREE.MeshLambertMaterial( { color:0xffffff } );
  250. geometry = new Cube( 5, 5, 5 );
  251. for( var i = 0; i < count; i++ ) {
  252. object = new THREE.Mesh( geometry, material );
  253. object.position.x = ( Math.random() - 0.5 ) * range;
  254. object.position.y = ( Math.random() - 0.5 ) * range;
  255. object.position.z = ( Math.random() - 0.5 ) * range;
  256. object.rotation.x = Math.random() * 6;
  257. object.rotation.y = Math.random() * 6;
  258. object.rotation.z = Math.random() * 6;
  259. result.scene.addObject( object );
  260. }
  261. light = new THREE.PointLight( 0xffffff );
  262. result.scene.addLight( light );
  263. light = new THREE.DirectionalLight( 0x111111 );
  264. light.position.x = 1;
  265. result.scene.addLight( light );
  266. return result;
  267. }
  268. function loop() {
  269. camera.position.x += ( mouseX - camera.position.x ) * .001;
  270. camera.position.y += ( - mouseY - camera.position.y ) * .001;
  271. renderer.render( scene, camera );
  272. stats.update();
  273. }
  274. function log( text ) {
  275. var e = $("log");
  276. e.innerHTML = text + "<br/>" + e.innerHTML;
  277. }
  278. // Scene explorer user interface
  279. function toggle( id ) {
  280. var scn = $( "section_" + id ).style,
  281. btn = $( "plus_" + id );
  282. if ( scn.display == "block" ) {
  283. scn.display = "none";
  284. btn.innerHTML = "[+]";
  285. }
  286. else {
  287. scn.display = "block";
  288. btn.innerHTML = "[-]";
  289. }
  290. }
  291. function createToggle( label ) { return function() { toggle( label ) } };
  292. function refreshSceneView( result ) {
  293. $( "section_exp" ).innerHTML = generateSceneView( result );
  294. var config = [ "obj", "geo", "mat", "tex", "lit", "cam" ];
  295. for ( var i = 0; i < config.length; i++ )
  296. $( "plus_" + config[i] ).addEventListener( 'click', createToggle( config[i] ), false );
  297. }
  298. function generateSection( label, id, objects ) {
  299. var html = "";
  300. html += "<h3><a id='plus_" + id + "' href='#'>[+]</a> " + label + "</h3>";
  301. html += "<div id='section_" + id + "' class='part'>";
  302. for( var o in objects ) {
  303. html += o + "<br/>";
  304. }
  305. html += "</div>";
  306. return html;
  307. }
  308. function generateSceneView( result ) {
  309. var config = [
  310. [ "Objects", "obj", result.objects ],
  311. [ "Geometries", "geo", result.geometries ],
  312. [ "Materials", "mat", result.materials ],
  313. [ "Textures", "tex", result.textures ],
  314. [ "Lights", "lit", result.lights ],
  315. [ "Cameras", "cam", result.cameras ]
  316. ];
  317. var html = "";
  318. for ( var i = 0; i < config.length; i++ )
  319. html += generateSection( config[i][0], config[i][1], config[i][2] );
  320. return html;
  321. }
  322. </script>
  323. </body>
  324. </html>