绘制圆筒

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 DrawHoop extends Sprite
	{
		private const D:int = 300;
		
		//定义路径类
		private var triangPath:GraphicsTrianglePath;
		
		private var sprite:Sprite = new Sprite;
		
		private var indices:Vector.<int> = new Vector.<int>;
		private var uvtData:Vector.<Number> =  new Vector.<Number>;
		private var vertices:Vector.<Vector3D> = new Vector.<Vector3D>;
		private var angel:int = 0;
		

		
		private const RADIUS:int = 100;//半径
		
		private var cols:int = 20;//分成多少份
		private var radian:Number = 2 * Math.PI / cols;//弧度
		//		private var rows:int = 20;
		
		public function DrawHoop()
		{
			sprite.x = this.stage.stageWidth / 2;
			sprite.y =  this.stage.stageHeight /2;
			addChild(sprite);
			
			for (var i:int = 0;i < cols; i++) 
			{
				//前面的点
				var before:Vector3D =  new Vector3D(Math.cos(radian * i) * RADIUS, -RADIUS/4, Math.sin(radian * i) * RADIUS);
				vertices.push(before);

				var after:Vector3D =  new Vector3D(Math.cos(radian * i) * RADIUS, RADIUS/4, Math.sin(radian * i) * RADIUS);
				vertices.push(after);
			}
			
			for (var j:int = 0;j < cols - 1; j++) 
			{
				//前面的点
				indices.push(2*j,2*j+3,2*j+1);
				indices.push(2*j,2*j+2,2*j+3);
			}
			
			var afterCols:int = cols - 1;
			//添加最后一个
			indices.push(2*afterCols,1,2*afterCols+1);
			indices.push(2*afterCols,0,1);
			
			for (var k:int = 0; k < (cols/2); k++) 
			{
				uvtData.push(0,0,1);
				uvtData.push(0,1,1);
				uvtData.push(1,0,1);
				uvtData.push(1,1,1);
			}
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			angel++;
			var radianx:Number = mouseX /180*Math.PI;
			var radiany:Number = mouseY /180*Math.PI;
			
			var newVertices:Vector.<Number> = new Vector.<Number>;
			
			for (var i:int = 0; i < vertices.length; i++) 
			{
				var vec3d:Vector3D =  vertices[i]
				var newX:Number = Math.cos(radianx) * vec3d.x -  Math.sin(radianx) * vec3d.z;
				var newZ:Number = Math.cos(radianx) * vec3d.z +  Math.sin(radianx) * vec3d.x;
				
				var newX2:Number = Math.cos(radiany) * newX -  Math.sin(radiany) * vec3d.y;
				var newY:Number = Math.cos(radiany) * vec3d.y +  Math.sin(radiany) * newX;
				
				var scale:Number =  D/(D+newZ);
				uvtData[i*3+2] = scale;
				
				newVertices.push(scale * newX2);
				newVertices.push(scale * newY);
			}
			
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			sprite.graphics.drawTriangles(newVertices,indices,uvtData,TriangleCulling.NEGATIVE);
			sprite.graphics.endFill();
			
		}		
	}
}

 

首先设置好分成的数量以及半径,然后根据勾股定理,计算每个点的坐标,包括前后,正z以及-z两个点。然后设置索引,

		indices.push(2*afterCols,1,2*afterCols+1);
		indices.push(2*afterCols,0,1);

 最后两个三角形因为连接起始点需要单独设置,最后设置重复的贴图坐标即可,可交换y,z顶点坐标,修改显示的方向,上面就是修改后的。

posted @ 2012-03-15 11:22  luozhifu  阅读(294)  评论(1编辑  收藏  举报