webxr_vr_panorama_depth.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - panorama with depth</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - panorama with depth<br />
  13. Created by <a href="https://orfleisher.com" target="_blank" rel="noopener">@juniorxsound</a>. Panorama from <a href="https://krpano.com/examples/?depthmap" target="_blank" rel="noopener">krpano</a>.
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { VRButton } from 'three/addons/webxr/VRButton.js';
  29. let camera, scene, renderer, sphere, clock;
  30. init();
  31. animate();
  32. function init() {
  33. const container = document.getElementById( 'container' );
  34. clock = new THREE.Clock();
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0x101010 );
  37. const light = new THREE.AmbientLight( 0xffffff, 1 );
  38. scene.add( light );
  39. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  40. scene.add( camera );
  41. // Create the panoramic sphere geometery
  42. const panoSphereGeo = new THREE.SphereGeometry( 6, 256, 256 );
  43. // Create the panoramic sphere material
  44. const panoSphereMat = new THREE.MeshStandardMaterial( {
  45. side: THREE.BackSide,
  46. displacementScale: - 4.0
  47. } );
  48. // Create the panoramic sphere mesh
  49. sphere = new THREE.Mesh( panoSphereGeo, panoSphereMat );
  50. // Load and assign the texture and depth map
  51. const manager = new THREE.LoadingManager();
  52. const loader = new THREE.TextureLoader( manager );
  53. loader.load( './textures/kandao3.jpg', function ( texture ) {
  54. texture.colorSpace = THREE.SRGBColorSpace;
  55. texture.minFilter = THREE.NearestFilter;
  56. texture.generateMipmaps = false;
  57. sphere.material.map = texture;
  58. } );
  59. loader.load( './textures/kandao3_depthmap.jpg', function ( depth ) {
  60. depth.minFilter = THREE.NearestFilter;
  61. depth.generateMipmaps = false;
  62. sphere.material.displacementMap = depth;
  63. } );
  64. // On load complete add the panoramic sphere to the scene
  65. manager.onLoad = function () {
  66. scene.add( sphere );
  67. };
  68. renderer = new THREE.WebGLRenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.xr.enabled = true;
  72. renderer.xr.setReferenceSpaceType( 'local' );
  73. container.appendChild( renderer.domElement );
  74. document.body.appendChild( VRButton.createButton( renderer ) );
  75. //
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. renderer.setAnimationLoop( render );
  85. }
  86. function render() {
  87. // If we are not presenting move the camera a little so the effect is visible
  88. if ( renderer.xr.isPresenting === false ) {
  89. const time = clock.getElapsedTime();
  90. sphere.rotation.y += 0.001;
  91. sphere.position.x = Math.sin( time ) * 0.2;
  92. sphere.position.z = Math.cos( time ) * 0.2;
  93. }
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>