With Unreal C++, I would like to draw the rectangle to display always the same,…
- User
With Unreal C++, I would like to draw the rectangle to display always the same, orthogonal the User screen (or Viewport Widget), so unchanged, and not depending on the viewport Camera rotation/angle changes. Can you show the code eventually using ViewportClient->GetViewRotation() and drawing it in the viewport using FPrimitiveDrawInterface* PDI; PDI->DrawLine(Box... ?
Assistant
To achieve the goal of drawing a rectangle in the viewport that remains orthogonal to the user's screen and doesn't change with camera rotations, you can use Unreal Engine's
FPrimitiveDrawInterfaceto draw in the viewport. The key is to calculate the rectangle's positions in screen space, which doesn't change with camera rotations.Below is an example of how you could accomplish this. This example assumes you're working within a custom
HUDclass or similar, where you have access to thePlayerControllerand can get theViewportClient.This code snippet demonstrates how to draw a rectangle in screen space. Note that in screen space, coordinates (0,0) start from the top-left of the screen and go down to the bottom-right.
void YourCustomHUD::DrawHUD() { Super::DrawHUD(); // Assuming you have access to the PlayerController if (GetPlayerController()) { UGameplayStatics::DeprojectMousePositionToWorld(GetPlayerController(), MousePosition, MouseDirection); // Get viewport size FVector2D ViewportSize; GetPlayerController()->GetViewportSize(ViewportSize); // Define your rectangle's dimensions float RectWidth = 200.0f; float RectHeight = 100.0f; // Position of the rectangle (top-left corner), you can change this as needed FVector2D RectPosition(50, 50); // Convert screen coordinates to viewport coordinates if needed FVector2D TopLeftViewport = RectPosition; FVector2D BottomRightViewport = RectPosition + FVector2D(RectWidth, RectHeight); // Draw the rectangle directly in screen space FPrimitiveDrawInterface* PDI = GetWorld()->GetPrimitiveDrawInterface(); if (PDI) { PDI->DrawLine(FVector(TopLeftViewport.X, TopLeftViewport.Y, 0), FVector(BottomRightViewport.X, TopLeftViewport.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRightViewport.X, TopLeftViewport.Y, 0), FVector(BottomRightViewport.X, BottomRightViewport.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRightViewport.X, BottomRightViewport.Y, 0), FVector(TopLeftViewport.X, BottomRightViewport.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(TopLeftViewport.X, BottomRightViewport.Y, 0), FVector(TopLeftViewport.X, TopLeftViewport.Y, 0), FColor::Red, false, 2.0f); } } }However, if you specifically want to use
GetViewRotation()and ensure that the rectangle appears as if it's always orthogonal to the screen (which might be overcomplicating things for a simple HUD rectangle), you could consider transforming points into world space and then project them back to screen space. But for a simple rectangle that doesn't change with camera rotation, the above approach is straightforward and effective.Also, keep in mind that
DrawHUD()is a built-in function inHUDclass which might be where you're placing this code. If you're working in a different context (like aWidget), the approach will be entirely different, focusing more on Slate framework.If you're directly working with Viewport and trying to draw from a
Gamemodule or similar, ensure you have the right context and consider usingFRenderTargetandFCanvasfor more advanced rendering tasks.Lastly, always verify the compatibility of the methods I describe with your Unreal Engine version, as APIs can change.
- User
How to achieve hat in the method ::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) (with no access to ViewportClient)?
Assistant
To draw a rectangle in the viewport that remains orthogonal to the user's screen within the
::Rendermethod, which typically looks like this:void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Your drawing code here }You can use the
FSceneViewto get the view's projection and view matrices, but for drawing a simple rectangle that doesn't change with camera rotation, you can directly use screen coordinates. However, to convert between world and screen space, you'll typically use theFSceneView's projection and view matrices.Here's a basic example of how you could draw a rectangle. This example assumes you want to draw the rectangle at a specific screen position and size, regardless of the camera's rotation:
void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get viewport size FVector2D ViewportSize = Viewport->GetSizeXY(); // Define your rectangle's dimensions float RectWidth = 200.0f; float RectHeight = 100.0f; // Position of the rectangle (top-left corner), you can change this as needed FVector2D RectPosition(50, 50); // Convert screen coordinates to world space points FVector TopLeftWorld, BottomRightWorld; View->DeprojectMousePositionToWorld(FVector2D(RectPosition.X, RectPosition.Y), TopLeftWorld); View->DeprojectMousePositionToWorld(FVector2D(RectPosition.X + RectWidth, RectPosition.Y + RectHeight), BottomRightWorld); // Alternatively, if you want to keep it simple and not use Deproject // Just use screen coordinates directly (keep in mind Z is arbitrary here) FVector TopLeftScreen(RectPosition.X, RectPosition.Y, 0); FVector BottomRightScreen(RectPosition.X + RectWidth, RectPosition.Y + RectHeight, 0); // Draw the rectangle PDI->DrawLine(TopLeftScreen, FVector(BottomRightScreen.X, TopLeftScreen.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRightScreen.X, TopLeftScreen.Y, 0), BottomRightScreen, FColor::Red, false, 2.0f); PDI->DrawLine(BottomRightScreen, FVector(TopLeftScreen.X, BottomRightScreen.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(TopLeftScreen.X, BottomRightScreen.Y, 0), TopLeftScreen, FColor::Red, false, 2.0f); }However, note that using screen coordinates directly might not be the best approach here because
PDI->DrawLineexpects world space coordinates.A better approach would be to project your rectangle's corners into world space using the view's projection matrix inverse, but for simplicity and to ensure it works as expected, consider directly using
FCanvasandFRenderTargetfor more advanced rendering tasks.If you're looking for an orthogonal rectangle that scales with the camera distance (which seems to be what you initially asked), consider transforming points into world space, then use
PDIto draw.But if you simply want a HUD-like rectangle that doesn't change with camera rotation and always faces the camera, consider using Slate for UI elements or leveraging
DrawHUDin aHUDclass for in-game HUD elements.Always verify the compatibility of the methods with your Unreal Engine version.
- User
the same, but considering the rectangle was drawn by User from drag&drop and we have the following stored: const FVector2D& Start = SelectionFrameStartPos; const FVector2D& End = SelectionFrameEndPos;
Assistant
If you have the start and end positions of the rectangle that was drawn by the user through drag-and-drop, and you want to draw this rectangle in the viewport, you can use the following code:
void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get viewport size FVector2D ViewportSize = Viewport->GetSizeXY(); // Define your rectangle's positions const FVector2D& Start = SelectionFrameStartPos; const FVector2D& End = SelectionFrameEndPos; // Ensure Start is the top-left and End is the bottom-right FVector2D TopLeft = FVector2D(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y)); FVector2D BottomRight = FVector2D(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y)); // Draw the rectangle PDI->DrawLine(FVector(TopLeft.X, TopLeft.Y, 0), FVector(BottomRight.X, TopLeft.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRight.X, TopLeft.Y, 0), FVector(BottomRight.X, BottomRight.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRight.X, BottomRight.Y, 0), FVector(TopLeft.X, BottomRight.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(TopLeft.X, BottomRight.Y, 0), FVector(TopLeft.X, TopLeft.Y, 0), FColor::Red, false, 2.0f); }This code assumes that
SelectionFrameStartPosandSelectionFrameEndPosare the start and end positions of the rectangle drawn by the user. The rectangle is then drawn at these positions.However, keep in mind that
PDI->DrawLineexpects world space coordinates. If yourSelectionFrameStartPosandSelectionFrameEndPosare in screen space, this code will work directly. But if they are in world space, you may need to project them onto the screen.Also, note that this code does not take into account the camera's rotation or position. If you want the rectangle to be drawn at a specific position in world space, you will need to transform the points into screen space using the view's projection matrix.
If you want to ensure that the rectangle is always visible and centered on the screen, regardless of the camera's rotation, consider using Slate for UI elements or leveraging
DrawHUDin aHUDclass for in-game HUD elements.Always verify the compatibility of the methods with your Unreal Engine version.
Here's an alternative approach using
FSceneViewto project points:void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get viewport size FVector2D ViewportSize = Viewport->GetSizeXY(); // Define your rectangle's positions const FVector2D& Start = SelectionFrameStartPos; const FVector2D& End = SelectionFrameEndPos; // Ensure Start is the top-left and End is the bottom-right FVector2D TopLeft = FVector2D(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y)); FVector2D BottomRight = FVector2D(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y)); // Project points into world space FVector TopLeftWorld, BottomRightWorld; View->DeprojectMousePositionToWorld(TopLeft, TopLeftWorld); View->DeprojectMousePositionToWorld(BottomRight, BottomRightWorld); // Draw the rectangle PDI->DrawLine(TopLeftWorld, FVector(BottomRightWorld.X, TopLeftWorld.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRightWorld.X, TopLeftWorld.Y, 0), BottomRightWorld, FColor::Red, false, 2.0f); PDI->DrawLine(BottomRightWorld, FVector(TopLeftWorld.X, BottomRightWorld.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(TopLeftWorld.X, BottomRightWorld.Y, 0), TopLeftWorld, FColor::Red, false, 2.0f); }However, this approach may not work as expected if your points are not in screen space.
Always test and verify the results.
- User
how to get something like ViewportClient->GetViewRotation from the Render() method?
Assistant
In the
Render()method, you can access the view rotation through theFSceneViewobject that is passed to the method.Here's how you can do it:
void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get the view rotation const FRotator ViewRotation = View->GetViewRotation(); // Use the view rotation as needed }The
GetViewRotation()method returns the rotation of the view, which is equivalent toViewportClient->GetViewRotation().Note that
FSceneViewprovides a lot of useful information about the current view, including:GetViewRotation(): Returns the rotation of the view.GetViewLocation(): Returns the location of the view.GetProjectionMatrix(): Returns the projection matrix of the view.GetViewMatrix(): Returns the view matrix of the view.
You can use these methods to access various properties of the view and perform transformations as needed.
Also, keep in mind that
FSceneViewis a snapshot of the view at a particular point in time, so it's safe to access its properties from within theRender()method.- User
can we simply apply that FRotator so from 2 points (StartSelectionPos and EndSelectionPos) we can draw a rectangle that is always orthogonal to the Screen (and not the camera orientation) ?
Assistant
Yes, you can apply the
FRotatorto transform the pointsStartSelectionPosandEndSelectionPosinto a coordinate system that is orthogonal to the screen.However, to draw a rectangle that is always orthogonal to the screen, you don't actually need to apply the
FRotatorto the points. Instead, you can simply project the points onto the screen and then draw the rectangle using those projected points.Here's an example of how you can do it:
void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get the viewport size FVector2D ViewportSize = Viewport->GetSizeXY(); // Define your rectangle's positions const FVector2D& Start = SelectionFrameStartPos; const FVector2D& End = SelectionFrameEndPos; // Ensure Start is the top-left and End is the bottom-right FVector2D TopLeft = FVector2D(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y)); FVector2D BottomRight = FVector2D(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y)); // Project points into screen space FVector TopLeftScreen, BottomRightScreen, TopRightScreen, BottomLeftScreen; View->Project(TopLeft, TopLeftScreen); View->Project(FVector(BottomRight.X, TopLeft.Y, 0), TopRightScreen); View->Project(BottomRight, BottomRightScreen); View->Project(FVector(TopLeft.X, BottomRight.Y, 0), BottomLeftScreen); // Draw the rectangle PDI->DrawLine(TopLeftScreen, TopRightScreen, FColor::Red, false, 2.0f); PDI->DrawLine(TopRightScreen, BottomRightScreen, FColor::Red, false, 2.0f); PDI->DrawLine(BottomRightScreen, BottomLeftScreen, FColor::Red, false, 2.0f); PDI->DrawLine(BottomLeftScreen, TopLeftScreen, FColor::Red, false, 2.0f); }In this example, we first project the points
TopLeft,TopRight,BottomRight, andBottomLeftinto screen space using theProject()method ofFSceneView. Then, we draw the rectangle using those projected points.This approach ensures that the rectangle is always orthogonal to the screen, regardless of the camera's orientation.
Note that we use
View->Project()to project the points into screen space. This method takes a 3D point and projects it onto the screen, returning the projected 3D point.Also, keep in mind that the
Project()method assumes that the points are in world space. If your points are in a different coordinate system, you may need to transform them before projecting them onto the screen.- User
I have 2 points: FVector2D SelectionFrameStartPos; FVector2D SelectionFrameEndPos; and: const FVector ViewOrigin = View->ViewLocation; const FMatrix InvViewProjMatrix = View->ViewMatrices.GetInvViewProjectionMatrix(); or FMatrix InvViewMatrix = View->ViewMatrices.GetInvViewMatrix(); FRotator ViewRotation = InvViewMatrix.Rotator();
How to draw a rectangle with FPrimitiveDrawInterface* PDI->DrawLine() that is always straight on the screen (parallel to the application window borders)?
Assistant
To draw a rectangle that is always straight on the screen (parallel to the application window borders), you can use the following approach:
void YourCustomRenderer::Render(const FSceneView* View, FViewport* Viewport, FPrimitiveDrawInterface* PDI) { Super::Render(View, Viewport, PDI); // Get the viewport size FVector2D ViewportSize = Viewport->GetSizeXY(); // Define your rectangle's positions const FVector2D& Start = SelectionFrameStartPos; const FVector2D& End = SelectionFrameEndPos; // Ensure Start is the top-left and End is the bottom-right FVector2D TopLeft = FVector2D(FMath::Min(Start.X, End.X), FMath::Min(Start.Y, End.Y)); FVector2D BottomRight = FVector2D(FMath::Max(Start.X, End.X), FMath::Max(Start.Y, End.Y)); // Convert to screen space FVector TopLeftScreen = FVector(TopLeft.X, TopLeft.Y, 0); FVector BottomRightScreen = FVector(BottomRight.X, BottomRight.Y, 0); // Draw the rectangle PDI->DrawLine(TopLeftScreen, FVector(BottomRightScreen.X, TopLeftScreen.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(BottomRightScreen.X, TopLeftScreen.Y, 0), BottomRightScreen, FColor::Red, false, 2.0f); PDI->DrawLine(BottomRightScreen, FVector(TopLeftScreen.X, BottomRightScreen.Y, 0), FColor::Red, false, 2.0f); PDI->DrawLine(FVector(TopLeftScreen.X, BottomRightScreen.Y, 0), TopLeftScreen, FColor::Red, false, 2.0f); }In this example, we simply draw the rectangle using the
TopLeftScreenandBottomRightScreenpoints, which are already in screen space.The key here is that we are not transforming the points using the
InvViewProjMatrixorInvViewMatrix. Instead, we are simply using the 2D coordinates of the points to draw the rectangle.This approach works because the
FPrimitiveDrawInterface::DrawLine()method expects points in screen space, and we are providing points that are already in screen space.Note that if you want to transform the points using the
InvViewProjMatrixorInvViewMatrix, you can do so, but you will need to make sure that you are transforming the points correctly.For example, you can use the following code to transform the points:
FVector TopLeftWorld = FVector(TopLeft.X, TopLeft.Y, 0); FVector BottomRightWorld = FVector(BottomRight.X, BottomRight.Y, 0); TopLeftWorld = ViewOrigin + InvViewMatrix.TransformVector(TopLeftWorld - ViewOrigin); BottomRightWorld = ViewOrigin + InvViewMatrix.TransformVector(BottomRightWorld - ViewOrigin); PDI->DrawLine(TopLeftWorld, FVector(BottomRightWorld.X, TopLeftWorld.Y, TopLeftWorld.Z), FColor::Red, false, 2.0f);However, this approach is not necessary in this case, and the simpler approach above will work just fine.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.