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

Revision 2186, 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.00005
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.2,0.2,0.2,1)
22
23float4 shadowMapDepth(LightVPos_OUT IN,
24                      uniform sampler2D shadowMap) : COLOR
25{
26  float4 light = shadowColor;     
27       
28  if(IN.LightVPos.z > 0.0)
29  {
30        float4 pos = (IN.LightVPos / IN.LightVPos.w);
31               
32        pos.xy = (pos.xy + 1.0) / 2.0;
33        pos.y = 1.0 - pos.y;
34        float4 storedDepth = tex2D(shadowMap, pos.xy);
35        light = max(storedDepth.r + DEPTH_BIAS >= pos.z, shadowColor);                 
36  }             
37  return light;
38}
39
40float4 shadowMapDepth_Variance(LightVPos_OUT IN,
41                               uniform sampler2D shadowMap) : COLOR
42{
43  float4 light = float4(1,1,1,1);
44       
45  float4 pos = (IN.LightVPos / IN.LightVPos.w);
46 
47  if( pos.z > 0.0)
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  else
66   light = 0;   
67       
68  return shadowColor + (1 - shadowColor) * light;       
69 
70}
71
72float4 shadowMapDist(LightCPos_OUT IN,
73                                        uniform float lightFarPlane,
74                                         uniform sampler2D shadowMap) : COLOR
75{
76  float4 light = shadowColor; 
77  if( IN.LightVPos.z > 0.0)
78  {     
79    float4 pos = (IN.LightVPos / IN.LightVPos.w);
80        //float d = length(pos.xy);
81    //light = saturate((1.0 - d)/0.05);         
82    //if(d <= 1.0)
83    //{
84                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
85                pos.xy = (pos.xy + 1.0) / 2.0;
86                pos.y = 1.0 - pos.y;
87                float4 storedDist = tex2D(shadowMap, pos.xy);
88                dist -= DIST_BIAS;
89                light = max(dist <= storedDist.r, shadowColor);         
90    //}
91  }
92       
93  return light;
94}
95
96float4 shadowMapDist_Variance(LightCPos_OUT IN,
97                              uniform float lightFarPlane,
98                              uniform sampler2D shadowMap) : COLOR
99{
100  float4 light = float4(1,1,1,1);
101 
102  if( IN.LightVPos.z > 0.0)
103  {     
104    float4 pos = (IN.LightVPos / IN.LightVPos.w);
105        float d = length(pos.xy);
106    light = saturate((1.0 - d)/0.05);           
107    if(d <= 1.0)
108    {
109                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
110                pos.xy = (pos.xy + 1.0) / 2.0;
111                pos.y = 1.0 - pos.y;
112                float4 storedDist = tex2D(shadowMap, pos.xy);
113                dist -= DIST_BIAS_VSM;
114                float lit_factor = light * (dist <= storedDist.r);     
115         
116                float M1 = storedDist.r;
117                float M2 = storedDist.g;
118                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
119                float m_d = M1 - dist;
120        float pmax = v2 / (v2 + m_d * m_d);
121                                               
122        // Adjust the light color based on the shadow attenuation
123        light = max(lit_factor, pmax);         
124    }
125  }
126  else
127   light = 0;   
128       
129  return shadowColor + (1 - shadowColor) * light;
130}
Note: See TracBrowser for help on using the repository browser.