2
0

webvr_cubes.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 M62+), origin = https://threejs.org, expires = 2018-06-19 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-06-19" content="Alxt96tYGgIr9l6EXU0eeI360zcmzOY6Kuo3kcTfBGIRDOQbgFIZKRQ1joExQ74WZr1einsE+cUMHgSclNHCQQ4AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUyOTM5NzgyOH0=">
  9. <!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-06-15 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-06-15" content="AtJH9g6nn0B87bnjJt+9m1joZXEYDmLSlRvtMr5qJD52hMcm3S86S7jg5I7y2I5cgQglE0rzsXzti5DECQLb8QkAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUyOTA4NDY2OH0=">
  11. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-06-15 -->
  12. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-06-15" content="Aihhr0yXkVlCKF0DIpTbH8WX7ZmEexUhI/95+t8aoLfvBkePMiZ/iOoDPU3xefyfuczkDahH1L6eiPvRsuzITAAAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTI5MDg0NjY4fQ==">
  13. <style>
  14. body {
  15. font-family: Monospace;
  16. background-color: #101010;
  17. color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. a {
  22. color: #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <script src="../build/three.js"></script>
  28. <script src="js/vr/WebVR.js"></script>
  29. <script>
  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. scene.background = new THREE.Color( 0x505050 );
  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.RingBufferGeometry( 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.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
  65. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  66. );
  67. room.position.y = 3;
  68. scene.add( room );
  69. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  70. var light = new THREE.DirectionalLight( 0xffffff );
  71. light.position.set( 1, 1, 1 ).normalize();
  72. scene.add( light );
  73. var geometry = new THREE.BoxBufferGeometry( 0.15, 0.15, 0.15 );
  74. for ( var i = 0; i < 200; i ++ ) {
  75. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  76. object.position.x = Math.random() * 4 - 2;
  77. object.position.y = Math.random() * 4 - 2;
  78. object.position.z = Math.random() * 4 - 2;
  79. object.rotation.x = Math.random() * 2 * Math.PI;
  80. object.rotation.y = Math.random() * 2 * Math.PI;
  81. object.rotation.z = Math.random() * 2 * Math.PI;
  82. object.scale.x = Math.random() + 0.5;
  83. object.scale.y = Math.random() + 0.5;
  84. object.scale.z = Math.random() + 0.5;
  85. object.userData.velocity = new THREE.Vector3();
  86. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  87. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  88. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  89. room.add( object );
  90. }
  91. raycaster = new THREE.Raycaster();
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.vr.enabled = true;
  96. container.appendChild( renderer.domElement );
  97. renderer.domElement.addEventListener( 'mousedown', onMouseDown, false );
  98. renderer.domElement.addEventListener( 'mouseup', onMouseUp, false );
  99. renderer.domElement.addEventListener( 'touchstart', onMouseDown, false );
  100. renderer.domElement.addEventListener( 'touchend', onMouseUp, false );
  101. window.addEventListener( 'resize', onWindowResize, false );
  102. //
  103. window.addEventListener( 'vrdisplaypointerrestricted', onPointerRestricted, false );
  104. window.addEventListener( 'vrdisplaypointerunrestricted', onPointerUnrestricted, false );
  105. document.body.appendChild( WEBVR.createButton( renderer ) );
  106. }
  107. function onMouseDown() {
  108. isMouseDown = true;
  109. }
  110. function onMouseUp() {
  111. isMouseDown = false;
  112. }
  113. function onPointerRestricted() {
  114. var pointerLockElement = renderer.domElement;
  115. if ( pointerLockElement && typeof(pointerLockElement.requestPointerLock) === 'function' ) {
  116. pointerLockElement.requestPointerLock();
  117. }
  118. }
  119. function onPointerUnrestricted() {
  120. var currentPointerLockElement = document.pointerLockElement;
  121. var expectedPointerLockElement = renderer.domElement;
  122. if ( currentPointerLockElement && currentPointerLockElement === expectedPointerLockElement && typeof(document.exitPointerLock) === 'function' ) {
  123. document.exitPointerLock();
  124. }
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. //
  132. function animate() {
  133. renderer.setAnimationLoop( render );
  134. }
  135. function render() {
  136. var delta = clock.getDelta() * 60;
  137. if ( isMouseDown === true ) {
  138. var cube = room.children[ 0 ];
  139. room.remove( cube );
  140. cube.position.set( 0, 0, - 0.75 );
  141. cube.position.applyQuaternion( camera.quaternion );
  142. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  143. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  144. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  145. cube.userData.velocity.applyQuaternion( camera.quaternion );
  146. room.add( cube );
  147. }
  148. // find intersections
  149. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  150. var intersects = raycaster.intersectObjects( room.children );
  151. if ( intersects.length > 0 ) {
  152. if ( INTERSECTED != intersects[ 0 ].object ) {
  153. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  154. INTERSECTED = intersects[ 0 ].object;
  155. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  156. INTERSECTED.material.emissive.setHex( 0xff0000 );
  157. }
  158. } else {
  159. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  160. INTERSECTED = undefined;
  161. }
  162. // Keep cubes inside room
  163. for ( var i = 0; i < room.children.length; i ++ ) {
  164. var cube = room.children[ i ];
  165. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  166. cube.position.add( cube.userData.velocity );
  167. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  168. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  169. cube.userData.velocity.x = - cube.userData.velocity.x;
  170. }
  171. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  172. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  173. cube.userData.velocity.y = - cube.userData.velocity.y;
  174. }
  175. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  176. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  177. cube.userData.velocity.z = - cube.userData.velocity.z;
  178. }
  179. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  180. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  181. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  182. }
  183. renderer.render( scene, camera );
  184. }
  185. </script>
  186. </body>
  187. </html>