13#include "InstancedFoliageActor.h"
14#include "GameFramework/Character.h"
17#include "FileHelper.h"
25 TArray<FEnvironmentObject> Result;
28 bool FilterByTagEnabled = (TagQueried != crp::CityObjectLabel::Any);
32 if(!FilterByTagEnabled || (It.ObjectLabel == TagQueried))
46 for(
AActor* Actor : Actors)
49 FString ClassName = Actor->GetClass()->GetName();
51 if(ClassName.Equals(
"BP_Sky_C"))
continue;
84 FileContent += FString::Printf(TEXT(
"Num actors %d\n"), Actors.Num());
85 FileContent += FString::Printf(TEXT(
"Num registered objects %d\n\n"),
EnvironmentObjects.Num());
89 FileContent += FString::Printf(TEXT(
"%llu\t"), Object.Id);
90 FileContent += FString::Printf(TEXT(
"%s\t"), *Object.Name);
91 FileContent += FString::Printf(TEXT(
"%s\t"), *Object.IdStr);
92 FileContent += FString::Printf(TEXT(
"%d\n"),
static_cast<int32
>(Object.Type));
96 FString FilePath = FPaths::ProjectSavedDir() +
"RegisteredObjects.txt";
97 FFileHelper::SaveStringToFile(
100 FFileHelper::EEncodingOptions::AutoDetect,
101 &IFileManager::Get(),
102 EFileWrite::FILEWRITE_Silent);
110 for(uint64 It : EnvObjectIds)
115 if(It == EnvironmentObject.Id)
124 UE_LOG(LogCarla, Error, TEXT(
"EnableEnvironmentObjects id not found %llu"), It);
136 const FString ActorName = Actor->GetName();
137 const char* ActorNameChar = TCHAR_TO_ANSI(*ActorName);
140 EnvironmentObject.
Transform = Actor->GetActorTransform();
141 EnvironmentObject.
Id = CityHash64(ActorNameChar, ActorName.Len());
142 EnvironmentObject.
Name = ActorName;
143 EnvironmentObject.
Actor = Actor;
144 EnvironmentObject.
CanTick = Actor->IsActorTickEnabled();
147 EnvironmentObject.
Type = Type;
170 TArray<FBoundingBox> BBs;
174 check(BBs.Num() == Tags.Num());
176 const FTransform Transform =
TrafficLight->GetTransform();
178 const bool IsActorTickEnabled =
TrafficLight->IsActorTickEnabled();
180 for(
int i = 0; i < BBs.Num(); i++)
183 const uint8 Tag = Tags[i];
188 const FString SMName = FString::Printf(TEXT(
"%s_%s_%d"), *ActorName, *TagString, i);
192 EnvironmentObject.
Id = CityHash64(TCHAR_TO_ANSI(*SMName), SMName.Len());
193 EnvironmentObject.
Name = SMName;
195 EnvironmentObject.
CanTick = IsActorTickEnabled;
203 TArray<UStaticMeshComponent*> StaticMeshComps;
205 for(
const UStaticMeshComponent* Comp : StaticMeshComps)
216 TArray<UInstancedStaticMeshComponent*> ISMComps;
217 Actor->GetComponents<UInstancedStaticMeshComponent>(ISMComps);
219 const FString ActorName = Actor->GetName();
220 int InstanceCount = 0;
221 bool IsActorTickEnabled = Actor->IsActorTickEnabled();
224 if(Cast<AInstancedFoliageActor>(Actor))
229 for(UInstancedStaticMeshComponent* Comp : ISMComps)
231 const TArray<FInstancedStaticMeshInstanceData>& PerInstanceSMData = Comp->PerInstanceSMData;
232 const FTransform CompTransform = Comp->GetComponentTransform();
234 TArray<FBoundingBox> BoundingBoxes;
237 FString CompName = Comp->GetName();
240 for(
int i = 0; i < PerInstanceSMData.Num(); i++)
242 const FInstancedStaticMeshInstanceData& It = PerInstanceSMData[i];
243 const FTransform InstanceTransform = FTransform(It.Transform);
244 const FVector InstanceLocation = InstanceTransform.GetLocation();
247 const int32 X =
static_cast<int32
>(InstanceLocation.X);
248 const int32 Y =
static_cast<int32
>(InstanceLocation.Y);
249 const int32 Z =
static_cast<int32
>(InstanceLocation.Z);
251 const FString InstanceName = FString::Printf(TEXT(
"%s_Inst_%d_%d"), *ActorName, InstanceCount, i);
252 const FString InstanceIdStr = FString::Printf(TEXT(
"%s_%s_%d_%d_%d_%d"), *ActorName, *CompName, X, Y, Z, InstanceCount);
253 uint64 InstanceId = CityHash64(TCHAR_TO_ANSI(*InstanceIdStr), InstanceIdStr.Len());
256 EnvironmentObject.
Transform = InstanceTransform * CompTransform;
257 EnvironmentObject.
Id = InstanceId;
258 EnvironmentObject.
Name = InstanceName;
259 EnvironmentObject.
IdStr = InstanceIdStr;
260 EnvironmentObject.
Actor = Actor;
261 EnvironmentObject.
CanTick = IsActorTickEnabled;
262 if( i < BoundingBoxes.Num())
276 if(Cast<AInstancedFoliageActor>(Actor))
286 TArray<UStaticMeshComponent*> StaticMeshComps;
287 Actor->GetComponents<UStaticMeshComponent>(StaticMeshComps);
289 TArray<FBoundingBox> BBs;
292 check(BBs.Num() == Tags.Num());
294 const FTransform Transform = Actor->GetTransform();
295 const FString ActorName = Actor->GetName();
296 const bool IsActorTickEnabled = Actor->IsActorTickEnabled();
298 for(
int i = 0; i < BBs.Num(); i++)
300 const FString SMName = FString::Printf(TEXT(
"%s_SM_%d"), *ActorName, i);
304 EnvironmentObject.
Id = CityHash64(TCHAR_TO_ANSI(*SMName), SMName.Len());
305 EnvironmentObject.
Name = SMName;
306 EnvironmentObject.
Actor = Actor;
307 EnvironmentObject.
CanTick = IsActorTickEnabled;
319 TArray<USkeletalMeshComponent*> SkeletalMeshComps;
320 Actor->GetComponents<USkeletalMeshComponent>(SkeletalMeshComps);
322 TArray<FBoundingBox> BBs;
325 check(BBs.Num() == Tags.Num());
327 const FTransform Transform = Actor->GetTransform();
328 const FString ActorName = Actor->GetName();
329 const bool IsActorTickEnabled = Actor->IsActorTickEnabled();
331 for(
int i = 0; i < BBs.Num(); i++)
333 const FString SKMName = FString::Printf(TEXT(
"%s_SKM_%d"), *ActorName, i);
337 EnvironmentObject.
Id = CityHash64(TCHAR_TO_ANSI(*SKMName), SKMName.Len());
338 EnvironmentObject.
Name = SKMName;
339 EnvironmentObject.
Actor = Actor;
340 EnvironmentObject.
CanTick = IsActorTickEnabled;
354 switch (EnvironmentObject.
Type)
379 Actor->SetActorHiddenInGame(!Enable);
380 Actor->SetActorEnableCollision(Enable);
383 Actor->SetActorTickEnabled(Enable);
393 TArray<const UStaticMeshComponent*> ObjectComps;
396 for(
const UStaticMeshComponent* Comp : ObjectComps)
398 UStaticMeshComponent*
SMComp =
const_cast<UStaticMeshComponent*
>(Comp);
399 SMComp->SetHiddenInGame(!Enable);
400 ECollisionEnabled::Type CollisionType = Enable ? ECollisionEnabled::Type::QueryAndPhysics : ECollisionEnabled::Type::NoCollision;
401 SMComp->SetCollisionEnabled(CollisionType);
408 TArray<const UStaticMeshComponent*> ObjectComps;
409 TArray<FString> InstanceName;
410 FTransform InstanceTransform = EnvironmentObject.
Transform;
413 EnvironmentObject.
Name.ParseIntoArray(InstanceName, TEXT(
"_"),
false);
415 int Index = FCString::Atoi(*InstanceName[InstanceName.Num() - 1]);
419 InstanceTransform.SetTranslation(FVector(1000000.0f));
420 InstanceTransform.SetScale3D(FVector(0.0f));
423 UStaticMeshComponent*
SMComp =
const_cast<UStaticMeshComponent*
>(ObjectComps[0]);
424 UInstancedStaticMeshComponent*
ISMComp = Cast<UInstancedStaticMeshComponent>(
SMComp);
425 bool Result =
ISMComp->UpdateInstanceTransform(Index, InstanceTransform,
true,
true);
Base class for CARLA wheeled vehicles.
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
static FString GetTagAsString(crp::CityObjectLabel Tag)
Retrieve the tags of an already tagged actor.
static void GetBBsOfSkeletalMeshComponents(const TArray< USkeletalMeshComponent * > &SkeletalMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static FBoundingBox GetCharacterBoundingBox(const ACharacter *Character, uint8 InTagQueried=0xFF)
static void GetISMBoundingBox(UInstancedStaticMeshComponent *ISMComp, TArray< FBoundingBox > &OutBoundingBox)
static void GetTrafficLightBoundingBox(const ATrafficLightBase *TrafficLight, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static void GetBBsOfStaticMeshComponents(const TArray< UStaticMeshComponent * > &StaticMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static FBoundingBox GetVehicleBoundingBox(const ACarlaWheeledVehicle *Vehicle, uint8 InTagQueried=0xFF)
static void GetMeshCompsFromActorBoundingBox(const AActor *Actor, const FBoundingBox &InBB, TArray< UStaticMeshComponent * > &OutStaticMeshComps)
void RegisterVehicle(ACarlaWheeledVehicle *Vehicle)
void RegisterSMComponents(AActor *Actor)
TArray< FEnvironmentObject > EnvironmentObjects
void EnableEnvironmentObject(FEnvironmentObject &EnvironmentObject, bool Enable)
void EnableISMComp(FEnvironmentObject &EnvironmentObject, bool Enable)
void EnableEnvironmentObjects(const TSet< uint64 > &EnvObjectIds, bool Enable)
void RegisterTrafficLight(ATrafficLightBase *TrafficLight)
void RegisterEnvironmentObject(AActor *Actor, FBoundingBox &BoundingBox, EnvironmentObjectType Type, uint8 Tag)
void RegisterObjects(TArray< AActor * > Actors)
void RegisterSKMComponents(AActor *Actor)
TArray< FEnvironmentObject > GetEnvironmentObjects(uint8 InTagQueried=0xFF) const
void RegisterCharacter(ACharacter *Character)
void EnableActor(FEnvironmentObject &EnvironmentObject, bool Enable)
TMultiMap< uint64, const UStaticMeshComponent * > ObjectIdToComp
void EnableTrafficLight(FEnvironmentObject &EnvironmentObject, bool Enable)
int FoliageActorInstanceCount
void RegisterISMComponents(AActor *Actor)
EnvironmentObjectType Type
crp::CityObjectLabel ObjectLabel