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
51void ARoadPainterWrapper::ReadConfigFile(const FString &CurrentMapName, const TMap<FString, FString> &DecalNamesMap)
52{
53 // Get road painter configuration file
54 FString JsonConfigFile;
55
56 TArray<FString> FileList;
57 IFileManager::Get().FindFilesRecursive(FileList, *(FPaths::ProjectContentDir()),
58 *(FString("roadpainter_decals.json")), true, false, false);
59
60 if(FFileHelper::LoadFileToString(JsonConfigFile, *(IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FileList[0]))))
61 {
62 TSharedPtr<FJsonObject> JsonParsed;
63 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonConfigFile);
64 if(FJsonSerializer::Deserialize(JsonReader, JsonParsed))
65 {
66 // Get decals object array
67 auto DecalJsonArray = JsonParsed->GetArrayField(TEXT("decals"));
68 for(auto &DecalJsonValue : DecalJsonArray)
69 {
70 const auto DecalJsonObject = DecalJsonValue->AsObject();
71
72 // Inside the decals object array, get the map name where the decals should be applied
73 // If it coincides with the map this object is in, then it's the correct configuration
74 FString JsonMapName = DecalJsonObject->GetStringField(TEXT("map_name"));
75 if(JsonMapName.Equals(CurrentMapName) == true)
76 {
77 // With the decal name array we created earlier, we traverse it
78 // and look up it's name in the .json file
79 for (const TPair<FString, FString>& Pair : DecalNamesMap) {
80 if (DecalJsonObject->HasField(Pair.Key) == true) {
81 DecalPropertiesConfig.DecalMaterials.Add(LoadObject<UMaterialInstanceConstant>(nullptr, *Pair.Value));
82 DecalPropertiesConfig.DecalNumToSpawn.Add(DecalJsonObject->GetIntegerField(Pair.Key));
83 }
84 }
85
86 // Prepare the decal properties struct variable inside the class
87 // so the blueprint can read from it, later on
88 DecalPropertiesConfig.DecalScale = ReadVectorFromJsonObject(DecalJsonObject->GetObjectField(TEXT("decal_scale")));
89 DecalPropertiesConfig.FixedDecalOffset = ReadVectorFromJsonObject(DecalJsonObject->GetObjectField(TEXT("fixed_decal_offset")));
90 DecalPropertiesConfig.DecalMinScale = (float)DecalJsonObject->GetNumberField(TEXT("decal_min_scale"));
91 DecalPropertiesConfig.DecalMaxScale = (float)DecalJsonObject->GetNumberField(TEXT("decal_max_scale"));
92 DecalPropertiesConfig.DecalRandomYaw = (float)DecalJsonObject->GetNumberField(TEXT("decal_random_yaw"));
93 DecalPropertiesConfig.RandomOffset = (float)DecalJsonObject->GetNumberField(TEXT("random_offset"));
94 }
95 }
96 }
97 }
98}
99
100FVector ARoadPainterWrapper::ReadVectorFromJsonObject(TSharedPtr<FJsonObject> JsonObject)
101{
102 FVector ObjectVector;
103 ObjectVector.X = (float)JsonObject->GetNumberField(TEXT("x_axis"));
104 ObjectVector.Y = (float)JsonObject->GetNumberField(TEXT("y_axis"));
105 ObjectVector.Z = (float)JsonObject->GetNumberField(TEXT("z_axis"));
106 return ObjectVector;
107}
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