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

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