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

Revision 2404, 4.2 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.05
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.9,0.9,0.9,1)
22#define shadowColor float4(0.2,0.2,0.2,1)
23
24float4 shadowMapDepth(LightVPos_OUT IN,
25                      uniform sampler2D shadowMap) : COLOR
26{
27  float4 light = shadowColor;     
28       
29 // if(IN.LightVPos.z > 0.0)
30  {
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;
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);                     
38  }             
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)
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  else
68   light = 0;   
69       
70  return shadowColor + (1 - shadowColor) * light;       
71 
72}
73
74float4 shadowMapDist(LightCPos_OUT IN,
75                                        uniform float lightFarPlane,
76                                         uniform sampler2D shadowMap) : COLOR
77{
78  float4 light = 0;//shadowColor; 
79  if( IN.LightVPos.z > 0.0)
80  {     
81    float4 pos = (IN.LightVPos / IN.LightVPos.w);
82        float d = length(pos.xy);
83    //light = saturate((1.0 - d)/0.05);         
84    if(d <= 1.0)
85    {
86                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
87                pos.xy = (pos.xy + 1.0) / 2.0;
88                pos.y = 1.0 - pos.y;
89                float storedDist = tex2D(shadowMap, pos.xy).r;
90                light = max(dist < storedDist + DIST_BIAS, shadowColor);   
91                light = dist < storedDist + DIST_BIAS;
92                //if(dist < storedDist + DIST_BIAS)
93                //light = 1;
94    }
95  }
96       
97  return light;
98}
99
100float4 shadowMapDist_Variance(LightCPos_OUT IN,
101                              uniform float lightFarPlane,
102                              uniform sampler2D shadowMap) : COLOR
103{
104  float4 light = float4(1,1,1,1);
105 
106  if( IN.LightVPos.z > 0.0)
107  {     
108    float4 pos = (IN.LightVPos / IN.LightVPos.w);
109        float d = length(pos.xy);
110    if(d <= 1.0)
111    {
112                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
113                pos.xy = (pos.xy + 1.0) / 2.0;
114                pos.y = 1.0 - pos.y;
115                float4 storedDist = tex2D(shadowMap, pos.xy);
116                dist -= DIST_BIAS_VSM;
117                float lit_factor = light * (dist <= storedDist.r);     
118         
119                float M1 = storedDist.r;
120                float M2 = storedDist.g;
121                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
122                float m_d = M1 - dist;
123        float pmax = v2 / (v2 + m_d * m_d);
124                                               
125        // Adjust the light color based on the shadow attenuation
126        light = max(lit_factor, pmax);         
127    }
128  }
129       
130  return shadowColor + (1 - shadowColor) * light;
131}
132
133
134float4 shadowMapDist_POINT_Variance(LightCPos_OUT IN,
135                              uniform float lightFarPlane,
136                              uniform samplerCUBE shadowMap) : COLOR
137{
138  float4 light = float4(1,1,1,1);
139 
140  float dist = length(IN.LightCPos.xyz) / lightFarPlane;
141  float4 storedDist = texCUBE(shadowMap, float3(IN.LightCPos.xy, -IN.LightCPos.z));
142  dist -= DIST_BIAS_VSM;
143  float lit_factor = light * (dist <= storedDist.r);   
144         
145  float M1 = storedDist.r;
146  float M2 = storedDist.g;
147  float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
148  float m_d = M1 - dist;
149  float pmax = v2 / (v2 + m_d * m_d);
150                                               
151  // Adjust the light color based on the shadow attenuation
152  light = max(lit_factor, pmax);       
153       
154 
155  return (shadowColor + (1 - shadowColor) * light);
156}
Note: See TracBrowser for help on using the repository browser.