drawTriangles用法

 
看pan大神的博客,研究源码。
package
{
	import flash.display.Bitmap;
	import flash.display.GraphicsTrianglePath;
	import flash.display.Sprite;
	
	/**
	 *  @author:Gaara
	 *  2012-3-13
	 *
	 **/
	[SWF(width="800", height="600" ,frameRate="30", backgroundColor="#000000")]
	public class TestDrawTriang1 extends Sprite
	{
		
		//定义路径类
		private var triangPath:GraphicsTrianglePath;
		
		public function TestDrawTriang1()
		{
			super();
			
			var sprite:Sprite = new Sprite;
			sprite.x = this.stage.stageWidth / 2;
			sprite.y =  this.stage.stageHeight /2;
			addChild(sprite);
			
			var vertices:Vector.<Number> = new Vector.<Number>();
			vertices.push(0,0);
			vertices.push(0,200);
			vertices.push(200,200);
			vertices.push(200,0);
			
			var indices:Vector.<int> = new Vector.<int>();
			indices.push(1,2,3,1,3,0);
			
			var uvtData:Vector.<Number> = new Vector.<Number>();
			uvtData.push(0,0);
			uvtData.push(0,1);
			uvtData.push(1,1);
			uvtData.push(1,0);
				
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			sprite.graphics.drawTriangles (vertices,indices,uvtData);
			sprite.graphics.endFill();
		}
	}
}

 

对应stage中的内容,顶点,索引和贴图坐标。

 

3d坐标映射到2d坐标

 

		var indices:Vector.<int> = new Vector.<int>();
			indices.push(0,1,2,0,2,3);
			
			var uvtData:Vector.<Number> = new Vector.<Number>();
			uvtData.push(0,0);
			uvtData.push(1,0);
			uvtData.push(1,1);
			uvtData.push(0,1);
			
			var vertices:Vector.<Vector3D> = new Vector.<Vector3D>();
			
			vertices.push(new Vector3D(0,0,0));
			vertices.push(new Vector3D(100,0,50));
			vertices.push(new Vector3D(100,100,50));
			vertices.push(new Vector3D(0,100,0));
			
			var newVertices:Vector.<Number> = new Vector.<Number>;
			for each (var vec3d:Vector3D in vertices) 
			{
				var scale:Number =  D/(D+vec3d.z);
				newVertices.push(scale * vec3d.x);
				newVertices.push(scale * vec3d.y);
			}
			
			
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			sprite.graphics.drawTriangles(newVertices,indices,uvtData);
			sprite.graphics.endFill();

 y轴旋转

package
{
	import flash.display.Bitmap;
	import flash.display.GraphicsTrianglePath;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	
	/**
	 *  @author:Gaara
	 *  2012-3-13
	 *
	 **/
	[SWF(width="800", height="600" ,frameRate="30", backgroundColor="#000000")]
	public class TestDrawTriang1 extends Sprite
	{
		
		private const D:int = 300;
		
		//定义路径类
		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 angel:int = 0;
		
		
		public function TestDrawTriang1()
		{
			super();
			

			sprite.x = this.stage.stageWidth / 2;
			sprite.y =  this.stage.stageHeight /2;
			addChild(sprite);
			
			indices = new Vector.<int>();
			indices.push(0,1,2,0,2,3);
			
			uvtData = new Vector.<Number>();
			uvtData.push(0,0);
			uvtData.push(1,0);
			uvtData.push(1,1);
			uvtData.push(0,1);
			
			vertices = new Vector.<Vector3D>();
			
			vertices.push(new Vector3D(0,0,0));
			vertices.push(new Vector3D(100,0,50));
			vertices.push(new Vector3D(100,100,50));
			vertices.push(new Vector3D(0,100,0));
			
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			angel++;
			var radian:Number = angel/180*Math.PI;
			
			var newVertices:Vector.<Number> = new Vector.<Number>;
			for each (var vec3d:Vector3D in vertices) 
			{
				var newX:Number = Math.cos(radian) * vec3d.x -  Math.sin(radian) * vec3d.z;
				var newZ:Number = Math.cos(radian) * vec3d.z +  Math.sin(radian) * vec3d.x;
				
				var scale:Number =  D/(D+newZ);
				newVertices.push(scale * newX);
				newVertices.push(scale * vec3d.y);
			}
			
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			sprite.graphics.drawTriangles(newVertices,indices,uvtData);
			sprite.graphics.endFill();
			
		}		
		
	}
}

 

 

