1 | #version 120 |
---|
2 | |
---|
3 | uniform float inverseShadowmapSize; |
---|
4 | uniform float fixedDepthBias; |
---|
5 | uniform float gradientClamp; |
---|
6 | uniform float gradientScaleBias; |
---|
7 | |
---|
8 | uniform sampler2D shadowMap; |
---|
9 | |
---|
10 | varying vec4 oUv; |
---|
11 | varying vec4 outColor; |
---|
12 | |
---|
13 | void main() |
---|
14 | { |
---|
15 | vec4 shadowUV = oUv; |
---|
16 | // point on shadowmap |
---|
17 | shadowUV = shadowUV / shadowUV.w; |
---|
18 | float centerdepth = texture2D(shadowMap, shadowUV.xy).x; |
---|
19 | |
---|
20 | // gradient calculation |
---|
21 | float pixeloffset = inverseShadowmapSize; |
---|
22 | vec4 depths = vec4( |
---|
23 | texture2D(shadowMap, shadowUV.xy + vec2(-pixeloffset, 0)).x, |
---|
24 | texture2D(shadowMap, shadowUV.xy + vec2(+pixeloffset, 0)).x, |
---|
25 | texture2D(shadowMap, shadowUV.xy + vec2(0, -pixeloffset)).x, |
---|
26 | texture2D(shadowMap, shadowUV.xy + vec2(0, +pixeloffset)).x); |
---|
27 | |
---|
28 | vec2 differences = abs( depths.yw - depths.xz ); |
---|
29 | float gradient = min(gradientClamp, max(differences.x, differences.y)); |
---|
30 | float gradientFactor = gradient * gradientScaleBias; |
---|
31 | |
---|
32 | // visibility function |
---|
33 | float depthAdjust = gradientFactor + (fixedDepthBias * centerdepth); |
---|
34 | float finalCenterDepth = centerdepth + depthAdjust; |
---|
35 | |
---|
36 | // shadowUV.z contains lightspace position of current object |
---|
37 | #if PCF |
---|
38 | // use depths from prev, calculate diff |
---|
39 | depths += depthAdjust; |
---|
40 | float final = (finalCenterDepth > shadowUV.z) ? 1.0 : 0.0; |
---|
41 | final += (depths.x > shadowUV.z) ? 1.0 : 0.0; |
---|
42 | final += (depths.y > shadowUV.z) ? 1.0 : 0.0; |
---|
43 | final += (depths.z > shadowUV.z) ? 1.0 : 0.0; |
---|
44 | final += (depths.w > shadowUV.z) ? 1.0 : 0.0; |
---|
45 | |
---|
46 | final *= 0.2; |
---|
47 | |
---|
48 | gl_FragColor = vec4(outColor.xyz * final, 1); |
---|
49 | |
---|
50 | #else |
---|
51 | gl_FragColor = (centerdepth > shadowUV.z) ? vec4(outColor.xyz,1) : vec4(0,0,0,1); |
---|
52 | #endif |
---|
53 | } |
---|
54 | |
---|