Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CRT Shader by EasyMode
- License: GPL
- */
- #pragma parameter SHARPNESS_H "Sharpness Horizontal" 0.5 0.0 1.0 0.05
- #pragma parameter SHARPNESS_V "Sharpness Vertical" 1.0 0.0 1.0 0.05
- #pragma parameter MASK_STRENGTH "Mask Strength" 0.3 0.0 1.0 0.01
- #pragma parameter MASK_SUBPIXEL_WIDTH "Mask Subpixel Width" 1.0 1.0 100.0 1.0
- #pragma parameter MASK_SUBPIXEL_HEIGHT "Mask Subpixel Height" 1.0 1.0 100.0 1.0
- #pragma parameter MASK_STAGGER "Mask Stagger" 0.0 0.0 100.0 1.0
- #pragma parameter MASK_SIZE "Mask Size" 1.0 1.0 4.0 1.0
- #pragma parameter SCANLINE_STRENGTH "Scanline Strength" 1.0 0.0 1.0 0.05
- #pragma parameter SCANLINE_BEAM_WIDTH "Scanline Beam Width" 1.5 1.0 5.0 0.5
- #pragma parameter SCANLINE_BRIGHT_MIN "Scanline Brightness Min." 0.35 0.0 1.0 0.05
- #pragma parameter SCANLINE_BRIGHT_MAX "Scanline Brightness Max." 0.65 0.0 1.0 0.05
- #pragma parameter GAMMA_INPUT "Gamma Input" 2.0 1.0 3.0 0.1
- #pragma parameter GAMMA_OUTPUT "Gamma Output" 1.8 1.0 3.0 0.1
- #pragma parameter BRIGHT_BOOST "Brightness Boost" 1.2 1.0 2.0 0.01
- #ifdef PARAMETER_UNIFORM
- uniform float BRIGHT_BOOST;
- uniform float GAMMA_INPUT;
- uniform float GAMMA_OUTPUT;
- uniform float MASK_SIZE;
- uniform float MASK_STAGGER;
- uniform float MASK_STRENGTH;
- uniform float MASK_SUBPIXEL_HEIGHT;
- uniform float MASK_SUBPIXEL_WIDTH;
- uniform float SCANLINE_BEAM_WIDTH;
- uniform float SCANLINE_BRIGHT_MAX;
- uniform float SCANLINE_BRIGHT_MIN;
- uniform float SCANLINE_STRENGTH;
- uniform float SHARPNESS_H;
- uniform float SHARPNESS_V;
- #else
- #define BRIGHT_BOOST 1.2
- #define GAMMA_INPUT 2.0
- #define GAMMA_OUTPUT 1.8
- #define MASK_SIZE 1.0
- #define MASK_STAGGER 0.0
- #define MASK_STRENGTH 0.3
- #define MASK_SUBPIXEL_HEIGHT 1.0
- #define MASK_SUBPIXEL_WIDTH 1.0
- #define SCANLINE_BEAM_WIDTH 1.5
- #define SCANLINE_BRIGHT_MAX 0.65
- #define SCANLINE_BRIGHT_MIN 0.35
- #define SCANLINE_STRENGTH 1.0
- #define SHARPNESS_H 0.5
- #define SHARPNESS_V 1.0
- #endif
- #define PI 3.141592653589
- #define LINEAR_PROCESSING
- #ifdef LINEAR_PROCESSING
- #define TEX2D(c) pow(tex2D(tex, c).rgb, float3(GAMMA_INPUT))
- #else
- #define TEX2D(c) tex2D(tex, c).rgb
- #endif
- void main_vertex
- (
- float4 position : POSITION,
- out float4 oPosition : POSITION,
- uniform float4x4 modelViewProj,
- float2 tex : TEXCOORD,
- out float2 oTex : TEXCOORD
- )
- {
- oPosition = mul(modelViewProj, position);
- oTex = tex;
- }
- struct input
- {
- float2 video_size;
- float2 texture_size;
- float2 output_size;
- float frame_count;
- float frame_direction;
- float frame_rotation;
- };
- float curve_distance(float x, float sharp)
- {
- /*
- apply half-circle s-curve to distance for sharper (more pixelated) interpolation
- single line formula for Graph Toy:
- 0.5 - sqrt(0.25 - (x - step(0.5, x)) * (x - step(0.5, x))) * sign(0.5 - x)
- */
- float x_step = step(0.5, x);
- float curve = 0.5 - sqrt(0.25 - (x - x_step) * (x - x_step)) * sign(0.5 - x);
- return lerp(x, curve, sharp);
- }
- float4 main_fragment(uniform sampler2D tex : TEXUNIT0, float2 coords : TEXCOORD0, uniform input IN) : COLOR
- {
- float2 one = 1.0 / IN.texture_size;
- float2 one_half = one / 2.0;
- float2 c1 = floor((coords - one_half) / one) * one + one_half;
- float2 c2 = float2(c1.x + one.x, c1.y);
- float2 c3 = float2(c1.x, c1.y + one.y);
- float2 c4 = float2(c2.x, c3.y);
- float2 dist = (coords - c1) / (c4 - c1);
- float x_curve = curve_distance(dist.x, SHARPNESS_H);
- float3 col = lerp(TEX2D(c1), TEX2D(c2), x_curve);
- float3 col2 = lerp(TEX2D(c3), TEX2D(c4), x_curve);
- col = lerp(col, col2, curve_distance(dist.y, SHARPNESS_V));
- #ifndef LINEAR_PROCESSING
- col = pow(col, float3(GAMMA_INPUT));
- #endif
- float luma = dot(float3(0.2126, 0.7152, 0.0722), col);
- float bright = (max(col.r, max(col.g, col.b)) + luma) / 2.0;
- float scan_bright = clamp(bright, SCANLINE_BRIGHT_MIN, SCANLINE_BRIGHT_MAX);
- float scan_weight = 1.0 - pow(cos(coords.y * 2.0 * PI * IN.texture_size.y) * 0.5 + 0.5, SCANLINE_BEAM_WIDTH) * SCANLINE_STRENGTH;
- float mask = 1.0 - (MASK_STRENGTH);
- float2 mod_fac = floor(coords * IN.output_size * IN.texture_size / (IN.video_size * float2(MASK_SIZE, MASK_SUBPIXEL_HEIGHT * MASK_SIZE)));
- int subpixel = int(mod((mod_fac.x + mod(mod_fac.y, 2.0) * MASK_STAGGER) / MASK_SUBPIXEL_WIDTH, 3.0));
- float3 mask_weight;
- if (subpixel == 0) mask_weight = float3(1.0, mask, mask);
- else if (subpixel == 1) mask_weight = float3(mask, 1.0, mask);
- else mask_weight = float3(mask, mask, 1.0);
- if (IN.output_size.y / IN.video_size.y < 3.0) { scan_weight = 1.0; }
- col2 = col.rgb;
- col *= float3(scan_weight);
- col = lerp(col, col2, scan_bright);
- col *= mask_weight;
- col = pow(col, float3(1.0 / GAMMA_OUTPUT));
- return float4(col * BRIGHT_BOOST, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement