threejs-webvr-look-to-select-selector.html 3.6 KB

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