webxr_xr_ballshooter.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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, { 'optionalFeatures': [ 'depth-sensing'] } ) );
  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( { visible: false } );
  131. const floor = new THREE.Mesh( geometry, material );
  132. floor.position.y = - 1;
  133. floor.userData.physics = { mass: 0 };
  134. scene.add( floor );
  135. // Walls
  136. const wallPX = new THREE.Mesh( geometry, material );
  137. wallPX.position.set( 4, 3, 0 );
  138. wallPX.rotation.z = Math.PI / 2;
  139. wallPX.userData.physics = { mass: 0 };
  140. scene.add( wallPX );
  141. const wallNX = new THREE.Mesh( geometry, material );
  142. wallNX.position.set( - 4, 3, 0 );
  143. wallNX.rotation.z = Math.PI / 2;
  144. wallNX.userData.physics = { mass: 0 };
  145. scene.add( wallNX );
  146. const wallPZ = new THREE.Mesh( geometry, material );
  147. wallPZ.position.set( 0, 3, 4 );
  148. wallPZ.rotation.x = Math.PI / 2;
  149. wallPZ.userData.physics = { mass: 0 };
  150. scene.add( wallPZ );
  151. const wallNZ = new THREE.Mesh( geometry, material );
  152. wallNZ.position.set( 0, 3, - 4 );
  153. wallNZ.rotation.x = Math.PI / 2;
  154. wallNZ.userData.physics = { mass: 0 };
  155. scene.add( wallNZ );
  156. }
  157. // Spheres
  158. const geometry = new THREE.IcosahedronGeometry( 0.08, 3 );
  159. const material = new THREE.MeshLambertMaterial();
  160. spheres = new THREE.InstancedMesh( geometry, material, 800 );
  161. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  162. spheres.userData.physics = { mass: 1, restitution: 1.1 };
  163. scene.add( spheres );
  164. const matrix = new THREE.Matrix4();
  165. const color = new THREE.Color();
  166. for ( let i = 0; i < spheres.count; i ++ ) {
  167. const x = Math.random() * 4 - 2;
  168. const y = Math.random() * 4;
  169. const z = Math.random() * 4 - 2;
  170. matrix.setPosition( x, y, z );
  171. spheres.setMatrixAt( i, matrix );
  172. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  173. }
  174. physics.addScene( scene );
  175. }
  176. //
  177. function handleController( controller ) {
  178. if ( controller.userData.isSelecting ) {
  179. physics.setMeshPosition( spheres, controller.position, count );
  180. velocity.x = ( Math.random() - 0.5 ) * 2;
  181. velocity.y = ( Math.random() - 0.5 ) * 2;
  182. velocity.z = ( Math.random() - 9 );
  183. velocity.applyQuaternion( controller.quaternion );
  184. physics.setMeshVelocity( spheres, velocity, count );
  185. if ( ++ count === spheres.count ) count = 0;
  186. }
  187. }
  188. function render() {
  189. handleController( controller1 );
  190. handleController( controller2 );
  191. renderer.render( scene, camera );
  192. }
  193. </script>
  194. </body>
  195. </html>