webxr_xr_ballshooter.html 7.8 KB

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