webxr_vr_panorama_depth.html 3.5 KB

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