From 4798c1cadda5c6dfb57e5d170bc4aab8b765e560 Mon Sep 17 00:00:00 2001 From: Aaron Ruiz Mora Date: Mon, 17 May 2021 12:28:08 +0100 Subject: [PATCH] Fix cloth receiving weird impulses when actors are spawned --- .../ClothComponentMesh/ClothComponentMesh.cpp | 22 ++++++++++--------- .../ClothComponentMesh/ClothComponentMesh.h | 4 ++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.cpp b/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.cpp index d88fcb65ce..0914e26cb7 100644 --- a/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.cpp +++ b/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.cpp @@ -36,6 +36,9 @@ namespace NvCloth AZ_CVAR(float, cloth_DistanceToTeleport, 0.5f, nullptr, AZ::ConsoleFunctorFlags::Null, "The amount of meters the entity has to move in a frame to consider it a teleport for cloth."); + AZ_CVAR(float, cloth_SecondsToDelaySimulationOnActorSpawned, 0.25f, nullptr, AZ::ConsoleFunctorFlags::Null, + "The amount of time in seconds the cloth simulation will be delayed to avoid sudden impulses when actors are spawned."); + // Helper class to map an RPI buffer from a buffer asset view. template class MappedBuffer @@ -190,7 +193,7 @@ namespace NvCloth m_meshClothInfo.m_particles, m_cloth->GetParticles().size(), m_meshRemappedVertices); - m_numberOfClothSkinningUpdates = 0; + m_timeClothSkinningUpdates = 0.0f; m_clothConstraints = ClothConstraints::Create( m_meshClothInfo.m_motionConstraints, @@ -248,7 +251,7 @@ namespace NvCloth void ClothComponentMesh::OnPreSimulation( [[maybe_unused]] ClothId clothId, - [[maybe_unused]] float deltaTime) + float deltaTime) { AZ_PROFILE_FUNCTION(AZ::Debug::ProfileCategory::Cloth); @@ -256,7 +259,7 @@ namespace NvCloth if (m_actorClothSkinning) { - UpdateSimulationSkinning(); + UpdateSimulationSkinning(deltaTime); UpdateSimulationConstraints(); } @@ -338,7 +341,7 @@ namespace NvCloth } } - void ClothComponentMesh::UpdateSimulationSkinning() + void ClothComponentMesh::UpdateSimulationSkinning(float deltaTime) { if (m_actorClothSkinning) { @@ -349,22 +352,21 @@ namespace NvCloth // Since component activation order is not trivial, the actor's pose might not be updated // immediately. Because of this cloth will receive a sudden impulse when changing from // T pose to animated pose. To avoid this undesired effect we will override cloth simulation during - // a short amount of frames. - const AZ::u32 numberOfTicksToDoFullSkinning = 10; - m_numberOfClothSkinningUpdates++; + // a short amount of time. + m_timeClothSkinningUpdates += deltaTime; // While the actor is not visible the skinned joints are not updated. Then when // it becomes visible the jump to the new skinned positions causes a sudden // impulse to cloth simulation. To avoid this undesired effect we will override cloth simulation during - // a short amount of frames. + // a short amount of time. m_actorClothSkinning->UpdateActorVisibility(); if (!m_actorClothSkinning->WasActorVisible() && m_actorClothSkinning->IsActorVisible()) { - m_numberOfClothSkinningUpdates = 0; + m_timeClothSkinningUpdates = 0.0f; } - if (m_numberOfClothSkinningUpdates <= numberOfTicksToDoFullSkinning) + if (m_timeClothSkinningUpdates <= cloth_SecondsToDelaySimulationOnActorSpawned) { // Update skinning for all particles and apply it to cloth AZStd::vector particles = m_cloth->GetParticles(); diff --git a/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.h b/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.h index a1fb6d5572..f8772d2e93 100644 --- a/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.h +++ b/Gems/NvCloth/Code/Source/Components/ClothComponentMesh/ClothComponentMesh.h @@ -85,7 +85,7 @@ namespace NvCloth private: void UpdateSimulationCollisions(); - void UpdateSimulationSkinning(); + void UpdateSimulationSkinning(float deltaTime); void UpdateSimulationConstraints(); void UpdateRenderData(const AZStd::vector& particles); @@ -133,7 +133,7 @@ namespace NvCloth // Cloth Skinning from the character AZStd::unique_ptr m_actorClothSkinning; - AZ::u32 m_numberOfClothSkinningUpdates = 0; + float m_timeClothSkinningUpdates = 0.0f; // Cloth Constraints AZStd::unique_ptr m_clothConstraints;