caustics.glslf 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef CAUSTICS_GLSLF
  2. #define CAUSTICS_GLSLF
  3. /*==============================================================================
  4. VARS
  5. ==============================================================================*/
  6. #var SUN_NUM 0
  7. #var CAUST_SCALE 0.25
  8. #var CAUST_SPEED vec2(0.0)
  9. #var CAUST_BRIGHT 0.5
  10. /*============================================================================*/
  11. #include <procedural.glslf>
  12. #include <math.glslv>
  13. #define CAUSTICS_VIEW_DISTANCE 100.0
  14. //Add caustics to underwater objects
  15. void apply_caustics(inout vec3 color, float plane_pos,
  16. float time, vec4 shadow_factor, vec3 normal,
  17. vec3 sun_direction, vec3 sun_color_intens,
  18. vec4 sun_q, vec3 pos_world, float view_dist) {
  19. if (view_dist > CAUSTICS_VIEW_DISTANCE)
  20. return;
  21. vec3 v = pos_world + normal;
  22. v.xz = 10.0 * sin(0.1 * v.xz);
  23. // rotate world coordinates to match sun directions
  24. vec3 rotated_world = qrot(qinv(sun_q), v);
  25. vec2 texcoord = rotated_world.xz;
  26. vec3 light_vec = sun_direction;
  27. float l_dot_l = max(dot (normal, light_vec), 0.0);
  28. // how strong will aberration effect be
  29. float aberration = 0.025;
  30. vec2 caust_delta = CAUST_SPEED * time;
  31. // wave aberrations on texture coordinates
  32. texcoord.s += 0.25 * sin( dot (pos_world + time, vec3(1.0)));
  33. texcoord.t += 0.35 * (-sin( dot (pos_world - time, vec3(-0.7))));
  34. texcoord.st += 0.15 * cos( 4.0 * plane_pos - caust_delta.x)
  35. + 1.5 * sin( plane_pos - 0.3 * caust_delta.y);
  36. float scale = CAUST_SCALE * (1.0 + max(0.1 * plane_pos, 0.0));
  37. vec3 caustics = cellular2x2_caust((texcoord / scale), aberration);
  38. caustics *= CAUST_BRIGHT;
  39. caustics *= caustics;
  40. float height_factor = min(0.25 * plane_pos, -plane_pos) + 1.0;
  41. height_factor = max(height_factor, 0.0);
  42. float fade = shadow_factor[SUN_NUM] * l_dot_l;
  43. // side: 1 - above the water, 0 - below the water)
  44. float water_side = max(sign(plane_pos), 0.0);
  45. // caustics kinda reflect to surfaces facing towards the water
  46. fade = fade + max(0.5 * sign(-normal.y) * water_side, 0.0);
  47. fade = min(fade, 1.0);
  48. color += sun_color_intens * caustics * height_factor * fade;
  49. }
  50. #endif