1 | #version 150 |
---|
2 | |
---|
3 | uniform float inverseShadowmapSize; |
---|
4 | uniform float fixedDepthBias; |
---|
5 | uniform float gradientClamp; |
---|
6 | uniform float gradientScaleBias; |
---|
7 | uniform float shadowFuzzyWidth; |
---|
8 | |
---|
9 | uniform sampler2D shadowMap; |
---|
10 | out vec4 fragColour; |
---|
11 | in vec4 shadowUV; |
---|
12 | in vec4 oColour; |
---|
13 | |
---|
14 | void main() |
---|
15 | { |
---|
16 | float centerdepth = texture(shadowMap, shadowUV.xy).x; |
---|
17 | |
---|
18 | // gradient calculation |
---|
19 | float pixeloffset = inverseShadowmapSize; |
---|
20 | vec4 depths = vec4( |
---|
21 | texture(shadowMap, shadowUV.xy + vec2(-pixeloffset, 0)).x, |
---|
22 | texture(shadowMap, shadowUV.xy + vec2(+pixeloffset, 0)).x, |
---|
23 | texture(shadowMap, shadowUV.xy + vec2(0, -pixeloffset)).x, |
---|
24 | texture(shadowMap, shadowUV.xy + vec2(0, +pixeloffset)).x); |
---|
25 | |
---|
26 | vec2 differences = abs( depths.yw - depths.xz ); |
---|
27 | float gradient = min(gradientClamp, max(differences.x, differences.y)); |
---|
28 | float gradientFactor = gradient * gradientScaleBias; |
---|
29 | |
---|
30 | // visibility function |
---|
31 | float depthAdjust = gradientFactor + (fixedDepthBias * centerdepth); |
---|
32 | float finalCenterDepth = centerdepth + depthAdjust; |
---|
33 | |
---|
34 | // shadowUV.z contains lightspace position of current object |
---|
35 | |
---|
36 | #if FUZZY_TEST |
---|
37 | // fuzzy test - introduces some ghosting in result and doesn't appear to be needed? |
---|
38 | //float visibility = saturate(1 + delta_z / (gradient * shadowFuzzyWidth)); |
---|
39 | float visibility = saturate(1 + (finalCenterDepth - shadowUV.z) * shadowFuzzyWidth * shadowUV.w); |
---|
40 | |
---|
41 | fragColour = vertexColour * visibility; |
---|
42 | #else |
---|
43 | // hard test |
---|
44 | #if PCF |
---|
45 | // use depths from prev, calculate diff |
---|
46 | depths += depthAdjust; |
---|
47 | float final = (finalCenterDepth > shadowUV.z) ? 1.0 : 0.0; |
---|
48 | final += (depths.x > shadowUV.z) ? 1.0 : 0.0; |
---|
49 | final += (depths.y > shadowUV.z) ? 1.0 : 0.0; |
---|
50 | final += (depths.z > shadowUV.z) ? 1.0 : 0.0; |
---|
51 | final += (depths.w > shadowUV.z) ? 1.0 : 0.0; |
---|
52 | |
---|
53 | final *= 0.2; |
---|
54 | |
---|
55 | fragColour = vec4(oColour.xyz * final, 1); |
---|
56 | |
---|
57 | #else |
---|
58 | fragColour = (finalCenterDepth > shadowUV.z) ? oColour : vec4(0.5,0,0,1); |
---|
59 | #endif |
---|
60 | |
---|
61 | #endif |
---|
62 | |
---|
63 | } |
---|
64 | |
---|