source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPBasic/GTPShadowMap_PS.hlsl @ 2179

Revision 2179, 3.3 KB checked in by szirmay, 17 years ago (diff)
Line 
1struct LightVPos_OUT
2{
3 float4 VPos : POSITION;
4 float4 LightVPos : TEXCOORD0;
5};
6
7struct LightCPos_OUT
8{
9 float4 VPos : POSITION;
10 float4 LightVPos : TEXCOORD0;
11 float4 LightCPos : TEXCOORD1;
12};
13
14#define DEPTH_BIAS      0.001
15#define DEPTH_BIAS_VSM  0.001
16#define DEPTH_EPSILON   0.001
17#define DIST_BIAS       0.1
18#define DIST_BIAS_VSM   0.001
19#define DIST_EPSILON    0.001
20
21#define shadowColor float4(0.85,0.85,0.85,1)
22
23float4 shadowMapDepth(LightVPos_OUT IN,
24                      uniform sampler2D shadowMap) : COLOR
25{
26  float4 light = shadowColor;   
27  if( IN.LightVPos.z > 0.0)
28  {
29    float4 pos = (IN.LightVPos / IN.LightVPos.w);
30        pos.xy = (pos.xy + 1.0) / 2.0;
31    pos.y = 1.0 - pos.y;
32    float storedDepth = tex2D(shadowMap, pos.xy).r;
33        pos.z -= DIST_BIAS;
34        light = max(pos.z <= storedDepth.r, shadowColor);           
35  }
36  return light;
37}
38
39float4 shadowMapDepth_Variance(LightVPos_OUT IN,
40                               uniform sampler2D shadowMap) : COLOR
41{
42  float4 light = float4(1,1,1,1);
43       
44  float4 pos = (IN.LightVPos / IN.LightVPos.w);
45 
46  if( pos.z > 0.0)
47  {     
48    {
49        float depth = pos.z;
50        pos.xy = (pos.xy + 1.0) / 2.0;
51        pos.y = 1.0 - pos.y;
52        float4 storedDepth = tex2D(shadowMap, pos.xy);
53        depth -= DEPTH_BIAS_VSM;
54        float lit_factor = light * (depth <= storedDepth.r);   
55         
56        float M1 = storedDepth.r;
57        float M2 = storedDepth.g;
58        float v2 = min(max(M2 - M1 * M1, 0.0) +  DEPTH_EPSILON, 1.0);
59        float m_d = M1 - depth;
60        float pmax = v2 / (v2 + m_d * m_d);
61                                               
62        // Adjust the light color based on the shadow attenuation
63        light = max(lit_factor, pmax);
64       
65     }
66  }
67  else
68   light = 0;   
69       
70  return shadowColor + (1 - shadowColor) * light;       
71 
72}
73
74float4 shadowMapDist(LightCPos_OUT IN,
75                                        //uniform float lightFarPlane,
76                                         uniform sampler2D shadowMap) : COLOR
77{
78  float4 light = shadowColor;
79 
80  if( IN.LightVPos.z > 0.0)
81  {     
82    float4 pos = (IN.LightVPos / IN.LightVPos.w);
83        //float d = length(pos.xy);
84    //light = saturate((1.0 - d)/0.05);         
85    //if(d <= 1.0)
86    //{
87                float dist = length(IN.LightCPos.xyz) ;
88                pos.xy = (pos.xy + 1.0) / 2.0;
89                pos.y = 1.0 - pos.y;
90                float4 storedDist = tex2D(shadowMap, pos.xy);
91                dist -= DIST_BIAS;
92                light = max(dist <= storedDist.r, shadowColor);         
93    //}
94  }
95       
96  return light;
97}
98
99float4 shadowMapDist_Variance(LightCPos_OUT IN,
100                              uniform float lightFarPlane,
101                              uniform sampler2D shadowMap) : COLOR
102{
103  float4 light = float4(1,1,1,1);
104 
105  if( IN.LightVPos.z > 0.0)
106  {     
107    float4 pos = (IN.LightVPos / IN.LightVPos.w);
108        float d = length(pos.xy);
109    light = saturate((1.0 - d)/0.05);           
110    if(d <= 1.0)
111    {
112                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
113                pos.xy = (pos.xy + 1.0) / 2.0;
114                pos.y = 1.0 - pos.y;
115                float4 storedDist = tex2D(shadowMap, pos.xy);
116                dist -= DIST_BIAS_VSM;
117                float lit_factor = light * (dist <= storedDist.r);     
118         
119                float M1 = storedDist.r;
120                float M2 = storedDist.g;
121                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
122                float m_d = M1 - dist;
123        float pmax = v2 / (v2 + m_d * m_d);
124                                               
125        // Adjust the light color based on the shadow attenuation
126        light = max(lit_factor, pmax);         
127    }
128  }
129  else
130   light = 0;   
131       
132  return shadowColor + (1 - shadowColor) * light;
133}
Note: See TracBrowser for help on using the repository browser.