webgl_panorama_dualfisheye.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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" rel="noopener">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. // invert the geometry on the x-axis so that all of the faces point inward
  51. geometry.scale( - 1, 1, 1 );
  52. // Remap UVs
  53. var normals = geometry.attributes.normal.array;
  54. var uvs = geometry.attributes.uv.array;
  55. for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
  56. var x = normals[ i * 3 + 0 ];
  57. var y = normals[ i * 3 + 1 ];
  58. var z = normals[ i * 3 + 2 ];
  59. if ( i < l / 2 ) {
  60. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  61. uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
  62. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  63. } else {
  64. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  65. uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
  66. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  67. }
  68. }
  69. geometry.rotateZ( - Math.PI / 2 );
  70. //
  71. var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
  72. texture.format = THREE.RGBFormat;
  73. var material = new THREE.MeshBasicMaterial( { map: texture } );
  74. mesh = new THREE.Mesh( geometry, material );
  75. scene.add( mesh );
  76. renderer = new THREE.WebGLRenderer();
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  81. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  82. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  83. document.addEventListener( 'wheel', 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. distance += event.deltaY * 0.05;
  111. distance = THREE.Math.clamp( distance, 400, 1000 );
  112. }
  113. function animate() {
  114. requestAnimationFrame( animate );
  115. update();
  116. }
  117. function update() {
  118. if ( isUserInteracting === false ) {
  119. lon += 0.1;
  120. }
  121. lat = Math.max( - 85, Math.min( 85, lat ) );
  122. phi = THREE.Math.degToRad( 90 - lat );
  123. theta = THREE.Math.degToRad( lon - 180 );
  124. camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
  125. camera.position.y = distance * Math.cos( phi );
  126. camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
  127. camera.lookAt( scene.position );
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>