13 #include "InstancedFoliageActor.h" 14 #include "GameFramework/Character.h" 17 #include "FileHelper.h" 25 TArray<FEnvironmentObject> Result;
32 if(!FilterByTagEnabled || (It.ObjectLabel == TagQueried))
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,
102 EFileWrite::FILEWRITE_Silent);
104 #endif // WITH_EDITOR 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;
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();
177 const FString ActorName = TrafficLight->GetName();
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())
264 EnvironmentObject.BoundingBox = BoundingBoxes[i];
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;
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;
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);
static void GetBBsOfSkeletalMeshComponents(const TArray< USkeletalMeshComponent *> &SkeletalMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
EnvironmentObjectType Type
void RegisterVehicle(ACarlaWheeledVehicle *Vehicle)
TArray< FEnvironmentObject > GetEnvironmentObjects(uint8 InTagQueried=0xFF) const
static void GetISMBoundingBox(UInstancedStaticMeshComponent *ISMComp, TArray< FBoundingBox > &OutBoundingBox)
void RegisterEnvironmentObject(AActor *Actor, FBoundingBox &BoundingBox, EnvironmentObjectType Type, uint8 Tag)
void EnableTrafficLight(FEnvironmentObject &EnvironmentObject, bool Enable)
static FBoundingBox GetCharacterBoundingBox(const ACharacter *Character, uint8 InTagQueried=0xFF)
void EnableISMComp(FEnvironmentObject &EnvironmentObject, bool Enable)
void EnableActor(FEnvironmentObject &EnvironmentObject, bool Enable)
void RegisterCharacter(ACharacter *Character)
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
static FBoundingBox GetVehicleBoundingBox(const ACarlaWheeledVehicle *Vehicle, uint8 InTagQueried=0xFF)
void EnableEnvironmentObjects(const TSet< uint64 > &EnvObjectIds, bool Enable)
static T Get(carla::rpc::Response< T > &response)
static void GetTrafficLightBoundingBox(const ATrafficLightBase *TrafficLight, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
void RegisterTrafficLight(ATrafficLightBase *TrafficLight)
carla::SharedPtr< cc::Actor > Actor
void RegisterISMComponents(AActor *Actor)
void RegisterSMComponents(AActor *Actor)
static FString GetTagAsString(crp::CityObjectLabel Tag)
Retrieve the tags of an already tagged actor.
TArray< FEnvironmentObject > EnvironmentObjects
static void GetBBsOfStaticMeshComponents(const TArray< UStaticMeshComponent *> &StaticMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
void RegisterSKMComponents(AActor *Actor)
void EnableEnvironmentObject(FEnvironmentObject &EnvironmentObject, bool Enable)
TMultiMap< uint64, const UStaticMeshComponent * > ObjectIdToComp
void RegisterObjects(TArray< AActor *> Actors)
static void GetMeshCompsFromActorBoundingBox(const AActor *Actor, const FBoundingBox &InBB, TArray< UStaticMeshComponent *> &OutStaticMeshComps)
crp::CityObjectLabel ObjectLabel
int FoliageActorInstanceCount
Base class for CARLA wheeled vehicles.
geom::Transform Transform