webxr-basic-vr-optional.html 3.8 KB

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