| 1 | uniform float inverseShadowmapSize; | 
|---|
| 2 | uniform float fixedDepthBias; | 
|---|
| 3 | uniform float gradientClamp; | 
|---|
| 4 | uniform float gradientScaleBias; | 
|---|
| 5 | uniform float shadowFuzzyWidth; | 
|---|
| 6 |  | 
|---|
| 7 | uniform sampler2D shadowMap; | 
|---|
| 8 |  | 
|---|
| 9 | void main() | 
|---|
| 10 | { | 
|---|
| 11 | vec4 shadowUV = gl_TexCoord[0]; | 
|---|
| 12 | // point on shadowmap | 
|---|
| 13 | #if LINEAR_RANGE | 
|---|
| 14 | shadowUV.xy = shadowUV.xy / shadowUV.w; | 
|---|
| 15 | #else | 
|---|
| 16 | shadowUV = shadowUV / shadowUV.w; | 
|---|
| 17 | #endif | 
|---|
| 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 |  | 
|---|
| 38 | #if FUZZY_TEST | 
|---|
| 39 | // fuzzy test - introduces some ghosting in result and doesn't appear to be needed? | 
|---|
| 40 | //float visibility = saturate(1 + delta_z / (gradient * shadowFuzzyWidth)); | 
|---|
| 41 | float visibility = saturate(1 + (finalCenterDepth - shadowUV.z) * shadowFuzzyWidth * shadowUV.w); | 
|---|
| 42 |  | 
|---|
| 43 | gl_FragColor = vertexColour * visibility; | 
|---|
| 44 | #else | 
|---|
| 45 | // hard test | 
|---|
| 46 | #if PCF | 
|---|
| 47 | // use depths from prev, calculate diff | 
|---|
| 48 | depths += depthAdjust; | 
|---|
| 49 | float final = (finalCenterDepth > shadowUV.z) ? 1.0 : 0.0; | 
|---|
| 50 | final += (depths.x > shadowUV.z) ? 1.0 : 0.0; | 
|---|
| 51 | final += (depths.y > shadowUV.z) ? 1.0 : 0.0; | 
|---|
| 52 | final += (depths.z > shadowUV.z) ? 1.0 : 0.0; | 
|---|
| 53 | final += (depths.w > shadowUV.z) ? 1.0 : 0.0; | 
|---|
| 54 |  | 
|---|
| 55 | final *= 0.2; | 
|---|
| 56 |  | 
|---|
| 57 | gl_FragColor = vec4(gl_Color.xyz * final, 1); | 
|---|
| 58 |  | 
|---|
| 59 | #else | 
|---|
| 60 | gl_FragColor = (finalCenterDepth > shadowUV.z) ? gl_Color : vec4(0,0,0,1); | 
|---|
| 61 | #endif | 
|---|
| 62 |  | 
|---|
| 63 | #endif | 
|---|
| 64 |  | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|