three.js 3D全景图之一

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>erath - three.js</title>
	<script src="js/three.min.js"></script>
	<script src="js/TrackballControls.js"></script>
	<script src="js/jquery.min.js"></script>
</head>
<style>
	* {
		padding: 0px;
		margin: 0px;
	}
		html,body,.main {
			width: 100%;
			height: 100%;
			overflow: hidden;
		}
		.main {
			position: relative;
		}
			.div {
				float: left;
				cursor: move;
				width:100%;
				height:100%
			}
</style>
<body>
	<div class="main">
		<div class="div div1"></div>
	</div>
</body>
</html>
<script>
	function Three(className) {
		this.off;
		this.width = $(className).width();
		this.height = $(className).height();
		this.renderer =  new THREE.WebGLRenderer({
            antialias : true   //开启锯齿,默认是false
        });
		this.renderer.setSize(this.width, this.height); // 给渲染区域设置宽高
	    this.renderer.setClearColor("white"); // 设置背景色
	    $(className).append(this.renderer.domElement); 
	}
	Three.prototype = {
		init:function(url) {
			var that = this;
			this.scence = new THREE.Scene();  // 创建舞台
			
			// 设置视角及参数
			this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 10000);
			this.camera.position.set(0,0,200);
            this.camera.lookAt(new THREE.Vector3(0, 0, 0));
	        this.controls = new THREE.TrackballControls(this.camera, this.renderer.domElement);
            // this.controls.minDistance = 200;
            this.controls.rotateSpeed = 1.0;
			this.controls.zoomSpeed = 1.2;
			this.controls.panSpeed = 0.8;
			this.controls.noZoom = true;
			this.controls.noPan = true;
			this.controls.staticMoving = true;
			this.controls.dynamicDampingFactor = 0.3;  

			// 设置灯光及参数
            this.light = new THREE.AmbientLight(0xDDDDDD);
            this.light.position.set(100, 100, 200);
            this.scence.add(this.light);

            // 创建角色
			var circle =  new THREE.SphereGeometry(900,50,50);
            var texture = new THREE.TextureLoader();
            
			var material = new THREE.MeshBasicMaterial();
			// 给circle添加背景图片
			material.map = texture.load("images/"+url,function(){
            	that.renderer.render(that.scence, that.camera);
			});
            that.mesh = new THREE.Mesh(circle,material);
            that.mesh.position.set(0,0,-260);
            that.mesh.scale.x = -1;
    		that.scence.add(that.mesh);
		},
		animate:function() {
			this.off = requestAnimationFrame(this.animate.bind(this));
				this.mesh.rotation.y += 0.003;
				this.controls.target.copy( this.mesh.position );
				this.controls.update();
				this.renderer.render(this.scence, this.camera);
		},
		
		start:function() {
			this.animate();
		}
	}
	
	function Dothree() {
      var three=new Three($(".div"));
      three.init("164409shep99yc3gm01c99.jpg")
      three.start()
	}
	Dothree();
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是两种使用three.js实现全景图的例子: 1. 使用WebGL渲染全景图 ```html <!DOCTYPE html> <html> <head> <title>WebGL Panorama Cube</title> <meta charset="utf-8"> <style> body { margin: 0; overflow: hidden; } canvas { width: 100%; height: 100%; } </style> </head> <body> <script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.min.js"></script> <script> var scene, camera, renderer; var geometry, material, mesh; init(); animate(); function init() { scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000); camera.position.z = 0.01; geometry = new THREE.BoxGeometry(1, 1, 1); var urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ]; material = new THREE.MeshBasicMaterial({ map: new THREE.CubeTextureLoader().load(urls), side: THREE.BackSide }); mesh = new THREE.Mesh(geometry, material); scene.add(mesh); renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); } function animate() { requestAnimationFrame(animate); mesh.rotation.x += 0.01; mesh.rotation.y += 0.02; renderer.render(scene, camera); } </script> </body> </html> ``` 2. 使用CSS3D渲染全景图 ```html <!DOCTYPE html> <html> <head> <title>CSS3D Panorama</title> <meta charset="utf-8"> <style> body { margin: 0; overflow: hidden; } #container { width: 100%; height: 100%; position: absolute; -webkit-perspective: 1000px; perspective: 1000px; } #pano { width: 100%; height: 100%; position: absolute; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } #pano img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; -webkit-transform: translateZ(-1px) scale(-1, 1); transform: translateZ(-1px) scale(-1, 1); } </style> </head> <body> <div id="container"> <div id="pano"> <img src="pano.jpg"> </div> </div> <script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.min.js"></script> <script> var camera, scene, renderer; var isUserInteracting = false, 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'); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1100); camera.target = new THREE.Vector3(0, 0, 0); scene = new THREE.Scene(); var geometry = new THREE.SphereGeometry(500, 60, 40); geometry.scale(-1, 1, 1); var material = new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('pano.jpg') }); mesh = new THREE.Mesh(geometry, material); scene.add(mesh); renderer = new THREE.CSS3DRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); renderer.domElement.style.position = 'absolute'; renderer.domElement.style.top = 0; 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 = (event.clientY - onPointerDownPointerY) * 0.1 + onPointerDownLat; } } function onDocumentMouseUp(event) { isUserInteracting = false; } function onDocumentMouseWheel(event) { camera.fov += event.deltaY * 0.05; camera.updateProjectionMatrix(); } function animate() { requestAnimationFrame(animate); update(); } function update() { if (isUserInteracting === false) { lon += 0.1; } lat = Math.max(-85, Math.min(85, lat)); phi = THREE.Math.degToRad(90 - lat); theta = THREE.Math.degToRad(lon); 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); renderer.render(scene, camera); } </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值