lens_flares.glslv 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var NUM_LIGHTS 0
  6. /*============================================================================*/
  7. #define LIGHT_INDEX 0
  8. #include <std.glsl>
  9. #include <math.glslv>
  10. uniform mat3 u_view_tsr;
  11. uniform mat4 u_proj_matrix;
  12. uniform vec3 u_light_directions[NUM_LIGHTS];
  13. /*==============================================================================
  14. SHADER INTERFACE
  15. ==============================================================================*/
  16. GLSL_IN float a_lf_dist;
  17. GLSL_IN vec2 a_lf_bb_vertex;
  18. GLSL_IN vec2 a_texcoord;
  19. //------------------------------------------------------------------------------
  20. GLSL_OUT vec2 v_texcoord;
  21. /*==============================================================================
  22. MAIN
  23. ==============================================================================*/
  24. void main(void) {
  25. v_texcoord = a_texcoord;
  26. // locate flare center
  27. vec3 dir = normalize(u_light_directions[LIGHT_INDEX]);
  28. vec4 pos_clip = u_proj_matrix * vec4(tsr9_transform_dir(u_view_tsr, dir), 0.0);
  29. pos_clip.x /= pos_clip.w;
  30. pos_clip.y /= pos_clip.w;
  31. // remove duplicate rear flares
  32. pos_clip += 99999.0 * step(pos_clip.z, 0.0);
  33. // cull by distance
  34. pos_clip += 100.0 * (step(1.0, abs(pos_clip.x)) + step(1.0, abs(pos_clip.y)));
  35. // move flares reverse to camera movement
  36. pos_clip.x = a_lf_dist * pos_clip.x;
  37. pos_clip.y = a_lf_dist * pos_clip.y;
  38. // billboard vertices
  39. float aspect = u_proj_matrix[1][1] / u_proj_matrix[0][0];
  40. vec2 bb_rel_pos = vec2(a_lf_bb_vertex.x / aspect, a_lf_bb_vertex.y);
  41. // scale flares but not sun
  42. if (a_lf_dist < 0.999) {
  43. const float SCALE_FACTOR = 1.9;
  44. bb_rel_pos *= (1.0 + SCALE_FACTOR * length(pos_clip.xy));
  45. }
  46. // z = 0.9999; compare with skydome
  47. gl_Position = vec4(pos_clip.xy + bb_rel_pos, 0.999999, 1.0);
  48. }