From bf886973743fb83e343d462fcc334e6d1d86fdc9 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Sat, 29 Sep 2018 14:24:15 +0200 Subject: [PATCH] Add debugging methods to scene capture sensor --- .../Carla/Sensor/SceneCaptureSensor.cpp | 41 +++++++++++++++++++ .../Source/Carla/Sensor/SceneCaptureSensor.h | 14 ++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp index 2a20e9f6d..7c6f554c3 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp @@ -7,6 +7,12 @@ #include "Carla.h" #include "Carla/Sensor/SceneCaptureSensor.h" +#include "Components/DrawFrustumComponent.h" +#include "Components/SceneCaptureComponent2D.h" +#include "Components/StaticMeshComponent.h" +#include "Engine/TextureRenderTarget2D.h" +#include "HighResScreenshot.h" + static auto SCENE_CAPTURE_COUNTER = 0u; // ============================================================================= @@ -87,6 +93,41 @@ float ASceneCaptureSensor::GetFOVAngle() const return CaptureComponent2D->FOVAngle; } +bool ASceneCaptureSensor::ReadPixels_GameThread(TArray &BitMap) const +{ + check(IsInGameThread()); + if (!CaptureRenderTarget) + { + UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target")); + return false; + } + FTextureRenderTargetResource *RTResource = + CaptureRenderTarget->GameThread_GetRenderTargetResource(); + if (RTResource == nullptr) + { + UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target")); + return false; + } + FReadSurfaceDataFlags ReadPixelFlags(RCM_UNorm); + ReadPixelFlags.SetLinearToGamma(true); + return RTResource->ReadPixels(BitMap, ReadPixelFlags); +} + +bool ASceneCaptureSensor::SaveCaptureToDisk(const FString &FilePath) const +{ + TArray OutBMP; + if (!ReadPixels_GameThread(OutBMP)) { + return false; + } + for (FColor &color : OutBMP) { + color.A = 255u; + } + const FIntPoint DestSize(GetImageWidth(), GetImageHeight()); + FString ResultPath; + FHighResScreenshotConfig &HighResScreenshotConfig = GetHighResScreenshotConfig(); + return HighResScreenshotConfig.SaveImage(FilePath, OutBMP, DestSize, &ResultPath); +} + void ASceneCaptureSensor::PostActorCreated() { Super::PostActorCreated(); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h index 5b05df0a1..e411702ee 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h @@ -22,14 +22,14 @@ class UTextureRenderTarget2D; /// SetUpSceneCaptureComponent function. /// /// @warning All the setters should be called before BeginPlay. -UCLASS() +UCLASS(Abstract) class CARLA_API ASceneCaptureSensor : public ASensor { GENERATED_BODY() public: - ASceneCaptureSensor(const FObjectInitializer& ObjectInitializer); + ASceneCaptureSensor(const FObjectInitializer &ObjectInitializer); void SetImageSize(uint32 Width, uint32 Height); @@ -43,11 +43,15 @@ public: return ImageHeight; } +protected: + void EnablePostProcessingEffects(bool Enable = true) { bEnablePostProcessingEffects = Enable; } +public: + bool ArePostProcessingEffectsEnabled() const { return bEnablePostProcessingEffects; @@ -57,6 +61,12 @@ public: float GetFOVAngle() const; + /// Use only for debugging purposes. + bool ReadPixels_GameThread(TArray &BitMap) const; + + /// Use only for debugging purposes. + bool SaveCaptureToDisk(const FString &FilePath) const; + protected: virtual void PostActorCreated() override;