2014-02-25 08:49:37 +00:00
|
|
|
// -*-C++-*-
|
2016-08-03 07:31:07 +00:00
|
|
|
#version 120
|
2014-02-25 08:49:37 +00:00
|
|
|
|
|
|
|
uniform sampler2D texture;
|
|
|
|
|
|
|
|
uniform float visibility;
|
|
|
|
uniform float avisibility;
|
|
|
|
uniform float hazeLayerAltitude;
|
|
|
|
uniform float eye_alt;
|
2014-02-28 07:15:41 +00:00
|
|
|
uniform float terminator;
|
2014-07-30 09:21:09 +00:00
|
|
|
uniform float size;
|
2014-02-25 08:49:37 +00:00
|
|
|
|
2016-03-18 07:32:34 +00:00
|
|
|
uniform bool use_IR_vision;
|
|
|
|
uniform bool use_night_vision;
|
2014-10-24 06:36:03 +00:00
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
varying vec3 relPos;
|
2014-04-24 06:43:28 +00:00
|
|
|
varying vec2 rawPos;
|
2014-02-25 08:49:37 +00:00
|
|
|
varying float pixelSize;
|
|
|
|
|
|
|
|
float alt;
|
|
|
|
|
2014-04-24 06:43:28 +00:00
|
|
|
float Noise2D(in vec2 coord, in float wavelength);
|
2014-02-25 08:49:37 +00:00
|
|
|
|
|
|
|
float fog_func (in float targ)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
float fade_mix;
|
|
|
|
|
|
|
|
// for large altitude > 30 km, we switch to some component of quadratic distance fading to
|
|
|
|
// create the illusion of improved visibility range
|
|
|
|
|
|
|
|
targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
|
|
|
|
|
|
|
|
|
|
|
|
if (alt < 30000.0)
|
|
|
|
{return exp(-targ - targ * targ * targ * targ);}
|
|
|
|
else if (alt < 50000.0)
|
|
|
|
{
|
|
|
|
fade_mix = (alt - 30000.0)/20000.0;
|
2015-01-05 20:49:40 +00:00
|
|
|
return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
|
2014-02-25 08:49:37 +00:00
|
|
|
}
|
2015-01-05 20:49:40 +00:00
|
|
|
else
|
2014-02-25 08:49:37 +00:00
|
|
|
{
|
|
|
|
return exp(- targ * targ - pow(targ,4.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-29 09:53:46 +00:00
|
|
|
float light_sprite (in vec2 coord, in float transmission, in float noise)
|
2014-02-28 07:15:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
coord.s = coord.s - 0.5;
|
|
|
|
coord.t = coord.t - 0.5;
|
|
|
|
|
|
|
|
float r = length(coord);
|
|
|
|
|
2015-12-30 11:14:00 +00:00
|
|
|
if (pixelSize<1.3) {return 0.08;}
|
2014-02-28 07:15:41 +00:00
|
|
|
|
2014-10-16 07:24:27 +00:00
|
|
|
float angle = noise * 6.2832;
|
2015-01-05 20:49:40 +00:00
|
|
|
|
2014-04-24 06:43:28 +00:00
|
|
|
float sinphi = dot(vec2 (sin(angle),cos(angle)), normalize(coord));
|
2014-10-24 06:36:03 +00:00
|
|
|
float sinterm = sin(mod((sinphi-3.0) * (sinphi-3.0),6.2832));
|
|
|
|
float ray = 0.0;
|
|
|
|
if (sinterm == 0.0)
|
2015-01-05 20:49:40 +00:00
|
|
|
{ray = 0.0;}
|
|
|
|
else
|
2017-03-04 08:01:45 +00:00
|
|
|
//{ray = clamp(pow(sinterm,10.0),0.0,1.0);}
|
|
|
|
{ray = sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm;}
|
|
|
|
|
2014-02-28 07:15:41 +00:00
|
|
|
|
|
|
|
float fogEffect = (1.0-smoothstep(0.4,0.8,transmission));
|
2015-12-29 09:53:46 +00:00
|
|
|
float halo = 0.2 * exp(-10.0 * r * r);
|
|
|
|
float base = exp(-80.0*r*r);
|
|
|
|
ray *= exp(-40.0 * r * r);
|
2014-02-28 07:15:41 +00:00
|
|
|
|
2017-03-04 08:01:45 +00:00
|
|
|
|
|
|
|
float intensity = clamp(ray + base + halo,0.0,1.0) + 0.1 * fogEffect * (1.0-smoothstep(0.3, 0.6,r));
|
2014-02-28 07:15:41 +00:00
|
|
|
|
2015-12-29 09:53:46 +00:00
|
|
|
return intensity;
|
2014-02-28 07:15:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2015-01-05 20:49:40 +00:00
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
float dist = length(relPos);
|
|
|
|
float delta_z = hazeLayerAltitude - eye_alt;
|
|
|
|
float transmission;
|
|
|
|
float vAltitude;
|
|
|
|
float delta_zv;
|
|
|
|
float H;
|
|
|
|
float distance_in_layer;
|
|
|
|
float transmission_arg;
|
|
|
|
|
2016-03-18 07:32:34 +00:00
|
|
|
if (use_IR_vision) {discard;}
|
|
|
|
|
2015-01-05 20:49:40 +00:00
|
|
|
// Discard the second and third vertex, which are used for directional lighting
|
|
|
|
if (gl_Color.a == 0.0) {discard;}
|
|
|
|
|
|
|
|
float noise = Noise2D(rawPos.xy ,1.0);
|
2014-04-24 06:43:28 +00:00
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
// angle with horizon
|
|
|
|
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
|
|
|
|
|
|
|
|
|
|
|
|
// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
|
|
|
|
|
|
|
|
if (delta_z > 0.0) // we're inside the layer
|
|
|
|
{
|
2015-01-05 20:49:40 +00:00
|
|
|
if (ct < 0.0) // we look down
|
2014-02-25 08:49:37 +00:00
|
|
|
{
|
|
|
|
distance_in_layer = dist;
|
|
|
|
vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
|
|
|
|
delta_zv = delta_z - vAltitude;
|
|
|
|
}
|
|
|
|
else // we may look through upper layer edge
|
|
|
|
{
|
|
|
|
H = dist * ct;
|
|
|
|
if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
|
|
|
|
else {distance_in_layer = dist;}
|
|
|
|
vAltitude = min(distance_in_layer,visibility) * ct;
|
2015-01-05 20:49:40 +00:00
|
|
|
delta_zv = delta_z - vAltitude;
|
2014-02-25 08:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // we see the layer from above, delta_z < 0.0
|
2015-01-05 20:49:40 +00:00
|
|
|
{
|
2014-02-25 08:49:37 +00:00
|
|
|
H = dist * -ct;
|
|
|
|
if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
|
|
|
|
{
|
|
|
|
distance_in_layer = 0.0;
|
|
|
|
delta_zv = 0.0;
|
2015-01-05 20:49:40 +00:00
|
|
|
}
|
2014-02-25 08:49:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
vAltitude = H + delta_z;
|
2015-01-05 20:49:40 +00:00
|
|
|
distance_in_layer = vAltitude/H * dist;
|
2014-02-25 08:49:37 +00:00
|
|
|
vAltitude = min(distance_in_layer,visibility) * (-ct);
|
|
|
|
delta_zv = vAltitude;
|
2015-01-05 20:49:40 +00:00
|
|
|
}
|
2014-02-25 08:49:37 +00:00
|
|
|
}
|
2015-01-05 20:49:40 +00:00
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
|
|
|
|
// ground haze cannot be thinner than aloft visibility in the model,
|
|
|
|
// so we need to use aloft visibility otherwise
|
|
|
|
|
|
|
|
|
|
|
|
transmission_arg = (dist-distance_in_layer)/avisibility;
|
|
|
|
if (visibility < avisibility)
|
|
|
|
{
|
|
|
|
transmission_arg = transmission_arg + (distance_in_layer/visibility);
|
|
|
|
}
|
2015-01-05 20:49:40 +00:00
|
|
|
else
|
2014-02-25 08:49:37 +00:00
|
|
|
{
|
|
|
|
transmission_arg = transmission_arg + (distance_in_layer/avisibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transmission = fog_func(transmission_arg);
|
2014-02-28 07:15:41 +00:00
|
|
|
float lightArg = terminator/100000.0;
|
2014-07-30 09:21:09 +00:00
|
|
|
float attenuationScale = 1.0 + 20.0 * (1.0 -smoothstep(-15.0, 0.0, lightArg));
|
2017-03-04 08:01:45 +00:00
|
|
|
float dist_att = exp(-dist/200.0/size/attenuationScale);
|
2014-02-25 08:49:37 +00:00
|
|
|
|
2015-12-29 09:53:46 +00:00
|
|
|
float intensity = light_sprite(gl_TexCoord[0].st,transmission, noise);
|
|
|
|
vec3 light_color = gl_Color.rgb;
|
2016-03-18 07:32:34 +00:00
|
|
|
|
|
|
|
if (use_night_vision)
|
|
|
|
{light_color.rgb = vec3 (0.0, 1.0, 0.0);}
|
|
|
|
|
2016-01-10 07:42:04 +00:00
|
|
|
light_color = mix(light_color, vec3 (1.0, 1.0, 1.0), 0.5 * intensity * intensity);
|
2015-12-29 09:53:46 +00:00
|
|
|
|
|
|
|
gl_FragColor = vec4 (clamp(light_color.rgb,0.0,1.0), intensity * transmission * dist_att);
|
2015-01-05 20:49:40 +00:00
|
|
|
|
2014-02-25 08:49:37 +00:00
|
|
|
|
|
|
|
}
|