webvr_cubes.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #101010;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a {
  16. color: #f00;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/three.js"></script>
  22. <script src="js/controls/VRControls.js"></script>
  23. <script src="js/effects/VREffect.js"></script>
  24. <script src="js/vr/WebVR.js"></script>
  25. <script>
  26. if ( WEBVR.isAvailable() === false ) {
  27. document.body.appendChild( WEBVR.getMessage() );
  28. }
  29. //
  30. var clock = new THREE.Clock();
  31. var container;
  32. var camera, scene, raycaster, renderer;
  33. var effect, controls;
  34. var room;
  35. var isMouseDown = false;
  36. var INTERSECTED;
  37. var crosshair;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  49. container.appendChild( info );
  50. scene = new THREE.Scene();
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  52. scene.add( camera );
  53. crosshair = new THREE.Mesh(
  54. new THREE.RingGeometry( 0.02, 0.04, 32 ),
  55. new THREE.MeshBasicMaterial( {
  56. color: 0xffffff,
  57. opacity: 0.5,
  58. transparent: true
  59. } )
  60. );
  61. crosshair.position.z = - 2;
  62. camera.add( crosshair );
  63. room = new THREE.Mesh(
  64. new THREE.BoxGeometry( 6, 6, 6, 8, 8, 8 ),
  65. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  66. );
  67. scene.add( room );
  68. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  69. var light = new THREE.DirectionalLight( 0xffffff );
  70. light.position.set( 1, 1, 1 ).normalize();
  71. scene.add( light );
  72. var geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
  73. for ( var i = 0; i < 200; i ++ ) {
  74. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  75. object.position.x = Math.random() * 4 - 2;
  76. object.position.y = Math.random() * 4 - 2;
  77. object.position.z = Math.random() * 4 - 2;
  78. object.rotation.x = Math.random() * 2 * Math.PI;
  79. object.rotation.y = Math.random() * 2 * Math.PI;
  80. object.rotation.z = Math.random() * 2 * Math.PI;
  81. object.scale.x = Math.random() + 0.5;
  82. object.scale.y = Math.random() + 0.5;
  83. object.scale.z = Math.random() + 0.5;
  84. object.userData.velocity = new THREE.Vector3();
  85. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  86. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  87. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  88. room.add( object );
  89. }
  90. raycaster = new THREE.Raycaster();
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setClearColor( 0x505050 );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.sortObjects = false;
  96. container.appendChild( renderer.domElement );
  97. controls = new THREE.VRControls( camera );
  98. effect = new THREE.VREffect( renderer );
  99. if ( navigator.getVRDisplays ) {
  100. navigator.getVRDisplays()
  101. .then( function ( displays ) {
  102. effect.setVRDisplay( displays[ 0 ] );
  103. controls.setVRDisplay( displays[ 0 ] );
  104. } )
  105. .catch( function () {
  106. // no displays
  107. } );
  108. document.body.appendChild( WEBVR.getButton( effect ) );
  109. }
  110. renderer.domElement.addEventListener( 'mousedown', onMouseDown, false );
  111. renderer.domElement.addEventListener( 'mouseup', onMouseUp, false );
  112. renderer.domElement.addEventListener( 'touchstart', onMouseDown, false );
  113. renderer.domElement.addEventListener( 'touchend', onMouseUp, false );
  114. //
  115. window.addEventListener( 'resize', onWindowResize, false );
  116. }
  117. function onMouseDown() {
  118. isMouseDown = true;
  119. }
  120. function onMouseUp() {
  121. isMouseDown = false;
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. effect.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. //
  129. function animate() {
  130. effect.requestAnimationFrame( animate );
  131. render();
  132. }
  133. function render() {
  134. var delta = clock.getDelta() * 60;
  135. if ( isMouseDown === true ) {
  136. var cube = room.children[ 0 ];
  137. room.remove( cube );
  138. cube.position.set( 0, 0, - 0.75 );
  139. cube.position.applyQuaternion( camera.quaternion );
  140. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  141. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  142. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  143. cube.userData.velocity.applyQuaternion( camera.quaternion );
  144. room.add( cube );
  145. }
  146. // find intersections
  147. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  148. var intersects = raycaster.intersectObjects( room.children );
  149. if ( intersects.length > 0 ) {
  150. if ( INTERSECTED != intersects[ 0 ].object ) {
  151. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  152. INTERSECTED = intersects[ 0 ].object;
  153. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  154. INTERSECTED.material.emissive.setHex( 0xff0000 );
  155. }
  156. } else {
  157. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  158. INTERSECTED = undefined;
  159. }
  160. // Keep cubes inside room
  161. for ( var i = 0; i < room.children.length; i ++ ) {
  162. var cube = room.children[ i ];
  163. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  164. cube.position.add( cube.userData.velocity );
  165. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  166. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  167. cube.userData.velocity.x = - cube.userData.velocity.x;
  168. }
  169. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  170. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  171. cube.userData.velocity.y = - cube.userData.velocity.y;
  172. }
  173. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  174. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  175. cube.userData.velocity.z = - cube.userData.velocity.z;
  176. }
  177. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  178. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  179. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  180. }
  181. controls.update();
  182. effect.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>