11 PrimaryActorTick.bCanEverTick =
true;
17 RootSMComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(
"RootComponent"));
19 RootComponent->SetMobility(EComponentMobility::Static);
23 const UStaticMesh* SM)
26 FString SMName = SM->GetName();
28 UHierarchicalInstancedStaticMeshComponent** HISMCompPtr =
HISMComps.Find(SMName);
30 if(HISMCompPtr)
return *HISMCompPtr;
32 UHierarchicalInstancedStaticMeshComponent* HISMComp = *HISMCompPtr;
35 HISMComp = NewObject<UHierarchicalInstancedStaticMeshComponent>(
this,
36 FName(*FString::Printf(TEXT(
"HISMComp_%d"),
HISMComps.Num())));
37 HISMComp->SetupAttachment(RootComponent);
38 HISMComp->RegisterComponent();
41 HISMComp->SetStaticMesh(const_cast<UStaticMesh*>(SM));
53 ChildComp->SetMobility(EComponentMobility::Type::Static);
62 TArray<UHierarchicalInstancedStaticMeshComponent*> OtherHISMComps;
63 BP_Building->GetComponents<UHierarchicalInstancedStaticMeshComponent>(OtherHISMComps);
65 for(UHierarchicalInstancedStaticMeshComponent* OtherHISMComp : OtherHISMComps)
67 const UStaticMesh* SM = OtherHISMComp->GetStaticMesh();
70 UHierarchicalInstancedStaticMeshComponent* NewHISMComp =
GetHISMComp(SM);
73 const TArray<FInstancedStaticMeshInstanceData>& PerInstanceSMData = OtherHISMComp->PerInstanceSMData;
75 for(
const FInstancedStaticMeshInstanceData& InstSMIData : PerInstanceSMData)
77 FTransform
Transform = FTransform(InstSMIData.Transform);
79 NewHISMComp->AddInstance(Transform);
84 TArray<UChildActorComponent*> OtherChildComps;
85 BP_Building->GetComponents<UChildActorComponent>(OtherChildComps);
87 for(
const UChildActorComponent* OtherChildActor : OtherChildComps)
90 UChildActorComponent* ChildActorComp = NewObject<UChildActorComponent>(
this,
91 FName(*FString::Printf(TEXT(
"ChildActorComp_%d"),
ChildActorComps.Num() )));
92 ChildActorComp->SetMobility(EComponentMobility::Type::Static);
93 ChildActorComp->SetupAttachment(RootComponent);
96 ChildActorComp->SetChildActorClass(OtherChildActor->GetChildActorClass());
97 ChildActorComp->SetRelativeTransform(OtherChildActor->GetRelativeTransform());
100 ChildActorComp->RegisterComponent();
102 AActor* NewChildActor = ChildActorComp->GetChildActor();
106 NewChildActor->SetFolderPath(FName( *FString::Printf(TEXT(
"/Buildings/%s"), *GetName())));
110 TArray<UStaticMeshComponent*> NewSMComps;
111 NewChildActor->GetComponents<UStaticMeshComponent>(NewSMComps);
114 UStaticMeshComponent* PivotSMComp = NewSMComps[0];
115 PivotSMComp->SetVisibility(
false,
false);
126 AActor* ChildActor = ChildActorComp->GetChildActor();
127 TArray<UStaticMeshComponent*> SMComps;
128 ChildActor->GetComponents<UStaticMeshComponent>(SMComps);
129 if(SMComps.Num() > 0)
131 SMComps[0]->SetVisibility(
false,
false);
137 const TSet<int>& InDoorsIndexPosition,
138 const TArray<bool>& InUseWallMesh,
143 bool InUseFullBlocks)
155 const TArray<bool>& InSideVisibility,
156 const TArray<bool>& InCornerVisibility,
157 bool InRoofVisibility)
165 const TArray<UStaticMesh*>& InBaseMeshes,
166 const TArray<TSubclassOf<AActor>>& InBaseBPs,
167 const TArray<UStaticMesh*>& InCornerBaseMeshes,
168 const TArray<TSubclassOf<AActor>>& InCornerBaseBPs,
169 const TArray<UStaticMesh*>& InDoorMeshes,
170 const TArray<TSubclassOf<AActor>>& InDoorBPs)
181 const TArray<UStaticMesh*>& InBodyMeshes,
182 const TArray<TSubclassOf<AActor>>& InBodyBPs,
183 const TArray<UStaticMesh*>& InCornerBodyMeshes,
184 const TArray<TSubclassOf<AActor>>& InCornerBodyBPs,
185 const TArray<UStaticMesh*>& InWallMeshes,
186 const TArray<TSubclassOf<AActor>>& InWallBPs)
197 const TArray<UStaticMesh*>& InTopMeshes,
198 const TArray<TSubclassOf<AActor>>& InTopBPs,
199 const TArray<UStaticMesh*>& InCornerTopMeshes,
200 const TArray<TSubclassOf<AActor>>& InCornerTopBPs,
201 const TArray<UStaticMesh*>& InRoofMeshes,
202 const TArray<TSubclassOf<AActor>>& InRoofBPs)
248 const TSet <UActorComponent*> Comps = GetComponents();
253 const FString& MeshName = It.Key;
254 UHierarchicalInstancedStaticMeshComponent* HISMComp = It.Value;
256 HISMComp->ClearInstances();
266 ChildActorComp->DestroyComponent();
269 ChildActorComps.Reset();
288 int SideLengthAcumulator = 0;
292 TSet<int> AuxiliarPositions;
294 bool MainVisibility =
true;
295 bool CornerVisbility =
true;
309 float SideMaxZ =
CreateSide(MeshCollection, AuxiliarPositions, SideLength, MainVisibility, CornerVisbility);
310 MaxZ = (MaxZ < SideMaxZ) ? SideMaxZ : MaxZ;
313 SideLengthAcumulator += SideLength;
318 const FQuat RotationToAdd = FRotator(0.0f, 90.0f, 0.0f).Quaternion();
324 FVector NewLocation =
CurrentTransform.GetTranslation() + FVector(0.0f, 0.0f, MaxZ);
331 UStaticMesh* SelectedMesh =
nullptr;
332 TSubclassOf<AActor> SelectedBP =
nullptr;
333 FBox SelectedMeshBounds;
339 BoxSize = FVector(0.0f, -BoxSize.Y, 0.0f);
343 if(AreRoofMeshesAvailable)
346 for(
int i = 0; i <
LengthY; i++)
349 for(
int j = 0; j <
LengthX; j++)
368 const TSet<int>& AuxiliarPositions,
371 bool CornerVisbility)
373 const TArray<UStaticMesh*>* MainMeshes = MeshCollection.
MainMeshes;
374 const TArray<TSubclassOf<AActor>>* MainBPs = MeshCollection.
MainBPs;
375 const TArray<UStaticMesh*>* CornerMeshes = MeshCollection.
CornerMeshes;
376 const TArray<TSubclassOf<AActor>>* CornerBPs = MeshCollection.
CornerBPs;
377 const TArray<UStaticMesh*>* AuxiliarMeshes = MeshCollection.
AuxiliarMeshes;
378 const TArray<TSubclassOf<AActor>>* AuxiliarBPs = MeshCollection.
AuxiliarBPs;
384 UStaticMesh* SelectedMesh =
nullptr;
385 TSubclassOf<AActor> SelectedBP =
nullptr;
386 FBox SelectedMeshBounds;
390 bool AreMainMeshesAvailable = (MainMeshes && (MainMeshes->Num() > 0)) || (MainBPs && (MainBPs->Num() > 0));
391 bool AreAuxMeshesAvailable = (MainMeshes && (MainMeshes->Num() > 0)) || (MainBPs && (MainBPs->Num() > 0));
393 for(
int i = 0; (i < SideLength) && AreMainMeshesAvailable; i++)
395 const int* AuxiliarPosition = AuxiliarPositions.Find(i);
396 if(AreAuxMeshesAvailable && AuxiliarPosition)
407 float ChunkZ =
AddChunck(SelectedMesh, SelectedBP, MainVisibility, SelectedMeshBounds);
408 MaxZ = (MaxZ < ChunkZ) ? ChunkZ : MaxZ;
414 bool AreCornerMeshesAvailable = (CornerMeshes && (CornerMeshes->Num() > 0)) || (CornerBPs && (CornerBPs->Num() > 0));
415 if(
Corners && AreCornerMeshesAvailable)
419 float ChunkZ =
AddChunck(SelectedMesh, SelectedBP, CornerVisbility, SelectedMeshBounds);
420 MaxZ = (MaxZ < ChunkZ) ? ChunkZ : MaxZ;
455 int MaxIndex = StartIndex + Length;
459 if( StartIndex <= i && i < MaxIndex )
461 int RelativePostion = i - StartIndex;
462 Result.Emplace(RelativePostion);
471 for(
int i = 0; i < Length; i++)
485 const TArray<UStaticMesh*>& InMeshes,
486 const TArray<TSubclassOf<AActor>>& InBPs,
487 UStaticMesh** OutMesh =
nullptr,
488 TSubclassOf<AActor>* OutBP =
nullptr)
490 int NumMeshes = InMeshes.Num();
491 int NumBPs = InBPs.Num();
492 int Range = NumMeshes + NumBPs;
494 int Choosen = FMath::RandRange(0, Range - 1);
496 if(Choosen < NumMeshes)
498 *OutMesh = InMeshes[Choosen];
500 if(NumMeshes <= Choosen && Choosen < NumBPs)
502 *OutBP = InBPs[Choosen - NumMeshes];
507 const UStaticMesh* SelectedMesh,
508 const TSubclassOf<AActor> SelectedBP,
510 FBox& OutSelectedMeshBounds)
522 Result = MeshBound.Z;
525 OutSelectedMeshBounds = SelectedMesh->GetBoundingBox();
531 UChildActorComponent* ChildActorComp = NewObject<UChildActorComponent>(
this,
532 FName(*FString::Printf(TEXT(
"ChildActorComp_%d"),
ChildActorComps.Num() )));
533 ChildActorComp->SetMobility(EComponentMobility::Type::Static);
534 ChildActorComp->SetupAttachment(RootComponent);
537 ChildActorComp->SetChildActorClass(SelectedBP);
541 ChildActorComp->RegisterComponent();
543 AActor* ChildActor = ChildActorComp->GetChildActor();
547 ChildActor->SetFolderPath(FName( *FString::Printf(TEXT(
"/Buildings/%s"), *GetName())));
551 TArray<UStaticMeshComponent*> SMComps;
552 UStaticMeshComponent* PivotSMComp =
nullptr;
554 ChildActor->GetComponents<UStaticMeshComponent>(SMComps);
557 PivotSMComp = SMComps[0];
558 const UStaticMesh* SM = PivotSMComp->GetStaticMesh();
567 ChildActorComp->DestroyComponent();
571 Result = MeshBound.Z;
576 PivotSMComp->SetVisibility(
false,
false);
577 OutSelectedMeshBounds = SM->GetBoundingBox();
587 UHierarchicalInstancedStaticMeshComponent* HISMComp =
GetHISMComp(SM);
593 FBox
Box = SM->GetBoundingBox();
594 return Box.GetSize();
604 FVector ForwardVector = -Rotation.GetForwardVector();
606 FVector NewLocation =
CurrentTransform.GetTranslation() + ForwardVector * Box.X;
618 FVector RightVector = -Rotation.GetRightVector();
619 FVector Size = Box.GetSize();
621 FVector NewLocation =
CurrentTransform.GetTranslation() + RightVector * Size.Y;
void AddMeshToBuilding(const UStaticMesh *SM)
void SetTopMeshes(const TArray< UStaticMesh *> &InTopMeshes, const TArray< TSubclassOf< AActor >> &InTopBPs, const TArray< UStaticMesh *> &InCornerTopMeshes, const TArray< TSubclassOf< AActor >> &InCornerTopBPs, const TArray< UStaticMesh *> &InRoofMeshes, const TArray< TSubclassOf< AActor >> &InRoofBPs)
UHierarchicalInstancedStaticMeshComponent * GetHISMComp(const UStaticMesh *SM)
TSet< int > DoorsIndexPosition
Base Parameters.
TMap< FString, UHierarchicalInstancedStaticMeshComponent * > HISMComps
TArray< TSubclassOf< AActor > > * MainBPs
TArray< TSubclassOf< AActor > > * AuxiliarBPs
TArray< UStaticMesh * > CornerBaseMeshes
void CreateFloor(const FloorMeshCollection &MeshCollection, bool IncludeDoors, bool IncludeWalls)
TArray< UStaticMesh * > * MainMeshes
TArray< TSubclassOf< AActor > > * CornerBPs
void SetBodyMeshes(const TArray< UStaticMesh *> &InBodyMeshes, const TArray< TSubclassOf< AActor >> &InBodyBPs, const TArray< UStaticMesh *> &InCornerBodyMeshes, const TArray< TSubclassOf< AActor >> &InCornerBodyBPs, const TArray< UStaticMesh *> &InWallMeshes, const TArray< TSubclassOf< AActor >> &InWallBPs)
TArray< TSubclassOf< AActor > > RoofBPs
TArray< UStaticMesh * > * AuxiliarMeshes
FTransform CurrentTransform
TSet< int > CalculateDoorsIndexInSide(int StartIndex, int Length)
void CalculateSideVisibilities(int SideIndex, bool &MainVisibility, bool &CornerVisbility)
TArray< TSubclassOf< AActor > > TopBPs
void ChooseGeometryToSpawn(const TArray< UStaticMesh *> &InMeshes, const TArray< TSubclassOf< AActor >> &InMainBPs, UStaticMesh **OutMesh, TSubclassOf< AActor > *OutBP)
TArray< UStaticMesh * > BaseMeshes
Meshes.
void ConvertOldBP_ToNativeCodeObject(AActor *BP_Building)
void CalculateSidesLength()
void SetBaseMeshes(const TArray< UStaticMesh *> &InBaseMeshes, const TArray< TSubclassOf< AActor >> &InBaseBPs, const TArray< UStaticMesh *> &InCornerBaseMeshes, const TArray< TSubclassOf< AActor >> &InCornerBaseBPs, const TArray< UStaticMesh *> &InDoorMeshes, const TArray< TSubclassOf< AActor >> &InDoorBPs)
TArray< bool > SideVisibility
Base Parameters | Visibility.
bg::model::box< Point3D > Box
TArray< TSubclassOf< AActor > > BodyBPs
TArray< TSubclassOf< AActor > > WallBPs
TArray< UStaticMesh * > CornerBodyMeshes
TSet< int > GenerateWallsIndexPositions(int Length)
TArray< UStaticMesh * > BodyMeshes
Meshes | Body.
TArray< UStaticMesh * > WallMeshes
void SetVisibilityParameters(const TArray< bool > &InSideVisibility, const TArray< bool > &InCornerVisibility, bool InRoofVisibility)
void SetBaseParameters(const TSet< int > &InDoorsIndexPosition, const TArray< bool > &InUseWallMesh, int InNumFloors, int InLengthX, int InLengthY, bool InCorners, bool InUseFullBlocks)
TArray< UStaticMesh * > CornerTopMeshes
float CreateSide(const FloorMeshCollection &MeshCollection, const TSet< int > &AuxiliarPositions, int SideLength, bool MainVisibility, bool CornerVisbility)
TArray< UStaticMesh * > TopMeshes
Meshes | Top.
TArray< TSubclassOf< AActor > > DoorBPs
FVector GetMeshSize(const UStaticMesh *SM)
TArray< UChildActorComponent * > ChildActorComps
TArray< bool > UseWallMesh
TArray< UStaticMesh * > RoofMeshes
TArray< UStaticMesh * > * CornerMeshes
TArray< TSubclassOf< AActor > > CornerBaseBPs
void UpdateTransformPositionToNextSide(const FBox &Box)
TArray< UStaticMesh * > DoorMeshes
TArray< int > SidesLength
TArray< TSubclassOf< AActor > > CornerBodyBPs
float AddChunck(const UStaticMesh *SelectedMesh, const TSubclassOf< AActor > SelectedBP, bool Visible, FBox &OutSelectedMeshBounds)
FBox LastSelectedMeshBounds
TArray< bool > CornerVisibility
TArray< TSubclassOf< AActor > > CornerTopBPs
geom::Transform Transform
UStaticMeshComponent * RootSMComp
TArray< TSubclassOf< AActor > > BaseBPs
void UpdateTransformPositionToNextChunk(const FVector &Box)