# How to generate a gradient to the edges of a triangle in GLSL?

I'm looking for an effect similar to this image. Assuming the UV coordinates of the triangle are known, such as:

p0 = [0, 0]
p1 = [1, 0]
p2 = [0, 1]


I know the centroid of the triangle formed by these UV coordinates is [0.33333, 0.33333]

How do I output a color from black in the center to white at the edges, such as it follows the gradient pattern in this image?

You can calculate the perpendicular distance from each edge, and then take the minimum of the three edges. This is related to barycentric coordinates (which are normalized edge distances). If you already have the barycentrics $$a, b, c$$, you can calculate $$3 \cdot \min(a, b, c)$$. The 3 is to normalize the gradient to the [0, 1] range, since the centroid of the triangle lies at barycentrics $$(\tfrac{1}{3}, \tfrac{1}{3}, \tfrac{1}{3})$$.
The UV mapping you mentioned makes $$u, v$$ equal to two of the barycentrics, so you can then calculate the gradient coordinate as $$3 \cdot \min(u, v, 1 - u - v)$$.