PageRenderTime 46ms CodeModel.GetById 28ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/away3d/entities/TextureProjector.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 122 lines | 74 code | 14 blank | 34 comment | 3 complexity | 0c0fd0427a067c62eb7973681ebeaacf MD5 | raw file
  1package away3d.entities
  2{
  3	import away3d.arcane;
  4	import away3d.cameras.lenses.PerspectiveLens;
  5	import away3d.containers.ObjectContainer3D;
  6	import away3d.events.LensEvent;
  7	import away3d.library.assets.AssetType;
  8	import away3d.textures.Texture2DBase;
  9	
 10	import flash.geom.Matrix3D;
 11	
 12	use namespace arcane;
 13	
 14	/**
 15	 * TextureProjector is an object in the scene that can be used to project textures onto geometry. To do so,
 16	 * the object's material must have a ProjectiveTextureMethod method added to it with a TextureProjector object
 17	 * passed in the constructor.
 18	 * This can be used for various effects apart from acting like a normal projector, such as projecting fake shadows
 19	 * unto a surface, the impact of light coming through a stained glass window, ...
 20	 *
 21	 * @see away3d.materials.methods.ProjectiveTextureMethod
 22	 */
 23	public class TextureProjector extends ObjectContainer3D
 24	{
 25		private var _lens:PerspectiveLens;
 26		private var _viewProjectionInvalid:Boolean = true;
 27		private var _viewProjection:Matrix3D = new Matrix3D();
 28		private var _texture:Texture2DBase;
 29		
 30		/**
 31		 * Creates a new TextureProjector object.
 32		 * @param texture The texture to be projected on the geometry. Since any point that is projected out of the range
 33		 * of the projector's cone is clamped to the texture's edges, the edges should be entirely neutral.
 34		 */
 35		public function TextureProjector(texture:Texture2DBase)
 36		{
 37			_lens = new PerspectiveLens();
 38			_lens.addEventListener(LensEvent.MATRIX_CHANGED, onInvalidateLensMatrix, false, 0, true);
 39			_texture = texture;
 40			_lens.aspectRatio = texture.width/texture.height;
 41			rotationX = -90;
 42		}
 43		
 44		/**
 45		 * The aspect ratio of the texture or projection. By default this is the same aspect ratio of the texture (width/height)
 46		 */
 47		public function get aspectRatio():Number
 48		{
 49			return _lens.aspectRatio;
 50		}
 51		
 52		public function set aspectRatio(value:Number):void
 53		{
 54			_lens.aspectRatio = value;
 55		}
 56		
 57		/**
 58		 * The vertical field of view of the projection, or the angle of the cone.
 59		 */
 60		public function get fieldOfView():Number
 61		{
 62			return _lens.fieldOfView;
 63		}
 64		
 65		public function set fieldOfView(value:Number):void
 66		{
 67			_lens.fieldOfView = value;
 68		}
 69		
 70		public override function get assetType():String
 71		{
 72			return AssetType.TEXTURE_PROJECTOR;
 73		}
 74		
 75		/**
 76		 * The texture to be projected on the geometry.
 77		 * IMPORTANT: Since any point that is projected out of the range of the projector's cone is clamped to the texture's edges,
 78		 * the edges should be entirely neutral. Depending on the blend mode, the neutral color is:
 79		 * White for MULTIPLY,
 80		 * Black for ADD,
 81		 * Transparent for MIX
 82		 */
 83		public function get texture():Texture2DBase
 84		{
 85			return _texture;
 86		}
 87		
 88		public function set texture(value:Texture2DBase):void
 89		{
 90			if (value == _texture)
 91				return;
 92			_texture = value;
 93		}
 94		
 95		/**
 96		 * The matrix that projects a point in scene space into the texture coordinates.
 97		 */
 98		public function get viewProjection():Matrix3D
 99		{
100			if (_viewProjectionInvalid) {
101				_viewProjection.copyFrom(inverseSceneTransform);
102				_viewProjection.append(_lens.matrix);
103				_viewProjectionInvalid = false;
104			}
105			return _viewProjection;
106		}
107		
108		/**
109		 * @inheritDoc
110		 */
111		override protected function invalidateSceneTransform():void
112		{
113			super.invalidateSceneTransform();
114			_viewProjectionInvalid = true;
115		}
116		
117		private function onInvalidateLensMatrix(event:LensEvent):void
118		{
119			_viewProjectionInvalid = true;
120		}
121	}
122}