This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanlines2.glsl
92 lines (80 loc) · 2.72 KB
/
scanlines2.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Adapted from https://www.shadertoy.com/view/XsjSzR
#define res (iResolution.xy/6.0)
float hardScan=-8.0;
float hardPix=-3.0;
float maskDark=0.5;
float maskLight=1.5;
vec2 warp=vec2(1.0/32.0,1.0/24.0);
ivec2 iResolution = ivec2(576, 384);
float ToLinear1(float c){return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);}
vec3 ToLinear(vec3 c){return vec3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));}
float ToSrgb1(float c){return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);}
vec3 ToSrgb(vec3 c){return vec3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));}
vec2 Dist(vec2 pos){pos=pos*res;return -((pos-floor(pos))-vec2(0.5));}
float Gaus(float pos,float scale){return exp2(scale*pos*pos);}
vec3 Fetch(Image texture, vec2 pos,vec2 off) {
pos=floor(pos*res+off)/res;
if(max(abs(pos.x-0.5),abs(pos.y-0.5))>0.5)return vec3(0.0,0.0,0.0);
return ToLinear(Texel(texture, pos.xy,-16.0).rgb);
}
vec3 Horz3(Image texture,vec2 pos,float off){
vec3 b=Fetch(texture,pos,vec2(-1.0,off));
vec3 c=Fetch(texture,pos,vec2( 0.0,off));
vec3 d=Fetch(texture,pos,vec2( 1.0,off));
float dst=Dist(pos).x;
float scale=hardPix;
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
return (b*wb+c*wc+d*wd)/(wb+wc+wd);
}
vec3 Horz5(Image texture,vec2 pos,float off){
vec3 a=Fetch(texture,pos,vec2(-2.0,off));
vec3 b=Fetch(texture,pos,vec2(-1.0,off));
vec3 c=Fetch(texture,pos,vec2( 0.0,off));
vec3 d=Fetch(texture,pos,vec2( 1.0,off));
vec3 e=Fetch(texture,pos,vec2( 2.0,off));
float dst=Dist(pos).x;
float scale=hardPix;
float wa=Gaus(dst-2.0,scale);
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
float we=Gaus(dst+2.0,scale);
return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);
}
float Scan(vec2 pos,float off){
float dst=Dist(pos).y;
return Gaus(dst+off,hardScan);
}
vec3 Tri(Image texture, vec2 pos){
vec3 a=Horz3(texture, pos,-1.0);
vec3 b=Horz5(texture, pos, 0.0);
vec3 c=Horz3(texture, pos, 1.0);
float wa=Scan(pos,-1.0);
float wb=Scan(pos, 0.0);
float wc=Scan(pos, 1.0);
return a*wa+b*wb+c*wc;
}
vec3 Mask(vec2 pos) {
pos.x+=pos.y*3.0;
vec3 mask=vec3(maskDark,maskDark,maskDark);
pos.x=fract(pos.x/6.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
return mask;
}
vec2 Warp(vec2 pos){
pos=pos*2.0-1.0;
pos*=vec2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
return pos*0.5+0.5;
}
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
vec2 pos=Warp(screen_coords.xy/iResolution.xy);
vec3 pixel = Texel(texture, pos).rgb;
pixel = Tri(texture, pos)*Mask(screen_coords.xy);
pixel.rgb=ToSrgb(pixel.rgb);
return vec4(pixel, 1.0);
}