98 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "CameraCaptureActor.h"
#include "Components/SceneCaptureComponent2D.h"
#include "Camera/CameraComponent.h"
#include "Engine/TextureRenderTarget2D.h"
ACameraCaptureActor::ACameraCaptureActor()
{
// PrimaryActorTick.bCanEverTick = false;
}
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;
}
if (ViewRT)
{
ViewRT = nullptr;
}
ViewRT = NewObject<UTextureRenderTarget2D>(this);
ViewRT->ClearColor = FLinearColor::Black;
ViewRT->InitAutoFormat(ViewWidth, ViewHeight);
ViewRT->UpdateResourceImmediate(true);
ViewCapture = NewObject<USceneCaptureComponent2D>(this, TEXT("ViewCapture"));
ViewCapture->SetupAttachment(GetRootComponent());
ViewCapture->RegisterComponent();
ViewCapture->TextureTarget = ViewRT;
ViewCapture->CaptureSource = ESceneCaptureSource::SCS_FinalColorHDR;
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)
{
// 以本 ActorCameraActor的 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();
}