2014-11-08 13:47:08 -05:00
|
|
|
uniform sampler2D dtex;
|
|
|
|
uniform float density;
|
|
|
|
uniform vec3 fogcol;
|
|
|
|
|
|
|
|
flat in vec3 center;
|
|
|
|
flat in float energy;
|
|
|
|
flat in vec3 col;
|
|
|
|
flat in float radius;
|
|
|
|
|
|
|
|
out vec4 Fog;
|
|
|
|
|
2016-06-27 07:11:27 -04:00
|
|
|
#stk_include "utils/getPosFromUVDepth.frag"
|
2014-11-08 13:47:08 -05:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2017-12-25 01:00:10 -05:00
|
|
|
vec4 pseudocenter = u_view_matrix * vec4(center.xyz, 1.0);
|
2014-11-08 13:47:08 -05:00
|
|
|
pseudocenter /= pseudocenter.w;
|
|
|
|
vec3 light_pos = pseudocenter.xyz;
|
|
|
|
vec3 light_col = col.xyz;
|
|
|
|
|
|
|
|
// Compute pixel position
|
2017-12-25 01:00:10 -05:00
|
|
|
vec2 texc = 2. * gl_FragCoord.xy / u_screen;
|
2014-11-08 13:47:08 -05:00
|
|
|
float z = texture(dtex, texc).x;
|
2017-12-25 01:00:10 -05:00
|
|
|
vec4 pixelpos = getPosFromUVDepth(vec3(texc, z), u_inverse_projection_matrix);
|
2014-11-08 13:47:08 -05:00
|
|
|
vec3 eyedir = -normalize(pixelpos.xyz);
|
|
|
|
|
|
|
|
vec3 farthestpoint = - eyedir * (min(dot(-eyedir, light_pos) + radius, length(pixelpos.xyz)));
|
|
|
|
vec3 closestpoint = - eyedir * (dot(-eyedir, light_pos) - radius);
|
|
|
|
if (closestpoint.z < 1.) closestpoint = vec3(0.);
|
|
|
|
|
2016-06-27 07:39:20 -04:00
|
|
|
float stepsize = length(farthestpoint - closestpoint) / 16.;
|
2014-11-08 13:47:08 -05:00
|
|
|
vec3 fog = vec3(0.);
|
|
|
|
vec3 xpos = farthestpoint;
|
2018-12-23 17:16:18 -05:00
|
|
|
vec3 fog_factor = light_col * density * stepsize * energy * 20.;
|
|
|
|
vec3 xpos_step = eyedir * stepsize;
|
2014-11-08 13:47:08 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
float d = distance(light_pos, xpos);
|
2016-06-27 07:39:20 -04:00
|
|
|
float l = (16. - float(i)) * stepsize;
|
2018-12-23 17:16:18 -05:00
|
|
|
fog += fog_factor / (1. + d * d) * max((radius - d) / radius, 0.) * exp(- density * d) * exp(- density * l);
|
|
|
|
xpos += xpos_step;
|
2014-11-08 13:47:08 -05:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:23:33 -05:00
|
|
|
Fog = vec4(fogcol * fog, 0.);
|
2014-11-08 13:47:08 -05:00
|
|
|
}
|