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

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