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