source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/SpaceStation/Media/GTPBasic/GTPShadowMap_PS.hlsl @ 3255

Revision 3255, 4.8 KB checked in by szirmay, 15 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.01
15#define DEPTH_BIAS_VSM  0.00005
16#define DEPTH_EPSILON   0.01
17#define DIST_BIAS       0.005
18#define DIST_BIAS_VSM   0.0005
19#define DIST_EPSILON    0.001
20
21//#define shadowColor float4(0.95,0.95,0.95,1)
22#define shadowColor float4(0.8,0.8,0.8,1)
23#define shadowColor2 float4(0.2,0.2,0.2,1)
24//#define shadowColor float4(0.0,0.0,0.0,1)
25
26float4 shadowMapDepth(LightVPos_OUT IN,
27                      uniform sampler2D shadowMap) : COLOR
28{
29  float4 light = shadowColor;     
30       
31 // if(IN.LightVPos.z > 0.0)
32  {
33        float4 pos = (IN.LightVPos / IN.LightVPos.w);
34               
35        pos.xy = (pos.xy + 1.0) / 2.0;
36        pos.y = 1.0 - pos.y;
37        float storedDepth = abs(tex2D(shadowMap, pos.xy).r);
38        //light = (pos.z - storedDepth.r)*100.0;
39        light = max(storedDepth + DEPTH_BIAS > pos.z, shadowColor2);                   
40  }             
41  return light;
42}
43
44float4 shadowMapCameraDepth(LightCPos_OUT IN,
45                      uniform sampler2D shadowMap) : COLOR
46{
47  float4 light = shadowColor;     
48       
49 
50        float4 pos = (IN.LightVPos / IN.LightVPos.w);
51               
52        pos.xy = (pos.xy + 1.0) / 2.0;
53        pos.y = 1.0 - pos.y;
54        float storedDepth = tex2D(shadowMap, pos.xy).r;
55        //light = (pos.z - storedDepth.r)*100.0;
56        float depth = -IN.LightCPos.z;
57        light = max(storedDepth + DEPTH_BIAS > depth, shadowColor2);                   
58        return storedDepth;
59        return light;   
60}
61
62float4 shadowMapDepth_Variance(LightVPos_OUT IN,
63                               uniform sampler2D shadowMap) : COLOR
64{
65  float4 light = float4(1,1,1,1);
66       
67  float4 pos = (IN.LightVPos / IN.LightVPos.w);
68 
69  if( pos.z > 0.0)
70  {         
71        float depth = pos.z;
72        pos.xy = (pos.xy + 1.0) / 2.0;
73        pos.y = 1.0 - pos.y;
74        float4 storedDepth = tex2D(shadowMap, pos.xy);
75        depth -= DEPTH_BIAS_VSM;
76        float lit_factor = light * (depth <= storedDepth.r);   
77         
78        float M1 = storedDepth.r;
79        float M2 = storedDepth.g;
80        float v2 = min(max(M2 - M1 * M1, 0.0) +  DEPTH_EPSILON, 1.0);
81        float m_d = M1 - depth;
82    float pmax = v2 / (v2 + m_d * m_d);
83                                               
84    // Adjust the light color based on the shadow attenuation
85    light = max(lit_factor, pmax);   
86  }
87  else
88   light = 0;   
89       
90  return light;
91 
92}
93
94float4 shadowMapDist(LightCPos_OUT IN,
95                                        uniform float lightFarPlane,
96                                         uniform sampler2D shadowMap) : COLOR
97{
98  float4 light = 0;//shadowColor; 
99  if( IN.LightVPos.z > 0.0)
100  {     
101    float4 pos = (IN.LightVPos / IN.LightVPos.w);
102        float d = length(pos.xy);
103    //light = saturate((1.0 - d)/0.05);         
104    if(d <= 1.0)
105    {
106                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
107                pos.xy = (pos.xy + 1.0) / 2.0;
108                pos.y = 1.0 - pos.y;
109                float storedDist = tex2D(shadowMap, pos.xy).r;
110                light = dist < storedDist + DIST_BIAS;
111                //if(dist < storedDist + DIST_BIAS)
112                //      light = 1;
113    }
114  }
115  light = max(light, shadowColor);   
116                       
117  return light;
118}
119
120float4 shadowMapDist_Variance(LightCPos_OUT IN,
121                              uniform float lightFarPlane,
122                              uniform sampler2D shadowMap) : COLOR
123{
124  float4 light = 0;//loat4(1,1,1,1);
125 
126  if( IN.LightVPos.z > 0.0)
127  {     
128    float4 pos = (IN.LightVPos / IN.LightVPos.w);
129        float d = length(pos.xy);
130    if(d <= 1.0)
131    {
132                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
133                pos.xy = (pos.xy + 1.0) / 2.0;
134                pos.y = 1.0 - pos.y;
135                float4 storedDist = tex2D(shadowMap, pos.xy);
136                dist -= DIST_BIAS_VSM;
137                float lit_factor = light * (dist <= storedDist.r);     
138         
139                float M1 = storedDist.r;
140                float M2 = storedDist.g;
141                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
142                float m_d = M1 - dist;
143        float pmax = v2 / (v2 + m_d * m_d);
144                                               
145        // Adjust the light color based on the shadow attenuation
146        light = max(lit_factor, pmax);         
147    }
148  }
149       
150  return shadowColor + (1 - shadowColor) * light;
151}
152
153
154float4 shadowMapDist_POINT_Variance(LightCPos_OUT IN,
155                              uniform float lightFarPlane,
156                              uniform samplerCUBE shadowMap) : COLOR
157{
158  float4 light = float4(1,1,1,1);
159 
160  float dist = length(IN.LightCPos.xyz) / lightFarPlane;
161  float4 storedDist = texCUBE(shadowMap, float3(IN.LightCPos.xy, -IN.LightCPos.z));
162  dist -= DIST_BIAS_VSM;
163  float lit_factor = light * (dist <= storedDist.r);   
164         
165  float M1 = storedDist.r;
166  float M2 = storedDist.g;
167  float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
168  float m_d = M1 - dist;
169  float pmax = v2 / (v2 + m_d * m_d);
170                                               
171  // Adjust the light color based on the shadow attenuation
172  light = max(lit_factor, pmax);       
173       
174 
175  return (shadowColor + (1 - shadowColor) * light);
176}
Note: See TracBrowser for help on using the repository browser.