Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
- //
- // by Timothy Lottes
- //
- // This is more along the style of a really good CGA arcade monitor.
- // With RGB inputs instead of NTSC.
- // The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
- //
- // Left it unoptimized to show the theory behind the algorithm.
- //
- // It is an example what I personally would want as a display option for pixel art games.
- // Please take and use, change, or whatever.
- //
- // -- config -- //
- #pragma parameter hardScan "hardScan" -8.0 -20.0 0.0 1.0 // default, minimum, maximum, optional step
- #pragma parameter hardPix "hardPix" -3.0 -20.0 0.0 1.0
- #pragma parameter warpX "warpX" 0.031 0.0 0.125 0.01
- #pragma parameter warpY "warpY" 0.041 0.0 0.125 0.01
- #pragma parameter maskDark "maskDark" 0.5 0.0 2.0 0.1
- #pragma parameter maskLight "maskLight" 1.5 0.0 2.0 0.1
- #pragma parameter scaleInLinearGamma "scaleInLinearGamma" 1.0 0.0 1.0 1.0
- #pragma parameter shadowMask "shadowMask" 1.0 0.0 1.0 1.0
- #pragma parameter brightboost "brightness" 1.0 0.0 2.0 0.05
- #pragma parameter hardBloomPix "bloom-x soft" -1.5 -2.0 -0.5 0.1
- #pragma parameter hardBloomScan "bloom-y soft" -2.0 -4.0 -1.0 0.1
- #pragma parameter bloomAmount "bloom amt" 0.15 0.0 1.0 0.05
- #ifdef PARAMETER_UNIFORM // If the shader implementation understands #pragma parameters, this is defined.
- uniform float hardScan;
- uniform float hardPix;
- uniform float warpX;
- uniform float warpY;
- uniform float maskDark;
- uniform float maskLight;
- uniform float scaleInLinearGamma;
- uniform float shadowMask;
- uniform float brightboost;
- uniform float hardBloomScan;
- uniform float hardBloomPix;
- uniform float bloomAmount;
- #else
- #define hardScan -8.0
- #define hardPix -3.0
- #define warpX 0.031
- #define warpY 0.041
- #define maskDark 0.5
- #define maskLight 1.5
- #define scaleInLinearGamma 1
- #define shadowMask 1
- #define brightboost 1
- #define hardBloomScan -2.0
- #define hardBloomPix -1.5
- #define bloomAmount 1.0/16.0
- #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;
- };
- input IN_global;
- sampler2D s0_global;
- #define warp float2(warpX,warpY)
- //------------------------------------------------------------------------
- // sRGB to Linear.
- // Assuing using sRGB typed textures this should not be needed.
- float ToLinear1(float c)
- {
- if (scaleInLinearGamma==0) return c;
- return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);
- }
- float3 ToLinear(float3 c)
- {
- if (scaleInLinearGamma==0) return c;
- return float3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));
- }
- // Linear to sRGB.
- // Assuing using sRGB typed textures this should not be needed.
- float ToSrgb1(float c)
- {
- if (scaleInLinearGamma==0) return c;
- return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);
- }
- float3 ToSrgb(float3 c)
- {
- if (scaleInLinearGamma==0) return c;
- return float3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));
- }
- // Nearest emulated sample given floating point position and texel offset.
- // Also zero's off screen.
- float3 Fetch(float2 pos,float2 off){
- pos=(floor(pos*IN_global.texture_size.xy+off)+float2(0.5,0.5))/IN_global.texture_size.xy;
- return ToLinear(brightboost * tex2D(s0_global,pos.xy).rgb);}
- // Distance in emulated pixels to nearest texel.
- float2 Dist(float2 pos){pos=pos*IN_global.texture_size.xy;return -((pos-floor(pos))-float2(0.5));}
- // 1D Gaussian.
- float Gaus(float pos,float scale){return exp2(scale*pos*pos);}
- // 3-tap Gaussian filter along horz line.
- float3 Horz3(float2 pos,float off){
- float3 b=Fetch(pos,float2(-1.0,off));
- float3 c=Fetch(pos,float2( 0.0,off));
- float3 d=Fetch(pos,float2( 1.0,off));
- float dst=Dist(pos).x;
- // Convert distance to weight.
- 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 filtered sample.
- return (b*wb+c*wc+d*wd)/(wb+wc+wd);}
- // 5-tap Gaussian filter along horz line.
- float3 Horz5(float2 pos,float off){
- float3 a=Fetch(pos,float2(-2.0,off));
- float3 b=Fetch(pos,float2(-1.0,off));
- float3 c=Fetch(pos,float2( 0.0,off));
- float3 d=Fetch(pos,float2( 1.0,off));
- float3 e=Fetch(pos,float2( 2.0,off));
- float dst=Dist(pos).x;
- // Convert distance to weight.
- 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 filtered sample.
- return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);}
- // 7-tap Gaussian filter along horz line.
- float3 Horz7(float2 pos,float off){
- float3 a=Fetch(pos,float2(-3.0,off));
- float3 b=Fetch(pos,float2(-2.0,off));
- float3 c=Fetch(pos,float2(-1.0,off));
- float3 d=Fetch(pos,float2( 0.0,off));
- float3 e=Fetch(pos,float2( 1.0,off));
- float3 f=Fetch(pos,float2( 2.0,off));
- float3 g=Fetch(pos,float2( 3.0,off));
- float dst=Dist(pos).x;
- // Convert distance to weight.
- float scale=hardBloomPix;
- float wa=Gaus(dst-3.0,scale);
- float wb=Gaus(dst-2.0,scale);
- float wc=Gaus(dst-1.0,scale);
- float wd=Gaus(dst+0.0,scale);
- float we=Gaus(dst+1.0,scale);
- float wf=Gaus(dst+2.0,scale);
- float wg=Gaus(dst+3.0,scale);
- // Return filtered sample.
- return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);}
- // Return scanline weight.
- float Scan(float2 pos,float off){
- float dst=Dist(pos).y;
- return Gaus(dst+off,hardScan);}
- // Return scanline weight for bloom.
- float BloomScan(float2 pos,float off){
- float dst=Dist(pos).y;
- return Gaus(dst+off,hardBloomScan);}
- // Allow nearest three lines to effect pixel.
- float3 Tri(float2 pos){
- float3 a=Horz3(pos,-1.0);
- float3 b=Horz5(pos, 0.0);
- float3 c=Horz3(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;}
- // Small bloom.
- float3 Bloom(float2 pos){
- float3 a=Horz5(pos,-2.0);
- float3 b=Horz7(pos,-1.0);
- float3 c=Horz7(pos, 0.0);
- float3 d=Horz7(pos, 1.0);
- float3 e=Horz5(pos, 2.0);
- float wa=BloomScan(pos,-2.0);
- float wb=BloomScan(pos,-1.0);
- float wc=BloomScan(pos, 0.0);
- float wd=BloomScan(pos, 1.0);
- float we=BloomScan(pos, 2.0);
- return a*wa+b*wb+c*wc+d*wd+e*we;}
- // Distortion of scanlines, and end of screen alpha.
- float2 Warp(float2 pos){
- pos=pos*2.0-1.0;
- pos*=float2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
- return pos*0.5+0.5;}
- // Very compressed TV style shadow mask.
- float3 Mask1(float2 pos){
- float line=maskLight;
- float odd=0.0;
- if(fract(pos.x/6.0)<0.5)odd=1.0;
- if(fract((pos.y+odd)/2.0)<0.5)line=maskDark;
- pos.x=fract(pos.x/3.0);
- float3 mask1=float3(maskDark,maskDark,maskDark);
- if(pos.x<0.333)mask1.r=maskLight;
- else if(pos.x<0.666)mask1.g=maskLight;
- else mask1.b=maskLight;
- mask1*=line;
- return mask1;}
- // Aperture-grille.
- float3 Mask2(float2 pos){
- pos.x=fract(pos.x/3.0);
- float3 mask2=float3(maskDark,maskDark,maskDark);
- if(pos.x<0.333)mask2.r=maskLight;
- else if(pos.x<0.666)mask2.g=maskLight;
- else mask2.b=maskLight;
- return mask2;}
- // Stretched VGA style shadow mask (same as prior shaders).
- float3 Mask3(float2 pos){
- pos.x+=pos.y*3.0;
- float3 mask3=float3(maskDark,maskDark,maskDark);
- pos.x=fract(pos.x/6.0);
- if(pos.x<0.333)mask3.r=maskLight;
- else if(pos.x<0.666)mask3.g=maskLight;
- else mask3.b=maskLight;
- return mask3;}
- // VGA style shadow mask.
- float3 Mask4(float2 pos){
- pos.xy=floor(pos.xy*float2(1.0,0.5));
- pos.x+=pos.y*3.0;
- float3 mask4=float3(maskDark,maskDark,maskDark);
- pos.x=fract(pos.x/6.0);
- if(pos.x<0.333)mask4.r=maskLight;
- else if(pos.x<0.666)mask4.g=maskLight;
- else mask4.b=maskLight;
- return mask4;}
- float4 main_fragment (float2 tex : TEXCOORD, uniform sampler2D s0 : TEXUNIT0, uniform input IN) : COLOR
- {
- float2 pos=Warp(tex.xy*(IN.texture_size.xy/IN.video_size.xy))*(IN.video_size.xy/IN.texture_size.xy);
- s0_global = s0;
- IN_global = IN;
- float3 outColor = Tri(pos);
- //Add Bloom
- outColor.rgb+=Bloom(pos)*bloomAmount;
- if(shadowMask)
- outColor.rgb*=Mask1(floor(tex.xy*(IN.texture_size.xy/IN.video_size.xy)*IN.output_size.xy)+float2(0.5,0.5));
- return float4(ToSrgb(outColor.rgb),1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement