2
0

webxr_vr_panorama_depth.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { VRButton } from './jsm/webxr/VRButton.js';
  18. let camera, scene, renderer, sphere, clock;
  19. init();
  20. animate();
  21. function init() {
  22. const container = document.getElementById( 'container' );
  23. clock = new THREE.Clock();
  24. scene = new THREE.Scene();
  25. scene.background = new THREE.Color( 0x101010 );
  26. const light = new THREE.AmbientLight( 0xffffff, 1 );
  27. scene.add( light );
  28. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  29. scene.add( camera );
  30. // Create the panoramic sphere geometery
  31. const panoSphereGeo = new THREE.SphereGeometry( 6, 256, 256 );
  32. // Create the panoramic sphere material
  33. const panoSphereMat = new THREE.MeshStandardMaterial( {
  34. side: THREE.BackSide,
  35. displacementScale: - 4.0
  36. } );
  37. // Create the panoramic sphere mesh
  38. sphere = new THREE.Mesh( panoSphereGeo, panoSphereMat );
  39. // Load and assign the texture and depth map
  40. const manager = new THREE.LoadingManager();
  41. const loader = new THREE.TextureLoader( manager );
  42. loader.load( './textures/kandao3.jpg', function ( texture ) {
  43. texture.minFilter = THREE.NearestFilter;
  44. texture.generateMipmaps = false;
  45. sphere.material.map = texture;
  46. } );
  47. loader.load( './textures/kandao3_depthmap.jpg', function ( depth ) {
  48. depth.minFilter = THREE.NearestFilter;
  49. depth.generateMipmaps = false;
  50. sphere.material.displacementMap = depth;
  51. } );
  52. // On load complete add the panoramic sphere to the scene
  53. manager.onLoad = function () {
  54. scene.add( sphere );
  55. };
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. renderer.xr.enabled = true;
  60. renderer.xr.setReferenceSpaceType( 'local' );
  61. container.appendChild( renderer.domElement );
  62. document.body.appendChild( VRButton.createButton( renderer ) );
  63. //
  64. window.addEventListener( 'resize', onWindowResize );
  65. }
  66. function onWindowResize() {
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. }
  71. function animate() {
  72. renderer.setAnimationLoop( render );
  73. }
  74. function render() {
  75. // If we are not presenting move the camera a little so the effect is visible
  76. if ( renderer.xr.isPresenting === false ) {
  77. const time = clock.getElapsedTime();
  78. sphere.rotation.y += 0.001;
  79. sphere.position.x = Math.sin( time ) * 0.2;
  80. sphere.position.z = Math.cos( time ) * 0.2;
  81. }
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>