CARLA
 
载入中...
搜索中...
未找到
RoadPainterWrapper.cpp
浏览该文件的文档.
1// Copyright (c) 2021 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
9#if WITH_EDITOR
10 #include "FileHelper.h"
11#endif
12#include "JsonObject.h"
13#include "JsonSerializer.h"
14#include "AssetRegistry/AssetRegistryModule.h"
15#include "Engine/StaticMeshActor.h"
16#include "Engine/TextureRenderTarget2D.h"
17#include "Kismet/GameplayStatics.h"
18#include "Kismet/KismetRenderingLibrary.h"
19#include "Materials/MaterialInstanceDynamic.h"
20#include "UObject/ConstructorHelpers.h"
21
23
24#if WITH_EDITORONLY_DATA
25
26 // Initialization of map for translating from the JSON "decal_names" to MaterialInstance
27
28 TArray<AActor*> MeshActors;
29 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AStaticMeshActor::StaticClass(), MeshActors);
30 for (int32 i = 0; i < MeshActors.Num(); ++i) {
31
32 AStaticMeshActor *StaticMeshActor = Cast<AStaticMeshActor>(MeshActors[i]);
33 if (StaticMeshActor) {
34
35 if (StaticMeshActor->GetName().Contains("Curb", ESearchCase::Type::IgnoreCase) || StaticMeshActor->GetName().Contains("Gutter", ESearchCase::Type::IgnoreCase)) {
36
37 StaticMeshActor->GetStaticMeshComponent()->bReceivesDecals = false;
38 }
39 }
40 }
41
42#endif
43}
44
46{
47 Super::BeginPlay();
48
49}
50
51// 读取并解析指定地图的路面绘制配置文件,设置 decal 属性。
52// 配置文件为 JSON 格式,包含了在特定地图上应用 decal 的信息。
53// 如果 JSON 配置文件中存在与当前地图名称匹配的配置,则将其应用到当前对象的 `DecalPropertiesConfig` 中。
54void ARoadPainterWrapper::ReadConfigFile(const FString &CurrentMapName, const TMap<FString, FString> &DecalNamesMap)
55{
56 // Get road painter configuration file
57 FString JsonConfigFile;
58
59 TArray<FString> FileList;
60 IFileManager::Get().FindFilesRecursive(FileList, *(FPaths::ProjectContentDir()),
61 *(FString("roadpainter_decals.json")), true, false, false);
62
63 if(FFileHelper::LoadFileToString(JsonConfigFile, *(IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FileList[0]))))
64 {
65 TSharedPtr<FJsonObject> JsonParsed;
66 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonConfigFile);
67 if(FJsonSerializer::Deserialize(JsonReader, JsonParsed))
68 {
69 // Get decals object array
70 auto DecalJsonArray = JsonParsed->GetArrayField(TEXT("decals"));
71 for(auto &DecalJsonValue : DecalJsonArray)
72 {
73 const auto DecalJsonObject = DecalJsonValue->AsObject();
74
75 // Inside the decals object array, get the map name where the decals should be applied
76 // If it coincides with the map this object is in, then it's the correct configuration
77 FString JsonMapName = DecalJsonObject->GetStringField(TEXT("map_name"));
78 if(JsonMapName.Equals(CurrentMapName) == true)
79 {
80 // With the decal name array we created earlier, we traverse it
81 // and look up it's name in the .json file
82 for (const TPair<FString, FString>& Pair : DecalNamesMap) {
83 if (DecalJsonObject->HasField(Pair.Key) == true) {
84 DecalPropertiesConfig.DecalMaterials.Add(LoadObject<UMaterialInstanceConstant>(nullptr, *Pair.Value));
85 DecalPropertiesConfig.DecalNumToSpawn.Add(DecalJsonObject->GetIntegerField(Pair.Key));
86 }
87 }
88
89 // Prepare the decal properties struct variable inside the class
90 // so the blueprint can read from it, later on
91 DecalPropertiesConfig.DecalScale = ReadVectorFromJsonObject(DecalJsonObject->GetObjectField(TEXT("decal_scale")));
92 DecalPropertiesConfig.FixedDecalOffset = ReadVectorFromJsonObject(DecalJsonObject->GetObjectField(TEXT("fixed_decal_offset")));
93 DecalPropertiesConfig.DecalMinScale = (float)DecalJsonObject->GetNumberField(TEXT("decal_min_scale"));
94 DecalPropertiesConfig.DecalMaxScale = (float)DecalJsonObject->GetNumberField(TEXT("decal_max_scale"));
95 DecalPropertiesConfig.DecalRandomYaw = (float)DecalJsonObject->GetNumberField(TEXT("decal_random_yaw"));
96 DecalPropertiesConfig.RandomOffset = (float)DecalJsonObject->GetNumberField(TEXT("random_offset"));
97 }
98 }
99 }
100 }
101}
102
103FVector ARoadPainterWrapper::ReadVectorFromJsonObject(TSharedPtr<FJsonObject> JsonObject)
104{
105 FVector ObjectVector;
106 ObjectVector.X = (float)JsonObject->GetNumberField(TEXT("x_axis"));
107 ObjectVector.Y = (float)JsonObject->GetNumberField(TEXT("y_axis"));
108 ObjectVector.Z = (float)JsonObject->GetNumberField(TEXT("z_axis"));
109 return ObjectVector;
110}
FVector ReadVectorFromJsonObject(TSharedPtr< FJsonObject > JsonObject)
Function to read 3D vectors from a JSON file
FDecalsProperties DecalPropertiesConfig
Variable used for storing the JSON values of the decals so it can be later used by the blueprint (Roa...
void ReadConfigFile(const FString &CurrentMapName, const TMap< FString, FString > &DecalNamesMap)
Function for reading the decals configuration file (in JSON format)
float DecalRandomYaw
The decal yaw to be applied randomly
float DecalMaxScale
Maximum scale to be applied to the decals
TArray< int32 > DecalNumToSpawn
How many decals (or material instances) of each, should be applied to the road
float RandomOffset
Random offset from one decal to another
TArray< UMaterialInstance * > DecalMaterials
The decals used on the road
float DecalMinScale
Min scale to be applied to the decals
FVector DecalScale
Scale of each decal on the road
FVector FixedDecalOffset
Min offset from one decal to another