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.
void UpdateDrawFrustum();
UPROPERTY(Category = SceneCapture, EditAnywhere)
UPROPERTY(Category = "Scene Capture", EditAnywhere)
uint32 SizeX;
UPROPERTY(Category = SceneCapture, EditAnywhere)
UPROPERTY(Category = "Scene Capture", EditAnywhere)
uint32 SizeY;
UPROPERTY(Category = SceneCapture, EditAnywhere)
UPROPERTY(Category = "Scene Capture", EditAnywhere)
EPostProcessEffect PostProcessEffect;
/** 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 "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 "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) :
Super(ObjectInitializer),
SaveToFolder(FPlatformProcess::UserDir()),
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();
}
SaveToFolder(FPaths::Combine(FPaths::GameSavedDir(), "SceneCaptures")),
FileName("capture_%05d.png") {}
void ASceneCaptureToDiskCamera::BeginPlay()
{
Super::BeginPlay();
CaptureRenderTarget->InitCustomFormat(SizeX, SizeY, PF_B8G8R8A8, false);
CaptureRenderTarget->UpdateResource();
PrimaryActorTick.bCanEverTick = bCaptureScene;
PrimaryActorTick.TickInterval = 1.0f / CapturesPerSecond;
CaptureComponent2D->Deactivate();
CaptureComponent2D->TextureTarget = CaptureRenderTarget;
CaptureComponent2D->UpdateContent();
CaptureComponent2D->Activate();
CaptureFileNameCount = 0u;
}
void ASceneCaptureToDiskCamera::Tick(float Delta)
void ASceneCaptureToDiskCamera::Tick(const float DeltaTime)
{
Super::Tick(Delta);
Super::Tick(DeltaTime);
const float CaptureUpdateTime = 1.0f / CapturesPerSecond;
ElapsedTimeSinceLastCapture += Delta;
if (bCaptureScene && (ElapsedTimeSinceLastCapture >= CaptureUpdateTime))
{
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;
if (bCaptureScene) {
const FString FilePath = FPaths::Combine(SaveToFolder, FString::Printf(*FileName, CaptureFileNameCount));
UE_LOG(LogCarla, Log, TEXT("DeltaTime %fs: Capture %s"), DeltaTime, *FilePath);
SaveCaptureToDisk(FilePath);
++CaptureFileNameCount;
}
}
UStaticMeshComponent* ASceneCaptureToDiskCamera::GetMeshComp() const
bool ASceneCaptureToDiskCamera::SaveCaptureToDisk(const FString &FilePath) const
{
return MeshComp;
}
USceneCaptureComponent2D* ASceneCaptureToDiskCamera::GetCaptureComponent2D() const
{
return CaptureComponent2D;
}
UDrawFrustumComponent* ASceneCaptureToDiskCamera::GetDrawFrustum() const
{
return DrawFrustum;
TArray<FColor> OutBMP;
if (!ReadPixels(OutBMP)) {
return false;
}
for (FColor &color : OutBMP) {
color.A = 255;
}
const FIntPoint DestSize(GetImageSizeX(), GetImageSizeY());
FString ResultPath;
FHighResScreenshotConfig &HighResScreenshotConfig = GetHighResScreenshotConfig();
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.
/**
* Own SceneCapture, re-implementing some of the methods since ASceneCapture
* cannot be subclassed.
*/
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#pragma once
#include "GameFramework/Actor.h"
#include "StaticMeshResources.h"
#include "SceneCaptureCamera.h"
#include "SceneCaptureToDiskCamera.generated.h"
class UDrawFrustumComponent;
class USceneCaptureComponent2D;
class UStaticMeshComponent;
UCLASS(hidecategories=(Collision, Material, Attachment, Actor))
class CARLA_API ASceneCaptureToDiskCamera : public AActor
UCLASS(Blueprintable,BlueprintType)
class CARLA_API ASceneCaptureToDiskCamera : public ASceneCaptureCamera
{
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:
ASceneCaptureToDiskCamera(const FObjectInitializer& ObjectInitializer);
~ASceneCaptureToDiskCamera();
virtual void PostActorCreated() 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. */
void UpdateDrawFrustum();
UFUNCTION(BlueprintCallable)
bool SaveCaptureToDisk(const FString &FilePath) const;
UFUNCTION(BlueprintCallable, Category="Rendering")
void OnInterpToggle(bool bEnable);
public:
/** Returns CaptureComponent2D subobject **/
USceneCaptureComponent2D* GetCaptureComponent2D() const;
UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite)
bool bCaptureScene = true;
/** Returns DrawFrustum subobject **/
UDrawFrustumComponent* GetDrawFrustum() const;
UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
FString SaveToFolder;
/** Returns MeshComp subobject **/
UStaticMeshComponent* GetMeshComp() const;
UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
FString FileName;
UPROPERTY(Category = "Scene Capture", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bCaptureScene))
float CapturesPerSecond = 10.0f;
private:
UPROPERTY()
uint32 CaptureFileNameCount;
};