UE4:采用给定相机的视锥体和 returns 形成它的六个平面

UE4: Take a given camera's view frustum and returns six planes that form it

我有一个正交相机,我想沿着它的顶部、底部、左侧和右侧边缘创建一个平面,但是在 Unreal Engine 4 中没有明显的方法来获取世界中的位置 space,我只能得到远近裁剪平面,这不是很有帮助。

Unity3D an utility function 为每个平截头体创建一个平面,但我还没有找到它的实现来了解它是如何工作的。

这是我目前使用的相机,它是品红色的截锥体。

我为此创建了一个自定义蓝图节点 returns 顶部、左侧、底部和右侧平截头体平面中心的世界坐标。

为了测试它,我在游戏开始时在每个平面的中心启动了一个火焰粒子。立方体代表相机位置。

蓝图使用

CameraUtils.h

#pragma once

#include "Camera/CameraActor.h"
#include "CameraUtils.generated.h"

/**
 * 
 */
UCLASS()
class PROJECTNAME_API ACameraUtils : public ACameraActor
{
    GENERATED_BODY()


public:

    UFUNCTION(BlueprintPure,
        meta = (
            DisplayName = "Get Camera Edges from Frustum",
            Keywords = "Camera Edges Frustum"
            ),
        Category = "Camera|Utility")
        static void GetCameraFrustumEdges(UCameraComponent* camera, FVector& topCenter, FVector& leftCenter, FVector& bottomCenter, FVector& rightCenter);


};

CameraUtils.cpp

#include "ProjectName.h"
#include "CameraUtils.h"


void ACameraUtils::GetCameraFrustumEdges(UCameraComponent* camera, FVector& topCenter, FVector& leftCenter, FVector& bottomCenter, FVector& rightCenter)
{
    // Assumptions: Camera is orthographic, Z axis is upwards, Y axis is right, X is forward

    FMinimalViewInfo cameraView;
    camera->GetCameraView(0.0f, cameraView);

    float width = cameraView.OrthoWidth;
    float height = width / cameraView.AspectRatio;

    topCenter.Set(cameraView.Location.X,
                  cameraView.Location.Y,
                  cameraView.Location.Z + height/2.0f);

    bottomCenter.Set(cameraView.Location.X,
                     cameraView.Location.Y,
                     cameraView.Location.Z - height / 2.0f);

    leftCenter.Set(cameraView.Location.X,
                   cameraView.Location.Y - width / 2.0f,
                   cameraView.Location.Z);

    rightCenter.Set(cameraView.Location.X,
                    cameraView.Location.Y + width / 2.0f,
                    cameraView.Location.Z);

}