Advertisement
Guest User

crt-hyllian - variable beam width test - v11

a guest
Apr 21st, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 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 increase the sharpness of the scanlines.
  29. //#define SHARPER
  30.  
  31. // Control the level of sharpness when SHARPER is enabled.
  32. #define SHARPNESS 2.0
  33.  
  34. // Comment next line if you don't desire the phosphor effect.
  35. //#define PHOSPHOR
  36.  
  37. // Uncomment to enable adjustment of red and green saturation.
  38. //#define RED_GREEN_CONTROL
  39.  
  40. // Control red and green saturation when RED_GREEN_CONTROL is enabled.
  41. #define RED_BOOST 1.0
  42. #define GREEN_BOOST 1.0
  43.  
  44. // Control scanlines transparency by changing this param below. Use always values between 0.0 and 1.0.
  45. #define SCANLINES_STRENGTH 0.9
  46.  
  47. // Control the beam width range by changing these two params below. Use always values between 0.0 and 1.0.
  48. #define BEAM_MIN_WIDTH 0.1
  49. #define BEAM_MAX_WIDTH 1.0
  50.  
  51. // You can saturate all colors using this parameter.
  52. #define COLOR_BOOST 1.4
  53.  
  54. // This param change the threshold from where the beam gets thicker.
  55. #define BEAM_WIDTH_SENSITIVITY 0.5
  56.  
  57. // Cool tint. Controls the blue level. CRT TVs from the 90's had a blue tint.
  58. // To turn OFF this effect, use 1.0 value.
  59. #define CRT_TV_BLUE_TINT 1.1
  60.  
  61. // Constants used with gamma correction.
  62. #define InputGamma 2.4
  63. #define OutputGamma 2.2
  64.  
  65. #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
  66. #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
  67.  
  68.  
  69. const static float4x4 invX = float4x4(-1.0/6.0, 0.5, -1.0/3.0, 0.0,
  70. 0.5, -1.0, -0.5, 1.0,
  71. -0.5, 0.5, 1.0, 0.0,
  72. 1.0/6.0, 0.0, -1.0/6.0, 0.0);
  73.  
  74.  
  75. struct input
  76. {
  77. float2 video_size;
  78. float2 texture_size;
  79. float2 output_size;
  80. float frame_count;
  81. float frame_direction;
  82. float frame_rotation;
  83. };
  84.  
  85.  
  86. struct out_vertex {
  87. float2 texCoord;
  88. };
  89.  
  90. /* VERTEX_SHADER */
  91. out_vertex main_vertex
  92. (
  93. float4 position : POSITION,
  94. out float4 oPosition : POSITION,
  95. float2 texCoord : TEXCOORD0,
  96.  
  97. uniform float4x4 modelViewProj,
  98. uniform input IN
  99. )
  100. {
  101. #ifdef SHARPER
  102. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  103. #else
  104. float2 TextureSize = IN.texture_size;
  105. #endif
  106. float2 ps = 1.0/TextureSize;
  107.  
  108. oPosition = mul(modelViewProj, position);
  109.  
  110. out_vertex OUT = {
  111. texCoord + ps*float2(-0.49, 0.0)
  112. };
  113.  
  114. return OUT;
  115. }
  116.  
  117.  
  118. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  119. {
  120. #ifdef SHARPER
  121. float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
  122. #else
  123. float2 TextureSize = IN.texture_size;
  124. #endif
  125.  
  126. float2 dx = float2(1.0/TextureSize.x, 0.0);
  127. float2 dy = float2(0.0, 1.0/TextureSize.y);
  128.  
  129. float2 tc = (floor(VAR.texCoord*TextureSize)+float2(0.5,0.5))/TextureSize;
  130.  
  131. float2 fp = frac(VAR.texCoord*TextureSize);
  132.  
  133. float3 c10 = tex2D(s_p, tc - dx).xyz;
  134. float3 c11 = tex2D(s_p, tc ).xyz;
  135. float3 c12 = tex2D(s_p, tc + dx).xyz;
  136. float3 c13 = tex2D(s_p, tc + 2.0*dx).xyz;
  137.  
  138. float4x3 color_matrix = float4x3(c10, c11, c12, c13);
  139.  
  140. float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  141. float3 color = mul(invX_Px, color_matrix);
  142.  
  143. float2 pos = abs(fp - float2(0.5));
  144.  
  145. // c11 = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), c11);
  146. // c12 = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), c12);
  147. // c11 = pow(c11,float3(BEAM_WIDTH_SENSITIVITY));
  148. // c12 = pow(c12,float3(BEAM_WIDTH_SENSITIVITY));
  149.  
  150. float3 lum = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), color);
  151. lum = pow(lum,float3(BEAM_WIDTH_SENSITIVITY));
  152.  
  153. // float3 lum = lerp(c11,c11*step(fp.x, 0.2499999)+c12*step(0.25, fp.x), 1.0-c11);
  154. // float3 lum = c11*step(fp.x, 0.49)+c12*step(0.51, fp.x);
  155. float3 d = clamp(pos.y/lum, 0.0, 1.0);
  156.  
  157. d = smoothstep(0.0, 1.0, 1.0-d);
  158.  
  159. d = SCANLINES_STRENGTH*(d-1.0)+1.0;
  160.  
  161. #ifdef RED_GREEN_CONTROL
  162. color.rgb *= float3(RED_BOOST, GREEN_BOOST, CRT_TV_BLUE_TINT);
  163. #else
  164. color.b *= CRT_TV_BLUE_TINT;
  165. #endif
  166.  
  167. color = clamp(color*d, 0.0, 1.0);
  168.  
  169. #ifdef PHOSPHOR
  170. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  171.  
  172. float3 dotMaskWeights = lerp(
  173. float3(1.0, 0.7, 1.0),
  174. float3(0.7, 1.0, 0.7),
  175. floor(fmod(mod_factor, 2.0))
  176. );
  177. #endif
  178.  
  179. color = GAMMA_IN(color);
  180.  
  181. #ifdef PHOSPHOR
  182. color.rgb *= dotMaskWeights;
  183. #endif
  184.  
  185. color *= COLOR_BOOST;
  186. color = GAMMA_OUT(color);
  187.  
  188. return float4(color, 1.0);
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement