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

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