Debian Codegraph @

Shaders/agriculture-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, May 2014
5 // based on ALS terrain shader
6 // Ambient term comes in gl_Color.rgb.
7 varying vec4 diffuse_term;
8 varying vec3 normal;
9 varying vec3 relPos;
10 varying vec2 rawPos;
11 varying vec3 worldPos;
12 varying vec3 ecViewdir;
13 varying vec4 ecPosition;
14
15
16 uniform sampler2D texture;
17 uniform sampler2D mix_texture;
18 uniform sampler2D grain_texture;
19 uniform sampler2D gradient_texture;
20
21
22 varying float steepness;
23 varying vec2 grad_dir;
24
25
26 uniform float visibility;
27 uniform float avisibility;
28 uniform float scattering;
29 uniform float terminator;
30 uniform float terrain_alt;
31 uniform float hazeLayerAltitude;
32 uniform float overcast;
33 uniform float eye_alt;
34 uniform float snowlevel;
35 uniform float dust_cover_factor;
36 uniform float lichen_cover_factor;
37 uniform float wetness;
38 uniform float fogstructure;
39 uniform float snow_thickness_factor;
40 uniform float cloud_self_shading;
41 uniform float season;
42 uniform float air_pollution;
43 uniform float grain_strength;
44 uniform float intrinsic_wetness;
45 uniform float overlay_fraction;
46 uniform float overlay_scale;
47 uniform float rotation_scale;
48 uniform float distortion_factor;
49 uniform float uv_xoffset;
50 uniform float uv_yoffset;
51 uniform float landing_light1_offset;
52 uniform float landing_light2_offset;
53 uniform float landing_light3_offset;
54
55 uniform float dust_resistance;
56 uniform float WindE;
57 uniform float WindN;
58 uniform float osg_SimulationTime;
59
60 uniform int quality_level;
61 uniform int tquality_level;
62 uniform int wind_effects;
63 uniform int cloud_shadow_flag;
64 uniform int rotation_flag;
65 uniform int use_searchlight;
66 uniform int use_landing_light;
67 uniform int use_alt_landing_light;
68
69
70 const float EarthRadius = 5800000.0;
71 const float terminator_width = 200000.0;
72
73 float alt;
74 float eShade;
75 float yprime_alt;
76 float mie_angle;
77
78 float shadow_func (in float x, in float y, in float noise, in float dist);
79 float Noise2D(in vec2 coord, in float wavelength);
80 float Noise3D(in vec3 coord, in float wavelength);
81 float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand);
82 float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness);
83 float fog_func (in float targ, in float alt);
84 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
85 float alt_factor(in float eye_alt, in float vertex_alt);
86 float light_distance_fading(in float dist);
87 float fog_backscatter(in float avisibility);
88
89 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
90 vec3 get_hazeColor(in float light_arg);
91 vec3 searchlight();
92 vec3 landing_light(in float offset, in float offsetv);
93 vec3 filter_combined (in vec3 color) ;
94
95 float getShadowing();
96 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
97
98
99 // a fade function for procedural scales which are smaller than a pixel
100
101 float detail_fade (in float scale, in float angle, in float dist)
102 {
103 float fade_dist = 2000.0 * scale * angle/max(pow(steepness,4.0), 0.1);
104
105 return 1.0 - smoothstep(0.5 * fade_dist, fade_dist, dist);
106 }
107
108
109
110
111
112 void main()
113 {
114
115
116 yprime_alt = diffuse_term.a;
117 //diffuse_term.a = 1.0;
118 mie_angle = gl_Color.a;
119 float effective_scattering = min(scattering, cloud_self_shading);
120
121 // distance to fragment
122 float dist = length(relPos);
123 // angle of view vector with horizon
124 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
125 // float altitude of fragment above sea level
126 float msl_altitude = (relPos.z + eye_alt);
127
128
129 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
130 // this is taken from default.frag
131 vec3 n;
132 float NdotL, NdotHV, fogFactor;
133 vec4 color = gl_Color;
134 color.a = 1.0;
135 vec3 lightDir = gl_LightSource[0].position.xyz;
136 vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
137 vec4 texel;
138 vec4 snow_texel;
139 vec4 detail_texel;
140 vec4 mix_texel;
141 vec4 grain_texel;
142 vec4 dot_texel;
143 vec4 gradient_texel;
144 vec4 foam_texel;
145 vec4 fragColor;
146 vec4 specular = vec4(0.0);
147 float intensity;
148
149
150
151 // Wind motion of the overlay noise simulating movement of vegetation and loose debris
152
153 vec2 windPos;
154
155 if (wind_effects > 1)
156 {
157 float windSpeed = length(vec2 (WindE,WindN)) /3.0480;
158 // interfering sine wave wind pattern
159 float sineTerm = sin(0.35 * windSpeed * osg_SimulationTime + 0.05 * (rawPos.x + rawPos.y));
160 sineTerm = sineTerm + sin(0.3 * windSpeed * osg_SimulationTime + 0.04 * (rawPos.x + rawPos.y));
161 sineTerm = sineTerm + sin(0.22 * windSpeed * osg_SimulationTime + 0.05 * (rawPos.x + rawPos.y));
162 sineTerm = sineTerm/3.0;
163 // non-linear amplification to simulate gusts
164 sineTerm = sineTerm * sineTerm;//smoothstep(0.2, 1.0, sineTerm);
165
166 // wind starts moving dust and leaves at around 8 m/s
167 float timeArg = 0.01 * osg_SimulationTime * windSpeed * smoothstep(8.0, 15.0, windSpeed);
168 timeArg = timeArg + 0.02 * sineTerm;
169
170 windPos = vec2 (rawPos.x + WindN * timeArg, rawPos.y + WindE * timeArg);
171 }
172 else
173 {
174 windPos = rawPos.xy;
175 }
176
177
178
179 // get noise at different wavelengths
180
181 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
182 // 50m: detail texel
183 // 250m: detail texel
184 // 500m: distortion and overlay
185 // 1500m: overlay, detail, dust, fog
186 // 2000m: overlay, detail, snow, fog
187
188 // Perlin noise
189
190 float noise_10m = Noise2D(rawPos.xy, 10.0);
191 float noise_5m = Noise2D(rawPos.xy ,5.0);
192 float noise_2m = Noise2D(rawPos.xy ,2.0);
193 float noise_1m = Noise2D(rawPos.xy ,1.0);
194 float noise_01m = Noise2D(windPos.xy, 0.1);
195
196 float noisegrad_10m;
197 float noisegrad_5m;
198 float noisegrad_2m;
199 float noisegrad_1m;
200
201
202
203 float noise_25m = Noise2D(rawPos.xy, 25.0);
204 float noise_50m = Noise2D(rawPos.xy, 50.0);
205
206
207 float noise_250m = Noise3D(worldPos.xyz,250.0);
208 float noise_500m = Noise3D(worldPos.xyz, 500.0);
209 float noise_1500m = Noise3D(worldPos.xyz, 1500.0);
210 float noise_2000m = Noise3D(worldPos.xyz, 2000.0);
211
212 // tiling noise
213
214 float vnoise_overlay = VoronoiNoise2D(rawPos.xy, overlay_scale, distortion_factor, distortion_factor);
215 float vnoise_rotation = VoronoiNoise2D(rawPos.xy, rotation_scale, distortion_factor, distortion_factor);
216
217
218
219 // slope noise
220
221 float slopenoise_50m = SlopeLines2D(rawPos, grad_dir, 50.0, steepness);
222 float slopenoise_100m = SlopeLines2D(rawPos, grad_dir, 100.0, steepness);
223
224 float snownoise_25m = mix(noise_25m, slopenoise_50m, clamp(3.0*(1.0-steepness),0.0,1.0));
225 float snownoise_50m = mix(noise_50m, slopenoise_100m, clamp(3.0*(1.0-steepness),0.0,1.0));
226
227
228
229 // get the texels
230
231 float distortion_factor = 1.0;
232 vec2 stprime;
233 int flag = 1;
234 int mix_flag = 1;
235 float noise_term;
236 float snow_alpha;
237
238 float angle = vnoise_rotation * 2.0 * 3.1415;
239
240 if (rotation_flag ==2)
241 {
242 if (vnoise_rotation < 0.25) {angle = 0.5 * 3.1415926;}
243 else if (vnoise_rotation < 0.5) {angle = 3.1415926;}
244 else if (vnoise_rotation < 0.75) {angle = 1.5 * 3.1415926;}
245 else {angle = 0.0;}
246 }
247
248 if (rotation_flag > 0)
249 {
250 stprime = vec2 (cos(angle)*gl_TexCoord[0].s + sin(angle)*gl_TexCoord[0].t, -sin(angle)*gl_TexCoord[0].s + cos(angle)*gl_TexCoord[0].t);
251 }
252 else
253 {stprime = gl_TexCoord[0].st;}
254 stprime+= vec2 (uv_xoffset, uv_yoffset);
255 texel = texture2D(texture, stprime);
256 float local_autumn_factor = texel.a;
257 grain_texel = texture2D(grain_texture, gl_TexCoord[0].st * 25.0);
258 gradient_texel = texture2D(gradient_texture, gl_TexCoord[0].st * 4.0);
259
260
261 // we need to fade procedural structures when they get smaller than a single pixel, for this we need
262 // to know under what angle we see the surface
263
264 float view_angle = abs(dot(normalize(normal), normalize(ecViewdir)));
265 float sfactor = sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
266
267 // the snow texel is generated procedurally
268 if (msl_altitude +500.0 > snowlevel)
269 {
270 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
271 snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
272 snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
273 snow_texel.a = 1.0;
274 noise_term = 0.1 * (noise_500m-0.5) ;
275 noise_term = noise_term + 0.2 * (snownoise_50m -0.5) * detail_fade(50.0, view_angle, 0.5*dist) ;
276 noise_term = noise_term + 0.2 * (snownoise_25m -0.5) * detail_fade(25.0, view_angle, 0.5*dist) ;
277 noise_term = noise_term + 0.3 * (noise_10m -0.5) * detail_fade(10.0, view_angle, 0.8*dist) ;
278 noise_term = noise_term + 0.3 * (noise_5m - 0.5) * detail_fade(5.0, view_angle, dist);
279 noise_term = noise_term + 0.15 * (noise_2m -0.5) * detail_fade(2.0, view_angle, dist);
280 noise_term = noise_term + 0.08 * (noise_1m -0.5) * detail_fade(1.0, view_angle, dist);
281 snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + snow_thickness_factor +0.0001*(msl_altitude -snowlevel) );
282 }
283
284 // the mixture/gradient texture
285 mix_texel = texture2D(mix_texture, stprime * 1.3);
286 if (mix_texel.a <0.1) {mix_flag = 0;}
287
288
289
290 // texture preparation according to detail level
291
292 // mix in hires texture patches
293
294 float dist_fact;
295 float nSum;
296 float mix_factor;
297
298 // first the second texture overlay
299 // transition model 0: random patch overlay without any gradient information
300 // transition model 1: only gradient-driven transitions, no randomness
301
302
303 if (mix_flag == 1)
304 {
305 //nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
306 //nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
307 //nSum = nSum + 0.4 * (1.0 -smoothstep(0.9,0.95, abs(steepness)+ 0.05 * (noise_50m - 0.5))) * min(1.0, 2.0 * transition_model);
308 //mix_factor = smoothstep(0.5, 0.54, nSum);
309 if (vnoise_overlay > overlay_fraction) {mix_factor = 0.0;} else {mix_factor = 1.0;}
310 texel = mix(texel, mix_texel, mix_factor);
311 local_autumn_factor = texel.a;
312 }
313
314
315
316 // rock for very steep gradients
317
318 if (gradient_texel.a > 0.0)
319 {
320 texel = mix(texel, gradient_texel, 1.0 - smoothstep(0.75,0.8,abs(steepness)+ 0.00002* msl_altitude + 0.05 * (noise_50m - 0.5)));
321 local_autumn_factor = texel.a;
322 }
323
324
325
326
327
328
329 // then the grain texture overlay
330
331 texel.rgb = mix(texel.rgb, grain_texel.rgb, grain_strength * grain_texel.a * (1.0 - mix_factor) * (1.0-smoothstep(2000.0,5000.0, dist)));
332
333 // for really hires, add procedural noise overlay
334 texel.rgb = texel.rgb * (1.0 + 0.4 * (noise_01m-0.5) * detail_fade(0.1, view_angle, dist)) ;
335
336 // autumn colors
337
338 float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
339
340
341 texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
342 texel.g = texel.g;
343 texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
344
345
346 if (local_autumn_factor < 1.0)
347 {
348 intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
349 texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
350 }
351
352 // slope line overlay
353 texel.rgb = texel.rgb * (1.0 - 0.12 * slopenoise_50m - 0.08 * slopenoise_100m);
354
355 //const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
356 const vec4 dust_color = vec4 (0.76, 0.65, 0.45, 1.0);
357 const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);
358
359 // mix vegetation
360 float gradient_factor = smoothstep(0.5, 1.0, steepness);
361 texel = mix(texel, lichen_color, gradient_factor * (0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m))) );
362 // mix dust
363 texel = mix(texel, dust_color, clamp(0.5 * dust_cover_factor *dust_resistance + 3.0 * dust_cover_factor * dust_resistance *(((noise_1500m - 0.5) * 0.125)+0.125 ) - 0.03*slopenoise_100m,0.0, 1.0) );
364 // mix snow
365 float snow_mix_factor = 0.0;
366 if (msl_altitude +500.0 > snowlevel)
367 {
368 snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
369 snow_mix_factor = snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * msl_altitude+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0);
370 texel = mix(texel, snow_texel, snow_mix_factor);
371 }
372
373
374
375
376 // get distribution of water when terrain is wet
377
378 float combined_wetness = min(1.0, wetness + intrinsic_wetness);
379 float water_threshold1;
380 float water_threshold2;
381 float water_factor =0.0;
382
383
384 if ((dist < 5000.0)&& (quality_level > 3) && (combined_wetness>0.0))
385 {
386 water_threshold1 = 1.0-0.5* combined_wetness;
387 water_threshold2 = 1.0 - 0.3 * combined_wetness;
388 water_factor = smoothstep(water_threshold1, water_threshold2 , (0.3 * (2.0 * (1.0-noise_10m) + (1.0 -noise_5m)) * (1.0 - smoothstep(2000.0, 5000.0, dist))) - 5.0 * (1.0 -steepness));
389 }
390
391 // darken wet terrain
392
393 texel.rgb = texel.rgb * (1.0 - 0.6 * combined_wetness);
394
395
396
397 // light computations
398
399
400 vec4 light_specular = gl_LightSource[0].specular;
401
402 // If gl_Color.a == 0, this is a back-facing polygon and the
403 // normal should be reversed.
404 //n = (2.0 * gl_Color.a - 1.0) * normal;
405 n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
406 n = normalize(n);
407
408 NdotL = dot(n, lightDir);
409
410 noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
411 noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
412 noisegrad_2m = (noise_2m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),2.0))/0.05;
413 noisegrad_1m = (noise_1m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),1.0))/0.05;
414
415
416
417 NdotL = NdotL + (noisegrad_10m * detail_fade(10.0, view_angle,dist) + 0.5* noisegrad_5m * detail_fade(5.0, view_angle,dist)) * mix_factor/0.8;
418 NdotL = NdotL + 0.15 * noisegrad_2m * mix_factor/0.8 * detail_fade(2.0,view_angle,dist);
419 NdotL = NdotL + 0.1 * noisegrad_2m * detail_fade(2.0,view_angle,dist);
420 NdotL = NdotL + 0.05 * noisegrad_1m * detail_fade(1.0, view_angle,dist);
421
422 if (NdotL > 0.0) {
423 float shadowmap = getShadowing();
424 if (cloud_shadow_flag == 1) {NdotL = NdotL * shadow_func(relPos.x, relPos.y, 0.3 * noise_250m + 0.5 * noise_500m+0.2 * noise_1500m, dist);}
425 color += diffuse_term * NdotL * shadowmap;
426 NdotHV = max(dot(n, halfVector), 0.0);
427 if (gl_FrontMaterial.shininess > 0.0)
428 specular.rgb = ((gl_FrontMaterial.specular.rgb * 0.1 + (water_factor * vec3 (1.0, 1.0, 1.0)))
429 * light_specular.rgb
430 * pow(NdotHV, gl_FrontMaterial.shininess + (20.0 * water_factor))
431 * shadowmap);
432 }
433 color.a = 1.0;//diffuse_term.a;
434 // This shouldn't be necessary, but our lighting becomes very
435 // saturated. Clamping the color before modulating by the texture
436 // is closer to what the OpenGL fixed function pipeline does.
437 color = clamp(color, 0.0, 1.0);
438
439 vec3 secondary_light = vec3 (0.0,0.0,0.0);
440
441 if (use_searchlight == 1)
442 {
443 secondary_light += searchlight();
444 }
445 if (use_landing_light == 1)
446 {
447 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
448 }
449 if (use_alt_landing_light == 1)
450 {
451 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
452 }
453 color.rgb +=secondary_light * light_distance_fading(dist);
454
455
456 fragColor = color * texel + specular;
457 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
458
459 float lightArg = (terminator-yprime_alt)/100000.0;
460 vec3 hazeColor = get_hazeColor(lightArg);
461
462
463
464 // Rayleigh color shift due to out-scattering
465 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
466 float outscatter = 1.0-exp(-dist/rayleigh_length);
467 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
468
469 // Rayleigh color shift due to in-scattering
470
471 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
472 //float lightIntensity = length(diffuse_term.rgb)/1.73 * rShade;
473 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
474 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
475 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
476 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
477
478
479 // here comes the terrain haze model
480
481
482 float delta_z = hazeLayerAltitude - eye_alt;
483
484 float mvisibility = min(visibility,avisibility);
485
486 if (dist > 0.04 * mvisibility)
487 {
488
489 alt = eye_alt;
490
491
492 float transmission;
493 float vAltitude;
494 float delta_zv;
495 float H;
496 float distance_in_layer;
497 float transmission_arg;
498
499
500
501
502 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
503
504 if (delta_z > 0.0) // we're inside the layer
505 {
506 if (ct < 0.0) // we look down
507 {
508 distance_in_layer = dist;
509 vAltitude = min(distance_in_layer,mvisibility) * ct;
510 delta_zv = delta_z - vAltitude;
511 }
512 else // we may look through upper layer edge
513 {
514 H = dist * ct;
515 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
516 else {distance_in_layer = dist;}
517 vAltitude = min(distance_in_layer,visibility) * ct;
518 delta_zv = delta_z - vAltitude;
519 }
520 }
521 else // we see the layer from above, delta_z < 0.0
522 {
523 H = dist * -ct;
524 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
525 {
526 distance_in_layer = 0.0;
527 delta_zv = 0.0;
528 }
529 else
530 {
531 vAltitude = H + delta_z;
532 distance_in_layer = vAltitude/H * dist;
533 vAltitude = min(distance_in_layer,visibility) * (-ct);
534 delta_zv = vAltitude;
535 }
536 }
537
538 // blur of the haze layer edge
539
540 float blur_thickness = 50.0;
541 float cphi = dot(vec3(0.0, 1.0, 0.0), relPos)/dist;
542 float ctlayer = delta_z/dist-0.01 + 0.02 * Noise2D(vec2(cphi,1.0),0.1) -0.01;
543 float ctblur = 0.035 ;
544
545 float blur_dist;
546
547 if (abs(delta_z) < 400.0)
548 {
549 blur_dist = dist * (1.0-smoothstep(0.0,300.0,-delta_z)) * smoothstep(-400.0,-200.0, -delta_z);
550 blur_dist = blur_dist * smoothstep(ctlayer-4.0*ctblur, ctlayer-ctblur, ct) * (1.0-smoothstep(ctlayer+0.5*ctblur, ctlayer+ctblur, ct));
551 distance_in_layer = max(distance_in_layer, blur_dist);
552 }
553
554
555 // ground haze cannot be thinner than aloft visibility in the model,
556 // so we need to use aloft visibility otherwise
557
558
559 transmission_arg = (dist-distance_in_layer)/avisibility;
560
561
562 float eqColorFactor;
563
564
565
566 if (visibility < avisibility)
567 {
568 if (quality_level > 3)
569 {
570 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
571
572 }
573 else
574 {
575 transmission_arg = transmission_arg + (distance_in_layer/visibility);
576 }
577 // this combines the Weber-Fechner intensity
578 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 - effective_scattering);
579
580 }
581 else
582 {
583 if (quality_level > 3)
584 {
585 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
586 }
587 else
588 {
589 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
590 }
591 // this combines the Weber-Fechner intensity
592 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
593 }
594
595
596
597 transmission = fog_func(transmission_arg, alt);
598
599 // there's always residual intensity, we should never be driven to zero
600 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
601
602
603 //float lightArg = (terminator-yprime_alt)/100000.0;
604
605
606
607 // now dim the light for haze
608 eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
609
610 // Mie-like factor
611
612 if (lightArg < 10.0)
613 {
614 intensity = length(hazeColor);
615 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
616 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)) );
617 }
618
619 intensity = length(hazeColor);
620
621 if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
622 {
623
624
625 // high altitude desaturation of the haze color
626 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
627
628 // blue hue of haze
629 hazeColor.x = hazeColor.x * 0.83;
630 hazeColor.y = hazeColor.y * 0.9;
631
632
633 // additional blue in indirect light
634 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
635 intensity = length(hazeColor);
636 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
637
638 // change haze color to blue hue for strong fogging
639 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
640
641
642
643 // reduce haze intensity when looking at shaded surfaces, only in terminator region
644 float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
645 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
646 }
647
648 // don't let the light fade out too rapidly
649 lightArg = (terminator + 200000.0)/100000.0;
650 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
651 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
652
653 hazeColor.rgb *= eqColorFactor * eShade;
654 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
655
656 fragColor.rgb = mix(hazeColor + secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
657
658 }
659
660 fragColor.rgb = filter_combined(fragColor.rgb);
661
662 gl_FragColor = fragColor;
663
664 }
665