webxr_vr_panorama_depth.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. <meta property="og:title" content="three.js vr - panorama with depth">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webxr_vr_panorama_depth.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webxr_vr_panorama_depth.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - panorama with depth<br />
  17. 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>.
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { VRButton } from 'three/addons/webxr/VRButton.js';
  30. let camera, scene, renderer, sphere, timer;
  31. init();
  32. function init() {
  33. const container = document.getElementById( 'container' );
  34. timer = new THREE.Timer();
  35. timer.connect( document );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x101010 );
  38. const light = new THREE.AmbientLight( 0xffffff, 3 );
  39. scene.add( light );
  40. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  41. scene.add( camera );
  42. // Create the panoramic sphere geometry
  43. const panoSphereGeo = new THREE.SphereGeometry( 6, 256, 256 );
  44. // Create the panoramic sphere material
  45. const panoSphereMat = new THREE.MeshStandardMaterial( {
  46. side: THREE.BackSide,
  47. displacementScale: - 4.0
  48. } );
  49. // Create the panoramic sphere mesh
  50. sphere = new THREE.Mesh( panoSphereGeo, panoSphereMat );
  51. // Load and assign the texture and depth map
  52. const manager = new THREE.LoadingManager();
  53. const loader = new THREE.TextureLoader( manager );
  54. loader.load( './textures/kandao3.jpg', function ( texture ) {
  55. texture.colorSpace = THREE.SRGBColorSpace;
  56. texture.minFilter = THREE.NearestFilter;
  57. texture.generateMipmaps = false;
  58. sphere.material.map = texture;
  59. } );
  60. loader.load( './textures/kandao3_depthmap.jpg', function ( depth ) {
  61. depth.minFilter = THREE.NearestFilter;
  62. depth.generateMipmaps = false;
  63. sphere.material.displacementMap = depth;
  64. } );
  65. // On load complete add the panoramic sphere to the scene
  66. manager.onLoad = function () {
  67. scene.add( sphere );
  68. };
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. renderer.xr.enabled = true;
  74. renderer.xr.setReferenceSpaceType( 'local' );
  75. container.appendChild( renderer.domElement );
  76. document.body.appendChild( VRButton.createButton( renderer ) );
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. timer.update();
  87. // If we are not presenting move the camera a little so the effect is visible
  88. if ( renderer.xr.isPresenting === false ) {
  89. const time = timer.getElapsed();
  90. sphere.rotation.y += 0.001;
  91. sphere.position.x = Math.sin( time ) * 0.2;
  92. sphere.position.z = Math.cos( time ) * 0.2;
  93. }
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>
粤ICP备19079148号