Guest User

CRTAperture

a guest
Mar 30th, 2017
315
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. CRT Shader by EasyMode
  3. License: GPL
  4. */
  5.  
  6. #include "ReShade.fxh"
  7.  
  8. uniform float resX <
  9. ui_type = "drag";
  10. ui_min = 1.0;
  11. ui_max = BUFFER_WIDTH;
  12. ui_label = "Screen Width [CRT-Aperture]";
  13. > = 320.0;
  14.  
  15. uniform float resY <
  16. ui_type = "drag";
  17. ui_min = 1.0;
  18. ui_max = BUFFER_HEIGHT;
  19. ui_label = "Screen Height [CRT-Aperture]";
  20. > = 240.0;
  21.  
  22. uniform int SHARPNESS_IMAGE <
  23. ui_type = "drag";
  24. ui_min = 1.0;
  25. ui_max = 5.0;
  26. ui_step = 1.0;
  27. ui_label = "Sharpness Image [CRT-Aperture]";
  28. > = 1;
  29.  
  30. uniform int SHARPNESS_EDGES <
  31. ui_type = "drag";
  32. ui_min = 1;
  33. ui_max = 5;
  34. ui_step = 1;
  35. ui_label = "Sharpness Edges [CRT-Aperture]";
  36. > = 3;
  37.  
  38. uniform float GLOW_WIDTH <
  39. ui_type = "drag";
  40. ui_min = 0.05;
  41. ui_max = 0.65;
  42. ui_step = 0.05;
  43. ui_label = "Glow Width [CRT-Aperture]";
  44. > = 0.5;
  45.  
  46. uniform float GLOW_HEIGHT <
  47. ui_type = "drag";
  48. ui_min = 0.05;
  49. ui_max = 0.65;
  50. ui_step = 0.05;
  51. ui_label = "Glow Height [CRT-Aperture]";
  52. > = 0.5;
  53.  
  54. uniform float GLOW_HALATION <
  55. ui_type = "drag";
  56. ui_min = 0.0;
  57. ui_max = 1.0;
  58. ui_step = 0.01;
  59. ui_label = "Glow Halation [CRT-Aperture]";
  60. > = 0.1;
  61.  
  62. uniform float GLOW_DIFFUSION <
  63. ui_type = "drag";
  64. ui_min = 0.0;
  65. ui_max = 1.0;
  66. ui_step = 0.01;
  67. ui_label = "Glow Diffusion [CRT-Aperture]";
  68. > = 0.05;
  69.  
  70. uniform float MASK_COLORS <
  71. ui_type = "drag";
  72. ui_min = 2.0;
  73. ui_max = 3.0;
  74. ui_step = 1.0;
  75. ui_label = "Mask Colors [CRT-Aperture]";
  76. > = 2.0;
  77.  
  78. uniform float MASK_STRENGTH <
  79. ui_type = "drag";
  80. ui_min = 0.0;
  81. ui_max = 1.0;
  82. ui_step = 0.05;
  83. ui_label = "Mask Strength [CRT-Aperture]";
  84. > = 0.3;
  85.  
  86. uniform int MASK_SIZE <
  87. ui_type = "drag";
  88. ui_min = 1;
  89. ui_max = 9;
  90. ui_step = 1;
  91. ui_label = "Mask Size [CRT-Aperture]";
  92. > = 1;
  93.  
  94. uniform float SCANLINE_SIZE_MIN <
  95. ui_type = "drag";
  96. ui_min = 0.5;
  97. ui_max = 1.5;
  98. ui_step = 0.05;
  99. ui_label = "Scanline Size Min. [CRT-Aperture]";
  100. > = 0.5;
  101.  
  102. uniform float SCANLINE_SIZE_MAX <
  103. ui_type = "drag";
  104. ui_min = 0.5;
  105. ui_max = 1.5;
  106. ui_step = 0.05;
  107. ui_label = "Scanline Size Max. [CRT-Aperture]";
  108. > = 1.5;
  109.  
  110. uniform float GAMMA_INPUT <
  111. ui_type = "drag";
  112. ui_min = 1.0;
  113. ui_max = 5.0;
  114. ui_step = 0.1;
  115. ui_label = "Gamma Input [CRT-Aperture]";
  116. > = 2.4;
  117.  
  118. uniform float GAMMA_OUTPUT <
  119. ui_type = "drag";
  120. ui_min = 1.0;
  121. ui_max = 5.0;
  122. ui_step = 0.1;
  123. ui_label = "Gamma Output [CRT-Aperture]";
  124. > = 2.4;
  125.  
  126. uniform float BRIGHTNESS <
  127. ui_type = "drag";
  128. ui_min = 0.0;
  129. ui_max = 2.0;
  130. ui_step = 0.05;
  131. ui_label = "Brightness [CRT-Aperture]";
  132. > = 1.5;
  133.  
  134. #define FIX(c) max(abs(c), 1e-5)
  135. #define PI 3.141592653589
  136. #define TEX2D(c) pow(tex2D(tex, c).rgb, GAMMA_INPUT)
  137.  
  138. float mod(float x, float y)
  139. {
  140. return x - y * floor(x/y);
  141. }
  142.  
  143. float3 blur(float3x3 m, float dist, float rad)
  144. {
  145. float3 x = float3(dist - 1.0, dist, dist + 1.0) / rad;
  146. float3 w = exp2(x * x * -1.0);
  147.  
  148. return (m[0] * w.x + m[1] * w.y + m[2] * w.z) / (w.x + w.y + w.z);
  149. }
  150.  
  151. float3x3 get_color_matrix(sampler2D tex, float2 co, float2 dx)
  152. {
  153. return float3x3(TEX2D(co - dx), TEX2D(co), TEX2D(co + dx));
  154. }
  155.  
  156. float3 filter_gaussian(sampler2D tex, float2 co, float2 tex_size)
  157. {
  158. float2 dx = float2(1.0 / tex_size.x, 0.0);
  159. float2 dy = float2(0.0, 1.0 / tex_size.y);
  160. float2 pix_co = co * tex_size;
  161. float2 tex_co = (floor(pix_co) + 0.5) / tex_size;
  162. float2 dist = (frac(pix_co) - 0.5) * -1.0;
  163.  
  164. float3x3 line0 = get_color_matrix(tex, tex_co - dy, dx);
  165. float3x3 line1 = get_color_matrix(tex, tex_co, dx);
  166. float3x3 line2 = get_color_matrix(tex, tex_co + dy, dx);
  167. float3x3 column = float3x3(blur(line0, dist.x, GLOW_WIDTH),
  168. blur(line1, dist.x, GLOW_WIDTH),
  169. blur(line2, dist.x, GLOW_WIDTH));
  170.  
  171. return blur(column, dist.y, GLOW_HEIGHT);
  172. }
  173.  
  174. float3 filter_lanczos(sampler2D tex, float2 co, float2 tex_size, float sharp)
  175. {
  176. tex_size.x *= sharp;
  177.  
  178. float2 dx = float2(1.0 / tex_size.x, 0.0);
  179. float2 pix_co = co * tex_size - float2(0.5, 0.0);
  180. float2 tex_co = (floor(pix_co) + float2(0.5, 0.0)) / tex_size;
  181. float2 dist = frac(pix_co);
  182. float4 coef = PI * float4(dist.x + 1.0, dist.x, dist.x - 1.0, dist.x - 2.0);
  183.  
  184. coef = FIX(coef);
  185. coef = 2.0 * sin(coef) * sin(coef / 2.0) / (coef * coef);
  186. coef /= dot(coef, float4(1.0,1.0,1.0,1.0));
  187.  
  188. float4 col1 = float4(TEX2D(tex_co), 1.0);
  189. float4 col2 = float4(TEX2D(tex_co + dx), 1.0);
  190.  
  191. return mul(coef, float4x4(col1, col1, col2, col2)).rgb;
  192. }
  193.  
  194. float3 get_scanline_weight(float x, float3 col)
  195. {
  196. float3 beam = lerp(float3(SCANLINE_SIZE_MIN,SCANLINE_SIZE_MIN,SCANLINE_SIZE_MIN), float3(SCANLINE_SIZE_MAX,SCANLINE_SIZE_MAX,SCANLINE_SIZE_MAX), col);
  197. float3 x_mul = 2.0 / beam;
  198. float3 x_offset = x_mul * 0.5;
  199.  
  200. return smoothstep(0.0, 1.0, 1.0 - abs(x * x_mul - x_offset)) * x_offset;
  201. }
  202.  
  203. float3 get_mask_weight(float x)
  204. {
  205. float i = mod(floor(x * resX * ReShade::ScreenSize / (resX * MASK_SIZE)), MASK_COLORS);
  206.  
  207. if (i == 0.0) return lerp(float3(1.0, 0.0, 1.0), float3(1.0, 0.0, 0.0), MASK_COLORS - 2.0);
  208. else if (i == 1.0) return float3(0.0, 1.0, 0.0);
  209. else return float3(0.0, 0.0, 1.0);
  210. }
  211.  
  212. float4 PS_CRTAperture(float4 vpos : SV_Position, float2 co : TexCoord) : SV_Target
  213. {
  214. float3 col_glow = filter_gaussian(ReShade::BackBuffer, co, float2(resX,resY)).rgb;
  215. float3 col_soft = filter_lanczos(ReShade::BackBuffer, co, float2(resX,resY), SHARPNESS_IMAGE).rgb;
  216. float3 col_sharp = filter_lanczos(ReShade::BackBuffer, co, float2(resX,resY), SHARPNESS_EDGES).rgb;
  217. float3 col = sqrt(col_sharp * col_soft);
  218.  
  219. col *= get_scanline_weight(frac(co.y * resY), col_soft);
  220. col_glow = saturate(col_glow - col);
  221. col += col_glow * col_glow * GLOW_HALATION;
  222. col = lerp(col, col * get_mask_weight(co.x) * MASK_COLORS, MASK_STRENGTH);
  223. col += col_glow * GLOW_DIFFUSION;
  224. col = pow(col * BRIGHTNESS, 1.0 / GAMMA_OUTPUT);
  225.  
  226. return float4(col, 1.0);
  227. }
  228.  
  229. technique ApertureCRT {
  230. pass CRT_Aperture {
  231. VertexShader=PostProcessVS;
  232. PixelShader=PS_CRTAperture;
  233. }
  234. }
RAW Paste Data