webgl_panorama_equirectangular.html 4.8 KB
Newer Older
M
Mr.doob 已提交
1
<!DOCTYPE html>
2 3
<html lang="en">
	<head>
M
Mr.doob 已提交
4
		<title>three.js webgl - equirectangular panorama</title>
5
		<meta charset="utf-8">
6 7
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
			body {
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family:Monospace;
				font-size:13px;
				font-weight: bold;
				text-align:center;
			}

			a {
				color: #ffffff;
			}
		</style>
	</head>
	<body>

A
alteredq 已提交
32
		<div id="container"></div>
33
		<div id="info">
34
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - equirectangular panorama demo. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">Jón Ragnarsson</a>.<br />
35 36
			drag equirectangular texture into the page.
		</div>
37

W
WestLangley 已提交
38
		<script src="../build/three.js"></script>
39

M
Mr.doob 已提交
40
		<script>
41 42 43

			var camera, scene, renderer;

M
Mr.doob 已提交
44
			var isUserInteracting = false,
45 46 47 48 49 50 51 52 53 54 55 56 57 58
			onMouseDownMouseX = 0, onMouseDownMouseY = 0,
			lon = 0, onMouseDownLon = 0,
			lat = 0, onMouseDownLat = 0,
			phi = 0, theta = 0;

			init();
			animate();

			function init() {

				var container, mesh;

				container = document.getElementById( 'container' );

M
Mr.doob 已提交
59
				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
A
alteredq 已提交
60
				camera.target = new THREE.Vector3( 0, 0, 0 );
61 62

				scene = new THREE.Scene();
63

M
Mugen87 已提交
64 65
				var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
				// invert the geometry on the x-axis so that all of the faces point inward
66
				geometry.scale( - 1, 1, 1 );
67 68

				var material = new THREE.MeshBasicMaterial( {
69
					map: new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' )
70 71 72
				} );

				mesh = new THREE.Mesh( geometry, material );
73

74
				scene.add( mesh );
75 76

				renderer = new THREE.WebGLRenderer();
77
				renderer.setPixelRatio( window.devicePixelRatio );
78 79 80 81 82 83
				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 );
M
Mr.doob 已提交
84
				document.addEventListener( 'wheel', onDocumentMouseWheel, false );
85

A
alteredq 已提交
86 87
				//

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
				document.addEventListener( 'dragover', function ( event ) {

					event.preventDefault();
					event.dataTransfer.dropEffect = 'copy';

				}, false );

				document.addEventListener( 'dragenter', function ( event ) {

					document.body.style.opacity = 0.5;

				}, false );

				document.addEventListener( 'dragleave', function ( event ) {

					document.body.style.opacity = 1;

				}, false );

				document.addEventListener( 'drop', function ( event ) {

					event.preventDefault();

					var reader = new FileReader();
					reader.addEventListener( 'load', function ( event ) {

						material.map.image.src = event.target.result;
						material.map.needsUpdate = true;

					}, false );
					reader.readAsDataURL( event.dataTransfer.files[ 0 ] );

					document.body.style.opacity = 1;

				}, false );

				//

A
alteredq 已提交
126 127 128 129 130 131 132 133 134 135 136
				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

137 138 139 140 141 142 143 144
			}

			function onDocumentMouseDown( event ) {

				event.preventDefault();

				isUserInteracting = true;

145 146
				onMouseDownMouseX = event.clientX;
				onMouseDownMouseY = event.clientY;
147

148 149
				onMouseDownLon = lon;
				onMouseDownLat = lat;
150 151 152 153 154

			}

			function onDocumentMouseMove( event ) {

M
Mr.doob 已提交
155
				if ( isUserInteracting === true ) {
156

157 158
					lon = ( onMouseDownMouseX - event.clientX ) * 0.1 + onMouseDownLon;
					lat = ( event.clientY - onMouseDownMouseY ) * 0.1 + onMouseDownLat;
159 160

				}
M
Mr.doob 已提交
161

162 163 164 165 166 167 168 169 170 171
			}

			function onDocumentMouseUp( event ) {

				isUserInteracting = false;

			}

			function onDocumentMouseWheel( event ) {

W
WestLangley 已提交
172 173 174 175
				var fov = camera.fov + event.deltaY * 0.05;

				camera.fov = THREE.Math.clamp( fov, 10, 75 );

M
Mr.doob 已提交
176
				camera.updateProjectionMatrix();
177 178 179 180 181 182

			}

			function animate() {

				requestAnimationFrame( animate );
M
Mr.doob 已提交
183
				update();
184 185 186

			}

M
Mr.doob 已提交
187 188 189 190 191 192 193
			function update() {

				if ( isUserInteracting === false ) {

					lon += 0.1;

				}
194 195

				lat = Math.max( - 85, Math.min( 85, lat ) );
196 197
				phi = THREE.Math.degToRad( 90 - lat );
				theta = THREE.Math.degToRad( lon );
198

A
alteredq 已提交
199 200 201 202 203
				camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
				camera.target.y = 500 * Math.cos( phi );
				camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );

				camera.lookAt( camera.target );
204 205 206

				/*
				// distortion
M
Mr.doob 已提交
207
				camera.position.copy( camera.target ).negate();
208 209 210 211 212 213 214 215 216
				*/

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>