webgl_effects_vr.html 5.3 KB

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