3 // written by Thorsten Renk, Oct 2011, based on default.frag
4 // Ambient term comes in gl_Color.rgb.
5 varying vec4 diffuse_term;
12 uniform sampler2D texture;
13 uniform sampler2D snow_texture;
16 //varying float yprime_alt;
17 //varying float mie_angle;
18 varying float steepness;
21 uniform float visibility;
22 uniform float avisibility;
23 uniform float scattering;
24 uniform float terminator;
25 uniform float terrain_alt;
26 uniform float hazeLayerAltitude;
27 uniform float overcast;
28 uniform float eye_alt;
29 uniform float snowlevel;
30 uniform float dust_cover_factor;
31 uniform float fogstructure;
32 uniform float cloud_self_shading;
33 uniform float snow_thickness_factor;
35 uniform float zlimit1;
36 uniform float zlimit2;
37 uniform float wetness;
38 uniform int quality_level;
39 uniform int tquality_level;
42 const float EarthRadius = 5800000.0;
43 const float terminator_width = 200000.0;
53 float rand2D(in vec2 co){
54 return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
57 float cosine_interpolate(in float a, in float b, in float x)
59 float ft = x * 3.1415927;
60 float f = (1.0 - cos(ft)) * .5;
62 return a*(1.0-f) + b*f;
65 float simple_interpolate(in float a, in float b, in float x)
67 return a + smoothstep(0.0,1.0,x) * (b-a);
71 float interpolatedNoise2D(in float x, in float y)
73 float integer_x = x - fract(x);
74 float fractional_x = x - integer_x;
76 float integer_y = y - fract(y);
77 float fractional_y = y - integer_y;
79 float v1 = rand2D(vec2(integer_x, integer_y));
80 float v2 = rand2D(vec2(integer_x+1.0, integer_y));
81 float v3 = rand2D(vec2(integer_x, integer_y+1.0));
82 float v4 = rand2D(vec2(integer_x+1.0, integer_y +1.0));
84 float i1 = simple_interpolate(v1 , v2 , fractional_x);
85 float i2 = simple_interpolate(v3 , v4 , fractional_x);
87 return simple_interpolate(i1 , i2 , fractional_y);
91 float Noise2D(in vec2 coord, in float wavelength)
93 return interpolatedNoise2D(coord.x/wavelength, coord.y/wavelength);
99 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
103 // use the asymptotics to shorten computations
104 if (x > 30.0) {return e;}
105 if (x < -15.0) {return 0.0;}
107 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
110 // this determines how light is attenuated in the distance
111 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
112 // for distance > visibility
114 float fog_func (in float targ)
120 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
121 // create the illusion of improved visibility range
123 targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
127 {return exp(-targ - targ * targ * targ * targ);}
128 else if (alt < 50000.0)
130 fade_mix = (alt - 30000.0)/20000.0;
131 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
135 return exp(- targ * targ - pow(targ,4.0));
143 //if ((gl_FragCoord.y < ylimit) && (gl_FragCoord.x > zlimit1) && (gl_FragCoord.x < zlimit2))
147 float effective_scattering = min(scattering, cloud_self_shading);
148 yprime_alt = diffuse_term.a;
149 diffuse_term.a = 1.0;
150 mie_angle = gl_Color.a;
152 vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
154 float dist = length(relPos);
155 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
157 // this is taken from default.frag
159 float NdotL, NdotHV, fogFactor;
160 vec4 color = gl_Color;
162 vec3 lightDir = gl_LightSource[0].position.xyz;
163 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
169 vec4 specular = vec4(0.0);
173 // get noise at different wavelengths
175 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
176 // 500m: distortion and overlay
177 // 1500m: overlay, detail, dust, fog
178 // 2000m: overlay, detail, snow, fog
181 float noise_1m = Noise2D(rawPos.xy, 1.0);
184 float noise_10m = Noise2D(rawPos.xy, 10.0);
185 float noise_5m = Noise2D(rawPos.xy,5.0);
189 float noise_50m = Noise2D(rawPos.xy, 500.0);
190 float noise_1500m = Noise2D(rawPos.xy, 1500.0);
191 float noise_2000m = Noise2D(rawPos.xy, 2000.0);
202 texel = texture2D(texture, gl_TexCoord[0].st * 5.0);
204 float distortion_factor = 1.0;
208 if (quality_level > 3)
210 //snow_texel = texture2D(snow_texture, gl_TexCoord[0].st);
212 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_50m + 0.1* (1.0 - noise_10m) );
214 noise_term = 0.1 * (noise_50m-0.5);
215 sfactor = 1.0;//sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
216 noise_term = noise_term + 0.2 * (noise_10m -0.5) * (1.0 - smoothstep(10000.0*sfactor, 16000.0*sfactor, dist) ) ;
217 noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1200.0 * sfactor, 2000.0 * sfactor, dist) ) ;
218 if (dist < 1000.0*sfactor){ noise_term = noise_term + 0.3 * (noise_1m -0.5) * (1.0 - smoothstep(500.0 * sfactor, 1000.0 *sfactor, dist) );}
219 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) );
229 float water_factor = 0.0;
230 float water_threshold1;
231 float water_threshold2;
234 // get distribution of water when terrain is wet
236 if ((dist < 3000.0)&& (quality_level > 3) && (wetness>0.0))
238 water_threshold1 = 1.0-0.5* wetness;
239 water_threshold2 = 1.0 - 0.3 * wetness;
240 water_factor = smoothstep(water_threshold1, water_threshold2 , 0.5 * (noise_5m + (1.0 -noise_1m))) * (1.0 - smoothstep(1000.0, 3000.0, dist));
244 // color and shade variation of the grass
246 float nfact_1m = 3.0 * (noise_1m - 0.5) * (1.0 - smoothstep(3000.0, 6000.0, dist/ abs(ct)));
247 float nfact_5m = 2.0 * (noise_5m - 0.5);
248 float nfact_10m = 1.0 * (noise_10m - 0.5);
249 texel.rgb = texel.rgb * (0.85 + 0.1 * (nfact_1m + nfact_5m + nfact_10m));
250 //texel.rgb = texel.rgb * (0.7 + 0.1 * (noise_10m + 2.0 * noise_5m + 3.0 * noise_1m));
251 //texel.rgb = texel.rgb * (0.7 + 0.1 * (noise_10m + 2.0 * (1.0-smoothstep(50.0, 200.0, dist*abs(ct))) * noise_5m + (1.0-smoothstep(20.0,100.0, dist*abs(ct))) * 3.0 *noise_1m));
252 texel.r = texel.r * (1.0 + 0.14 * smoothstep(0.5,0.7, 0.33*(2.0 * noise_10m + (1.0-noise_5m))));
258 if (quality_level > 3)
261 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
262 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) );
265 snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
266 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));
270 // darken grass when wet
271 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
275 // light computations
278 vec4 light_specular = gl_LightSource[0].specular ;
280 // If gl_Color.a == 0, this is a back-facing polygon and the
281 // normal should be reversed.
282 //n = (2.0 * gl_Color.a - 1.0) * normal;
284 n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
287 NdotL = dot(n, lightDir);
288 if ((dist < 200.0) && (quality_level > 4))
290 noise_01m = Noise2D(rawPos.xy,0.1);
291 NdotL = NdotL + 0.8 * (noise_01m-0.5) * (1.0 - smoothstep(50.0, 100.0, dist)) * (1.0 - water_factor);
295 color += diffuse_term * NdotL;
298 NdotHV = max(dot(n, halfVector), 0.0);
300 if (gl_FrontMaterial.shininess > 0.0)
301 specular.rgb = ((gl_FrontMaterial.specular.rgb + (water_factor * vec3 (1.0, 1.0, 1.0)))
303 * pow(NdotHV, (gl_FrontMaterial.shininess + 20.0 * water_factor)));
305 color.a = diffuse_term.a;
306 // This shouldn't be necessary, but our lighting becomes very
307 // saturated. Clamping the color before modulating by the texture
308 // is closer to what the OpenGL fixed function pipeline does.
309 color = clamp(color, 0.0, 1.0);
314 fragColor = color * texel + specular;
316 // here comes the terrain haze model
319 float delta_z = hazeLayerAltitude - eye_alt;
321 if (dist > max(40.0, 0.04 * min(visibility,avisibility)))
331 float distance_in_layer;
332 float transmission_arg;
337 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
339 if (delta_z > 0.0) // we're inside the layer
341 if (ct < 0.0) // we look down
343 distance_in_layer = dist;
344 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
345 delta_zv = delta_z - vAltitude;
347 else // we may look through upper layer edge
350 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
351 else {distance_in_layer = dist;}
352 vAltitude = min(distance_in_layer,visibility) * ct;
353 delta_zv = delta_z - vAltitude;
356 else // we see the layer from above, delta_z < 0.0
359 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
361 distance_in_layer = 0.0;
366 vAltitude = H + delta_z;
367 distance_in_layer = vAltitude/H * dist;
368 vAltitude = min(distance_in_layer,visibility) * (-ct);
369 delta_zv = vAltitude;
374 // ground haze cannot be thinner than aloft visibility in the model,
375 // so we need to use aloft visibility otherwise
378 transmission_arg = (dist-distance_in_layer)/avisibility;
385 if (visibility < avisibility)
387 if (quality_level > 3)
389 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
394 transmission_arg = transmission_arg + (distance_in_layer/visibility);
396 // this combines the Weber-Fechner intensity
397 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 - effective_scattering);
402 if (quality_level > 3)
404 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
408 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
410 // this combines the Weber-Fechner intensity
411 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
416 transmission = fog_func(transmission_arg);
418 // there's always residual intensity, we should never be driven to zero
419 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
422 float lightArg = (terminator-yprime_alt)/100000.0;
426 hazeColor.b = light_func(lightArg, 1.330e-05, 0.264, 2.527, 1.08e-05, 1.0);
427 hazeColor.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
428 hazeColor.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
431 // now dim the light for haze
432 eShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
437 {intensity = length(hazeColor);
438 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
439 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)) );
442 // high altitude desaturation of the haze color
444 intensity = length(hazeColor);
445 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
449 hazeColor.x = hazeColor.x * 0.83;
450 hazeColor.y = hazeColor.y * 0.9;
453 // additional blue in indirect light
454 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
455 intensity = length(hazeColor);
456 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
458 // change haze color to blue hue for strong fogging
459 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
462 // reduce haze intensity when looking at shaded surfaces, only in terminator region
464 float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
465 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
470 fragColor.xyz = mix(eqColorFactor * hazeColor * eShade, fragColor.xyz,transmission);
473 gl_FragColor = fragColor;
477 else // if dist < threshold no fogging at all
479 gl_FragColor = fragColor;