webxr_vr_panorama_depth.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. animate();
  29. function init() {
  30. const container = document.getElementById( 'container' );
  31. clock = new THREE.Clock();
  32. scene = new THREE.Scene();
  33. scene.background = new THREE.Color( 0x101010 );
  34. const light = new THREE.AmbientLight( 0xffffff, 3 );
  35. scene.add( light );
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  37. scene.add( camera );
  38. // Create the panoramic sphere geometery
  39. const panoSphereGeo = new THREE.SphereGeometry( 6, 256, 256 );
  40. // Create the panoramic sphere material
  41. const panoSphereMat = new THREE.MeshStandardMaterial( {
  42. side: THREE.BackSide,
  43. displacementScale: - 4.0
  44. } );
  45. // Create the panoramic sphere mesh
  46. sphere = new THREE.Mesh( panoSphereGeo, panoSphereMat );
  47. // Load and assign the texture and depth map
  48. const manager = new THREE.LoadingManager();
  49. const loader = new THREE.TextureLoader( manager );
  50. loader.load( './textures/kandao3.jpg', function ( texture ) {
  51. texture.colorSpace = THREE.SRGBColorSpace;
  52. texture.minFilter = THREE.NearestFilter;
  53. texture.generateMipmaps = false;
  54. sphere.material.map = texture;
  55. } );
  56. loader.load( './textures/kandao3_depthmap.jpg', function ( depth ) {
  57. depth.minFilter = THREE.NearestFilter;
  58. depth.generateMipmaps = false;
  59. sphere.material.displacementMap = depth;
  60. } );
  61. // On load complete add the panoramic sphere to the scene
  62. manager.onLoad = function () {
  63. scene.add( sphere );
  64. };
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  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. renderer.setAnimationLoop( render );
  82. }
  83. function render() {
  84. // If we are not presenting move the camera a little so the effect is visible
  85. if ( renderer.xr.isPresenting === false ) {
  86. const time = clock.getElapsedTime();
  87. sphere.rotation.y += 0.001;
  88. sphere.position.x = Math.sin( time ) * 0.2;
  89. sphere.position.z = Math.cos( time ) * 0.2;
  90. }
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>