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