1
/* Extract shadow (how light is the point) from variance shadow map.
2
This closely follows VSM presentation, slide 16. */
4
float shadow(sampler2D shadowMap, const vec4 shadowMapCoord, const in float size)
6
/* Avoid back-projecting shadows. */
7
if (shadowMapCoord.z < 0.0) return 0.0;
9
/* When coord2 is outside (0, 0) - (1, 1) square,
10
it's always in the shadow. Otherwise shadows would be stretched
11
over whole scene, due to clamping. */
12
vec2 coord2 = shadowMapCoord.st / shadowMapCoord.q;
13
if (coord2.s < 0.0 || coord2.s > 1.0 ||
14
coord2.t < 0.0 || coord2.t > 1.0)
17
vec4 moments = texture2D(shadowMap, coord2);
18
float distance_to_light = shadowMapCoord.z / shadowMapCoord.q;
20
if (distance_to_light <= moments[0])
23
float E_x2 = moments[1];
24
float Ex_2 = moments[0] * moments[0];
25
float variance = E_x2 - Ex_2;
26
float m_d = moments[0] - distance_to_light;
27
return variance / (variance + m_d * m_d);
31
float shadow_depth(sampler2D shadowMap, const vec4 shadowMapCoord)
33
/* Avoid back-projecting shadows. */
34
if (shadowMapCoord.z < 0.0) return 0.0;
36
vec2 coord2 = shadowMapCoord.st / shadowMapCoord.q;
38
/* When coord2 is outside (0, 0) - (1, 1) square, set d = 0.
39
Otherwise texture would be visible stretched due to clamping. */
40
if (coord2.s < 0.0 || coord2.s > 1.0 ||
41
coord2.t < 0.0 || coord2.t > 1.0)
43
return texture2D(shadowMap, coord2).x;