webxr_xr_ballshooter.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 ) );
  51. const light = new THREE.DirectionalLight( 0xffffff );
  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.xr.enabled = true;
  60. document.body.appendChild( renderer.domElement );
  61. //
  62. const controls = new OrbitControls( camera, renderer.domElement );
  63. controls.maxDistance = 10;
  64. controls.target.y = 1.6;
  65. controls.update();
  66. document.body.appendChild( XRButton.createButton( renderer ) );
  67. // controllers
  68. function onSelectStart() {
  69. this.userData.isSelecting = true;
  70. }
  71. function onSelectEnd() {
  72. this.userData.isSelecting = false;
  73. }
  74. controller1 = renderer.xr.getController( 0 );
  75. controller1.addEventListener( 'selectstart', onSelectStart );
  76. controller1.addEventListener( 'selectend', onSelectEnd );
  77. controller1.addEventListener( 'connected', function ( event ) {
  78. this.add( buildController( event.data ) );
  79. } );
  80. controller1.addEventListener( 'disconnected', function () {
  81. this.remove( this.children[ 0 ] );
  82. } );
  83. scene.add( controller1 );
  84. controller2 = renderer.xr.getController( 1 );
  85. controller2.addEventListener( 'selectstart', onSelectStart );
  86. controller2.addEventListener( 'selectend', onSelectEnd );
  87. controller2.addEventListener( 'connected', function ( event ) {
  88. this.add( buildController( event.data ) );
  89. } );
  90. controller2.addEventListener( 'disconnected', function () {
  91. this.remove( this.children[ 0 ] );
  92. } );
  93. scene.add( controller2 );
  94. // The XRControllerModelFactory will automatically fetch controller models
  95. // that match what the user is holding as closely as possible. The models
  96. // should be attached to the object returned from getControllerGrip in
  97. // order to match the orientation of the held device.
  98. const controllerModelFactory = new XRControllerModelFactory();
  99. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  100. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  101. scene.add( controllerGrip1 );
  102. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  103. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  104. scene.add( controllerGrip2 );
  105. //
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function buildController( data ) {
  109. let geometry, material;
  110. switch ( data.targetRayMode ) {
  111. case 'tracked-pointer':
  112. geometry = new THREE.BufferGeometry();
  113. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  114. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  115. material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
  116. return new THREE.Line( geometry, material );
  117. case 'gaze':
  118. geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  119. material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  120. return new THREE.Mesh( geometry, material );
  121. }
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. async function initPhysics() {
  129. physics = await RapierPhysics();
  130. {
  131. // Floor
  132. const geometry = new THREE.BoxGeometry( 6, 2, 6 );
  133. const material = new THREE.MeshNormalMaterial();
  134. const floor = new THREE.Mesh( geometry, material );
  135. floor.position.y = - 1;
  136. physics.addMesh( floor );
  137. // Walls
  138. const wallPX = new THREE.Mesh( geometry, material );
  139. wallPX.position.set( 4, 3, 0 );
  140. wallPX.rotation.z = Math.PI / 2;
  141. physics.addMesh( wallPX );
  142. const wallNX = new THREE.Mesh( geometry, material );
  143. wallNX.position.set( - 4, 3, 0 );
  144. wallNX.rotation.z = Math.PI / 2;
  145. physics.addMesh( wallNX );
  146. const wallPZ = new THREE.Mesh( geometry, material );
  147. wallPZ.position.set( 0, 3, 4 );
  148. wallPZ.rotation.x = Math.PI / 2;
  149. physics.addMesh( wallPZ );
  150. const wallNZ = new THREE.Mesh( geometry, material );
  151. wallNZ.position.set( 0, 3, - 4 );
  152. wallNZ.rotation.x = Math.PI / 2;
  153. physics.addMesh( wallNZ );
  154. }
  155. // Spheres
  156. const geometry = new THREE.IcosahedronGeometry( 0.08, 3 );
  157. const material = new THREE.MeshLambertMaterial();
  158. spheres = new THREE.InstancedMesh( geometry, material, 800 );
  159. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  160. scene.add( spheres );
  161. const matrix = new THREE.Matrix4();
  162. const color = new THREE.Color();
  163. for ( let i = 0; i < spheres.count; i ++ ) {
  164. const x = Math.random() * 4 - 2;
  165. const y = Math.random() * 4;
  166. const z = Math.random() * 4 - 2;
  167. matrix.setPosition( x, y, z );
  168. spheres.setMatrixAt( i, matrix );
  169. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  170. }
  171. physics.addMesh( spheres, 1, 1.1 );
  172. }
  173. //
  174. function handleController( controller ) {
  175. if ( controller.userData.isSelecting ) {
  176. physics.setMeshPosition( spheres, controller.position, count );
  177. velocity.x = ( Math.random() - 0.5 ) * 2;
  178. velocity.y = ( Math.random() - 0.5 ) * 2;
  179. velocity.z = ( Math.random() - 9 );
  180. velocity.applyQuaternion( controller.quaternion );
  181. physics.setMeshVelocity( spheres, velocity, count );
  182. if ( ++ count === spheres.count ) count = 0;
  183. }
  184. }
  185. function render() {
  186. handleController( controller1 );
  187. handleController( controller2 );
  188. renderer.render( scene, camera );
  189. }
  190. </script>
  191. </body>
  192. </html>