threejs-webvr-basic-vr-optional.html 3.7 KB

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