webgl_panorama_equirectangular.html 3.5 KB

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