From 605817e1b800bea84fc648d116f7d92df3a6fb9c Mon Sep 17 00:00:00 2001 From: Blyron Date: Wed, 29 May 2024 14:53:17 +0200 Subject: [PATCH] Added filters and operation reverse --- .../BlueprintLibary/MapGenFunctionLibrary.cpp | 38 +++++++++++++++++-- .../BlueprintLibary/MapGenFunctionLibrary.h | 6 ++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.cpp b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.cpp index 075c001c4..a030e9f86 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.cpp +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.cpp @@ -239,7 +239,7 @@ void UMapGenFunctionLibrary::CleanupGEngine(){ #endif } -void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, int MinNumOfInstancesToBeChanged) +void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray Filter, int MinNumOfInstancesToBeChanged) { TArray FoundActors; UGameplayStatics::GetAllActorsOfClass(World, AStaticMeshActor::StaticClass(), FoundActors); @@ -249,8 +249,11 @@ void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshe AStaticMeshActor* CurrentStaticMeshActor = Cast(CurrentActor); UStaticMeshComponent* CurrentStaticMeshComponent = CurrentStaticMeshActor->GetStaticMeshComponent(); UStaticMesh* CurrentStaticMesh = CurrentStaticMeshComponent->GetStaticMesh(); - TArray& Instances = ActorsToCheckIfReplacedMap.FindOrAdd(CurrentStaticMesh); - Instances.Add(CurrentStaticMeshActor); + if( Filter.IsEmpty() || Filter.Contains(CurrentStaticMesh) ) + { + TArray& Instances = ActorsToCheckIfReplacedMap.FindOrAdd(CurrentStaticMesh); + Instances.Add(CurrentStaticMeshActor); + } } for(auto CurrentPair : ActorsToCheckIfReplacedMap) @@ -261,8 +264,9 @@ void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshe for( AStaticMeshActor* SMActor : CurrentPair.Value ) { TransformsToBeInstanced.Add(SMActor->GetActorTransform()); + SMActor->Destroy(); } - + AInstancedStaticMeshActor* InstancedStaticMeshActor = World->SpawnActor(); if(InstancedStaticMeshActor) { @@ -272,3 +276,29 @@ void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshe } } } + +void UMapGenFunctionLibrary::RevertStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray Filter) +{ + TArray FoundActors; + UGameplayStatics::GetAllActorsOfClass(World, AInstancedStaticMeshActor::StaticClass(), FoundActors); + + for(AActor* CurrentActor : FoundActors ) + { + AInstancedStaticMeshActor* InstancedStaticMeshActor = Cast(CurrentActor); + if(InstancedStaticMeshActor) + { + UStaticMesh* CurrentStaticMesh = InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->GetStaticMesh(); + if( Filter.IsEmpty() || Filter.Contains(CurrentStaticMesh)) + { + const TArray& Instances = InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->PerInstanceSMData; + for( FInstancedStaticMeshInstanceData CurrentInstanceData : Instances ) + { + AStaticMeshActor* StaticMeshActor = World->SpawnActor(); + StaticMeshActor->GetStaticMeshComponent()->SetStaticMesh(CurrentStaticMesh); + StaticMeshActor->SetActorTransform(FTransform(CurrentInstanceData.Transform)); + } + InstancedStaticMeshActor->Destroy(); + } + } + } +} diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.h b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.h index 9d617eb5e..1618cbb3c 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.h +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/BlueprintLibary/MapGenFunctionLibrary.h @@ -55,5 +55,9 @@ public: // This function will count instances of each static mesh in the level, if there are > MinNumOfInstancesToBeChanged they will be changed by instanced static meshes // to reduce draw calls UFUNCTION(BlueprintCallable) - static void ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, int MinNumOfInstancesToBeChanged = 20); + static void ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray Filter, int MinNumOfInstancesToBeChanged = 20); + + // This function will removed instanced static meshes in the level and place static mesh actors instead + UFUNCTION(BlueprintCallable) + static void RevertStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray Filter); };