webgl_panorama_dualfisheye.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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( 'wheel', onDocumentMouseWheel, false );
  83. //
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. }
  86. function onWindowResize() {
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function onDocumentMouseDown( event ) {
  92. event.preventDefault();
  93. isUserInteracting = true;
  94. onPointerDownPointerX = event.clientX;
  95. onPointerDownPointerY = event.clientY;
  96. onPointerDownLon = lon;
  97. onPointerDownLat = lat;
  98. }
  99. function onDocumentMouseMove( event ) {
  100. if ( isUserInteracting === true ) {
  101. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  102. lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
  103. }
  104. }
  105. function onDocumentMouseUp( event ) {
  106. isUserInteracting = false;
  107. }
  108. function onDocumentMouseWheel( event ) {
  109. distance += event.deltaY * 0.05;
  110. }
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. update();
  114. }
  115. function update() {
  116. if ( isUserInteracting === false ) {
  117. lon += 0.1;
  118. }
  119. lat = Math.max( - 85, Math.min( 85, lat ) );
  120. phi = THREE.Math.degToRad( 90 - lat );
  121. theta = THREE.Math.degToRad( lon - 180 );
  122. camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
  123. camera.position.y = distance * Math.cos( phi );
  124. camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
  125. camera.lookAt( scene.position );
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>