webxr-basic-vr-optional.html 4.0 KB

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