webgl_loader_msgpack.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!doctype html>
  2. <!--
  3. Requires msgpack-js.js
  4. https://github.com/creationix/msgpack-js-browser
  5. (ported from https://github.com/creationix/msgpack-js)
  6. -->
  7. <html lang='en'>
  8. <head>
  9. <title>three.js webgl - msgpack loader</title>
  10. <meta charset='utf-8'>
  11. <meta name='viewport' content='width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
  12. <style>
  13. body {
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #viewport {
  18. position: absolute;
  19. width: 100%;
  20. height: 100%;
  21. background: #1b1c1e;
  22. background-image: linear-gradient(#7d8fa3, #1b1c1e);
  23. }
  24. #info {
  25. color: #fff;
  26. position: absolute;
  27. top: 10px;
  28. width: 100%;
  29. text-align: center;
  30. z-index: 100;
  31. display:block;
  32. }
  33. a { color: orange }
  34. </style>
  35. <script src='../build/three.min.js'></script>
  36. <script src='js/controls/OrbitControls.js'></script>
  37. <script src='js/Detector.js'></script>
  38. <script src='js/libs/stats.min.js'></script>
  39. <script src='js/libs/require.js'></script>
  40. </head>
  41. <body>
  42. <div id='info'>
  43. <p>Robo Pigeon, from <a href='http://www.tearsofsteel.org' target='_blank'>Tears of Steel</a>, is licensed under
  44. <a href='http://creativecommons.org/licenses/by/3.0/' target='_blank'>Creative Commons Attribution 3.0</a>.
  45. </p>
  46. </div>
  47. <div id='viewport'></div>
  48. <script>
  49. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  50. requirejs.config( { baseUrl: 'js/libs' } );
  51. var container, scene, camera, renderer;
  52. function render() {
  53. renderer.render( scene, camera );
  54. }
  55. function threePointLight() {
  56. var directionalLight = new THREE.DirectionalLight( 0xb8b8b8 );
  57. directionalLight.position.set(1, 1, 1).normalize();
  58. directionalLight.intensity = 1.0;
  59. scene.add( directionalLight );
  60. directionalLight = new THREE.DirectionalLight( 0xb8b8b8 );
  61. directionalLight.position.set(-1, 0.6, 0.5).normalize();
  62. directionalLight.intensity = 0.5;
  63. scene.add(directionalLight);
  64. directionalLight = new THREE.DirectionalLight();
  65. directionalLight.position.set(-0.3, 0.6, -0.8).normalize( 0xb8b8b8 );
  66. directionalLight.intensity = 0.45;
  67. scene.add(directionalLight);
  68. }
  69. function setupScene( result ) {
  70. scene = result;
  71. scene.add( new THREE.GridHelper( 10, 2.5 ) );
  72. threePointLight();
  73. render();
  74. }
  75. function loadMSGPack() {
  76. require(['msgpack-js'], function ( msgpack ) {
  77. var xhr = new XMLHttpRequest();
  78. xhr.open('GET', 'scenes/robo_pigeon.pack', true);
  79. xhr.responseType = 'arraybuffer';
  80. xhr.onload = function( e ) {
  81. var decoded = msgpack.decode( this.response );
  82. var loader = new THREE.ObjectLoader();
  83. setupScene( loader.parse( decoded ) );
  84. };
  85. xhr.send();
  86. } );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = container.offsetWidth / container.offsetHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( container.offsetWidth, container.offsetHeight );
  92. render();
  93. }
  94. function init() {
  95. scene = new THREE.Scene();
  96. scene.add( new THREE.GridHelper( 10, 2.5 ) );
  97. container = document.getElementById('viewport');
  98. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  99. renderer.setSize( container.offsetWidth, container.offsetHeight );
  100. renderer.setClearColor( 0x000000, 0 );
  101. container.appendChild( renderer.domElement );
  102. var aspect = container.offsetWidth / container.offsetHeight;
  103. camera = new THREE.PerspectiveCamera( 60, aspect, 0.01, 50 );
  104. orbit = new THREE.OrbitControls( camera, container );
  105. orbit.addEventListener( 'change', render );
  106. camera.position.z = 5;
  107. camera.position.x = 5;
  108. camera.position.y = 5;
  109. var target = new THREE.Vector3( 0, 1, 0 );
  110. camera.lookAt( target );
  111. orbit.target = target;
  112. camera.updateProjectionMatrix();
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. loadMSGPack();
  115. }
  116. init();
  117. </script>
  118. </body>
  119. </html>