webgl_effects_vr.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - native vr demo</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. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. .menu {
  15. position: fixed;
  16. bottom: 20px;
  17. right: 20px;
  18. }
  19. .button {
  20. display: inline-block;
  21. padding: 8px;
  22. color: #FFF;
  23. background-color: #555;
  24. }
  25. .button.enabled {
  26. background-color: rgb(18, 36, 70);
  27. }
  28. .button:hover {
  29. cursor: pointer;
  30. background-color: rgb(18, 36, 70);
  31. }
  32. .button.error {
  33. pointer-events: none;
  34. background-color: red;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="menu">
  40. <div class="button mouse-look">Enable Mouse Look</div>
  41. <div class="button full-screen">Start VR Mode</div>
  42. </div>
  43. <script src="../build/three.min.js"></script>
  44. <script src="js/controls/MouseControls.js"></script>
  45. <script src="js/effects/VREffect.js"></script>
  46. <script src="js/controls/VRControls.js"></script>
  47. <script src="js/libs/stats.min.js"></script>
  48. <script>
  49. var container, stats;
  50. var camera, scene, raycaster, renderer;
  51. var vrEffect;
  52. var vrControls;
  53. var mouseControls;
  54. var headControls;
  55. var mouse = new THREE.Vector2(), INTERSECTED;
  56. var radius = 100, theta = 0;
  57. init();
  58. animate();
  59. function init() {
  60. container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. var info = document.createElement( 'div' );
  63. info.style.position = 'absolute';
  64. info.style.top = '10px';
  65. info.style.width = '100%';
  66. info.style.textAlign = 'center';
  67. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  68. container.appendChild( info );
  69. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  70. scene = new THREE.Scene();
  71. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  72. light.position.set( 1, 1, 1 ).normalize();
  73. scene.add( light );
  74. var geometry = new THREE.BoxGeometry( 20, 20, 20 );
  75. for ( var i = 0; i < 2000; i ++ ) {
  76. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  77. object.position.x = Math.random() * 800 - 400;
  78. object.position.y = Math.random() * 800 - 400;
  79. object.position.z = Math.random() * 800 - 400;
  80. object.rotation.x = Math.random() * 2 * Math.PI;
  81. object.rotation.y = Math.random() * 2 * Math.PI;
  82. object.rotation.z = Math.random() * 2 * Math.PI;
  83. object.scale.x = Math.random() + 0.5;
  84. object.scale.y = Math.random() + 0.5;
  85. object.scale.z = Math.random() + 0.5;
  86. scene.add( object );
  87. }
  88. raycaster = new THREE.Raycaster();
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. var fullScreenButton = document.querySelector( '.full-screen' );
  91. var mouseLookButton = document.querySelector( '.mouse-look' );
  92. var mouseLook = false;
  93. fullScreenButton.onclick = function() {
  94. vrEffect.setFullScreen( true );
  95. };
  96. vrControls = new THREE.VRControls(camera);
  97. mouseControls = new THREE.MouseControls(camera);
  98. headControls = vrControls;
  99. mouseLookButton.onclick = function() {
  100. mouseLook = !mouseLook;
  101. if (mouseLook) {
  102. headControls = mouseControls;
  103. mouseLookButton.classList.add('enabled');
  104. } else {
  105. headControls = vrControls;
  106. mouseLookButton.classList.remove('enabled');
  107. }
  108. }
  109. vrEffect = new THREE.VREffect(renderer, VREffectLoaded);
  110. function VREffectLoaded(error) {
  111. if (error) {
  112. fullScreenButton.innerHTML = error;
  113. fullScreenButton.classList.add('error');
  114. }
  115. }
  116. renderer.setClearColor( 0xf0f0f0 );
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. renderer.sortObjects = false;
  119. container.appendChild( renderer.domElement );
  120. stats = new Stats();
  121. stats.domElement.style.position = 'absolute';
  122. stats.domElement.style.top = '0px';
  123. container.appendChild( stats.domElement );
  124. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  125. //
  126. window.addEventListener( 'resize', onWindowResize, false );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. vrEffect.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function onDocumentMouseMove( event ) {
  134. event.preventDefault();
  135. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  136. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  137. }
  138. //
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. render();
  142. stats.update();
  143. }
  144. function render() {
  145. theta += 0.1;
  146. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  147. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  148. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  149. camera.lookAt( scene.position );
  150. // find intersections
  151. var vector = new THREE.Vector3( mouse.x, mouse.y, 1 ).unproject( camera );
  152. raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
  153. var intersects = raycaster.intersectObjects( scene.children );
  154. if ( intersects.length > 0 ) {
  155. if ( INTERSECTED != intersects[ 0 ].object ) {
  156. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  157. INTERSECTED = intersects[ 0 ].object;
  158. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  159. INTERSECTED.material.emissive.setHex( 0xff0000 );
  160. }
  161. } else {
  162. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  163. INTERSECTED = null;
  164. }
  165. headControls.update();
  166. vrEffect.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>