1
$\begingroup$

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?

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

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)$.

This coordinate runs from 0 at the edges to 1 at the center, so to go from black at the enter to white at the edges, you could take 1 minus that. And of course, you can do any other gradient using this coordinate to interpolate the desired colors.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.