Added filters and operation reverse

This commit is contained in:
Blyron 2024-05-29 14:53:17 +02:00 committed by Blyron
parent bb416218df
commit 605817e1b8
2 changed files with 39 additions and 5 deletions

View File

@ -239,7 +239,7 @@ void UMapGenFunctionLibrary::CleanupGEngine(){
#endif
}
void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, int MinNumOfInstancesToBeChanged)
void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray<UStaticMesh*> Filter, int MinNumOfInstancesToBeChanged)
{
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(World, AStaticMeshActor::StaticClass(), FoundActors);
@ -249,8 +249,11 @@ void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshe
AStaticMeshActor* CurrentStaticMeshActor = Cast<AStaticMeshActor>(CurrentActor);
UStaticMeshComponent* CurrentStaticMeshComponent = CurrentStaticMeshActor->GetStaticMeshComponent();
UStaticMesh* CurrentStaticMesh = CurrentStaticMeshComponent->GetStaticMesh();
TArray<AStaticMeshActor*>& Instances = ActorsToCheckIfReplacedMap.FindOrAdd(CurrentStaticMesh);
Instances.Add(CurrentStaticMeshActor);
if( Filter.IsEmpty() || Filter.Contains(CurrentStaticMesh) )
{
TArray<AStaticMeshActor*>& 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<AInstancedStaticMeshActor>();
if(InstancedStaticMeshActor)
{
@ -272,3 +276,29 @@ void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshe
}
}
}
void UMapGenFunctionLibrary::RevertStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, TArray<UStaticMesh*> Filter)
{
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(World, AInstancedStaticMeshActor::StaticClass(), FoundActors);
for(AActor* CurrentActor : FoundActors )
{
AInstancedStaticMeshActor* InstancedStaticMeshActor = Cast<AInstancedStaticMeshActor>(CurrentActor);
if(InstancedStaticMeshActor)
{
UStaticMesh* CurrentStaticMesh = InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->GetStaticMesh();
if( Filter.IsEmpty() || Filter.Contains(CurrentStaticMesh))
{
const TArray<FInstancedStaticMeshInstanceData>& Instances = InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->PerInstanceSMData;
for( FInstancedStaticMeshInstanceData CurrentInstanceData : Instances )
{
AStaticMeshActor* StaticMeshActor = World->SpawnActor<AStaticMeshActor>();
StaticMeshActor->GetStaticMeshComponent()->SetStaticMesh(CurrentStaticMesh);
StaticMeshActor->SetActorTransform(FTransform(CurrentInstanceData.Transform));
}
InstancedStaticMeshActor->Destroy();
}
}
}
}

View File

@ -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<UStaticMesh*> 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<UStaticMesh*> Filter);
};