From a7f22efd1b4cc7390198838a8939eb085ab680b7 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Sat, 18 Jan 2020 15:31:10 +0930 Subject: [PATCH] Optimize cloth simulation --- src/meshgenerator.cpp | 5 ++++- src/simulateclothmeshes.cpp | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/meshgenerator.cpp b/src/meshgenerator.cpp index 694cee63..797c8ee0 100644 --- a/src/meshgenerator.cpp +++ b/src/meshgenerator.cpp @@ -873,14 +873,17 @@ bool MeshGenerator::componentRemeshed(const std::map *componen { if (nullptr == component) return false; + bool isCloth = false; if (ComponentLayer::Cloth == ComponentLayerFromString(valueOfKeyInMapOrEmpty(*component, "layer").toUtf8().constData())) { if (nullptr != polyCountValue) *polyCountValue = PolyCountToValue(PolyCount::UltraHighPoly); - return true; + isCloth = true; } auto polyCount = PolyCountFromString(valueOfKeyInMapOrEmpty(*component, "polyCount").toUtf8().constData()); if (nullptr != polyCountValue) *polyCountValue = PolyCountToValue(polyCount); + if (isCloth) + return true; return polyCount != PolyCount::Original; } diff --git a/src/simulateclothmeshes.cpp b/src/simulateclothmeshes.cpp index c672e5c6..4df1c264 100644 --- a/src/simulateclothmeshes.cpp +++ b/src/simulateclothmeshes.cpp @@ -36,19 +36,26 @@ public: std::vector &filteredClothVertices = clothMesh->vertices; std::vector externalForces; + std::vector postProcessDirections; const auto &clothForce = clothMesh->clothForce; - float clothOffset = 0.015f + (clothMesh->clothOffset * 0.05f); + float clothOffset = 0.01f + (clothMesh->clothOffset * 0.05f); if (ClothForce::Centripetal == clothForce) { externalForces.resize(filteredClothVertices.size()); + postProcessDirections.resize(filteredClothVertices.size()); for (size_t i = 0; i < filteredClothFaces.size(); ++i) { const auto &face = filteredClothFaces[i]; auto faceForceDirection = -polygonNormal(filteredClothVertices, face); for (const auto &vertex: face) externalForces[vertex] += faceForceDirection; } - for (auto &it: externalForces) - it = (it.normalized() + QVector3D(0.0f, -1.0f, 0.0f)).normalized(); + for (size_t i = 0; i < externalForces.size(); ++i) { + auto &it = externalForces[i]; + it.normalize(); + postProcessDirections[i] = it * clothOffset; + it = (it + QVector3D(0.0f, -1.0f, 0.0f)).normalized(); + } } else { + postProcessDirections.resize(filteredClothVertices.size(), QVector3D(0.0f, -1.0f, 0.0f) * clothOffset); externalForces.resize(filteredClothVertices.size(), QVector3D(0.0f, -1.0f, 0.0f)); } ClothSimulator clothSimulator(filteredClothVertices, @@ -62,7 +69,7 @@ public: clothSimulator.step(); clothSimulator.getCurrentVertices(&filteredClothVertices); for (size_t i = 0; i < filteredClothVertices.size(); ++i) { - filteredClothVertices[i] -= externalForces[i] * clothOffset; + filteredClothVertices[i] -= postProcessDirections[i]; } } void operator()(const tbb::blocked_range &range) const