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.