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.h"
10#include "Engine/World.h"
11
13
14AInstanceSegmentationCamera::AInstanceSegmentationCamera(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
15{
16 // Use USceneCaptureComponent2D ReadPixels method to get pixels instead of FrameGrabber API.
17 // Otherwise ID's of instance segmentation are not correct
18 // due to sRGB conversion done by FrameGrabber.
19 UseParallelLateTick = false;
20 UseReadPixels = true;
21 PrimaryActorTick.bCanEverTick = false;
22}
23
24void AInstanceSegmentationCamera::Init(FCameraBaseParameters parameters, bool SimulateSensor)
25{
26 // Defined in Camera.h
27 CameraName = "InstanceSegmentationCamera ";
28 FilePrefix = "Data/InstanceSegmentationCamera_";
29
30 AddPostProcessingMaterial("/Game/Agrarsense/Materials/PostProcessingMaterials/M_InstanceSegmentation.M_InstanceSegmentation");
31 Super::Init(parameters, SimulateSensor);
32
33 SetShadowRendering(false);
35 SetTemporalAA(false);
36
37#ifdef InstanceSegmentationPass_EXISTS
38 // If user is using AGRARSENSE Unreal Engine fork, we can utilize this sensor
39 UWorld* World = GetWorld();
40 if (GEngine && World)
41 {
42 GEngine->Exec(World, TEXT("r.EnableSegmentationRendering 1"));
43 }
45#else
46 // else log and destroy this Actor as the user cannot use this sensor anyway.
47 SimulatorLog::Log("InstanceSegmentationCamera.cpp: This sensor is only supported with our AGRARSENSE fork of the engine.");
48 Destroy();
49#endif
50}
51
52void AInstanceSegmentationCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
53{
54 Super::EndPlay(EndPlayReason);
55
56#ifdef InstanceSegmentationPass_EXISTS
59 {
60 UWorld* World = GetWorld();
61 if (GEngine && World)
62 {
63 GEngine->Exec(World, TEXT("r.EnableSegmentationRendering 0"));
64 }
65 }
66#endif
67}
static int32 InstanceSegmentationCameraCount
bool UseParallelLateTick
Definition: Camera.h:235
void AddPostProcessingMaterial(const FString &Path, float Weight=1.0f)
Definition: Camera.cpp:62
bool UseReadPixels
Definition: Camera.h:311
FString FilePrefix
Definition: Camera.h:286
void SetShadowRendering(bool RenderShadows)
Definition: Camera.cpp:930
void SetTemporalAA(bool SetTemporal)
Definition: Camera.cpp:941
FString CameraName
Definition: Camera.h:284
void DisableShowFlags()
Definition: Camera.cpp:970
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)