webvr_panorama_depth.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - 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> webvr - 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 { WEBVR } from './jsm/vr/WebVR.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( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  32. var cameraRig = new THREE.Object3D();
  33. cameraRig.position.y = - 1.6;
  34. cameraRig.add( camera );
  35. scene.add( cameraRig );
  36. // Create the panoramic sphere geometery
  37. var panoSphereGeo = new THREE.SphereBufferGeometry( 6, 256, 256 );
  38. // Create the panoramic sphere material
  39. var panoSphereMat = new THREE.MeshStandardMaterial( {
  40. side: THREE.BackSide,
  41. displacementScale: - 4.0
  42. } );
  43. // Create the panoramic sphere mesh
  44. sphere = new THREE.Mesh( panoSphereGeo, panoSphereMat );
  45. // Load and assign the texture and depth map
  46. var manager = new THREE.LoadingManager();
  47. var loader = new THREE.TextureLoader( manager );
  48. loader.load( './textures/kandao3.jpg', function ( texture ) {
  49. texture.minFilter = THREE.NearestFilter;
  50. texture.format = THREE.RGBFormat;
  51. texture.generateMipmaps = false;
  52. sphere.material.map = texture;
  53. } );
  54. loader.load( './textures/kandao3_depthmap.jpg', function ( depth ) {
  55. depth.minFilter = THREE.NearestFilter;
  56. depth.format = THREE.RGBFormat;
  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.vr.enabled = true;
  68. container.appendChild( renderer.domElement );
  69. document.body.appendChild( WEBVR.createButton( renderer ) );
  70. //
  71. window.addEventListener( 'resize', onWindowResize, false );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function animate() {
  79. renderer.setAnimationLoop( render );
  80. }
  81. function render() {
  82. // If we are not presenting move the camera a little so the effect is visible
  83. if ( ! renderer.vr.isPresenting() ) {
  84. var time = clock.getElapsedTime();
  85. sphere.rotation.y += 0.001;
  86. sphere.position.x = Math.sin( time ) * 0.2;
  87. sphere.position.z = Math.cos( time ) * 0.2;
  88. }
  89. renderer.render( scene, camera );
  90. }
  91. </script>
  92. </body>
  93. </html>