Debian Codegraph @

Shaders/terrain-ALS-ultra.frag

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