103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
#include "CameraCaptureActor.h"
|
||
|
||
#include "Components/SceneCaptureComponent2D.h"
|
||
#include "Camera/CameraComponent.h"
|
||
#include "Engine/TextureRenderTarget2D.h"
|
||
|
||
|
||
|
||
ACameraCaptureActor::ACameraCaptureActor()
|
||
{
|
||
// PrimaryActorTick.bCanEverTick = false;
|
||
}
|
||
|
||
void ACameraCaptureActor::CaptureNow()
|
||
{
|
||
if (ViewCapture)
|
||
{
|
||
ViewCapture->CaptureScene();
|
||
}
|
||
}
|
||
|
||
void ACameraCaptureActor::BeginPlay()
|
||
{
|
||
Super::BeginPlay();
|
||
BuildViewCaptures();
|
||
}
|
||
|
||
#if WITH_EDITOR
|
||
void ACameraCaptureActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
|
||
{
|
||
Super::PostEditChangeProperty(PropertyChangedEvent);
|
||
|
||
// 任何关键参数变化都重建(避免 RT 尺寸/数量不匹配)
|
||
BuildViewCaptures();
|
||
}
|
||
|
||
#endif
|
||
void ACameraCaptureActor::BuildViewCaptures()
|
||
{
|
||
if (ViewCapture)
|
||
{
|
||
ViewCapture->DestroyComponent();
|
||
ViewCapture = nullptr;
|
||
}
|
||
|
||
ViewRT = NewObject<UTextureRenderTarget2D>(this);
|
||
ViewRT->RenderTargetFormat = RTF_RGBA8;
|
||
ViewRT->InitCustomFormat(ViewWidth, ViewHeight, PF_B8G8R8A8, true);
|
||
ViewRT->ClearColor = FLinearColor::Black;
|
||
ViewRT->UpdateResourceImmediate(true);
|
||
|
||
ViewCapture = NewObject<USceneCaptureComponent2D>(this, TEXT("ViewCapture"));
|
||
ViewCapture->SetupAttachment(GetRootComponent());
|
||
ViewCapture->RegisterComponent();
|
||
|
||
ViewCapture->TextureTarget = ViewRT;
|
||
ViewCapture->CaptureSource = ESceneCaptureSource::SCS_FinalColorLDR;
|
||
|
||
ViewCapture->bCaptureEveryFrame = false;
|
||
ViewCapture->bCaptureOnMovement = false;
|
||
|
||
ViewCapture->PostProcessBlendWeight = 1.0f;
|
||
|
||
ViewCapture->PostProcessSettings.bOverride_AutoExposureMethod = true;
|
||
ViewCapture->PostProcessSettings.AutoExposureMethod = EAutoExposureMethod::AEM_Manual;
|
||
|
||
ViewCapture->PostProcessSettings.bOverride_AutoExposureBias = true;
|
||
ViewCapture->PostProcessSettings.AutoExposureBias = 1.0f;
|
||
|
||
ViewCapture->SetRelativeLocation(FVector::ZeroVector);
|
||
ViewCapture->SetRelativeRotation(FRotator::ZeroRotator);
|
||
}
|
||
|
||
void ACameraCaptureActor::UpdateViewTransforms(const int32 Index)
|
||
{
|
||
// 以本 Actor(CameraActor)的 transform 为中心
|
||
const FVector Center = GetActorLocation();
|
||
const FRotator Rot = GetActorRotation();
|
||
const FVector Right = Rot.Quaternion().GetRightVector();
|
||
|
||
// FOV 从主 CameraComponent 取
|
||
float FOV = 90.f;
|
||
if (UCameraComponent* Cam = GetCameraComponent())
|
||
{
|
||
FOV = Cam->FieldOfView;
|
||
}
|
||
|
||
if (ViewCapture)
|
||
{
|
||
// 线性水平分布:居中对称
|
||
const float Offset = (Index - (ViewCount - 1) * 0.5f) * Baseline;
|
||
const FVector Pos = Center + Right * Offset;
|
||
|
||
ViewCapture->SetWorldLocationAndRotation(Pos, Rot);
|
||
ViewCapture->FOVAngle = FOV;
|
||
}
|
||
}
|
||
|
||
void ACameraCaptureActor::CaptureViews()
|
||
{
|
||
ViewCapture->CaptureScene();
|
||
}
|