webgl_panorama_dualfisheye.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - dualfisheye panorama
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. var camera, scene, renderer;
  26. var isUserInteracting = false,
  27. lon = 0, lat = 0,
  28. phi = 0, theta = 0,
  29. distance = 500,
  30. onPointerDownPointerX = 0,
  31. onPointerDownPointerY = 0,
  32. onPointerDownLon = 0,
  33. onPointerDownLat = 0;
  34. init();
  35. animate();
  36. function init() {
  37. var container, mesh;
  38. container = document.getElementById( 'container' );
  39. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  40. scene = new THREE.Scene();
  41. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 ).toNonIndexed();
  42. // invert the geometry on the x-axis so that all of the faces point inward
  43. geometry.scale( - 1, 1, 1 );
  44. // Remap UVs
  45. var normals = geometry.attributes.normal.array;
  46. var uvs = geometry.attributes.uv.array;
  47. for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
  48. var x = normals[ i * 3 + 0 ];
  49. var y = normals[ i * 3 + 1 ];
  50. var z = normals[ i * 3 + 2 ];
  51. if ( i < l / 2 ) {
  52. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  53. uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
  54. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  55. } else {
  56. var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
  57. uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
  58. uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
  59. }
  60. }
  61. geometry.rotateZ( - Math.PI / 2 );
  62. //
  63. var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
  64. texture.minFilter = THREE.LinearFilter;
  65. texture.format = THREE.RGBFormat;
  66. var material = new THREE.MeshBasicMaterial( { map: texture } );
  67. mesh = new THREE.Mesh( geometry, material );
  68. scene.add( mesh );
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. container.appendChild( renderer.domElement );
  73. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  74. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  75. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  76. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function onDocumentMouseDown( event ) {
  86. event.preventDefault();
  87. isUserInteracting = true;
  88. onPointerDownPointerX = event.clientX;
  89. onPointerDownPointerY = event.clientY;
  90. onPointerDownLon = lon;
  91. onPointerDownLat = lat;
  92. }
  93. function onDocumentMouseMove( event ) {
  94. if ( isUserInteracting === true ) {
  95. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  96. lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
  97. }
  98. }
  99. function onDocumentMouseUp() {
  100. isUserInteracting = false;
  101. }
  102. function onDocumentMouseWheel( event ) {
  103. distance += event.deltaY * 0.05;
  104. distance = THREE.MathUtils.clamp( distance, 400, 1000 );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. update();
  109. }
  110. function update() {
  111. if ( isUserInteracting === false ) {
  112. lon += 0.1;
  113. }
  114. lat = Math.max( - 85, Math.min( 85, lat ) );
  115. phi = THREE.MathUtils.degToRad( 90 - lat );
  116. theta = THREE.MathUtils.degToRad( lon - 180 );
  117. camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
  118. camera.position.y = distance * Math.cos( phi );
  119. camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
  120. camera.lookAt( scene.position );
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>