PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/primitives/PrimitiveBase.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 152 lines | 89 code | 21 blank | 42 comment | 9 complexity | d5897dcbd0d400dc0a398ee81cf94bf1 MD5 | raw file
  1package away3d.primitives
  2{
  3	import away3d.arcane;
  4	import away3d.core.base.CompactSubGeometry;
  5	import away3d.core.base.Geometry;
  6	import away3d.core.base.ISubGeometry;
  7	import away3d.errors.AbstractMethodError;
  8	
  9	import flash.geom.Matrix3D;
 10	
 11	use namespace arcane;
 12	
 13	/**
 14	 * PrimitiveBase is an abstract base class for mesh primitives, which are prebuilt simple meshes.
 15	 */
 16	public class PrimitiveBase extends Geometry
 17	{
 18		protected var _geomDirty:Boolean = true;
 19		protected var _uvDirty:Boolean = true;
 20		
 21		private var _subGeometry:CompactSubGeometry;
 22		
 23		/**
 24		 * Creates a new PrimitiveBase object.
 25		 * @param material The material with which to render the object
 26		 */
 27		public function PrimitiveBase()
 28		{
 29			_subGeometry = new CompactSubGeometry();
 30			_subGeometry.autoGenerateDummyUVs = false;
 31			addSubGeometry(_subGeometry);
 32		}
 33		
 34		/**
 35		 * @inheritDoc
 36		 */
 37		override public function get subGeometries():Vector.<ISubGeometry>
 38		{
 39			if (_geomDirty)
 40				updateGeometry();
 41			if (_uvDirty)
 42				updateUVs();
 43			
 44			return super.subGeometries;
 45		}
 46		
 47		/**
 48		 * @inheritDoc
 49		 */
 50		override public function clone():Geometry
 51		{
 52			if (_geomDirty)
 53				updateGeometry();
 54			if (_uvDirty)
 55				updateUVs();
 56			
 57			return super.clone();
 58		}
 59		
 60		/**
 61		 * @inheritDoc
 62		 */
 63		override public function scale(scale:Number):void
 64		{
 65			if (_geomDirty)
 66				updateGeometry();
 67			
 68			super.scale(scale);
 69		}
 70		
 71		/**
 72		 * @inheritDoc
 73		 */
 74		override public function scaleUV(scaleU:Number = 1, scaleV:Number = 1):void
 75		{
 76			if (_uvDirty)
 77				updateUVs();
 78			
 79			super.scaleUV(scaleU, scaleV);
 80		}
 81		
 82		/**
 83		 * @inheritDoc
 84		 */
 85		override public function applyTransformation(transform:Matrix3D):void
 86		{
 87			if (_geomDirty)
 88				updateGeometry();
 89			super.applyTransformation(transform);
 90		}
 91		
 92		/**
 93		 * Builds the primitive's geometry when invalid. This method should not be called directly. The calling should
 94		 * be triggered by the invalidateGeometry method (and in turn by updateGeometry).
 95		 */
 96		protected function buildGeometry(target:CompactSubGeometry):void
 97		{
 98			throw new AbstractMethodError();
 99		}
100		
101		/**
102		 * Builds the primitive's uv coordinates when invalid. This method should not be called directly. The calling
103		 * should be triggered by the invalidateUVs method (and in turn by updateUVs).
104		 */
105		protected function buildUVs(target:CompactSubGeometry):void
106		{
107			throw new AbstractMethodError();
108		}
109		
110		/**
111		 * Invalidates the primitive's geometry, causing it to be updated when requested.
112		 */
113		protected function invalidateGeometry():void
114		{
115			_geomDirty = true;
116		}
117		
118		/**
119		 * Invalidates the primitive's uv coordinates, causing them to be updated when requested.
120		 */
121		protected function invalidateUVs():void
122		{
123			_uvDirty = true;
124		}
125		
126		/**
127		 * Updates the geometry when invalid.
128		 */
129		private function updateGeometry():void
130		{
131			buildGeometry(_subGeometry);
132			_geomDirty = false;
133		}
134		
135		/**
136		 * Updates the uv coordinates when invalid.
137		 */
138		private function updateUVs():void
139		{
140			buildUVs(_subGeometry);
141			_uvDirty = false;
142		}
143		
144		override arcane function validate():void
145		{
146			if (_geomDirty)
147				updateGeometry();
148			if (_uvDirty)
149				updateUVs();
150		}
151	}
152}