WebGL编程

webgl_interactive_cubes.html

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - map controls</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 {
				color: #000;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				font-weight: bold;

				background-color: #fff;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				color:#000;
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
				box-sizing: border-box;
			}

			a {
				color: red;
			}
		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - map controls example
		</div>

		<script src="js/three.js"></script>
		<script src="js/controls/MapControls.js"></script>
		<script src="js/controls/DragControls.js"></script>
		<script src="js/WebGL.js"></script>

		<script src='js/libs/dat.gui.min.js'></script>

		<script>

			if ( WEBGL.isWebGLAvailable() === false ) {

				document.body.appendChild( WEBGL.getWebGLErrorMessage() );

			}

			var camera, controls, scene, renderer;

			init();
			//render(); // remove when using next line for animation loop (requestAnimationFrame)
			animate();

			function init() {

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xcccccc );
				// scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 ); // 雾
				var objects = [];

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
				//camera.up.x = 0;
                //camera.up.y = 0;
                //camera.up.z = 1;//相机以Z方向为上方
				/*
				 * x:0
				 * y:1000
				 * z:0
				 */
				camera.position.set( 0, 1000, 0 ); // 摄像机的位置

				// controls

				controls = new THREE.MapControls( camera, renderer.domElement );

				//controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)

				controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
				controls.dampingFactor = 0.25; // 旋转灵敏度

				controls.screenSpacePanning = false;

				controls.minDistance = 0;
				controls.maxDistance = 2000;

				controls.maxPolarAngle = Math.PI / 2;
				
				// 地板图
				var loader = new THREE.TextureLoader();
				loader.load("js/img/aaaaaa.png", function(texture) {
					texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
					texture.repeat.set(1, 1); // 图片排列
					var floorGeometry = new THREE.BoxGeometry(3875, 1746, 1); // 平面大小
					var floorMaterial = new THREE.MeshBasicMaterial({
						map : texture,
						side : THREE.DoubleSide
					});
					var floor = new THREE.Mesh(floorGeometry, floorMaterial);
					floor.position.y = 1;
					floor.rotation.x = Math.PI / 2;
					scene.add(floor);
				});

				// world
				var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
				geometry.translate( 0, 0.5, 0 );
				var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
				var diban = new THREE.Mesh( geometry, material );
				for ( var i = 0; i < 500; i ++ ) {

					var mesh = new THREE.Mesh( geometry, material );
					mesh.position.x = Math.random() * 1600 - 800;
					mesh.position.y = 0;
					mesh.position.z = Math.random() * 1600 - 800;
					mesh.scale.x = 20;
					mesh.scale.y = 50;
					mesh.scale.z = 20;
					mesh.updateMatrix();
					mesh.matrixAutoUpdate = false;
					mesh.name = "Jinold" + i;
					
					
					scene.add( mesh );
					objects.push( mesh );

				}

				// lights

				var light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 1, 1, 1 );
				scene.add( light );

				var light = new THREE.DirectionalLight( 0x002288 );
				light.position.set( - 1, - 1, - 1 );
				scene.add( light );

				var light = new THREE.AmbientLight( 0x222222 );
				scene.add( light );

				//

				window.addEventListener( 'resize', onWindowResize, false );


				var gui = new dat.GUI();

				gui.add( controls, 'screenSpacePanning' );
				
				
				// 
				var dragControls = new THREE.DragControls( objects, camera, renderer.domElement );
				dragControls.addEventListener( 'dragstart', function (event) {
					// alert(event.object.name)
					event.object.material.emissive.Color = 0xff00ff
					controls.enabled = false;

				} );
				dragControls.addEventListener( 'dragend', function (event) {

					controls.enabled = true;

				} );

			}

			function onWindowResize() {

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

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

			}

			function animate() {

				requestAnimationFrame( animate );

				controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true

				render();

			}

			function render() {

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值