screenspace.fp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #version 140
  2. // Inputs should match the vertex shader's outputs.
  3. in vec2 var_texcoord0;
  4. in vec4 var_screen_texcoord;
  5. // The color texture.
  6. uniform lowp sampler2D texture0;
  7. // The pattern texture.
  8. uniform lowp sampler2D texture_pattern;
  9. // The user defined uniforms.
  10. uniform user_fp
  11. {
  12. // pattern_opts.x - alpha, default 1.0 (set 0.0 to disable the screen space effect).
  13. // pattern_opts.y - scale, default 30.0.
  14. // pattern_opts.z - offset by x, default 0.0.
  15. // pattern_opts.w - rotation in radians.
  16. vec4 pattern_opts;
  17. // The screen size, used to calculate the aspect ratio.
  18. vec4 screen_size;
  19. };
  20. // The final color of the fragment.
  21. out lowp vec4 final_color;
  22. // Rotate 2D vector "v" by the "a" angle in radians
  23. vec2 rotate(vec2 v, float a)
  24. {
  25. float s = sin(a);
  26. float c = cos(a);
  27. return mat2(c, s, -s, c) * v;
  28. }
  29. void main()
  30. {
  31. // Sample the color texture at the fragment's texture coordinates.
  32. vec4 color = texture(texture0, var_texcoord0.xy);
  33. // Counteract the perspective correction and scale the coords.
  34. vec2 pattern_coord = (var_screen_texcoord.xy / var_screen_texcoord.w) * pattern_opts.y;
  35. // + Correct the aspect ratio
  36. float aspect = screen_size.x / screen_size.y;
  37. pattern_coord.x *= aspect;
  38. // + Offset the grid horizontally
  39. pattern_coord.x += pattern_opts.z;
  40. // + Rotate
  41. pattern_coord = rotate(pattern_coord, pattern_opts.w);
  42. // Output the sampled color
  43. if (pattern_opts.x > 0.0)
  44. {
  45. // Sample the pattern at the screen space texture coordinates.
  46. vec4 pattern_color = texture(texture_pattern, pattern_coord);
  47. // Blend the colors: (sRGBA*1) + (dRGBA*(1-sA))
  48. final_color = pattern_color * pattern_opts.x + color * (1.0 - (pattern_color.a * pattern_opts.x));
  49. }
  50. else
  51. {
  52. // No pattern, just output the color.
  53. final_color = color;
  54. }
  55. }