webxr-look-to-select-w-cursor.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 - Look to Select w/cursor</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. {
  47. const loader = new THREE.CubeTextureLoader();
  48. const texture = loader.load( [
  49. 'resources/images/grid-1024.png',
  50. 'resources/images/grid-1024.png',
  51. 'resources/images/grid-1024.png',
  52. 'resources/images/grid-1024.png',
  53. 'resources/images/grid-1024.png',
  54. 'resources/images/grid-1024.png',
  55. ] );
  56. scene.background = texture;
  57. }
  58. {
  59. const color = 0xFFFFFF;
  60. const intensity = 3;
  61. const light = new THREE.DirectionalLight( color, intensity );
  62. light.position.set( - 1, 2, 4 );
  63. scene.add( light );
  64. }
  65. function makeDataTexture( data, width, height ) {
  66. const texture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  67. texture.minFilter = THREE.NearestFilter;
  68. texture.magFilter = THREE.NearestFilter;
  69. texture.needsUpdate = true;
  70. return texture;
  71. }
  72. const boxWidth = 1;
  73. const boxHeight = 1;
  74. const boxDepth = 1;
  75. const boxGeometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  76. const sphereRadius = 0.5;
  77. const sphereGeometry = new THREE.SphereGeometry( sphereRadius );
  78. function makeInstance( geometry, color, x ) {
  79. const material = new THREE.MeshPhongMaterial( { color } );
  80. const cube = new THREE.Mesh( geometry, material );
  81. scene.add( cube );
  82. cube.position.x = x;
  83. cube.position.y = 1.6;
  84. cube.position.z = - 2;
  85. return cube;
  86. }
  87. const meshToMeshMap = new Map();
  88. [
  89. { x: 0, boxColor: 0x44aa88, sphereColor: 0xFF4444, },
  90. { x: 2, boxColor: 0x8844aa, sphereColor: 0x44FF44, },
  91. { x: - 2, boxColor: 0xaa8844, sphereColor: 0x4444FF, },
  92. ].forEach( ( info ) => {
  93. const { x, boxColor, sphereColor } = info;
  94. const sphere = makeInstance( sphereGeometry, sphereColor, x );
  95. const box = makeInstance( boxGeometry, boxColor, x );
  96. // hide the sphere
  97. sphere.visible = false;
  98. // map the sphere to the box
  99. meshToMeshMap.set( box, sphere );
  100. // map the box to the sphere
  101. meshToMeshMap.set( sphere, box );
  102. } );
  103. class PickHelper {
  104. constructor( camera ) {
  105. this.raycaster = new THREE.Raycaster();
  106. this.pickedObject = null;
  107. this.pickedObjectSavedColor = 0;
  108. const cursorColors = new Uint8Array( [
  109. 64, 64, 64, 64, // dark gray
  110. 255, 255, 255, 255, // white
  111. ] );
  112. this.cursorTexture = makeDataTexture( cursorColors, 2, 1 );
  113. const ringRadius = 0.4;
  114. const tubeRadius = 0.1;
  115. const tubeSegments = 4;
  116. const ringSegments = 64;
  117. const cursorGeometry = new THREE.TorusGeometry(
  118. ringRadius, tubeRadius, tubeSegments, ringSegments );
  119. const cursorMaterial = new THREE.MeshBasicMaterial( {
  120. color: 'white',
  121. map: this.cursorTexture,
  122. transparent: true,
  123. blending: THREE.CustomBlending,
  124. blendSrc: THREE.OneMinusDstColorFactor,
  125. blendDst: THREE.OneMinusSrcColorFactor,
  126. } );
  127. const cursor = new THREE.Mesh( cursorGeometry, cursorMaterial );
  128. camera.add( cursor );
  129. cursor.position.z = - 1;
  130. const scale = 0.05;
  131. cursor.scale.set( scale, scale, scale );
  132. this.cursor = cursor;
  133. this.selectTimer = 0;
  134. this.selectDuration = 2;
  135. this.lastTime = 0;
  136. }
  137. pick( normalizedPosition, scene, camera, time ) {
  138. const elapsedTime = time - this.lastTime;
  139. this.lastTime = time;
  140. const lastPickedObject = this.pickedObject;
  141. // restore the color if there is a picked object
  142. if ( this.pickedObject ) {
  143. this.pickedObject = undefined;
  144. }
  145. // cast a ray through the frustum
  146. this.raycaster.setFromCamera( normalizedPosition, camera );
  147. // get the list of objects the ray intersected
  148. const intersectedObjects = this.raycaster.intersectObjects( scene.children );
  149. if ( intersectedObjects.length ) {
  150. // pick the first object. It's the closest one
  151. this.pickedObject = intersectedObjects[ 0 ].object;
  152. }
  153. // show or hide cursor
  154. this.cursor.visible = this.pickedObject ? true : false;
  155. let selected = false;
  156. // if we're looking at the same object as before
  157. // increment time select timer
  158. if ( this.pickedObject && lastPickedObject === this.pickedObject ) {
  159. this.selectTimer += elapsedTime;
  160. if ( this.selectTimer >= this.selectDuration ) {
  161. this.selectTimer = 0;
  162. selected = true;
  163. }
  164. } else {
  165. this.selectTimer = 0;
  166. }
  167. // set cursor material to show the timer state
  168. const fromStart = 0;
  169. const fromEnd = this.selectDuration;
  170. const toStart = - 0.5;
  171. const toEnd = 0.5;
  172. this.cursorTexture.offset.x = THREE.MathUtils.mapLinear(
  173. this.selectTimer,
  174. fromStart, fromEnd,
  175. toStart, toEnd );
  176. return selected ? this.pickedObject : undefined;
  177. }
  178. }
  179. const pickHelper = new PickHelper( camera );
  180. scene.add( camera );
  181. function resizeRendererToDisplaySize( renderer ) {
  182. const canvas = renderer.domElement;
  183. const width = canvas.clientWidth;
  184. const height = canvas.clientHeight;
  185. const needResize = canvas.width !== width || canvas.height !== height;
  186. if ( needResize ) {
  187. renderer.setSize( width, height, false );
  188. }
  189. return needResize;
  190. }
  191. function render( time ) {
  192. time *= 0.001;
  193. if ( resizeRendererToDisplaySize( renderer ) ) {
  194. const canvas = renderer.domElement;
  195. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  196. camera.updateProjectionMatrix();
  197. }
  198. let ndx = 0;
  199. for ( const mesh of meshToMeshMap.keys() ) {
  200. const speed = 1 + ndx * .1;
  201. const rot = time * speed;
  202. mesh.rotation.x = rot;
  203. mesh.rotation.y = rot;
  204. ++ ndx;
  205. }
  206. // 0, 0 is the center of the view in normalized coordinates.
  207. const selectedObject = pickHelper.pick( { x: 0, y: 0 }, scene, camera, time );
  208. if ( selectedObject ) {
  209. selectedObject.visible = false;
  210. const partnerObject = meshToMeshMap.get( selectedObject );
  211. partnerObject.visible = true;
  212. }
  213. renderer.render( scene, camera );
  214. }
  215. renderer.setAnimationLoop( render );
  216. }
  217. main();
  218. </script>
  219. </html>