webgl_panorama_dualfisheye.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - dual fisheye 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. <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: #000000;
  17. padding: 5px;
  18. font-family:Monospace;
  19. font-size:13px;
  20. font-weight: bold;
  21. text-align:center;
  22. }
  23. a {
  24. color: #0000ff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank">three.js webgl</a> - dualfisheye panorama.
  32. </div>
  33. <script src="../build/three.js"></script>
  34. <script>
  35. var camera, scene, renderer;
  36. var isUserInteracting = false,
  37. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  38. lon = 0, onMouseDownLon = 0,
  39. lat = 0, onMouseDownLat = 0,
  40. phi = 0, theta = 0,
  41. distance = 500;
  42. init();
  43. animate();
  44. function init() {
  45. var container, mesh;
  46. container = document.getElementById( 'container' );
  47. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  48. scene = new THREE.Scene();
  49. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 ).toNonIndexed();
  50. geometry.scale( - 1, 1, 1 );
  51. // Remap UVs
  52. var normals = geometry.attributes.normal.array;
  53. var uvs = geometry.attributes.uv.array;
  54. for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
  55. var x = normals[ i * 3 + 0 ];
  56. var y = normals[ i * 3 + 1 ];
  57. var z = normals[ i * 3 + 2 ];
  58. if ( i < l / 2 ) {
  59. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  60. uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
  61. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  62. } else {
  63. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  64. uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
  65. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  66. }
  67. }
  68. geometry.rotateZ( - Math.PI / 2 );
  69. //
  70. var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
  71. texture.format = THREE.RGBFormat;
  72. var material = new THREE.MeshBasicMaterial( { map: texture } );
  73. mesh = new THREE.Mesh( geometry, material );
  74. scene.add( mesh );
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  80. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  81. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  82. document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
  83. document.addEventListener( 'MozMousePixelScroll', onDocumentMouseWheel, false );
  84. //
  85. window.addEventListener( 'resize', onWindowResize, false );
  86. }
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function onDocumentMouseDown( event ) {
  93. event.preventDefault();
  94. isUserInteracting = true;
  95. onPointerDownPointerX = event.clientX;
  96. onPointerDownPointerY = event.clientY;
  97. onPointerDownLon = lon;
  98. onPointerDownLat = lat;
  99. }
  100. function onDocumentMouseMove( event ) {
  101. if ( isUserInteracting === true ) {
  102. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  103. lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
  104. }
  105. }
  106. function onDocumentMouseUp( event ) {
  107. isUserInteracting = false;
  108. }
  109. function onDocumentMouseWheel( event ) {
  110. // WebKit
  111. if ( event.wheelDeltaY ) {
  112. distance -= event.wheelDeltaY * 0.05;
  113. // Opera / Explorer 9
  114. } else if ( event.wheelDelta ) {
  115. distance -= event.wheelDelta * 0.05;
  116. // Firefox
  117. } else if ( event.detail ) {
  118. distance += event.detail * 1.0;
  119. }
  120. }
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. update();
  124. }
  125. function update() {
  126. if ( isUserInteracting === false ) {
  127. lon += 0.1;
  128. }
  129. lat = Math.max( - 85, Math.min( 85, lat ) );
  130. phi = THREE.Math.degToRad( 90 - lat );
  131. theta = THREE.Math.degToRad( lon - 180 );
  132. camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
  133. camera.position.y = distance * Math.cos( phi );
  134. camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
  135. camera.lookAt( scene.position );
  136. renderer.render( scene, camera );
  137. }
  138. </script>
  139. </body>
  140. </html>