displace.frag 1.1 KB

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