PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/src/away3d/materials/methods/HardShadowMapMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 99 lines | 64 code | 15 blank | 20 comment | 0 complexity | 36e092db3bd2a53cbaa0a9733ca08936 MD5 | raw file
 1package away3d.materials.methods
 2{
 3	import away3d.arcane;
 4	import away3d.core.managers.Stage3DProxy;
 5	import away3d.lights.LightBase;
 6	import away3d.materials.compilation.ShaderRegisterCache;
 7	import away3d.materials.compilation.ShaderRegisterElement;
 8	
 9	use namespace arcane;
10
11	/**
12	 * HardShadowMapMethod provides the cheapest shadow map method by using a single tap without any filtering.
13	 */
14	public class HardShadowMapMethod extends SimpleShadowMapMethodBase
15	{
16		/**
17		 * Creates a new HardShadowMapMethod object.
18		 */
19		public function HardShadowMapMethod(castingLight:LightBase)
20		{
21			super(castingLight);
22		}
23		
24		/**
25		 * @inheritDoc
26		 */
27		override protected function getPlanarFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
28		{
29			var depthMapRegister:ShaderRegisterElement = regCache.getFreeTextureReg();
30			var decReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
31			// needs to be reserved anyway. DO NOT REMOVE
32			var dataReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
33			// TODO not used
34			dataReg = dataReg;
35			var depthCol:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
36			var code:String = "";
37			
38			vo.fragmentConstantsIndex = decReg.index*4;
39			vo.texturesIndex = depthMapRegister.index;
40			
41			code += "tex " + depthCol + ", " + _depthMapCoordReg + ", " + depthMapRegister + " <2d, nearest, clamp>\n" +
42				"dp4 " + depthCol + ".z, " + depthCol + ", " + decReg + "\n" +
43				"slt " + targetReg + ".w, " + _depthMapCoordReg + ".z, " + depthCol + ".z\n"; // 0 if in shadow
44			
45			return code;
46		}
47
48		/**
49		 * @inheritDoc
50		 */
51		override protected function getPointFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
52		{
53			var depthMapRegister:ShaderRegisterElement = regCache.getFreeTextureReg();
54			var decReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
55			var epsReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
56			var posReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
57			var depthSampleCol:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
58			regCache.addFragmentTempUsages(depthSampleCol, 1);
59			var lightDir:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
60			var code:String = "";
61			
62			vo.fragmentConstantsIndex = decReg.index*4;
63			vo.texturesIndex = depthMapRegister.index;
64			
65			code += "sub " + lightDir + ", " + _sharedRegisters.globalPositionVarying + ", " + posReg + "\n" +
66				"dp3 " + lightDir + ".w, " + lightDir + ".xyz, " + lightDir + ".xyz\n" +
67				"mul " + lightDir + ".w, " + lightDir + ".w, " + posReg + ".w\n" +
68				"nrm " + lightDir + ".xyz, " + lightDir + ".xyz\n" +
69				
70				"tex " + depthSampleCol + ", " + lightDir + ", " + depthMapRegister + " <cube, nearest, clamp>\n" +
71				"dp4 " + depthSampleCol + ".z, " + depthSampleCol + ", " + decReg + "\n" +
72				"add " + targetReg + ".w, " + lightDir + ".w, " + epsReg + ".x\n" +    // offset by epsilon
73				
74				"slt " + targetReg + ".w, " + targetReg + ".w, " + depthSampleCol + ".z\n"; // 0 if in shadow
75			
76			regCache.removeFragmentTempUsage(depthSampleCol);
77			
78			return code;
79		}
80
81		/**
82		 * @inheritDoc
83		 */
84		override arcane function getCascadeFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, decodeRegister:ShaderRegisterElement, depthTexture:ShaderRegisterElement, depthProjection:ShaderRegisterElement, targetRegister:ShaderRegisterElement):String
85		{
86			var temp:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
87			return "tex " + temp + ", " + depthProjection + ", " + depthTexture + " <2d, nearest, clamp>\n" +
88				"dp4 " + temp + ".z, " + temp + ", " + decodeRegister + "\n" +
89				"slt " + targetRegister + ".w, " + depthProjection + ".z, " + temp + ".z\n"; // 0 if in shadow
90		}
91
92		/**
93		 * @inheritDoc
94		 */
95		override arcane function activateForCascade(vo:MethodVO, stage3DProxy:Stage3DProxy):void
96		{
97		}
98	}
99}