gb-pass-4.fs 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This is a port of the original CG shader to the quark format
  2. // the original shader can be found here :
  3. // https://github.com/libretro/common-shaders/tree/master/handheld/gameboy
  4. ///////////////////////////////////////////////////////////////////////////
  5. // //
  6. // Gameboy Classic Shader v0.2.2 //
  7. // //
  8. // Copyright (C) 2013 Harlequin : unknown92835@gmail.com //
  9. // //
  10. // This program is free software: you can redistribute it and/or modify //
  11. // it under the terms of the GNU General Public License as published by //
  12. // the Free Software Foundation, either version 3 of the License, or //
  13. // (at your option) any later version. //
  14. // //
  15. // This program is distributed in the hope that it will be useful, //
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  18. // GNU General Public License for more details. //
  19. // //
  20. // You should have received a copy of the GNU General Public License //
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. #version 150
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. //config //
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. #define contrast 1.95 //analogous to the contrast slider on the original Gameboy, higher values darken the image - [0, 1]
  29. #define bg_smoothing 0.75 //higher values suppress changes in background color directly beneath the foreground to improve image clarity - [0, 1]
  30. #define shadow_opacity 0.55 //how strongly shadows affect the background, higher values darken the shadows - [0, 1]
  31. #define shadow_offset_x 1.0 //how far the shadow should be shifted to the right in pixels - [-infinity, infinity]
  32. #define shadow_offset_y 1.0 //how far the shadow should be shifted to down in pixels - [-infinity, infinity]
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. //fragment definitions/declarations //
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. #define bg_color texture(pixmap[0], vec2(0.25, 0.5)) //sample the background color from the palette
  37. #define shadow_alpha (contrast * shadow_opacity) //blending factor used when overlaying shadows on the background
  38. #define shadow_offset vec2(shadow_offset_x * sourceSize[0].z, shadow_offset_y * sourceSize[0].w) //offset for the shadow
  39. uniform sampler2D source[];
  40. uniform sampler2D frame[];
  41. uniform sampler2D pixmap[];
  42. uniform vec4 sourceSize[];
  43. uniform vec4 targetSize;
  44. in Vertex {
  45. vec2 texCoord;
  46. };
  47. out vec4 fragColor;
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //fragment shader //
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. void main(void){
  52. //sample all the relevant textures
  53. vec4 foreground = texture(source[2], texCoord);
  54. vec4 background = texture(pixmap[1], texCoord);
  55. vec4 shadows = texture(source[0], texCoord - shadow_offset);
  56. vec4 background_color = bg_color;
  57. //foreground and background are blended with the background color
  58. foreground *= bg_color;
  59. background -= (background - 0.5) * bg_smoothing * ((foreground.a > 0.0)?1.0:0.0); //suppress drastic background color changes under the foreground to improve clarity
  60. background.rgb = clamp(vec3( //allows for highlights, background = bg_color when the background color is 0.5 gray
  61. bg_color.r + mix(-1.0, 1.0, background.r),
  62. bg_color.g + mix(-1.0, 1.0, background.g),
  63. bg_color.b + mix(-1.0, 1.0, background.b) ),0.0,1.0);
  64. //shadows are alpha blended with the background
  65. vec4 out_color = (shadows * shadows.a * shadow_alpha) + (background * (1 - shadows.a * shadow_alpha));
  66. //foreground is alpha blended with the shadowed background
  67. out_color = (foreground * foreground.a * contrast) + (out_color * (1 - foreground.a * contrast));
  68. fragColor=out_color;
  69. }