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

Revision 2337, 4.2 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
14#define DEPTH_BIAS      0.001
15#define DEPTH_BIAS_VSM  0.001
[2294]16#define DEPTH_EPSILON   0.05
[2311]17#define DIST_BIAS       0.0005
18#define DIST_BIAS_VSM   0.0005
19#define DIST_EPSILON    0.001
[2024]20
[2294]21//#define shadowColor float4(0.9,0.9,0.9,1)
[2311]22#define shadowColor float4(0.2,0.2,0.2,1)
[2024]23
24float4 shadowMapDepth(LightVPos_OUT IN,
25                      uniform sampler2D shadowMap) : COLOR
26{
[2181]27  float4 light = shadowColor;     
28       
[2294]29 // if(IN.LightVPos.z > 0.0)
[2024]30  {
[2181]31        float4 pos = (IN.LightVPos / IN.LightVPos.w);
32               
33        pos.xy = (pos.xy + 1.0) / 2.0;
34        pos.y = 1.0 - pos.y;
[2294]35        float storedDepth = abs(tex2D(shadowMap, pos.xy).r);
36        //light = (pos.z - storedDepth.r)*100.0;
37        light = max(storedDepth + DEPTH_BIAS > pos.z, shadowColor);                     
[2181]38  }             
[2024]39  return light;
40}
41
42float4 shadowMapDepth_Variance(LightVPos_OUT IN,
43                               uniform sampler2D shadowMap) : COLOR
44{
45  float4 light = float4(1,1,1,1);
46       
47  float4 pos = (IN.LightVPos / IN.LightVPos.w);
48 
49  if( pos.z > 0.0)
[2181]50  {         
[2024]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;
[2181]62    float pmax = v2 / (v2 + m_d * m_d);
[2024]63                                               
[2181]64    // Adjust the light color based on the shadow attenuation
65    light = max(lit_factor, pmax);   
[2024]66  }
67  else
68   light = 0;   
69       
70  return shadowColor + (1 - shadowColor) * light;       
71 
72}
73
[2179]74float4 shadowMapDist(LightCPos_OUT IN,
[2181]75                                        uniform float lightFarPlane,
[2179]76                                         uniform sampler2D shadowMap) : COLOR
[2024]77{
[2181]78  float4 light = shadowColor; 
[2179]79  if( IN.LightVPos.z > 0.0)
[2024]80  {     
[2179]81    float4 pos = (IN.LightVPos / IN.LightVPos.w);
[2217]82        float d = length(pos.xy);
83    light = saturate((1.0 - d)/0.05);           
84    if(d <= 1.0)
85    {
[2181]86                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
[2179]87                pos.xy = (pos.xy + 1.0) / 2.0;
88                pos.y = 1.0 - pos.y;
[2311]89                float storedDist = tex2D(shadowMap, pos.xy).r;
90                light = max(dist < storedDist + DIST_BIAS, shadowColor);   
91                light = dist < storedDist + DIST_BIAS;
[2217]92    }
[2179]93  }
94       
[2024]95  return light;
96}
97
98float4 shadowMapDist_Variance(LightCPos_OUT IN,
99                              uniform float lightFarPlane,
100                              uniform sampler2D shadowMap) : COLOR
101{
102  float4 light = float4(1,1,1,1);
103 
[2179]104  if( IN.LightVPos.z > 0.0)
[2024]105  {     
[2179]106    float4 pos = (IN.LightVPos / IN.LightVPos.w);
107        float d = length(pos.xy);
108    if(d <= 1.0)
[2024]109    {
[2179]110                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
111                pos.xy = (pos.xy + 1.0) / 2.0;
112                pos.y = 1.0 - pos.y;
113                float4 storedDist = tex2D(shadowMap, pos.xy);
114                dist -= DIST_BIAS_VSM;
115                float lit_factor = light * (dist <= storedDist.r);     
[2024]116         
[2179]117                float M1 = storedDist.r;
118                float M2 = storedDist.g;
119                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
120                float m_d = M1 - dist;
[2024]121        float pmax = v2 / (v2 + m_d * m_d);
122                                               
123        // Adjust the light color based on the shadow attenuation
[2179]124        light = max(lit_factor, pmax);         
125    }
[2024]126  }
127       
128  return shadowColor + (1 - shadowColor) * light;
[2337]129}
130
131
132float4 shadowMapDist_POINT_Variance(LightCPos_OUT IN,
133                              uniform float lightFarPlane,
134                              uniform samplerCUBE shadowMap) : COLOR
135{
136  float4 light = float4(1,1,1,1);
137 
138  float dist = length(IN.LightCPos.xyz) / lightFarPlane;
139  float4 storedDist = texCUBE(shadowMap, float3(IN.LightCPos.xy, -IN.LightCPos.z));
140  dist -= DIST_BIAS_VSM;
141  float lit_factor = light * (dist <= storedDist.r);   
142         
143  float M1 = storedDist.r;
144  float M2 = storedDist.g;
145  float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
146  float m_d = M1 - dist;
147  float pmax = v2 / (v2 + m_d * m_d);
148                                               
149  // Adjust the light color based on the shadow attenuation
150  light = max(lit_factor, pmax);       
151       
152 
153  return (shadowColor + (1 - shadowColor) * light);
[2024]154}
Note: See TracBrowser for help on using the repository browser.