1
0

webxr-point-to-select.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. const pointerGeometry = new THREE.BufferGeometry().setFromPoints( [
  107. new THREE.Vector3( 0, 0, 0 ),
  108. new THREE.Vector3( 0, 0, - 1 ),
  109. ] );
  110. this.controllers = [];
  111. for ( let i = 0; i < 2; ++ i ) {
  112. const controller = renderer.xr.getController( i );
  113. controller.addEventListener( 'select', ( event ) => {
  114. const controller = event.target;
  115. const selectedObject = this.controllerToObjectMap.get( controller );
  116. if ( selectedObject ) {
  117. this.dispatchEvent( { type: 'select', controller, selectedObject } );
  118. }
  119. } );
  120. scene.add( controller );
  121. const line = new THREE.Line( pointerGeometry );
  122. line.scale.z = 5;
  123. controller.add( line );
  124. this.controllers.push( { controller, line } );
  125. }
  126. }
  127. reset() {
  128. // restore the colors
  129. this.objectToColorMap.forEach( ( color, object ) => {
  130. object.material.emissive.setHex( color );
  131. } );
  132. this.objectToColorMap.clear();
  133. this.controllerToObjectMap.clear();
  134. }
  135. update( pickablesParent, time ) {
  136. this.reset();
  137. for ( const { controller, line } of this.controllers ) {
  138. // cast a ray through the from the controller
  139. this.raycaster.setFromXRController( controller );
  140. // get the list of objects the ray intersected
  141. const intersections = this.raycaster.intersectObjects( pickablesParent.children );
  142. if ( intersections.length ) {
  143. const intersection = intersections[ 0 ];
  144. // make the line touch the object
  145. line.scale.z = intersection.distance;
  146. // pick the first object. It's the closest one
  147. const pickedObject = intersection.object;
  148. // save which object this controller picked
  149. this.controllerToObjectMap.set( controller, pickedObject );
  150. // highlight the object if we haven't already
  151. if ( this.objectToColorMap.get( pickedObject ) === undefined ) {
  152. // save its color
  153. this.objectToColorMap.set( pickedObject, pickedObject.material.emissive.getHex() );
  154. // set its emissive color to flashing red/yellow
  155. pickedObject.material.emissive.setHex( ( time * 8 ) % 2 > 1 ? 0xFF2000 : 0xFF0000 );
  156. }
  157. } else {
  158. line.scale.z = 5;
  159. }
  160. }
  161. }
  162. }
  163. const pickHelper = new ControllerPickHelper( scene );
  164. pickHelper.addEventListener( 'select', ( event ) => {
  165. event.selectedObject.visible = false;
  166. const partnerObject = meshToMeshMap.get( event.selectedObject );
  167. partnerObject.visible = true;
  168. } );
  169. function resizeRendererToDisplaySize( renderer ) {
  170. const canvas = renderer.domElement;
  171. const width = canvas.clientWidth;
  172. const height = canvas.clientHeight;
  173. const needResize = canvas.width !== width || canvas.height !== height;
  174. if ( needResize ) {
  175. renderer.setSize( width, height, false );
  176. }
  177. return needResize;
  178. }
  179. function render( time ) {
  180. time *= 0.001;
  181. if ( resizeRendererToDisplaySize( renderer ) ) {
  182. const canvas = renderer.domElement;
  183. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  184. camera.updateProjectionMatrix();
  185. }
  186. let ndx = 0;
  187. for ( const mesh of meshToMeshMap.keys() ) {
  188. const speed = 1 + ndx * .1;
  189. const rot = time * speed;
  190. mesh.rotation.x = rot;
  191. mesh.rotation.y = rot;
  192. ++ ndx;
  193. }
  194. pickHelper.update( pickRoot, time );
  195. renderer.render( scene, camera );
  196. }
  197. renderer.setAnimationLoop( render );
  198. }
  199. main();
  200. </script>
  201. </html>