col.a = (col.a - _Cutoff) / max(fwidth(col.a), 0.0001) + 0.5;

Sure, lets go through each part first.

col.a - _Cutoff

That should be familiar to anyone who’s done alpha testing before. That’s aligning the edge of where the cutoff should be within the alpha gradient. So now anywhere that’s visible is ≥ 0.0, anywhere that’s supposed to clipped is < 0.0.

/ max(fwidth(col.a), 0.0001)

Divide the previous value by how much the alpha is changing per pixel (fwidth), clamped to 0.0001 so we don’t do a divide by zero in places where the alpha isn’t changing.

This now means the value is going from 0.0 to 1.0 in the space of one pixel, which is good, but leaving it like that means we’re effectively shrinking the size of the texture compared to alpha test alone because we’re fading from the cutoff edge in.

And that brings us to: + 0.5

That last bit is to put what was the 0.0 edge of the original cutoff at 0.5 in the final alpha so the 1 pixel gradient we’ve calculated is half a pixel outside and half a pixel inside the original alpha tested shape which helps maintain the original’s visual size.

Tech Artist & Graphics Programmer lately focused on Unity VR game dev.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store