I’ve been trying to get accurate bounding boxes for the glyphs produced by the UText3DComponent, part of the 3D Text plugin. I need this info so I can set the caret position and text breaks while using unfixed-width fonts. Unfortunately, the 3D Text Component doesn’t seem to have any member functions that will directly give me this info, and it I read the Bounds variable, it’s always just 0.0 on all axes.
So I came up with a (mid) solution that kinda works:
void AQUserInterface::SetCaretPosition(UText3DComponent* Text)
{
int32 GlyphCount = Text->GetGlyphCount();
if (GlyphCount > 0)
{
UStaticMeshComponent* LastGlyph = Text->GetGlyphMeshComponent(GlyphCount - 1);
if (LastGlyph)
{
FVector LocalOrigin, LocalExtent;
LastGlyph->GetLocalBounds(LocalOrigin, LocalExtent);
FVector RightSideLocalPosition = LocalOrigin + FVector(0.0f, LocalExtent.Y, 0.0f);
FVector RightSideWorldPosition = LastGlyph->GetComponentTransform().TransformPosition(RightSideLocalPosition);
Caret->SetWorldLocation(RightSideWorldPosition);
// Debug
FVector WorldOrigin = LastGlyph->GetComponentTransform().TransformPosition(LocalOrigin);
FColor BoxColor = FColor::Red;
float Duration = 1.0f;
float Thickness = 1.5f;
DrawDebugBox(GetWorld(), WorldOrigin, LocalExtent, LastGlyph->GetComponentTransform().GetRotation(), BoxColor, true, Duration, 0, Thickness);
}
}
}
But unfortunately, the debug boxes appear a lil large:
The upper-right-rear corners are in the correct positions, but the boxes seem to be double the intended size so I adjust the DrawDebug code, which works great on every letter EXCEPT those like lowercase p, y, and q (Edit: It turns out this is an issue on the original implementation as well).
// Debug
FVector CorrectedExtent = LocalExtent * 0.5;
FVector WorldOrigin = LastGlyph->GetComponentTransform().TransformPosition(LocalOrigin);
FColor BoxColor = FColor::Red;
float Duration = 1.0f;
float Thickness = 1.5f; // Thickness of the box lines
DrawDebugBox(GetWorld(), WorldOrigin + CorrectedExtent, CorrectedExtent, LastGlyph->GetComponentTransform().GetRotation(), BoxColor, true, Duration, 0, Thickness);
What am I getting wrong here and what suggestions might you have to improve this? So does anyone know how to work with UText3DComponent and is willing to give me a hand? Please let me know if I’m making any mistakes or incorrect assumptions.