webgl_panorama_equirectangular.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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="module">
  16. import * as THREE from '../build/three.module.js';
  17. var camera, scene, renderer;
  18. var isUserInteracting = false,
  19. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  20. lon = 0, onMouseDownLon = 0,
  21. lat = 0, onMouseDownLat = 0,
  22. phi = 0, theta = 0;
  23. init();
  24. animate();
  25. function init() {
  26. var container, mesh;
  27. container = document.getElementById( 'container' );
  28. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  29. camera.target = new THREE.Vector3( 0, 0, 0 );
  30. scene = new THREE.Scene();
  31. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
  32. // invert the geometry on the x-axis so that all of the faces point inward
  33. geometry.scale( - 1, 1, 1 );
  34. var texture = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  35. var material = new THREE.MeshBasicMaterial( { map: texture } );
  36. mesh = new THREE.Mesh( geometry, material );
  37. scene.add( mesh );
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. container.appendChild( renderer.domElement );
  42. document.addEventListener( 'mousedown', onPointerStart, false );
  43. document.addEventListener( 'mousemove', onPointerMove, false );
  44. document.addEventListener( 'mouseup', onPointerUp, false );
  45. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  46. document.addEventListener( 'touchstart', onPointerStart, false );
  47. document.addEventListener( 'touchmove', onPointerMove, false );
  48. document.addEventListener( 'touchend', onPointerUp, false );
  49. //
  50. document.addEventListener( 'dragover', function ( event ) {
  51. event.preventDefault();
  52. event.dataTransfer.dropEffect = 'copy';
  53. }, false );
  54. document.addEventListener( 'dragenter', function () {
  55. document.body.style.opacity = 0.5;
  56. }, false );
  57. document.addEventListener( 'dragleave', function () {
  58. document.body.style.opacity = 1;
  59. }, false );
  60. document.addEventListener( 'drop', function ( event ) {
  61. event.preventDefault();
  62. var reader = new FileReader();
  63. reader.addEventListener( 'load', function ( event ) {
  64. material.map.image.src = event.target.result;
  65. material.map.needsUpdate = true;
  66. }, false );
  67. reader.readAsDataURL( event.dataTransfer.files[ 0 ] );
  68. document.body.style.opacity = 1;
  69. }, false );
  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 onPointerStart( event ) {
  79. isUserInteracting = true;
  80. var clientX = event.clientX || event.touches[ 0 ].clientX;
  81. var clientY = event.clientY || event.touches[ 0 ].clientY;
  82. onMouseDownMouseX = clientX;
  83. onMouseDownMouseY = clientY;
  84. onMouseDownLon = lon;
  85. onMouseDownLat = lat;
  86. }
  87. function onPointerMove( event ) {
  88. if ( isUserInteracting === true ) {
  89. var clientX = event.clientX || event.touches[ 0 ].clientX;
  90. var clientY = event.clientY || event.touches[ 0 ].clientY;
  91. lon = ( onMouseDownMouseX - clientX ) * 0.1 + onMouseDownLon;
  92. lat = ( clientY - onMouseDownMouseY ) * 0.1 + onMouseDownLat;
  93. }
  94. }
  95. function onPointerUp() {
  96. isUserInteracting = false;
  97. }
  98. function onDocumentMouseWheel( event ) {
  99. var fov = camera.fov + event.deltaY * 0.05;
  100. camera.fov = THREE.MathUtils.clamp( fov, 10, 75 );
  101. camera.updateProjectionMatrix();
  102. }
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. update();
  106. }
  107. function update() {
  108. if ( isUserInteracting === false ) {
  109. lon += 0.1;
  110. }
  111. lat = Math.max( - 85, Math.min( 85, lat ) );
  112. phi = THREE.MathUtils.degToRad( 90 - lat );
  113. theta = THREE.MathUtils.degToRad( lon );
  114. camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
  115. camera.target.y = 500 * Math.cos( phi );
  116. camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );
  117. camera.lookAt( camera.target );
  118. /*
  119. // distortion
  120. camera.position.copy( camera.target ).negate();
  121. */
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>