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

Revision 2442, 4.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.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.95,0.95,0.95,1)
22#define shadowColor float4(0.8,0.8,0.8,1)
23//#define shadowColor 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, shadowColor);                     
40  }             
41  return light;
42}
43
44float4 shadowMapDepth_Variance(LightVPos_OUT IN,
45                               uniform sampler2D shadowMap) : COLOR
46{
47  float4 light = float4(1,1,1,1);
48       
49  float4 pos = (IN.LightVPos / IN.LightVPos.w);
50 
51  if( pos.z > 0.0)
52  {         
53        float depth = pos.z;
54        pos.xy = (pos.xy + 1.0) / 2.0;
55        pos.y = 1.0 - pos.y;
56        float4 storedDepth = tex2D(shadowMap, pos.xy);
57        depth -= DEPTH_BIAS_VSM;
58        float lit_factor = light * (depth <= storedDepth.r);   
59         
60        float M1 = storedDepth.r;
61        float M2 = storedDepth.g;
62        float v2 = min(max(M2 - M1 * M1, 0.0) +  DEPTH_EPSILON, 1.0);
63        float m_d = M1 - depth;
64    float pmax = v2 / (v2 + m_d * m_d);
65                                               
66    // Adjust the light color based on the shadow attenuation
67    light = max(lit_factor, pmax);   
68  }
69  else
70   light = 0;   
71       
72  return shadowColor + (1 - shadowColor) * light;       
73 
74}
75
76float4 shadowMapDist(LightCPos_OUT IN,
77                                        uniform float lightFarPlane,
78                                         uniform sampler2D shadowMap) : COLOR
79{
80  float4 light = 0;//shadowColor; 
81  if( IN.LightVPos.z > 0.0)
82  {     
83    float4 pos = (IN.LightVPos / IN.LightVPos.w);
84        float d = length(pos.xy);
85    //light = saturate((1.0 - d)/0.05);         
86    if(d <= 1.0)
87    {
88                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
89                pos.xy = (pos.xy + 1.0) / 2.0;
90                pos.y = 1.0 - pos.y;
91                float storedDist = tex2D(shadowMap, pos.xy).r;
92                light = dist < storedDist + DIST_BIAS;
93                //if(dist < storedDist + DIST_BIAS)
94                //      light = 1;
95    }
96  }
97  light = max(light, shadowColor);   
98                       
99  return light;
100}
101
102float4 shadowMapDist_Variance(LightCPos_OUT IN,
103                              uniform float lightFarPlane,
104                              uniform sampler2D shadowMap) : COLOR
105{
106  float4 light = 0;//loat4(1,1,1,1);
107 
108  if( IN.LightVPos.z > 0.0)
109  {     
110    float4 pos = (IN.LightVPos / IN.LightVPos.w);
111        float d = length(pos.xy);
112    if(d <= 1.0)
113    {
114                float dist = length(IN.LightCPos.xyz) / lightFarPlane;
115                pos.xy = (pos.xy + 1.0) / 2.0;
116                pos.y = 1.0 - pos.y;
117                float4 storedDist = tex2D(shadowMap, pos.xy);
118                dist -= DIST_BIAS_VSM;
119                float lit_factor = light * (dist <= storedDist.r);     
120         
121                float M1 = storedDist.r;
122                float M2 = storedDist.g;
123                float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
124                float m_d = M1 - dist;
125        float pmax = v2 / (v2 + m_d * m_d);
126                                               
127        // Adjust the light color based on the shadow attenuation
128        light = max(lit_factor, pmax);         
129    }
130  }
131       
132  return shadowColor + (1 - shadowColor) * light;
133}
134
135
136float4 shadowMapDist_POINT_Variance(LightCPos_OUT IN,
137                              uniform float lightFarPlane,
138                              uniform samplerCUBE shadowMap) : COLOR
139{
140  float4 light = float4(1,1,1,1);
141 
142  float dist = length(IN.LightCPos.xyz) / lightFarPlane;
143  float4 storedDist = texCUBE(shadowMap, float3(IN.LightCPos.xy, -IN.LightCPos.z));
144  dist -= DIST_BIAS_VSM;
145  float lit_factor = light * (dist <= storedDist.r);   
146         
147  float M1 = storedDist.r;
148  float M2 = storedDist.g;
149  float v2 = min(max(M2 - M1 * M1, 0.0) +  DIST_EPSILON, 1.0);
150  float m_d = M1 - dist;
151  float pmax = v2 / (v2 + m_d * m_d);
152                                               
153  // Adjust the light color based on the shadow attenuation
154  light = max(lit_factor, pmax);       
155       
156 
157  return (shadowColor + (1 - shadowColor) * light);
158}
Note: See TracBrowser for help on using the repository browser.