webxr_vr_panorama_depth.html 3.8 KB

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