webgl_scene_test_blender.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - io - scene loader [blender]</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. #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 [blender]
  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. <script src="../build/Three.js"></script>
  130. <script src="js/Detector.js"></script>
  131. <script src="js/RequestAnimationFrame.js"></script>
  132. <script src="js/Stats.js"></script>
  133. <script>
  134. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  135. var SCREEN_WIDTH = window.innerWidth;
  136. var SCREEN_HEIGHT = window.innerHeight;
  137. var container,stats;
  138. var camera, scene, loaded;
  139. var renderer;
  140. var mesh, zmesh, geometry;
  141. var mouseX = 0, mouseY = 0;
  142. var windowHalfX = window.innerWidth / 2;
  143. var windowHalfY = window.innerHeight / 2;
  144. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  145. init();
  146. animate();
  147. function $( id ) {
  148. return document.getElementById( id );
  149. }
  150. function handle_update( result, pieces ) {
  151. refreshSceneView( result );
  152. renderer.initWebGLObjects( result.scene );
  153. var m, material, count = 0;
  154. for ( m in result.materials ) {
  155. material = result.materials[ m ];
  156. if ( ! ( material instanceof THREE.MeshFaceMaterial ) ) {
  157. if( !material.program ) {
  158. console.log(m);
  159. renderer.initMaterial( material, result.scene.lights, result.scene.fog );
  160. count += 1;
  161. if( count > pieces ) {
  162. //console.log("xxxxxxxxx");
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. function init() {
  170. container = document.createElement( 'div' );
  171. document.body.appendChild( container );
  172. var loadScene = createLoadScene();
  173. scene = loadScene.scene;
  174. camera = loadScene.camera;
  175. renderer = new THREE.WebGLRenderer();
  176. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  177. renderer.domElement.style.position = "relative";
  178. container.appendChild( renderer.domElement );
  179. stats = new Stats();
  180. stats.domElement.style.position = 'absolute';
  181. stats.domElement.style.top = '0px';
  182. stats.domElement.style.right = '0px';
  183. stats.domElement.style.zIndex = 100;
  184. container.appendChild( stats.domElement );
  185. $( "start" ).addEventListener( 'click', onStartClick, false );
  186. var callbackProgress = function( progress, result ) {
  187. var bar = 250,
  188. total = progress.total_models + progress.total_textures,
  189. loaded = progress.loaded_models + progress.loaded_textures;
  190. if ( total )
  191. bar = Math.floor( bar * loaded / total );
  192. $( "bar" ).style.width = bar + "px";
  193. count = 0;
  194. for ( var m in result.materials ) count++;
  195. handle_update( result, Math.floor( count/total ) );
  196. }
  197. var callbackFinished = function( result ) {
  198. loaded = result;
  199. $( "message" ).style.display = "none";
  200. $( "progressbar" ).style.display = "none";
  201. $( "start" ).style.display = "block";
  202. $( "start" ).className = "enabled";
  203. handle_update( result, 1 );
  204. }
  205. $( "progress" ).style.display = "block";
  206. var loader = new THREE.SceneLoader();
  207. loader.callbackProgress = callbackProgress;
  208. loader.load( "obj/blenderscene/scene.js", callbackFinished );
  209. $( "plus_exp" ).addEventListener( 'click', createToggle( "exp" ), false );
  210. }
  211. function setButtonActive( id ) {
  212. $( "start" ).style.backgroundColor = "green";
  213. }
  214. function onStartClick() {
  215. $( "progress" ).style.display = "none";
  216. scene = loaded.scene;
  217. camera = loaded.currentCamera;
  218. camera.aspect = window.innerWidth / window.innerHeight;
  219. camera.updateProjectionMatrix();
  220. renderer.setClearColor( loaded.bgColor, loaded.bgAlpha );
  221. }
  222. function onDocumentMouseMove(event) {
  223. mouseX = ( event.clientX - windowHalfX );
  224. mouseY = ( event.clientY - windowHalfY );
  225. }
  226. function createLoadScene() {
  227. var result = {
  228. scene: new THREE.Scene(),
  229. camera: new THREE.Camera( 65, window.innerWidth / window.innerHeight, 1, 1000 )
  230. };
  231. result.camera.position.z = 100;
  232. result.camera.useTarget = true;
  233. var object, geometry, material, light, count = 500, range = 200;
  234. material = new THREE.MeshLambertMaterial( { color:0xffffff } );
  235. geometry = new THREE.CubeGeometry( 5, 5, 5 );
  236. for( var i = 0; i < count; i++ ) {
  237. object = new THREE.Mesh( geometry, material );
  238. object.position.x = ( Math.random() - 0.5 ) * range;
  239. object.position.y = ( Math.random() - 0.5 ) * range;
  240. object.position.z = ( Math.random() - 0.5 ) * range;
  241. object.rotation.x = Math.random() * 6;
  242. object.rotation.y = Math.random() * 6;
  243. object.rotation.z = Math.random() * 6;
  244. object.matrixAutoUpdate = false;
  245. object.updateMatrix();
  246. result.scene.add( object );
  247. }
  248. result.scene.matrixAutoUpdate = false;
  249. light = new THREE.PointLight( 0xffffff );
  250. result.scene.add( light );
  251. light = new THREE.DirectionalLight( 0x111111 );
  252. light.position.x = 1;
  253. result.scene.add( light );
  254. return result;
  255. }
  256. //
  257. function animate() {
  258. requestAnimationFrame( animate );
  259. render();
  260. stats.update();
  261. }
  262. function render() {
  263. camera.position.x += ( mouseX - camera.position.x ) * .001;
  264. camera.position.y += ( - mouseY - camera.position.y ) * .001;
  265. renderer.render( scene, camera );
  266. }
  267. // Scene explorer user interface
  268. function toggle( id ) {
  269. var scn = $( "section_" + id ).style,
  270. btn = $( "plus_" + id );
  271. if ( scn.display == "block" ) {
  272. scn.display = "none";
  273. btn.innerHTML = "[+]";
  274. }
  275. else {
  276. scn.display = "block";
  277. btn.innerHTML = "[-]";
  278. }
  279. }
  280. function createToggle( label ) { return function() { toggle( label ) } };
  281. function refreshSceneView( result ) {
  282. $( "section_exp" ).innerHTML = generateSceneView( result );
  283. var config = [ "obj", "geo", "mat", "tex", "lit", "cam" ];
  284. for ( var i = 0; i < config.length; i++ )
  285. $( "plus_" + config[i] ).addEventListener( 'click', createToggle( config[i] ), false );
  286. }
  287. function generateSection( label, id, objects ) {
  288. var html = "";
  289. html += "<h3><a id='plus_" + id + "' href='#'>[+]</a> " + label + "</h3>";
  290. html += "<div id='section_" + id + "' class='part'>";
  291. for( var o in objects ) {
  292. html += o + "<br/>";
  293. }
  294. html += "</div>";
  295. return html;
  296. }
  297. function generateSceneView( result ) {
  298. var config = [
  299. [ "Objects", "obj", result.objects ],
  300. [ "Geometries", "geo", result.geometries ],
  301. [ "Materials", "mat", result.materials ],
  302. [ "Textures", "tex", result.textures ],
  303. [ "Lights", "lit", result.lights ],
  304. [ "Cameras", "cam", result.cameras ]
  305. ];
  306. var html = "";
  307. for ( var i = 0; i < config.length; i++ )
  308. html += generateSection( config[i][0], config[i][1], config[i][2] );
  309. return html;
  310. }
  311. </script>
  312. </body>
  313. </html>