KP3Dii/Data/resources/shaders/scanlines_f.glsl
2024-07-22 02:48:42 -05:00

179 lines
4.9 KiB
GLSL

#version 330 core
in vec4 v_color;
in vec2 v_texcoord;
out vec4 v_output;
uniform sampler2D u_texture;
uniform vec2 u_tex_size;
uniform float u_ticks;
uniform int NTSC_PALETTE[98304];
#define iTime u_ticks
#define iChannel0 u_texture
vec3 rgb2yiq(vec3 c){
return vec3(
(0.2989*c.x + 0.5959*c.y + 0.2115*c.z),
(0.5870*c.x - 0.2744*c.y - 0.5229*c.z),
(0.1140*c.x - 0.3216*c.y + 0.3114*c.z)
);
}
vec3 yiq2rgb(vec3 c){
return vec3(
( 1.0*c.x + 1.0*c.y + 1.0*c.z),
( 0.956*c.x - 0.2720*c.y - 1.1060*c.z),
(0.6210*c.x - 0.6474*c.y + 1.7046*c.z)
);
}
vec2 Circle(float Start, float Points, float Point)
{
float Rad = (3.141592 * 2.0 * (1.0 / Points)) * (Point + Start);
//return vec2(sin(Rad), cos(Rad));
return vec2(-(.3+Rad), cos(Rad));
}
vec3 Blur(vec2 uv, float f, float d){
// d=abs(d);
float t = (sin(iTime*5.0+uv.y*5.0))/10.0;
float b = 1.0;
//t=sin(iTime*5.0+f)/10.0;
t=0.0;
vec2 PixelOffset=vec2(d+.0005*t,0);
float Start = 2.0 / 14.0;
vec2 Scale = 0.66 * 4.0 * 2.0 * PixelOffset.xy;
vec3 N0 = texture(iChannel0, uv + Circle(Start, 14.0, 0.0) * Scale).rgb;
vec3 N1 = texture(iChannel0, uv + Circle(Start, 14.0, 1.0) * Scale).rgb;
vec3 N2 = texture(iChannel0, uv + Circle(Start, 14.0, 2.0) * Scale).rgb;
vec3 N3 = texture(iChannel0, uv + Circle(Start, 14.0, 3.0) * Scale).rgb;
vec3 N4 = texture(iChannel0, uv + Circle(Start, 14.0, 4.0) * Scale).rgb;
vec3 N5 = texture(iChannel0, uv + Circle(Start, 14.0, 5.0) * Scale).rgb;
vec3 N6 = texture(iChannel0, uv + Circle(Start, 14.0, 6.0) * Scale).rgb;
vec3 N7 = texture(iChannel0, uv + Circle(Start, 14.0, 7.0) * Scale).rgb;
vec3 N8 = texture(iChannel0, uv + Circle(Start, 14.0, 8.0) * Scale).rgb;
vec3 N9 = texture(iChannel0, uv + Circle(Start, 14.0, 9.0) * Scale).rgb;
vec3 N10 = texture(iChannel0, uv + Circle(Start, 14.0, 10.0) * Scale).rgb;
vec3 N11 = texture(iChannel0, uv + Circle(Start, 14.0, 11.0) * Scale).rgb;
vec3 N12 = texture(iChannel0, uv + Circle(Start, 14.0, 12.0) * Scale).rgb;
vec3 N13 = texture(iChannel0, uv + Circle(Start, 14.0, 13.0) * Scale).rgb;
vec3 N14 = texture(iChannel0, uv).rgb;
vec4 clr = texture(iChannel0, uv);
float W = 1.0 / 15.0;
clr.rgb=
(N0 * W) +
(N1 * W) +
(N2 * W) +
(N3 * W) +
(N4 * W) +
(N5 * W) +
(N6 * W) +
(N7 * W) +
(N8 * W) +
(N9 * W) +
(N10 * W) +
(N11 * W) +
(N12 * W) +
(N13 * W) +
(N14 * W);
return vec3(clr.xyz)*b;
}
float rand(float n){return fract(sin(n) * 43758.5453123);}
float noise(float p){
float fl = floor(p);
float fc = fract(p);
return mix(rand(fl), rand(fl + 1.0), fc);
}
// All components are in the range [0…1], including hue.
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// All components are in the range [0…1], including hue.
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main()
{
vec2 size = u_tex_size;
vec4 tex = texture(u_texture, v_texcoord);
// NTSC
#define fragCoord gl_FragCoord
vec4 fragColor = vec4(1.0, 1.0, 1.0, 1.0);
float d = .1;
vec2 uv = fragCoord.xy / size.xy;
float s = 0.05; // Warping/blur
float e = min(.30,pow(max(0.0,cos(uv.y*4.0+.3)-.75)*(s+0.5)*1.0,3.0))*25.0;
s-=pow(texture(u_texture,vec2(0.01+(uv.y*32.0)/32.0,1.0)).r,1.0);
uv.x+=e*abs(s*3.0);
float r = 0.0f; // Rolling distortion looking thing
uv.x+=abs(r*pow(min(.003,(uv.y-.15))*6.0,2.0));
d=.051+abs(sin(s/4.0));
float c = max(0.0001,.002*d);
vec2 uvo = uv;
fragColor.xyz =Blur(uv,0.0,c+c*(uv.x));
float y = rgb2yiq(fragColor.xyz).r;
uv.x+=.01*d;
c*=6.0;
fragColor.xyz =Blur(uv,.333,c);
float i = rgb2yiq(fragColor.xyz).g;
uv.x+=.005*d;
c*=2.50;
fragColor.xyz =Blur(uv,.666,c);
float q = rgb2yiq(fragColor.xyz).b;
fragColor.xyz=yiq2rgb(vec3(y,i,q))-pow(s+e*2.0,3.0);
fragColor.xyz*=smoothstep(1.0,.999,uv.x-.1);
// Scanlines
float lines = 0.0;
float scale = 200.0;
float angle = (gl_FragCoord.y * scale);
float offset = 1.0;
vec4 coloro = tex * v_color;
float amp = 0.7;
lines = sin(angle + u_ticks) + offset;
lines *= 1.0 - amp;
lines += amp;
coloro *= lines;
coloro = fragColor * lines;
// Desaturate
vec3 hsv = rgb2hsv(coloro.rgb);
hsv.y *= 0.75;
coloro.rgb = hsv2rgb(hsv);
// Debug: standard output
// v_output = texture(u_texture, v_texcoord);
// Apply final output
v_output = coloro;
}