rework label shader

This commit is contained in:
ohyzha
2024-08-28 11:12:59 +03:00
parent e2ae1687ac
commit 6305cbfe1e
7 changed files with 231 additions and 129 deletions

View File

@@ -1,44 +1,73 @@
#version 450
layout(location = 0) in vec4 color;
layout(location = 1) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
layout(set = 5, binding = 0) uniform LabelData
{
vec2 textSize;
vec4 textSize;
vec4 color;
vec4 bboxCenter;
float radius;
float arrowLength;
float arrowWidth;
bool hasRoundedCorners;
bool hasArrow;
} labelInfo;
void main()
{
if (labelInfo.hasRoundedCorners)
{
vec2 bbox;
if (!labelInfo.hasArrow)
{
bbox = labelInfo.textSize;
}
else
{
bbox = vec2(labelInfo.textSize.x, labelInfo.textSize.y + labelInfo.arrowLength);
}
if (labelInfo.hasRoundedCorners || labelInfo.hasArrow)
{
vec2 bbox = vec2(labelInfo.textSize);
const float arrowLength = int(labelInfo.hasArrow) * labelInfo.arrowLength;
float arrowWidth = labelInfo.arrowWidth;
vec2 uvScaled = texCoord;
const float arrowLength = labelInfo.hasArrow ? labelInfo.arrowLength : 0;
vec2 uvScaled = texCoord * bbox;
//vec2 uvNoArrow = texCoord * labelInfo.textSize;
float distX = min(uvScaled.x, bbox.x - uvScaled.x);
float distY = min(uvScaled.y - arrowLength, bbox.y - uvScaled.y);
float distanceFromEdge = distX * distY;
float scaledRadius = labelInfo.radius/10000.f;
if (distanceFromEdge < scaledRadius)
float distanceFromCorner = distX * distY;
if (distanceFromCorner < labelInfo.radius)
{
discard;
}
// plain arrow
if (labelInfo.hasArrow && !labelInfo.hasRoundedCorners)
{
// Some bias to prevent line between arrow and label +
// check that we are dealing with lower parts of the label where arrow should be drawn,
// to prevent discarding fragments from upper corners
if (distY <= 0.01 && uvScaled.y < (bbox.y - arrowWidth) && arrowLength != 0.0f)
{
arrowWidth = arrowWidth * ((arrowLength + distY) / arrowLength);
if (distX < (bbox.x - arrowWidth) * 0.5)
{
discard;
}
}
}
// TODO: rounded corners + rounded arrow.
// now renders rounded corners and sharp arrow
else if (labelInfo.hasArrow && labelInfo.hasRoundedCorners)
{
if (distY <= 0.05 && uvScaled.y < (bbox.y - arrowWidth) && arrowLength != 0.0f)
{
arrowWidth = arrowWidth * ((arrowLength + distY) / arrowLength);
if (distX < (bbox.x - arrowWidth) * 0.5)
{
discard;
}
}
else
{
discard;
}
}
// no arrow, rounded corners
else
{
discard;
}
}
}
outColor = color;
outColor = labelInfo.color;
}