webxr_vr_panorama_depth.html 3.5 KB

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