threejs-webvr-basic.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script type="module">
  23. import * as THREE from './resources/threejs/r108/build/three.module.js';
  24. import {WEBVR} from './resources/threejs/r108/examples/jsm/vr/WebVR.js';
  25. function main() {
  26. const canvas = document.querySelector('#c');
  27. const renderer = new THREE.WebGLRenderer({canvas});
  28. renderer.vr.enabled = true;
  29. document.body.appendChild(WEBVR.createButton(renderer));
  30. const fov = 75;
  31. const aspect = 2; // the canvas default
  32. const near = 0.1;
  33. const far = 50;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. const scene = new THREE.Scene();
  36. {
  37. const color = 0xFFFFFF;
  38. const intensity = 1;
  39. const light = new THREE.DirectionalLight(color, intensity);
  40. light.position.set(-1, 2, 4);
  41. scene.add(light);
  42. }
  43. const boxWidth = 1;
  44. const boxHeight = 1;
  45. const boxDepth = 1;
  46. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  47. function makeInstance(geometry, color, x) {
  48. const material = new THREE.MeshPhongMaterial({color});
  49. const cube = new THREE.Mesh(geometry, material);
  50. scene.add(cube);
  51. cube.position.x = x;
  52. cube.position.y = 1.6;
  53. cube.position.z = -2;
  54. return cube;
  55. }
  56. const cubes = [
  57. makeInstance(geometry, 0x44aa88, 0),
  58. makeInstance(geometry, 0x8844aa, -2),
  59. makeInstance(geometry, 0xaa8844, 2),
  60. ];
  61. function resizeRendererToDisplaySize(renderer) {
  62. const canvas = renderer.domElement;
  63. const width = canvas.clientWidth;
  64. const height = canvas.clientHeight;
  65. const needResize = canvas.width !== width || canvas.height !== height;
  66. if (needResize) {
  67. renderer.setSize(width, height, false);
  68. }
  69. return needResize;
  70. }
  71. function render(time) {
  72. time *= 0.001;
  73. if (!renderer.vr.isPresenting() && resizeRendererToDisplaySize(renderer)) {
  74. const canvas = renderer.domElement;
  75. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  76. camera.updateProjectionMatrix();
  77. }
  78. cubes.forEach((cube, ndx) => {
  79. const speed = 1 + ndx * .1;
  80. const rot = time * speed;
  81. cube.rotation.x = rot;
  82. cube.rotation.y = rot;
  83. });
  84. renderer.render(scene, camera);
  85. }
  86. renderer.setAnimationLoop(render);
  87. }
  88. main();
  89. </script>
  90. </html>