PageRenderTime 38ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/src/away3d/lights/shadowmaps/ShadowMapperBase.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 143 lines | 113 code | 21 blank | 9 comment | 12 complexity | 1ab77f6805c23e5ae1265cff2a29b672 MD5 | raw file
  1package away3d.lights.shadowmaps
  2{
  3	import away3d.arcane;
  4	import away3d.cameras.Camera3D;
  5	import away3d.containers.Scene3D;
  6	import away3d.core.managers.Stage3DProxy;
  7	import away3d.core.render.DepthRenderer;
  8	import away3d.core.traverse.EntityCollector;
  9	import away3d.core.traverse.ShadowCasterCollector;
 10	import away3d.errors.AbstractMethodError;
 11	import away3d.lights.LightBase;
 12	import away3d.textures.RenderTexture;
 13	import away3d.textures.TextureProxyBase;
 14	
 15	import flash.display3D.textures.TextureBase;
 16	
 17	use namespace arcane;
 18	
 19	public class ShadowMapperBase
 20	{
 21		protected var _casterCollector:ShadowCasterCollector;
 22		
 23		private var _depthMap:TextureProxyBase;
 24		protected var _depthMapSize:uint = 2048;
 25		protected var _light:LightBase;
 26		private var _explicitDepthMap:Boolean;
 27		private var _autoUpdateShadows:Boolean = true;
 28		arcane var _shadowsInvalid:Boolean;
 29		
 30		public function ShadowMapperBase()
 31		{
 32			_casterCollector = createCasterCollector();
 33		}
 34		
 35		protected function createCasterCollector():ShadowCasterCollector
 36		{
 37			return new ShadowCasterCollector();
 38		}
 39		
 40		public function get autoUpdateShadows():Boolean
 41		{
 42			return _autoUpdateShadows;
 43		}
 44		
 45		public function set autoUpdateShadows(value:Boolean):void
 46		{
 47			_autoUpdateShadows = value;
 48		}
 49		
 50		public function updateShadows():void
 51		{
 52			_shadowsInvalid = true;
 53		}
 54		
 55		/**
 56		 * This is used by renderers that can support depth maps to be shared across instances
 57		 * @param depthMap
 58		 */
 59		arcane function setDepthMap(depthMap:TextureProxyBase):void
 60		{
 61			if (_depthMap == depthMap)
 62				return;
 63			if (_depthMap && !_explicitDepthMap)
 64				_depthMap.dispose();
 65			_depthMap = depthMap;
 66			if (_depthMap) {
 67				_explicitDepthMap = true;
 68				_depthMapSize = _depthMap.width;
 69			} else
 70				_explicitDepthMap = false;
 71		}
 72		
 73		public function get light():LightBase
 74		{
 75			return _light;
 76		}
 77		
 78		public function set light(value:LightBase):void
 79		{
 80			_light = value;
 81		}
 82		
 83		public function get depthMap():TextureProxyBase
 84		{
 85			return _depthMap ||= createDepthTexture();
 86		}
 87		
 88		public function get depthMapSize():uint
 89		{
 90			return _depthMapSize;
 91		}
 92		
 93		public function set depthMapSize(value:uint):void
 94		{
 95			if (value == _depthMapSize)
 96				return;
 97			_depthMapSize = value;
 98			
 99			if (_explicitDepthMap)
100				throw Error("Cannot set depth map size for the current renderer.");
101			else if (_depthMap) {
102				_depthMap.dispose();
103				_depthMap = null;
104			}
105		}
106		
107		public function dispose():void
108		{
109			_casterCollector = null;
110			if (_depthMap && !_explicitDepthMap)
111				_depthMap.dispose();
112			_depthMap = null;
113		}
114		
115		protected function createDepthTexture():TextureProxyBase
116		{
117			return new RenderTexture(_depthMapSize, _depthMapSize);
118		}
119		
120		/**
121		 * Renders the depth map for this light.
122		 * @param entityCollector The EntityCollector that contains the original scene data.
123		 * @param renderer The DepthRenderer to render the depth map.
124		 */
125		arcane function renderDepthMap(stage3DProxy:Stage3DProxy, entityCollector:EntityCollector, renderer:DepthRenderer):void
126		{
127			_shadowsInvalid = false;
128			updateDepthProjection(entityCollector.camera);
129			_depthMap ||= createDepthTexture();
130			drawDepthMap(_depthMap.getTextureForStage3D(stage3DProxy), entityCollector.scene, renderer);
131		}
132		
133		protected function updateDepthProjection(viewCamera:Camera3D):void
134		{
135			throw new AbstractMethodError();
136		}
137		
138		protected function drawDepthMap(target:TextureBase, scene:Scene3D, renderer:DepthRenderer):void
139		{
140			throw new AbstractMethodError();
141		}
142	}
143}