Advertisement
Guest User

crt-hyllian - variable beam width test - v8

a guest
Apr 16th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 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 - sergiogdb@gmail.com
  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. const static float pi = 3.1415926535897932384626433832795;
  28.  
  29. const static float3x3 yuv = float3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  30.  
  31. // Uncomment to increase the sharpness of the scanlines
  32. //#define SHARPER
  33.  
  34. // Comment next line if you don't desire the phosphor effect.
  35. #define PHOSPHOR
  36.  
  37. // Control scanlines transparency by changing this param below. Use always values between 0.0 and 1.0.
  38. #define SCANLINES_STRENGTH 1.0
  39.  
  40. // Control the beam width range by changing these two params below. Use always values between 0.0 and 1.0.
  41. #define BEAM_MIN_WIDTH 0.1
  42. #define BEAM_MAX_WIDTH 0.7
  43.  
  44. // You can saturate colors using this parameter.
  45. #define COLOR_BOOST 2.0
  46.  
  47. // This param change the threshold from where the beam gets thicker.
  48. #define BEAM_WIDTH_SENSITIVITY 0.5
  49.  
  50. // Cool tint. Controls the blue level. CRT TVs from the 90's had a blue tint.
  51. // To turn OFF this effect, use 1.0 value.
  52. #define CRT_TV_BLUE_TINT 1.15
  53.  
  54.  
  55.  
  56.  
  57. #define SINC2(X)\
  58. (X<(1E-5)) ? 1.0 : sin(pi*X)*sin(pi*X)/(pi*X*pi*X);
  59.  
  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, float4(InputGamma, InputGamma, InputGamma, InputGamma))
  66. #define GAMMA_OUT(color) pow(color, float4(1.0 / OutputGamma, 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.  
  76. struct input
  77. {
  78. float2 video_size;
  79. float2 texture_size;
  80. float2 output_size;
  81. float frame_count;
  82. float frame_direction;
  83. float frame_rotation;
  84. };
  85.  
  86.  
  87. struct out_vertex {
  88. float2 texCoord;
  89. float4 t1;
  90. float4 t2;
  91. float4 t3;
  92. float4 t4;
  93. float4 t5;
  94. float4 t6;
  95. float4 t7;
  96. float2 t8;
  97. };
  98.  
  99. /* VERTEX_SHADER */
  100. out_vertex main_vertex
  101. (
  102. float4 position : POSITION,
  103. out float4 oPosition : POSITION,
  104. float2 tex1 : TEXCOORD0,
  105.  
  106. uniform float4x4 modelViewProj,
  107. uniform input IN
  108. )
  109. {
  110. #ifdef SHARPER
  111. float2 TextureSize = float2(2.0*IN.texture_size.x, IN.texture_size.y);
  112. #else
  113. float2 TextureSize = IN.texture_size;
  114. #endif
  115. float2 ps = 1.0/TextureSize;
  116. float dx = ps.x;
  117. float dy = ps.y;
  118.  
  119. float2 tex = tex1+float2(0.0000001, 0.0000001)+ps*float2(-0.5, 1.0);
  120. oPosition = mul(modelViewProj, position);
  121.  
  122. out_vertex OUT = {
  123. tex,
  124. float4(tex,tex) + float4( -dx, -dy, 0.0, -dy),
  125. float4(tex,tex) + float4( dx, -dy, 2.0*dx, -dy),
  126. float4(tex,tex) + float4( -dx, 0.0, dx, 0.0),
  127. float4(tex,tex) + float4(2.0*dx, 0.0, -dx, dy),
  128. float4(tex,tex) + float4( 0.0, dy, dx, dy),
  129. float4(tex,tex) + float4(2.0*dx, dy, -dx, 2.0*dy),
  130. float4(tex,tex) + float4( 0.0, 2.0*dy, dx, 2.0*dy),
  131. tex + float2(2.0*dx, 2.0*dy)
  132. };
  133. /*
  134. out_vertex OUT = {
  135. tex,
  136. float4(tex,tex) + float4(-2.0*dx, 0.0, -dx, 0.0),
  137. float4(tex,tex) + float4( dx, 0.0,-2.0*dx, dy),
  138. float4(tex,tex) + float4( -dx, dy, 0.0, dy),
  139. float4(tex,tex) + float4( dx, dy,-2.0*dx, 2.0*dy),
  140. float4(tex,tex) + float4( -dx, 2.0*dy, 0.0, 2.0*dy),
  141. float4(tex,tex) + float4( dx, 2.0*dy,-2.0*dx, 3.0*dy),
  142. float4(tex,tex) + float4( -dx, 3.0*dy, 0.0, 3.0*dy),
  143. tex + float2( dx, 3.0*dy)
  144. };
  145. */
  146. return OUT;
  147. }
  148.  
  149.  
  150. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  151. {
  152. #ifdef SHARPER
  153. float2 TextureSize = float2(2.0*IN.texture_size.x, IN.texture_size.y);
  154. #else
  155. float2 TextureSize = IN.texture_size;
  156. #endif
  157.  
  158.  
  159. float2 fp = frac(VAR.texCoord*TextureSize);
  160.  
  161.  
  162.  
  163. float3 c00 = tex2D(s_p, VAR.t1.xy).xyz;
  164. float3 c01 = tex2D(s_p, VAR.t1.zw).xyz;
  165. float3 c02 = tex2D(s_p, VAR.t2.xy).xyz;
  166. float3 c03 = tex2D(s_p, VAR.t2.zw).xyz;
  167. float3 c10 = tex2D(s_p, VAR.t3.xy).xyz;
  168. float3 c11 = tex2D(s_p, VAR.texCoord).xyz;
  169. float3 c12 = tex2D(s_p, VAR.t3.zw).xyz;
  170. float3 c13 = tex2D(s_p, VAR.t4.xy).xyz;
  171. float3 c20 = tex2D(s_p, VAR.t4.zw).xyz;
  172. float3 c21 = tex2D(s_p, VAR.t5.xy).xyz;
  173. float3 c22 = tex2D(s_p, VAR.t5.zw).xyz;
  174. float3 c23 = tex2D(s_p, VAR.t6.xy).xyz;
  175. float3 c30 = tex2D(s_p, VAR.t6.zw).xyz;
  176. float3 c31 = tex2D(s_p, VAR.t7.xy).xyz;
  177. float3 c32 = tex2D(s_p, VAR.t7.zw).xyz;
  178. float3 c33 = tex2D(s_p, VAR.t8.xy).xyz;
  179.  
  180.  
  181. float4x4 red_matrix = float4x4(c00.x, c01.x, c02.x, c03.x,
  182. c10.x, c11.x, c12.x, c13.x,
  183. c20.x, c21.x, c22.x, c23.x,
  184. c30.x, c31.x, c32.x, c33.x);
  185.  
  186. float4x4 green_matrix = float4x4(c00.y, c01.y, c02.y, c03.y,
  187. c10.y, c11.y, c12.y, c13.y,
  188. c20.y, c21.y, c22.y, c23.y,
  189. c30.y, c31.y, c32.y, c33.y);
  190.  
  191. float4x4 blue_matrix = float4x4(c00.z, c01.z, c02.z, c03.z,
  192. c10.z, c11.z, c12.z, c13.z,
  193. c20.z, c21.z, c22.z, c23.z,
  194. c30.z, c31.z, c32.z, c33.z);
  195.  
  196.  
  197. float4x1 invX_Px = mul(invX, float4x1(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  198.  
  199.  
  200. float red = mul( red_matrix, invX_Px);
  201. float green = mul(green_matrix, invX_Px);
  202. float blue = mul( blue_matrix, invX_Px);
  203.  
  204. float2 pos = abs(fp - float2(0.5));
  205.  
  206. //float3 Y = mul( float3x3(c01, c02, c00), yuv[0] );
  207. //Y = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), Y);
  208. //float2 Y;
  209. //Y.x = max(c01.r, max(c01.g, c01.b));
  210. //Y.y = max(c02.r, max(c02.g, c02.b));
  211. //Y = lerp(float2(BEAM_MIN_WIDTH), float2(BEAM_MAX_WIDTH), Y);
  212. //Y.x = (c01.r + c01.g + c01.b)/3;
  213. //Y.y = (c02.r + c02.g + c02.b)/3;
  214. //Y=pow(Y,BEAM_WIDTH_SENSITIVITY);
  215. //float lum = (Y.x*(1.5-pos.x) + Y.y*(0.5+pos.x) + Y.z*(0.5-pos.x))/(2.5-pos.x);
  216. //float lum = lerp(Y.x,Y.y, 1.0-Y.x);
  217.  
  218. //float d = clamp(pos.y/lum, 0.0, 1.0);
  219. c01 = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), c01);
  220. c02 = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), c02);
  221. c01=pow(c01,BEAM_WIDTH_SENSITIVITY);
  222. c02=pow(c02,BEAM_WIDTH_SENSITIVITY);
  223. float3 lum = lerp(c01,c02, 1.0-c01);
  224. float3 d = clamp(pos.y/lum, 0.0, 1.0);
  225.  
  226. d = smoothstep(0.0, 1.0, 1.0-d);
  227.  
  228. d = SCANLINES_STRENGTH*(d-1.0)+1.0;
  229.  
  230. float3 color = float3(red, green, blue);
  231. //float3 color = c11.rgb;
  232.  
  233.  
  234. color.b *= CRT_TV_BLUE_TINT;
  235.  
  236. color = clamp(color*d, 0.0, 1.0);
  237.  
  238. #ifdef PHOSPHOR
  239. float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
  240.  
  241. float3 dotMaskWeights = lerp(
  242. float3(1.0, 0.7, 1.0),
  243. float3(0.7, 1.0, 0.7),
  244. floor(fmod(mod_factor, 2.0))
  245. );
  246. #endif
  247.  
  248.  
  249.  
  250. color = GAMMA_IN(color);
  251. #ifdef PHOSPHOR
  252. color.rgb *= dotMaskWeights;
  253. #endif
  254. color = color*COLOR_BOOST;
  255. color = GAMMA_OUT(color);
  256.  
  257. return float4(color, 1.0);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement