Debian Codegraph @

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