ActorClothSkinning.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/std/limits.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <NvCloth/Types.h>
  12. #include <Components/ClothComponentMesh/ClothComponentMesh.h>
  13. namespace NvCloth
  14. {
  15. //! One skinning influence of a vertex.
  16. struct SkinningInfluence
  17. {
  18. //! Weight of the joint that influences the vertex.
  19. float m_jointWeight = 0.0f;
  20. //! Index of the joint that influences the vertex.
  21. AZ::u16 m_jointIndex = AZStd::numeric_limits<AZ::u16>::max();
  22. };
  23. //! Class to retrieve skinning information from an actor on the same entity
  24. //! and use that data to apply skinning to vertices.
  25. class ActorClothSkinning
  26. {
  27. public:
  28. AZ_TYPE_INFO(ActorClothSkinning, "{3E7C664D-096B-4126-8553-3241BA965533}");
  29. virtual ~ActorClothSkinning() = default;
  30. static AZStd::unique_ptr<ActorClothSkinning> Create(
  31. AZ::EntityId entityId,
  32. const MeshNodeInfo& meshNodeInfo,
  33. const AZStd::vector<SimParticleFormat>& originalMeshParticles,
  34. const size_t numSimulatedVertices,
  35. const AZStd::vector<int>& meshRemappedVertices);
  36. explicit ActorClothSkinning(AZ::EntityId entityId);
  37. //! Updates skinning with the current pose of the actor.
  38. virtual void UpdateSkinning() = 0;
  39. //! Applies skinning to a list of positions.
  40. //! @note w components are not affected.
  41. virtual void ApplySkinning(
  42. const AZStd::vector<AZ::Vector4>& originalPositions,
  43. AZStd::vector<AZ::Vector4>& positions) = 0;
  44. //! Applies skinning to a list of positions and vectors whose vertices
  45. //! have not been used for simulation.
  46. virtual void ApplySkinningOnNonSimulatedVertices(
  47. const MeshClothInfo& originalData,
  48. ClothComponentMesh::RenderData& renderData) = 0;
  49. //! Updates visibility variables.
  50. void UpdateActorVisibility();
  51. //! Returns true if actor is currently visible on screen.
  52. bool IsActorVisible() const;
  53. //! Returns true if actor was visible on screen in previous update.
  54. bool WasActorVisible() const;
  55. protected:
  56. AZ::EntityId m_entityId;
  57. // Skinning influences of all vertices
  58. AZStd::vector<SkinningInfluence> m_skinningInfluences;
  59. struct SimulatedVertex
  60. {
  61. AZ::u32 m_influenceOffset = 0;
  62. // Influence count is a bit redundant since that is uniform across a sub-mesh,
  63. // not unique to each vertex. However, it is necessary to track it here,
  64. // since m_simulatedVertices remapped out of the original vertex order,
  65. // and there is no way to correlate an element of m_simulatedVertices to which
  66. // sub-mesh it came from
  67. AZ::u32 m_influenceCount = 0;
  68. };
  69. // Offsets to skinning influences that are part of the simulation
  70. AZStd::vector<SimulatedVertex> m_simulatedVertices;
  71. struct NonSimulatedVertex
  72. {
  73. AZ::u32 m_originalVertexIndex = 0;
  74. AZ::u32 m_influenceOffset = 0;
  75. // Influence count is a bit redundant since that is uniform across a sub-mesh,
  76. // not unique to each vertex. However, the code is simpler and less error
  77. // prone to just track the influence count here instead of associating a range
  78. // of non-simulated vertices to a particular sub-mesh
  79. AZ::u32 m_influenceCount = 0;
  80. };
  81. // Offsets to skinning influences that are not part of the simulation
  82. AZStd::vector<NonSimulatedVertex> m_nonSimulatedVertices;
  83. // Collection of skeleton joint indices that influence the vertices
  84. AZStd::vector<AZ::u16> m_jointIndices;
  85. // Visibility variables
  86. bool m_wasActorVisible = false;
  87. bool m_isActorVisible = false;
  88. };
  89. }// namespace NvCloth