[390] | 1 | uniform float inverseShadowmapSize; |
---|
| 2 | uniform float fixedDepthBias; |
---|
| 3 | uniform float gradientClamp; |
---|
| 4 | uniform float gradientScaleBias; |
---|
| 5 | uniform float shadowFuzzyWidth; |
---|
| 6 | uniform vec4 lightColour; |
---|
| 7 | |
---|
| 8 | uniform sampler2D shadowMap; |
---|
| 9 | uniform sampler2D normalMap; |
---|
| 10 | |
---|
| 11 | varying vec3 tangentLightDir; |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | // Expand a range-compressed vector |
---|
| 15 | vec3 expand(vec3 v) |
---|
| 16 | { |
---|
| 17 | return (v - 0.5) * 2.0; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | void main() |
---|
| 21 | { |
---|
| 22 | |
---|
| 23 | // get the new normal and diffuse values |
---|
| 24 | vec3 normal = normalize(expand(texture2D(normalMap, gl_TexCoord[1].xy).xyz)); |
---|
| 25 | |
---|
| 26 | vec4 vertexColour = clamp(dot(normal, tangentLightDir),0.0,1.0) * lightColour; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | vec4 shadowUV = gl_TexCoord[0]; |
---|
| 30 | // point on shadowmap |
---|
| 31 | #if LINEAR_RANGE |
---|
| 32 | shadowUV.xy = shadowUV.xy / shadowUV.w; |
---|
| 33 | #else |
---|
| 34 | shadowUV = shadowUV / shadowUV.w; |
---|
| 35 | #endif |
---|
| 36 | float centerdepth = texture2D(shadowMap, shadowUV.xy).x; |
---|
| 37 | |
---|
| 38 | // gradient calculation |
---|
| 39 | float pixeloffset = inverseShadowmapSize; |
---|
| 40 | vec4 depths = vec4( |
---|
| 41 | texture2D(shadowMap, shadowUV.xy + vec2(-pixeloffset, 0)).x, |
---|
| 42 | texture2D(shadowMap, shadowUV.xy + vec2(+pixeloffset, 0)).x, |
---|
| 43 | texture2D(shadowMap, shadowUV.xy + vec2(0, -pixeloffset)).x, |
---|
| 44 | texture2D(shadowMap, shadowUV.xy + vec2(0, +pixeloffset)).x); |
---|
| 45 | |
---|
| 46 | vec2 differences = abs( depths.yw - depths.xz ); |
---|
| 47 | float gradient = min(gradientClamp, max(differences.x, differences.y)); |
---|
| 48 | float gradientFactor = gradient * gradientScaleBias; |
---|
| 49 | |
---|
| 50 | // visibility function |
---|
| 51 | float depthAdjust = gradientFactor + (fixedDepthBias * centerdepth); |
---|
| 52 | float finalCenterDepth = centerdepth + depthAdjust; |
---|
| 53 | |
---|
| 54 | // shadowUV.z contains lightspace position of current object |
---|
| 55 | |
---|
| 56 | #if FUZZY_TEST |
---|
| 57 | // fuzzy test - introduces some ghosting in result and doesn't appear to be needed? |
---|
| 58 | //float visibility = saturate(1 + delta_z / (gradient * shadowFuzzyWidth)); |
---|
| 59 | float visibility = saturate(1 + (finalCenterDepth - shadowUV.z) * shadowFuzzyWidth * shadowUV.w); |
---|
| 60 | |
---|
| 61 | gl_FragColor = vertexColour * visibility; |
---|
| 62 | #else |
---|
| 63 | // hard test |
---|
| 64 | #if PCF |
---|
| 65 | // use depths from prev, calculate diff |
---|
| 66 | depths += depthAdjust; |
---|
| 67 | float final = (finalCenterDepth > shadowUV.z) ? 1.0 : 0.0; |
---|
| 68 | final += (depths.x > shadowUV.z) ? 1.0 : 0.0; |
---|
| 69 | final += (depths.y > shadowUV.z) ? 1.0 : 0.0; |
---|
| 70 | final += (depths.z > shadowUV.z) ? 1.0 : 0.0; |
---|
| 71 | final += (depths.w > shadowUV.z) ? 1.0 : 0.0; |
---|
| 72 | |
---|
| 73 | final *= 0.2; |
---|
| 74 | |
---|
| 75 | gl_FragColor = vec4(vertexColour.xyz * final, 1); |
---|
| 76 | |
---|
| 77 | #else |
---|
| 78 | gl_FragColor = (finalCenterDepth > shadowUV.z) ? vertexColour : vec4(0,0,0,1); |
---|
| 79 | #endif |
---|
| 80 | |
---|
| 81 | #endif |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | |
---|