Skip to content

Commit d03ca01

Browse files
committed
This one works, but is a total mess. Updated to calculate the viewport width in meters at the center of the viewport, and set the extent of the spline to half of that value. It consistently shows up in the viewport when created. Need to change this to updating the polygon actor's transform to the center, and keeping the spline points centered around 0,0,0.
1 parent f9f20b2 commit d03ca01

File tree

1 file changed

+101
-14
lines changed

1 file changed

+101
-14
lines changed

Source/CesiumRuntime/Private/CesiumCartographicPolygon.cpp

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,99 @@ struct FViewportCornerInfo
2424
bool hitsPlane;
2525
};
2626

27+
float GetEditorViewportWidthAtGround()
28+
{
29+
float Width = 0.0f;
30+
31+
#if WITH_EDITOR
32+
if (GEditor && GEditor->GetActiveViewport())
33+
{
34+
FViewport* ActiveViewport = GEditor->GetActiveViewport();
35+
FLevelEditorViewportClient* ViewportClient = nullptr;
36+
37+
// Get the viewport client
38+
for (FLevelEditorViewportClient* Client : GEditor->GetLevelViewportClients())
39+
{
40+
if (Client && Client->Viewport == ActiveViewport)
41+
{
42+
ViewportClient = Client;
43+
break;
44+
}
45+
}
46+
47+
if (ViewportClient)
48+
{
49+
FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(
50+
ViewportClient->Viewport,
51+
ViewportClient->GetScene(),
52+
ViewportClient->EngineShowFlags)
53+
.SetRealtimeUpdate(true));
54+
55+
FSceneView* View = ViewportClient->CalcSceneView(&ViewFamily);
56+
57+
if (View)
58+
{
59+
FIntPoint ViewportSize = ActiveViewport->GetSizeXY();
60+
61+
// Define the plane at Z=0
62+
FPlane GroundPlane(FVector(0, 0, 0), FVector(0, 0, 1));
63+
64+
// Get left edge of viewport (vertically centered)
65+
FVector2D LeftScreen(0, ViewportSize.Y * 0.5f);
66+
FVector LeftWorldPos, LeftDirection;
67+
View->DeprojectFVector2D(LeftScreen, LeftWorldPos, LeftDirection);
68+
LeftDirection.Normalize();
69+
70+
// Get right edge of viewport (vertically centered)
71+
FVector2D RightScreen(ViewportSize.X, ViewportSize.Y * 0.5f);
72+
FVector RightWorldPos, RightDirection;
73+
View->DeprojectFVector2D(RightScreen, RightWorldPos, RightDirection);
74+
RightDirection.Normalize();
75+
76+
// Project left ray onto Z=0 plane
77+
FVector LeftPoint = FVector::ZeroVector;
78+
bool bLeftHits = false;
79+
float DenominatorLeft = FVector::DotProduct(LeftDirection, GroundPlane);
80+
if (FMath::Abs(DenominatorLeft) > KINDA_SMALL_NUMBER)
81+
{
82+
float T = -(FVector::DotProduct(LeftWorldPos, GroundPlane) + GroundPlane.W) / DenominatorLeft;
83+
if (T >= 0)
84+
{
85+
bLeftHits = true;
86+
LeftPoint = LeftWorldPos + (LeftDirection * T);
87+
}
88+
}
89+
90+
// Project right ray onto Z=0 plane
91+
FVector RightPoint = FVector::ZeroVector;
92+
bool bRightHits = false;
93+
float DenominatorRight = FVector::DotProduct(RightDirection, GroundPlane);
94+
if (FMath::Abs(DenominatorRight) > KINDA_SMALL_NUMBER)
95+
{
96+
float T = -(FVector::DotProduct(RightWorldPos, GroundPlane) + GroundPlane.W) / DenominatorRight;
97+
if (T >= 0)
98+
{
99+
bRightHits = true;
100+
RightPoint = RightWorldPos + (RightDirection * T);
101+
}
102+
}
103+
104+
// Calculate distance between the two points if both hit
105+
if (bLeftHits && bRightHits)
106+
{
107+
Width = FVector::Dist(LeftPoint, RightPoint);
108+
109+
// Convert from Unreal units (cm) to meters
110+
// Width = Width / 100.0f;
111+
}
112+
}
113+
}
114+
}
115+
#endif
116+
117+
return Width;
118+
}
119+
27120
bool GetEditorViewportCorners(TArray<FViewportCornerInfo>& Corners)
28121
{
29122
#if WITH_EDITOR
@@ -54,7 +147,7 @@ bool GetEditorViewportCorners(TArray<FViewportCornerInfo>& Corners)
54147

55148
if (View)
56149
{
57-
FIntPoint ViewportSize = ActiveViewport->GetSizeXY();
150+
const FIntPoint ViewportSize = ActiveViewport->GetSizeXY();
58151

59152
TArray<FVector2D> ScreenPositions = {
60153
FVector2D(0.1 * ViewportSize.X, 0.9 * ViewportSize.Y), // bottom left
@@ -116,24 +209,18 @@ ACesiumCartographicPolygon::ACesiumCartographicPolygon() : AActor() {
116209

117210
if (GetEditorViewportCorners(corners))
118211
{
119-
float extent = FVector::Distance(corners[0].PlanePosition, corners[1].PlanePosition) * 0.9f;
212+
// float extent = FVector::Distance(corners[0].PlanePosition, corners[1].PlanePosition) * 0.9f;
213+
float extent = GetEditorViewportWidthAtGround() * 0.4f;
120214
FVector center = corners[4].PlanePosition;
121215
points = {
122-
{ -extent, -extent, 0 },
123-
{ extent, -extent, 0 },
124-
{ extent, extent, 0 },
125-
{ -extent, extent, 0 },
216+
{ -1, -1, 0 },
217+
{ 1, -1, 0 },
218+
{ 1, 1, 0 },
219+
{ -1, 1, 0 },
126220
};
127-
//
128-
// for (FVector& point : points) {
129-
// point += center;
130-
// }
131221
if (corners.Num() >= 4) {
132222
for (int i=0; i < 4; ++i) {
133-
points[i] += center;
134-
// FVector& p1 = points[i];
135-
// FViewportCornerInfo& corner = corners[i];
136-
// p1 = corner.PlanePosition;
223+
points[i] = points[i] * extent + center;
137224
}
138225
}
139226
}

0 commit comments

Comments
 (0)