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 sampler3D NoiseTex;
15 uniform sampler2D snow_texture;
16 uniform sampler2D detail_texture;
17 uniform sampler2D mix_texture;
19 //varying float yprime_alt;
20 //varying float mie_angle;
21 varying float steepness;
24 uniform float visibility;
25 uniform float avisibility;
26 uniform float scattering;
27 uniform float terminator;
28 uniform float terrain_alt;
29 uniform float hazeLayerAltitude;
30 uniform float overcast;
31 uniform float eye_alt;
32 uniform float snowlevel;
33 uniform float dust_cover_factor;
34 uniform float lichen_cover_factor;
35 uniform float wetness;
36 uniform float fogstructure;
37 uniform float snow_thickness_factor;
38 uniform float cloud_self_shading;
40 uniform float transition_model;
41 uniform float hires_overlay_bias;
42 uniform int quality_level;
43 uniform int tquality_level;
45 const float EarthRadius = 5800000.0;
46 const float terminator_width = 200000.0;
55 float rand2D(in vec2 co){
56 return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
59 float cosine_interpolate(in float a, in float b, in float x)
61 float ft = x * 3.1415927;
62 float f = (1.0 - cos(ft)) * .5;
64 return a*(1.0-f) + b*f;
67 float simple_interpolate(in float a, in float b, in float x)
69 return a + smoothstep(0.0,1.0,x) * (b-a);
73 float interpolatedNoise2D(in float x, in float y)
75 float integer_x = x - fract(x);
76 float fractional_x = x - integer_x;
78 float integer_y = y - fract(y);
79 float fractional_y = y - integer_y;
81 float v1 = rand2D(vec2(integer_x, integer_y));
82 float v2 = rand2D(vec2(integer_x+1.0, integer_y));
83 float v3 = rand2D(vec2(integer_x, integer_y+1.0));
84 float v4 = rand2D(vec2(integer_x+1.0, integer_y +1.0));
86 float i1 = simple_interpolate(v1 , v2 , fractional_x);
87 float i2 = simple_interpolate(v3 , v4 , fractional_x);
89 return simple_interpolate(i1 , i2 , fractional_y);
93 float Noise2D(in vec2 coord, in float wavelength)
95 return interpolatedNoise2D(coord.x/wavelength, coord.y/wavelength);
101 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
105 // use the asymptotics to shorten computations
106 if (x > 30.0) {return e;}
107 if (x < -15.0) {return 0.0;}
109 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
112 // this determines how light is attenuated in the distance
113 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
114 // for distance > visibility
116 float fog_func (in float targ)
122 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
123 // create the illusion of improved visibility range
125 targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
129 {return exp(-targ - targ * targ * targ * targ);}
130 else if (alt < 50000.0)
132 fade_mix = (alt - 30000.0)/20000.0;
133 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
137 return exp(- targ * targ - pow(targ,4.0));
146 yprime_alt = diffuse_term.a;
147 //diffuse_term.a = 1.0;
148 mie_angle = gl_Color.a;
149 float effective_scattering = min(scattering, cloud_self_shading);
151 // distance to fragment
152 float dist = length(relPos);
153 // angle of view vector with horizon
154 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
157 vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
158 // this is taken from default.frag
160 float NdotL, NdotHV, fogFactor;
161 vec4 color = gl_Color;
163 vec3 lightDir = gl_LightSource[0].position.xyz;
164 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
165 //vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
171 vec4 specular = vec4(0.0);
175 // get noise at different wavelengths
177 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
179 // 250m: detail texel
180 // 500m: distortion and overlay
181 // 1500m: overlay, detail, dust, fog
182 // 2000m: overlay, detail, snow, fog
186 noise_10m = Noise2D(rawPos.xy, 10.0);
187 noise_5m = Noise2D(rawPos.xy ,5.0);
192 float noise_50m = Noise2D(rawPos.xy, 50.0);;
194 float noise_500m = Noise2D(rawPos.xy, 500.0);
195 float noise_1500m = Noise2D(rawPos.xy, 1500.0);
196 float noise_2000m = Noise2D(rawPos.xy, 2000.0);
207 texel = texture2D(texture, gl_TexCoord[0].st);
208 float local_autumn_factor = texel.a;
210 float distortion_factor = 1.0;
220 //float view_angle = abs(dot(normal, normalize(ecViewdir)));
222 if ((quality_level > 3)&&(relPos.z + eye_alt +500.0 > snowlevel))
224 //snow_texel = texture2D(snow_texture, gl_TexCoord[0].st);
226 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
227 snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
228 snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
230 noise_term = 0.1 * (noise_500m-0.5);
231 sfactor = sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
232 noise_term = noise_term + 0.2 * (noise_50m -0.5) * (1.0 - smoothstep(18000.0*sfactor, 40000.0*sfactor, dist) ) ;
233 noise_term = noise_term + 0.3 * (noise_10m -0.5) * (1.0 - smoothstep(4000.0 * sfactor, 8000.0 * sfactor, dist) ) ;
234 if (dist < 3000.0*sfactor){ noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1000.0 * sfactor, 3000.0 *sfactor, dist) );}
235 snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) );
239 if (tquality_level > 2)
241 mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3);
242 if (mix_texel.a <0.1) {mix_flag = 0;}
246 if (tquality_level > 3)
248 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);
249 //distortion_factor = 0.9375 + (1.0 * nvL[2]);
250 distortion_factor = 0.97 + 0.06 * noise_500m;
251 stprime = stprime * distortion_factor * 15.0;
252 if (quality_level > 4)
254 stprime = stprime + normalize(relPos).xy * 0.02 * (noise_10m + 0.5 * noise_5m - 0.75);
256 detail_texel = texture2D(detail_texture, stprime);
257 if (detail_texel.a <0.1) {flag = 0;}
261 // texture preparation according to detail level
263 // mix in hires texture patches
269 if (tquality_level > 2)
271 // first the second texture overlay
272 // transition model 0: random patch overlay without any gradient information
273 // transition model 1: only gradient-driven transitions, no randomness
278 nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
279 nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
280 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);
281 mix_factor = smoothstep(0.5, 0.54, nSum);
282 texel = mix(texel, mix_texel, mix_factor);
283 local_autumn_factor = texel.a;
286 // then the detail texture overlay
289 if (tquality_level > 3)
295 //noise_50m = Noise2D(rawPos.xy, 50.0);
296 noise_250m = Noise2D(rawPos.xy, 250.0);
297 dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
298 nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
299 nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
300 mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias - dist_fact);
301 if (mix_factor > 0.8) {mix_factor = 0.8;}
302 texel = mix(texel, detail_texel,mix_factor);
303 local_autumn_factor = texel.a;
312 float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
315 texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
317 texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
320 if (local_autumn_factor < 1.0)
322 intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
323 texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
328 const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
329 const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);;
332 if (quality_level > 3)
336 texel = mix(texel, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) );
338 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) );
341 if (relPos.z + eye_alt +500.0 > snowlevel)
343 snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
344 //texel = mix(texel, snow_texel, texel_snow_fraction);
345 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));
351 // get distribution of water when terrain is wet
353 float water_threshold1;
354 float water_threshold2;
355 float water_factor =0.0;
358 if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0))
360 water_threshold1 = 1.0-0.5* wetness;
361 water_threshold2 = 1.0 - 0.3 * wetness;
362 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));
365 // darken wet terrain
367 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
370 // light computations
373 vec4 light_specular = gl_LightSource[0].specular;
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;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
381 NdotL = dot(n, lightDir);
382 if ((tquality_level > 3) && (mix_flag ==1)&& (dist < 2000.0) && (quality_level > 4))
384 noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
385 noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
386 NdotL = NdotL + 1.0 * (noisegrad_10m + 0.5* noisegrad_5m) * mix_factor/0.8 * (1.0 - smoothstep(1000.0, 2000.0, dist));
389 color += diffuse_term * NdotL;
390 NdotHV = max(dot(n, halfVector), 0.0);
391 if (gl_FrontMaterial.shininess > 0.0)
392 specular.rgb = ((gl_FrontMaterial.specular.rgb + (water_factor * vec3 (1.0, 1.0, 1.0)))
394 * pow(NdotHV, gl_FrontMaterial.shininess + (20.0 * water_factor)));
396 color.a = 1.0;//diffuse_term.a;
397 // This shouldn't be necessary, but our lighting becomes very
398 // saturated. Clamping the color before modulating by the texture
399 // is closer to what the OpenGL fixed function pipeline does.
400 color = clamp(color, 0.0, 1.0);
405 fragColor = color * texel + specular;
407 // here comes the terrain haze model
410 float delta_z = hazeLayerAltitude - eye_alt;
412 if (dist > max(40.0, 0.04 * min(visibility,avisibility)))
413 //if ((gl_FragCoord.y > ylimit) || (gl_FragCoord.x < zlimit1) || (gl_FragCoord.x > zlimit2))
424 float distance_in_layer;
425 float transmission_arg;
430 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
432 if (delta_z > 0.0) // we're inside the layer
434 if (ct < 0.0) // we look down
436 distance_in_layer = dist;
437 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
438 delta_zv = delta_z - vAltitude;
440 else // we may look through upper layer edge
443 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
444 else {distance_in_layer = dist;}
445 vAltitude = min(distance_in_layer,visibility) * ct;
446 delta_zv = delta_z - vAltitude;
449 else // we see the layer from above, delta_z < 0.0
452 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
454 distance_in_layer = 0.0;
459 vAltitude = H + delta_z;
460 distance_in_layer = vAltitude/H * dist;
461 vAltitude = min(distance_in_layer,visibility) * (-ct);
462 delta_zv = vAltitude;
467 // ground haze cannot be thinner than aloft visibility in the model,
468 // so we need to use aloft visibility otherwise
471 transmission_arg = (dist-distance_in_layer)/avisibility;
478 if (visibility < avisibility)
480 if (quality_level > 3)
482 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
487 transmission_arg = transmission_arg + (distance_in_layer/visibility);
489 // this combines the Weber-Fechner intensity
490 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 - effective_scattering);
495 if (quality_level > 3)
497 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
501 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
503 // this combines the Weber-Fechner intensity
504 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
509 transmission = fog_func(transmission_arg);
511 // there's always residual intensity, we should never be driven to zero
512 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
515 float lightArg = (terminator-yprime_alt)/100000.0;
519 hazeColor.b = light_func(lightArg, 1.330e-05, 0.264, 2.527, 1.08e-05, 1.0);
520 hazeColor.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
521 hazeColor.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
524 // now dim the light for haze
525 eShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
531 intensity = length(hazeColor);
532 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
533 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)) );
536 intensity = length(hazeColor);
538 if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
542 // high altitude desaturation of the haze color
543 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
546 hazeColor.x = hazeColor.x * 0.83;
547 hazeColor.y = hazeColor.y * 0.9;
550 // additional blue in indirect light
551 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
552 intensity = length(hazeColor);
553 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
555 // change haze color to blue hue for strong fogging
556 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
560 // reduce haze intensity when looking at shaded surfaces, only in terminator region
561 float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
562 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
568 fragColor.rgb = mix(eqColorFactor * hazeColor * eShade , fragColor.rgb,transmission);
571 gl_FragColor = fragColor;
575 else // if dist < threshold no fogging at all
577 gl_FragColor = fragColor;