正方体

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 Test3d extends Sprite
	{
		
		private const D:int = 300;
		
		//定义路径类
		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 angel:int = 0;
		
		
		public function Test3d()
		{
			super();
			
			
			sprite.x = this.stage.stageWidth / 2;
			sprite.y =  this.stage.stageHeight /2;
			addChild(sprite);
			
			indices = new Vector.<int>();
			indices.push(0,1,2);
			indices.push(0,2,3);
			
			indices.push(4,5,6);
			indices.push(4,6,7);
			
			indices.push(8,9,10);
			indices.push(8,10,11);
			
			indices.push(12,13,14);
			indices.push(12,14,15);
			
			uvtData = new Vector.<Number>();
			uvtData.push(0,0,1);
			uvtData.push(1,0,1);
			uvtData.push(1,1,1);
			uvtData.push(0,1,1);
			
			uvtData.push(0,0,1);
			uvtData.push(1,0,1);
			uvtData.push(1,1,1);
			uvtData.push(0,1,1);
			
			uvtData.push(0,0,1);
			uvtData.push(1,0,1);
			uvtData.push(1,1,1);
			uvtData.push(0,1,1);
			
			uvtData.push(0,0,1);
			uvtData.push(1,0,1);
			uvtData.push(1,1,1);
			uvtData.push(0,1,1);
			
			vertices = new Vector.<Vector3D>();
			
			vertices.push(new Vector3D(0,0,0));
			vertices.push(new Vector3D(100,0,0));
			vertices.push(new Vector3D(100,100,0));
			vertices.push(new Vector3D(0,100,0));
			
			
			vertices.push(new Vector3D(100,0,0));
			vertices.push(new Vector3D(100,0,100));
			vertices.push(new Vector3D(100,100,100));
			vertices.push(new Vector3D(100,100,0));

		
			vertices.push(new Vector3D(100,0,100));
			vertices.push(new Vector3D(0,0,100));
			vertices.push(new Vector3D(0,100,100));
			vertices.push(new Vector3D(100,100,100));
			
			vertices.push(new Vector3D(0,0,100));
			vertices.push(new Vector3D(0,0,0));
			vertices.push(new Vector3D(0,100,0));
			vertices.push(new Vector3D(0,100,100));
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			angel++;
			var radian:Number = angel/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(radian) * vec3d.x -  Math.sin(radian) * vec3d.z;
				var newZ:Number = Math.cos(radian) * vec3d.z +  Math.sin(radian) * vec3d.x;
	
				var scale:Number =  D/(D+newZ);
				uvtData[i*3+2] = scale;
				
				newVertices.push(scale * newX);
				newVertices.push(scale * vec3d.y);
			}
			
			sprite.graphics.clear();
			sprite.graphics.beginBitmapFill(ResConfig.myLpBmp.bitmapData);
			sprite.graphics.drawTriangles(newVertices,indices,uvtData,TriangleCulling.NEGATIVE);
			sprite.graphics.endFill();
			
		}		
	}
}

 三角形增加深度z。等于绽放比例。GraphicsTrianglePath的最后一个参数,表示剔除算法,顶点顺时针时,法线指向z坐标正方向,TriangleCulling.POSITIVE剔除指向正的三角形,TriangleCulling.NEGATIVE剔除负的三角形。逆时针反之。

				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 radianx:Number = mouseX /180*Math.PI;
			var radiany:Number = mouseY /180*Math.PI;

 

 

转载于:https://www.cnblogs.com/riaol/archive/2012/03/13/2393995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值