webxr_xr_ballshooter.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js xr - 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> xr - ball shooter
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { BoxLineGeometry } from 'three/addons/geometries/BoxLineGeometry.js';
  24. import { XRButton } from 'three/addons/webxr/XRButton.js';
  25. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  26. import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer;
  29. let controller1, controller2;
  30. let controllerGrip1, controllerGrip2;
  31. let room, spheres;
  32. let physics, velocity = new THREE.Vector3();
  33. let count = 0;
  34. init();
  35. await initPhysics();
  36. function init() {
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x505050 );
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 50 );
  40. camera.position.set( 0, 1.6, 3 );
  41. room = new THREE.LineSegments(
  42. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
  43. new THREE.LineBasicMaterial( { color: 0x808080 } )
  44. );
  45. room.geometry.translate( 0, 3, 0 );
  46. scene.add( room );
  47. scene.add( new THREE.HemisphereLight( 0xbbbbbb, 0x888888, 3 ) );
  48. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  49. light.position.set( 1, 1, 1 ).normalize();
  50. scene.add( light );
  51. //
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.setAnimationLoop( render );
  56. renderer.xr.enabled = true;
  57. document.body.appendChild( renderer.domElement );
  58. //
  59. const controls = new OrbitControls( camera, renderer.domElement );
  60. controls.maxDistance = 10;
  61. controls.target.y = 1.6;
  62. controls.update();
  63. document.body.appendChild( XRButton.createButton( renderer ) );
  64. // controllers
  65. function onSelectStart() {
  66. this.userData.isSelecting = true;
  67. }
  68. function onSelectEnd() {
  69. this.userData.isSelecting = false;
  70. }
  71. controller1 = renderer.xr.getController( 0 );
  72. controller1.addEventListener( 'selectstart', onSelectStart );
  73. controller1.addEventListener( 'selectend', onSelectEnd );
  74. controller1.addEventListener( 'connected', function ( event ) {
  75. this.add( buildController( event.data ) );
  76. } );
  77. controller1.addEventListener( 'disconnected', function () {
  78. this.remove( this.children[ 0 ] );
  79. } );
  80. scene.add( controller1 );
  81. controller2 = renderer.xr.getController( 1 );
  82. controller2.addEventListener( 'selectstart', onSelectStart );
  83. controller2.addEventListener( 'selectend', onSelectEnd );
  84. controller2.addEventListener( 'connected', function ( event ) {
  85. this.add( buildController( event.data ) );
  86. } );
  87. controller2.addEventListener( 'disconnected', function () {
  88. this.remove( this.children[ 0 ] );
  89. } );
  90. scene.add( controller2 );
  91. // The XRControllerModelFactory will automatically fetch controller models
  92. // that match what the user is holding as closely as possible. The models
  93. // should be attached to the object returned from getControllerGrip in
  94. // order to match the orientation of the held device.
  95. const controllerModelFactory = new XRControllerModelFactory();
  96. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  97. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  98. scene.add( controllerGrip1 );
  99. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  100. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  101. scene.add( controllerGrip2 );
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function buildController( data ) {
  106. let geometry, material;
  107. switch ( data.targetRayMode ) {
  108. case 'tracked-pointer':
  109. geometry = new THREE.BufferGeometry();
  110. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  111. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  112. material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
  113. return new THREE.Line( geometry, material );
  114. case 'gaze':
  115. geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  116. material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  117. return new THREE.Mesh( geometry, material );
  118. }
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. async function initPhysics() {
  126. physics = await RapierPhysics();
  127. {
  128. // Floor
  129. const geometry = new THREE.BoxGeometry( 6, 2, 6 );
  130. const material = new THREE.MeshNormalMaterial();
  131. const floor = new THREE.Mesh( geometry, material );
  132. floor.position.y = - 1;
  133. physics.addMesh( floor );
  134. // Walls
  135. const wallPX = new THREE.Mesh( geometry, material );
  136. wallPX.position.set( 4, 3, 0 );
  137. wallPX.rotation.z = Math.PI / 2;
  138. physics.addMesh( wallPX );
  139. const wallNX = new THREE.Mesh( geometry, material );
  140. wallNX.position.set( - 4, 3, 0 );
  141. wallNX.rotation.z = Math.PI / 2;
  142. physics.addMesh( wallNX );
  143. const wallPZ = new THREE.Mesh( geometry, material );
  144. wallPZ.position.set( 0, 3, 4 );
  145. wallPZ.rotation.x = Math.PI / 2;
  146. physics.addMesh( wallPZ );
  147. const wallNZ = new THREE.Mesh( geometry, material );
  148. wallNZ.position.set( 0, 3, - 4 );
  149. wallNZ.rotation.x = Math.PI / 2;
  150. physics.addMesh( wallNZ );
  151. }
  152. // Spheres
  153. const geometry = new THREE.IcosahedronGeometry( 0.08, 3 );
  154. const material = new THREE.MeshLambertMaterial();
  155. spheres = new THREE.InstancedMesh( geometry, material, 800 );
  156. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  157. scene.add( spheres );
  158. const matrix = new THREE.Matrix4();
  159. const color = new THREE.Color();
  160. for ( let i = 0; i < spheres.count; i ++ ) {
  161. const x = Math.random() * 4 - 2;
  162. const y = Math.random() * 4;
  163. const z = Math.random() * 4 - 2;
  164. matrix.setPosition( x, y, z );
  165. spheres.setMatrixAt( i, matrix );
  166. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  167. }
  168. physics.addMesh( spheres, 1, 1.1 );
  169. }
  170. //
  171. function handleController( controller ) {
  172. if ( controller.userData.isSelecting ) {
  173. physics.setMeshPosition( spheres, controller.position, count );
  174. velocity.x = ( Math.random() - 0.5 ) * 2;
  175. velocity.y = ( Math.random() - 0.5 ) * 2;
  176. velocity.z = ( Math.random() - 9 );
  177. velocity.applyQuaternion( controller.quaternion );
  178. physics.setMeshVelocity( spheres, velocity, count );
  179. if ( ++ count === spheres.count ) count = 0;
  180. }
  181. }
  182. function render() {
  183. handleController( controller1 );
  184. handleController( controller2 );
  185. renderer.render( scene, camera );
  186. }
  187. </script>
  188. </body>
  189. </html>