3 // written by Thorsten Renk, Oct 2011, based on default.frag
4 // Ambient term comes in gl_Color.rgb.
5 varying vec4 diffuse_term;
10 //varying vec3 ecViewdir;
13 uniform sampler2D texture;
14 uniform sampler2D NormalTex;
15 //uniform sampler3D NoiseTex;
16 uniform sampler2D snow_texture;
17 uniform sampler2D detail_texture;
18 uniform sampler2D mix_texture;
20 //varying float yprime_alt;
21 //varying float mie_angle;
22 varying float steepness;
25 uniform float visibility;
26 uniform float avisibility;
27 uniform float scattering;
28 uniform float terminator;
29 uniform float terrain_alt;
30 uniform float hazeLayerAltitude;
31 uniform float overcast;
32 uniform float eye_alt;
33 uniform float snowlevel;
34 uniform float dust_cover_factor;
35 uniform float lichen_cover_factor;
36 uniform float wetness;
37 uniform float fogstructure;
38 uniform float snow_thickness_factor;
39 uniform float cloud_self_shading;
41 uniform float zlimit1;
42 uniform float zlimit2;
44 uniform int quality_level;
45 uniform int tquality_level;
47 const float EarthRadius = 5800000.0;
48 const float terminator_width = 200000.0;
57 float rand2D(in vec2 co){
58 return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
61 float cosine_interpolate(in float a, in float b, in float x)
63 float ft = x * 3.1415927;
64 float f = (1.0 - cos(ft)) * .5;
66 return a*(1.0-f) + b*f;
69 float simple_interpolate(in float a, in float b, in float x)
71 return a + smoothstep(0.0,1.0,x) * (b-a);
75 float interpolatedNoise2D(in float x, in float y)
77 float integer_x = x - fract(x);
78 float fractional_x = x - integer_x;
80 float integer_y = y - fract(y);
81 float fractional_y = y - integer_y;
83 float v1 = rand2D(vec2(integer_x, integer_y));
84 float v2 = rand2D(vec2(integer_x+1.0, integer_y));
85 float v3 = rand2D(vec2(integer_x, integer_y+1.0));
86 float v4 = rand2D(vec2(integer_x+1.0, integer_y +1.0));
88 float i1 = simple_interpolate(v1 , v2 , fractional_x);
89 float i2 = simple_interpolate(v3 , v4 , fractional_x);
91 return simple_interpolate(i1 , i2 , fractional_y);
95 float Noise2D(in vec2 coord, in float wavelength)
97 return interpolatedNoise2D(coord.x/wavelength, coord.y/wavelength);
103 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
107 // use the asymptotics to shorten computations
108 if (x > 30.0) {return e;}
109 if (x < -15.0) {return 0.0;}
111 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
114 // this determines how light is attenuated in the distance
115 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
116 // for distance > visibility
118 float fog_func (in float targ)
124 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
125 // create the illusion of improved visibility range
127 targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
131 {return exp(-targ - targ * targ * targ * targ);}
132 else if (alt < 50000.0)
134 fade_mix = (alt - 30000.0)/20000.0;
135 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
139 return exp(- targ * targ - pow(targ,4.0));
147 // drop fragments behind the mask of the instrument panel as passed by properties
150 int xoffset = int(xslope * (ylimit - gl_FragCoord.y));
153 if ((gl_FragCoord.y < ylimit) && (gl_FragCoord.x > zlimit1 - xoffset) && (gl_FragCoord.x < zlimit2 + xoffset))
157 yprime_alt = diffuse_term.a;
158 diffuse_term.a = 1.0;
159 mie_angle = gl_Color.a;
160 float effective_scattering = min(scattering, cloud_self_shading);
162 // distance to fragment
163 float dist = length(relPos);
164 // angle of view vector with horizon
165 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
168 vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
169 // this is taken from default.frag
171 float NdotL, NdotHV, fogFactor;
172 vec4 color = gl_Color;
174 vec3 lightDir = gl_LightSource[0].position.xyz;
175 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
176 //vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
182 vec4 specular = vec4(0.0);
186 // get noise at different wavelengths
188 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
190 // 250m: detail texel
191 // 500m: distortion and overlay
192 // 1500m: overlay, detail, dust, fog
193 // 2000m: overlay, detail, snow, fog
196 float noise_1m = Noise2D(rawPos.xy, 1.0);
199 noise_10m = Noise2D(rawPos.xy, 10.0);
200 noise_5m = Noise2D(rawPos.xy ,5.0);
205 float noise_50m = Noise2D(rawPos.xy, 50.0);;
207 float noise_500m = Noise2D(rawPos.xy, 500.0);
208 float noise_1500m = Noise2D(rawPos.xy, 1500.0);
209 float noise_2000m = Noise2D(rawPos.xy, 2000.0);
220 texel = texture2D(texture, gl_TexCoord[0].st);
221 vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st * 8.0);
222 vec3 N = nmap.rgb * 2.0 - 1.0;
224 float distortion_factor = 1.0;
234 //float view_angle = abs(dot(normal, normalize(ecViewdir)));
236 if ((quality_level > 3)&&(relPos.z + eye_alt +500.0 > snowlevel))
239 noise_01m = Noise2D(rawPos.xy,0.1);
240 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_50m + 0.1* (1.0 - noise_10m) );
242 noise_term = 0.1 * (noise_50m-0.5);
243 sfactor = 1.0;//sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
244 noise_term = noise_term + 0.2 * (noise_10m -0.5) * (1.0 - smoothstep(10000.0*sfactor, 16000.0*sfactor, dist) ) ;
245 noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1200.0 * sfactor, 2000.0 * sfactor, dist) ) ;
246 noise_term = noise_term + 0.3 * (noise_1m -0.5) * (1.0 - smoothstep(500.0 * sfactor, 1000.0 *sfactor, dist) );
247 noise_term = noise_term + 0.3 * (noise_01m -0.5) * (1.0 - smoothstep(20.0 * sfactor, 100.0 *sfactor, dist) );
248 snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + 0.2*snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) );
252 if (tquality_level > 2)
254 mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3);
255 if (mix_texel.a <0.1) {mix_flag = 0;}
259 if (tquality_level > 3)
261 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);
262 //distortion_factor = 0.9375 + (1.0 * nvL[2]);
263 distortion_factor = 0.97 + 0.06 * noise_500m;
264 stprime = stprime * distortion_factor * 15.0;
265 if (quality_level > 4)
267 stprime = stprime + normalize(relPos).xy * 0.02 * (noise_10m + 0.5 * noise_5m - 0.75);
269 detail_texel = texture2D(detail_texture, stprime);
270 if (detail_texel.a <0.1) {flag = 0;}
274 // texture preparation according to detail level
276 // mix in hires texture patches
282 if (tquality_level > 2)
284 // first the second texture overlay
289 nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
290 nSum = nSum + 0.4 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
291 mix_factor = smoothstep(0.5, 0.54, nSum);
292 texel = mix(texel, mix_texel, mix_factor);
296 // then the detail texture overlay
299 if (tquality_level > 3)
305 //noise_50m = Noise2D(rawPos.xy, 50.0);
306 noise_250m = Noise2D(rawPos.xy, 250.0);
307 dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
308 nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
309 nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
310 mix_factor = smoothstep(0.47, 0.54, nSum - dist_fact);
311 if (mix_factor > 0.8) {mix_factor = 0.8;}
312 texel = mix(texel, detail_texel,mix_factor);
318 const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
319 const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);;
322 if (quality_level > 3)
326 texel = mix(texel, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) );
328 texel = mix(texel, dust_color, clamp(0.5 * dust_cover_factor + 3.0 * dust_cover_factor * (((noise_1500m - 0.5) * 0.125)+0.125 ),0.0, 1.0) );
331 if (relPos.z + eye_alt +500.0 > snowlevel)
333 snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
334 texel = mix(texel, snow_texel, snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * (relPos.z + eye_alt)+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0));
340 // get distribution of water when terrain is wet
342 float water_threshold1;
343 float water_threshold2;
344 float water_factor =0.0;
347 if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0))
349 water_threshold1 = 1.0-0.5* wetness;
350 water_threshold2 = 1.0 - 0.3 * wetness;
351 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));
354 // darken wet terrain
356 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
359 // light computations
362 vec4 light_specular = gl_LightSource[0].specular;
364 // If gl_Color.a == 0, this is a back-facing polygon and the
365 // normal should be reversed.
366 //n = (2.0 * gl_Color.a - 1.0) * normal;
367 n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
370 NdotL = dot(n, lightDir);
371 if ((tquality_level > 3) && (mix_flag ==1)&& (dist < 2000.0) && (quality_level > 4))
373 noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
374 noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
375 NdotL = NdotL +1.0 * (noisegrad_10m + 0.5* noisegrad_5m) * mix_factor/0.8 * (1.0 - smoothstep(1000.0, 2000.0, dist));
377 if (quality_level > 4)
379 NdotL = NdotL + 3.0 * N + 0.1 * (noise_01m-0.5) ;
382 color += diffuse_term * NdotL;
383 NdotHV = max(dot(n, halfVector), 0.0);
384 if (gl_FrontMaterial.shininess > 0.0)
385 specular.rgb = ((gl_FrontMaterial.specular.rgb + (water_factor * vec3 (1.0, 1.0, 1.0)))
387 * pow(NdotHV, gl_FrontMaterial.shininess + (20.0 * water_factor)));
389 color.a = diffuse_term.a;
390 // This shouldn't be necessary, but our lighting becomes very
391 // saturated. Clamping the color before modulating by the texture
392 // is closer to what the OpenGL fixed function pipeline does.
393 color = clamp(color, 0.0, 1.0);
398 fragColor = color * texel + specular;
400 // here comes the terrain haze model
403 float delta_z = hazeLayerAltitude - eye_alt;
405 if (dist > max(40.0, 0.04 * min(visibility,avisibility)))
406 //if ((gl_FragCoord.y > ylimit) || (gl_FragCoord.x < zlimit1) || (gl_FragCoord.x > zlimit2))
417 float distance_in_layer;
418 float transmission_arg;
423 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
425 if (delta_z > 0.0) // we're inside the layer
427 if (ct < 0.0) // we look down
429 distance_in_layer = dist;
430 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
431 delta_zv = delta_z - vAltitude;
433 else // we may look through upper layer edge
436 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
437 else {distance_in_layer = dist;}
438 vAltitude = min(distance_in_layer,visibility) * ct;
439 delta_zv = delta_z - vAltitude;
442 else // we see the layer from above, delta_z < 0.0
445 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
447 distance_in_layer = 0.0;
452 vAltitude = H + delta_z;
453 distance_in_layer = vAltitude/H * dist;
454 vAltitude = min(distance_in_layer,visibility) * (-ct);
455 delta_zv = vAltitude;
460 // ground haze cannot be thinner than aloft visibility in the model,
461 // so we need to use aloft visibility otherwise
464 transmission_arg = (dist-distance_in_layer)/avisibility;
471 if (visibility < avisibility)
473 if (quality_level > 3)
475 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
480 transmission_arg = transmission_arg + (distance_in_layer/visibility);
482 // this combines the Weber-Fechner intensity
483 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 - effective_scattering);
488 if (quality_level > 3)
490 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
494 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
496 // this combines the Weber-Fechner intensity
497 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
502 transmission = fog_func(transmission_arg);
504 // there's always residual intensity, we should never be driven to zero
505 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
508 float lightArg = (terminator-yprime_alt)/100000.0;
512 hazeColor.b = light_func(lightArg, 1.330e-05, 0.264, 2.527, 1.08e-05, 1.0);
513 hazeColor.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
514 hazeColor.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
517 // now dim the light for haze
518 eShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
524 intensity = length(hazeColor);
525 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
526 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)) );
529 intensity = length(hazeColor);
531 if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
535 // high altitude desaturation of the haze color
536 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
539 hazeColor.x = hazeColor.x * 0.83;
540 hazeColor.y = hazeColor.y * 0.9;
543 // additional blue in indirect light
544 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
545 intensity = length(hazeColor);
546 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
548 // change haze color to blue hue for strong fogging
549 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
553 // reduce haze intensity when looking at shaded surfaces, only in terminator region
554 float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
555 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
561 fragColor.rgb = mix(eqColorFactor * hazeColor * eShade , fragColor.rgb,transmission);
564 gl_FragColor = fragColor;
568 else // if dist < threshold no fogging at all
570 gl_FragColor = fragColor;