webxr-look-to-select-selector.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 (selection 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. function main() {
  26. const canvas = document.querySelector('#c');
  27. const renderer = new THREE.WebGLRenderer({canvas});
  28. const left = -2; // Use values for left
  29. const right = 2; // right, top and bottom
  30. const top = 1; // that match the default
  31. const bottom = -1; // canvas size.
  32. const near = -1;
  33. const far = 1;
  34. const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
  35. function makeDataTexture(data, width, height) {
  36. const texture = new THREE.DataTexture(data, width, height, THREE.RGBAFormat);
  37. texture.minFilter = THREE.NearestFilter;
  38. texture.magFilter = THREE.NearestFilter;
  39. texture.needsUpdate = true;
  40. return texture;
  41. }
  42. const backgroundColors = new Uint8Array([
  43. 0, 0, 0, 255, // black
  44. 90, 38, 38, 255, // dark red
  45. 100, 175, 103, 255, // medium green
  46. 255, 239, 151, 255, // light yellow
  47. ]);
  48. const backgroundTexture = makeDataTexture(backgroundColors, 2, 2);
  49. backgroundTexture.wrapS = THREE.RepeatWrapping;
  50. backgroundTexture.wrapT = THREE.RepeatWrapping;
  51. backgroundTexture.repeat.set(4, 4);
  52. const scene = new THREE.Scene();
  53. scene.background = backgroundTexture;
  54. //const innerRadius = 0.4;
  55. //const outerRadius = 0.5;
  56. //const segments = 64;
  57. //const cursorGeometry = new THREE.RingGeometry(
  58. // innerRadius, outerRadius, segments);
  59. const ringRadius = 0.4;
  60. const tubeRadius = 0.1;
  61. const tubeSegments = 4;
  62. const ringSegments = 64;
  63. const cursorGeometry = new THREE.TorusGeometry(
  64. ringRadius, tubeRadius, tubeSegments, ringSegments);
  65. const cursorColors = new Uint8Array([
  66. 64, 64, 64, 64, // dark gray
  67. 255, 255, 255, 255, // white
  68. ]);
  69. const cursorTexture = makeDataTexture(cursorColors, 2, 1);
  70. const cursorMaterial = new THREE.MeshBasicMaterial({
  71. color: 'white',
  72. map: cursorTexture,
  73. transparent: true,
  74. blending: THREE.CustomBlending,
  75. blendSrc: THREE.OneMinusDstColorFactor,
  76. blendDst: THREE.OneMinusSrcColorFactor,
  77. });
  78. const cursor = new THREE.Mesh(cursorGeometry, cursorMaterial);
  79. scene.add(cursor);
  80. function resizeRendererToDisplaySize(renderer) {
  81. const canvas = renderer.domElement;
  82. const width = canvas.clientWidth;
  83. const height = canvas.clientHeight;
  84. const needResize = canvas.width !== width || canvas.height !== height;
  85. if (needResize) {
  86. renderer.setSize(width, height, false);
  87. }
  88. return needResize;
  89. }
  90. function render(time) {
  91. time *= 0.001;
  92. if (resizeRendererToDisplaySize(renderer)) {
  93. const canvas = renderer.domElement;
  94. const aspect = canvas.clientWidth / canvas.clientHeight;
  95. camera.left = -aspect;
  96. camera.right = aspect;
  97. camera.updateProjectionMatrix();
  98. }
  99. const fromStart = 0;
  100. const fromEnd = 2;
  101. const toStart = -0.5;
  102. const toEnd = 0.5;
  103. cursorTexture.offset.x = THREE.MathUtils.mapLinear(
  104. time % 2,
  105. fromStart, fromEnd,
  106. toStart, toEnd);
  107. renderer.render(scene, camera);
  108. }
  109. renderer.setAnimationLoop(render);
  110. }
  111. main();
  112. </script>
  113. </html>