webgl_scene_test.html 10 KB

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