Create base class for hierachical instanced static mesh, removed Blueprint one
This commit is contained in:
parent
85286d7565
commit
bb416218df
|
@ -0,0 +1,17 @@
|
|||
// Copyright (c) 2024 Computer Vision Center (CVC) at the Universitat Autonoma
|
||||
// de Barcelona (UAB).
|
||||
//
|
||||
// This work is licensed under the terms of the MIT license.
|
||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#include "Carla/Actor/LevelActor/InstancedStaticMeshActor.h"
|
||||
#include "Components/HierarchicalInstancedStaticMeshComponent.h"
|
||||
|
||||
AInstancedStaticMeshActor::AInstancedStaticMeshActor(const FObjectInitializer& ObjectInitializer)
|
||||
: Super(ObjectInitializer)
|
||||
{
|
||||
PrimaryActorTick.bCanEverTick = false;
|
||||
InstancedStaticMeshComponent = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>("InstancedStaticMeshComponent");
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) 2024 Computer Vision Center (CVC) at the Universitat Autonoma
|
||||
// de Barcelona (UAB).
|
||||
//
|
||||
// This work is licensed under the terms of the MIT license.
|
||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
|
||||
#include "InstancedStaticMeshActor.generated.h"
|
||||
|
||||
|
||||
class UHierarchicalInstancedStaticMeshComponent;
|
||||
|
||||
UCLASS()
|
||||
class CARLA_API AInstancedStaticMeshActor : public AActor
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
AInstancedStaticMeshActor(const FObjectInitializer &ObjectInitializer);
|
||||
|
||||
UFUNCTION(BlueprintPure, Category="Getter")
|
||||
UHierarchicalInstancedStaticMeshComponent* GetInstancedStaticMeshComponent(){
|
||||
return InstancedStaticMeshComponent;
|
||||
}
|
||||
|
||||
protected:
|
||||
UPROPERTY(VisibleAnywhere, Category="Components")
|
||||
UHierarchicalInstancedStaticMeshComponent* InstancedStaticMeshComponent;
|
||||
};
|
|
@ -12,6 +12,8 @@
|
|||
#include "StaticMeshAttributes.h"
|
||||
#include "RenderingThread.h"
|
||||
#include "PhysicsEngine/BodySetup.h"
|
||||
#include "Engine/StaticMeshActor.h"
|
||||
#include "Carla/Actor/LevelActor/InstancedStaticMeshActor.h"
|
||||
#if WITH_EDITOR
|
||||
#include "Editor/EditorEngine.h"
|
||||
#include "Editor/Transactor.h"
|
||||
|
@ -236,3 +238,37 @@ void UMapGenFunctionLibrary::CleanupGEngine(){
|
|||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void UMapGenFunctionLibrary::ChangeStaticMeshesInTheLevelForInstancedStaticMeshes(UWorld* World, int MinNumOfInstancesToBeChanged)
|
||||
{
|
||||
TArray<AActor*> FoundActors;
|
||||
UGameplayStatics::GetAllActorsOfClass(World, AStaticMeshActor::StaticClass(), FoundActors);
|
||||
TMap<UStaticMesh*, TArray<AStaticMeshActor*>> ActorsToCheckIfReplacedMap;
|
||||
for(AActor* CurrentActor : FoundActors )
|
||||
{
|
||||
AStaticMeshActor* CurrentStaticMeshActor = Cast<AStaticMeshActor>(CurrentActor);
|
||||
UStaticMeshComponent* CurrentStaticMeshComponent = CurrentStaticMeshActor->GetStaticMeshComponent();
|
||||
UStaticMesh* CurrentStaticMesh = CurrentStaticMeshComponent->GetStaticMesh();
|
||||
TArray<AStaticMeshActor*>& Instances = ActorsToCheckIfReplacedMap.FindOrAdd(CurrentStaticMesh);
|
||||
Instances.Add(CurrentStaticMeshActor);
|
||||
}
|
||||
|
||||
for(auto CurrentPair : ActorsToCheckIfReplacedMap)
|
||||
{
|
||||
if(CurrentPair.Value.Num() > MinNumOfInstancesToBeChanged)
|
||||
{
|
||||
TArray<FTransform> TransformsToBeInstanced;
|
||||
for( AStaticMeshActor* SMActor : CurrentPair.Value )
|
||||
{
|
||||
TransformsToBeInstanced.Add(SMActor->GetActorTransform());
|
||||
}
|
||||
|
||||
AInstancedStaticMeshActor* InstancedStaticMeshActor = World->SpawnActor<AInstancedStaticMeshActor>();
|
||||
if(InstancedStaticMeshActor)
|
||||
{
|
||||
InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->SetStaticMesh(CurrentPair.Key);
|
||||
InstancedStaticMeshActor->GetInstancedStaticMeshComponent()->AddInstances(TransformsToBeInstanced, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,4 +51,9 @@ public:
|
|||
|
||||
UFUNCTION(BlueprintCallable)
|
||||
static void CleanupGEngine();
|
||||
|
||||
// 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);
|
||||
};
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue