PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/materials/utils/WireframeMapGenerator.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 98 lines | 59 code | 14 blank | 25 comment | 8 complexity | f53ff0a407270a1dccda86a3ba8b44fe MD5 | raw file
 1package away3d.materials.utils
 2{
 3	import away3d.arcane;
 4	import away3d.core.base.ISubGeometry;
 5	import away3d.entities.Mesh;
 6	
 7	import flash.display.BitmapData;
 8	import flash.display.Graphics;
 9	import flash.display.Sprite;
10	import flash.display.TriangleCulling;
11	
12	use namespace arcane;
13	
14	/**
15	 * WireframeMapGenerator is a utility class to generate a wireframe texture for uniquely mapped meshes.
16	 */
17	public class WireframeMapGenerator
18	{
19		/**
20		 * Create a wireframe map with a texture fill.
21		 * @param mesh The Mesh object for which to create the wireframe texture.
22		 * @param bitmapData The BitmapData to use as the fill texture.
23		 * @param lineColor The wireframe's line colour.
24		 * @param lineThickness The wireframe's line thickness.
25		 */
26		public static function generateTexturedMap(mesh:Mesh, bitmapData:BitmapData, lineColor:uint = 0xffffff, lineThickness:Number = 2):BitmapData
27		{
28			bitmapData = bitmapData.clone();
29			
30			for (var i:uint = 0; i < mesh.subMeshes.length; ++i)
31				drawLines(lineColor, lineThickness, bitmapData, mesh.subMeshes[i].subGeometry);
32			
33			return bitmapData;
34		}
35		
36		/**
37		 * Create a wireframe map with a solid colour fill.
38		 * @param mesh The Mesh object for which to create the wireframe texture.
39		 * @param lineColor The wireframe's line colour.
40		 * @param lineThickness The wireframe's line thickness.
41		 * @param fillColor The colour of the wireframe fill.
42		 * @param fillAlpha The alpha of the wireframe fill.
43		 * @param width The texture's width.
44		 * @param height The texture's height.
45		 * @return A BitmapData containing the texture underneath the wireframe.
46		 */
47		public static function generateSolidMap(mesh:Mesh, lineColor:uint = 0xffffff, lineThickness:Number = 2, fillColor:uint = 0, fillAlpha:Number = 0, width:uint = 512, height:uint = 512):BitmapData
48		{
49			var bitmapData:BitmapData;
50			
51			if (fillAlpha > 1)
52				fillAlpha = 1;
53			else if (fillAlpha < 0)
54				fillAlpha = 0;
55			
56			bitmapData = new BitmapData(width, height, fillAlpha == 1? false : true, (fillAlpha << 24) | (fillColor & 0xffffff));
57			
58			for (var i:uint = 0; i < mesh.subMeshes.length; ++i)
59				drawLines(lineColor, lineThickness, bitmapData, mesh.subMeshes[i].subGeometry);
60			
61			return bitmapData;
62		}
63		
64		/**
65		 * Draws the actual lines.
66		 */
67		private static function drawLines(lineColor:uint, lineThickness:Number, bitmapData:BitmapData, subGeom:ISubGeometry):void
68		{
69			var sprite:Sprite = new Sprite();
70			var g:Graphics = sprite.graphics;
71			var uvs:Vector.<Number> = subGeom.UVData;
72			var i:uint, len:uint = uvs.length;
73			var w:Number = bitmapData.width, h:Number = bitmapData.height;
74			var texSpaceUV:Vector.<Number> = new Vector.<Number>(len, true);
75			var indices:Vector.<uint> = subGeom.indexData;
76			var indexClone:Vector.<int>;
77			
78			do {
79				texSpaceUV[i] = uvs[i]*w;
80				++i;
81				texSpaceUV[i] = uvs[i]*h;
82			} while (++i < len);
83			
84			len = indices.length;
85			indexClone = new Vector.<int>(len, true);
86			i = 0;
87			// awesome, just to convert from uint to int vector -_-
88			do
89				indexClone[i] = indices[i];
90			while (++i < len);
91			
92			g.lineStyle(lineThickness, lineColor);
93			g.drawTriangles(texSpaceUV, indexClone, null, TriangleCulling.NONE);
94			bitmapData.draw(sprite);
95			g.clear();
96		}
97	}
98}