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

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