Optimize label size calculation

This commit is contained in:
Georg Hagen
2025-01-05 16:31:04 +01:00
parent e9a1c629d9
commit aca64c57b7
6 changed files with 102 additions and 103 deletions

View File

@@ -5,69 +5,69 @@ layout(location = 0) out vec4 outColor;
layout(set = 5, binding = 0) uniform LabelData
{
vec4 textSize;
vec4 color;
vec4 bboxCenter;
float radius;
float arrowLength;
float arrowWidth;
bool hasRoundedCorners;
bool hasArrow;
vec4 color;
vec2 textSize;
vec2 bboxCenter;
float radius;
float arrowLength;
float arrowWidth;
bool hasRoundedCorners;
bool hasArrow;
} labelInfo;
void main()
{
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;
{
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;
float distX = min(uvScaled.x, bbox.x - uvScaled.x);
float distY = min(uvScaled.y - arrowLength, bbox.y - uvScaled.y);
float distanceFromCorner = distX * distY;
float distX = min(uvScaled.x, bbox.x - uvScaled.x);
float distY = min(uvScaled.y - arrowLength, bbox.y - uvScaled.y);
float distanceFromCorner = distX * distY;
if (distanceFromCorner < labelInfo.radius)
{
// 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 = labelInfo.color;
if (distanceFromCorner < labelInfo.radius)
{
// 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 = labelInfo.color;
}

View File

@@ -8,18 +8,18 @@ layout(set = 0, binding = 0) uniform NodeData
layout(set = 1, binding = 0) uniform CameraData
{
mat4 viewProjection;
mat4 viewProjection;
} cam;
layout(set = 5, binding = 0) uniform LabelData
{
vec4 textSize;
vec4 color;
vec4 bboxCenter;
float radius;
float arrowLength;
bool hasRoundedCorners;
bool hasArrow;
vec4 color;
vec2 textSize;
vec2 bboxCenter;
float radius;
float arrowLength;
bool hasRoundedCorners;
bool hasArrow;
} labelInfo;
layout(location = 0) out vec4 color;
@@ -28,18 +28,18 @@ layout(location = 1) out vec2 textureCoordinates;
// Background plane positions are in clipped space
const vec4 PLANE[4] = vec4[](
vec4(-0.5, -0.5, 0, 1), vec4(0.5, -0.5, 0, 1), vec4(-0.5, 0.5, 0, 1), vec4(0.5, 0.5, 0, 1)
);
const vec2 TEX_COORDS[4] = vec2[](
vec2(0, 0), vec2(1, 0), vec2(0, 1), vec2(1, 1)
);
void main() {
vec4 position = PLANE[gl_VertexIndex];
vec2 bbox = labelInfo.textSize.xy;
position.xy *= bbox;
position.xy += vec2(labelInfo.bboxCenter);
position.z = -0.001;
gl_Position = cam.viewProjection * node.world * position;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * bbox;
void main()
{
vec4 position = PLANE[gl_VertexIndex];
position.xy *= labelInfo.textSize;
position.xy += labelInfo.bboxCenter;
//position.z = -0.001;
gl_Position = cam.viewProjection * node.world * position;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * labelInfo.textSize;
}

View File

@@ -30,9 +30,9 @@ layout(set = 4, binding = 0) uniform BillboardData
layout(set = 5, binding = 0) uniform LabelData
{
vec4 textSize;
vec4 color;
vec4 bboxCenter;
vec2 textSize;
vec2 bboxCenter;
float radius;
float arrowLength;
bool hasRoundedCorners;
@@ -53,11 +53,10 @@ const vec2 TEX_COORDS[4] = vec2[](
void main() {
vec4 position = PLANE[gl_VertexIndex];
vec2 bbox = labelInfo.textSize.xy;
position.xy *= bbox;
position.xy += vec2(labelInfo.bboxCenter);
position.xy *= labelInfo.textSize;
position.xy += labelInfo.bboxCenter;
position.z = -0.001;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * bbox;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * labelInfo.textSize;
if (!billboardInfo.isFixedSize)
{