MeshBuilderSkinningInfo.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Memory/Memory.h>
  10. #include <AzCore/base.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include "MeshBuilderInvalidIndex.h"
  13. namespace AZ::MeshBuilder
  14. {
  15. class MeshBuilderSkinningInfo
  16. {
  17. public:
  18. AZ_CLASS_ALLOCATOR_DECL
  19. struct Influence
  20. {
  21. size_t mNodeNr = InvalidIndex;
  22. float mWeight = 1.0f;
  23. Influence() = default;
  24. Influence(size_t nodeNr, float weight)
  25. : mNodeNr(nodeNr)
  26. , mWeight(weight)
  27. {}
  28. };
  29. MeshBuilderSkinningInfo(size_t numOrgVertices);
  30. void AddInfluence(size_t orgVtxNr, const Influence& influence) { mInfluences.resize(AZStd::max(mInfluences.size(), orgVtxNr + 1)); mInfluences.at(orgVtxNr).emplace_back(influence); }
  31. void RemoveInfluence(size_t orgVtxNr, size_t influenceNr) { mInfluences.at(orgVtxNr).erase(mInfluences.at(orgVtxNr).begin() + influenceNr); }
  32. const Influence& GetInfluence(size_t orgVtxNr, size_t influenceNr) const { return mInfluences.at(orgVtxNr).at(influenceNr); }
  33. size_t GetNumInfluences(size_t orgVtxNr) const { return mInfluences.at(orgVtxNr).size(); }
  34. size_t GetNumOrgVertices() const { return mInfluences.size(); }
  35. void OptimizeMemoryUsage()
  36. {
  37. for (auto& subArray : mInfluences)
  38. {
  39. subArray.shrink_to_fit();
  40. }
  41. mInfluences.shrink_to_fit();
  42. }
  43. // optimize the weight data
  44. void Optimize(AZStd::vector<Influence>& influences, AZ::u32 maxNumWeightsPerVertex = 4, float weightThreshold = 0.0001f);
  45. // optimize weights
  46. static void OptimizeSkinningInfluences(AZStd::vector<Influence>& influences, float tolerance, size_t maxWeights);
  47. // sort the influences, starting with the biggest weight
  48. static void SortInfluencesByWeight(AZStd::vector<Influence>& influences);
  49. private:
  50. AZStd::vector<AZStd::vector<Influence>> mInfluences;
  51. };
  52. } // namespace AZ::MeshBuilder