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