zoukankan      html  css  js  c++  java
  • 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;
    
  • 相关阅读:
    JNI接口的使用(简单版)
    Android内核剖析(1)
    Spring 和 Mybatis 整合
    AppStore被拒原因及总结
    iOS开发中,应用内直接跳转到Appstore
    ios 中生成二维码和相册中识别二维码
    IOS开发中(null)与<null>的处理
    融云即时通讯~~
    iOS中菊花。。。
    关于判断邮箱 手机号等一系列的正则表达式
  • 原文地址:https://www.cnblogs.com/riaol/p/2393995.html
Copyright © 2011-2022 走看看