CARLA
Loading...
Searching...
No Matches
ShaderBasedSensor_WideAngleLens.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 <util/ue-header-guard-end.h>
15
16AShaderBasedSensor_WideAngleLens::AShaderBasedSensor_WideAngleLens(const FObjectInitializer& ObjectInitializer) :
17 Super(ObjectInitializer)
18{
20}
21
23{
24 // ConstructorHelpers::FObjectFinder is documented as constructor-only and
25 // has undefined behavior when used from runtime functions. FSoftObjectPath
26 // is the runtime-safe path resolver; it accepts both plain object paths
27 // and the class-prefixed export-text form ("Material'/Game/.../Foo'") that
28 // FObjectFinder previously normalised internally.
29 UMaterial* Material = Cast<UMaterial>(FSoftObjectPath(Path).TryLoad());
30 if (Material != nullptr)
31 {
32 MaterialsFound.Add(Material);
33 return true;
34 }
35 UE_LOG(LogCarla, Error,
36 TEXT("AShaderBasedSensor_WideAngleLens::AddPostProcessingMaterial: failed to load material '%s'"),
37 *Path);
38 return false;
39}
40
41void AShaderBasedSensor_WideAngleLens::SetUpSceneCaptureComponents(TArrayView<USceneCaptureComponent2D_CARLA*> SceneCaptures)
42{
43 // Create one dynamic instance per post-processing material. The six face
44 // captures of a wide-angle sensor must share an identical post-process
45 // chain, so the instances are created once and reused across all faces.
46 // Creating them inside the per-face loop would append 6x the materials to
47 // the shared Shaders array and give each face a different blendable set.
48 for (const auto& MaterialFound : MaterialsFound)
49 {
50 // Create a dynamic instance of the Material (Shader)
51 AddShader({ UMaterialInstanceDynamic::Create(MaterialFound, this), 1.0 });
52 }
53
54 for (auto SceneCapture : SceneCaptures)
55 {
56 for (const auto& Shader : Shaders)
57 {
58 // Attach the instance into the blendables
59 SceneCapture->PostProcessSettings.AddBlendable(Shader.PostProcessMaterial, Shader.Weight);
60 }
61 }
62
63 // Set the value for each Float parameter in the shader. The dynamic
64 // material instances are shared across faces, so each parameter only
65 // needs to be applied once.
66 if (Shaders.Num() != 0)
67 {
68 for (const auto& ParameterValue : FloatShaderParams)
69 {
70 Shaders[ParameterValue.ShaderIndex].PostProcessMaterial->SetScalarParameterValue(
71 ParameterValue.ParameterName,
72 ParameterValue.Value);
73 }
74 }
75}
76
78{
79 Super::Set(Description);
81}
82
84 uint8_t ShaderIndex,
85 const FName& ParameterName,
86 float Value)
87{
88 FloatShaderParams.Add({ ShaderIndex, ParameterName, Value });
89}
void Set(const FActorDescription &ActorDescription) override
TArray< FShaderFloatParameterValue > FloatShaderParams
TArray< TObjectPtr< UMaterial > > MaterialsFound
AShaderBasedSensor_WideAngleLens(const FObjectInitializer &ObjectInitializer)
void SetUpSceneCaptureComponents(TArrayView< USceneCaptureComponent2D_CARLA * > SceneCaptures) override
void SetFloatShaderParameter(uint8_t ShaderIndex, const FName &ParameterName, float Value)
void AddShader(const FSensorShader &Shader)
Add a post-processing shader.
static void SetCamera(const FActorDescription &Description, ASceneCaptureSensor *Camera)
A description of a Carla Actor with all its variation.