Debian Codegraph @

Shaders/shadows-include.frag

raw
1 #version 120
2
3 uniform sampler2DShadow shadow_tex;
4
5 uniform bool shadows_enabled;
6 uniform int sun_atlas_size;
7
8 varying vec4 lightSpacePos[4];
9
10 const float DEPTH_BIAS = 2.0;
11 const float BAND_SIZE = 0.1;
12 const vec2 BAND_BOTTOM_LEFT = vec2(BAND_SIZE);
13 const vec2 BAND_TOP_RIGHT = vec2(1.0 - BAND_SIZE);
14
15 // Ideally these should be passed as an uniform, but we don't support uniform
16 // arrays yet
17 const vec2 uv_shifts[4] = vec2[4](
18 vec2(0.0, 0.0), vec2(0.5, 0.0),
19 vec2(0.0, 0.5), vec2(0.5, 0.5));
20 const vec2 uv_factor = vec2(0.5, 0.5);
21
22
23 float sampleOffset(vec4 pos, vec2 offset, vec2 invTexelSize)
24 {
25 return shadow2DProj(
26 shadow_tex, vec4(
27 pos.xy + offset * invTexelSize * pos.w,
28 pos.z - DEPTH_BIAS * invTexelSize.x,
29 pos.w)).r;
30 }
31
32 // OptimizedPCF from https://github.com/TheRealMJP/Shadows
33 // Original by Ignacio CastaƱo for The Witness
34 // Released under The MIT License
35 float sampleOptimizedPCF(vec4 pos)
36 {
37 vec2 invTexelSize = vec2(1.0 / float(sun_atlas_size));
38
39 vec2 uv = pos.xy * sun_atlas_size;
40 vec2 base_uv = floor(uv + 0.5);
41 float s = (uv.x + 0.5 - base_uv.x);
42 float t = (uv.y + 0.5 - base_uv.y);
43 base_uv -= vec2(0.5);
44 base_uv *= invTexelSize;
45 pos.xy = base_uv.xy;
46
47 float sum = 0.0;
48
49 float uw0 = (4.0 - 3.0 * s);
50 float uw1 = 7.0;
51 float uw2 = (1.0 + 3.0 * s);
52
53 float u0 = (3.0 - 2.0 * s) / uw0 - 2.0;
54 float u1 = (3.0 + s) / uw1;
55 float u2 = s / uw2 + 2.0;
56
57 float vw0 = (4.0 - 3.0 * t);
58 float vw1 = 7.0;
59 float vw2 = (1.0 + 3.0 * t);
60
61 float v0 = (3.0 - 2.0 * t) / vw0 - 2.0;
62 float v1 = (3.0 + t) / vw1;
63 float v2 = t / vw2 + 2.0;
64
65 sum += uw0 * vw0 * sampleOffset(pos, vec2(u0, v0), invTexelSize);
66 sum += uw1 * vw0 * sampleOffset(pos, vec2(u1, v0), invTexelSize);
67 sum += uw2 * vw0 * sampleOffset(pos, vec2(u2, v0), invTexelSize);
68
69 sum += uw0 * vw1 * sampleOffset(pos, vec2(u0, v1), invTexelSize);
70 sum += uw1 * vw1 * sampleOffset(pos, vec2(u1, v1), invTexelSize);
71 sum += uw2 * vw1 * sampleOffset(pos, vec2(u2, v1), invTexelSize);
72
73 sum += uw0 * vw2 * sampleOffset(pos, vec2(u0, v2), invTexelSize);
74 sum += uw1 * vw2 * sampleOffset(pos, vec2(u1, v2), invTexelSize);
75 sum += uw2 * vw2 * sampleOffset(pos, vec2(u2, v2), invTexelSize);
76
77 return sum / 144.0;
78 }
79
80 float sampleCascade(int n)
81 {
82 vec4 pos = lightSpacePos[n];
83 pos.xy *= uv_factor;
84 pos.xy += uv_shifts[n];
85 return sampleOptimizedPCF(pos);
86 }
87
88 float sampleAndBlendBand(int n1, int n2)
89 {
90 vec2 s = smoothstep(vec2(0.0), BAND_BOTTOM_LEFT, lightSpacePos[n1].xy)
91 - smoothstep(BAND_TOP_RIGHT, vec2(1.0), lightSpacePos[n1].xy);
92 float blend = 1.0 - s.x * s.y;
93 return mix(sampleCascade(n1), sampleCascade(n2), blend);
94 }
95
96 bool checkWithinBounds(vec2 coords, vec2 bottomLeft, vec2 topRight)
97 {
98 vec2 r = step(bottomLeft, coords) - step(topRight, coords);
99 return bool(r.x * r.y);
100 }
101
102 bool isInsideCascade(int n)
103 {
104 return checkWithinBounds(lightSpacePos[n].xy, vec2(0.0), vec2(1.0)) &&
105 ((lightSpacePos[n].z / lightSpacePos[n].w) <= 1.0);
106 }
107
108 bool isInsideBand(int n)
109 {
110 return !checkWithinBounds(lightSpacePos[n].xy, BAND_BOTTOM_LEFT, BAND_TOP_RIGHT);
111 }
112
113 // Get a value between 0.0 and 1.0 where 0.0 means shadowed and 1.0 means lit
114 float getShadowing()
115 {
116 float shadow = 1.0;
117 if (shadows_enabled) {
118 for (int i = 0; i < 4; ++i) {
119 // Map-based cascade selection
120 // We test if we are inside the cascade bounds to find the tightest
121 // map that contains the fragment.
122 if (isInsideCascade(i)) {
123 if (isInsideBand(i) && ((i+1) < 4)) {
124 // Blend between cascades if the fragment is near the
125 // next cascade to avoid abrupt transitions.
126 shadow = clamp(sampleAndBlendBand(i, i+1), 0.0, 1.0);
127 } else {
128 // We are far away from the borders of the cascade, so
129 // we skip the blending to avoid the performance cost
130 // of sampling the shadow map twice.
131 shadow = clamp(sampleCascade(i), 0.0, 1.0);
132 }
133 break;
134 }
135 }
136 }
137 return shadow;
138 }