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

Revision 2550, 4.8 KB checked in by szirmay, 17 years ago (diff)
RevLine 
[2024]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
[2507]14#define DEPTH_BIAS      0.01
15#define DEPTH_BIAS_VSM  0.00005
16#define DEPTH_EPSILON   0.01
[2404]17#define DIST_BIAS       0.005
[2311]18#define DIST_BIAS_VSM   0.0005
19#define DIST_EPSILON    0.001
[2024]20
[2438]21//#define shadowColor float4(0.95,0.95,0.95,1)
[2442]22#define shadowColor float4(0.8,0.8,0.8,1)
[2507]23#define shadowColor2 float4(0.2,0.2,0.2,1)
[2442]24//#define shadowColor float4(0.0,0.0,0.0,1)
[2024]25
26float4 shadowMapDepth(LightVPos_OUT IN,
27                      uniform sampler2D shadowMap) : COLOR
28{
[2181]29  float4 light = shadowColor;     
30       
[2294]31 // if(IN.LightVPos.z > 0.0)
[2024]32  {
[2181]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;
[2294]37        float storedDepth = abs(tex2D(shadowMap, pos.xy).r);
38        //light = (pos.z - storedDepth.r)*100.0;
[2507]39        light = max(storedDepth + DEPTH_BIAS > pos.z, shadowColor2);                   
[2181]40  }             
[2024]41  return light;
42}
43
[2550]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
[2024]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)
[2181]70  {         
[2024]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;
[2181]82    float pmax = v2 / (v2 + m_d * m_d);
[2024]83                                               
[2181]84    // Adjust the light color based on the shadow attenuation
85    light = max(lit_factor, pmax);   
[2024]86  }
87  else
88   light = 0;   
89       
[2507]90  return light;
[2024]91 
92}
93
[2179]94float4 shadowMapDist(LightCPos_OUT IN,
[2181]95                                        uniform float lightFarPlane,
[2179]96                                         uniform sampler2D shadowMap) : COLOR
[2024]97{
[2404]98  float4 light = 0;//shadowColor; 
[2179]99  if( IN.LightVPos.z > 0.0)
[2024]100  {     
[2179]101    float4 pos = (IN.LightVPos / IN.LightVPos.w);
[2217]102        float d = length(pos.xy);
[2404]103    //light = saturate((1.0 - d)/0.05);         
[2217]104    if(d <= 1.0)
105    {
[2181]106                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
[2179]107                pos.xy = (pos.xy + 1.0) / 2.0;
108                pos.y = 1.0 - pos.y;
[2311]109                float storedDist = tex2D(shadowMap, pos.xy).r;
110                light = dist < storedDist + DIST_BIAS;
[2404]111                //if(dist < storedDist + DIST_BIAS)
[2422]112                //      light = 1;
[2217]113    }
[2179]114  }
[2422]115  light = max(light, shadowColor);   
116                       
[2024]117  return light;
118}
119
120float4 shadowMapDist_Variance(LightCPos_OUT IN,
121                              uniform float lightFarPlane,
122                              uniform sampler2D shadowMap) : COLOR
123{
[2442]124  float4 light = 0;//loat4(1,1,1,1);
[2024]125 
[2179]126  if( IN.LightVPos.z > 0.0)
[2024]127  {     
[2179]128    float4 pos = (IN.LightVPos / IN.LightVPos.w);
129        float d = length(pos.xy);
130    if(d <= 1.0)
[2024]131    {
[2179]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);     
[2024]138         
[2179]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;
[2024]143        float pmax = v2 / (v2 + m_d * m_d);
144                                               
145        // Adjust the light color based on the shadow attenuation
[2179]146        light = max(lit_factor, pmax);         
147    }
[2024]148  }
149       
150  return shadowColor + (1 - shadowColor) * light;
[2337]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);
[2024]176}
Note: See TracBrowser for help on using the repository browser.