PageRenderTime 356ms CodeModel.GetById 141ms app.highlight 91ms RepoModel.GetById 120ms app.codeStats 0ms

/src/away3d/materials/methods/TripleFilteredShadowMapMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 194 lines | 146 code | 36 blank | 12 comment | 1 complexity | 414565af80f14e1e1c22fdc3420bc425 MD5 | raw file
  1package away3d.materials.methods
  2{
  3	import away3d.arcane;
  4	import away3d.lights.DirectionalLight;
  5	import away3d.lights.PointLight;
  6	import away3d.materials.compilation.ShaderRegisterCache;
  7	import away3d.materials.compilation.ShaderRegisterElement;
  8	
  9	use namespace arcane;
 10	
 11	[Deprecated(message="Please consider any of the superior shadow map methods: SoftShadowMapMethod or DitheredShadowMapMethod")]
 12	public class TripleFilteredShadowMapMethod extends SimpleShadowMapMethodBase
 13	{
 14		/**
 15		 * Creates a new BasicDiffuseMethod object.
 16		 *
 17		 * @param castingLight The light casting the shadow
 18		 */
 19		public function TripleFilteredShadowMapMethod(castingLight:DirectionalLight)
 20		{
 21			super(castingLight);
 22			if (castingLight is PointLight)
 23				throw new Error("FilteredShadowMapMethod not supported for Point Lights");
 24		}
 25		
 26		override arcane function initConstants(vo:MethodVO):void
 27		{
 28			super.initConstants(vo);
 29			var fragmentData:Vector.<Number> = vo.fragmentData;
 30			var index:int = vo.fragmentConstantsIndex;
 31			
 32			fragmentData[index + 8] = 1/3;
 33			fragmentData[index + 9] = castingLight.shadowMapper.depthMapSize;
 34			fragmentData[index + 10] = 1/castingLight.shadowMapper.depthMapSize;
 35		}
 36		
 37		/**
 38		 * @inheritDoc
 39		 */
 40		override protected function getPlanarFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
 41		{
 42			var depthMapRegister:ShaderRegisterElement = regCache.getFreeTextureReg();
 43			var decReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 44			var dataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 45			var customDataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 46			var depthCol:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
 47			var uvReg:ShaderRegisterElement;
 48			var code:String;
 49			vo.fragmentConstantsIndex = decReg.index*4;
 50			
 51			regCache.addFragmentTempUsages(depthCol, 1);
 52			
 53			uvReg = regCache.getFreeFragmentVectorTemp();
 54			regCache.addFragmentTempUsages(uvReg, 1);
 55			
 56			var viewDirReg:ShaderRegisterElement = _sharedRegisters.viewDirFragment;
 57			
 58			code = "mov " + uvReg + ", " + _depthMapCoordReg + "\n" +
 59				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 60				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 61				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z	\n" +    // 0 if in shadow
 62				
 63				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z		\n" + // (1, 0)
 64				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 65				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 66				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z		\n" +    // 0 if in shadow
 67				
 68				"div " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z		\n" +
 69				"frc " + depthCol + ".x, " + depthCol + ".x		\n" +
 70				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z		\n" +
 71				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x		\n" +
 72				"add " + viewDirReg + ".w, " + uvReg + ".z, " + uvReg + ".w		\n" +
 73				
 74				"sub " + uvReg + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z	\n" +
 75				"add " + uvReg + ".y, " + _depthMapCoordReg + ".y, " + customDataReg + ".z	\n" +	// (0, 1)
 76				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 77				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 78				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z		\n" +   // 0 if in shadow
 79				
 80				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z						\n" +	// (1, 1)
 81				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
 82				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
 83				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z			\n" +   // 0 if in shadow
 84				
 85				// recalculate fraction, since we ran out of registers :(
 86				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".y			\n" +
 87				"frc " + depthCol + ".x, " + depthCol + ".x								\n" +
 88				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z					\n" +
 89				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x				\n" +
 90				"add " + uvReg + ".w, " + uvReg + ".z, " + uvReg + ".w					\n" +
 91				
 92				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".y, " + customDataReg + ".y		\n" +
 93				"frc " + depthCol + ".x, " + depthCol + ".x								\n" +
 94				"sub " + uvReg + ".w, " + uvReg + ".w, " + viewDirReg + ".w		\n" +
 95				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x					\n" +
 96				"add " + targetReg + ".w, " + viewDirReg + ".w, " + uvReg + ".w	\n" +
 97				
 98				
 99				"sub " + uvReg + ".xy, " + _depthMapCoordReg + ".xy, " + customDataReg + ".zz		\n" +
100				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
101				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
102				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z	\n" +   // 0 if in shadow
103				
104				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z			\n" +	// (1, 0)
105				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
106				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
107				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z					\n" +   // 0 if in shadow
108				
109				"div " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z		\n" +
110				"frc " + depthCol + ".x, " + depthCol + ".x								\n" +
111				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z					\n" +
112				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x					\n" +
113				"add " + viewDirReg + ".w, " + uvReg + ".z, " + uvReg + ".w	\n" +
114				
115				"mov " + uvReg + ".x, " + _depthMapCoordReg + ".x							\n" +
116				"add " + uvReg + ".y, " + uvReg + ".y, " + customDataReg + ".z				\n" +	// (0, 1)
117				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
118				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
119				"sub " + depthCol + ".z, " + depthCol + ".z, " + dataReg + ".x		\n" +	// offset by epsilon
120				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z		\n" +   // 0 if in shadow
121				
122				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z				\n" +	// (1, 1)
123				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
124				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
125				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z		\n" +   // 0 if in shadow
126				
127				// recalculate fraction, since we ran out of registers :(
128				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".y		\n" +
129				"frc " + depthCol + ".x, " + depthCol + ".x								\n" +
130				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z					\n" +
131				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x				\n" +
132				"add " + uvReg + ".w, " + uvReg + ".z, " + uvReg + ".w					\n" +
133				
134				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".y, " + customDataReg + ".y					\n" +
135				"frc " + depthCol + ".x, " + depthCol + ".x											\n" +
136				"sub " + uvReg + ".w, " + uvReg + ".w, " + viewDirReg + ".w				\n" +
137				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x							\n" +
138				"add " + viewDirReg + ".w, " + viewDirReg + ".w, " + uvReg + ".w	\n" +
139				"add " + targetReg + ".w, " + targetReg + ".w, " + viewDirReg + ".w		\n";
140			
141			code += "add " + uvReg + ".xy, " + _depthMapCoordReg + ".xy, " + customDataReg + ".zz						\n" +	// (1, 0)
142				//					"mov " + uvReg + ".y, " + _depthMapVar + ".y										\n" +	// (1, 0)
143				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
144				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
145				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z					\n" +   // 0 if in shadow
146				
147				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z							\n" +	// (2, 0)
148				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
149				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
150				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z					\n" +   // 0 if in shadow
151				
152				"div " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".z					\n" +
153				"frc " + depthCol + ".x, " + depthCol + ".x											\n" +
154				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z								\n" +
155				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x							\n" +
156				"add " + viewDirReg + ".w, " + uvReg + ".z, " + uvReg + ".w				\n" +
157				
158				"add " + uvReg + ".xy, " + _depthMapCoordReg + ".xy, " + customDataReg + ".zz				\n" +	// (0, 1)
159				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
160				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
161				"slt " + uvReg + ".z, " + _depthMapCoordReg + ".z, " + depthCol + ".z					\n" +   // 0 if in shadow
162				
163				"add " + uvReg + ".x, " + uvReg + ".x, " + customDataReg + ".z							\n" +	// (1, 1)
164				"tex " + depthCol + ", " + uvReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
165				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
166				"slt " + uvReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z						\n" +   // 0 if in shadow
167				
168				// recalculate fraction, since we ran out of registers :(
169				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".x, " + customDataReg + ".y		\n" +
170				"frc " + depthCol + ".x, " + depthCol + ".x											\n" +
171				"sub " + uvReg + ".w, " + uvReg + ".w, " + uvReg + ".z								\n" +
172				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x							\n" +
173				"add " + uvReg + ".w, " + uvReg + ".z, " + uvReg + ".w								\n" +
174				
175				"mul " + depthCol + ".x, " + _depthMapCoordReg + ".y, " + customDataReg + ".y			\n" +
176				"frc " + depthCol + ".x, " + depthCol + ".x											\n" +
177				"sub " + uvReg + ".w, " + uvReg + ".w, " + viewDirReg + ".w				\n" +
178				"mul " + uvReg + ".w, " + uvReg + ".w, " + depthCol + ".x							\n" +
179				"add " + viewDirReg + ".w, " + viewDirReg + ".w, " + uvReg + ".w	\n" +
180				"add " + targetReg + ".w, " + targetReg + ".w, " + viewDirReg + ".w		\n" +
181				
182				
183				"mul " + targetReg + ".w, " + targetReg + ".w, " + customDataReg + ".x					\n";
184			
185			regCache.removeFragmentTempUsage(depthCol);
186			regCache.removeFragmentTempUsage(uvReg);
187			
188			vo.texturesIndex = depthMapRegister.index;
189			
190			return code;
191		}
192	
193	}
194}