Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Created February 6, 2014 03:34
Show Gist options
  • Save keiranlovett/8838023 to your computer and use it in GitHub Desktop.
Save keiranlovett/8838023 to your computer and use it in GitHub Desktop.
Vignetting Shader
Shader "Hidden/Vignetting" {
Properties {
_MainTex ("Base", 2D) = "white" {}
_VignetteTex ("Vignette", 2D) = "white" {}
_GlitchTex ("Glitch", 2D) = "white" {}
}
CGINCLUDE
#pragma target 3.0
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
};
sampler2D _MainTex;
sampler2D _VignetteTex;
sampler2D _GlitchTex;
half _Intensity;
half _Blur;
half _GlitchIntensity;
half _GlitchTweak;
half _GlitchRadial;
half _GlitchCRT;
half _ScanDirection;
half _ScanIntensity;
float4 _MainTex_TexelSize;
v2f vert( appdata_img v ) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
o.uv2 = v.texcoord.xy;
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
o.uv2.y = 1.0 - o.uv2.y;
#endif
return o;
}
static float d = 1.25;
static float R = 4.0;
static float2 angle = float2( 0.0,-0.0 );
float intersect(float2 xy, float2 sinangle, float2 cosangle)
{
float A = dot(-xy,xy)+d*d;
float B = 2.0*(_GlitchTweak*(dot(xy,sinangle)-d*cosangle.x*cosangle.y)-d*d);
float C = d*d + 2.0*_GlitchTweak*d*cosangle.x*cosangle.y;
return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
}
float2 bkwtrans( float2 xy, float2 sinangle, float2 cosangle )
{
float c = intersect(xy, sinangle, cosangle);
float2 point = float2(c)*xy;
return point;
}
float2 radialDistortion(float2 coord, float2 screenSize)
{
float2 aspect = float2( 1.0, ( screenSize.y / screenSize.x ) * _GlitchTweak );
float2 cd = ( coord - float2( 0.5 ) ) * aspect;
return ( bkwtrans( cd, sin( angle ), cos( angle ) ) / aspect + float2( 0.5 ) );
}
float4 scanlineWeights(float distance, float4 color)
{
float4 wid = 2.0 + 2.0 * pow(color, float4(4.0));
float4 weights = float4(distance / 0.3);
return 2.0 * exp(-pow(weights * rsqrt(0.25 * wid), wid)) / (0.6 + 0.2 * wid);
}
half4 frag(v2f i) : COLOR
{
float2 aspect = float2( 1.0, _ScreenParams.y / _ScreenParams.x );
float2 screenSize = float2( _ScreenParams.x, _ScreenParams.y ) * aspect;
float2 pix = 1.0 / screenSize.xy;
float2 uv = i.uv.xy;
float2 xy = radialDistortion( i.uv.xy, screenSize );
half2 coords = i.uv2;
coords = (coords - 0.5) * 2.0;
half coordDot = dot( coords, coords );
float mask = 1.0 - ( coordDot * _Intensity * 0.1 );
float2 ratio_scale = ( xy * screenSize.xy - float2(0.5) );
float2 uv_ratio = frac( ratio_scale );
float distort = mask * 0.1;
float distortRadial = _GlitchRadial * scanlineWeights( sin( _Time.y ) * 0.001 + abs( uv_ratio.y ) - ( 1.0 - uv_ratio.y ), 0.0 ).x * 0.25;
float scanline = fmod( 40 * ( 4 + _Time.x ) - xy.y * screenSize.y * 0.125, 1.0 ) * 0.25;
distort += distortRadial * scanline * 0.75;
float crt = pow( sin( xy.y * screenSize.y * 0.2 ), 0.125 );
crt *= pow( sin( uv.x * screenSize.x * 0.25 ), 0.125 );
distort += crt * 0.75 * _GlitchCRT;
float scanlineFinal = pow( sin( 32 * _Time.x + xy.y * screenSize.y * 0.0025 ), 64 );
distort += scanlineFinal * 0.15 + distort * scanlineFinal * 0.15;
float3 distortionAberrationOffset = float3( 0.125, 0.75, 1.0 );
float2 modUV = float2( distort * _GlitchIntensity * 0.00325, 0.0 );
float3 sample = tex2D( _MainTex, xy - modUV ).rgb * distortionAberrationOffset;
float3 sample2 = tex2D( _MainTex, xy + modUV ).rgb * ( 1.0 - distortionAberrationOffset );
half3 color = ( sample + sample2 );
color *= mask;
color *= 0.975 + 0.025 * sin( 110.0 * _Time.y );
float scan = saturate( sin( xy.y * screenSize.y + xy.x * screenSize.x * _ScanDirection ) * _ScanIntensity );
color -= scan;
float lum = saturate( dot( float3( 0.2125, 0.7154, 0.0721 ), color ) );
return float4( color.x, color.y, color.z, lum + scan );
}
ENDCG
Subshader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
}
@keiranlovett
Copy link
Author

Hud Distortion, Scanline and Curvature v0.5

Originally by https://twitter.com/Orihaus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment