Orihaus

Hud Distortion, Scanline and Curvature v0.5

Nov 17th, 2013
1,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. Shader "Hidden/Vignetting" {
  2.     Properties {
  3.         _MainTex ("Base", 2D) = "white" {}
  4.         _VignetteTex ("Vignette", 2D) = "white" {}
  5.         _GlitchTex ("Glitch", 2D) = "white" {}
  6.     }
  7.    
  8.     CGINCLUDE
  9.    
  10.     #pragma target 3.0
  11.     #include "UnityCG.cginc"
  12.    
  13.     struct v2f {
  14.         float4 pos : POSITION;
  15.         float2 uv : TEXCOORD0;
  16.         float2 uv2 : TEXCOORD1;
  17.     };
  18.    
  19.     sampler2D _MainTex;
  20.     sampler2D _VignetteTex;
  21.     sampler2D _GlitchTex;
  22.    
  23.     half _Intensity;
  24.     half _Blur;
  25.     half _GlitchIntensity;
  26.     half _GlitchTweak;
  27.     half _GlitchRadial;
  28.     half _GlitchCRT;
  29.    
  30.     half _ScanDirection;
  31.     half _ScanIntensity;
  32.  
  33.     float4 _MainTex_TexelSize;
  34.        
  35.     v2f vert( appdata_img v ) {
  36.         v2f o;
  37.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  38.         o.uv = v.texcoord.xy;
  39.         o.uv2 = v.texcoord.xy;
  40.  
  41.         #if UNITY_UV_STARTS_AT_TOP
  42.         if (_MainTex_TexelSize.y < 0)
  43.              o.uv2.y = 1.0 - o.uv2.y;
  44.         #endif
  45.  
  46.         return o;
  47.     }
  48.  
  49.     static float d = 1.25;
  50.     static float R = 4.0;
  51.     static float2 angle = float2( 0.0,-0.0 );
  52.    
  53.     float intersect(float2 xy, float2 sinangle, float2 cosangle)
  54.     {
  55.             float A = dot(-xy,xy)+d*d;
  56.             float B = 2.0*(_GlitchTweak*(dot(xy,sinangle)-d*cosangle.x*cosangle.y)-d*d);
  57.             float C = d*d + 2.0*_GlitchTweak*d*cosangle.x*cosangle.y;
  58.             return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
  59.     }
  60.    
  61.     float2 bkwtrans( float2 xy, float2 sinangle, float2 cosangle )
  62.     {
  63.         float c = intersect(xy, sinangle, cosangle);
  64.         float2 point = float2(c)*xy;
  65.         return point;
  66.     }
  67.    
  68.     float2 radialDistortion(float2 coord, float2 screenSize)
  69.     {
  70.         float2 aspect = float2( 1.0, ( screenSize.y / screenSize.x ) * _GlitchTweak );
  71.         float2 cd = ( coord - float2( 0.5 ) ) * aspect;
  72.         return ( bkwtrans( cd, sin( angle ), cos( angle ) ) / aspect + float2( 0.5 ) );
  73.  
  74.     }
  75.  
  76.     float4 scanlineWeights(float distance, float4 color)
  77.     {
  78.         float4 wid = 2.0 + 2.0 * pow(color, float4(4.0));
  79.         float4 weights = float4(distance / 0.3);
  80.         return 2.0 * exp(-pow(weights * rsqrt(0.25 * wid), wid)) / (0.6 + 0.2 * wid);
  81.     }
  82.  
  83.     half4 frag(v2f i) : COLOR
  84.     {
  85.         float2 aspect = float2( 1.0, _ScreenParams.y / _ScreenParams.x );
  86.         float2 screenSize = float2( _ScreenParams.x, _ScreenParams.y ) * aspect;
  87.         float2 pix = 1.0 / screenSize.xy;  
  88.         float2 uv = i.uv.xy;
  89.  
  90.         float2 xy = radialDistortion( i.uv.xy, screenSize );
  91.        
  92.         half2 coords = i.uv2;
  93.         coords = (coords - 0.5) * 2.0;     
  94.         half coordDot = dot( coords, coords );
  95.         float mask = 1.0 - ( coordDot * _Intensity * 0.1 );
  96.  
  97.         float2 ratio_scale = ( xy * screenSize.xy - float2(0.5) );
  98.         float2 uv_ratio = frac( ratio_scale );
  99.         float distort = mask * 0.1;
  100.        
  101.         float distortRadial = _GlitchRadial * scanlineWeights( sin( _Time.y ) * 0.001  + abs( uv_ratio.y ) - ( 1.0 - uv_ratio.y ), 0.0 ).x * 0.25;
  102.         float scanline = fmod( 40 * ( 4 + _Time.x ) - xy.y * screenSize.y * 0.125, 1.0 ) * 0.25;
  103.         distort += distortRadial * scanline * 0.75;
  104.  
  105.         float crt = pow( sin( xy.y * screenSize.y * 0.2 ), 0.125 );
  106.         crt *= pow( sin( uv.x * screenSize.x * 0.25 ), 0.125 );
  107.         distort += crt * 0.75 * _GlitchCRT;
  108.        
  109.         float scanlineFinal = pow( sin( 32 * _Time.x + xy.y * screenSize.y * 0.0025 ), 64 );
  110.         distort += scanlineFinal * 0.15 + distort * scanlineFinal * 0.15;
  111.        
  112.         float3 distortionAberrationOffset = float3( 0.125, 0.75, 1.0 );
  113.        
  114.         float2 modUV = float2( distort * _GlitchIntensity * 0.00325, 0.0 );
  115.         float3 sample = tex2D( _MainTex, xy - modUV ).rgb * distortionAberrationOffset;
  116.         float3 sample2 = tex2D( _MainTex, xy + modUV ).rgb * ( 1.0 - distortionAberrationOffset );
  117.  
  118.         half3 color = ( sample + sample2 );
  119.         color *= mask;
  120.         color *= 0.975 + 0.025 * sin( 110.0 * _Time.y );
  121.        
  122.         float scan = saturate( sin( xy.y * screenSize.y + xy.x * screenSize.x * _ScanDirection ) * _ScanIntensity );
  123.         color -= scan;
  124.        
  125.         float lum = saturate( dot( float3( 0.2125, 0.7154, 0.0721 ), color ) );
  126.         return float4( color.x, color.y, color.z, lum + scan );
  127.     }
  128.  
  129.     ENDCG
  130.    
  131. Subshader {
  132.  Pass {
  133.       ZTest Always Cull Off ZWrite Off
  134.       Fog { Mode off }      
  135.  
  136.       CGPROGRAM
  137.       #pragma fragmentoption ARB_precision_hint_fastest
  138.       #pragma vertex vert
  139.       #pragma fragment frag
  140.       ENDCG
  141.   }
  142. }
  143.  
  144. Fallback off   
  145. }
Advertisement
Add Comment
Please, Sign In to add comment