1. 程式人生 > >ThreeJS學習筆記(6)- 讓場景動起來

ThreeJS學習筆記(6)- 讓場景動起來

場景中的物體怎麼才能運動起來。我們這裡從《古蘭經》講起,這樣,你永遠不會忘記。

《古蘭經》上有這樣一個故事:一天穆罕穆德告訴人們說大山會向我們走來。於是人們就遠望大山,看它怎麼走過來,可是等了好長時間大山還是紋絲不動的在那裡,人們就問穆罕穆德,大山也沒向我們走來啊。默罕默德告訴人們:既然大山沒向我們走來,那我們就向大山走去吧。於是人們來到了大山的山頂,人們征服了那座大山。

這個故事揭示了場景動起來的方法,第一種方法是讓物體在座標系裡面移動,攝像機不動。第二種方法是讓攝像機在座標系裡面移動,物體不動。這樣場景就能夠動起來了。攝像機可以理解我們自己的眼睛。

<1> 讓攝像機在座標系裡面移動

<!DOCTYPE html>

<html>
<head>
	<title>
	</title>
	<style type="text/css">
		body{
			margin:0;
		}
		div#canvas-frame {
			border: none;
			cursor: pointer;
			width: 100%;
			height: 600px;
			background-color: #EEEEEE;
		}
	</style>
	<script src="js/three.js"></script>
	<script src="js/jquery.js"></script>
	<script>
		//定義場景並初始化
		var scene;
		function initScene(){
			scene = new THREE.Scene();
		}
		
		//定義相機並初始化
		var camera;
		function initCamera(){
			camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,1,10000);
			camera.position.set(0,100,500);
			camera.lookAt(new THREE.Vector3(0,0,0));
		}
		
		//定義渲染器並初始化
		var renderer;
		function initRenderer(){
			renderer= new THREE.WebGLRenderer();
			renderer.setSize(window.innerWidth,window.innerHeight);
			$("#canvas-frame").append(renderer.domElement);
			renderer.setClearColor(0x39609b,1);
		}
		
		//定義燈光並初始化
		var light;
		function initLight(){
			light=new THREE.DirectionalLight(0xFF0000,1.0,0);
			light.position.set(100,100,200);
			scene.add(light);
		}
		
		//新增漸變線
		function initObject(){
			var geometry = new THREE.Geometry();
			var material = new THREE.MeshBasicMaterial({ vertexColors: true });
			var color1=new THREE.Color(0x444444);
			var color2=new THREE.Color(0xFF0000);
			
			var p1=new THREE.Vector3(-100,0,100);
			var p2=new THREE.Vector3(100,0,-100);
			
			geometry.vertices.push(p1);
			geometry.vertices.push(p2);
			geometry.colors.push(color1,color2);
			
			var line=new THREE.Line(geometry,material);
			
			scene.add(line);
		}
		
		//新增3D文字
		function init3DText(fontloader){
			var fontloader;
			fontloader = new THREE.FontLoader();
			fontloader.load('fonts/helvetiker_bold.typeface.json',function(font){
				var text="FOX";
				var g=new THREE.TextGeometry(text,{
					font:font,
					size:50,
					height:30,
				});
				
				g.computeBoundingBox();
				//3D文字材質
				var m = new THREE.MeshBasicMaterial({color:0xffff00});
				var mesh = new THREE.Mesh(g,m);			
				// 加入到場景中
				scene.add(mesh);  
			});
		}
		
		//新增網路線
		function initNetLine(){
			    var geometry = new THREE.Geometry();
                geometry.vertices.push( new THREE.Vector3( - 500, 0, 0 ) );
                geometry.vertices.push( new THREE.Vector3( 500, 0, 0 ) );

                for ( var i = 0; i <= 100; i ++ ) {

                    var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
                    line.position.z = ( i * 10 ) - 500;
                    scene.add( line );

                    var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
                    line.position.x = ( i * 10 ) - 500;
                    line.rotation.y = 90 * Math.PI / 180;
                    scene.add( line );

                }
		}
	
		function threeStart(){
			initScene();
			initCamera();
			initRenderer();
			initLight();
			//initObject();
			//init3DText();
			initNetLine();
			// 渲染
			animate();
		}
		
        function animate(){
			camera.position.z =camera.position.z - 1;
            requestAnimationFrame( animate );
             // 渲染
            renderer.render(scene,camera);   
        }
	</script>
