參考官方UGameplayStatics::ProjectWorldToScreen()函數的方法,進行一些修改調整。用來判定三維世界中一個點是否在屏幕範圍內。
bool AMyActor::IsInScrrenViewport(const FVector& WorldPosition)
{
APlayerController *Player = UGameplayStatics::GetPlayerController(this, 0);
ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
if (LP && LP->ViewportClient)
{
// get the projection data
FSceneViewProjectionData ProjectionData;
if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
{
FMatrix const ViewProjectionMatrix = ProjectionData.ComputeViewProjectionMatrix();
FVector2D ScreenPosition;
bool bResult = FSceneView::ProjectWorldToScreen(WorldPosition, ProjectionData.GetConstrainedViewRect(), ViewProjectionMatrix, ScreenPosition);
if (bResult && ScreenPosition.X > ProjectionData.GetViewRect().Min.X && ScreenPosition.X < ProjectionData.GetViewRect().Max.X
&& ScreenPosition.Y > ProjectionData.GetViewRect().Min.Y && ProjectionData.GetViewRect().Y < LensPadding.Max.Y)
{
return true;
}
}
}
return false;
}//本文由CSDN博主執手畫眉彎原創,未經允許不得轉載!