webxr-basic-vr-optional.html 4.0 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 - 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. <!-- Import maps polyfill -->
  33. <!-- Remove this when import maps will be widely supported -->
  34. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../../build/three.module.js"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import {VRButton} from '../../examples/jsm/webxr/VRButton.js';
  45. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  46. function main() {
  47. const canvas = document.querySelector('#c');
  48. const renderer = new THREE.WebGLRenderer({canvas});
  49. const fov = 75;
  50. const aspect = 2; // the canvas default
  51. const near = 0.1;
  52. const far = 5;
  53. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  54. camera.position.set(0, 1.6, 0);
  55. const params = (new URL(document.location)).searchParams;
  56. const allowvr = params.get('allowvr') === 'true';
  57. if (allowvr) {
  58. renderer.xr.enabled = true;
  59. document.body.appendChild(VRButton.createButton(renderer));
  60. document.querySelector('#vr').style.display = 'none';
  61. } else {
  62. // no VR, add some controls
  63. const controls = new OrbitControls(camera, canvas);
  64. controls.target.set(0, 1.6, -2);
  65. controls.update();
  66. document.querySelector('#nonvr').style.display = 'none';
  67. }
  68. const scene = new THREE.Scene();
  69. {
  70. const loader = new THREE.CubeTextureLoader();
  71. const texture = loader.load([
  72. 'resources/images/grid-1024.png',
  73. 'resources/images/grid-1024.png',
  74. 'resources/images/grid-1024.png',
  75. 'resources/images/grid-1024.png',
  76. 'resources/images/grid-1024.png',
  77. 'resources/images/grid-1024.png',
  78. ]);
  79. scene.background = texture;
  80. }
  81. {
  82. const color = 0xFFFFFF;
  83. const intensity = 1;
  84. const light = new THREE.DirectionalLight(color, intensity);
  85. light.position.set(-1, 2, 4);
  86. scene.add(light);
  87. }
  88. const boxWidth = 1;
  89. const boxHeight = 1;
  90. const boxDepth = 1;
  91. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  92. function makeInstance(geometry, color, x) {
  93. const material = new THREE.MeshPhongMaterial({color});
  94. const cube = new THREE.Mesh(geometry, material);
  95. scene.add(cube);
  96. cube.position.x = x;
  97. cube.position.y = 1.6;
  98. cube.position.z = -2;
  99. return cube;
  100. }
  101. const cubes = [
  102. makeInstance(geometry, 0x44aa88, 0),
  103. makeInstance(geometry, 0x8844aa, -2),
  104. makeInstance(geometry, 0xaa8844, 2),
  105. ];
  106. function resizeRendererToDisplaySize(renderer) {
  107. const canvas = renderer.domElement;
  108. const width = canvas.clientWidth;
  109. const height = canvas.clientHeight;
  110. const needResize = canvas.width !== width || canvas.height !== height;
  111. if (needResize) {
  112. renderer.setSize(width, height, false);
  113. }
  114. return needResize;
  115. }
  116. function render(time) {
  117. time *= 0.001;
  118. if (resizeRendererToDisplaySize(renderer)) {
  119. const canvas = renderer.domElement;
  120. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  121. camera.updateProjectionMatrix();
  122. }
  123. cubes.forEach((cube, ndx) => {
  124. const speed = 1 + ndx * .1;
  125. const rot = time * speed;
  126. cube.rotation.x = rot;
  127. cube.rotation.y = rot;
  128. });
  129. renderer.render(scene, camera);
  130. }
  131. renderer.setAnimationLoop(render);
  132. }
  133. main();
  134. </script>
  135. </html>