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

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