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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js",
  30. "three/addons/": "../../examples/jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { VRButton } from 'three/addons/webxr/VRButton.js';
  37. function main() {
  38. const canvas = document.querySelector( '#c' );
  39. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  40. renderer.useLegacyLights = false;
  41. renderer.xr.enabled = true;
  42. document.body.appendChild( VRButton.createButton( renderer ) );
  43. const fov = 75;
  44. const aspect = 2; // the canvas default
  45. const near = 0.1;
  46. const far = 50;
  47. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  48. camera.position.set( 0, 1.6, 0 );
  49. const scene = new THREE.Scene();
  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. function makeDataTexture( data, width, height ) {
  70. const texture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  71. texture.minFilter = THREE.NearestFilter;
  72. texture.magFilter = THREE.NearestFilter;
  73. texture.needsUpdate = true;
  74. return texture;
  75. }
  76. const boxWidth = 1;
  77. const boxHeight = 1;
  78. const boxDepth = 1;
  79. const boxGeometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  80. const sphereRadius = 0.5;
  81. const sphereGeometry = new THREE.SphereGeometry( sphereRadius );
  82. function makeInstance( geometry, color, x ) {
  83. const material = new THREE.MeshPhongMaterial( { color } );
  84. const cube = new THREE.Mesh( geometry, material );
  85. scene.add( cube );
  86. cube.position.x = x;
  87. cube.position.y = 1.6;
  88. cube.position.z = - 2;
  89. return cube;
  90. }
  91. const meshToMeshMap = new Map();
  92. [
  93. { x: 0, boxColor: 0x44aa88, sphereColor: 0xFF4444, },
  94. { x: 2, boxColor: 0x8844aa, sphereColor: 0x44FF44, },
  95. { x: - 2, boxColor: 0xaa8844, sphereColor: 0x4444FF, },
  96. ].forEach( ( info ) => {
  97. const { x, boxColor, sphereColor } = info;
  98. const sphere = makeInstance( sphereGeometry, sphereColor, x );
  99. const box = makeInstance( boxGeometry, boxColor, x );
  100. // hide the sphere
  101. sphere.visible = false;
  102. // map the sphere to the box
  103. meshToMeshMap.set( box, sphere );
  104. // map the box to the sphere
  105. meshToMeshMap.set( sphere, box );
  106. } );
  107. class PickHelper {
  108. constructor( camera ) {
  109. this.raycaster = new THREE.Raycaster();
  110. this.pickedObject = null;
  111. this.pickedObjectSavedColor = 0;
  112. const cursorColors = new Uint8Array( [
  113. 64, 64, 64, 64, // dark gray
  114. 255, 255, 255, 255, // white
  115. ] );
  116. this.cursorTexture = makeDataTexture( cursorColors, 2, 1 );
  117. const ringRadius = 0.4;
  118. const tubeRadius = 0.1;
  119. const tubeSegments = 4;
  120. const ringSegments = 64;
  121. const cursorGeometry = new THREE.TorusGeometry(
  122. ringRadius, tubeRadius, tubeSegments, ringSegments );
  123. const cursorMaterial = new THREE.MeshBasicMaterial( {
  124. color: 'white',
  125. map: this.cursorTexture,
  126. transparent: true,
  127. blending: THREE.CustomBlending,
  128. blendSrc: THREE.OneMinusDstColorFactor,
  129. blendDst: THREE.OneMinusSrcColorFactor,
  130. } );
  131. const cursor = new THREE.Mesh( cursorGeometry, cursorMaterial );
  132. camera.add( cursor );
  133. cursor.position.z = - 1;
  134. const scale = 0.05;
  135. cursor.scale.set( scale, scale, scale );
  136. this.cursor = cursor;
  137. this.selectTimer = 0;
  138. this.selectDuration = 2;
  139. this.lastTime = 0;
  140. }
  141. pick( normalizedPosition, scene, camera, time ) {
  142. const elapsedTime = time - this.lastTime;
  143. this.lastTime = time;
  144. const lastPickedObject = this.pickedObject;
  145. // restore the color if there is a picked object
  146. if ( this.pickedObject ) {
  147. this.pickedObject = undefined;
  148. }
  149. // cast a ray through the frustum
  150. this.raycaster.setFromCamera( normalizedPosition, camera );
  151. // get the list of objects the ray intersected
  152. const intersectedObjects = this.raycaster.intersectObjects( scene.children );
  153. if ( intersectedObjects.length ) {
  154. // pick the first object. It's the closest one
  155. this.pickedObject = intersectedObjects[ 0 ].object;
  156. }
  157. // show or hide cursor
  158. this.cursor.visible = this.pickedObject ? true : false;
  159. let selected = false;
  160. // if we're looking at the same object as before
  161. // increment time select timer
  162. if ( this.pickedObject && lastPickedObject === this.pickedObject ) {
  163. this.selectTimer += elapsedTime;
  164. if ( this.selectTimer >= this.selectDuration ) {
  165. this.selectTimer = 0;
  166. selected = true;
  167. }
  168. } else {
  169. this.selectTimer = 0;
  170. }
  171. // set cursor material to show the timer state
  172. const fromStart = 0;
  173. const fromEnd = this.selectDuration;
  174. const toStart = - 0.5;
  175. const toEnd = 0.5;
  176. this.cursorTexture.offset.x = THREE.MathUtils.mapLinear(
  177. this.selectTimer,
  178. fromStart, fromEnd,
  179. toStart, toEnd );
  180. return selected ? this.pickedObject : undefined;
  181. }
  182. }
  183. const pickHelper = new PickHelper( camera );
  184. scene.add( camera );
  185. function resizeRendererToDisplaySize( renderer ) {
  186. const canvas = renderer.domElement;
  187. const width = canvas.clientWidth;
  188. const height = canvas.clientHeight;
  189. const needResize = canvas.width !== width || canvas.height !== height;
  190. if ( needResize ) {
  191. renderer.setSize( width, height, false );
  192. }
  193. return needResize;
  194. }
  195. function render( time ) {
  196. time *= 0.001;
  197. if ( resizeRendererToDisplaySize( renderer ) ) {
  198. const canvas = renderer.domElement;
  199. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  200. camera.updateProjectionMatrix();
  201. }
  202. let ndx = 0;
  203. for ( const mesh of meshToMeshMap.keys() ) {
  204. const speed = 1 + ndx * .1;
  205. const rot = time * speed;
  206. mesh.rotation.x = rot;
  207. mesh.rotation.y = rot;
  208. ++ ndx;
  209. }
  210. // 0, 0 is the center of the view in normalized coordinates.
  211. const selectedObject = pickHelper.pick( { x: 0, y: 0 }, scene, camera, time );
  212. if ( selectedObject ) {
  213. selectedObject.visible = false;
  214. const partnerObject = meshToMeshMap.get( selectedObject );
  215. partnerObject.visible = true;
  216. }
  217. renderer.render( scene, camera );
  218. }
  219. renderer.setAnimationLoop( render );
  220. }
  221. main();
  222. </script>
  223. </html>