Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mikko Mononen's UI 'feathering' technique
void CalculateEdgeNormal(float& nx, float& ny, float x0, float y0, float x1, float y1)
{
const float x01 = x1 - x0;
const float y01 = y1 - y0;
const float length = Sqrt(x01 * x01 + y01 * y01);
const float dx = x01 / length;
const float dy = y01 / length;
nx = dy;
ny = -dx;
}
void FeatherConvexPolygon(Primitives& primitives, const Vertex* vertices, int count, float amount, const Texture* texture)
{
Vertex* extruded = Memory::Allocate<Vertex>(Memory::Temp, sizeof(Vertex) * count);
for (int i = 0; i < count; ++i)
{
const Vertex& previous = vertices[(i + count - 1) % count];
const Vertex& current = vertices[i];
const Vertex& next = vertices[(i + 1) % count];
float nx0, ny0, nx1, ny1;
CalculateEdgeNormal(nx0, ny0, previous.x, previous.y, current.x, current.y);
CalculateEdgeNormal(nx1, ny1, current.x, current.y, next.x, next.y);
float nx = (nx0 + nx1) * 0.5f;
float ny = (ny0 + ny1) * 0.5f;
extruded[i] = Vertex(current.x + nx * amount, current.y + ny * amount, Color(current.r, current.g, current.b, 0.0f));
}
for (int i = 0; i < count; ++i)
{
const int j = (i + 1) % count;
AddQuad(primitives, vertices[i], extruded[i], extruded[j], vertices[j], texture);
}
Memory::Free(extruded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.