CARLA
VegetationManager.h
Go to the documentation of this file.
1 // Copyright (c) 2022 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 #pragma once
8 
9 #include "GameFramework/Actor.h"
10 #include "ProceduralFoliageVolume.h"
11 #include "Components/InstancedStaticMeshComponent.h"
12 #include "Materials/MaterialInstanceDynamic.h"
13 #include "InstancedFoliageActor.h"
14 
15 #include "Containers/Array.h"
16 #include "Containers/Map.h"
17 
20 #include <memory>
21 
22 #include "VegetationManager.generated.h"
23 
24 USTRUCT()
26 {
27  GENERATED_BODY()
28  UInstancedStaticMeshComponent* InstancedStaticMeshComponent {nullptr};
29  TArray<int32> IndicesInUse {};
30  bool bIsAlive = false;
31 };
32 
33 USTRUCT()
34 struct FTileData
35 {
36  GENERATED_BODY()
37  AInstancedFoliageActor* InstancedFoliageActor {nullptr};
38  AProceduralFoliageVolume* ProceduralFoliageVolume {nullptr};
39  TArray<std::shared_ptr<FTileMeshComponent>> TileMeshesCache {};
40  TArray<UMaterialInstanceDynamic*> MaterialInstanceDynamicCache {};
41 
42  bool ContainsMesh(const UInstancedStaticMeshComponent*) const;
43  void UpdateTileMeshComponent(UInstancedStaticMeshComponent* NewInstancedStaticMeshComponent);
44  void UpdateMaterialCache(const FLinearColor& Value, bool DebugMaterials);
45  ~FTileData();
46 };
47 
48 USTRUCT()
50 {
51  GENERATED_BODY()
52  FString BPFullClassName {};
53  TSubclassOf<AActor> SpawnedClass { nullptr };
54 
55  bool IsValid() const;
56  bool SetBPClassName(const FString& Path);
57  bool SetSpawnedClass();
58 };
59 
60 USTRUCT()
62 {
63  GENERATED_BODY()
64  bool InUse { false };
65  bool IsActive { false };
66  AActor* Actor { nullptr };
67  FTransform GlobalTransform {FTransform()};
68  int32 Index {-1};
69  std::shared_ptr<FTileMeshComponent> TileMeshComponent {nullptr};
70 
71  void EnableActor(
72  const FTransform& Transform,
73  int32 NewIndex,
74  std::shared_ptr<FTileMeshComponent>& NewTileMeshComponent);
75  void ActiveActor();
76  void DisableActor();
77 };
78 
79 USTRUCT()
81 {
82  GENERATED_BODY()
83  std::shared_ptr<FTileMeshComponent> TileMeshComponent;
85  TArray<TPair<FTransform, int32>> TransformIndex;
86 };
87 
88 UCLASS()
89 class CARLA_API AVegetationManager : public AActor
90 {
91  GENERATED_BODY()
92 
93 public:
94  void AddVehicle(ACarlaWheeledVehicle* Vehicle);
95  void RemoveVehicle(ACarlaWheeledVehicle* Vehicle);
96 
97  UFUNCTION(BlueprintCallable)
98  void UpdatePoolBasePosition();
99 
100 public:
101  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
102  bool DebugMaterials {false};
103 
104  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
105  float HideMaterialDistance {500.0f};
106 
107  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
108  float ActiveActorDistance {500.0f};
109 
110  //Filters for debug
111  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
112  bool SpawnBushes {true};
113 
114  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
115  bool SpawnTrees {true};
116 
117  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
118  bool SpawnRocks {true};
119 
120  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
121  bool SpawnPlants {true};
122 
123  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
124  float SpawnScale {1.0f};
125 
126  UPROPERTY(Category = "CARLA Vegetation Spawner", EditDefaultsOnly)
127  int32 InitialPoolSize {10};
128 
129  /// @}
130  // ===========================================================================
131  /// @name Overriden from AActor
132  // ===========================================================================
133  /// @{
134 protected:
135  virtual void BeginPlay() override;
136  virtual void Tick(float DeltaTime) override;
137 
138 private:
139  bool IsFoliageTypeEnabled(const FString& Path) const;
140  bool CheckForNewTiles() const;
141 
142  TArray<FString> GetTilesInUse();
143 
144  void UpdateMaterials(FTileData* Tile);
145  TArray<FElementsToSpawn> GetElementsToSpawn(FTileData* Tile);
146  void SpawnSkeletalFoliages(TArray<FElementsToSpawn>& ElementsToSpawn);
147  void DestroySkeletalFoliages();
148  void ActivePooledActors();
149  bool EnableActorFromPool(
150  const FTransform& Transform,
151  int32 Index,
152  std::shared_ptr<FTileMeshComponent>& TileMeshComponent,
153  TArray<FPooledActor>& Pool);
154 
155  void CreateOrUpdateTileCache(ULevel* InLevel);
156  void UpdateFoliageBlueprintCache(ULevel* InLevel);
157  void SetTileDataInternals(FTileData& TileData);
158  void SetInstancedStaticMeshComponentCache(FTileData& TileData);
159  void SetMaterialCache(FTileData& TileData);
160 
161  void FreeTileCache(ULevel* InLevel);
162 
163  void OnLevelAddedToWorld(ULevel* InLevel, UWorld* InWorld);
164  void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld);
165  void PostWorldOriginOffset(UWorld*, FIntVector, FIntVector InDstOrigin);
166 
167  void CreatePoolForBPClass(const FFoliageBlueprint& BP);
168  AActor* CreateFoliage(const FFoliageBlueprint& BP, const FTransform& Transform) const;
169 
170  void GetSketalTemplates();
171 
172 private:
173  float PoolTranslationTimer {30.0f};
174  FTransform InactivePoolTransform { FQuat(1.0f, 1.0f, 1.0f, 1.0f), FVector(1.0f, 1.0f, 1.0f), FVector(1.0f, 1.0f, 1.0f)};
175  //Actors
176  ALargeMapManager* LargeMap {nullptr};
177  ACarlaWheeledVehicle* HeroVehicle {nullptr};
178  //Caches
179  TMap<FString, FFoliageBlueprint> FoliageBlueprintCache {};
180  TMap<FString, FTileData> TileCache {};
181  //Pools
182  TMap<FString, TArray<FPooledActor>> ActorPool {};
183 
185 };
std::vector< cg::Location > Path
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
carla::SharedPtr< cc::Actor > Actor
FTimerHandle UpdatePoolInactiveTransformTimer
Base class for CARLA wheeled vehicles.
geom::Transform Transform
Definition: rpc/Transform.h:16