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#include "Materials/Material.h"
8
9ADepthCamera::ADepthCamera(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
10{
11 // Ticked by Camera base class
12 PrimaryActorTick.bCanEverTick = false;
13}
14
15void ADepthCamera::DepthInit(FDepthCameraParameters Parameters, bool SimulateSensor)
16{
17 DepthCameraParameters = Parameters;
19}
20
21void ADepthCamera::Init(FCameraBaseParameters parameters, bool SimulateSensor)
22{
23 // Set directory and camera name, defined in Camera.h
24 CameraName = "DepthCamera ";
25 FilePrefix = "Data/DepthCamera_";
26
27 // Setup Depth material
29
30 // Call Camera base class Init
31 Super::Init(parameters, SimulateSensor);
32
33 // This camera doesn't need to render shadows
34 SetShadowRendering(false);
35}
36
37void ADepthCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
38{
39 Super::EndPlay(EndPlayReason);
40
42 DepthEffectMaterial.Reset();
43}
44
46{
47 DepthCameraParameters = Parameters;
50}
51
52void ADepthCamera::SetupDepthMaterial(const bool UseGrayscale)
53{
54 // Get SceneCaptureComponent2D from the base Camera class
55 USceneCaptureComponent2D* SceneCaptureComponent = GetCaptureComponent2D();
56
57 if (SceneCaptureComponent)
58 {
59 // Determine which post-processing material to apply based on whether grayscale is used
60 if (UseGrayscale)
61 {
62 // Load and apply the grayscale material if it hasn't been loaded already
63 if (!DepthEffectMaterialGrayscale.IsValid())
64 {
65 FString Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterialGrayscale.DepthEffectMaterialGrayscale";
66 DepthEffectMaterialGrayscale = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *Path));
67 AddPostProcessingMaterial(Path, 1.0f);
68 }
69
70 // Remove the non-grayscale material if it's currently applied
71 if (DepthEffectMaterial.IsValid())
72 {
74 DepthEffectMaterial.Reset();
75 }
76 }
77 else
78 {
79 // Load and apply the non-grayscale material if it hasn't been loaded already
80 if (!DepthEffectMaterial.IsValid())
81 {
82 FString Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/DepthEffectMaterial.DepthEffectMaterial";
83 DepthEffectMaterial = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *Path));
84 AddPostProcessingMaterial(Path, 1.0f);
85 }
86
87 // Remove the grayscale material if it's currently applied
88 if (DepthEffectMaterialGrayscale.IsValid())
89 {
92 }
93 }
94 }
95}
void AddPostProcessingMaterial(const FString &Path, float Weight=1.0f)
Definition: Camera.cpp:55
void ChangeCameraParameters(FCameraBaseParameters newParameters)
Definition: Camera.cpp:50
void RemovePostProcessingMaterial(UMaterial *Material)
Definition: Camera.cpp:71
FString FilePrefix
Definition: Camera.h:264
void SetShadowRendering(bool RenderShadows)
Definition: Camera.cpp:730
FString CameraName
Definition: Camera.h:262
USceneCaptureComponent2D * GetCaptureComponent2D() const
Definition: Camera.h:113
void DepthInit(FDepthCameraParameters Parameters, bool SimulateSensor=true)
Definition: DepthCamera.cpp:15
void ChangeDepthCameraParameters(FDepthCameraParameters Parameters)
Definition: DepthCamera.cpp:45
TWeakObjectPtr< UMaterial > DepthEffectMaterialGrayscale
Definition: DepthCamera.h:91
void SetupDepthMaterial(const bool UseGrayscale)
Definition: DepthCamera.cpp:52
ADepthCamera(const FObjectInitializer &ObjectInitializer)
Definition: DepthCamera.cpp:9
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: DepthCamera.cpp:37
TWeakObjectPtr< UMaterial > DepthEffectMaterial
Definition: DepthCamera.h:93
FDepthCameraParameters DepthCameraParameters
Definition: DepthCamera.h:89
void Init(FCameraBaseParameters parameters, bool SimulateSensor=true) override
Definition: DepthCamera.cpp:21
FCameraBaseParameters CameraParameters