Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* COMPATIBILITY
- - HLSL compilers
- - Cg compilers
- */
- /*
- Hyllian's CRT Shader
- Copyright (C) 2011-2014 Hyllian/Jararaca - sergiogdb@gmail.com
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- // Uncomment to increase the sharpness of the scanlines.
- //#define SHARPER
- // Control the level of sharpness when SHARPER is enabled.
- #define SHARPNESS 2.0
- // Comment next line if you don't desire the phosphor effect.
- #define PHOSPHOR
- // Uncomment to enable adjustment of red and green saturation.
- //#define RED_GREEN_CONTROL
- // Control red and green saturation when RED_GREEN_CONTROL is enabled.
- #define RED_BOOST 1.0
- #define GREEN_BOOST 1.0
- // Control scanlines transparency by changing this param below. Use always values between 0.0 and 1.0.
- #define SCANLINES_STRENGTH 0.7
- // Control the beam width range by changing these two params below. Use always values between 0.0 and 1.0.
- #define BEAM_MIN_WIDTH 0.1
- #define BEAM_MAX_WIDTH 1.0
- // You can saturate all colors using this parameter.
- #define COLOR_BOOST 1.2
- // This param change the threshold from where the beam gets thicker.
- #define BEAM_WIDTH_SENSITIVITY 0.5
- // Cool tint. Controls the blue level. CRT TVs from the 90's had a blue tint.
- // To turn OFF this effect, use 1.0 value.
- #define CRT_TV_BLUE_TINT 1.05
- // Constants used with gamma correction.
- #define InputGamma 2.4
- #define OutputGamma 2.2
- #define GAMMA_IN(color) pow(color, float3(InputGamma, InputGamma, InputGamma))
- #define GAMMA_OUT(color) pow(color, float3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
- const static float4x4 invX = float4x4( -0.5, 1.0, -0.5, 0.0,
- 1.5, -2.5, 0.0, 1.0,
- -1.5, 2.0, 0.5, 0.0,
- 0.5, -0.5, 0.0, 0.0);
- struct input
- {
- float2 video_size;
- float2 texture_size;
- float2 output_size;
- float frame_count;
- float frame_direction;
- float frame_rotation;
- };
- struct out_vertex {
- float2 texCoord;
- };
- /* VERTEX_SHADER */
- out_vertex main_vertex
- (
- float4 position : POSITION,
- out float4 oPosition : POSITION,
- float2 texCoord : TEXCOORD0,
- uniform float4x4 modelViewProj,
- uniform input IN
- )
- {
- #ifdef SHARPER
- float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
- #else
- float2 TextureSize = IN.texture_size;
- #endif
- float2 ps = 1.0/TextureSize;
- oPosition = mul(modelViewProj, position);
- out_vertex OUT = {
- texCoord + ps*float2(-0.49, 0.0)
- };
- return OUT;
- }
- float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
- {
- #ifdef SHARPER
- float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);
- #else
- float2 TextureSize = IN.texture_size;
- #endif
- float2 dx = float2(1.0/TextureSize.x, 0.0);
- float2 dy = float2(0.0, 1.0/TextureSize.y);
- float2 tc = (floor(VAR.texCoord*TextureSize)+float2(0.5,0.5))/TextureSize;
- float2 fp = frac(VAR.texCoord*TextureSize);
- float3 c10 = tex2D(s_p, tc - 1.0*dx).xyz;
- float3 c11 = tex2D(s_p, tc ).xyz;
- float3 c12 = tex2D(s_p, tc + dx).xyz;
- float3 c13 = tex2D(s_p, tc + 2.0*dx).xyz;
- float4x3 color_matrix = float4x3(c10, c11, c12, c13);
- float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
- float3 color = mul(invX_Px, color_matrix);
- float pos = abs(fp.y - 0.5);
- float3 lum = lerp(float3(BEAM_MIN_WIDTH), float3(BEAM_MAX_WIDTH), color);
- lum = pow(lum,float3(BEAM_WIDTH_SENSITIVITY));
- float3 d = clamp(pos/lum, 0.0, 1.0);
- d = smoothstep(0.0, 1.0, 1.0-d);
- d = SCANLINES_STRENGTH*(d-1.0)+1.0;
- #ifdef RED_GREEN_CONTROL
- color.rgb *= float3(RED_BOOST, GREEN_BOOST, CRT_TV_BLUE_TINT);
- #else
- color.b *= CRT_TV_BLUE_TINT;
- #endif
- color = clamp(color*d, 0.0, 1.0);
- #ifdef PHOSPHOR
- float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
- float3 dotMaskWeights = lerp(
- float3(1.0, 0.7, 1.0),
- float3(0.7, 1.0, 0.7),
- floor(fmod(mod_factor, 2.0))
- );
- #endif
- color = GAMMA_IN(color);
- #ifdef PHOSPHOR
- color.rgb *= dotMaskWeights;
- #endif
- color *= COLOR_BOOST;
- color = GAMMA_OUT(color);
- return float4(color, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement