webxr_vr_ballshooter.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - ball shooter</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - ball shooter
  12. </div>
  13. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
  17. import { VRButton } from './jsm/webxr/VRButton.js';
  18. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  19. var camera, scene, renderer;
  20. var controller1, controller2;
  21. var controllerGrip1, controllerGrip2;
  22. var room;
  23. var count = 0;
  24. var radius = 0.08;
  25. var normal = new THREE.Vector3();
  26. var relativeVelocity = new THREE.Vector3();
  27. var clock = new THREE.Clock();
  28. init();
  29. animate();
  30. function init() {
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x505050 );
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  34. camera.position.set( 0, 1.6, 3 );
  35. room = new THREE.LineSegments(
  36. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
  37. new THREE.LineBasicMaterial( { color: 0x808080 } )
  38. );
  39. room.geometry.translate( 0, 3, 0 );
  40. scene.add( room );
  41. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  42. var light = new THREE.DirectionalLight( 0xffffff );
  43. light.position.set( 1, 1, 1 ).normalize();
  44. scene.add( light );
  45. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  46. for ( var i = 0; i < 200; i ++ ) {
  47. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  48. object.position.x = Math.random() * 4 - 2;
  49. object.position.y = Math.random() * 4;
  50. object.position.z = Math.random() * 4 - 2;
  51. object.userData.velocity = new THREE.Vector3();
  52. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  53. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  54. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  55. room.add( object );
  56. }
  57. //
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.outputEncoding = THREE.sRGBEncoding;
  62. renderer.xr.enabled = true;
  63. document.body.appendChild( renderer.domElement );
  64. //
  65. document.body.appendChild( VRButton.createButton( renderer ) );
  66. // controllers
  67. function onSelectStart() {
  68. this.userData.isSelecting = true;
  69. }
  70. function onSelectEnd() {
  71. this.userData.isSelecting = false;
  72. }
  73. controller1 = renderer.xr.getController( 0 );
  74. controller1.addEventListener( 'selectstart', onSelectStart );
  75. controller1.addEventListener( 'selectend', onSelectEnd );
  76. controller1.addEventListener( 'connected', function ( event ) {
  77. this.add( buildController( event.data ) );
  78. } );
  79. controller1.addEventListener( 'disconnected', function () {
  80. this.remove( this.children[ 0 ] );
  81. } );
  82. scene.add( controller1 );
  83. controller2 = renderer.xr.getController( 1 );
  84. controller2.addEventListener( 'selectstart', onSelectStart );
  85. controller2.addEventListener( 'selectend', onSelectEnd );
  86. controller2.addEventListener( 'connected', function ( event ) {
  87. this.add( buildController( event.data ) );
  88. } );
  89. controller2.addEventListener( 'disconnected', function () {
  90. this.remove( this.children[ 0 ] );
  91. } );
  92. scene.add( controller2 );
  93. // The XRControllerModelFactory will automatically fetch controller models
  94. // that match what the user is holding as closely as possible. The models
  95. // should be attached to the object returned from getControllerGrip in
  96. // order to match the orientation of the held device.
  97. var controllerModelFactory = new XRControllerModelFactory();
  98. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  99. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  100. scene.add( controllerGrip1 );
  101. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  102. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  103. scene.add( controllerGrip2 );
  104. //
  105. window.addEventListener( 'resize', onWindowResize, false );
  106. }
  107. function buildController( data ) {
  108. switch ( data.targetRayMode ) {
  109. case 'tracked-pointer':
  110. var geometry = new THREE.BufferGeometry();
  111. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  112. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  113. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, blending: THREE.AdditiveBlending } );
  114. return new THREE.Line( geometry, material );
  115. case 'gaze':
  116. var geometry = new THREE.RingBufferGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  117. var material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  118. return new THREE.Mesh( geometry, material );
  119. }
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function handleController( controller ) {
  127. if ( controller.userData.isSelecting ) {
  128. var object = room.children[ count ++ ];
  129. object.position.copy( controller.position );
  130. object.userData.velocity.x = ( Math.random() - 0.5 ) * 3;
  131. object.userData.velocity.y = ( Math.random() - 0.5 ) * 3;
  132. object.userData.velocity.z = ( Math.random() - 9 );
  133. object.userData.velocity.applyQuaternion( controller.quaternion );
  134. if ( count === room.children.length ) count = 0;
  135. }
  136. }
  137. //
  138. function animate() {
  139. renderer.setAnimationLoop( render );
  140. }
  141. function render() {
  142. handleController( controller1 );
  143. handleController( controller2 );
  144. //
  145. var delta = clock.getDelta() * 0.8; // slow down simulation
  146. var range = 3 - radius;
  147. for ( var i = 0; i < room.children.length; i ++ ) {
  148. var object = room.children[ i ];
  149. object.position.x += object.userData.velocity.x * delta;
  150. object.position.y += object.userData.velocity.y * delta;
  151. object.position.z += object.userData.velocity.z * delta;
  152. // keep objects inside room
  153. if ( object.position.x < - range || object.position.x > range ) {
  154. object.position.x = THREE.MathUtils.clamp( object.position.x, - range, range );
  155. object.userData.velocity.x = - object.userData.velocity.x;
  156. }
  157. if ( object.position.y < radius || object.position.y > 6 ) {
  158. object.position.y = Math.max( object.position.y, radius );
  159. object.userData.velocity.x *= 0.98;
  160. object.userData.velocity.y = - object.userData.velocity.y * 0.8;
  161. object.userData.velocity.z *= 0.98;
  162. }
  163. if ( object.position.z < - range || object.position.z > range ) {
  164. object.position.z = THREE.MathUtils.clamp( object.position.z, - range, range );
  165. object.userData.velocity.z = - object.userData.velocity.z;
  166. }
  167. for ( var j = i + 1; j < room.children.length; j ++ ) {
  168. var object2 = room.children[ j ];
  169. normal.copy( object.position ).sub( object2.position );
  170. var distance = normal.length();
  171. if ( distance < 2 * radius ) {
  172. normal.multiplyScalar( 0.5 * distance - radius );
  173. object.position.sub( normal );
  174. object2.position.add( normal );
  175. normal.normalize();
  176. relativeVelocity.copy( object.userData.velocity ).sub( object2.userData.velocity );
  177. normal = normal.multiplyScalar( relativeVelocity.dot( normal ) );
  178. object.userData.velocity.sub( normal );
  179. object2.userData.velocity.add( normal );
  180. }
  181. }
  182. object.userData.velocity.y -= 9.8 * delta;
  183. }
  184. renderer.render( scene, camera );
  185. }
  186. </script>
  187. </body>
  188. </html>