![](https://csdnimg.cn/release/download_crawler_static/10744459/bg4.jpg)
4 CHAPTER 1. 基础知识
1 /** The platform−specific viewport which this viewport client is attached to. */
2 FViewport* Viewport;
使用这个属性,我们可以得到与硬件相关的一些视口的信息。在虚幻引擎 ™ 样例策略塔防游戏中
有如下辅助类:
1 class FStrategyHelpers
2 {
3 public:
4 /** convert point in screen space to ray in world space */
5 static bool DeprojectScreenToWorld(const FVector2D& ScreenPosition, class ULocalPlayer* Player,
,→ FVector& RayOrigin, FVector& RayDirection);
6
7 /** find intersection of ray in world space with ground plane */
8 static FVector IntersectRayWithPlane(const FVector& RayOrigin, const FVector& RayDirection, const
,→ FPlane& Plane);
9
10 /** create alpha map from UTexture2D for hit−tests in Slate */
11 static TSharedPtr<TArray<uint8>> CreateAlphaMapFromTexture(UTexture2D* Texture);
12
13 /** creates FCanvasUVTri without UV from 3x FVector2D */
14 static FCanvasUVTri CreateCanvasTri(FVector2D V0, FVector2D V1,FVector2D V2);
15 };
Listing 1.2: FStrategyHelpers
其中,转换屏幕中的一个点到三维空间函数static bool DeprojectScreenToWorld(const FVector2D& ScreenPosition
,→ , class ULocalPlayer* Player, FVector& RayOrigin, FVector& RayDirection);就是使用了 UGameViewport-
Client。代码讲解如下:
1 bool FStrategyHelpers::DeprojectScreenToWorld(const FVector2D& ScreenPosition, ULocalPlayer* Player,
,→ FVector& RayOrigin, FVector& RayDirection)
2 {
3 if (Player != NULL && Player−>ViewportClient != NULL && Player−>ViewportClient−>Viewport != NULL &&
,→ Player−>PlayerController != NULL)
4 {
5 //get the projection data
6 FSceneViewProjectionData ProjectionData;
7 if (Player−>GetProjectionData(Player−>ViewportClient−>Viewport, eSSP_FULL, /*out*/ ProjectionData
,→ ))
8 {
9 const FMatrix ViewMatrix = FTranslationMatrix(−ProjectionData.ViewOrigin) * ProjectionData.
,→ ViewRotationMatrix;
10 const FMatrix InvViewMatrix = ViewMatrix.InverseFast();