2
0

webxr-point-to-select.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - WebXR - Point to Select</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { VRButton } from 'three/addons/webxr/VRButton.js';
  34. function main() {
  35. const canvas = document.querySelector( '#c' );
  36. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  37. renderer.xr.enabled = true;
  38. document.body.appendChild( VRButton.createButton( renderer ) );
  39. const fov = 75;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 50;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.set( 0, 1.6, 0 );
  45. const scene = new THREE.Scene();
  46. // object to put pickable objects on so we can easily
  47. // separate them from non-pickable objects
  48. const pickRoot = new THREE.Object3D();
  49. scene.add( pickRoot );
  50. {
  51. const loader = new THREE.CubeTextureLoader();
  52. const texture = loader.load( [
  53. 'resources/images/grid-1024.png',
  54. 'resources/images/grid-1024.png',
  55. 'resources/images/grid-1024.png',
  56. 'resources/images/grid-1024.png',
  57. 'resources/images/grid-1024.png',
  58. 'resources/images/grid-1024.png',
  59. ] );
  60. scene.background = texture;
  61. }
  62. {
  63. const color = 0xFFFFFF;
  64. const intensity = 3;
  65. const light = new THREE.DirectionalLight( color, intensity );
  66. light.position.set( - 1, 2, 4 );
  67. scene.add( light );
  68. }
  69. const boxWidth = 1;
  70. const boxHeight = 1;
  71. const boxDepth = 1;
  72. const boxGeometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  73. const sphereRadius = 0.5;
  74. const sphereGeometry = new THREE.SphereGeometry( sphereRadius );
  75. function makeInstance( geometry, color, x ) {
  76. const material = new THREE.MeshPhongMaterial( { color } );
  77. const cube = new THREE.Mesh( geometry, material );
  78. pickRoot.add( cube );
  79. cube.position.x = x;
  80. cube.position.y = 1.6;
  81. cube.position.z = - 2;
  82. return cube;
  83. }
  84. const meshToMeshMap = new Map();
  85. [
  86. { x: 0, boxColor: 0x44aa88, sphereColor: 0xFF4444, },
  87. { x: 2, boxColor: 0x8844aa, sphereColor: 0x44FF44, },
  88. { x: - 2, boxColor: 0xaa8844, sphereColor: 0x4444FF, },
  89. ].forEach( ( info ) => {
  90. const { x, boxColor, sphereColor } = info;
  91. const sphere = makeInstance( sphereGeometry, sphereColor, x );
  92. const box = makeInstance( boxGeometry, boxColor, x );
  93. // hide the sphere
  94. sphere.visible = false;
  95. // map the sphere to the box
  96. meshToMeshMap.set( box, sphere );
  97. // map the box to the sphere
  98. meshToMeshMap.set( sphere, box );
  99. } );
  100. class ControllerPickHelper extends THREE.EventDispatcher {
  101. constructor( scene ) {
  102. super();
  103. this.raycaster = new THREE.Raycaster();
  104. this.objectToColorMap = new Map();
  105. this.controllerToObjectMap = new Map();
  106. this.tempMatrix = new THREE.Matrix4();
  107. const pointerGeometry = new THREE.BufferGeometry().setFromPoints( [
  108. new THREE.Vector3( 0, 0, 0 ),
  109. new THREE.Vector3( 0, 0, - 1 ),
  110. ] );
  111. this.controllers = [];
  112. for ( let i = 0; i < 2; ++ i ) {
  113. const controller = renderer.xr.getController( i );
  114. controller.addEventListener( 'select', ( event ) => {
  115. const controller = event.target;
  116. const selectedObject = this.controllerToObjectMap.get( controller );
  117. if ( selectedObject ) {
  118. this.dispatchEvent( { type: 'select', controller, selectedObject } );
  119. }
  120. } );
  121. scene.add( controller );
  122. const line = new THREE.Line( pointerGeometry );
  123. line.scale.z = 5;
  124. controller.add( line );
  125. this.controllers.push( { controller, line } );
  126. }
  127. }
  128. reset() {
  129. // restore the colors
  130. this.objectToColorMap.forEach( ( color, object ) => {
  131. object.material.emissive.setHex( color );
  132. } );
  133. this.objectToColorMap.clear();
  134. this.controllerToObjectMap.clear();
  135. }
  136. update( pickablesParent, time ) {
  137. this.reset();
  138. for ( const { controller, line } of this.controllers ) {
  139. // cast a ray through the from the controller
  140. this.tempMatrix.identity().extractRotation( controller.matrixWorld );
  141. this.raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  142. this.raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( this.tempMatrix );
  143. // get the list of objects the ray intersected
  144. const intersections = this.raycaster.intersectObjects( pickablesParent.children );
  145. if ( intersections.length ) {
  146. const intersection = intersections[ 0 ];
  147. // make the line touch the object
  148. line.scale.z = intersection.distance;
  149. // pick the first object. It's the closest one
  150. const pickedObject = intersection.object;
  151. // save which object this controller picked
  152. this.controllerToObjectMap.set( controller, pickedObject );
  153. // highlight the object if we haven't already
  154. if ( this.objectToColorMap.get( pickedObject ) === undefined ) {
  155. // save its color
  156. this.objectToColorMap.set( pickedObject, pickedObject.material.emissive.getHex() );
  157. // set its emissive color to flashing red/yellow
  158. pickedObject.material.emissive.setHex( ( time * 8 ) % 2 > 1 ? 0xFF2000 : 0xFF0000 );
  159. }
  160. } else {
  161. line.scale.z = 5;
  162. }
  163. }
  164. }
  165. }
  166. const pickHelper = new ControllerPickHelper( scene );
  167. pickHelper.addEventListener( 'select', ( event ) => {
  168. event.selectedObject.visible = false;
  169. const partnerObject = meshToMeshMap.get( event.selectedObject );
  170. partnerObject.visible = true;
  171. } );
  172. function resizeRendererToDisplaySize( renderer ) {
  173. const canvas = renderer.domElement;
  174. const width = canvas.clientWidth;
  175. const height = canvas.clientHeight;
  176. const needResize = canvas.width !== width || canvas.height !== height;
  177. if ( needResize ) {
  178. renderer.setSize( width, height, false );
  179. }
  180. return needResize;
  181. }
  182. function render( time ) {
  183. time *= 0.001;
  184. if ( resizeRendererToDisplaySize( renderer ) ) {
  185. const canvas = renderer.domElement;
  186. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  187. camera.updateProjectionMatrix();
  188. }
  189. let ndx = 0;
  190. for ( const mesh of meshToMeshMap.keys() ) {
  191. const speed = 1 + ndx * .1;
  192. const rot = time * speed;
  193. mesh.rotation.x = rot;
  194. mesh.rotation.y = rot;
  195. ++ ndx;
  196. }
  197. pickHelper.update( pickRoot, time );
  198. renderer.render( scene, camera );
  199. }
  200. renderer.setAnimationLoop( render );
  201. }
  202. main();
  203. </script>
  204. </html>