CARLA
Loading...
Searching...
No Matches
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
8#include "Carla.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
17AShaderBasedSensor::AShaderBasedSensor(const FObjectInitializer& ObjectInitializer)
18: Super(ObjectInitializer)
19{
21}
22
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
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
66{
67 Super::Set(Description);
69}
70
72 uint8_t ShaderIndex,
73 const FName &ParameterName,
74 float Value)
75{
76 FloatShaderParams.Add({ShaderIndex, ParameterName, Value});
77}
void EnablePostProcessingEffects(bool Enable=true)
void Set(const FActorDescription &ActorDescription) override
TArray< FSensorShader > Shaders
AShaderBasedSensor(const FObjectInitializer &ObjectInitializer)
TArray< TObjectPtr< UMaterial > > MaterialsFound
void AddShader(const FSensorShader &Shader)
Add a post-processing shader.
void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) override
TArray< FShaderFloatParameterValue > FloatShaderParams
bool AddPostProcessingMaterial(const FString &Path)
Load the UMaterialInstanceDynamic at the given Path and append it to the list of shaders with Weight.
void SetFloatShaderParameter(uint8_t ShaderIndex, const FName &ParameterName, float Value)
static void SetCamera(const FActorDescription &Description, ASceneCaptureSensor *Camera)
A description of a Carla Actor with all its variation.