I am changing my procedural terrain system to use DynamicMeshComponent instead of ProceduralMeshComponent, but i’ve notices slower results. I’ve heard alot of people say that DMC is better then PMC, so I think I’m doing something wrong.
I currently have a dummy class to test both DMC and PMC.
void ADynamicTerrain::CreateMeshSection_Dynamic(int32 Size, UMaterialInstance* Material, bool Collisions)
{
FDynamicMesh3 MeshInfo;
MeshInfo.EnableAttributes();
MeshInfo.EnableVertexUVs(FVector2f::Zero());
for (int32 x = 0; x <= Size; x++)
{
for (int32 y = 0; y <= Size; y++)
{
const int32 VertexID = MeshInfo.AppendVertex(FVector3d(x * 100, y * 100, 0));
MeshInfo.SetVertexUV(VertexID, FVector2f(x,y));
}
}
for (int32 x = 0; x < Size; x++)
{
for (int32 y = 0; y < Size; y++)
{
int32 topLeft = x * (Size + 1) + y;
int32 topRight = topLeft + 1;
int32 bottomLeft = (x + 1) * (Size + 1) + y;
int32 bottomRight = bottomLeft + 1;
MeshInfo.AppendTriangle(topLeft, topRight, bottomLeft);
MeshInfo.AppendTriangle(bottomLeft, topRight, bottomRight);
}
}
UE::Geometry::FMeshNormals::QuickComputeVertexNormals(MeshInfo);
CopyVertexUVsToOverlay(MeshInfo, *MeshInfo.Attributes()->PrimaryUV());
//Create mesh
UDynamicMeshComponent* MeshComponent = NewObject<UDynamicMeshComponent>(this);
MeshComponent->RegisterComponent();
MeshComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
MeshComponent->CastShadow = false;
MeshComponent->SetMesh(MoveTemp(MeshInfo));
MeshComponent->SetMaterial(0, Material);
if (Collisions)
{
MeshComponent->bUseAsyncCooking = true;
MeshComponent->BodyInstance.SetCollisionProfileName("BlockAll");
MeshComponent->EnableComplexAsSimpleCollision();
}
}
Here is PMC :
void ADynamicTerrain::CreateMeshSection_Procedural(int32 Size, UMaterialInstance* Material, bool Collisions)
{
TArray<FVector> Vertices;
TArray<int32> Triangles;
TArray<FVector> Normals;
TArray<FVector2D> UV0;
for (int32 x = 0; x <= Size; x++)
{
for (int32 y = 0; y <= Size; y++)
{
Vertices.Add(FVector(x * 100, y * 100, 0));
UV0.Add(FVector2D(x,y));
}
}
for (int32 x = 0; x < Size; x++)
{
for (int32 y = 0; y < Size; y++)
{
int32 topLeft = x * (Size + 1) + y;
int32 topRight = topLeft + 1;
int32 bottomLeft = (x + 1) * (Size + 1) + y;
int32 bottomRight = bottomLeft + 1;
Triangles.Add(topLeft);
Triangles.Add(topRight);
Triangles.Add(bottomLeft);
Triangles.Add(bottomLeft);
Triangles.Add(topRight);
Triangles.Add(bottomRight);
}
}
CalculateFastNormals(Normals);
UProceduralMeshComponent* MeshComponent = NewObject<UProceduralMeshComponent>(this);
MeshComponent->RegisterComponent();
MeshComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
MeshComponent->CastShadow = false;
MeshComponent->bUseAsyncCooking = true;
MeshComponent->CreateMeshSection(0, Vertices, Triangles, Normals, UV0, TArray<FColor>(), TArray<FProcMeshTangent>(), Collisions);
MeshComponent->SetMaterial(0, Material);
}
I have made these as similiar as possible, yet could not get the performance of PMC using DMC. PMC (with collisions, normals and uv’s) is still faster then DMC (without collisions, normals and uv’s).