Agrarsense
DepthCamera.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 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
6#include "DepthCamera.h"
7
8#include "Materials/Material.h"
9#include "Math/UnrealMathUtility.h"
10
11ADepthCamera::ADepthCamera(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
12{
13 // Ticked by Camera base class
14 PrimaryActorTick.bCanEverTick = false;
15}
16
17void ADepthCamera::DepthInit(FDepthCameraParameters Parameters, bool SimulateSensor)
18{
19 DepthCameraParameters = Parameters;
21}
22
23void ADepthCamera::Init(FCameraBaseParameters parameters, bool SimulateSensor)
24{
25 // Set directory and camera name, defined in Camera.h
26 CameraName = "DepthCamera ";
27 FilePrefix = "Data/DepthCamera_";
28
29 // Setup Depth material
31
32 // Call Camera base class Init
33 Super::Init(parameters, SimulateSensor);
34
35 // This camera doesn't need to render shadows
36 SetShadowRendering(false);
37
38 // Or many other advanced show flags.
40}
41
42void ADepthCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
43{
44 Super::EndPlay(EndPlayReason);
45
47 DepthEffectMaterial.Reset();
48}
49
51{
52 DepthCameraParameters = Parameters;
55}
56
57void ADepthCamera::AddProcessingToFrameBuffer(TArray<FColor>& buffer)
58{
59 // TODO, can we avoid this altogether?
60
61 // Get the raw buffer
62 FColor* BufferPtr = buffer.GetData();
63
64 for (size_t i = 0; i < buffer.Num(); ++i)
65 {
66 // Convert the current FColor (sRGB 0-255) to FLinearColor (linear 0.0-1.0)
67 FLinearColor pixel = FLinearColor::FromSRGBColor(BufferPtr[i]);
68
69 // Process the pixel and convert it back to FColor (sRGB 0-255)
70 // This step applies gamma correction and ensures values are properly adjusted
71 BufferPtr[i] = FColor(
72 FMath::RoundToInt(pixel.R * 255.0f),
73 FMath::RoundToInt(pixel.G * 255.0f),
74 FMath::RoundToInt(pixel.B * 255.0f));
75 }
76}
77
78void ADepthCamera::SetupDepthMaterial(const bool UseGrayscale)
79{
80 // Get SceneCaptureComponent2D from the base Camera class
81 USceneCaptureComponent2D* SceneCaptureComponent = GetCaptureComponent2D();
82
83 if (SceneCaptureComponent)
84 {
85 // Determine which post-processing material to apply based on whether grayscale is used
86 if (UseGrayscale)
87 {
88 // Load and apply the grayscale material if it hasn't been loaded already
89 if (!DepthEffectMaterialGrayscale.IsValid())
90 {
91
92 FString Path;
93#if PLATFORM_LINUX
94 Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterialGrayscale_Alt.DepthEffectMaterialGrayscale_Alt";
95#else
96 Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterialGrayscale.DepthEffectMaterialGrayscale";
97#endif
98 DepthEffectMaterialGrayscale = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *Path));
99 AddPostProcessingMaterial(Path, 1.0f);
100 }
101
102 // Remove the non-grayscale material if it's currently applied
103 if (DepthEffectMaterial.IsValid())
104 {
106 DepthEffectMaterial.Reset();
107 }
108 }
109 else
110 {
111 // Load and apply the non-grayscale material if it hasn't been loaded already
112 if (!DepthEffectMaterial.IsValid())
113 {
114 FString Path;
115#if PLATFORM_LINUX
116 Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterial_Alt.DepthEffectMaterial_Alt";
117#else
118 Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterial.DepthEffectMaterial";
119#endif
120 DepthEffectMaterial = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *Path));
121 AddPostProcessingMaterial(Path, 1.0f);
122 }
123
124 // Remove the grayscale material if it's currently applied
125 if (DepthEffectMaterialGrayscale.IsValid())
126 {
129 }
130 }
131 }
132}
void AddPostProcessingMaterial(const FString &Path, float Weight=1.0f)
Definition: Camera.cpp:60
void ChangeCameraParameters(FCameraBaseParameters newParameters)
Definition: Camera.cpp:55
void RemovePostProcessingMaterial(UMaterial *Material)
Definition: Camera.cpp:76
FString FilePrefix
Definition: Camera.h:276
void SetShadowRendering(bool RenderShadows)
Definition: Camera.cpp:810
FString CameraName
Definition: Camera.h:274
USceneCaptureComponent2D * GetCaptureComponent2D() const
Definition: Camera.h:114
void DisableShowFlags()
Definition: Camera.cpp:842
void DepthInit(FDepthCameraParameters Parameters, bool SimulateSensor=true)
Definition: DepthCamera.cpp:17
void ChangeDepthCameraParameters(FDepthCameraParameters Parameters)
Definition: DepthCamera.cpp:50
TWeakObjectPtr< UMaterial > DepthEffectMaterialGrayscale
Definition: DepthCamera.h:97
void SetupDepthMaterial(const bool UseGrayscale)
Definition: DepthCamera.cpp:78
virtual void AddProcessingToFrameBuffer(TArray< FColor > &buffer) override
Definition: DepthCamera.cpp:57
ADepthCamera(const FObjectInitializer &ObjectInitializer)
Definition: DepthCamera.cpp:11
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: DepthCamera.cpp:42
TWeakObjectPtr< UMaterial > DepthEffectMaterial
Definition: DepthCamera.h:99
FDepthCameraParameters DepthCameraParameters
Definition: DepthCamera.h:95
void Init(FCameraBaseParameters parameters, bool SimulateSensor=true) override
Definition: DepthCamera.cpp:23
FCameraBaseParameters CameraParameters