Advertisement
Guest User

crt-hyllian - variable beam width test - v14

a guest
Apr 27th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. /* COMPATIBILITY
  2. - HLSL compilers
  3. - Cg compilers
  4. */
  5.  
  6. /*
  7. Hyllian's CRT Shader
  8.  
  9. Copyright (C) 2011-2014 Hyllian/Jararaca - [email protected]
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. */
  26.  
  27.  
  28. // Uncomment to dilate bright pixels horizontally.
  29. #define DILATION
  30.  
  31. // Control the dilation intensity by changing this param below when DILATION is enabled. Use always values between 0.0 and 1.0.
  32. #define DILATION_STRENGTH 0.9
  33.  
  34. // Uncomment to increase the sharpness of the scanlines.
  35. //#define SHARPER
  36.  
  37. // Control the level of sharpness when SHARPER is enabled.
  38. #define SHARPNESS 2.0
  39.  
  40. // Comment next line if you don't desire the phosphor effect.
  41. //#define PHOSPHOR
  42.  
  43. // Uncomment to enable adjustment of red and green saturation.
  44. //#define RED_GREEN_CONTROL
  45.  
  46. // Control red and green saturation when RED_GREEN_CONTROL is enabled.
  47. #define RED_BOOST 1.0
  48. #define GREEN_BOOST 1.0
  49.  
  50. // Control scanlines transparency by changing this param below. Use always values between 0.0 and 1.0.
  51. #define SCANLINES_STRENGTH 0.7
  52.  
  53. // Control the beam width range by changing these two params below. Use always values between 0.0 and 1.0.
  54. #define BEAM_MIN_WIDTH 0.1
  55. #define BEAM_MAX_WIDTH 0.7
  56.  
  57. // You can saturate all colors using this parameter.
  58. #define COLOR_BOOST 1.2
  59.  
  60. // This param change the threshold from where the beam gets thicker.
  61. #define BEAM_WIDTH_SENSITIVITY 0.5
  62.  
  63. // Cool tint. Controls the blue level. CRT TVs from the 90's had a blue tint.
  64. // To turn OFF this effect, use 1.0 value.
  65. #define CRT_TV_BLUE_TINT 1.05
  66.  
  67. // Constants used with gamma correction.
  68. #define InputGamma 2.4
  69. #define OutputGamma 2.2
  70.  
  71. #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
  72. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  73.  
  74.  
  75. const static float4x4 invX = float4x4( -0.5, 1.0, -0.5, 0.0,
  76. 1.5, -2.5, 0.0, 1.0,
  77. -1.5, 2.0, 0.5, 0.0,
  78. 0.5, -0.5, 0.0, 0.0);
  79.  
  80.  
  81. struct input
  82. {
  83. float2 video_size;
  84. float2 texture_size;
  85. float2 output_size;
  86. float frame_count;
  87. float frame_direction;
  88. float frame_rotation;
  89. };
  90.  
  91.  
  92. struct out_vertex {
  93. float2 texCoord;
  94. };
  95.  
  96. /* VERTEX_SHADER */
  97. out_vertex main_vertex
  98. (
  99. float4 position : POSITION,
  100. out float4 oPosition : POSITION,
  101. float2 texCoord : TEXCOORD0,
  102.  
  103. uniform float4x4 modelViewProj,
  104. uniform input IN
  105. )
  106. {
  107. #ifdef SHARPER
  108. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  109. #else
  110. float2 TextureSize = IN.texture_size;
  111. #endif
  112. float2 ps = 1.0/TextureSize;
  113.  
  114. oPosition = mul(modelViewProj, position);
  115.  
  116. out_vertex OUT = {
  117. texCoord + ps*float2(-0.49, 0.0)
  118. };
  119.  
  120. return OUT;
  121. }
  122.  
  123.  
  124. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  125. {
  126. #ifdef SHARPER
  127. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  128. #else
  129. float2 TextureSize = IN.texture_size;
  130. #endif
  131.  
  132. float2 dx = float2(1.0/TextureSize.x, 0.0);
  133. float2 dy = float2(0.0, 1.0/TextureSize.y);
  134.  
  135. float2 tc = (floor(VAR.texCoord*TextureSize)+float2(0.5,0.5))/TextureSize;
  136.  
  137. float2 fp = frac(VAR.texCoord*TextureSize);
  138.  
  139. float3 c10 = tex2D(s_p, tc - dx).xyz;
  140. float3 c11 = tex2D(s_p, tc ).xyz;
  141. float3 c12 = tex2D(s_p, tc + dx).xyz;
  142. float3 c13 = tex2D(s_p, tc + 2.0*dx).xyz;
  143.  
  144. float4x3 color_matrix = float4x3(c10, c11, c12, c13);
  145.  
  146. float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  147. float3 color = mul(invX_Px, color_matrix);
  148.  
  149. #ifdef DILATION
  150. float dz = DILATION_STRENGTH*IN.video_size.x/(IN.output_size.x);
  151.  
  152. float2 fpf = float2(fp.x + dz, fp.y);
  153. float2 fpd = float2(fp.x - dz, fp.y);
  154.  
  155. invX_Px = mul(invX, float4(fpf.x*fpf.x*fpf.x, fpf.x*fpf.x, fpf.x, 1.0));
  156. float3 colorf = mul(invX_Px, color_matrix);
  157.  
  158. invX_Px = mul(invX, float4(fpd.x*fpd.x*fpd.x, fpd.x*fpd.x, fpd.x, 1.0));
  159. float3 colord = mul(invX_Px, color_matrix);
  160.  
  161. color = max(color, max(colorf, colord));
  162. #endif
  163.  
  164. float pos = abs(fp.y - 0.5);
  165.  
  166. float3 lum = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), color);
  167. lum = pow(lum,float3(BEAM_WIDTH_SENSITIVITY));
  168.  
  169. float3 d = clamp(pos/lum, 0.0, 1.0);
  170.  
  171. d = smoothstep(0.0, 1.0, 1.0-d);
  172.  
  173. d = SCANLINES_STRENGTH*(d-1.0)+1.0;
  174.  
  175. #ifdef RED_GREEN_CONTROL
  176. color.rgb *= float3(RED_BOOST, GREEN_BOOST, CRT_TV_BLUE_TINT);
  177. #else
  178. color.b *= CRT_TV_BLUE_TINT;
  179. #endif
  180.  
  181. color = clamp(color*d, 0.0, 1.0);
  182.  
  183. #ifdef PHOSPHOR
  184. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  185.  
  186. float3 dotMaskWeights = lerp(
  187. float3(1.0, 0.7, 1.0),
  188. float3(0.7, 1.0, 0.7),
  189. floor(fmod(mod_factor, 2.0))
  190. );
  191. #endif
  192.  
  193. color = GAMMA_IN(color);
  194.  
  195. #ifdef PHOSPHOR
  196. color.rgb *= dotMaskWeights;
  197. #endif
  198.  
  199. color *= COLOR_BOOST;
  200. color = GAMMA_OUT(color);
  201.  
  202. return float4(color, 1.0);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement