misc_controls_oculusrift.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - controls - oculus rift</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body {
  8. margin: 0px;
  9. background-color: #000000;
  10. overflow: hidden;
  11. }
  12. #info {
  13. position: absolute;
  14. top: 0px; width: 100%;
  15. color: #ffffff;
  16. padding: 5px;
  17. font-family:Monospace;
  18. font-size:13px;
  19. font-weight: bold;
  20. text-align:center;
  21. }
  22. a {
  23. color: #ff8800;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="http://threejs.org" target="_blank">three.js</a> - headtracking demo for oculus rift. requires <a href="https://github.com/possan/oculus-rest/" target="_blank">oculus-rest</a> to get headtracking coordinates working.<br />
  30. (left click: forward, a/s/w/d/r/f: move, h: hide text)
  31. </div>
  32. <script src="../build/three.min.js"></script>
  33. <script src="js/ImprovedNoise.js"></script>
  34. <script src="js/effects/OculusRiftEffect.js"></script>
  35. <script src="js/controls/FirstPersonControls.js"></script>
  36. <script src="js/controls/OculusControls.js"></script>
  37. <script>
  38. var camera, scene, renderer;
  39. var realcamera;
  40. var guiVisible = true;
  41. var mesh, effect, controls, oculuscontrol;
  42. var meshes = [];
  43. var meshparent = [];
  44. var meshparent2 = [];
  45. var cols = 50;
  46. var rows = 30;
  47. var tot = cols * rows;
  48. var clock = new THREE.Clock();
  49. var perlin;
  50. init();
  51. animate();
  52. function init() {
  53. renderer = new THREE.WebGLRenderer();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. perlin = new ImprovedNoise();
  56. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 1, 5000 );
  57. camera.position.x = 200;
  58. camera.position.y = 250;
  59. camera.position.z = -200;
  60. scene = new THREE.Scene();
  61. effect = new THREE.OculusRiftEffect( renderer, { worldScale: 1 } );
  62. effect.setSize( window.innerWidth, window.innerHeight );
  63. controls = new THREE.FirstPersonControls( camera );
  64. controls.movementSpeed = 4000;
  65. controls.lookSpeed = 3.0;
  66. controls.lookVertical = true;
  67. oculuscontrol = new THREE.OculusControls( camera );
  68. document.body.appendChild( renderer.domElement );
  69. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
  70. hemiLight.color.setHSL( 0.6, 1, 0.6 );
  71. hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
  72. hemiLight.position.set( 0, 500, 0 );
  73. scene.add( hemiLight );
  74. var geometry = new THREE.BoxGeometry( 100, 100, 200 );
  75. var texture = THREE.ImageUtils.loadTexture( 'textures/crate.gif' );
  76. texture.anisotropy = renderer.getMaxAnisotropy();
  77. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 20, shading: THREE.FlatShading, map: texture } );
  78. for (var i=0; i<tot; i++) {
  79. meshparent2[i] = new THREE.Object3D();
  80. scene.add( meshparent2[i] );
  81. meshparent[i] = new THREE.Object3D();
  82. meshparent[i].position.y = 50;
  83. meshparent[i].position.x = -50;
  84. meshparent2[i].add( meshparent[i] );
  85. mesh = new THREE.Mesh( geometry, material );
  86. meshparent[i].add( mesh );
  87. meshes.push(mesh);
  88. }
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. document.addEventListener('keydown', keyPressed, false);
  91. oculuscontrol.connect();
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. realcamera.aspect = window.innerWidth / window.innerHeight;
  97. realcamera.updateProjectionMatrix();
  98. effect.setSize( window.innerWidth, window.innerHeight );
  99. controls.handleResize();
  100. }
  101. function keyPressed(event) {
  102. if (event.keyCode === 72) { // H
  103. guiVisible = !guiVisible;
  104. document.getElementById('info').style.display = guiVisible ? "block" : "none";
  105. }
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. var t = clock.getElapsedTime();
  110. for (var i=0; i<meshes.length; i++) {
  111. var c = Math.floor(i % cols);
  112. var r = Math.floor(i / cols);
  113. meshparent2[i].rotation.y = (c * 3.142 * 2 / cols);
  114. meshparent[i].rotation.x = ((r+10) * 3.142 * 2 / (rows+20));
  115. meshes[i].position.z = 1400 - 1350 * perlin.noise(c*8/cols,r*8/rows,t/2);
  116. }
  117. controls.update( clock.getDelta() );
  118. oculuscontrol.update( clock.getDelta() );
  119. effect.render( scene, camera );
  120. }
  121. </script>
  122. </body>
  123. </html>