PageRenderTime 390ms CodeModel.GetById 181ms app.highlight 9ms RepoModel.GetById 197ms app.codeStats 1ms

/src/away3d/core/base/Geometry.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 171 lines | 102 code | 23 blank | 46 comment | 11 complexity | 94a0aaa748329281f101aa9c205dbf1c MD5 | raw file
  1package away3d.core.base
  2{
  3	import away3d.arcane;
  4	import away3d.events.GeometryEvent;
  5	import away3d.library.assets.AssetType;
  6	import away3d.library.assets.IAsset;
  7	import away3d.library.assets.NamedAssetBase;
  8	
  9	import flash.geom.Matrix3D;
 10	
 11	use namespace arcane;
 12	
 13	/**
 14	 * Geometry is a collection of SubGeometries, each of which contain the actual geometrical data such as vertices,
 15	 * normals, uvs, etc. It also contains a reference to an animation class, which defines how the geometry moves.
 16	 * A Geometry object is assigned to a Mesh, a scene graph occurence of the geometry, which in turn assigns
 17	 * the SubGeometries to its respective SubMesh objects.
 18	 *
 19	 *
 20	 *
 21	 * @see away3d.core.base.SubGeometry
 22	 * @see away3d.scenegraph.Mesh
 23	 */
 24	public class Geometry extends NamedAssetBase implements IAsset
 25	{
 26		private var _subGeometries:Vector.<ISubGeometry>;
 27		
 28		public function get assetType():String
 29		{
 30			return AssetType.GEOMETRY;
 31		}
 32		
 33		/**
 34		 * A collection of SubGeometry objects, each of which contain geometrical data such as vertices, normals, etc.
 35		 */
 36		public function get subGeometries():Vector.<ISubGeometry>
 37		{
 38			return _subGeometries;
 39		}
 40		
 41		/**
 42		 * Creates a new Geometry object.
 43		 */
 44		public function Geometry()
 45		{
 46			_subGeometries = new Vector.<ISubGeometry>();
 47		}
 48		
 49		public function applyTransformation(transform:Matrix3D):void
 50		{
 51			var len:uint = _subGeometries.length;
 52			for (var i:int = 0; i < len; ++i)
 53				_subGeometries[i].applyTransformation(transform);
 54		}
 55		
 56		/**
 57		 * Adds a new SubGeometry object to the list.
 58		 * @param subGeometry The SubGeometry object to be added.
 59		 */
 60		public function addSubGeometry(subGeometry:ISubGeometry):void
 61		{
 62			_subGeometries.push(subGeometry);
 63			
 64			subGeometry.parentGeometry = this;
 65			if (hasEventListener(GeometryEvent.SUB_GEOMETRY_ADDED))
 66				dispatchEvent(new GeometryEvent(GeometryEvent.SUB_GEOMETRY_ADDED, subGeometry));
 67			
 68			invalidateBounds(subGeometry);
 69		}
 70		
 71		/**
 72		 * Removes a new SubGeometry object from the list.
 73		 * @param subGeometry The SubGeometry object to be removed.
 74		 */
 75		public function removeSubGeometry(subGeometry:ISubGeometry):void
 76		{
 77			_subGeometries.splice(_subGeometries.indexOf(subGeometry), 1);
 78			subGeometry.parentGeometry = null;
 79			if (hasEventListener(GeometryEvent.SUB_GEOMETRY_REMOVED))
 80				dispatchEvent(new GeometryEvent(GeometryEvent.SUB_GEOMETRY_REMOVED, subGeometry));
 81			
 82			invalidateBounds(subGeometry);
 83		}
 84		
 85		/**
 86		 * Clones the geometry.
 87		 * @return An exact duplicate of the current Geometry object.
 88		 */
 89		public function clone():Geometry
 90		{
 91			var clone:Geometry = new Geometry();
 92			var len:uint = _subGeometries.length;
 93			for (var i:int = 0; i < len; ++i)
 94				clone.addSubGeometry(_subGeometries[i].clone());
 95			return clone;
 96		}
 97		
 98		/**
 99		 * Scales the geometry.
100		 * @param scale The amount by which to scale.
101		 */
102		public function scale(scale:Number):void
103		{
104			var numSubGeoms:uint = _subGeometries.length;
105			for (var i:uint = 0; i < numSubGeoms; ++i)
106				_subGeometries[i].scale(scale);
107		}
108		
109		/**
110		 * Clears all resources used by the Geometry object, including SubGeometries.
111		 */
112		public function dispose():void
113		{
114			var numSubGeoms:uint = _subGeometries.length;
115			
116			for (var i:uint = 0; i < numSubGeoms; ++i) {
117				var subGeom:ISubGeometry = _subGeometries[0];
118				removeSubGeometry(subGeom);
119				subGeom.dispose();
120			}
121		}
122		
123		/**
124		 * Scales the uv coordinates (tiling)
125		 * @param scaleU The amount by which to scale on the u axis. Default is 1;
126		 * @param scaleV The amount by which to scale on the v axis. Default is 1;
127		 */
128		public function scaleUV(scaleU:Number = 1, scaleV:Number = 1):void
129		{
130			var numSubGeoms:uint = _subGeometries.length;
131			for (var i:uint = 0; i < numSubGeoms; ++i)
132				_subGeometries[i].scaleUV(scaleU, scaleV);
133		}
134		
135		/**
136		 * Updates the SubGeometries so all vertex data is represented in different buffers.
137		 * Use this for compatibility with Pixel Bender and PBPickingCollider
138		 */
139		public function convertToSeparateBuffers():void
140		{
141			var subGeom:ISubGeometry;
142			var numSubGeoms:int = _subGeometries.length;
143			var _removableCompactSubGeometries:Vector.<CompactSubGeometry> = new Vector.<CompactSubGeometry>();
144			
145			for (var i:int = 0; i < numSubGeoms; ++i) {
146				subGeom = _subGeometries[i];
147				if (subGeom is SubGeometry)
148					continue;
149				
150				_removableCompactSubGeometries.push(subGeom);
151				addSubGeometry(subGeom.cloneWithSeperateBuffers());
152			}
153			
154			for each (var s:CompactSubGeometry in _removableCompactSubGeometries) {
155				removeSubGeometry(s);
156				s.dispose();
157			}
158		}
159		
160		arcane function validate():void
161		{
162			// To be overridden when necessary
163		}
164		
165		arcane function invalidateBounds(subGeom:ISubGeometry):void
166		{
167			if (hasEventListener(GeometryEvent.BOUNDS_INVALID))
168				dispatchEvent(new GeometryEvent(GeometryEvent.BOUNDS_INVALID, subGeom));
169		}
170	}
171}