Debian Codegraph @

Shaders/model-ALS-ultra.frag

raw
1 // -*- mode: C; -*-
2 // Licence: GPL v2
3 // Authors: Frederic Bouvier and Gijs de Rooy
4 // with major additions and revisions by
5 // Emilian Huminiuc and Vivian Meazza 2011
6 // ported to Atmospheric Light Scattering
7 // by Thorsten Renk, 2013
8 #version 120
9
10 varying vec3 VBinormal;
11 varying vec3 VNormal;
12 varying vec3 VTangent;
13 varying vec3 rawpos;
14 varying vec3 reflVec;
15 varying vec3 vViewVec;
16 varying vec3 vertVec;
17
18 varying float alpha;
19
20 uniform sampler2D BaseTex;
21 uniform sampler2D LightMapTex;
22 uniform sampler2D NormalTex;
23 uniform sampler2D ReflMapTex;
24 uniform sampler2D ReflGradientsTex;
25 uniform sampler3D ReflNoiseTex;
26 uniform samplerCube Environment;
27 uniform sampler2D GrainTex;
28
29 uniform int dirt_enabled;
30 uniform int dirt_multi;
31 uniform int dirt_modulates_reflection;
32 uniform int lightmap_enabled;
33 uniform int lightmap_multi;
34 uniform int nmap_dds;
35 uniform int nmap_enabled;
36 uniform int refl_enabled;
37 uniform int refl_type;
38 uniform int refl_map;
39 uniform int grain_texture_enabled;
40 uniform int rain_enabled;
41 uniform int cloud_shadow_flag;
42 uniform int use_searchlight;
43 uniform int use_landing_light;
44 uniform int use_alt_landing_light;
45 uniform int snow_enabled;
46 uniform int building_flag;
47
48 uniform float amb_correction;
49 uniform float dirt_b_factor;
50 uniform float dirt_g_factor;
51 uniform float dirt_r_factor;
52 uniform float dirt_reflection_factor;
53 uniform float lightmap_a_factor;
54 uniform float lightmap_b_factor;
55 uniform float lightmap_g_factor;
56 uniform float lightmap_r_factor;
57 uniform float nmap_tile;
58 uniform float refl_correction;
59 uniform float refl_fresnel;
60 uniform float refl_fresnel_factor;
61 uniform float refl_noise;
62 uniform float refl_rainbow;
63 uniform float grain_magnification;
64 uniform float wetness;
65 uniform float rain_norm;
66
67 uniform float avisibility;
68 uniform float cloud_self_shading;
69 uniform float eye_alt;
70 uniform float ground_scattering;
71 uniform float hazeLayerAltitude;
72 uniform float moonlight;
73 uniform float overcast;
74 uniform float scattering;
75 uniform float terminator;
76 uniform float terrain_alt;
77 uniform float visibility;
78 uniform float air_pollution;
79 uniform float snowlevel;
80 uniform float snow_thickness_factor;
81
82 uniform int quality_level;
83 uniform int tquality_level;
84
85 uniform float osg_SimulationTime;
86 uniform mat4 osg_ViewMatrix;
87
88 uniform float landing_light1_offset;
89 uniform float landing_light2_offset;
90 uniform float landing_light3_offset;
91
92 uniform bool use_IR_vision;
93
94 // constants needed by the light and fog computations ###################################################
95
96 const float EarthRadius = 5800000.0;
97 const float terminator_width = 200000.0;
98
99 uniform vec3 lightmap_r_color;
100 uniform vec3 lightmap_g_color;
101 uniform vec3 lightmap_b_color;
102 uniform vec3 lightmap_a_color;
103
104 uniform vec3 dirt_r_color;
105 uniform vec3 dirt_g_color;
106 uniform vec3 dirt_b_color;
107
108 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
109 float Noise2D(in vec2 coord, in float wavelength);
110 float shadow_func (in float x, in float y, in float noise, in float dist);
111 float fog_func (in float targ, in float altitude);
112 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
113 float alt_factor(in float eye_alt, in float vertex_alt);
114 float light_distance_fading(in float dist);
115 float fog_backscatter(in float avisibility);
116
117 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
118 vec3 get_hazeColor(in float lightArg);
119 vec3 searchlight();
120 vec3 landing_light(in float offset, in float offsetv);
121 vec3 filter_combined (in vec3 color) ;
122 vec3 moonlight_perception (in vec3 light) ;
123 vec3 addLights(in vec3 color1, in vec3 color2);
124
125 float getShadowing();
126 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
127
128
129 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
130 {
131 if (x > 30.0) {return e;}
132 if (x < -15.0) {return 0.0;}
133 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
134 }
135
136
137
138
139
140 void main (void)
141 {
142 vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st);
143 vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st * nmap_tile);
144 vec4 reflmap = texture2D(ReflMapTex, gl_TexCoord[0].st);
145 vec4 noisevec = texture3D(ReflNoiseTex, rawpos.xyz);
146 vec4 lightmapTexel = texture2D(LightMapTex, gl_TexCoord[0].st);
147
148 vec4 grainTexel;
149
150 vec3 mixedcolor;
151 vec3 N = vec3(0.0,0.0,1.0);
152
153
154 float pf = 0.0;
155 float pf1 = 0.0;
156 ///some generic light scattering parameters
157 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
158 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight;
159 moonLightColor = moonlight_perception(moonLightColor);
160 float alt = eye_alt;
161 float effective_scattering = min(scattering, cloud_self_shading);
162
163
164 /// BEGIN geometry for light
165
166 vec3 up;
167
168 if (building_flag == 1) // osg_ViewMatrix contains the transforms of animations, but doesn't work for random buildings
169 {
170 up = (gl_ModelViewMatrix * vec4(0.0,0.0,1.0,0.0)).xyz;
171 }
172 else
173 {
174 up = (osg_ViewMatrix * vec4(0.0,0.0,1.0,0.0)).xyz;
175 }
176 //vec4 worldPos3D = (osg_ViewMatrixInverse * vec4 (0.0,0.0,0.0, 1.0));
177 //worldPos3D.a = 0.0;
178 //vec3 up = (osg_ViewMatrix * worldPos3D).xyz;
179 float dist = length(vertVec);
180 float vertex_alt = max(100.0,dot(up, vertVec) + alt);
181 float vertex_scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
182
183
184 vec3 lightHorizon = gl_LightSource[0].position.xyz - up * dot(up,gl_LightSource[0].position.xyz);
185 float yprime = -dot(vertVec, lightHorizon);
186 float yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
187 float lightArg = (terminator-yprime_alt)/100000.0;
188
189 float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
190
191 float mie_angle;
192 if (lightArg < 10.0)
193 {mie_angle = (0.5 * dot(normalize(vertVec), normalize(gl_LightSource[0].position.xyz)) ) + 0.5;}
194 else
195 {mie_angle = 1.0;}
196
197 float fog_vertex_alt = max(vertex_alt,hazeLayerAltitude);
198 float fog_yprime_alt = yprime_alt;
199 if (fog_vertex_alt > hazeLayerAltitude)
200 {
201 if (dist > 0.8 * avisibility)
202 {
203 fog_vertex_alt = mix(fog_vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
204 fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt);
205 }
206 }
207 else
208 {
209 fog_vertex_alt = hazeLayerAltitude;
210 fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt);
211 }
212
213 float fog_lightArg = (terminator-fog_yprime_alt)/100000.0;
214 float fog_earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, fog_yprime_alt) + 0.1;
215 float delta_z = hazeLayerAltitude - eye_alt;
216
217
218 float ct = dot(normalize(up), normalize(vertVec));
219 vec3 relPos = (gl_ModelViewMatrixInverse * vec4 (vertVec,0.0)).xyz;
220
221 /// END geometry for light
222
223
224 /// BEGIN light
225 vec4 light_diffuse;
226 vec4 light_ambient;
227 float intensity;
228
229 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
230 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
231 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
232 light_diffuse.a = 1.0;
233 light_diffuse = light_diffuse * vertex_scattering;
234
235 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
236 light_ambient.g = light_ambient.r * 0.4/0.33;
237 light_ambient.b = light_ambient.r * 0.5/0.33;
238 light_ambient.a = 1.0;
239
240 if (earthShade < 0.5)
241 {
242 intensity = length(light_ambient.rgb);
243 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) ));
244 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
245
246 intensity = length(light_diffuse.rgb);
247 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.7,earthShade) ));
248 }
249
250 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
251 vec3 ecViewDir = (gl_ModelViewMatrix * (ep - vec4(rawpos, 1.0))).xyz;
252 vec3 HV = normalize(normalize(gl_LightSource[0].position.xyz) + normalize(ecViewDir));
253
254 /// END light
255
256 /// BEGIN grain overlay
257 if (grain_texture_enabled ==1)
258 {
259 grainTexel = texture2D(GrainTex, gl_TexCoord[0].st * grain_magnification);
260 texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a );
261 }
262 else if (grain_texture_enabled == 2)
263 {
264 grainTexel = texture2D(GrainTex, rawpos.xy * grain_magnification);
265 texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a );
266 }
267
268 /// END grain overlay
269
270 /// BEGIN snowcover
271
272 vec4 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0);
273
274 if (snow_enabled == 1)
275 {
276 float noise_1m = Noise2D(rawpos.xy, 1.0);
277 float noise_5m = Noise2D(rawpos.xy, 5.0);
278
279 float noise_term = 0.5 * (noise_5m - 0.5);
280 noise_term += 0.5 * (noise_1m - 0.5);
281 snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + 0.5*snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) );
282
283 snow_texel.a *= smoothstep(0.5, 0.7,dot(VNormal, up));
284
285
286 float noise_2000m = 0.0;
287 float noise_10m = 0.0;
288
289
290 texel.rgb = mix(texel.rgb, snow_texel.rgb, snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, 1.0 * (relPos.z + eye_alt)+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0));
291 }
292
293 /// END snowcover
294
295 vec3 reflVecN;
296
297 ///BEGIN bump
298 if (nmap_enabled > 0){
299 N = nmap.rgb * 2.0 - 1.0;
300 // this is exact only for viewing under 90 degrees but much faster than the real solution
301 reflVecN = normalize (N.x * VTangent + N.y * VBinormal + N.z * reflVec);
302 N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
303 if (nmap_dds > 0)
304 N = -N;
305 } else {
306 N = normalize(VNormal);
307 reflVecN = reflVec;
308 }
309 ///END bump
310
311
312
313 vec4 reflection = textureCube(Environment, reflVecN );
314 vec3 viewVec = normalize(vViewVec);
315 float v = abs(dot(viewVec, normalize(VNormal)));// Map a rainbowish color
316 vec4 fresnel = texture2D(ReflGradientsTex, vec2(v, 0.75));
317 vec4 rainbow = texture2D(ReflGradientsTex, vec2(v, 0.25));
318
319 float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
320 //float nDotHV = max(0.0, dot(N, normalize(gl_LightSource[0].halfVector.xyz)));
321 float nDotHV = max(0.0, dot(N,HV));
322 //glare on the backside of tranparent objects
323 if ((gl_FrontMaterial.diffuse.a < 1.0 || texel.a < 1.0)
324 && dot(N, normalize(gl_LightSource[0].position.xyz)) < 0.0) {
325 nDotVP = max(0.0, dot(-N, normalize(gl_LightSource[0].position.xyz)) * (1.0 -texel.a) );
326 nDotHV = max(0.0, dot(-N, HV) * (1.0 -texel.a) );
327 }
328
329 float nDotVP1 = 0.0;
330 float nDotHV1 = 0.0;
331
332
333 // try specular reflection of sky irradiance
334 nDotVP1 = max(0.0, dot(N, up));
335 nDotHV1 = max(0.0, dot(N, normalize(normalize(up) + normalize(-vertVec))));
336
337
338 if (nDotVP == 0.0)
339 {pf = 0.0;}
340 else
341 {pf = pow(nDotHV, gl_FrontMaterial.shininess);}
342
343 if (nDotVP1 == 0.0)
344 {pf1 = 0.0;}
345 else
346 {pf1 = pow(nDotHV1, 0.5*gl_FrontMaterial.shininess);}
347
348 float shadowmap = getShadowing();
349 light_diffuse *= (0.3+0.7*shadowmap);
350
351 if (cloud_shadow_flag == 1)
352 {
353 float cloud_shadow_factor = shadow_func(relPos.x, relPos.y, 1.0, dist);
354 cloud_shadow_factor = 1.0 - ((1.0 - cloud_shadow_factor) * (1.0 - smoothstep (-100.0, 100.0, vertex_alt - hazeLayerAltitude)));
355 light_diffuse = light_diffuse * cloud_shadow_factor;
356 }
357
358 vec3 secondary_light = vec3 (0.0,0.0,0.0);
359
360 if (use_searchlight == 1)
361 {
362 secondary_light += searchlight();
363 }
364 if (use_landing_light == 1)
365 {
366 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
367 }
368 if (use_alt_landing_light == 1)
369 {
370 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
371 }
372
373
374 vec4 Diffuse = light_diffuse * nDotVP;
375 Diffuse.rgb += secondary_light * light_distance_fading(dist);
376 if (use_IR_vision)
377 {
378 Diffuse.rgb = max(Diffuse.rgb, vec3 (0.5, 0.5, 0.5));
379 }
380
381 ///BEGIN reflection correction by dirt
382
383 float refl_d = 1.0;
384
385 if ((dirt_enabled == 1) && (dirt_modulates_reflection == 1))
386 {
387 refl_d = 1.0 - (reflmap.r * dirt_r_factor * (1.0 - dirt_reflection_factor));
388 }
389
390 ///END reflection correction by dirt
391
392
393 vec4 Specular = gl_FrontMaterial.specular * light_diffuse * pf + gl_FrontMaterial.specular * light_ambient * pf1;
394 Specular+= gl_FrontMaterial.specular * pow(max(0.0,-dot(N,normalize(vertVec))),gl_FrontMaterial.shininess) * vec4(secondary_light,1.0);
395
396 Specular *= refl_d;
397 Specular *= shadowmap;
398
399 vec4 color = gl_Color + Diffuse * gl_FrontMaterial.diffuse;
400 color = clamp( color, 0.0, 1.0 );
401
402 ////////////////////////////////////////////////////////////////////
403 //BEGIN reflect
404 ////////////////////////////////////////////////////////////////////
405 if (refl_enabled > 0){
406 float reflFactor = 0.0;
407 float transparency_offset = clamp(refl_correction, -1.0, 1.0);// set the user shininess offset
408
409 if(refl_map > 0){
410 // map the shininess of the object with user input
411 //float pam = (map.a * -2) + 1; //reverse map
412 reflFactor = reflmap.a + transparency_offset;
413 } else if (nmap_enabled > 0) {
414 // set the reflectivity proportional to shininess with user input
415 reflFactor = gl_FrontMaterial.shininess * 0.0078125 * nmap.a + transparency_offset;
416 } else {
417 reflFactor = gl_FrontMaterial.shininess* 0.0078125 + transparency_offset;
418 }
419
420 // enhance low angle reflection by a fresnel term
421 float fresnel_enhance = (1.0-smoothstep(0.0,0.4, dot(N,-normalize(vertVec)))) * refl_fresnel_factor;
422
423 reflFactor+=fresnel_enhance;
424
425 reflFactor = clamp(reflFactor, 0.0, 1.0);
426
427 // add fringing fresnel and rainbow effects and modulate by reflection
428 vec4 reflcolor = mix(reflection, rainbow, refl_rainbow * v);
429 //vec4 reflcolor = reflection;
430 vec4 reflfrescolor = mix(reflcolor, fresnel, refl_fresnel * v);
431 vec4 noisecolor = mix(reflfrescolor, noisevec, refl_noise);
432 //vec4 raincolor = vec4(noisecolor.rgb * reflFactor, 1.0);
433 vec4 raincolor = vec4(noisecolor.rgb, 1.0);
434 raincolor += Specular;
435 raincolor *= light_diffuse;
436
437 if (refl_type == 1)
438 {mixedcolor = mix(texel, raincolor, reflFactor * refl_d).rgb;}
439 else if (refl_type == 2)
440 {mixedcolor = ((texel +(reflcolor * reflFactor * refl_d))-(0.5*reflFactor * refl_d)).rgb;}
441
442 } else {
443 mixedcolor = texel.rgb;
444 }
445 /////////////////////////////////////////////////////////////////////
446 //END reflect
447 /////////////////////////////////////////////////////////////////////
448
449 //////////////////////////////////////////////////////////////////////
450 //begin DIRT
451 //////////////////////////////////////////////////////////////////////
452 if (dirt_enabled >= 1){
453 vec3 dirtFactorIn = vec3 (dirt_r_factor, dirt_g_factor, dirt_b_factor);
454 vec3 dirtFactor = reflmap.rgb * dirtFactorIn.rgb;
455 //dirtFactor.r = smoothstep(0.0, 1.0, dirtFactor.r);
456 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_r_color, smoothstep(0.0, 1.0, dirtFactor.r));
457 if (dirt_multi > 0) {
458 //dirtFactor.g = smoothstep(0.0, 1.0, dirtFactor.g);
459 //dirtFactor.b = smoothstep(0.0, 1.0, dirtFactor.b);
460 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_g_color, smoothstep(0.0, 1.0, dirtFactor.g));
461 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_b_color, smoothstep(0.0, 1.0, dirtFactor.b));
462 }
463 }
464 //////////////////////////////////////////////////////////////////////
465 //END Dirt
466 //////////////////////////////////////////////////////////////////////
467
468 //////////////////////////////////////////////////////////////////////
469 //begin WETNESS
470 //////////////////////////////////////////////////////////////////////
471
472 if (rain_enabled >0.0)
473 {
474 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
475 float rain_factor = 0.0;
476
477 float rain_orientation = max(dot(VNormal, up),0.0);
478
479 if ((rain_norm > 0.0) && (rain_orientation > 0.0))
480 {
481 rain_factor += DotNoise2D(rawpos.xy, 0.2 ,0.5, rain_norm) * abs(sin(6.0*osg_SimulationTime));
482 rain_factor += DotNoise2D(rawpos.xy, 0.3 ,0.4, rain_norm) * abs(sin(6.0*osg_SimulationTime + 2.094));
483 rain_factor += DotNoise2D(rawpos.xy, 0.4 ,0.3, rain_norm)* abs(sin(6.0*osg_SimulationTime + 4.188));
484 }
485
486
487
488 // secondary reflection of sky irradiance in water film
489 float fresnelW = ((0.8 * wetness) ) * (1.0-smoothstep(0.0,0.4, dot(N,-normalize(vertVec)) * 1.0 - 0.2 * rain_factor * wetness));
490 float sky_factor = (1.0-ct*ct);
491 vec3 sky_light = vec3 (1.0,1.0,1.0) * length(light_diffuse.rgb) * (1.0-effective_scattering);
492 Specular.rgb += sky_factor * fresnelW * sky_light;
493 }
494 /////////////////////////////////////////////////////////////////////
495 //end WETNESS
496 //////////////////////////////////////////////////////////////////////
497
498
499 // set ambient adjustment to remove bluiness with user input
500 float ambient_offset = clamp(amb_correction, -1.0, 1.0);
501 //vec4 ambient = gl_LightModel.ambient + gl_LightSource[0].ambient;
502 vec4 ambient = gl_LightModel.ambient + light_ambient;
503 vec4 ambient_Correction = vec4(ambient.rg, ambient.b * 0.6, 1.0)
504 * ambient_offset ;
505 ambient_Correction = clamp(ambient_Correction, -1.0, 1.0);
506
507 color.a = texel.a * alpha;
508 vec4 fragColor = vec4(color.rgb * mixedcolor + ambient_Correction.rgb, color.a);
509
510 fragColor += Specular * nmap.a;
511
512 fragColor.rgb += getClusteredLightsContribution(vertVec, N, texel.rgb);
513
514 //////////////////////////////////////////////////////////////////////
515 // BEGIN lightmap
516 //////////////////////////////////////////////////////////////////////
517 if ( lightmap_enabled >= 1 ) {
518 vec3 lightmapcolor = vec3(0.0);
519 vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor,
520 lightmap_b_factor, lightmap_a_factor);
521
522 if (lightmap_multi > 0 ){
523 lightmapFactor = lightmapFactor * lightmapTexel;
524 //lightmapcolor = lightmap_r_color * lightmapFactor.r +
525 // lightmap_g_color * lightmapFactor.g +
526 // lightmap_b_color * lightmapFactor.b +
527 // lightmap_a_color * lightmapFactor.a ;
528
529 lightmapcolor = lightmap_r_color * lightmapFactor.r;
530 lightmapcolor = addLights(lightmapcolor, lightmap_g_color * lightmapFactor.g);
531 lightmapcolor = addLights(lightmapcolor, lightmap_b_color * lightmapFactor.b);
532 lightmapcolor = addLights(lightmapcolor, lightmap_a_color * lightmapFactor.a);
533
534
535 } else {
536 lightmapcolor = lightmapTexel.rgb * lightmap_r_color * lightmapFactor.r;
537 }
538 fragColor.rgb = max(fragColor.rgb, lightmapcolor * gl_FrontMaterial.diffuse.rgb * smoothstep(0.0, 1.0, mixedcolor*.5 + lightmapcolor*.5));
539 }
540 //////////////////////////////////////////////////////////////////////
541 // END lightmap
542 /////////////////////////////////////////////////////////////////////
543
544
545 /// BEGIN fog amount
546
547 float transmission;
548 float vAltitude;
549 float delta_zv;
550 float H;
551 float distance_in_layer;
552 float transmission_arg;
553 float eqColorFactor;
554
555 float mvisibility = min(visibility, avisibility);
556
557 if (dist > 0.04 * mvisibility)
558 {
559 if (delta_z > 0.0) // we're inside the layer
560 {
561 if (ct < 0.0) // we look down
562 {
563 distance_in_layer = dist;
564 vAltitude = min(distance_in_layer,mvisibility) * ct;
565 delta_zv = delta_z - vAltitude;
566 }
567 else // we may look through upper layer edge
568 {
569 H = dist * ct;
570 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
571 else {distance_in_layer = dist;}
572 vAltitude = min(distance_in_layer,visibility) * ct;
573 delta_zv = delta_z - vAltitude;
574 }
575 }
576 else // we see the layer from above, delta_z < 0.0
577 {
578 H = dist * -ct;
579 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
580 {
581 distance_in_layer = 0.0;
582 delta_zv = 0.0;
583 }
584 else
585 {
586 vAltitude = H + delta_z;
587 distance_in_layer = vAltitude/H * dist;
588 vAltitude = min(distance_in_layer,visibility) * (-ct);
589 delta_zv = vAltitude;
590 }
591 }
592
593 transmission_arg = (dist-distance_in_layer)/avisibility;
594
595
596 if (visibility < avisibility)
597 {
598 transmission_arg = transmission_arg + (distance_in_layer/visibility);
599 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
600 }
601 else
602 {
603 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
604 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
605 }
606 transmission = fog_func(transmission_arg, alt);
607 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
608 }
609 else
610 {
611 eqColorFactor = 1.0;
612 transmission = 1.0;
613 }
614
615 /// END fog amount
616
617 /// BEGIN fog color
618
619 vec3 hazeColor = get_hazeColor(fog_lightArg);
620
621 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
622 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
623
624 if (transmission< 1.0)
625 {
626
627
628
629 if (fog_lightArg < 10.0)
630 {
631 intensity = length(hazeColor);
632 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
633 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)) );
634 }
635
636 intensity = length(hazeColor);
637 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
638
639 hazeColor.r = hazeColor.r * 0.83;
640 hazeColor.g = hazeColor.g * 0.9;
641
642 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
643 intensity = length(hazeColor);
644 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,fog_earthShade) ));
645 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
646
647 float shadow = mix( min(1.0 + dot(VNormal,gl_LightSource[0].position.xyz),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
648 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
649 }
650 else
651 {
652 hazeColor = vec3 (1.0, 1.0, 1.0);
653 }
654
655 if (use_IR_vision)
656 {
657 //hazeColor.rgb = max(hazeColor.rgb, vec3 (0.5, 0.5, 0.5));
658 }
659
660
661 /// END fog color
662 fragColor = clamp(fragColor, 0.0, 1.0);
663 //hazeColor = clamp(hazeColor, 0.0, 1.0);
664
665 ///BEGIN Rayleigh fog ///
666 // Only compute fog if terrain level is 'Ultra'
667 if ((quality_level > 5) && (tquality_level > 5))
668 {
669 // Rayleigh color shift due to out-scattering
670 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
671 float outscatter = 1.0-exp(-dist/rayleigh_length);
672 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
673
674 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
675 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
676 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
677 }
678 /// END Rayleigh fog
679
680 // don't let the light fade out too rapidly
681 lightArg = (terminator + 200000.0)/100000.0;
682 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
683 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
684 hazeColor *= eqColorFactor * fog_earthShade;
685 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
686
687
688 fragColor.rgb = mix(hazeColor +secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
689
690
691 fragColor.rgb = filter_combined(fragColor.rgb);
692 gl_FragColor = fragColor;
693 }