| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
uniform float air_pollution; |
| 5 |
uniform int quality_level; |
| 6 |
uniform float fogstructure; |
| 7 |
uniform float cloud_self_shading; |
| 8 |
uniform float scattering; |
| 9 |
uniform float terminator; |
| 10 |
uniform float terrain_alt; |
| 11 |
uniform float overcast; |
| 12 |
uniform float eye_alt; |
| 13 |
|
| 14 |
const float terminator_width = 200000.0; |
| 15 |
const float EarthRadius = 5800000.0; |
| 16 |
|
| 17 |
varying vec3 relPos; |
| 18 |
varying vec4 light_diffuse_comp; |
| 19 |
varying vec3 normal; |
| 20 |
varying vec3 worldPos; |
| 21 |
|
| 22 |
float Noise2D(in vec2 coord, in float wavelength); |
| 23 |
vec3 filter_combined (in vec3 color) ; |
| 24 |
float Noise3D(in vec3 coord, in float wavelength); |
| 25 |
|
| 26 |
const float AtmosphericScaleHeight = 8500.0; |
| 27 |
|
| 28 |
// Development tools: |
| 29 |
// Reduce haze to almost zero, while preserving lighting. Useful for observing distant tiles. |
| 30 |
// Keeps the calculation overhead. This can be used for profiling. |
| 31 |
// Possible values: 0:Normal, 1:Reduced haze. |
| 32 |
const int reduce_haze_without_removing_calculation_overhead = 0; |
| 33 |
|
| 34 |
// standard ALS fog function with exp(-d/D) fading and cutoff at low altitude and exp(-d^2/D^2) at high altitude |
| 35 |
float fog_func (in float targ, in float alt) |
| 36 |
{ |
| 37 |
float fade_mix; |
| 38 |
|
| 39 |
targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn |
| 40 |
|
| 41 |
// for large altitude > 30 km, we switch to some component of quadratic distance fading to |
| 42 |
// create the illusion of improved visibility range |
| 43 |
|
| 44 |
if (alt < 30000.0) { |
| 45 |
return exp(-targ - targ * targ * targ * targ); |
| 46 |
} else if (alt < 50000.0) { |
| 47 |
fade_mix = (alt - 30000.0)/20000.0; |
| 48 |
return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0)); |
| 49 |
} else { |
| 50 |
return exp(- targ * targ - pow(targ,4.0)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// altitude correction for exponential drop in atmosphere density |
| 55 |
float alt_factor(in float eye_alt, in float vertex_alt) |
| 56 |
{ |
| 57 |
float h0 = AtmosphericScaleHeight; |
| 58 |
float h1 = min(eye_alt,vertex_alt); |
| 59 |
float h2 = max(eye_alt,vertex_alt); |
| 60 |
|
| 61 |
if ((h2-h1) < 200.0) // use a Taylor-expanded version |
| 62 |
{ |
| 63 |
return 0.5 * (exp(-h2/h0) + exp(-h1/h0)); |
| 64 |
} else { |
| 65 |
return h0/(h2-h1) * (exp(-h1/h0) - exp(-h2/h0)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Rayleigh in-scatter function |
| 70 |
float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt) |
| 71 |
{ |
| 72 |
float fade_length = avisibility * (2.5 - 2.2 * sqrt(air_pollution)); |
| 73 |
fade_length = fade_length / alt_factor(eye_alt, vertex_alt); |
| 74 |
return 1.0-exp(-dist/max(15000.0,fade_length)); |
| 75 |
} |
| 76 |
|
| 77 |
// Rayleigh out-scattering color shift |
| 78 |
vec3 rayleigh_out_shift(in vec3 color, in float outscatter) |
| 79 |
{ |
| 80 |
color.r = color.r * (1.0 - 0.4 * outscatter); |
| 81 |
color.g = color.g * (1.0 - 0.8 * outscatter); |
| 82 |
color.b = color.b * (1.0 - 1.6 * outscatter); |
| 83 |
return color; |
| 84 |
} |
| 85 |
|
| 86 |
// the generalized logistic function used to compute lightcurves |
| 87 |
float light_curve (in float x, in float a, in float b, in float c, in float d, in float e) |
| 88 |
{ |
| 89 |
x = x - 0.5; |
| 90 |
// use the asymptotics to shorten computations |
| 91 |
if (x > 30.0) { return e; } |
| 92 |
if (x < -15.0) { return 0.0; } |
| 93 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 94 |
} |
| 95 |
|
| 96 |
// the haze color function |
| 97 |
vec3 get_hazeColor(in float lightArg) |
| 98 |
{ |
| 99 |
vec3 hazeColor; |
| 100 |
hazeColor.r = light_curve(lightArg, 8.305e-06, 0.161, 4.827-3.0 *air_pollution, 3.04e-05, 1.0); |
| 101 |
hazeColor.g = light_curve(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 102 |
hazeColor.b = light_curve(lightArg, 1.330e-05, 0.264, 1.527+ 2.0*air_pollution, 1.08e-05, 1.0); |
| 103 |
return hazeColor; |
| 104 |
} |
| 105 |
|
| 106 |
// Apply the ALS haze model to a given fragment |
| 107 |
vec4 applyHaze(inout vec4 fragColor, |
| 108 |
inout vec3 hazeColor, |
| 109 |
in vec3 secondary_light, |
| 110 |
in float ct, |
| 111 |
in float hazeLayerAltitude, |
| 112 |
in float visibility, |
| 113 |
in float avisibility, |
| 114 |
in float dist, |
| 115 |
in float lightArg, |
| 116 |
in float mie_angle) |
| 117 |
{ |
| 118 |
float mvisibility = min(visibility,avisibility); |
| 119 |
|
| 120 |
if (dist > 0.04 * mvisibility) |
| 121 |
{ |
| 122 |
float transmission; |
| 123 |
float vAltitude; |
| 124 |
float delta_zv; |
| 125 |
float H; |
| 126 |
float distance_in_layer; |
| 127 |
float transmission_arg; |
| 128 |
float intensity; |
| 129 |
float eShade; |
| 130 |
|
| 131 |
float delta_z = hazeLayerAltitude - eye_alt; |
| 132 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 133 |
float yprime_alt = light_diffuse_comp.a; |
| 134 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 135 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 136 |
vec3 n = normal; |
| 137 |
n = normalize(n); |
| 138 |
|
| 139 |
// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer |
| 140 |
if (delta_z > 0.0) { // we're inside the layer |
| 141 |
if (ct < 0.0) { // we look down |
| 142 |
distance_in_layer = dist; |
| 143 |
vAltitude = min(distance_in_layer,mvisibility) * ct; |
| 144 |
delta_zv = delta_z - vAltitude; |
| 145 |
} else { |
| 146 |
// we may look through upper layer edge |
| 147 |
H = dist * ct; |
| 148 |
|
| 149 |
if (H > delta_z) { |
| 150 |
distance_in_layer = dist/H * delta_z; |
| 151 |
} else { |
| 152 |
distance_in_layer = dist; |
| 153 |
} |
| 154 |
|
| 155 |
vAltitude = min(distance_in_layer,visibility) * ct; |
| 156 |
delta_zv = delta_z - vAltitude; |
| 157 |
} |
| 158 |
} else { // we see the layer from above, delta_z < 0.0 |
| 159 |
H = dist * -ct; |
| 160 |
if (H < (-delta_z)) { // we don't see into the layer at all, aloft visibility is the only fading |
| 161 |
distance_in_layer = 0.0; |
| 162 |
delta_zv = 0.0; |
| 163 |
} else { |
| 164 |
vAltitude = H + delta_z; |
| 165 |
distance_in_layer = vAltitude/H * dist; |
| 166 |
vAltitude = min(distance_in_layer,visibility) * (-ct); |
| 167 |
delta_zv = vAltitude; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if ((quality_level > 4) && (abs(delta_z) < 400.0)) { |
| 172 |
float blur_thickness = 50.0; |
| 173 |
float cphi = dot(vec3(0.0, 1.0, 0.0), relPos)/dist; |
| 174 |
float ctlayer = delta_z/dist-0.01 + 0.02 * Noise2D(vec2(cphi,1.0),0.1) -0.01; |
| 175 |
float ctblur = 0.035 ; |
| 176 |
float blur_dist; |
| 177 |
|
| 178 |
blur_dist = dist * (1.0-smoothstep(0.0,300.0,-delta_z)) * smoothstep(-400.0,-200.0, -delta_z); |
| 179 |
blur_dist = blur_dist * smoothstep(ctlayer-4.0*ctblur, ctlayer-ctblur, ct) * (1.0-smoothstep(ctlayer+0.5*ctblur, ctlayer+ctblur, ct)); |
| 180 |
distance_in_layer = max(distance_in_layer, blur_dist); |
| 181 |
} |
| 182 |
|
| 183 |
// ground haze cannot be thinner than aloft visibility in the model, |
| 184 |
// so we need to use aloft visibility otherwise |
| 185 |
transmission_arg = (dist-distance_in_layer)/avisibility; |
| 186 |
float eqColorFactor; |
| 187 |
|
| 188 |
if (quality_level > 3) { |
| 189 |
float noise_1500m = Noise3D(worldPos.xyz, 1500.0); |
| 190 |
float noise_2000m = Noise3D(worldPos.xyz, 2000.0); |
| 191 |
transmission_arg = transmission_arg + (distance_in_layer/(1.0 * mvisibility + 1.0 * mvisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) )); |
| 192 |
} else { |
| 193 |
transmission_arg = transmission_arg + (distance_in_layer/mvisibility); |
| 194 |
} |
| 195 |
|
| 196 |
// this combines the Weber-Fechner intensity |
| 197 |
eqColorFactor = 1.0 - 0.1 * delta_zv/mvisibility - (1.0 - effective_scattering); |
| 198 |
transmission = fog_func(transmission_arg, eye_alt); |
| 199 |
|
| 200 |
// there's always residual intensity, we should never be driven to zero |
| 201 |
if (eqColorFactor < 0.2) eqColorFactor = 0.2; |
| 202 |
|
| 203 |
// now dim the light for haze |
| 204 |
eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt); |
| 205 |
|
| 206 |
// Mie-like factor |
| 207 |
if (lightArg < 10.0) { |
| 208 |
intensity = length(hazeColor); |
| 209 |
float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt)); |
| 210 |
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)) ); |
| 211 |
} |
| 212 |
|
| 213 |
intensity = length(hazeColor); |
| 214 |
|
| 215 |
if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly |
| 216 |
{ |
| 217 |
// high altitude desaturation of the haze color |
| 218 |
hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, eye_alt))); |
| 219 |
|
| 220 |
// blue hue of haze |
| 221 |
hazeColor.x = hazeColor.x * 0.83; |
| 222 |
hazeColor.y = hazeColor.y * 0.9; |
| 223 |
|
| 224 |
// additional blue in indirect light |
| 225 |
float fade_out = max(0.65 - 0.3 *overcast, 0.45); |
| 226 |
intensity = length(hazeColor); |
| 227 |
hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) )); |
| 228 |
|
| 229 |
// change haze color to blue hue for strong fogging |
| 230 |
hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor)))); |
| 231 |
|
| 232 |
// reduce haze intensity when looking at shaded surfaces, only in terminator region |
| 233 |
float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission)); |
| 234 |
hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator)); |
| 235 |
} |
| 236 |
|
| 237 |
// don't let the light fade out too rapidly |
| 238 |
lightArg = (terminator + 200000.0)/100000.0; |
| 239 |
float minLightIntensity = min(0.2,0.16 * lightArg + 0.5); |
| 240 |
vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4); |
| 241 |
hazeColor.rgb *= eqColorFactor * eShade; |
| 242 |
hazeColor.rgb = max(hazeColor.rgb, minLight.rgb); |
| 243 |
|
| 244 |
// Testing phase controls |
| 245 |
if (reduce_haze_without_removing_calculation_overhead == 1) |
| 246 |
{ |
| 247 |
transmission = 1.0 - (transmission/1000000.0); |
| 248 |
} |
| 249 |
|
| 250 |
// finally, mix fog in |
| 251 |
if (quality_level > 4) { |
| 252 |
float backscatter = 0.5* min(1.0,10000.0/(mvisibility*mvisibility)); |
| 253 |
fragColor.rgb = mix(hazeColor+secondary_light * backscatter , fragColor.rgb,transmission); |
| 254 |
} else { |
| 255 |
fragColor.rgb = mix(clamp(hazeColor,0.0,1.0) , clamp(fragColor.rgb,0.0,1.0),transmission); |
| 256 |
} |
| 257 |
} // end if (dist > 0.04 * mvisibility) |
| 258 |
|
| 259 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 260 |
return fragColor; |
| 261 |
} |