refraction.glslf 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef REFRACTION_GLSLF
  2. #define REFRACTION_GLSLF
  3. // #import u_scene_depth
  4. // #import v_view_depth
  5. // #import u_refractmap
  6. /*==============================================================================
  7. VARS
  8. ==============================================================================*/
  9. #var REFRACTIVE 0
  10. #var USE_REFRACTION_CORRECTION 0
  11. /*============================================================================*/
  12. #if REFRACTIVE
  13. #if USE_REFRACTION_CORRECTION
  14. #include <pack.glslf>
  15. #endif
  16. #include <color_util.glslf>
  17. #if USE_REFRACTION_CORRECTION
  18. float refraction_correction(in float scene_depth,
  19. inout vec2 refract_coord, in vec2 screen_coord) {
  20. vec4 scene_depth_refr_rgba = GLSL_TEXTURE(u_scene_depth, refract_coord);
  21. float scene_depth_refr = unpack_float(scene_depth_refr_rgba);
  22. // if refracted object is closer than surface use undisturbed coords
  23. if (scene_depth_refr < v_view_depth) {
  24. refract_coord = screen_coord;
  25. return scene_depth;
  26. } else {
  27. return scene_depth_refr;
  28. }
  29. return scene_depth;
  30. }
  31. #endif
  32. vec3 material_refraction(in vec3 tex_pos_clip, in vec2 perturbation) {
  33. vec2 screen_coord = tex_pos_clip.xy/tex_pos_clip.z;
  34. vec2 refract_coord = screen_coord + perturbation;
  35. #if USE_REFRACTION_CORRECTION
  36. vec4 scene_depth_rgba = GLSL_TEXTURE_PROJ(u_scene_depth, tex_pos_clip);
  37. float scene_depth = unpack_float(scene_depth_rgba);
  38. refraction_correction(scene_depth, refract_coord, screen_coord);
  39. #endif
  40. vec3 refract_color = GLSL_TEXTURE(u_refractmap, refract_coord).rgb;
  41. srgb_to_lin(refract_color);
  42. return refract_color;
  43. }
  44. #endif
  45. #endif