Optimization Help Volume(C++ modification)

Unreal Engine 4 has a lot of features to help you optimize. But some features may be needed to optimize a specific area.

In this post, let's create a volume that calculates the sum of the vertices of the Actor contained in itself.
Maybe it will help to optimize the building or grassland area.


Implementation

C ++ code work is required.

OptimizationHelpVolume.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Volume.h"
#include "OptimizationHelpVolume.generated.h"

/**
 * Shows information related to optimization of all actors in the volume.
 */
UCLASS()
class TEST01_API AOptimizationHelpVolume : public AVolume
{
 GENERATED_UCLASS_BODY()
 
public:
 
 UPROPERTY(VisibleInstanceOnly, Category = "Optimization", Transient)
 int TotalVertexCount;

protected:
 virtual void PostEditMove(bool bFinished);
 virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;

 void UpdateInfo();
};


OptimizationHelpVolume.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "OptimizationHelpVolume.h"

#include "EngineUtils.h"
#include "Engine/StaticMeshActor.h"
#include "Components/BrushComponent.h"
#include "Engine/CollisionProfile.h"

AOptimizationHelpVolume::AOptimizationHelpVolume(const FObjectInitializer& ObjectInitializer)
 : Super(ObjectInitializer)
{
 GetBrushComponent()->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
 GetBrushComponent()->bAlwaysCreatePhysicsState = true;

 TotalVertexCount = -1;
}

void AOptimizationHelpVolume::PostEditMove(bool bFinished)
{
 Super::PostEditMove(bFinished);

 UpdateInfo();
}

void AOptimizationHelpVolume::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
 Super::PostEditChangeProperty(PropertyChangedEvent);

 UpdateInfo();
}


void AOptimizationHelpVolume::UpdateInfo()
{
 TotalVertexCount = 0;

 UWorld* IteratorWorld = GWorld;
 for (FActorIterator Iter(IteratorWorld); Iter; ++Iter)
 {
  AStaticMeshActor *SMA = Cast<AStaticMeshActor>(*Iter);
  if (SMA)
  {
   FVector loc = SMA->GetActorLocation();

   float OutDistanceToPoint = 0.0f;

   if (EncompassesPoint(loc, 0.0f, &OutDistanceToPoint))
   {
    UStaticMeshComponent* SMC = SMA->GetStaticMeshComponent();
    if (SMC)
    {
     UStaticMesh* SM = SMC->GetStaticMesh();
     if (SM)
     {
      TotalVertexCount += SM->GetNumVertices(0);
     }
    }
   }
  }
 }

 //UE_LOG(LogTemp, Warning, TEXT("TotalVertexCount : %d"), TotalVertexCount);
}


More...

You need to do the following additional things :
  • The current implementation has the problem that it can not be updated automatically at first.
  • You should show more information such as the number of triangles.
  • Actors are more accurate to account for the size that they actually draw.

Code highlight by http://hilite.me/
Thank you!

Comments

Popular posts from this blog

Liquid material in a bottle

How to Make Circular Progress Bar(or Rounded Rectangle)

MatCap material