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

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