UE4 LocalPlayer 底层学习

ULocalPlayer相对于UPlayer增加了viewport的操作

而在gameinstance内,记录了所有的ULcalPlayer,如下: 

既可以用于二维转三维操作

FVector UPathSourceBlueprintFunctionLib::ProjectScreenPosToWorldLoc(APlayerController const* Player, USceneCaptureComponent2D* component) {
	FVector WorldLocation, WorldDirection;
	Player->PlayerCameraManager->SetFOV(component->FOVAngle);
	ULocalPlayer* LP = Player->GetLocalPlayer();
	FSceneViewProjectionData projectionData;
	LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, projectionData);
	FVector2D ScreenPosition = FVector2D::ZeroVector;
	//LP->ViewportClient->GetViewportSize(ScreenPosition);
	FMatrix viewPlanesMatrix = FMatrix(
		FPlane(0, 0, 1, 0),
		FPlane(1, 0, 0, 0),
		FPlane(0, 1, 0, 0),
		FPlane(0, 0, 0, 1));
	const FMatrix viewRotationMatrix = FInverseRotationMatrix(component->GetComponentRotation()) * viewPlanesMatrix;
	const FMatrix invViewProjMatrix = (FTranslationMatrix(-FVector(0.0, 0.0, 0.0)) * viewRotationMatrix * projectionData.ProjectionMatrix).InverseFast();
	FSceneView::DeprojectScreenToWorld(ScreenPosition, projectionData.GetConstrainedViewRect(), invViewProjMatrix, WorldLocation, WorldDirection);
	WorldLocation = component->GetComponentLocation();

	return WorldLocation;
}

将屏幕位置暴露:

FVector UPathSourceBlueprintFunctionLib::ProjectScreenPosToWorldLoc(APlayerController const* Player, USceneCaptureComponent2D* component, FVector2D ScreenPosition) {
	FVector WorldLocation, WorldDirection;
	Player->PlayerCameraManager->SetFOV(component->FOVAngle);
	ULocalPlayer* LP = Player->GetLocalPlayer();
	FSceneViewProjectionData projectionData;
	LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, projectionData);
	FMatrix viewPlanesMatrix = FMatrix(
		FPlane(0, 0, 1, 0),
		FPlane(1, 0, 0, 0),
		FPlane(0, 1, 0, 0),
		FPlane(0, 0, 0, 1));
	const FMatrix viewRotationMatrix = FInverseRotationMatrix(component->GetComponentRotation()) * viewPlanesMatrix;
	const FMatrix invViewProjMatrix = (FTranslationMatrix(-FVector(0.0, 0.0, 0.0)) * viewRotationMatrix * projectionData.ProjectionMatrix).InverseFast();
	FSceneView::DeprojectScreenToWorld(ScreenPosition, projectionData.GetConstrainedViewRect(), invViewProjMatrix, WorldLocation, WorldDirection);
	WorldLocation = component->GetComponentLocation();

	return WorldLocation;
}


FVector2D UPathSourceBlueprintFunctionLib::GetScreenPosition(APlayerController const* Player, FVector worldPosition, USceneCaptureComponent2D* component)
{
	Player->PlayerCameraManager->SetFOV(component->FOVAngle);
	ULocalPlayer* LP = Player->GetLocalPlayer();
	FSceneViewProjectionData projectionData;
	LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, projectionData);
	FVector2D ScreenPosition;
	FMatrix viewPlanesMatrix = FMatrix(
		FPlane(0, 0, 1, 0),
		FPlane(1, 0, 0, 0),
		FPlane(0, 1, 0, 0),
		FPlane(0, 0, 0, 1));
	const FMatrix viewRotationMatrix = FInverseRotationMatrix(component->GetComponentRotation()) * viewPlanesMatrix;
	const FMatrix ViewProjMatrix = FTranslationMatrix(-component->GetComponentLocation()) * viewRotationMatrix * projectionData.ProjectionMatrix;
	FSceneView::ProjectWorldToScreen(worldPosition, projectionData.GetConstrainedViewRect(), ViewProjMatrix, ScreenPosition);
	return ScreenPosition;
}

猜你喜欢

转载自blog.csdn.net/qqQQqsadfj/article/details/129280243