CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Sensor
ShaderBasedSensor.cpp
Go to the documentation of this file.
1
// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2
// de Barcelona (UAB).
3
//
4
// This work is licensed under the terms of the MIT license.
5
// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
#include "
Carla/Sensor/ShaderBasedSensor.h
"
8
#include "
Carla.h
"
9
#include "
Actor/ActorBlueprintFunctionLibrary.h
"
10
11
#include <util/ue-header-guard-begin.h>
12
#include "Materials/Material.h"
13
#include "Materials/MaterialInstanceDynamic.h"
14
#include "Components/SceneCaptureComponent2D.h"
15
#include <util/ue-header-guard-end.h>
16
17
AShaderBasedSensor::AShaderBasedSensor
(
const
FObjectInitializer& ObjectInitializer)
18
: Super(ObjectInitializer)
19
{
20
EnablePostProcessingEffects
(
false
);
21
}
22
23
bool
AShaderBasedSensor::AddPostProcessingMaterial
(
const
FString &Path)
24
{
25
// ConstructorHelpers::FObjectFinder is documented as constructor-only and
26
// has undefined behavior when used from runtime functions. FSoftObjectPath
27
// is the runtime-safe path resolver; it accepts both plain object paths
28
// and the class-prefixed export-text form ("Material'/Game/.../Foo'") that
29
// FObjectFinder previously normalised internally.
30
UMaterial *Material = Cast<UMaterial>(FSoftObjectPath(Path).TryLoad());
31
if
(Material !=
nullptr
)
32
{
33
MaterialsFound
.Add(Material);
34
return
true
;
35
}
36
UE_LOG(LogCarla, Error,
37
TEXT(
"AShaderBasedSensor::AddPostProcessingMaterial: failed to load material '%s'"
),
38
*Path);
39
return
false
;
40
}
41
42
void
AShaderBasedSensor::SetUpSceneCaptureComponent
(
USceneCaptureComponent2D
&SceneCapture)
43
{
44
for
(
const
auto
&MaterialFound :
MaterialsFound
)
45
{
46
// Create a dynamic instance of the Material (Shader)
47
AddShader
({UMaterialInstanceDynamic::Create(MaterialFound,
this
), 1.0});
48
}
49
50
for
(
const
auto
&Shader :
Shaders
)
51
{
52
// Attach the instance into the blendables
53
SceneCapture.PostProcessSettings.AddBlendable(Shader.PostProcessMaterial, Shader.Weight);
54
}
55
56
// Set the value for each Float parameter in the shader
57
for
(
const
auto
&ParameterValue :
FloatShaderParams
)
58
{
59
Shaders
[ParameterValue.ShaderIndex].PostProcessMaterial->SetScalarParameterValue(
60
ParameterValue.ParameterName,
61
ParameterValue.Value);
62
}
63
}
64
65
void
AShaderBasedSensor::Set
(
const
FActorDescription
&Description)
66
{
67
Super::Set(Description);
68
UActorBlueprintFunctionLibrary::SetCamera
(Description,
this
);
69
}
70
71
void
AShaderBasedSensor::SetFloatShaderParameter
(
72
uint8_t ShaderIndex,
73
const
FName &ParameterName,
74
float
Value)
75
{
76
FloatShaderParams
.Add({ShaderIndex, ParameterName, Value});
77
}
ActorBlueprintFunctionLibrary.h
Carla.h
ShaderBasedSensor.h
ASceneCaptureSensor::EnablePostProcessingEffects
void EnablePostProcessingEffects(bool Enable=true)
Definition
SceneCaptureSensor.h:155
AShaderBasedSensor::Set
void Set(const FActorDescription &ActorDescription) override
Definition
ShaderBasedSensor.cpp:65
AShaderBasedSensor::Shaders
TArray< FSensorShader > Shaders
Definition
ShaderBasedSensor.h:128
AShaderBasedSensor::AShaderBasedSensor
AShaderBasedSensor(const FObjectInitializer &ObjectInitializer)
Definition
ShaderBasedSensor.cpp:17
AShaderBasedSensor::MaterialsFound
TArray< TObjectPtr< UMaterial > > MaterialsFound
Definition
ShaderBasedSensor.h:125
AShaderBasedSensor::AddShader
void AddShader(const FSensorShader &Shader)
Add a post-processing shader.
Definition
ShaderBasedSensor.h:111
AShaderBasedSensor::SetUpSceneCaptureComponent
void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) override
Definition
ShaderBasedSensor.cpp:42
AShaderBasedSensor::FloatShaderParams
TArray< FShaderFloatParameterValue > FloatShaderParams
Definition
ShaderBasedSensor.h:131
AShaderBasedSensor::AddPostProcessingMaterial
bool AddPostProcessingMaterial(const FString &Path)
Load the UMaterialInstanceDynamic at the given Path and append it to the list of shaders with Weight.
Definition
ShaderBasedSensor.cpp:23
AShaderBasedSensor::SetFloatShaderParameter
void SetFloatShaderParameter(uint8_t ShaderIndex, const FName &ParameterName, float Value)
Definition
ShaderBasedSensor.cpp:71
UActorBlueprintFunctionLibrary::SetCamera
static void SetCamera(const FActorDescription &Description, ASceneCaptureSensor *Camera)
Definition
ActorBlueprintFunctionLibrary.cpp:2299
USceneCaptureComponent2D
FActorDescription
A description of a Carla Actor with all its variation.
Definition
Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Actor/ActorDescription.h:21
Generated by
1.18.0