void YourClass::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI)
{
if (!View || !PDI || !Viewport)
{
return;
}
// Selection rectangle corners in screen space
const FVector2D& Start = SelectionFrameStartPos;
const FVector2D& End = SelectionFrameEndPos;
// Get viewport size
const int32 ViewportWidth = Viewport->GetSizeXY().X;
const int32 ViewportHeight = Viewport->GetSizeXY().Y;
// Calculate all four corners of the rectangle in screen space
FVector2D TopLeft(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y));
FVector2D TopRight(FMath::Max(Start.X, End.X), FMath::Min(Start.Y, End.Y));
FVector2D BottomRight(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y));
FVector2D BottomLeft(FMath::Min(Start.X, End.X), FMath::Max(Start.Y, End.Y));
// Depth from camera for the rectangle
const float DepthFromCamera = 1000.0f;
// Get view information
const FVector ViewOrigin = View->ViewLocation;
const FMatrix InvViewMatrix = View->ViewMatrices.GetInvViewMatrix();
const FVector ForwardDir = InvViewMatrix.GetColumn(2).GetSafeNormal();
// Convert 2D screen points to 3D world space
FVector TopLeftWorld, TopRightWorld, BottomRightWorld, BottomLeftWorld;
// Convert normalized device coordinates (-1 to 1) to world space
FVector4 DeviceZ(0, 0, 0, 1);
// Convert each corner to world space
FSceneView::DeprojectScreenToWorld(TopLeft,
View->UnscaledViewRect,
View->ViewMatrices.GetInvViewProjectionMatrix(),
TopLeftWorld);
FSceneView::DeprojectScreenToWorld(TopRight,
View->UnscaledViewRect,
View->ViewMatrices.GetInvViewProjectionMatrix(),
TopRightWorld);
FSceneView::DeprojectScreenToWorld(BottomRight,
View->UnscaledViewRect,
View->ViewMatrices.GetInvViewProjectionMatrix(),
BottomRightWorld);
FSceneView::DeprojectScreenToWorld(BottomLeft,
View->UnscaledViewRect,
View->ViewMatrices.GetInvViewProjectionMatrix(),
BottomLeftWorld);
// Project these points to the desired depth
TopLeftWorld = ViewOrigin + (TopLeftWorld - ViewOrigin).GetSafeNormal() * DepthFromCamera;
TopRightWorld = ViewOrigin + (TopRightWorld - ViewOrigin).GetSafeNormal() * DepthFromCamera;
BottomRightWorld = ViewOrigin + (BottomRightWorld - ViewOrigin).GetSafeNormal() * DepthFromCamera;
BottomLeftWorld = ViewOrigin + (BottomLeftWorld - ViewOrigin).GetSafeNormal() * DepthFromCamera;
// Draw the rectangle
const FLinearColor LineColor = FLinearColor::White;
const uint8 DepthPriority = SDPG_Foreground;
PDI->DrawLine(TopLeftWorld, TopRightWorld, LineColor, DepthPriority);
PDI->DrawLine(TopRightWorld, BottomRightWorld, LineColor, DepthPriority);
PDI->DrawLine(BottomRightWorld, BottomLeftWorld, LineColor, DepthPriority);
PDI->DrawLine(BottomLeftWorld, TopLeftWorld, LineColor, DepthPriority);
}
void YourClass::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI)
{
if (!View || !PDI || !Viewport)
{
return;
}
// Selection rectangle corners in screen space
const FVector2D& Start = SelectionFrameStartPos;
const FVector2D& End = SelectionFrameEndPos;
// Calculate all four corners of the rectangle in screen space
FVector2D TopLeft(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y));
FVector2D TopRight(FMath::Max(Start.X, End.X), FMath::Min(Start.Y, End.Y));
FVector2D BottomRight(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y));
FVector2D BottomLeft(FMath::Min(Start.X, End.X), FMath::Max(Start.Y, End.Y));
// Get view information
const FVector ViewOrigin = View->ViewLocation;
const FMatrix InvViewProjMatrix = View->ViewMatrices.GetInvViewProjectionMatrix();
// Fixed depth from camera for the rectangle (in NDC space, 0.5 is middle of clip space)
const float DepthNDC = 0.5f;
// Convert each corner to world space at our fixed depth
FVector TopLeftWorld, TopRightWorld, BottomRightWorld, BottomLeftWorld;
// Convert screen coordinates to normalized device coordinates (-1 to 1)
FVector2D ViewportSize(Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y);
FVector2D NDCTopLeft = (TopLeft / ViewportSize) * 2.0f - FVector2D(1, 1);
FVector2D NDCTopRight = (TopRight / ViewportSize) * 2.0f - FVector2D(1, 1);
FVector2D NDCBottomRight = (BottomRight / ViewportSize) * 2.0f - FVector2D(1, 1);
FVector2D NDCBottomLeft = (BottomLeft / ViewportSize) * 2.0f - FVector2D(1, 1);
// Flip Y for NDC space
NDCTopLeft.Y = -NDCTopLeft.Y;
NDCTopRight.Y = -NDCTopRight.Y;
NDCBottomRight.Y = -NDCBottomRight.Y;
NDCBottomLeft.Y = -NDCBottomLeft.Y;
// Convert NDC to world space for each corner
TopLeftWorld = FVector(NDCTopLeft.X, NDCTopLeft.Y, DepthNDC);
TopLeftWorld = View->ViewMatrices.GetInvViewProjectionMatrix().TransformPosition(TopLeftWorld);
TopRightWorld = FVector(NDCTopRight.X, NDCTopRight.Y, DepthNDC);
TopRightWorld = View->ViewMatrices.GetInvViewProjectionMatrix().TransformPosition(TopRightWorld);
BottomRightWorld = FVector(NDCBottomRight.X, NDCBottomRight.Y, DepthNDC);
BottomRightWorld = View->ViewMatrices.GetInvViewProjectionMatrix().TransformPosition(BottomRightWorld);
BottomLeftWorld = FVector(NDCBottomLeft.X, NDCBottomLeft.Y, DepthNDC);
BottomLeftWorld = View->ViewMatrices.GetInvViewProjectionMatrix().TransformPosition(BottomLeftWorld);
// Draw the rectangle
const FLinearColor LineColor = FLinearColor::White;
const uint8 DepthPriority = SDPG_Foreground;
PDI->DrawLine(TopLeftWorld, TopRightWorld, LineColor, DepthPriority);
PDI->DrawLine(TopRightWorld, BottomRightWorld, LineColor, DepthPriority);
PDI->DrawLine(BottomRightWorld, BottomLeftWorld, LineColor, DepthPriority);
PDI->DrawLine(BottomLeftWorld, TopLeftWorld, LineColor, DepthPriority);
}
The DeprojectScreenToWorld function handles all the matrix math to ensure the points maintain their screen-space relationship when converted to world space. This guarantees that the rectangle will always appear straight on the screen, parallel to the application window borders, regardless of camera angle.