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

Revision 2024, 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.001
18#define DIST_BIAS_VSM   0.001
19#define DIST_EPSILON    0.001
20
21const float4 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  float4 pos = (IN.LightVPos / IN.LightVPos.w);
28  if( pos.z > 0.0)
29  {
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
34    if(pos.z - DEPTH_BIAS <= storedDepth.r)
35      light = 1;
36  }
37 
38  return light;
39}
40
41float4 shadowMapDepth_Variance(LightVPos_OUT IN,
42                               uniform sampler2D shadowMap) : COLOR
43{
44  float4 light = float4(1,1,1,1);
45       
46  float4 pos = (IN.LightVPos / IN.LightVPos.w);
47 
48  if( pos.z > 0.0)
49  {     
50    {
51        float depth = pos.z;
52        pos.xy = (pos.xy + 1.0) / 2.0;
53        pos.y = 1.0 - pos.y;
54        float4 storedDepth = tex2D(shadowMap, pos.xy);
55        depth -= DEPTH_BIAS_VSM;
56        float lit_factor = light * (depth <= storedDepth.r);   
57         
58        float M1 = storedDepth.r;
59        float M2 = storedDepth.g;
60        float v2 = min(max(M2 - M1 * M1, 0.0) +  DEPTH_EPSILON, 1.0);
61        float m_d = M1 - depth;
62        float pmax = v2 / (v2 + m_d * m_d);
63                                               
64        // Adjust the light color based on the shadow attenuation
65        light = max(lit_factor, pmax);
66       
67     }
68  }
69  else
70   light = 0;   
71       
72  return shadowColor + (1 - shadowColor) * light;       
73 
74}
75
76float4 shadowMapDist(LightCPos_OUT IN,                     
77                     uniform sampler2D shadowMap) : COLOR
78{
79  float4 light = shadowColor;   
80  float4 pos = (IN.LightVPos / IN.LightVPos.w);
81 
82  if( pos.z > 0.0)
83  {     
84    //float d = length(pos.xy);
85    //light = saturate((1.0 - d)/0.05);         
86    //if(d <= 1.0)
87    {
88        float dist = length(IN.LightCPos.xyz);
89        pos.xy = (pos.xy + 1.0) / 2.0;
90        pos.y = 1.0 - pos.y;
91        float storedDist = tex2D(shadowMap, pos.xy).r;
92        dist -= DIST_BIAS;
93
94        if(dist <= storedDist);
95          light = 1;
96     }
97  } 
98       
99  return light;
100}
101
102float4 shadowMapDist_Variance(LightCPos_OUT IN,
103                              uniform float lightFarPlane,
104                              uniform sampler2D shadowMap) : COLOR
105{
106  float4 light = float4(1,1,1,1);
107       
108  float4 pos = (IN.LightVPos / IN.LightVPos.w);
109 
110  if( pos.z > 0.0)
111  {     
112    //float d = length(pos.xy);
113    //light = saturate((1.0 - d)/0.05);         
114    //if(d <= 1.0)
115    {
116        float dist = length(IN.LightCPos.xyz) / lightFarPlane;
117        pos.xy = (pos.xy + 1.0) / 2.0;
118        pos.y = 1.0 - pos.y;
119        float4 storedDist = tex2D(shadowMap, pos.xy);
120        dist -= DIST_BIAS_VSM;
121        float lit_factor = light * (dist <= storedDist.r);     
122         
123        float M1 = storedDist.r;
124        float M2 = storedDist.g;
125        float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
126        float m_d = M1 - dist;
127        float pmax = v2 / (v2 + m_d * m_d);
128                                               
129        // Adjust the light color based on the shadow attenuation
130        light = max(lit_factor, pmax);
131       
132     }
133  }
134  else
135   light = 0;   
136       
137  return shadowColor + (1 - shadowColor) * light;
138}
Note: See TracBrowser for help on using the repository browser.