Cloth.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/RTTI/RTTI.h>
  10. #include <NvCloth/ICloth.h>
  11. #include <NvCloth/IClothConfigurator.h>
  12. #include <System/NvTypes.h>
  13. // NvCloth library includes
  14. #include <NvCloth/PhaseConfig.h>
  15. namespace NvCloth
  16. {
  17. class Solver;
  18. class Fabric;
  19. //! Implementation of the ICloth and IClothConfigurator interfaces.
  20. class Cloth
  21. : public ICloth
  22. , public IClothConfigurator
  23. {
  24. public:
  25. AZ_RTTI(Cloth, "{D9DEED18-FEF2-440B-8639-A080F8C1F6DB}", ICloth);
  26. Cloth(
  27. ClothId id,
  28. const AZStd::vector<SimParticleFormat>& initialParticles,
  29. Fabric* fabric,
  30. NvClothUniquePtr nvCloth);
  31. ~Cloth();
  32. //! Returns the fabric used to create this cloth.
  33. Fabric* GetFabric() { return m_fabric; }
  34. //! Returns the solver this cloth is added to or nullptr if it's not part of any solver.
  35. Solver* GetSolver() { return m_solver; }
  36. //! Retrieves the latest simulation data from NvCloth and updates the particles.
  37. void Update();
  38. // ICloth overrides ...
  39. ClothId GetId() const override;
  40. const AZStd::vector<SimParticleFormat>& GetInitialParticles() const override;
  41. const AZStd::vector<SimIndexType>& GetInitialIndices() const override;
  42. const AZStd::vector<SimParticleFormat>& GetParticles() const override;
  43. void SetParticles(const AZStd::vector<SimParticleFormat>& particles) override;
  44. void SetParticles(AZStd::vector<SimParticleFormat>&& particles) override;
  45. void DiscardParticleDelta() override;
  46. const FabricCookedData& GetFabricCookedData() const override;
  47. IClothConfigurator* GetClothConfigurator() override;
  48. // IClothConfigurator overrides ...
  49. void SetTransform(const AZ::Transform& transformWorld) override;
  50. void ClearInertia() override;
  51. void SetMass(float mass) override;
  52. void SetGravity(const AZ::Vector3& gravity) override;
  53. void SetStiffnessFrequency(float frequency) override;
  54. void SetDamping(const AZ::Vector3& damping) override;
  55. void SetDampingLinearDrag(const AZ::Vector3& linearDrag) override;
  56. void SetDampingAngularDrag(const AZ::Vector3& angularDrag) override;
  57. void SetLinearInertia(const AZ::Vector3& linearInertia) override;
  58. void SetAngularInertia(const AZ::Vector3& angularInertia) override;
  59. void SetCentrifugalInertia(const AZ::Vector3& centrifugalInertia) override;
  60. void SetWindVelocity(const AZ::Vector3& velocity) override;
  61. void SetWindDragCoefficient(float drag) override;
  62. void SetWindLiftCoefficient(float lift) override;
  63. void SetWindFluidDensity(float density) override;
  64. void SetCollisionFriction(float friction) override;
  65. void SetCollisionMassScale(float scale) override;
  66. void EnableContinuousCollision(bool value) override;
  67. void SetCollisionAffectsStaticParticles(bool value) override;
  68. void SetSelfCollisionDistance(float distance) override;
  69. void SetSelfCollisionStiffness(float stiffness) override;
  70. void SetVerticalPhaseConfig(
  71. float stiffness,
  72. float stiffnessMultiplier,
  73. float compressionLimit,
  74. float stretchLimit) override;
  75. void SetHorizontalPhaseConfig(
  76. float stiffness,
  77. float stiffnessMultiplier,
  78. float compressionLimit,
  79. float stretchLimit) override;
  80. void SetBendingPhaseConfig(
  81. float stiffness,
  82. float stiffnessMultiplier,
  83. float compressionLimit,
  84. float stretchLimit) override;
  85. void SetShearingPhaseConfig(
  86. float stiffness,
  87. float stiffnessMultiplier,
  88. float compressionLimit,
  89. float stretchLimit) override;
  90. void SetTetherConstraintStiffness(float stiffness) override;
  91. void SetTetherConstraintScale(float scale) override;
  92. void SetSolverFrequency(float frequency) override;
  93. void SetAcceleationFilterWidth(AZ::u32 width) override;
  94. void SetSphereColliders(const AZStd::vector<AZ::Vector4>& spheres) override;
  95. void SetSphereColliders(AZStd::vector<AZ::Vector4>&& spheres) override;
  96. void SetCapsuleColliders(const AZStd::vector<AZ::u32>& capsuleIndices) override;
  97. void SetCapsuleColliders(AZStd::vector<AZ::u32>&& capsuleIndices) override;
  98. void SetMotionConstraints(const AZStd::vector<AZ::Vector4>& constraints) override;
  99. void SetMotionConstraints(AZStd::vector<AZ::Vector4>&& constraints) override;
  100. void ClearMotionConstraints() override;
  101. void SetMotionConstraintsScale(float scale) override;
  102. void SetMotionConstraintsBias(float bias) override;
  103. void SetMotionConstraintsStiffness(float stiffness) override;
  104. void SetSeparationConstraints(const AZStd::vector<AZ::Vector4>& constraints) override;
  105. void SetSeparationConstraints(AZStd::vector<AZ::Vector4>&& constraints) override;
  106. void ClearSeparationConstraints() override;
  107. private:
  108. void ResolveStaticParticles();
  109. bool RetrieveSimulationResults();
  110. void RestoreSimulation();
  111. // Copies up current particles to NvCloth.
  112. void CopySimParticlesToNvCloth();
  113. // Copies up current inverse masses to NvCloth.
  114. void CopySimInverseMassesToNvCloth();
  115. void SetPhaseConfig(
  116. int32_t phaseType,
  117. float stiffness,
  118. float stiffnessMultiplier,
  119. float compressionLimit,
  120. float stretchLimit);
  121. void ApplyPhaseConfigs();
  122. // Cloth unique identifier.
  123. ClothId m_id;
  124. // NvCloth cloth object.
  125. NvClothUniquePtr m_nvCloth;
  126. // Fabric used to create this cloth.
  127. Fabric* m_fabric = nullptr;
  128. // Current solver this cloth is added to.
  129. Solver* m_solver = nullptr;
  130. // Initial data from cloth creation.
  131. AZStd::vector<SimParticleFormat> m_initialParticles;
  132. AZStd::vector<SimParticleFormat> m_initialParticlesWithMassApplied; // Needed by RestoreSimulation
  133. // Current simulation particles (positions + inverse masses).
  134. AZStd::vector<SimParticleFormat> m_simParticles;
  135. // Current mass value applied to all particles.
  136. float m_mass = 1.0f;
  137. // When true, colliders affect static particles.
  138. bool m_collisionAffectsStaticParticles = false;
  139. // Current phases configuration data.
  140. AZStd::vector<nv::cloth::PhaseConfig> m_nvPhaseConfigs;
  141. // Current motion constraints.
  142. // Caching it to be used in ResolveStaticParticles(), having it available avoids
  143. // having to call m_nvCloth->getMotionConstraints(), which there is no const version
  144. // and would wake the simulation.
  145. AZStd::vector<AZ::Vector4> m_motionConstraints;
  146. // Number of continuous invalid simulations.
  147. // That's when NvCloth provided invalid data when retrieving simulation results.
  148. AZ::u32 m_numInvalidSimulations = 0;
  149. // Solver has the responsibility of adding/removing cloths to solvers,
  150. // so it needs exclusive access to m_solver and m_nvCloth members.
  151. friend class Solver;
  152. };
  153. } // namespace NvCloth