pointlightscatter.frag 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. uniform sampler2D dtex;
  2. uniform float density;
  3. uniform vec3 fogcol;
  4. flat in vec3 center;
  5. flat in float energy;
  6. flat in vec3 col;
  7. flat in float radius;
  8. out vec4 Fog;
  9. vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
  10. void main()
  11. {
  12. vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0);
  13. pseudocenter /= pseudocenter.w;
  14. vec3 light_pos = pseudocenter.xyz;
  15. vec3 light_col = col.xyz;
  16. // Compute pixel position
  17. vec2 texc = 2. * gl_FragCoord.xy / screen;
  18. float z = texture(dtex, texc).x;
  19. vec4 pixelpos = getPosFromUVDepth(vec3(texc, z), InverseProjectionMatrix);
  20. vec3 eyedir = -normalize(pixelpos.xyz);
  21. vec3 farthestpoint = - eyedir * (min(dot(-eyedir, light_pos) + radius, length(pixelpos.xyz)));
  22. vec3 closestpoint = - eyedir * (dot(-eyedir, light_pos) - radius);
  23. if (closestpoint.z < 1.) closestpoint = vec3(0.);
  24. float stepsize = length(farthestpoint - closestpoint) / 16;
  25. vec3 fog = vec3(0.);
  26. vec3 xpos = farthestpoint;
  27. for (int i = 0; i < 16; i++)
  28. {
  29. float d = distance(light_pos, xpos);
  30. float l = (16 - i) * stepsize;
  31. float att = energy * 20. / (1. + d * d);
  32. att *= max((radius - d) / radius, 0.);
  33. fog += density * light_col * att * exp(- density * d) * exp(- density * l) * stepsize;
  34. xpos += stepsize * eyedir;
  35. }
  36. Fog = vec4(fogcol * fog, 0.);
  37. }