webxr-basic-vr-optional.html 4.0 KB

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