webvr_cubes.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 = WebXR Device API (For Chrome M69+), origin = https://threejs.org, expires = 2019-01-07 -->
  8. <meta http-equiv="origin-trial" data-feature="WebXR Device API (For Chrome M69+)" data-expires="2019-01-07" content="ArPzyYNrUDiXsGOh647Ya7MtVUA1nM+WFMnPWu7eoF2nQHOP6mTATIbiv0w+k2kFaPofZG/04ZEQdsACq4IA0wQAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU02OSIsImV4cGlyeSI6MTU0Njg4MzAxOH0=">
  9. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2019-01-07 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2019-01-07" content="Av16a8LVXasKVQV9j3u0OlXdTfz5O9qDqhROyL5Up6R3sdOPbtFuc6n6o5DHitwnb5VdirH0GyvROwvz8xxd0AkAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTQ2ODgzMDE4fQ==">
  11. <style>
  12. body {
  13. font-family: Monospace;
  14. background-color: #101010;
  15. color: #fff;
  16. margin: 0px;
  17. overflow: hidden;
  18. }
  19. a {
  20. color: #f00;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <script src="../build/three.js"></script>
  26. <script src="js/vr/WebVR.js"></script>
  27. <script src="js/geometries/BoxLineGeometry.js"></script>
  28. <script>
  29. var clock = new THREE.Clock();
  30. var container;
  31. var camera, scene, raycaster, renderer;
  32. var room;
  33. var isMouseDown = false;
  34. var INTERSECTED;
  35. var crosshair;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. var info = document.createElement( 'div' );
  42. info.style.position = 'absolute';
  43. info.style.top = '10px';
  44. info.style.width = '100%';
  45. info.style.textAlign = 'center';
  46. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes';
  47. container.appendChild( info );
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0x505050 );
  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.RingBufferGeometry( 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.LineSegments(
  63. new THREE.BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
  64. new THREE.LineBasicMaterial( { color: 0x808080 } )
  65. );
  66. room.position.y = 3;
  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.BoxBufferGeometry( 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.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.vr.enabled = true;
  95. container.appendChild( renderer.domElement );
  96. renderer.domElement.addEventListener( 'mousedown', onMouseDown, false );
  97. renderer.domElement.addEventListener( 'mouseup', onMouseUp, false );
  98. renderer.domElement.addEventListener( 'touchstart', onMouseDown, false );
  99. renderer.domElement.addEventListener( 'touchend', onMouseUp, false );
  100. window.addEventListener( 'resize', onWindowResize, false );
  101. //
  102. window.addEventListener( 'vrdisplaypointerrestricted', onPointerRestricted, false );
  103. window.addEventListener( 'vrdisplaypointerunrestricted', onPointerUnrestricted, false );
  104. document.body.appendChild( WEBVR.createButton( renderer ) );
  105. }
  106. function onMouseDown() {
  107. isMouseDown = true;
  108. }
  109. function onMouseUp() {
  110. isMouseDown = false;
  111. }
  112. function onPointerRestricted() {
  113. var pointerLockElement = renderer.domElement;
  114. if ( pointerLockElement && typeof ( pointerLockElement.requestPointerLock ) === 'function' ) {
  115. pointerLockElement.requestPointerLock();
  116. }
  117. }
  118. function onPointerUnrestricted() {
  119. var currentPointerLockElement = document.pointerLockElement;
  120. var expectedPointerLockElement = renderer.domElement;
  121. if ( currentPointerLockElement && currentPointerLockElement === expectedPointerLockElement && typeof ( document.exitPointerLock ) === 'function' ) {
  122. document.exitPointerLock();
  123. }
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function animate() {
  132. renderer.setAnimationLoop( render );
  133. }
  134. function render() {
  135. var delta = clock.getDelta() * 60;
  136. if ( isMouseDown === true ) {
  137. var cube = room.children[ 0 ];
  138. room.remove( cube );
  139. cube.position.set( 0, 0, - 0.75 );
  140. cube.position.applyQuaternion( camera.quaternion );
  141. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  142. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  143. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  144. cube.userData.velocity.applyQuaternion( camera.quaternion );
  145. room.add( cube );
  146. }
  147. // find intersections
  148. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  149. var intersects = raycaster.intersectObjects( room.children );
  150. if ( intersects.length > 0 ) {
  151. if ( INTERSECTED != intersects[ 0 ].object ) {
  152. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  153. INTERSECTED = intersects[ 0 ].object;
  154. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  155. INTERSECTED.material.emissive.setHex( 0xff0000 );
  156. }
  157. } else {
  158. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  159. INTERSECTED = undefined;
  160. }
  161. // Keep cubes inside room
  162. for ( var i = 0; i < room.children.length; i ++ ) {
  163. var cube = room.children[ i ];
  164. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  165. cube.position.add( cube.userData.velocity );
  166. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  167. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  168. cube.userData.velocity.x = - cube.userData.velocity.x;
  169. }
  170. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  171. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  172. cube.userData.velocity.y = - cube.userData.velocity.y;
  173. }
  174. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  175. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  176. cube.userData.velocity.z = - cube.userData.velocity.z;
  177. }
  178. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  179. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  180. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  181. }
  182. renderer.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>