Fix #334 - SceneCaptureCamera now names correctly in construction time and checks for the correctness of the rendertarget component in runtime

This commit is contained in:
CVC\jbelon 2018-04-12 12:54:48 +02:00
parent 819b1e7e8d
commit dc041d748a
3 changed files with 24 additions and 11 deletions

View File

@ -15,11 +15,10 @@
#include "Components/StaticMeshComponent.h" #include "Components/StaticMeshComponent.h"
#include "Engine/CollisionProfile.h" #include "Engine/CollisionProfile.h"
#include "Engine/TextureRenderTarget2D.h" #include "Engine/TextureRenderTarget2D.h"
#include "HighResScreenshot.h"
#include "Materials/Material.h" #include "Materials/Material.h"
#include "Paths.h"
#include "Kismet/KismetSystemLibrary.h" #include "Kismet/KismetSystemLibrary.h"
#include <memory> #include <memory>
#include "ConstructorHelpers.h"
static constexpr auto DEPTH_MAT_PATH = static constexpr auto DEPTH_MAT_PATH =
@ -36,6 +35,8 @@ static constexpr auto SEMANTIC_SEGMENTATION_MAT_PATH =
static void RemoveShowFlags(FEngineShowFlags &ShowFlags); static void RemoveShowFlags(FEngineShowFlags &ShowFlags);
uint32 ASceneCaptureCamera::NumSceneCapture = 0;
ASceneCaptureCamera::ASceneCaptureCamera(const FObjectInitializer& ObjectInitializer) : ASceneCaptureCamera::ASceneCaptureCamera(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer), Super(ObjectInitializer),
SizeX(720u), SizeX(720u),
@ -45,7 +46,7 @@ ASceneCaptureCamera::ASceneCaptureCamera(const FObjectInitializer& ObjectInitial
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.TickGroup = TG_PrePhysics; PrimaryActorTick.TickGroup = TG_PrePhysics;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CamMesh0")); MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CamMesh"));
MeshComp->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName); MeshComp->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
@ -54,11 +55,11 @@ ASceneCaptureCamera::ASceneCaptureCamera(const FObjectInitializer& ObjectInitial
MeshComp->PostPhysicsComponentTick.bCanEverTick = false; MeshComp->PostPhysicsComponentTick.bCanEverTick = false;
RootComponent = MeshComp; RootComponent = MeshComp;
DrawFrustum = CreateDefaultSubobject<UDrawFrustumComponent>(TEXT("DrawFrust0")); DrawFrustum = CreateDefaultSubobject<UDrawFrustumComponent>(TEXT("DrawFrust"));
DrawFrustum->bIsEditorOnly = true; DrawFrustum->bIsEditorOnly = true;
DrawFrustum->SetupAttachment(MeshComp); DrawFrustum->SetupAttachment(MeshComp);
CaptureRenderTarget = CreateDefaultSubobject<UTextureRenderTarget2D>(TEXT("CaptureRenderTarget0")); CaptureRenderTarget = CreateDefaultSubobject<UTextureRenderTarget2D>(FName(*FString::Printf(TEXT("CaptureRenderTarget%d"),NumSceneCapture)));
#if WITH_EDITORONLY_DATA #if WITH_EDITORONLY_DATA
CaptureRenderTarget->CompressionNoAlpha = true; CaptureRenderTarget->CompressionNoAlpha = true;
CaptureRenderTarget->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps; CaptureRenderTarget->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
@ -70,13 +71,14 @@ ASceneCaptureCamera::ASceneCaptureCamera(const FObjectInitializer& ObjectInitial
CaptureRenderTarget->AddressX = TextureAddress::TA_Clamp; CaptureRenderTarget->AddressX = TextureAddress::TA_Clamp;
CaptureRenderTarget->AddressY = TextureAddress::TA_Clamp; CaptureRenderTarget->AddressY = TextureAddress::TA_Clamp;
CaptureComponent2D = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCaptureComponent2D")); CaptureComponent2D = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCaptureComponent2D"));
CaptureComponent2D->SetupAttachment(MeshComp); CaptureComponent2D->SetupAttachment(MeshComp);
// Load post-processing materials. // Load post-processing materials.
static ConstructorHelpers::FObjectFinder<UMaterial> DEPTH(DEPTH_MAT_PATH); static ConstructorHelpers::FObjectFinder<UMaterial> DEPTH(DEPTH_MAT_PATH);
PostProcessDepth = DEPTH.Object; PostProcessDepth = DEPTH.Object;
static ConstructorHelpers::FObjectFinder<UMaterial> SEMANTIC_SEGMENTATION(SEMANTIC_SEGMENTATION_MAT_PATH); static ConstructorHelpers::FObjectFinder<UMaterial> SEMANTIC_SEGMENTATION(SEMANTIC_SEGMENTATION_MAT_PATH);
PostProcessSemanticSegmentation = SEMANTIC_SEGMENTATION.Object; PostProcessSemanticSegmentation = SEMANTIC_SEGMENTATION.Object;
NumSceneCapture++;
} }
void ASceneCaptureCamera::PostActorCreated() void ASceneCaptureCamera::PostActorCreated()
@ -109,7 +111,11 @@ void ASceneCaptureCamera::BeginPlay()
// Setup render target. // Setup render target.
const bool bInForceLinearGamma = bRemovePostProcessing; const bool bInForceLinearGamma = bRemovePostProcessing;
CaptureRenderTarget->InitCustomFormat(SizeX, SizeY, PF_B8G8R8A8, bInForceLinearGamma); CaptureRenderTarget->InitCustomFormat(SizeX, SizeY, PF_B8G8R8A8, bInForceLinearGamma);
if(!IsValid(CaptureComponent2D)||CaptureComponent2D->IsPendingKill())
{
CaptureComponent2D = NewObject<USceneCaptureComponent2D>(this,TEXT("SceneCaptureComponent2D"));
CaptureComponent2D->SetupAttachment(MeshComp);
}
CaptureComponent2D->Deactivate(); CaptureComponent2D->Deactivate();
CaptureComponent2D->TextureTarget = CaptureRenderTarget; CaptureComponent2D->TextureTarget = CaptureRenderTarget;
@ -163,6 +169,11 @@ void ASceneCaptureCamera::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
} }
void ASceneCaptureCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if(NumSceneCapture!=0) NumSceneCapture = 0;
}
void ASceneCaptureCamera::Tick(const float DeltaSeconds) void ASceneCaptureCamera::Tick(const float DeltaSeconds)
{ {
Super::Tick(DeltaSeconds); Super::Tick(DeltaSeconds);

View File

@ -37,7 +37,7 @@ protected:
public: public:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void Tick(float DeltaSeconds) override; virtual void Tick(float DeltaSeconds) override;
uint32 GetImageSizeX() const uint32 GetImageSizeX() const
@ -68,7 +68,9 @@ public:
void Set(const UCameraDescription &CameraDescription); void Set(const UCameraDescription &CameraDescription);
bool ReadPixels(TArray<FColor> &BitMap) const; bool ReadPixels(TArray<FColor> &BitMap) const;
protected:
static uint32 NumSceneCapture;
private: private:
///Read the camera buffer and write it to the client with no lock of the resources (for Vulkan API) ///Read the camera buffer and write it to the client with no lock of the resources (for Vulkan API)
@ -97,7 +99,7 @@ private:
UDrawFrustumComponent* DrawFrustum; UDrawFrustumComponent* DrawFrustum;
/** Render target necessary for scene capture */ /** Render target necessary for scene capture */
UPROPERTY(Transient) UPROPERTY()
UTextureRenderTarget2D* CaptureRenderTarget; UTextureRenderTarget2D* CaptureRenderTarget;
/** Scene capture component. */ /** Scene capture component. */

View File

@ -12,7 +12,7 @@
ASceneCaptureToDiskCamera::ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer) : ASceneCaptureToDiskCamera::ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer), Super(ObjectInitializer),
SaveToFolder(FPaths::Combine(FPaths::ProjectSavedDir(), "SceneCaptures")), SaveToFolder(*FPaths::Combine(FPaths::ProjectSavedDir(), TEXT("SceneCaptures"))),
FileName("capture_%05d.png") {} FileName("capture_%05d.png") {}
void ASceneCaptureToDiskCamera::BeginPlay() void ASceneCaptureToDiskCamera::BeginPlay()