webgl_panorama_dualfisheye.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. lon = 0, lat = 0,
  38. phi = 0, theta = 0,
  39. distance = 500,
  40. onPointerDownPointerX = 0,
  41. onPointerDownPointerY = 0,
  42. onPointerDownLon = 0,
  43. onPointerDownLat = 0;
  44. init();
  45. animate();
  46. function init() {
  47. var container, mesh;
  48. container = document.getElementById( 'container' );
  49. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  50. scene = new THREE.Scene();
  51. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 ).toNonIndexed();
  52. // invert the geometry on the x-axis so that all of the faces point inward
  53. geometry.scale( - 1, 1, 1 );
  54. // Remap UVs
  55. var normals = geometry.attributes.normal.array;
  56. var uvs = geometry.attributes.uv.array;
  57. for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
  58. var x = normals[ i * 3 + 0 ];
  59. var y = normals[ i * 3 + 1 ];
  60. var z = normals[ i * 3 + 2 ];
  61. if ( i < l / 2 ) {
  62. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  63. uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
  64. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  65. } else {
  66. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  67. uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
  68. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  69. }
  70. }
  71. geometry.rotateZ( - Math.PI / 2 );
  72. //
  73. var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
  74. texture.minFilter = THREE.LinearFilter;
  75. texture.format = THREE.RGBFormat;
  76. var material = new THREE.MeshBasicMaterial( { map: texture } );
  77. mesh = new THREE.Mesh( geometry, material );
  78. scene.add( mesh );
  79. renderer = new THREE.WebGLRenderer();
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  86. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  87. //
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function onDocumentMouseDown( event ) {
  96. event.preventDefault();
  97. isUserInteracting = true;
  98. onPointerDownPointerX = event.clientX;
  99. onPointerDownPointerY = event.clientY;
  100. onPointerDownLon = lon;
  101. onPointerDownLat = lat;
  102. }
  103. function onDocumentMouseMove( event ) {
  104. if ( isUserInteracting === true ) {
  105. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  106. lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
  107. }
  108. }
  109. function onDocumentMouseUp() {
  110. isUserInteracting = false;
  111. }
  112. function onDocumentMouseWheel( event ) {
  113. distance += event.deltaY * 0.05;
  114. distance = THREE.Math.clamp( distance, 400, 1000 );
  115. }
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. update();
  119. }
  120. function update() {
  121. if ( isUserInteracting === false ) {
  122. lon += 0.1;
  123. }
  124. lat = Math.max( - 85, Math.min( 85, lat ) );
  125. phi = THREE.Math.degToRad( 90 - lat );
  126. theta = THREE.Math.degToRad( lon - 180 );
  127. camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
  128. camera.position.y = distance * Math.cos( phi );
  129. camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
  130. camera.lookAt( scene.position );
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>