Debian Codegraph @

Shaders/tree-ALS-shadow.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, Oct 2011, based on default.frag
5
6
7 varying vec3 relPos;
8
9
10 uniform sampler2D texture;
11
12
13 varying float yprime_alt;
14
15 varying float is_shadow;
16 varying float autumn_flag;
17
18 uniform float visibility;
19 uniform float avisibility;
20 uniform float scattering;
21 uniform float ground_scattering;
22 uniform float cloud_self_shading;
23 uniform float terminator;
24 uniform float terrain_alt;
25 uniform float hazeLayerAltitude;
26 uniform float overcast;
27 uniform float eye_alt;
28 uniform float dust_cover_factor;
29 uniform float air_pollution;
30 uniform float landing_light1_offset;
31 uniform float landing_light2_offset;
32 uniform float landing_light3_offset;
33 uniform float cseason;
34
35 uniform int use_searchlight;
36 uniform int use_landing_light;
37 uniform int use_alt_landing_light;
38 uniform int quality_level;
39 uniform int tquality_level;
40
41
42 const float EarthRadius = 5800000.0;
43 const float terminator_width = 200000.0;
44
45 float alt;
46 float mie_angle;
47
48
49 float light_distance_fading(in float dist);
50 float fog_backscatter(in float avisibility);
51 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
52
53 vec3 searchlight();
54 vec3 landing_light(in float offset, in float offsetv);
55 vec3 get_hazeColor(in float light_arg);
56 vec3 filter_combined (in vec3 color) ;
57 float Noise2D(in vec2 coord, in float wavelength);
58
59 float luminance(vec3 color)
60 {
61 return dot(vec3(0.212671, 0.715160, 0.072169), color);
62 }
63
64
65 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
66 {
67 x = x - 0.5;
68
69 // use the asymptotics to shorten computations
70 if (x > 30.0) {return e;}
71 if (x < -15.0) {return 0.0;}
72
73 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
74 }
75
76 // this determines how light is attenuated in the distance
77 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
78 // for distance > visibility
79
80 float tree_fog_func (in float targ)
81 {
82
83
84 float fade_mix;
85
86 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
87 // create the illusion of improved visibility range
88
89 targ = 1.25 * targ * smoothstep(0.07,0.1,targ); // need to sync with the distance to which terrain is drawn
90
91
92 if (alt < 30000.0)
93 {return exp(-targ - targ * targ * targ * targ);}
94 else if (alt < 50000.0)
95 {
96 fade_mix = (alt - 30000.0)/20000.0;
97 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
98 }
99 else
100 {
101 return exp(- targ * targ - pow(targ,4.0));
102 }
103
104 }
105
106 void main()
107 {
108
109 if (is_shadow > 1.0) {discard;}
110
111 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
112
113
114 vec3 lightDir = gl_LightSource[0].position.xyz;
115 float intensity;
116
117 mie_angle = gl_Color.a;
118 vec4 texel = texture2D(texture, gl_TexCoord[0].st);
119
120 // angle with horizon
121 float dist = length(relPos);
122 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
123
124 // determine tree shadow properties
125 if (is_shadow>0.0)
126 {
127 if (ct > -0.1) {discard;} // we eliminate shadows above the camera to avoid artifacts
128 float illumination = length(gl_Color.rgb);
129 texel = vec4 (0.1,0.1,0.1,texel.a);
130 texel.a *= illumination;// * smoothstep(0.0, 0.2, is_shadow);
131 texel.a *=0.6 * smoothstep(0.5,0.8,scattering);
132 texel.a = min(0.8, texel.a);
133 }
134
135 float effective_scattering = min(scattering, cloud_self_shading);
136
137
138 if (quality_level > 3)
139 {
140 // seasonal color changes
141
142 if ((cseason < 1.5)&& (autumn_flag > 0.0) && (is_shadow <0.0))
143 {
144 texel.r = min(1.0, (1.0 + 5.0 *cseason * autumn_flag ) * texel.r);
145 texel.b = max(0.0, (1.0 - 8.0 * cseason) * texel.b);
146 }
147
148 // mix dust
149 vec4 dust_color = vec4 (0.76, 0.71, 0.56, texel.a);
150
151 texel = mix(texel, dust_color, clamp(0.6 * dust_cover_factor ,0.0, 1.0) );
152 }
153
154
155 // ALS secondary light sources
156
157 vec3 secondary_light = vec3 (0.0,0.0,0.0);
158
159 if ((quality_level>5) && (tquality_level>5))
160 {
161 if (use_searchlight == 1)
162 {
163 secondary_light += searchlight();
164 }
165 if (use_landing_light == 1)
166 {
167 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
168 }
169 if (use_alt_landing_light == 1)
170 {
171 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
172 }
173 }
174
175 vec4 fragColor = vec4 (gl_Color.rgb +secondary_light * light_distance_fading(dist),1.0) * texel;
176
177
178 // Rayleigh haze
179
180 float lightArg = (terminator-yprime_alt)/100000.0;
181 vec3 hazeColor = get_hazeColor(lightArg);
182
183 // Rayleigh color shift due to in-scattering
184
185 if ((quality_level > 5) && (tquality_level > 5))
186 {
187 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
188 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
189 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
190 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
191 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
192 }
193
194
195
196
197 // here comes the terrain haze model
198
199
200 float delta_z = hazeLayerAltitude - eye_alt;
201
202
203 if (dist > max(40.0, 0.07 * min(visibility,avisibility)))
204 {
205
206 alt = eye_alt;
207
208
209
210 float transmission;
211 float vAltitude;
212 float delta_zv;
213 float H;
214 float distance_in_layer;
215 float transmission_arg;
216
217
218
219
220 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
221
222 if (delta_z > 0.0) // we're inside the layer
223 {
224 if (ct < 0.0) // we look down
225 {
226 distance_in_layer = dist;
227 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
228 delta_zv = delta_z - vAltitude;
229 }
230 else // we may look through upper layer edge
231 {
232 H = dist * ct;
233 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
234 else {distance_in_layer = dist;}
235 vAltitude = min(distance_in_layer,visibility) * ct;
236 delta_zv = delta_z - vAltitude;
237 }
238 }
239 else // we see the layer from above, delta_z < 0.0
240 {
241 H = dist * -ct;
242 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
243 {
244 distance_in_layer = 0.0;
245 delta_zv = 0.0;
246 }
247 else
248 {
249 vAltitude = H + delta_z;
250 distance_in_layer = vAltitude/H * dist;
251 vAltitude = min(distance_in_layer,visibility) * (-ct);
252 delta_zv = vAltitude;
253 }
254 }
255
256
257 // blur of the haze layer edge
258
259 float blur_thickness = 50.0;
260 float cphi = dot(vec3(0.0, 1.0, 0.0), relPos)/dist;
261 float ctlayer;
262 float ctblur = 0.035 ;
263
264 float blur_dist;
265
266 if ((abs(delta_z) < 400.0)&&(quality_level>5)&&(tquality_level>5))
267 {
268 ctlayer = delta_z/dist-0.01 + 0.02 * Noise2D(vec2(cphi,1.0),0.1) -0.01;
269 blur_dist = dist * (1.0-smoothstep(0.0,300.0,-delta_z)) * smoothstep(-400.0,-200.0, -delta_z);
270 blur_dist = blur_dist * smoothstep(ctlayer-4.0*ctblur, ctlayer-ctblur, ct) * (1.0-smoothstep(ctlayer+0.5*ctblur, ctlayer+ctblur, ct));
271 distance_in_layer = max(distance_in_layer, blur_dist);
272 }
273
274 // ground haze cannot be thinner than aloft visibility in the model,
275 // so we need to use aloft visibility otherwise
276
277
278 transmission_arg = (dist-distance_in_layer)/avisibility;
279
280
281 float eqColorFactor;
282
283 //float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, relPos.z + eye_alt);
284
285 if (visibility < avisibility)
286 {
287 transmission_arg = transmission_arg + (distance_in_layer/visibility);
288 // this combines the Weber-Fechner intensity
289 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
290
291 }
292 else
293 {
294 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
295 // this combines the Weber-Fechner intensity
296 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
297 }
298
299
300
301 transmission = tree_fog_func(transmission_arg);
302
303 // there's always residual intensity, we should never be driven to zero
304 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
305
306
307
308
309 // now dim the light for haze
310 float eShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
311
312 // Mie-like factor
313
314 if (lightArg < 10.0)
315 {intensity = length(hazeColor);
316 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
317 hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_magnitude * (0.5 - 0.5 * mie_angle)) );
318 }
319
320 // high altitude desaturation of the haze color
321
322 intensity = length(hazeColor);
323 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
324
325 // blue hue of haze
326
327 hazeColor.x = hazeColor.x * 0.83;
328 hazeColor.y = hazeColor.y * 0.9;
329
330
331 // additional blue in indirect light
332 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
333 intensity = length(hazeColor);
334 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
335
336 // change haze color to blue hue for strong fogging
337
338 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
339
340
341
342 // determine the right mix of transmission and haze
343
344 //hazeColor = clamp(hazeColor,0.0,1.0);
345 fragColor.rgb = mix(eqColorFactor * hazeColor * eShade + secondary_light * fog_backscatter(avisibility), fragColor.rgb,transmission);
346
347 }
348
349 fragColor.rgb = filter_combined(fragColor.rgb);
350
351 gl_FragColor = fragColor;
352
353 }
354