webxr_xr_cubes.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js xr - cubes</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 - interactive cubes
  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. const clock = new THREE.Clock();
  27. let container;
  28. let camera, scene, raycaster, renderer;
  29. let room;
  30. let controller, controllerGrip;
  31. let INTERSECTED;
  32. init();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x505050 );
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  39. camera.position.set( 0, 1.6, 3 );
  40. scene.add( camera );
  41. room = new THREE.LineSegments(
  42. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ).translate( 0, 3, 0 ),
  43. new THREE.LineBasicMaterial( { color: 0xbcbcbc } )
  44. );
  45. scene.add( room );
  46. scene.add( new THREE.HemisphereLight( 0xa5a5a5, 0x898989, 3 ) );
  47. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  48. light.position.set( 1, 1, 1 ).normalize();
  49. scene.add( light );
  50. const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
  51. for ( let i = 0; i < 200; i ++ ) {
  52. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  53. object.position.x = Math.random() * 4 - 2;
  54. object.position.y = Math.random() * 4;
  55. object.position.z = Math.random() * 4 - 2;
  56. object.rotation.x = Math.random() * 2 * Math.PI;
  57. object.rotation.y = Math.random() * 2 * Math.PI;
  58. object.rotation.z = Math.random() * 2 * Math.PI;
  59. object.scale.x = Math.random() + 0.5;
  60. object.scale.y = Math.random() + 0.5;
  61. object.scale.z = Math.random() + 0.5;
  62. object.userData.velocity = new THREE.Vector3();
  63. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  64. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  65. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  66. room.add( object );
  67. }
  68. raycaster = new THREE.Raycaster();
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. renderer.xr.enabled = true;
  74. container.appendChild( renderer.domElement );
  75. //
  76. function onSelectStart() {
  77. this.userData.isSelecting = true;
  78. }
  79. function onSelectEnd() {
  80. this.userData.isSelecting = false;
  81. }
  82. controller = renderer.xr.getController( 0 );
  83. controller.addEventListener( 'selectstart', onSelectStart );
  84. controller.addEventListener( 'selectend', onSelectEnd );
  85. controller.addEventListener( 'connected', function ( event ) {
  86. this.add( buildController( event.data ) );
  87. } );
  88. controller.addEventListener( 'disconnected', function () {
  89. this.remove( this.children[ 0 ] );
  90. } );
  91. scene.add( controller );
  92. const controllerModelFactory = new XRControllerModelFactory();
  93. controllerGrip = renderer.xr.getControllerGrip( 0 );
  94. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  95. scene.add( controllerGrip );
  96. window.addEventListener( 'resize', onWindowResize );
  97. //
  98. document.body.appendChild( XRButton.createButton( renderer, {
  99. 'optionalFeatures': [ 'depth-sensing' ],
  100. 'depthSensing': { 'usagePreference': [ 'gpu-optimized' ], 'dataFormatPreference': [] }
  101. } ) );
  102. }
  103. function buildController( data ) {
  104. let geometry, material;
  105. switch ( data.targetRayMode ) {
  106. case 'tracked-pointer':
  107. geometry = new THREE.BufferGeometry();
  108. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  109. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  110. material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
  111. return new THREE.Line( geometry, material );
  112. case 'gaze':
  113. geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  114. material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  115. return new THREE.Mesh( geometry, material );
  116. }
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. function animate() {
  125. const delta = clock.getDelta() * 60;
  126. if ( controller.userData.isSelecting === true ) {
  127. const cube = room.children[ 0 ];
  128. room.remove( cube );
  129. cube.position.copy( controller.position );
  130. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  131. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  132. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  133. cube.userData.velocity.applyQuaternion( controller.quaternion );
  134. room.add( cube );
  135. }
  136. // find intersections
  137. raycaster.setFromXRController( controller );
  138. const intersects = raycaster.intersectObjects( room.children, false );
  139. if ( intersects.length > 0 ) {
  140. if ( INTERSECTED != intersects[ 0 ].object ) {
  141. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  142. INTERSECTED = intersects[ 0 ].object;
  143. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  144. INTERSECTED.material.emissive.setHex( 0xff0000 );
  145. }
  146. } else {
  147. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  148. INTERSECTED = undefined;
  149. }
  150. // Keep cubes inside room
  151. for ( let i = 0; i < room.children.length; i ++ ) {
  152. const cube = room.children[ i ];
  153. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  154. cube.position.add( cube.userData.velocity );
  155. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  156. cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
  157. cube.userData.velocity.x = - cube.userData.velocity.x;
  158. }
  159. if ( cube.position.y < 0 || cube.position.y > 6 ) {
  160. cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
  161. cube.userData.velocity.y = - cube.userData.velocity.y;
  162. }
  163. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  164. cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
  165. cube.userData.velocity.z = - cube.userData.velocity.z;
  166. }
  167. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  168. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  169. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  170. }
  171. renderer.render( scene, camera );
  172. }
  173. </script>
  174. </body>
  175. </html>