Agrarsense
InstanceSegmentationCamera.cpp
Go to the documentation of this file.
1// Copyright (c) 2025 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
8
9#include "Engine/World.h"
10
12
13AInstanceSegmentationCamera::AInstanceSegmentationCamera(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
14{
15 PrimaryActorTick.bCanEverTick = false;
16}
17
18void AInstanceSegmentationCamera::Init(FCameraBaseParameters parameters, bool SimulateSensor)
19{
20 // Defined in Camera.h
21 CameraName = "InstanceSegmentationCamera ";
22 FilePrefix = "Data/InstanceSegmentationCamera_";
23
24 AddPostProcessingMaterial("/Game/Agrarsense/Materials/PostProcessingMaterials/M_InstanceSegmentation.M_InstanceSegmentation");
25 Super::Init(parameters, SimulateSensor);
26
27 // This camera doesn't need to render shadows
28 SetShadowRendering(false);
29
30 // Or many other advanced show flags.
32
33#ifdef InstanceSegmentationPass_EXISTS
34 // If user is using AGRARSENSE Unreal Engine fork, we can utilize this sensor
35 UWorld* World = GetWorld();
36 if (GEngine && World)
37 {
38 GEngine->Exec(World, TEXT("r.EnableSegmentationRendering 1"));
39 }
41#else
42 // else log and destroy this Actor as the user cannot use this sensor anyway.
43 SimulatorLog::Log("InstanceSegmentationCamera.cpp: This sensor is only supported with our AGRARSENSE fork of the engine.");
44 Destroy();
45#endif
46}
47
48void AInstanceSegmentationCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
49{
50 Super::EndPlay(EndPlayReason);
51
52#ifdef InstanceSegmentationPass_EXISTS
55 {
56 UWorld* World = GetWorld();
57 if (GEngine && World)
58 {
59 GEngine->Exec(World, TEXT("r.EnableSegmentationRendering 0"));
60 }
61 }
62#endif
63}
64
66{
67 // Old way
68 //FColor* BufferPtr = buffer.GetData();
69
70 //for (int32 i = 0; i < buffer.Num(); ++i)
71 //{
72 // // Convert the current FColor (sRGB 0-255) to FLinearColor (linear 0.0-1.0)
73 // FLinearColor pixel = FLinearColor::FromSRGBColor(BufferPtr[i]);
74
75 // // Process the pixel and convert it back to FColor (sRGB 0-255)
76 // // This step applies gamma correction and ensures values are properly adjusted
77 // BufferPtr[i] = FColor(
78 // FMath::RoundToInt(pixel.R * 255.0f),
79 // FMath::RoundToInt(pixel.G * 255.0f),
80 // FMath::RoundToInt(pixel.B * 255.0f));
81 //}
82
84}
static int32 InstanceSegmentationCameraCount
void AddPostProcessingMaterial(const FString &Path, float Weight=1.0f)
Definition: Camera.cpp:60
void ApplyGammaCorrectionLUT(TArray< FColor > &Buffer)
Definition: Camera.cpp:600
FString FilePrefix
Definition: Camera.h:281
void SetShadowRendering(bool RenderShadows)
Definition: Camera.cpp:849
FString CameraName
Definition: Camera.h:279
void DisableShowFlags()
Definition: Camera.cpp:889
virtual void AddProcessingToFrameBuffer(TArray< FColor > &buffer) final override
void Init(FCameraBaseParameters parameters, bool SimulateSensor=true) override
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) final override
AInstanceSegmentationCamera(const FObjectInitializer &ObjectInitializer)
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)