Added extra functionality to the Procedural Building class

This commit is contained in:
Antonio Mata 2024-06-06 12:13:16 +02:00 committed by Blyron
parent becd102516
commit 8a8e1cb7cc
2 changed files with 43 additions and 0 deletions

View File

@ -13,6 +13,7 @@
#include "Materials/MaterialInstanceConstant.h"
#include "MeshMergeModule.h"
#include "ProceduralMeshComponent.h"
#include "Components/HierarchicalInstancedStaticMeshComponent.h"
#include "UObject/Class.h"
#include "UObject/UObjectGlobals.h"
#include "UObject/SavePackage.h"
@ -214,6 +215,44 @@ void AProceduralBuildingUtilities::CookProceduralMeshToMesh(
SaveArgs);
}
void AProceduralBuildingUtilities::PlaceBuilding(AActor* Parent, TArray<UHierarchicalInstancedStaticMeshComponent*> Components, const FString& Name)
{
//Security wall.
if(Parent == nullptr) return;
//We need to create a component. This line is just for readability purposes.
UClass* HSMClass = UHierarchicalInstancedStaticMeshComponent::StaticClass();
//For every hierarchichal component we passed in the function, set
//In the parent actor a new hierarchichal component that copies
//the instances of the passed one.
for(int i = 0; i < Components.Num(); i++)
{
//Creates the component. The index is needed so every component has a unique name, if not, each iteration
//will just override the previous one.
UHierarchicalInstancedStaticMeshComponent* NewComponent =
NewObject<UHierarchicalInstancedStaticMeshComponent>(Parent, HSMClass, FName(Name + FString::FromInt(i)));
//Sets static mesh
NewComponent->SetStaticMesh(Components[i]->GetStaticMesh());
//Sets the instances transform.
for(int j = 0; j < Components[i]->GetInstanceCount(); j++)
{
FTransform InstanceTransform;
Components[i]->GetInstanceTransform(j, InstanceTransform, false);
NewComponent->AddInstance(InstanceTransform, false);
}
//Registers the component in the parent actor and makes it visible for the user.
NewComponent->RegisterComponent();
NewComponent->AttachToComponent(
Parent->GetRootComponent(),
FAttachmentTransformRules::SnapToTargetIncludingScale);
Parent->AddInstanceComponent(NewComponent);
}
}
UMaterialInstanceConstant* AProceduralBuildingUtilities::GenerateBuildingMaterialAsset(
const FString& DuplicateParentPath,
const FString& DestinationPath,

View File

@ -8,6 +8,7 @@
class USceneCaptureComponent2D;
class UTexture2D;
class UHierarchicalInstancedStaticMeshComponent;
UENUM(BlueprintType)
enum EBuildingCameraView
@ -42,6 +43,9 @@ public:
UFUNCTION(BlueprintCallable, Category="Procedural Building Utilities")
void CookProceduralBuildingToMesh(const FString& DestinationPath, const FString& FileName);
UFUNCTION(BlueprintCallable, Category="Procedural Building Utilities")
void PlaceBuilding(AActor* Parent, TArray<UHierarchicalInstancedStaticMeshComponent*> Components, const FString& Name);
UFUNCTION(BlueprintCallable, Category="Procedural Building Utilities")
void CookProceduralMeshToMesh(class UProceduralMeshComponent* Mesh, const FString& DestinationPath, const FString& FileName);