| 1 |
uniform mat4 mvpl; |
| 2 |
uniform sampler2D shadowMap; |
| 3 |
uniform float texSize; |
| 4 |
varying vec4 shadowCoord; |
| 5 |
uniform float shadowIntensity; // 1.0 black, 0, transparent |
| 6 |
|
| 7 |
vec4 shadowCoordPostW; |
| 8 |
float chebyshevUpperBound( float distance) { |
| 9 |
// We retrieve the two moments previously stored (depth and depth*depth) |
| 10 |
vec2 moments = texture2D(shadowMap,shadowCoordPostW.xy).rb; |
| 11 |
// Surface is fully lit. as the current fragment is before the light occluder |
| 12 |
if (distance <= moments.x) |
| 13 |
return 1.0 ; |
| 14 |
|
| 15 |
float variance = moments.y - (moments.x*moments.x); |
| 16 |
//variance = max(variance,0.00002); |
| 17 |
variance = max(variance,0.000195); |
| 18 |
float d = distance - moments.x; |
| 19 |
float p_max = variance / (variance + d*d); |
| 20 |
return p_max; |
| 21 |
} |
| 22 |
|
| 23 |
void main() { |
| 24 |
shadowCoordPostW = shadowCoord / shadowCoord.w; |
| 25 |
shadowCoordPostW = shadowCoordPostW * 0.5 + 0.5; |
| 26 |
float shadow = chebyshevUpperBound(shadowCoordPostW.z); |
| 27 |
|
| 28 |
vec4 kd = gl_LightSource[0].diffuse; |
| 29 |
if (shadow > 0.4) |
| 30 |
discard; |
| 31 |
|
| 32 |
// gl_FragColor = vec4(vec3(0.0), 0.5 - shadow); |
| 33 |
gl_FragColor = vec4(vec3(0.0), (shadowIntensity-shadow)); |
| 34 |
|
| 35 |
} |