123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - dual fisheye panorama</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #fff;
- color: #444;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - dualfisheye panorama
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var camera, scene, renderer;
- var isUserInteracting = false,
- lon = 0, lat = 0,
- phi = 0, theta = 0,
- distance = 500,
- onPointerDownPointerX = 0,
- onPointerDownPointerY = 0,
- onPointerDownLon = 0,
- onPointerDownLat = 0;
- init();
- animate();
- function init() {
- var container, mesh;
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
- scene = new THREE.Scene();
- var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 ).toNonIndexed();
- // invert the geometry on the x-axis so that all of the faces point inward
- geometry.scale( - 1, 1, 1 );
- // Remap UVs
- var normals = geometry.attributes.normal.array;
- var uvs = geometry.attributes.uv.array;
- for ( var i = 0, l = normals.length / 3; i < l; i ++ ) {
- var x = normals[ i * 3 + 0 ];
- var y = normals[ i * 3 + 1 ];
- var z = normals[ i * 3 + 2 ];
- if ( i < l / 2 ) {
- var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
- uvs[ i * 2 + 0 ] = x * ( 404 / 1920 ) * correction + ( 447 / 1920 );
- uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
- } else {
- var correction = ( x == 0 && z == 0 ) ? 1 : ( Math.acos( - y ) / Math.sqrt( x * x + z * z ) ) * ( 2 / Math.PI );
- uvs[ i * 2 + 0 ] = - x * ( 404 / 1920 ) * correction + ( 1460 / 1920 );
- uvs[ i * 2 + 1 ] = z * ( 404 / 1080 ) * correction + ( 582 / 1080 );
- }
- }
- geometry.rotateZ( - Math.PI / 2 );
- //
- var texture = new THREE.TextureLoader().load( 'textures/ricoh_theta_s.jpg' );
- texture.minFilter = THREE.LinearFilter;
- texture.format = THREE.RGBFormat;
- var material = new THREE.MeshBasicMaterial( { map: texture } );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- document.addEventListener( 'wheel', onDocumentMouseWheel, false );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- isUserInteracting = true;
- onPointerDownPointerX = event.clientX;
- onPointerDownPointerY = event.clientY;
- onPointerDownLon = lon;
- onPointerDownLat = lat;
- }
- function onDocumentMouseMove( event ) {
- if ( isUserInteracting === true ) {
- lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
- lat = ( onPointerDownPointerY - event.clientY ) * 0.1 + onPointerDownLat;
- }
- }
- function onDocumentMouseUp() {
- isUserInteracting = false;
- }
- function onDocumentMouseWheel( event ) {
- distance += event.deltaY * 0.05;
- distance = THREE.MathUtils.clamp( distance, 400, 1000 );
- }
- function animate() {
- requestAnimationFrame( animate );
- update();
- }
- function update() {
- if ( isUserInteracting === false ) {
- lon += 0.1;
- }
- lat = Math.max( - 85, Math.min( 85, lat ) );
- phi = THREE.MathUtils.degToRad( 90 - lat );
- theta = THREE.MathUtils.degToRad( lon - 180 );
- camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
- camera.position.y = distance * Math.cos( phi );
- camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
- camera.lookAt( scene.position );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|