threejs-webvr-look-to-select-w-cursor.html 7.0 KB

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