PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/tools/data/MeshDebug.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 178 lines | 136 code | 39 blank | 3 comment | 5 complexity | 178747980e849099735d892c688046d2 MD5 | raw file
  1package away3d.tools.data
  2{
  3	import away3d.core.base.Geometry;
  4	import away3d.core.base.SubGeometry;
  5	import away3d.entities.Mesh;
  6	import away3d.entities.SegmentSet;
  7	import away3d.primitives.LineSegment;
  8
  9	import flash.geom.Vector3D;
 10
 11	/**
 12	* MeshDebug, holds the data for the MeshDebugger class
 13	*/
 14	public class MeshDebug extends SegmentSet{
 15		 
 16		private var _normal : Vector3D = new Vector3D();
 17		private const NORMALS : uint = 0;
 18		private const VERTEXNORMALS : uint = 1;
 19		private const TANGENTS : uint = 2;
 20		 
 21		public function clearAll():void
 22		{
 23			super.removeAllSegments();
 24		}
 25		
 26		public function displayNormals(mesh:Mesh, color:uint = 0xFF3399 , length:Number = 30):void
 27		{
 28			var geometry:Geometry = mesh.geometry;
 29			var geometries:Vector.<SubGeometry> = geometry.subGeometries;
 30			var numSubGeoms:uint = geometries.length;
 31			
 32			var vertices:Vector.<Number>;
 33			var indices:Vector.<uint>;
 34			var index:uint;
 35			var indexV:uint;
 36			var j:uint;
 37			
 38			var v0 : Vector3D = new Vector3D();
 39			var v1 : Vector3D = new Vector3D();
 40			var v2 : Vector3D = new Vector3D();
 41			
 42			var l0 : Vector3D = new Vector3D();
 43			var l1 : Vector3D = new Vector3D();
 44			
 45			for (var i:uint = 0; i<numSubGeoms; ++i){					
 46			 	vertices = SubGeometry(geometries[i]).vertexData;
 47				indices = SubGeometry(geometries[i]).indexData;
 48				for (j = 0; j < indices.length; j+=3){
 49					
 50					v0.x = vertices[index = indices[j]*3];
 51					v0.y = vertices[index+1];
 52					v0.z = vertices[index+2];
 53					
 54					v1.x = vertices[index = indices[j+1]*3];
 55					v1.y = vertices[index+1];
 56					v1.z = vertices[index+2];
 57					
 58					v2.x = vertices[index = indices[j+2]*3];
 59					v2.y = vertices[index+1];
 60					v2.z = vertices[index+2];
 61					
 62					calcNormal(v0,v1,v2);
 63					
 64					l0.x = (v0.x + v1.x + v2.x)/3;
 65					l0.y = (v0.y + v1.y + v2.y)/3;
 66					l0.z = (v0.z + v1.z + v2.z)/3;
 67					
 68					l1.x = l0.x + (_normal.x*length);
 69					l1.y = l0.y + (_normal.y*length);
 70					l1.z = l0.z + (_normal.z*length);
 71					
 72					addSegment(new LineSegment(l0, l1, color, color, 1 ));
 73				}
 74			}
 75		}
 76		
 77		public function displayVertexNormals(mesh:Mesh, color:uint = 0x66CCFF, length:Number = 30):void
 78		{
 79			build(mesh, VERTEXNORMALS , color, length);
 80		}
 81		
 82		public function displayTangents(mesh:Mesh, color:uint = 0xFFCC00, length:Number = 30):void
 83		{
 84			build(mesh, TANGENTS, color, length);
 85		}
 86		
 87		private function build(mesh:Mesh, type:uint, color:uint = 0x66CCFF, length:Number = 30):void
 88		{
 89			var geometry:Geometry = mesh.geometry;
 90			var geometries:Vector.<SubGeometry> = geometry.subGeometries;
 91			var numSubGeoms:uint = geometries.length;
 92			
 93			var vertices:Vector.<Number>; 
 94			var vertorTarget:Vector.<Number>;
 95			var tangents:Vector.<Number>;
 96			 
 97			var indices:Vector.<uint>;
 98			var index:uint;
 99			var indexV:uint;
100			var j:uint;
101			
102			var v0 : Vector3D = new Vector3D();
103			var v1 : Vector3D = new Vector3D();
104			var v2 : Vector3D = new Vector3D();
105			
106			var l0 : Vector3D = new Vector3D();
107			 
108			for (var i:uint = 0; i<numSubGeoms; ++i){					
109			 	vertices = SubGeometry(geometries[i]).vertexData;
110				vertorTarget = (type == 1)? SubGeometry(geometries[i]).vertexNormalData : SubGeometry(geometries[i]).vertexTangentData;
111				indices = SubGeometry(geometries[i]).indexData;
112				for (j = 0; j < indices.length; j+=3){
113					
114					v0.x = vertices[index = indices[j]*3];
115					_normal.x = vertorTarget[index];
116					v0.y = vertices[index+1];
117					_normal.y = vertorTarget[index+1];
118					v0.z = vertices[index+2];
119					_normal.z = vertorTarget[index+2];
120					
121					l0.x = v0.x + (_normal.x*length);
122					l0.y = v0.y + (_normal.y*length);
123					l0.z = v0.z + (_normal.z*length);
124					
125					addSegment(new LineSegment(v0, l0, color, color, 1 ));
126					 
127					
128					v1.x = vertices[index = indices[j+1]*3];
129					_normal.x = vertorTarget[index];
130					v1.y = vertices[index+1];
131					_normal.y = vertorTarget[index+1];
132					v1.z = vertices[index+2];
133					_normal.z = vertorTarget[index+2];
134					
135					l0.x = v1.x + (_normal.x*length);
136					l0.y = v1.y + (_normal.y*length);
137					l0.z = v1.z + (_normal.z*length);
138					
139					addSegment(new LineSegment(v1, l0, color, color, 1 ));
140					
141					
142					v2.x = vertices[index = indices[j+2]*3];
143					_normal.x = vertorTarget[index];
144					v2.y = vertices[index+1];
145					_normal.y = vertorTarget[index+1];
146					v2.z = vertices[index+2];
147					_normal.z = vertorTarget[index+2];
148					
149					l0.x = v2.x + (_normal.x*length);
150					l0.y = v2.y + (_normal.y*length);
151					l0.z = v2.z + (_normal.z*length);
152					
153					addSegment(new LineSegment(v2, l0, color, color, 1 ));
154				}
155			}
156		}
157		
158		private function calcNormal(v0:Vector3D,v1:Vector3D,v2:Vector3D):void
159		{
160			var dx1:Number = v2.x-v0.x;
161			var dy1:Number = v2.y-v0.y;
162			var dz1:Number = v2.z-v0.z;
163			var dx2:Number = v1.x-v0.x;
164			var dy2:Number = v1.y-v0.y;
165			var dz2:Number = v1.z-v0.z;
166			
167			var cx:Number = dz1*dy2 - dy1*dz2;
168			var cy:Number = dx1*dz2 - dz1*dx2;
169			var cz:Number = dy1*dx2 - dx1*dy2;
170			var d:Number  = 1/Math.sqrt(cx*cx+cy*cy+cz*cz);
171			 
172			_normal.x = cx*d;
173			_normal.y = cy*d;
174			_normal.z = cz*d; 
175		}
176		
177	}
178}