How far is this fragment from a triangle edge?

I’d like to write a vertex/fragment shader that outlines the triangles by drawing a different color when within some number of pixels (or world-space units — either would do) from the edge of a triangle.
6889517--805589--upload_2021-3-1_6-18-48.png
How can I tell, in my fragment function, how close I am to the edge of a triangle?

What you need is barycentric coordinates of the triangle. Take a look at this: https://discussions.unity.com/t/715163/7

Is it though? Unless I misunderstand, barycentric coordinates will “stretch” as the triangle stretches out, just like the texture on a texture-mapped triangle. So my borders will not be uniform. I need a distance-from-the-edge measure that is constant in either screen space or world space.

Ah, never mind. I found this tutorial, which explains how to combine barycentric coordinates with screen-space derivatives to work out how far in barycentric space you should allow the border to extend.

It’s complex stuff — but as usual, Catlike does an excellent job of explaining it. Thanks for the nudge in the right direction!

1 Like