three.js 全景看房

在这里插入图片描述

代码参考了官方的
https://threejs.org

<!DOCTYPE html>
<html>
	<head>
		<title>全景看房</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000000;
				margin: 0;
				cursor: move;
				overflow: hidden;
			}

			a {
				color: #ffffff;
			}

			#info {
				position: absolute;
				width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				font-weight: bold;
				text-align: center;
				z-index: 1;
			}
		</style>
	</head>
	<body>
		<!-- 引入本地js -->
		<!-- Three.js-master包下载 http://www.yanhuangxueyuan.com/links.html -->
		<script src="js/three.js"></script>
		<script src="js/CSS3DRenderer.js"></script>

		<script>
			var camera, scene, renderer;
			var geometry, material, mesh;
			var target = new THREE.Vector3();

			var lon = 90, lat = 0;
			var phi = 0, theta = 0;

			var touchX, touchY;

			init();
			animate();

			function init() {
				//创建相机
				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
				//创建场景
				scene = new THREE.Scene();
				
				//图片数组
				var sides = [
					{ //正面图
						url: 'picture/front.jpg',
						position: [ -512, 0, 0 ], //位置
						rotation: [ 0, Math.PI / 2, 0 ]  //旋转角度
					},
					{ //背面图
						url: 'picture/back.jpg',
						position: [ 512, 0, 0 ],
						rotation: [ 0, -Math.PI / 2, 0 ]
					},
					{ //顶部图
						url: 'picture/top.jpg',
						position: [ 0,  512, 0 ],
						rotation: [ Math.PI/2, 0, -Math.PI/2 ] //图片拼接不完美的时候调整这里
					},
					{ //底部图
						url: 'picture/bottom.jpg',
						position: [ 0, -512, 0 ],
						rotation: [ - Math.PI / 2, 0, Math.PI/2 ] //图片拼接不完美的时候调整这里
					},
					{ //右边图
						url: 'picture/right.jpg',
						position: [ 0, 0,  512 ],
						rotation: [ 0, Math.PI, 0 ]
					},
					{ //左边图
						url: 'picture/left.jpg',
						position: [ 0, 0, -512 ],
						rotation: [ 0, 0, 0 ]
					}
				];
				
				//循环贴图
				for ( var i = 0; i < sides.length; i ++ ) {					
					var side = sides[ i ];					
					var element = document.createElement( 'img' );
					element.width = 1026; 
					element.src = side.url;
					
					var object = new THREE.CSS3DObject( element );
					object.position.fromArray( side.position );
					object.rotation.fromArray( side.rotation );
					scene.add( object );					
				}
				
				//创建渲染器
				renderer = new THREE.CSS3DRenderer();
				renderer.setSize( window.innerWidth, window.innerHeight ); //设置渲染区域尺寸
				document.body.appendChild( renderer.domElement ); //使浏览器显示画面

				//监听鼠标键盘事件
				document.addEventListener( 'mousedown', onDocumentMouseDown, false );
				document.addEventListener( 'wheel', onDocumentMouseWheel, false );
				document.addEventListener( 'touchstart', onDocumentTouchStart, false );
				document.addEventListener( 'touchmove', onDocumentTouchMove, false );
				//双击事件
				document.addEventListener( 'dblclick', onMouseDbClick, 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();
				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
				document.addEventListener( 'mouseup', onDocumentMouseUp, false );
			}

			function onDocumentMouseMove( event ) {
				var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
				var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;

				lon -= movementX * 0.1;
				lat += movementY * 0.1;
			}

			function onDocumentMouseUp( event ) {
				document.removeEventListener( 'mousemove', onDocumentMouseMove );
				document.removeEventListener( 'mouseup', onDocumentMouseUp );
			}

			function onDocumentMouseWheel( event ) {
				var fov = camera.fov + event.deltaY * 0.05;
				camera.fov = THREE.Math.clamp( fov, 10, 75 );
				camera.updateProjectionMatrix();
			}

			function onDocumentTouchStart( event ) {
				event.preventDefault();
				var touch = event.touches[ 0 ];
				touchX = touch.screenX;
				touchY = touch.screenY;
			}

			function onDocumentTouchMove( event ) {
				event.preventDefault();
				var touch = event.touches[ 0 ];
				lon -= ( touch.screenX - touchX ) * 0.1;
				lat += ( touch.screenY - touchY ) * 0.1;
				touchX = touch.screenX;
				touchY = touch.screenY;
			}

			function onMouseDbClick( ) {
				let fullScreenElement = document.fullscreenElement;
				if (!fullScreenElement){
					//进入全屏
					renderer.domElement.requestFullscreen();
				} else {
					//退出全屏
					document.exitFullscreen();
				}
			}

			function animate() {
				requestAnimationFrame( animate );  //循环执行animate函数
				lon +=  0.1; //旋转速度
				lat = Math.max( - 85, Math.min( 85, lat ) );
				phi = THREE.Math.degToRad( 90 - lat );
				theta = THREE.Math.degToRad( lon );

				target.x = Math.sin( phi ) * Math.cos( theta );
				target.y = Math.cos( phi );
				target.z = Math.sin( phi ) * Math.sin( theta );

				camera.lookAt( target ); //设置相机方向

				renderer.render( scene, camera ); //执行渲染
			}
			
		</script>
	</body>
</html>

天空盒是一个包含了整个场景的立方体,它包含周围环境的6个图像。
在这里插入图片描述
所有图片的尺寸是 2048X2048
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

图片来自 :https://juejin.cn/post/7112696052884439070


2020-07-20 更新,以下是更简单的代码实现全景看房

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全景图</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
    <script src="./js/three.js"></script>
    <script src="./js/OrbitControls.js"></script>
</head>

<body>
	<script>
		//尺寸
	    const width = window.innerWidth;
	    const height = window.innerHeight;
		
		//场景
		const scene = new THREE.Scene();
		
		//相机
	    const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
	    camera.position.set(0,0,-200); //相机位置,想看图片的某个地方是在此处修改
	    camera.lookAt(scene.position);
		
		//轨道控制器
	    const controls = new THREE.OrbitControls(camera);
		
		//渲染器
	    const renderer = new THREE.WebGLRenderer();
	    renderer.setSize(width, height);
	    document.body.appendChild(renderer.domElement)
		
		create();
		render();
	
	    function create() {
	        let urls = [
	            './picture/left.jpg',
	            './picture/right.jpg',
	            './picture/top.jpg',
	            './picture/bottom.jpg',
	            './picture/front.jpg',
	            './picture/back.jpg'
	        ];
	        let cubeTexture = new THREE.CubeTextureLoader().load(urls);
	        scene.background = cubeTexture;
			
			//窗口缩放事件
			window.addEventListener( 'resize', onWindowResize, false );	
	    }
	
	    function render() {
	        renderer.render(scene, camera);
	        requestAnimationFrame(render);
	    }
		
		function onWindowResize() {
			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();
			renderer.setSize( window.innerWidth, window.innerHeight );
		}   
	
	</script>
</body>
</html>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值