From 5a15f96dd1813da4d3a71faf66478a8a027da74b Mon Sep 17 00:00:00 2001 From: Charles Alexander Date: Thu, 6 May 2021 12:22:35 -0400 Subject: [PATCH 1/2] Reduce allocations on footsteps Make temp arrays static to reduce allocations during footstep processing. --- .../Animation/Notify/ALSAnimNotifyFootstep.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp b/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp index 0acd6ffc..91138a06 100644 --- a/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp +++ b/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp @@ -39,7 +39,12 @@ void UALSAnimNotifyFootstep::Notify(USkeletalMeshComponent* MeshComp, UAnimSeque const FVector TraceEnd = FootLocation - MeshOwner->GetActorUpVector() * TraceLength; FHitResult Hit; - TArray ActorsToIgnore; + + check(IsInGameThread()); + checkNoRecursion(); + static TArray ActorsToIgnore; + ActorsToIgnore.Reset(); + ActorsToIgnore.Add(MeshOwner); ActorsToIgnore.Append(MeshOwner->Children); if (UKismetSystemLibrary::LineTraceSingle(World, FootLocation, TraceEnd, TraceChannel, true, ActorsToIgnore, @@ -52,7 +57,11 @@ void UALSAnimNotifyFootstep::Notify(USkeletalMeshComponent* MeshComp, UAnimSeque const EPhysicalSurface SurfaceType = Hit.PhysMaterial.Get()->SurfaceType; - TArray HitFXRows; + check(IsInGameThread()); + checkNoRecursion(); + static TArray HitFXRows; + HitFXRows.Reset(); + HitDataTable->GetAllRows(FString(), HitFXRows); FALSHitFX* HitFX = nullptr; From fefc8d9f6d0be7d631ebfb4a3be6be90fb663c3a Mon Sep 17 00:00:00 2001 From: Charles Alexander Date: Thu, 6 May 2021 17:29:38 -0400 Subject: [PATCH 2/2] Switch to cpp linetrace method Switch from the Kismet helper to UWorld::LineTraceSingleByChannel. This allows avoiding the allocations by directly constructing the FCollisionQueryParams (it's TArray is already set up with an TInlineAllocator too). --- .../Notify/ALSAnimNotifyFootstep.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp b/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp index 91138a06..ccf7771b 100644 --- a/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp +++ b/Source/ALSV4_CPP/Private/Character/Animation/Notify/ALSAnimNotifyFootstep.cpp @@ -40,15 +40,25 @@ void UALSAnimNotifyFootstep::Notify(USkeletalMeshComponent* MeshComp, UAnimSeque FHitResult Hit; - check(IsInGameThread()); - checkNoRecursion(); - static TArray ActorsToIgnore; - ActorsToIgnore.Reset(); - - ActorsToIgnore.Add(MeshOwner); - ActorsToIgnore.Append(MeshOwner->Children); - if (UKismetSystemLibrary::LineTraceSingle(World, FootLocation, TraceEnd, TraceChannel, true, ActorsToIgnore, - DrawDebugType, Hit, true)) + ECollisionChannel CollisionChannel = UEngineTypes::ConvertToCollisionChannel(TraceChannel); + + FCollisionQueryParams Params(SCENE_QUERY_STAT(ALSFootstep), true /*bTraceComplex*/, MeshOwner); + Params.bReturnPhysicalMaterial = true; + for (auto& Child : MeshOwner->Children) + { + Params.AddIgnoredActor(Child); + } + + bool const bHit = MeshComp->GetWorld() ? World->LineTraceSingleByChannel(Hit, FootLocation, TraceEnd, CollisionChannel, Params) : false; + +#if ENABLE_DRAW_DEBUG + if (MeshComp->GetWorld()) + { + DrawDebugLineTraceSingle(MeshComp->GetWorld(), FootLocation, TraceEnd, DrawDebugType, bHit, Hit, TraceColor, TraceHitColor, DrawTime); + } +#endif + + if (bHit) { if (!Hit.PhysMaterial.Get()) {