Search code examples
unreal-engine4

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


I have an orthographic camera and I'd like to create a plane along its top, bottom, left and right edges but in Unreal Engine 4 there is no obvious way of getting the locations in world space, all I could get is the far and near clipping plane and that's not very helpful.

Unity3D has an utility function that creates a plane for each frustum but I haven't found its implementation to see how it works.

Here's the camera I'm currently using and it's frustums in magenta. enter image description here


Solution

  • I created a custom blueprint node for that returns the world coordinates for the centers of the top, left, bottom and right frustum planes.

    To test it I've made it so that at game start a fire particle is initiated at the center of each of the planes. The cube represents the camera position.

    Before pressing play

    After pressing play

    Blueprint usage

    Blueprint usage

    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);
    
    }