webgl_panorama_equirectangular.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - equirectangular panorama</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  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 webgl</a> - equirectangular panorama demo. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">Jón Ragnarsson</a>.<br />
  13. drag equirectangular texture into the page.
  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. let camera, scene, renderer;
  26. let isUserInteracting = false,
  27. onPointerDownMouseX = 0, onPointerDownMouseY = 0,
  28. lon = 0, onPointerDownLon = 0,
  29. lat = 0, onPointerDownLat = 0,
  30. phi = 0, theta = 0;
  31. init();
  32. function init() {
  33. const container = document.getElementById( 'container' );
  34. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  35. scene = new THREE.Scene();
  36. const geometry = new THREE.SphereGeometry( 500, 60, 40 );
  37. // invert the geometry on the x-axis so that all of the faces point inward
  38. geometry.scale( - 1, 1, 1 );
  39. const texture = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  40. texture.colorSpace = THREE.SRGBColorSpace;
  41. const material = new THREE.MeshBasicMaterial( { map: texture } );
  42. const mesh = new THREE.Mesh( geometry, material );
  43. scene.add( mesh );
  44. renderer = new THREE.WebGLRenderer();
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.setAnimationLoop( animate );
  48. container.appendChild( renderer.domElement );
  49. container.style.touchAction = 'none';
  50. container.addEventListener( 'pointerdown', onPointerDown );
  51. document.addEventListener( 'wheel', onDocumentMouseWheel );
  52. window.addEventListener( 'resize', onWindowResize );
  53. }
  54. function onWindowResize() {
  55. camera.aspect = window.innerWidth / window.innerHeight;
  56. camera.updateProjectionMatrix();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. }
  59. function onPointerDown( event ) {
  60. if ( event.isPrimary === false ) return;
  61. isUserInteracting = true;
  62. onPointerDownMouseX = event.clientX;
  63. onPointerDownMouseY = event.clientY;
  64. onPointerDownLon = lon;
  65. onPointerDownLat = lat;
  66. document.addEventListener( 'pointermove', onPointerMove );
  67. document.addEventListener( 'pointerup', onPointerUp );
  68. }
  69. function onPointerMove( event ) {
  70. if ( event.isPrimary === false ) return;
  71. lon = ( onPointerDownMouseX - event.clientX ) * 0.1 + onPointerDownLon;
  72. lat = ( event.clientY - onPointerDownMouseY ) * 0.1 + onPointerDownLat;
  73. }
  74. function onPointerUp() {
  75. if ( event.isPrimary === false ) return;
  76. isUserInteracting = false;
  77. document.removeEventListener( 'pointermove', onPointerMove );
  78. document.removeEventListener( 'pointerup', onPointerUp );
  79. }
  80. function onDocumentMouseWheel( event ) {
  81. const fov = camera.fov + event.deltaY * 0.05;
  82. camera.fov = THREE.MathUtils.clamp( fov, 10, 75 );
  83. camera.updateProjectionMatrix();
  84. }
  85. function animate() {
  86. if ( isUserInteracting === false ) {
  87. lon += 0.1;
  88. }
  89. lat = Math.max( - 85, Math.min( 85, lat ) );
  90. phi = THREE.MathUtils.degToRad( 90 - lat );
  91. theta = THREE.MathUtils.degToRad( lon );
  92. const x = 500 * Math.sin( phi ) * Math.cos( theta );
  93. const y = 500 * Math.cos( phi );
  94. const z = 500 * Math.sin( phi ) * Math.sin( theta );
  95. camera.lookAt( x, y, z );
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>
粤ICP备19079148号