1// -*-C++-*-
2#version 120
3
4// adapted by James Hester 2018, based on rock-ALS.frag
5// by Thorsten Renk.
6// Ambient term comes in gl_Color.rgb.
7varying vec4 diffuse_term;
8varying vec3 normal;
9varying vec3 relPos;
10varying vec2 rawPos;
11varying vec3 worldPos;
12varying vec3 ecViewdir;
13
14
15
16varying float steepness;
17varying vec2 grad_dir;
18
19varying float flogz;
20
21
22uniform float fg_Fcoef;
23
24uniform float visibility;
25uniform float avisibility;
26uniform float scattering;
27uniform float terminator;
28uniform float terrain_alt;
29uniform float hazeLayerAltitude;
30uniform float overcast;
31uniform float eye_alt;
32uniform float dust_cover_factor;
33uniform float lichen_cover_factor;
34uniform float wetness;
35uniform float fogstructure;
36uniform float cloud_self_shading;
37uniform float contrast;
38uniform float air_pollution;
39uniform float intrinsic_wetness;
40uniform float transition_model;
41uniform float overlay_bias;
42uniform float crack_depth;
43uniform float crack_pattern_stretch;
44uniform float grain_fade_power;
45uniform float rock_brightness;
46uniform float overlay_alpha;
47uniform float dust_resistance;
48uniform float slopeline_strength;
49uniform float landing_light1_offset;
50uniform float landing_light2_offset;
51uniform float landing_light3_offset;
52uniform float osg_SimulationTime;
53
54uniform vec3 base_color;
55uniform vec3 overlay_color;
56
57uniform int wind_effects;
58uniform int cloud_shadow_flag;
59uniform int rock_strata;
60uniform int use_searchlight;
61uniform int use_landing_light;
62uniform int use_alt_landing_light;
63
64const float EarthRadius = 5800000.0;
65const float terminator_width = 200000.0;
66
67float alt;
68float eShade;
69float yprime_alt;
70float mie_angle;
71
72float shadow_func (in float x, in float y, in float noise, in float dist);
73float Noise2D(in vec2 coord, in float wavelength);
74float Noise3D(in vec3 coord, in float wavelength);
75float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand);
76float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness);
77float Strata3D(in vec3 coord, in float wavelength, in float variation);
78float fog_func (in float targ, in float alt);
79float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
80float alt_factor(in float eye_alt, in float vertex_alt);
81float light_distance_fading(in float dist);
82float fog_backscatter(in float avisibility);
83
84vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
85vec3 get_hazeColor(in float light_arg);
86vec3 searchlight();
87vec3 landing_light(in float offset, in float offsetv);
88vec3 filter_combined (in vec3 color) ;
89
90float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
91{
92x = x - 0.5;
93
94// use the asymptotics to shorten computations
95if (x > 30.0) {return e;}
96if (x < -15.0) {return 0.0;}
97
98return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
99}
100
101
102// a fade function for procedural scales which are smaller than a pixel
103
104float detail_fade (in float scale, in float angle, in float dist)
105{
106float fade_dist = 2000.0 * scale * angle/max(pow(steepness,4.0), 0.1);
107
108return 1.0 - smoothstep(0.5 * fade_dist, fade_dist, dist);
109}
110
111
112
113void main()
114{
115
116
117yprime_alt = diffuse_term.a;
118//diffuse_term.a = 1.0;
119mie_angle = gl_Color.a;
120float effective_scattering = min(scattering, cloud_self_shading);
121
122// distance to fragment
123float dist = length(relPos);
124// horizontal distance using texture coordinate
125float horiz_coord = gl_TexCoord[0].s * 1000.0;
126// angle of view vector with horizon
127float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
128// float altitude of fragment above base of cliff
129float cliff_altitude = gl_TexCoord[0].t * 1000.0;
130
131
132  vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
133// this is taken from default.frag
134    vec3 n;
135    float NdotL, NdotHV, fogFactor;
136    vec4 color = gl_Color;
137    color.a = 1.0;
138    vec3 lightDir = gl_LightSource[0].position.xyz;
139    vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
140    vec4 texel;
141    vec4 detail_texel;
142    vec4 mix_texel;
143	vec4 grain_texel;
144	vec4 dot_texel;
145	vec4 gradient_texel;
146	vec4 foam_texel;
147    vec4 fragColor;
148    vec4 specular = vec4(0.0);
149    float intensity;
150
151
152
153// Perlin noise, distance along cliff in texture coordinate instead of x,y
154
155float noise_100m = Noise2D(vec2(horiz_coord,cliff_altitude),100.0);
156float noise_50m = Noise2D(vec2(horiz_coord,cliff_altitude), 50.0);
157float noise_25m = Noise2D(vec2(horiz_coord,cliff_altitude), 25.0);
158float noise_10m = Noise2D(vec2(horiz_coord,cliff_altitude), 10.0);
159float noise_5m = Noise2D(vec2(horiz_coord,cliff_altitude) ,5.0);
160float noise_2m = Noise2D(vec2(horiz_coord,cliff_altitude) ,2.0);
161float noise_1m = Noise2D(vec2(horiz_coord,cliff_altitude) ,1.0);
162float noise_05m = Noise2D(vec2(horiz_coord,cliff_altitude),0.5);
163float noise_02m = Noise2D(vec2(horiz_coord,cliff_altitude),0.2);
164float noise_01m = Noise2D(vec2(horiz_coord,cliff_altitude), 0.1);
165
166float noisegrad_10m;
167float noisegrad_5m;
168float noisegrad_2m;
169float noisegrad_1m;
170float noisegrad_05m;
171float noisegrad_02m;
172float noisegrad_01m;
173
174
175
176
177
178float noise_250m = Noise3D(worldPos.xyz,250.0);
179float noise_500m = Noise3D(worldPos.xyz, 500.0);
180float noise_1500m = Noise3D(worldPos.xyz, 1500.0);
181float noise_2000m = Noise3D(worldPos.xyz, 2000.0);
182
183// dot noise
184
185float dotnoise_2m = 0.0;
186float dotnoise_10m = 0.0;
187float dotnoise_15m = 0.0;
188
189float dotnoisegrad_10m;
190
191// slope noise
192
193float slopenoise_50m = SlopeLines2D(rawPos, grad_dir, 50.0, steepness);
194float slopenoise_100m = SlopeLines2D(rawPos, grad_dir, 100.0, steepness);
195
196
197
198
199
200
201// get the texels
202
203    float noise_term;
204    float local_autumn_factor;
205
206	// we need to fade procedural structures when they get smaller than a single pixel, for this we need
207	// to know under what angle we see the surface
208
209    float view_angle = abs(dot(normalize(normal), normalize(ecViewdir)));
210
211   // strata noise
212
213	float stratnoise_50m;
214	float stratnoise_10m;
215
216	if (rock_strata==1)
217		{
218		stratnoise_50m = Strata3D(vec3 (rawPos.x, rawPos.y, cliff_altitude), 50.0, 0.2);
219		stratnoise_10m = Strata3D(vec3 (rawPos.x, rawPos.y, cliff_altitude), 10.0, 0.2);
220		stratnoise_50m = mix(stratnoise_50m, 1.0, smoothstep(0.8,0.9, steepness));
221		stratnoise_10m = mix(stratnoise_10m, 1.0, smoothstep(0.8,0.9, steepness));
222		texel *= (0.4 + 0.4 * stratnoise_50m + 0.2 * stratnoise_10m);
223		}
224
225
226
227
228
229
230
231 // procedural rock texture generation
232
233 texel.rgb = base_color;
234
235 // use powers of Perlin noise to generate the base pattern
236
237 float grainy_noise;
238 float fade_norm;
239
240 float gfp = grain_fade_power;
241 float gfptmp;
242
243 //grainy_noise = (0.5 * (1.0-slopenoise_100m) + 0.5 *noise_50m) + gfp * (0.5 * slopenoise_50m + 0.5 * noise_25m);
244 grainy_noise = (0.5 *noise_50m) + gfp * ( 0.5 * noise_25m);
245 fade_norm = 1.0+gfp;
246
247 gfptmp = gfp * gfp;
248 grainy_noise += noise_10m * gfptmp * detail_fade(10.0, view_angle, dist) ;
249 fade_norm += gfptmp * detail_fade(10.0, view_angle, dist) ;
250
251 gfptmp = gfptmp * gfp;
252 grainy_noise += noise_5m * gfptmp * detail_fade(5.0, view_angle, dist) ;
253 fade_norm += gfptmp * detail_fade(5.0, view_angle, dist) ;
254
255 gfptmp = gfptmp * gfp;
256 grainy_noise += noise_2m * gfptmp * detail_fade(2.0, view_angle, dist) ;
257 fade_norm += gfptmp * detail_fade(2.0, view_angle, dist) ;
258
259 gfptmp = gfptmp * gfp;
260 grainy_noise += noise_1m * gfptmp * detail_fade(1.0, view_angle, dist) ;
261 fade_norm += gfptmp * detail_fade(1.0, view_angle, dist) ;
262
263 gfptmp = gfptmp * gfp;
264 grainy_noise += noise_05m * gfptmp * detail_fade(0.5, view_angle, dist) ;
265 fade_norm += gfptmp * detail_fade(0.5, view_angle, dist) ;
266
267 grainy_noise = grainy_noise/fade_norm;
268 grainy_noise = smoothstep(-0.2, 1.2, grainy_noise);
269
270 // generate the crack pattern from isovalue lines of stretched Perlin noise
271
272 float cnoise_500m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 500.0);
273 float cnoise_250m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 250.0);
274 float cnoise_100m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 100.0);
275 float cnoise_50m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 50.0);
276 float cnoise_25m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 25.0);
277 float cnoise_10m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 10.0);
278 float cnoise_5m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 5.0);
279 float cnoise_2m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 2.0);
280 float cnoise_1m = Noise2D(vec2(horiz_coord+10.0, crack_pattern_stretch * cliff_altitude), 1.0);
281
282 float crack_noise;
283 float crack_factor;
284 float crack_size;
285 float scrack_noise;
286 float scrack_size;
287
288 crack_noise = cnoise_500m + 0.65 * cnoise_250m + 0.42 * cnoise_100m * detail_fade(50.0, view_angle, dist) ;
289 crack_noise = crack_noise + 0.27 * cnoise_50m * detail_fade(25.0, view_angle, dist) ;
290 crack_noise = crack_noise + 0.17 * cnoise_25m * detail_fade(10.0, view_angle, dist) ;
291 crack_noise = crack_noise + 0.11 * cnoise_10m * detail_fade(5.0, view_angle, dist) ;
292 crack_noise = 0.381 * crack_noise;
293
294
295
296 scrack_noise = cnoise_10m + 0.65 * cnoise_5m * detail_fade(5.0, view_angle, dist);
297 scrack_noise = scrack_noise + 0.3 * cnoise_2m + 0.1 * cnoise_1m * detail_fade(1.0, view_angle, dist);
298 scrack_noise = 0.48 * scrack_noise;
299
300  crack_size = 0.02 +0.00001 * dist;
301  crack_factor = smoothstep(0.5-crack_size,0.50,crack_noise) * (1.0-smoothstep(0.51,0.51+crack_size,crack_noise));
302  //crack_factor = step(0.5-0.2*crack_size,crack_noise) * (1.0-step(0.5+0.2*crack_size,crack_noise));
303
304  crack_size *= 0.5;
305  crack_factor += smoothstep(0.42,0.42+crack_size,crack_noise) * (1.0-smoothstep(0.43,0.43+crack_size,crack_noise));
306
307  scrack_size = crack_size * 4.0;
308  crack_factor += 0.75 * smoothstep(0.5-scrack_size,0.50,scrack_noise) * (1.0-smoothstep(0.51,0.51+scrack_size,scrack_noise))* (1.0- smoothstep(250.0,1000.0,dist));
309
310
311  crack_factor = crack_factor * min(1.0,0.03/crack_size);
312
313
314
315// distribution of overlay color
316
317
318 float overlay_noise;
319 float overlay_factor;
320
321 overlay_noise = 0.381 * (noise_50m + 0.65 * noise_25m + 0.42 * noise_10m + 0.27 * noise_5m + 0.17 * noise_2m + 0.11 * noise_1m);
322 overlay_noise = overlay_noise + 0.1 * (smoothstep(0.8,0.9, steepness));
323
324 overlay_factor = smoothstep(0.7, 0.72, overlay_noise + overlay_bias) + (1.0 - smoothstep(0.2, 0.22, overlay_noise - overlay_bias));
325
326
327
328// merge the noise components
329
330 //grainy_noise = grainy_noise * (1.0-crack_depth * crack_factor) + 0.5 * crack_depth * crack_factor;
331 texel.rgb = ((1.0 - contrast)  + contrast * grainy_noise ) * texel.rgb;
332 texel.rgb = mix(texel.rgb, overlay_color.rgb,overlay_alpha * overlay_factor);
333 texel.rgb = texel.rgb * ((1.0-crack_depth) +crack_depth*(1.0-crack_factor * (0.5 + 0.5 * noise_50m) ));
334
335 texel.rgb = texel.rgb * rock_brightness;
336
337 texel.rgb = texel.rgb * (1.0 + 0.4 * (noise_01m-0.5) * detail_fade(0.1, view_angle, dist)) ;
338
339
340const vec4 dust_color  = vec4 (0.76, 0.65, 0.45, 1.0);
341const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);
342
343// mix vegetation
344float gradient_factor = smoothstep(0.5, 1.0, steepness);
345texel = mix(texel, lichen_color, gradient_factor * (0.4 * lichen_cover_factor +  0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)))  );
346// mix dust
347texel = 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.0, 1.0) );
348
349// get distribution of water when terrain is wet
350
351float combined_wetness = min(1.0, wetness + intrinsic_wetness);
352float water_threshold1;
353float water_threshold2;
354float water_factor =0.0;
355
356
357if ((dist < 5000.0) && (combined_wetness>0.0))
358		{
359		water_threshold1 = 1.0-0.5* combined_wetness;
360		water_threshold2 = 1.0 - 0.3 * combined_wetness;
361		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));
362	}
363
364// darken wet terrain
365
366    texel.rgb = texel.rgb * (1.0 - 0.6 * combined_wetness);
367
368
369
370// light computations
371
372
373    vec4 light_specular = gl_LightSource[0].specular;
374
375    // If gl_Color.a == 0, this is a back-facing polygon and the
376    // normal should be reversed.
377    //n = (2.0 * gl_Color.a - 1.0) * normal;
378    n = normal;
379    n = normalize(n);
380
381    NdotL = dot(n, lightDir);
382
383	noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
384	noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
385	noisegrad_2m = (noise_2m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),2.0))/0.05;
386	noisegrad_1m = (noise_1m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),1.0))/0.05;
387	noisegrad_05m = (noise_05m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),0.5))/0.05;
388
389
390	dotnoisegrad_10m = 0.0;
391
392	float fresnel;
393
394    if (NdotL > 0.0) {
395	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);}
396        color += diffuse_term * NdotL;
397        NdotHV = max(dot(n, halfVector), 0.0);
398
399		fresnel = 1.0 + 5.0 * (1.0-smoothstep(0.0,0.2, dot(normalize(ecViewdir),n)));
400
401        //if (gl_FrontMaterial.shininess > 0.0)
402            specular.rgb = ((gl_FrontMaterial.specular.rgb * 0.1 + (water_factor * vec3 (1.0, 1.0, 1.0)))
403                            * light_specular.rgb
404                            * pow(NdotHV, max(gl_FrontMaterial.shininess,4.0) + (20.0 * water_factor)));
405    }
406    color.a = 1.0;//diffuse_term.a;
407    // This shouldn't be necessary, but our lighting becomes very
408    // saturated. Clamping the color before modulating by the texture
409    // is closer to what the OpenGL fixed function pipeline does.
410    color = clamp(color, 0.0, 1.0);
411
412   vec3 secondary_light = vec3 (0.0,0.0,0.0);
413
414    if (use_searchlight == 1)
415	{
416	secondary_light += searchlight();
417	}
418    if (use_landing_light == 1)
419	{
420	secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
421	}
422    if (use_alt_landing_light == 1)
423	{
424	secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
425	}
426    color.rgb +=secondary_light * light_distance_fading(dist);
427
428
429    fragColor = color * texel + specular;
430
431float lightArg = (terminator-yprime_alt)/100000.0;
432vec3 hazeColor = get_hazeColor(lightArg);
433
434
435
436// Rayleigh color shift due to out-scattering
437    float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
438    float outscatter = 1.0-exp(-dist/rayleigh_length);
439    fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
440
441// Rayleigh color shift due to in-scattering
442
443   float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
444   float lightIntensity = length(hazeColor * effective_scattering) * rShade;
445   vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
446   float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
447  fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
448
449
450// here comes the terrain haze model
451
452
453float delta_z = hazeLayerAltitude - eye_alt;
454
455float mvisibility = min(visibility,avisibility);
456
457if (dist > 0.04 * mvisibility)
458
459{
460
461alt = eye_alt;
462
463
464float transmission;
465float vAltitude;
466float delta_zv;
467float H;
468float distance_in_layer;
469float transmission_arg;
470
471
472
473
474// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
475
476if (delta_z > 0.0) // we're inside the layer
477	{
478	if (ct < 0.0) // we look down
479		{
480		distance_in_layer = dist;
481		vAltitude = min(distance_in_layer,mvisibility) * ct;
482  		delta_zv = delta_z - vAltitude;
483		}
484	else 	// we may look through upper layer edge
485		{
486		H = dist * ct;
487		if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
488		else {distance_in_layer = dist;}
489		vAltitude = min(distance_in_layer,visibility) * ct;
490  		delta_zv = delta_z - vAltitude;
491		}
492	}
493  else // we see the layer from above, delta_z < 0.0
494	{
495	H = dist * -ct;
496	if (H  < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
497		{
498		distance_in_layer = 0.0;
499		delta_zv = 0.0;
500		}
501	else
502		{
503		vAltitude = H + delta_z;
504		distance_in_layer = vAltitude/H * dist;
505		vAltitude = min(distance_in_layer,visibility) * (-ct);
506		delta_zv = vAltitude;
507		}
508	}
509
510// blur of the haze layer edge
511
512float blur_thickness = 50.0;
513float cphi = dot(vec3(0.0, 1.0, 0.0), relPos)/dist;
514float ctlayer = delta_z/dist-0.01 + 0.02 * Noise2D(vec2(cphi,1.0),0.1) -0.01;
515float ctblur = 	0.035 ;
516
517float blur_dist;
518
519if (abs(delta_z) < 400.0)
520	{
521	blur_dist = dist * (1.0-smoothstep(0.0,300.0,-delta_z)) * smoothstep(-400.0,-200.0, -delta_z);
522	blur_dist = blur_dist * smoothstep(ctlayer-4.0*ctblur, ctlayer-ctblur, ct) * (1.0-smoothstep(ctlayer+0.5*ctblur, ctlayer+ctblur, ct));
523	distance_in_layer = max(distance_in_layer, blur_dist);
524	}
525
526
527// ground haze cannot be thinner than aloft visibility in the model,
528// so we need to use aloft visibility otherwise
529
530
531transmission_arg = (dist-distance_in_layer)/avisibility;
532
533
534float eqColorFactor;
535
536
537
538if (visibility < avisibility)
539	{
540	transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
541	// this combines the Weber-Fechner intensity
542	eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 - effective_scattering);
543	}
544else
545	{
546	transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m  - 1.0) ));
547	// this combines the Weber-Fechner intensity
548	eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
549	}
550
551
552
553transmission =  fog_func(transmission_arg, alt);
554
555// there's always residual intensity, we should never be driven to zero
556if (eqColorFactor < 0.2) eqColorFactor = 0.2;
557
558
559
560
561
562
563
564// now dim the light for haze
565eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
566
567// Mie-like factor
568
569	if (lightArg < 10.0)
570		{
571		intensity = length(hazeColor);
572		float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
573		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)) );
574		}
575
576intensity = length(hazeColor);
577
578if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
579{
580
581
582	// high altitude desaturation of the haze color
583	hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
584
585	// blue hue of haze
586	hazeColor.x = hazeColor.x * 0.83;
587	hazeColor.y = hazeColor.y * 0.9;
588
589
590	// additional blue in indirect light
591	float fade_out = max(0.65 - 0.3 *overcast, 0.45);
592	intensity = length(hazeColor);
593	hazeColor = intensity * normalize(mix(hazeColor,  1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
594
595	// change haze color to blue hue for strong fogging
596	hazeColor = intensity * normalize(mix(hazeColor,  shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
597
598
599
600	// reduce haze intensity when looking at shaded surfaces, only in terminator region
601	float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
602	hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
603	}
604
605// don't let the light fade out too rapidly
606lightArg = (terminator + 200000.0)/100000.0;
607float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
608vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
609
610hazeColor.rgb *= eqColorFactor * eShade;
611hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
612
613
614fragColor.rgb = mix(hazeColor + secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
615
616}
617
618fragColor.rgb = filter_combined(fragColor.rgb);
619
620gl_FragColor = fragColor;
621// logarithmic depth
622gl_FragDepth = log2(flogz) * fg_Fcoef * 0.5;
623
624}
625
626