webvr_cubes.html 7.0 KB

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