webxr_xr_ballshooter.html 8.3 KB

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