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

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