Agrarsense
ThermalCamera.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 "ThermalCamera.h"
7
8#include "Components/SceneCaptureComponent2D.h"
9#include "Materials/Material.h"
10
11AThermalCamera::AThermalCamera(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
12{
13 PrimaryActorTick.bCanEverTick = false;
14}
15
16void AThermalCamera::ThermalInit(FThermalCameraParameters Parameters, bool SimulateSensor)
17{
18 ThermalCameraParameters = Parameters;
20}
21
22void AThermalCamera::Init(FCameraBaseParameters parameters, bool SimulateSensor)
23{
24 // Defined in Camera.h
25 CameraName = "ThermalCamera ";
26 FilePrefix = "Data/ThermalCamera_";
27
29
30 // Call Camera base class Init
31 Super::Init(parameters, SimulateSensor);
32
33 SetShadowRendering(false);
34}
35
36void AThermalCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
37{
38 Super::EndPlay(EndPlayReason);
39
40 ThermalMaterial.Reset();
41}
42
44{
45 // Set new Thermal camera parameters
46 ThermalCameraParameters = Parameters;
47
48 // Update material
50
51 // Notify Camera base class to change FCameraBaseParameters
53}
54
56{
57 // Get SceneCaptureComponent2D from the base Camera class
58 USceneCaptureComponent2D* SceneCaptureComponent = GetCaptureComponent2D();
59
60 if (SceneCaptureComponent)
61 {
62 FPostProcessSettings& PostProcessSettings = SceneCaptureComponent->PostProcessSettings;
63 if (!ThermalMaterial.IsValid())
64 {
65 FString Path = "/Game/Agrarsense/Materials/PostProcessingMaterials/M_ThermalCameraMaterial.M_ThermalCameraMaterial";
66 UMaterial* BaseMaterial = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *Path));
67 if (BaseMaterial)
68 {
69 UMaterialInstanceDynamic* DynamicMaterialInstance = UMaterialInstanceDynamic::Create(BaseMaterial, this);
70 ThermalMaterial = DynamicMaterialInstance;
71 PostProcessSettings.AddBlendable(ThermalMaterial.Get(), 1.0f);
73 }
74 }
75 }
76}
77
79{
80 UMaterialInstanceDynamic* Material = ThermalMaterial.Get();
81 if (Material)
82 {
83 float Width, Height;
85 {
88 }
89 else
90 {
93 }
94
95 // Set material instance dynamic parameters.
96 Material->SetScalarParameterValue(WidthNoise, Width);
97 Material->SetScalarParameterValue(HeightNoise, Height);
98
99 Material->SetVectorParameterValue(WarmColor, ThermalCameraParameters.WarmColor);
100 Material->SetVectorParameterValue(WarmColor2, ThermalCameraParameters.WarmColor2);
101
102 Material->SetVectorParameterValue(ColdColor, ThermalCameraParameters.ColdColor);
103 Material->SetVectorParameterValue(ColdColor2, ThermalCameraParameters.ColdColor2);
104 }
105}
void ChangeCameraParameters(FCameraBaseParameters newParameters)
Definition: Camera.cpp:50
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 Init(FCameraBaseParameters parameters, bool SimulateSensor=true) override
void UpdateThermalMaterial()
static const FName ColdColor2
Definition: ThermalCamera.h:83
static const FName WidthNoise
Definition: ThermalCamera.h:77
static const FName WarmColor
Definition: ThermalCamera.h:80
static const FName ColdColor
Definition: ThermalCamera.h:82
void ChangeThermalCameraParameters(FThermalCameraParameters Parameters)
TWeakObjectPtr< UMaterialInstanceDynamic > ThermalMaterial
Definition: ThermalCamera.h:75
AThermalCamera(const FObjectInitializer &ObjectInitializer)
void ThermalInit(FThermalCameraParameters Parameters, bool SimulateSensor=true)
FThermalCameraParameters ThermalCameraParameters
Definition: ThermalCamera.h:73
static const FName WarmColor2
Definition: ThermalCamera.h:81
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
static const FName HeightNoise
Definition: ThermalCamera.h:78
FCameraBaseParameters CameraParameters