</head>
<body onload="threeStart();">
	<div id="canvas-frame"></div>
</body>
</html>
<2>物體在座標系裡面移動,攝像機不動
<!DOCTYPE html>

<html>
<head>
	<title>
	</title>
	<style type="text/css">
		body{
			margin:0;
		}
		div#canvas-frame {
			border: none;
			cursor: pointer;
			width: 100%;
			height: 600px;
			background-color: #EEEEEE;
		}
	</style>
	<script src="js/three.js"></script>
	<script src="js/jquery.js"></script>
	<script>
		//定義場景並初始化
		var scene;
		function initScene(){
			scene = new THREE.Scene();
		}
		
		//定義相機並初始化
		var camera;
		function initCamera(){
			camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,1,10000);
			camera.position.set(0,100,500);
			camera.lookAt(new THREE.Vector3(0,0,0));
		}
		
		//定義渲染器並初始化
		var renderer;
		function initRenderer(){
			renderer= new THREE.WebGLRenderer();
			renderer.setSize(window.innerWidth,window.innerHeight);
			$("#canvas-frame").append(renderer.domElement);
			renderer.setClearColor(0x39609b,1);
		}
		
		//定義燈光並初始化
		var light;
		function initLight(){
			light=new THREE.DirectionalLight(0xFF0000,1.0,0);
			light.position.set(100,100,200);
			scene.add(light);
		}
		
		var line;
		//新增漸變線
		function initObject(){
			var geometry = new THREE.Geometry();
			var material = new THREE.MeshBasicMaterial({ vertexColors: true });
			var color1=new THREE.Color(0x444444);
			var color2=new THREE.Color(0xFF0000);
			
			var p1=new THREE.Vector3(-100,0,100);
			var p2=new THREE.Vector3(100,0,-100);
			
			geometry.vertices.push(p1);
			geometry.vertices.push(p2);
			geometry.colors.push(color1,color2);
			
			line=new THREE.Line(geometry,material);
			
			scene.add(line);
		}
		
		var mesh;
		//新增3D文字
		function init3DText(fontloader){
			var fontloader;
			fontloader = new THREE.FontLoader();
			fontloader.load('fonts/helvetiker_bold.typeface.json',function(font){
				var text="FOX";
				var g=new THREE.TextGeometry(text,{
					font:font,
					size:50,
					height:30,
				});
				
				g.computeBoundingBox();
				//3D文字材質
				var m = new THREE.MeshBasicMaterial({color:0xffff00});
				mesh = new THREE.Mesh(g,m);			
				// 加入到場景中
				scene.add(mesh);  
			});
		}
		
		//新增網路線
		function initNetLine(){
			    var geometry = new THREE.Geometry();
                geometry.vertices.push( new THREE.Vector3( - 500, 0, 0 ) );
                geometry.vertices.push( new THREE.Vector3( 500, 0, 0 ) );

                for ( var i = 0; i <= 100; i ++ ) {

                    var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
                    line.position.z = ( i * 10 ) - 500;
                    scene.add( line );

                    var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
                    line.position.x = ( i * 10 ) - 500;
                    line.rotation.y = 90 * Math.PI / 180;
                    scene.add( line );

                }
		}
	
		function threeStart(){
			initScene();
			initCamera();
			initRenderer();
			initLight();
			initObject();
			//init3DText();
			//initNetLine();
			// 渲染
			animate();
		}
		
        function animate(){
			line.position.y -= 1;
            requestAnimationFrame( animate );
             // 渲染
            renderer.render(scene,camera);   
        }
	</script>
</head>
<body onload="threeStart();">
	<div id="canvas-frame"></div>
</body>
</html>