PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/src/away3d/tools/Grid.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 136 lines | 89 code | 20 blank | 27 comment | 10 complexity | 62f7ba47dd4595624e3ee540386afd97 MD5 | raw file
  1package away3d.tools
  2{
  3	import away3d.arcane;
  4	import away3d.containers.ObjectContainer3D;
  5	import away3d.core.base.Geometry;
  6	import away3d.core.base.SubGeometry;
  7	import away3d.entities.Mesh;
  8
  9	use namespace arcane;
 10	/**
 11	 * Class Grid snaps vertexes or meshes according to a given grid unit.<code>Grid</code>
 12	 */
 13	public class Grid{
 14		
 15		private var _unit:Number;
 16		private var _objectSpace:Boolean;
 17		
 18		/**
 19		*  Grid snaps vertexes according to a given grid unit
 20		* @param	 unit						[optional] Number. The grid unit. Default is 1.
 21		* @param	 objectSpace			[optional] Boolean. Apply only to vertexes in geometry objectspace when Object3D are considered. Default is false.
 22		*/
 23		 
 24		function Grid(unit:Number = 1, objectSpace:Boolean = false):void
 25		{
 26			_objectSpace = objectSpace;
 27			_unit = Math.abs(unit);
 28		}
 29
 30		/**
 31		*  Apply the grid code to a given object3D. If type ObjectContainer3D, all children Mesh vertices will be affected.
 32		* @param	 object3d		Object3D. The Object3d to snap to grid.
 33		* @param	 dovert			[optional]. If the vertices must be handled or not. When false only object position is snapped to grid. Default is false.
 34		*/
 35		public function snapObject(object3d:ObjectContainer3D, dovert:Boolean = false):void
 36		{
 37			parse(object3d, dovert);
 38		}
 39		
 40		/**
 41		*  Snaps to grid a given Vector.&lt;Number&gt; of vertices
 42		* @param	 vertices		Vector.&lt;Number&gt;. The vertices vector
 43		*/		
 44		public function snapVertices(vertices:Vector.<Number>):Vector.<Number>
 45		{
 46			for (var i:uint = 0; i < vertices.length;++i)
 47					vertices[i] -= vertices[i]%_unit;
 48				
 49			return vertices;
 50		}
 51		/**
 52		*  Apply the grid code to a single mesh
 53		* @param	 mesh		Mesh. The mesh to snap to grid. Vertices are affected by default. Mesh position is snapped if grid.objectSpace is true;
 54		*/		
 55		public function snapMesh(mesh:Mesh):void
 56		{
 57			if(!_objectSpace){
 58				mesh.scenePosition.x -= mesh.scenePosition.x%_unit;
 59				mesh.scenePosition.y -= mesh.scenePosition.y%_unit;
 60				mesh.scenePosition.z -= mesh.scenePosition.z%_unit;
 61			}
 62			snap(mesh);
 63		}
 64		
 65		/**
 66		* Defines if the grid unit.
 67		*/
 68		public function set unit(val:Number):void
 69		{
 70			_unit = Math.abs(val);
 71			_unit = (_unit ==0)? .001 : _unit;
 72		}
 73		
 74		public function get unit():Number
 75		{
 76			return _unit;
 77		}
 78		
 79		/**
 80		* Defines if the grid unit is applied in objectspace or worldspace. In worldspace, objects positions are affected.
 81		*/
 82		public function set objectSpace(b:Boolean):void
 83		{
 84			_objectSpace = b;
 85		}
 86		public function get objectSpace():Boolean
 87		{
 88			return _objectSpace;
 89		}
 90		
 91		private function parse(object3d:ObjectContainer3D, dovert:Boolean = true):void
 92		{
 93			var child:ObjectContainer3D;
 94			
 95			if(!_objectSpace){
 96				object3d.scenePosition.x -= object3d.scenePosition.x%_unit;
 97				object3d.scenePosition.y -= object3d.scenePosition.y%_unit;
 98				object3d.scenePosition.z -= object3d.scenePosition.z%_unit;
 99			}
100				
101			if(object3d is Mesh && object3d.numChildren == 0 && dovert)
102				snap(Mesh(object3d));
103				 
104			for(var i:uint = 0;i<object3d.numChildren;++i){
105				child = object3d.getChildAt(i);
106				parse(child, dovert);
107			}
108		}
109		
110		private function snap(mesh:Mesh):void
111		{
112			var geometry:Geometry = mesh.geometry;
113			var geometries:Vector.<SubGeometry> = geometry.subGeometries;
114			var numSubGeoms:int = geometries.length;
115			
116			var vertices:Vector.<Number>;
117			var j : uint;
118			var i : uint;
119			var vecLength : uint;
120			var subGeom:SubGeometry;
121			
122			for (i = 0; i < numSubGeoms; ++i){
123				subGeom = SubGeometry(geometries[i]);
124				vertices = subGeom.vertexData;
125				vecLength = vertices.length;
126
127				for (j = 0; j < vecLength;++j){
128					vertices[j] -= vertices[j]%_unit;
129				}
130				
131				subGeom.updateVertexData(vertices);
132			}
133		}
134		 
135	}
136}