sp_displace.frag 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. uniform sampler2D displacement_tex;
  2. uniform sampler2D mask_tex;
  3. uniform sampler2D color_tex;
  4. #stk_include "utils/sp_texture_sampling.frag"
  5. uniform vec4 direction;
  6. in vec2 uv;
  7. in float camdist;
  8. out vec4 o_diffuse_color;
  9. const float maxlen = 0.02;
  10. void main()
  11. {
  12. float horiz = texture(displacement_tex, uv + direction.xy).x;
  13. float vert = texture(displacement_tex, (uv.yx + direction.zw) * vec2(0.9)).x;
  14. vec2 offset = vec2(horiz, vert);
  15. offset *= 2.0;
  16. offset -= 1.0;
  17. // Fade according to distance to cam
  18. float fade = 1.0 - smoothstep(1.0, 100.0, camdist);
  19. vec4 shiftval;
  20. shiftval.r = step(offset.x, 0.0) * -offset.x;
  21. shiftval.g = step(0.0, offset.x) * offset.x;
  22. shiftval.b = step(offset.y, 0.0) * -offset.y;
  23. shiftval.a = step(0.0, offset.y) * offset.y;
  24. vec2 shift;
  25. shift.x = -shiftval.x + shiftval.y;
  26. shift.y = -shiftval.z + shiftval.w;
  27. shift *= 0.02;
  28. vec2 tc = gl_FragCoord.xy / u_screen;
  29. float mask = texture(mask_tex, tc + shift).x;
  30. tc += (mask < 1.) ? vec2(0.) : shift;
  31. vec4 col = texture(color_tex, tc);
  32. vec4 blend_tex = sampleTextureLayer0(uv);
  33. col.rgb = blend_tex.rgb * blend_tex.a + (1. - blend_tex.a) * col.rgb;
  34. o_diffuse_color = vec4(col.rgb, 1.);
  35. }