webxr_xr_cubes.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. <!-- 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. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  30. const clock = new THREE.Clock();
  31. let container;
  32. let camera, scene, raycaster, renderer;
  33. let room;
  34. let controller, controllerGrip;
  35. let INTERSECTED;
  36. const tempMatrix = new THREE.Matrix4();
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0x505050 );
  44. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  45. camera.position.set( 0, 1.6, 3 );
  46. scene.add( camera );
  47. room = new THREE.LineSegments(
  48. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ).translate( 0, 3, 0 ),
  49. new THREE.LineBasicMaterial( { color: 0x808080 } )
  50. );
  51. scene.add( room );
  52. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  53. const light = new THREE.DirectionalLight( 0xffffff );
  54. light.position.set( 1, 1, 1 ).normalize();
  55. scene.add( light );
  56. const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
  57. for ( let i = 0; i < 200; i ++ ) {
  58. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  59. object.position.x = Math.random() * 4 - 2;
  60. object.position.y = Math.random() * 4;
  61. object.position.z = Math.random() * 4 - 2;
  62. object.rotation.x = Math.random() * 2 * Math.PI;
  63. object.rotation.y = Math.random() * 2 * Math.PI;
  64. object.rotation.z = Math.random() * 2 * Math.PI;
  65. object.scale.x = Math.random() + 0.5;
  66. object.scale.y = Math.random() + 0.5;
  67. object.scale.z = Math.random() + 0.5;
  68. object.userData.velocity = new THREE.Vector3();
  69. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  70. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  71. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  72. room.add( object );
  73. }
  74. raycaster = new THREE.Raycaster();
  75. renderer = new THREE.WebGLRenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.xr.enabled = true;
  79. container.appendChild( renderer.domElement );
  80. //
  81. function onSelectStart() {
  82. this.userData.isSelecting = true;
  83. }
  84. function onSelectEnd() {
  85. this.userData.isSelecting = false;
  86. }
  87. controller = renderer.xr.getController( 0 );
  88. controller.addEventListener( 'selectstart', onSelectStart );
  89. controller.addEventListener( 'selectend', onSelectEnd );
  90. controller.addEventListener( 'connected', function ( event ) {
  91. this.add( buildController( event.data ) );
  92. } );
  93. controller.addEventListener( 'disconnected', function () {
  94. this.remove( this.children[ 0 ] );
  95. } );
  96. scene.add( controller );
  97. const controllerModelFactory = new XRControllerModelFactory();
  98. controllerGrip = renderer.xr.getControllerGrip( 0 );
  99. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  100. scene.add( controllerGrip );
  101. window.addEventListener( 'resize', onWindowResize );
  102. //
  103. document.body.appendChild( XRButton.createButton( renderer ) );
  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. //
  126. function animate() {
  127. renderer.setAnimationLoop( render );
  128. }
  129. function render() {
  130. const delta = clock.getDelta() * 60;
  131. if ( controller.userData.isSelecting === true ) {
  132. const cube = room.children[ 0 ];
  133. room.remove( cube );
  134. cube.position.copy( controller.position );
  135. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  136. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  137. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  138. cube.userData.velocity.applyQuaternion( controller.quaternion );
  139. room.add( cube );
  140. }
  141. // find intersections
  142. tempMatrix.identity().extractRotation( controller.matrixWorld );
  143. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  144. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  145. const intersects = raycaster.intersectObjects( room.children, false );
  146. if ( intersects.length > 0 ) {
  147. if ( INTERSECTED != intersects[ 0 ].object ) {
  148. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  149. INTERSECTED = intersects[ 0 ].object;
  150. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  151. INTERSECTED.material.emissive.setHex( 0xff0000 );
  152. }
  153. } else {
  154. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  155. INTERSECTED = undefined;
  156. }
  157. // Keep cubes inside room
  158. for ( let i = 0; i < room.children.length; i ++ ) {
  159. const cube = room.children[ i ];
  160. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  161. cube.position.add( cube.userData.velocity );
  162. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  163. cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
  164. cube.userData.velocity.x = - cube.userData.velocity.x;
  165. }
  166. if ( cube.position.y < 0 || cube.position.y > 6 ) {
  167. cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
  168. cube.userData.velocity.y = - cube.userData.velocity.y;
  169. }
  170. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  171. cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
  172. cube.userData.velocity.z = - cube.userData.velocity.z;
  173. }
  174. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  175. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  176. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  177. }
  178. renderer.render( scene, camera );
  179. }
  180. </script>
  181. </body>
  182. </html>