小房子模型

package
{
	import flash.display.GraphicsTrianglePath;
	import flash.display.Sprite;
	import flash.display.TriangleCulling;
	import flash.events.Event;
	import flash.geom.Vector3D;
	
	/**
	 *  @author:Gaara
	 *  2012-3-14
	 *
	 **/
	[SWF(width="800", height="600" ,frameRate="30", backgroundColor="#FFFFFF")]
	public class TestCamer2 extends Sprite
	{
		//定义路径类
		private var triangPath:GraphicsTrianglePath;
		
		private var sprite:Sprite = new Sprite;
		
		private var indices:Vector.<int>;
		private var uvtData:Vector.<Number>;
		private var vertices:Vector.<Vector3D>;
		
		private  var _stringV:String = "-11765.4 -4473.12 0.000033 11765.4 -4473.12 0.000033 -11765.4 4473.12 -0.000033 11765.4 4473.12 -0.000033 11765.4 0.0001 17448.4 -11765.4 0.0001 17448.4 11765.4 -7863.28 6070.26 -11765.4 -7863.28 6070.26 11765.4 7863.28 6070.26 -11765.4 7863.28 6070.26";
		private  var _stringU:String = "0.667021 0.012993 -0.225191 0.996577 0.012993 -0.22519 0.667468 0.010391 0.5 0.997024 0.010391 0.5 0.996577 0.993478 0.137405 0.667021 0.993478 0.137405 0.996577 0.354103 -0.5 0.667021 0.354103 -0.5 0.258144 0.016835 -0.5 0.258144 0.016834 0.5 0.074669 0.016835 -0.5 0.331531 0.014076 0.068861 0.664663 0.014076 0.068861 0.664663 0.992088 -0.5 0.331531 0.992088 -0.5 0.074669 0.016834 0.5 0.166407 0.990086 0.5 0.166407 0.990086 -0.5 0.664663 0.354325 0.5 0.327671 0.355428 0.5 0.331531 0.354325 0.5 0.327671 0.355428 -0.5 0.005142 0.355428 0.5 0.005142 0.355428 -0.5";
		private  var _stringUV:String = "1 1 6 6 7 7 7 7 0 0 1 1 3 15 8 22 6 19 6 19 1 9 3 15 2 11 9 20 8 18 8 18 3 12 2 11 0 8 7 21 9 23 9 23 2 10 0 8 4 4 5 5 7 7 7 7 6 6 4 4 8 22 4 16 6 19 5 14 4 13 8 18 8 18 9 20 5 14 7 21 5 17 9 23";
		
		
		private var _Cam:Object = { x:0, y:0, z:0, br:200, angle_x:0, angle_y:0, angle_z:0 } //设计一个镜头
		
		//用于存放对应该点
		private var _v_array:Array=new Array()
		private var _u_array:Array=new Array()
		private var _uv_array:Array=new Array()
		
		public function TestCamer2()
		{
			super();
			
			sprite.x = this.stage.stageWidth / 2;
			sprite.y =  this.stage.stageHeight /2;
			addChild(sprite);
			
			
			var temp_array:Array = new Array();
			var scale_D:Number = 0.006  //这个比例值,就是对上面数据进行缩放的,
			
			temp_array = (_stringV.split(" "))   //拆分 3D点
			
			vertices = new Vector.<Vector3D>();
			
			for (var i:int = 0; i < temp_array.length / 3; i++ ) {
				_v_array.push(new Vector3D(temp_array[i * 3 + 0] * scale_D, temp_array[i * 3 + 2] * scale_D, temp_array[i * 3 + 1] * scale_D))
			}
			
			temp_array = (_stringU.split(" "))  //拆分 2D 贴图
			for (i = 0; i < temp_array.length / 3; i++ ) {
				_u_array.push(new Array(temp_array[i * 3 + 0], temp_array[i * 3 + 1],temp_array[i * 3 + 2]));
			}
			
			temp_array = (_stringUV.split(" "))   //折分UV 对应坐标
			for (i = 0; i < temp_array.length/2 ; i++ ) {
				_uv_array.push(new Array(temp_array[i*2+0],temp_array[i*2+1]))
			}
			
			for (i = 0; i < _uv_array.length; i++ ) {
				var index:int = _uv_array[i][0];
				var v:Vector3D = _v_array[index];
				vertices.push(v);
			}
			
			indices = new Vector.<int>();
			
			uvtData = new Vector.<Number>();
			for (i = 0; i < _uv_array.length; i++ ) {   //是_triangles进行 uvtData(贴图) 赋值 , 同时要给对应的vertices 进行赋值(暂为0,0)以后会运算的
				uvtData.push(_u_array[_uv_array[i][1]][0],1-_u_array[_uv_array[i][1]][1],1);
			}
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			_Cam.angle_y++;
			
			var rx:Number = 0;
			var ry:Number = 0;
			var rz:Number = 0;
			var tmp_angle_y:Number = _Cam.angle_y * Math.PI / 180;
			_Cam.x = Math.cos(tmp_angle_y) * rx + Math.sin(tmp_angle_y) * rz;
			_Cam.z = Math.sin(tmp_angle_y) * rx + Math.cos(tmp_angle_y) * rz;
			
			up_data() ;
		}
		
		public function up_data():void {			
			var radian:Number = _Cam.angle_y *  Math.PI/180;
			
			var newVertices:Vector.<Number> = new Vector.<Number>;
			
			for (var i:int = 0; i < vertices.length; i++) 
			{
				var vec3d:Vector3D =  vertices[i]
				
				var rx:Number = vec3d.x -_Cam.x
				var ry:Number = vec3d.y -_Cam.y
				var rz:Number = vec3d.z -_Cam.z
				
				var newX:Number = Math.cos(radian) * rx -  Math.sin(radian) * rz;
				var newZ:Number = Math.sin(radian) * rx + Math.cos(radian) * rz ;
				
				var scale:Number =  _Cam.br/(_Cam.br+newZ);
				uvtData[i*3+2] = _Cam.br/(_Cam.br+newZ);
				
				newVertices.push(newX*scale);
				newVertices.push(-ry*scale);
			}
			
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			//		sprite.graphics.lineStyle(1,0xFF0000);
			sprite.graphics.drawTriangles(newVertices,indices,uvtData,TriangleCulling.POSITIVE);
			sprite.graphics.endFill();
		}
	}		
}

 

posted @ 2012-03-17 10:52  luozhifu  阅读(318)  评论(0编辑  收藏  举报