Skip to content

Instantly share code, notes, and snippets.

@SoylentGraham
Created February 24, 2016 16:26
Show Gist options
  • Save SoylentGraham/c6326adb2047bfc13a28 to your computer and use it in GitHub Desktop.
Save SoylentGraham/c6326adb2047bfc13a28 to your computer and use it in GitHub Desktop.
pixel shader fade towards edges
float2 texelsize; // 1/width
bool isalpha(uv,offset)
{
// if bilinear sampling, then you might want to increase this rather than checking for pure 0
return sample( uv + offset*texelsize ).w < 0.1;
}
float getdistancetoedge(uv,float radius)
{
// can increase samples to improve quality but bileanear filtering might do this for you
float2 offsets[8] =
{
float2(-1,-1), float2(0,-1), float2(1,-1),
float2(-1,0), /*float2(0,0),*/ float2(1,0),
float2(-1,1), float2(0,1), float2(1,1)
}
float mindistance = 999;
for ( int i=0;i<8;i++ )
{
float distance = radius * length(offsets[i]);
bool alpha = isalpha( uv, offsets[i]*radius );
if ( alpha )
mindistance = min( distance, mindistance );
}
return mindistance;
}
frag()
{
float distancetoedge = getdistancetoedge( uv, blurrange );
distancetoedge = min( distancetoedge, 1 );
rgba = sample( uv );
rgba.w *= min( 1, range( distancetoedge, 0, 0.3 ) );
return rgba;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment