Fix depth shader for Windows and improve SceneCaptureToDiskCamera

This commit is contained in:
nsubiron 2017-06-22 11:53:05 +01:00
parent c2af87589c
commit 8818980297
4 changed files with 50 additions and 218 deletions

View File

@ -66,13 +66,13 @@ private:
/// SceneCaptureComponent2D settings. /// SceneCaptureComponent2D settings.
void UpdateDrawFrustum(); void UpdateDrawFrustum();
UPROPERTY(Category = SceneCapture, EditAnywhere) UPROPERTY(Category = "Scene Capture", EditAnywhere)
uint32 SizeX; uint32 SizeX;
UPROPERTY(Category = SceneCapture, EditAnywhere) UPROPERTY(Category = "Scene Capture", EditAnywhere)
uint32 SizeY; uint32 SizeY;
UPROPERTY(Category = SceneCapture, EditAnywhere) UPROPERTY(Category = "Scene Capture", EditAnywhere)
EPostProcessEffect PostProcessEffect; EPostProcessEffect PostProcessEffect;
/** To display the 3d camera in the editor. */ /** To display the 3d camera in the editor. */

View File

@ -1,169 +1,49 @@
// Fill out your copyright notice in the Description page of Project Settings. // CARLA, Copyright (C) 2017 Computer Vision Center (CVC) Project Settings.
#include "Carla.h" #include "Carla.h"
#include "SceneCaptureToDiskCamera.h" #include "SceneCaptureToDiskCamera.h"
#include "Components/DrawFrustumComponent.h"
#include "Components/SceneCaptureComponent2D.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/CollisionProfile.h"
#include "Engine/TextureRenderTarget2D.h"
#include "HighResScreenshot.h" #include "HighResScreenshot.h"
#include "Paths.h" #include "Paths.h"
#include "StaticMeshResources.h"
#include "TextureResource.h"
// -- Static methods -----------------------------------------------------------
static void SaveRenderTargetToDisk(UTextureRenderTarget2D* InRenderTarget, FString Filename)
{
FTextureRenderTargetResource* RTResource = InRenderTarget->GameThread_GetRenderTargetResource();
FReadSurfaceDataFlags ReadPixelFlags(RCM_UNorm);
ReadPixelFlags.SetLinearToGamma(true);
TArray<FColor> OutBMP;
RTResource->ReadPixels(OutBMP, ReadPixelFlags);
for (FColor& color : OutBMP)
{
color.A = 255;
}
FIntPoint DestSize(InRenderTarget->GetSurfaceWidth(), InRenderTarget->GetSurfaceHeight());
FString ResultPath;
FHighResScreenshotConfig& HighResScreenshotConfig = GetHighResScreenshotConfig();
HighResScreenshotConfig.SaveImage(Filename, OutBMP, DestSize, &ResultPath);
}
// -- ASceneCaptureToDiskCamera ------------------------------------------------------
ASceneCaptureToDiskCamera::ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer) : ASceneCaptureToDiskCamera::ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer), Super(ObjectInitializer),
SaveToFolder(FPlatformProcess::UserDir()), SaveToFolder(FPaths::Combine(FPaths::GameSavedDir(), "SceneCaptures")),
FileName("capture_%05d.png"), FileName("capture_%05d.png") {}
CapturesPerSecond(15.0f),
SizeX(200u),
SizeY(200u)
{
PrimaryActorTick.bCanEverTick = true;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CamMesh0"));
MeshComp->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
MeshComp->bHiddenInGame = true;
MeshComp->CastShadow = false;
MeshComp->PostPhysicsComponentTick.bCanEverTick = false;
RootComponent = MeshComp;
DrawFrustum = CreateDefaultSubobject<UDrawFrustumComponent>(TEXT("DrawFrust0"));
DrawFrustum->bIsEditorOnly = true;
DrawFrustum->SetupAttachment(GetMeshComp());
CaptureRenderTarget = CreateDefaultSubobject<UTextureRenderTarget2D>(TEXT("CaptureRenderTarget0"));
CaptureComponent2D = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCaptureComponent2D"));
CaptureComponent2D->SetupAttachment(GetMeshComp());
}
ASceneCaptureToDiskCamera::~ASceneCaptureToDiskCamera() {}
void ASceneCaptureToDiskCamera::OnInterpToggle(bool bEnable)
{
CaptureComponent2D->SetVisibility(bEnable);
}
void ASceneCaptureToDiskCamera::UpdateDrawFrustum()
{
if(DrawFrustum && CaptureComponent2D)
{
DrawFrustum->FrustumStartDist = GNearClippingPlane;
// 1000 is the default frustum distance, ideally this would be infinite but that might cause rendering issues
DrawFrustum->FrustumEndDist = (CaptureComponent2D->MaxViewDistanceOverride > DrawFrustum->FrustumStartDist)
? CaptureComponent2D->MaxViewDistanceOverride : 1000.0f;
DrawFrustum->FrustumAngle = CaptureComponent2D->FOVAngle;
//DrawFrustum->FrustumAspectRatio = CaptureComponent2D->AspectRatio;
}
}
void ASceneCaptureToDiskCamera::PostActorCreated()
{
Super::PostActorCreated();
// no need load the editor mesh when there is no editor
#if WITH_EDITOR
if(GetMeshComp())
{
if (!IsRunningCommandlet())
{
if( !GetMeshComp()->GetStaticMesh())
{
UStaticMesh* CamMesh = LoadObject<UStaticMesh>(NULL, TEXT("/Engine/EditorMeshes/MatineeCam_SM.MatineeCam_SM"), NULL, LOAD_None, NULL);
GetMeshComp()->SetStaticMesh(CamMesh);
}
}
}
#endif // WITH_EDITOR
// Sync component with CameraActor frustum settings.
UpdateDrawFrustum();
}
void ASceneCaptureToDiskCamera::BeginPlay() void ASceneCaptureToDiskCamera::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
CaptureRenderTarget->InitCustomFormat(SizeX, SizeY, PF_B8G8R8A8, false); PrimaryActorTick.bCanEverTick = bCaptureScene;
CaptureRenderTarget->UpdateResource(); PrimaryActorTick.TickInterval = 1.0f / CapturesPerSecond;
CaptureComponent2D->Deactivate(); CaptureFileNameCount = 0u;
CaptureComponent2D->TextureTarget = CaptureRenderTarget;
CaptureComponent2D->UpdateContent();
CaptureComponent2D->Activate();
} }
void ASceneCaptureToDiskCamera::Tick(float Delta) void ASceneCaptureToDiskCamera::Tick(const float DeltaTime)
{ {
Super::Tick(Delta); Super::Tick(DeltaTime);
const float CaptureUpdateTime = 1.0f / CapturesPerSecond; if (bCaptureScene) {
ElapsedTimeSinceLastCapture += Delta; const FString FilePath = FPaths::Combine(SaveToFolder, FString::Printf(*FileName, CaptureFileNameCount));
UE_LOG(LogCarla, Log, TEXT("DeltaTime %fs: Capture %s"), DeltaTime, *FilePath);
if (bCaptureScene && (ElapsedTimeSinceLastCapture >= CaptureUpdateTime)) SaveCaptureToDisk(FilePath);
{ ++CaptureFileNameCount;
if (CaptureComponent2D == nullptr)
{
UE_LOG(LogCarla, Error, TEXT("Missing capture component"));
}
if (CaptureRenderTarget)
{
// Capture scene.
FString FilePath = FPaths::Combine(SaveToFolder, FString::Printf(*FileName, CaptureFileNameCount));
//UE_LOG(LogCarla, Log, TEXT("Delta %fs: Capture %s"), ElapsedTimeSinceLastCapture, *FilePath);
SaveRenderTargetToDisk(CaptureRenderTarget, FilePath);
++CaptureFileNameCount;
}
else
{
UE_LOG(LogCarla, Error, TEXT("Missing render target"));
}
ElapsedTimeSinceLastCapture = 0.0f;
} }
} }
UStaticMeshComponent* ASceneCaptureToDiskCamera::GetMeshComp() const bool ASceneCaptureToDiskCamera::SaveCaptureToDisk(const FString &FilePath) const
{ {
return MeshComp; TArray<FColor> OutBMP;
} if (!ReadPixels(OutBMP)) {
return false;
USceneCaptureComponent2D* ASceneCaptureToDiskCamera::GetCaptureComponent2D() const }
{ for (FColor &color : OutBMP) {
return CaptureComponent2D; color.A = 255;
} }
const FIntPoint DestSize(GetImageSizeX(), GetImageSizeY());
UDrawFrustumComponent* ASceneCaptureToDiskCamera::GetDrawFrustum() const FString ResultPath;
{ FHighResScreenshotConfig &HighResScreenshotConfig = GetHighResScreenshotConfig();
return DrawFrustum; return HighResScreenshotConfig.SaveImage(FilePath, OutBMP, DestSize, &ResultPath);
} }

View File

@ -1,90 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings. // CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
/**
* Own SceneCapture, re-implementing some of the methods since ASceneCapture
* cannot be subclassed.
*/
#pragma once #pragma once
#include "GameFramework/Actor.h" #include "SceneCaptureCamera.h"
#include "StaticMeshResources.h"
#include "SceneCaptureToDiskCamera.generated.h" #include "SceneCaptureToDiskCamera.generated.h"
class UDrawFrustumComponent; UCLASS(Blueprintable,BlueprintType)
class USceneCaptureComponent2D; class CARLA_API ASceneCaptureToDiskCamera : public ASceneCaptureCamera
class UStaticMeshComponent;
UCLASS(hidecategories=(Collision, Material, Attachment, Actor))
class CARLA_API ASceneCaptureToDiskCamera : public AActor
{ {
GENERATED_BODY() GENERATED_BODY()
float ElapsedTimeSinceLastCapture = 0.0f;
size_t CaptureFileNameCount = 0u;
public:
UPROPERTY(Category = SceneCapture, EditAnywhere)
bool bCaptureScene = true;
UPROPERTY(Category = SceneCapture, EditAnywhere, meta = (EditCondition = bCaptureScene))
FString SaveToFolder;
UPROPERTY(Category = SceneCapture, EditAnywhere, meta = (EditCondition = bCaptureScene))
FString FileName;
UPROPERTY(Category = SceneCapture, EditAnywhere, meta = (EditCondition = bCaptureScene))
float CapturesPerSecond;
UPROPERTY(Category = SceneCapture, EditAnywhere, meta = (EditCondition = bCaptureScene))
uint32 SizeX;
UPROPERTY(Category = SceneCapture, EditAnywhere, meta = (EditCondition = bCaptureScene))
uint32 SizeY;
private:
/** To display the 3d camera in the editor. */
UPROPERTY()
class UStaticMeshComponent* MeshComp;
/** To allow drawing the camera frustum in the editor. */
UPROPERTY()
class UDrawFrustumComponent* DrawFrustum;
/** Render target necessary for scene capture */
UPROPERTY(Transient)
class UTextureRenderTarget2D* CaptureRenderTarget;
/** Scene capture component. */
UPROPERTY(Transient)
class USceneCaptureComponent2D* CaptureComponent2D;
public: public:
ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer); ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer);
~ASceneCaptureToDiskCamera();
virtual void PostActorCreated() override;
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void Tick(float Delta) override; virtual void Tick(float DeltaTime) override;
/** Used to synchronize the DrawFrustumComponent with the SceneCaptureComponent2D settings. */ UFUNCTION(BlueprintCallable)
void UpdateDrawFrustum(); bool SaveCaptureToDisk(const FString &FilePath) const;
UFUNCTION(BlueprintCallable, Category="Rendering") public:
void OnInterpToggle(bool bEnable);
/** Returns CaptureComponent2D subobject **/ UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite)
USceneCaptureComponent2D* GetCaptureComponent2D() const; bool bCaptureScene = true;
/** Returns DrawFrustum subobject **/ UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
UDrawFrustumComponent* GetDrawFrustum() const; FString SaveToFolder;
/** Returns MeshComp subobject **/ UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
UStaticMeshComponent* GetMeshComp() const; FString FileName;
UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
float CapturesPerSecond = 10.0f;
private:
UPROPERTY()
uint32 CaptureFileNameCount;
}; };