CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Util
RoadPainterWrapper.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 "
RoadPainterWrapper.h
"
8
9
#include <util/ue-header-guard-begin.h>
10
#if WITH_EDITOR
11
#include "Misc/FileHelper.h"
12
#endif
13
#include "Dom/JsonObject.h"
14
#include "Serialization/JsonSerializer.h"
15
#include "AssetRegistry/AssetRegistryModule.h"
16
#include "Engine/StaticMeshActor.h"
17
#include "Engine/TextureRenderTarget2D.h"
18
#include "Kismet/GameplayStatics.h"
19
#include "Kismet/KismetRenderingLibrary.h"
20
#include "Materials/MaterialInstanceDynamic.h"
21
#include "UObject/ConstructorHelpers.h"
22
#include <util/ue-header-guard-end.h>
23
24
ARoadPainterWrapper::ARoadPainterWrapper
(){
25
26
#if WITH_EDITORONLY_DATA
27
28
// Initialization of map for translating from the JSON "decal_names" to MaterialInstance
29
30
TArray<AActor*> MeshActors;
31
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AStaticMeshActor::StaticClass(), MeshActors);
32
for
(int32 i = 0; i < MeshActors.Num(); ++i) {
33
34
AStaticMeshActor *StaticMeshActor = Cast<AStaticMeshActor>(MeshActors[i]);
35
if
(StaticMeshActor) {
36
37
if
(StaticMeshActor->GetName().Contains(
"Curb"
, ESearchCase::Type::IgnoreCase) || StaticMeshActor->GetName().Contains(
"Gutter"
, ESearchCase::Type::IgnoreCase)) {
38
39
StaticMeshActor->GetStaticMeshComponent()->bReceivesDecals =
false
;
40
}
41
}
42
}
43
44
#endif
45
}
46
47
void
ARoadPainterWrapper::BeginPlay
()
48
{
49
Super::BeginPlay();
50
51
}
52
53
void
ARoadPainterWrapper::ReadConfigFile
(
const
FString &CurrentMapName,
const
TMap<FString, FString> &DecalNamesMap)
54
{
55
// Get road painter configuration file
56
FString JsonConfigFile;
57
58
TArray<FString> FileList;
59
IFileManager::Get().FindFilesRecursive(FileList, *(FPaths::ProjectContentDir()),
60
*(FString(
"roadpainter_decals.json"
)),
true
,
false
,
false
);
61
62
if
(FFileHelper::LoadFileToString(JsonConfigFile, *(IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FileList[0]))))
63
{
64
TSharedPtr<FJsonObject> JsonParsed;
65
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonConfigFile);
66
if
(FJsonSerializer::Deserialize(JsonReader, JsonParsed))
67
{
68
// Get decals object array
69
auto
DecalJsonArray = JsonParsed->GetArrayField(TEXT(
"decals"
));
70
for
(
auto
&DecalJsonValue : DecalJsonArray)
71
{
72
const
auto
DecalJsonObject = DecalJsonValue->AsObject();
73
74
// Inside the decals object array, get the map name where the decals should be applied
75
// If it coincides with the map this object is in, then it's the correct configuration
76
FString JsonMapName = DecalJsonObject->GetStringField(TEXT(
"map_name"
));
77
if
(JsonMapName.Equals(CurrentMapName) ==
true
)
78
{
79
// With the decal name array we created earlier, we traverse it
80
// and look up it's name in the .json file
81
for
(
const
TPair<FString, FString>& Pair : DecalNamesMap) {
82
if
(DecalJsonObject->HasField(Pair.Key) ==
true
) {
83
DecalPropertiesConfig
.
DecalMaterials
.Add(LoadObject<UMaterialInstanceConstant>(
nullptr
, *Pair.Value));
84
DecalPropertiesConfig
.
DecalNumToSpawn
.Add(DecalJsonObject->GetIntegerField(Pair.Key));
85
}
86
}
87
88
// Prepare the decal properties struct variable inside the class
89
// so the blueprint can read from it, later on
90
DecalPropertiesConfig
.
DecalScale
=
ReadVectorFromJsonObject
(DecalJsonObject->GetObjectField(TEXT(
"decal_scale"
)));
91
DecalPropertiesConfig
.
FixedDecalOffset
=
ReadVectorFromJsonObject
(DecalJsonObject->GetObjectField(TEXT(
"fixed_decal_offset"
)));
92
DecalPropertiesConfig
.
DecalMinScale
= (float)DecalJsonObject->GetNumberField(TEXT(
"decal_min_scale"
));
93
DecalPropertiesConfig
.
DecalMaxScale
= (float)DecalJsonObject->GetNumberField(TEXT(
"decal_max_scale"
));
94
DecalPropertiesConfig
.
DecalRandomYaw
= (float)DecalJsonObject->GetNumberField(TEXT(
"decal_random_yaw"
));
95
DecalPropertiesConfig
.
RandomOffset
= (float)DecalJsonObject->GetNumberField(TEXT(
"random_offset"
));
96
}
97
}
98
}
99
}
100
}
101
102
FVector
ARoadPainterWrapper::ReadVectorFromJsonObject
(TSharedPtr<FJsonObject> JsonObject)
103
{
104
FVector ObjectVector;
105
ObjectVector.X = (float)JsonObject->GetNumberField(TEXT(
"x_axis"
));
106
ObjectVector.Y = (float)JsonObject->GetNumberField(TEXT(
"y_axis"
));
107
ObjectVector.Z = (float)JsonObject->GetNumberField(TEXT(
"z_axis"
));
108
return
ObjectVector;
109
}
RoadPainterWrapper.h
ARoadPainterWrapper::ARoadPainterWrapper
ARoadPainterWrapper()
Definition
RoadPainterWrapper.cpp:24
ARoadPainterWrapper::ReadVectorFromJsonObject
FVector ReadVectorFromJsonObject(TSharedPtr< FJsonObject > JsonObject)
Function to read 3D vectors from a JSON file.
Definition
RoadPainterWrapper.cpp:102
ARoadPainterWrapper::BeginPlay
void BeginPlay() override
Definition
RoadPainterWrapper.cpp:47
ARoadPainterWrapper::DecalPropertiesConfig
FDecalsProperties DecalPropertiesConfig
Variable used for storing the JSON values of the decals so it can be later used by the blueprint (Roa...
Definition
RoadPainterWrapper.h:84
ARoadPainterWrapper::ReadConfigFile
void ReadConfigFile(const FString &CurrentMapName, const TMap< FString, FString > &DecalNamesMap)
Function for reading the decals configuration file (in JSON format).
Definition
RoadPainterWrapper.cpp:53
FDecalsProperties::DecalRandomYaw
float DecalRandomYaw
The decal yaw to be applied randomly.
Definition
RoadPainterWrapper.h:47
FDecalsProperties::DecalMaxScale
float DecalMaxScale
Maximum scale to be applied to the decals.
Definition
RoadPainterWrapper.h:39
FDecalsProperties::DecalNumToSpawn
TArray< int32 > DecalNumToSpawn
How many decals (or material instances) of each, should be applied to the road.
Definition
RoadPainterWrapper.h:27
FDecalsProperties::RandomOffset
float RandomOffset
Random offset from one decal to another.
Definition
RoadPainterWrapper.h:51
FDecalsProperties::DecalMaterials
TArray< UMaterialInstance * > DecalMaterials
The decals used on the road.
Definition
RoadPainterWrapper.h:23
FDecalsProperties::DecalMinScale
float DecalMinScale
Min scale to be applied to the decals.
Definition
RoadPainterWrapper.h:43
FDecalsProperties::DecalScale
FVector DecalScale
Scale of each decal on the road.
Definition
RoadPainterWrapper.h:31
FDecalsProperties::FixedDecalOffset
FVector FixedDecalOffset
Min offset from one decal to another.
Definition
RoadPainterWrapper.h:35
Generated by
1.18.0