Get Camera Edges From Frustum

From Epic Wiki
Jump to: navigation, search

Template:Rating

Overview

This custom blueprint node retrieves the center of an orthographic camera's top, left, bottom and right frustum. It can be useful when you need to spawn actors right at the edge of the viewport.

Camera frustum visualized

CameraFrusum.png

Fire particle spawned at the center of each frustum. The cube represents the camera's location.

FireParticleSpawned.png

Usage

The node requires a CameraComponent as the input and outputs four FVectors, one for each frustum center.

BlueprintUsage.png


To get this node inside the project copy the following files to your C++ classes directory.

CameraUtils.h

<syntaxhighlight lang="cpp">

  1. pragma once
  1. include "Camera/CameraActor.h"
  2. 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);


}; </syntaxhighlight>


CameraUtils.cpp

Note: Don't forget to change ProjectName.h to whatever your project is named <syntaxhighlight lang="cpp">

  1. include "ProjectName.h"
  2. 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);

} </syntaxhighlight>