webgl_panorama_equirectangular.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - equirectangular panorama demo</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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family:Monospace;
  19. font-size:13px;
  20. font-weight: bold;
  21. text-align:center;
  22. }
  23. a {
  24. color: #ffffff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js webgl</a> - equirectangular panorama demo. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank">Jón Ragnarsson</a>.</div>
  31. <script src="../build/Three.js"></script>
  32. <script src="js/RequestAnimationFrame.js"></script>
  33. <script>
  34. var camera, scene, renderer;
  35. var fov = 70,
  36. texture_placeholder,
  37. isUserInteracting = false,
  38. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  39. lon = 0, onMouseDownLon = 0,
  40. lat = 0, onMouseDownLat = 0,
  41. phi = 0, theta = 0;
  42. init();
  43. animate();
  44. function init() {
  45. var container, mesh;
  46. container = document.getElementById( 'container' );
  47. camera = new THREE.Camera( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  48. scene = new THREE.Scene();
  49. mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 60, 40 ), new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/2294472375_24a3b8ef46_o.jpg' ) } ) );
  50. mesh.scale.x = -1;
  51. scene.add( mesh );
  52. renderer = new THREE.WebGLRenderer();
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. container.appendChild( renderer.domElement );
  55. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  56. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  57. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  58. document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
  59. document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
  60. }
  61. function onDocumentMouseDown( event ) {
  62. event.preventDefault();
  63. isUserInteracting = true;
  64. onPointerDownPointerX = event.clientX;
  65. onPointerDownPointerY = event.clientY;
  66. onPointerDownLon = lon;
  67. onPointerDownLat = lat;
  68. }
  69. function onDocumentMouseMove( event ) {
  70. if ( isUserInteracting ) {
  71. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  72. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  73. }
  74. }
  75. function onDocumentMouseUp( event ) {
  76. isUserInteracting = false;
  77. }
  78. function onDocumentMouseWheel( event ) {
  79. // WebKit
  80. if ( event.wheelDeltaY ) {
  81. fov -= event.wheelDeltaY * 0.05;
  82. // Opera / Explorer 9
  83. } else if ( event.wheelDelta ) {
  84. fov -= event.wheelDelta * 0.05;
  85. // Firefox
  86. } else if ( event.detail ) {
  87. fov += event.detail * 1.0;
  88. }
  89. camera.projectionMatrix = THREE.Matrix4.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  90. render();
  91. }
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. render();
  95. }
  96. function render() {
  97. lat = Math.max( - 85, Math.min( 85, lat ) );
  98. phi = ( 90 - lat ) * Math.PI / 180;
  99. theta = lon * Math.PI / 180;
  100. camera.target.position.x = 500 * Math.sin( phi ) * Math.cos( theta );
  101. camera.target.position.y = 500 * Math.cos( phi );
  102. camera.target.position.z = 500 * Math.sin( phi ) * Math.sin( theta );
  103. /*
  104. // distortion
  105. camera.position.x = - camera.target.position.x;
  106. camera.position.y = - camera.target.position.y;
  107. camera.position.z = - camera.target.position.z;
  108. */
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>