misc_controls_oculusrift.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. perlin = new ImprovedNoise();
  57. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 1, 5000 );
  58. camera.position.x = 200;
  59. camera.position.y = 250;
  60. camera.position.z = -200;
  61. scene = new THREE.Scene();
  62. effect = new THREE.OculusRiftEffect( renderer, { worldScale: 1 } );
  63. effect.setSize( window.innerWidth, window.innerHeight );
  64. controls = new THREE.FirstPersonControls( camera );
  65. controls.movementSpeed = 4000;
  66. controls.lookSpeed = 3.0;
  67. controls.lookVertical = true;
  68. oculuscontrol = new THREE.OculusControls( camera );
  69. document.body.appendChild( renderer.domElement );
  70. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
  71. hemiLight.color.setHSL( 0.6, 1, 0.6 );
  72. hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
  73. hemiLight.position.set( 0, 500, 0 );
  74. scene.add( hemiLight );
  75. var geometry = new THREE.BoxGeometry( 100, 100, 200 );
  76. var texture = THREE.ImageUtils.loadTexture( 'textures/crate.gif' );
  77. texture.anisotropy = renderer.getMaxAnisotropy();
  78. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 20, shading: THREE.FlatShading, map: texture } );
  79. for (var i=0; i<tot; i++) {
  80. meshparent2[i] = new THREE.Object3D();
  81. scene.add( meshparent2[i] );
  82. meshparent[i] = new THREE.Object3D();
  83. meshparent[i].position.y = 50;
  84. meshparent[i].position.x = -50;
  85. meshparent2[i].add( meshparent[i] );
  86. mesh = new THREE.Mesh( geometry, material );
  87. meshparent[i].add( mesh );
  88. meshes.push(mesh);
  89. }
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. document.addEventListener('keydown', keyPressed, false);
  92. oculuscontrol.connect();
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. realcamera.aspect = window.innerWidth / window.innerHeight;
  98. realcamera.updateProjectionMatrix();
  99. effect.setSize( window.innerWidth, window.innerHeight );
  100. controls.handleResize();
  101. }
  102. function keyPressed(event) {
  103. if (event.keyCode === 72) { // H
  104. guiVisible = !guiVisible;
  105. document.getElementById('info').style.display = guiVisible ? "block" : "none";
  106. }
  107. }
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. var t = clock.getElapsedTime();
  111. for (var i=0; i<meshes.length; i++) {
  112. var c = Math.floor(i % cols);
  113. var r = Math.floor(i / cols);
  114. meshparent2[i].rotation.y = (c * 3.142 * 2 / cols);
  115. meshparent[i].rotation.x = ((r+10) * 3.142 * 2 / (rows+20));
  116. meshes[i].position.z = 1400 - 1350 * perlin.noise(c*8/cols,r*8/rows,t/2);
  117. }
  118. controls.update( clock.getDelta() );
  119. oculuscontrol.update( clock.getDelta() );
  120. effect.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>