webxr-basic-vr-optional.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 - Basic - VR Optional</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. .mode {
  19. position: absolute;
  20. right: 1em;
  21. top: 1em;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <canvas id="c"></canvas>
  27. <div class="mode">
  28. <a href="?allowvr=true" id="vr">Allow VR</a>
  29. <a href="?" id="nonvr">Use Non-VR Mode</a>
  30. </div>
  31. </body>
  32. <script type="module">
  33. import * as THREE from '../../build/three.module.js';
  34. import {VRButton} from '../../examples/jsm/webxr/VRButton.js';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. function main() {
  37. const canvas = document.querySelector('#c');
  38. const renderer = new THREE.WebGLRenderer({canvas});
  39. const fov = 75;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 5;
  43. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  44. camera.position.set(0, 1.6, 0);
  45. const params = (new URL(document.location)).searchParams;
  46. const allowvr = params.get('allowvr') === 'true';
  47. if (allowvr) {
  48. renderer.xr.enabled = true;
  49. document.body.appendChild(VRButton.createButton(renderer));
  50. document.querySelector('#vr').style.display = 'none';
  51. } else {
  52. // no VR, add some controls
  53. const controls = new OrbitControls(camera, canvas);
  54. controls.target.set(0, 1.6, -2);
  55. controls.update();
  56. document.querySelector('#nonvr').style.display = 'none';
  57. }
  58. const scene = new THREE.Scene();
  59. {
  60. const loader = new THREE.CubeTextureLoader();
  61. const texture = loader.load([
  62. 'resources/images/grid-1024.png',
  63. 'resources/images/grid-1024.png',
  64. 'resources/images/grid-1024.png',
  65. 'resources/images/grid-1024.png',
  66. 'resources/images/grid-1024.png',
  67. 'resources/images/grid-1024.png',
  68. ]);
  69. scene.background = texture;
  70. }
  71. {
  72. const color = 0xFFFFFF;
  73. const intensity = 1;
  74. const light = new THREE.DirectionalLight(color, intensity);
  75. light.position.set(-1, 2, 4);
  76. scene.add(light);
  77. }
  78. const boxWidth = 1;
  79. const boxHeight = 1;
  80. const boxDepth = 1;
  81. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  82. function makeInstance(geometry, color, x) {
  83. const material = new THREE.MeshPhongMaterial({color});
  84. const cube = new THREE.Mesh(geometry, material);
  85. scene.add(cube);
  86. cube.position.x = x;
  87. cube.position.y = 1.6;
  88. cube.position.z = -2;
  89. return cube;
  90. }
  91. const cubes = [
  92. makeInstance(geometry, 0x44aa88, 0),
  93. makeInstance(geometry, 0x8844aa, -2),
  94. makeInstance(geometry, 0xaa8844, 2),
  95. ];
  96. function resizeRendererToDisplaySize(renderer) {
  97. const canvas = renderer.domElement;
  98. const width = canvas.clientWidth;
  99. const height = canvas.clientHeight;
  100. const needResize = canvas.width !== width || canvas.height !== height;
  101. if (needResize) {
  102. renderer.setSize(width, height, false);
  103. }
  104. return needResize;
  105. }
  106. function render(time) {
  107. time *= 0.001;
  108. if (resizeRendererToDisplaySize(renderer)) {
  109. const canvas = renderer.domElement;
  110. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  111. camera.updateProjectionMatrix();
  112. }
  113. cubes.forEach((cube, ndx) => {
  114. const speed = 1 + ndx * .1;
  115. const rot = time * speed;
  116. cube.rotation.x = rot;
  117. cube.rotation.y = rot;
  118. });
  119. renderer.render(scene, camera);
  120. }
  121. renderer.setAnimationLoop(render);
  122. }
  123. main();
  124. </script>
  125. </html>