ClothComponentMesh.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/Component/TransformBus.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/Math/Aabb.h>
  12. #include <AzFramework/Physics/WindBus.h>
  13. #include <NvCloth/ICloth.h>
  14. #include <Components/ClothConfiguration.h>
  15. #include <Utils/AssetHelper.h>
  16. namespace NvCloth
  17. {
  18. class ActorClothColliders;
  19. class ActorClothSkinning;
  20. class ClothConstraints;
  21. class ClothDebugDisplay;
  22. //! Class that applies cloth simulation to Static Meshes and Actors
  23. //! by reading their data and modifying the render nodes in real time.
  24. class ClothComponentMesh
  25. : public AZ::TransformNotificationBus::Handler
  26. , public AZ::TickBus::Handler
  27. , public Physics::WindNotificationsBus::Handler
  28. {
  29. public:
  30. AZ_RTTI(ClothComponentMesh, "{15A0F10C-6248-4CE4-A6FD-0E2D8AFCFEE8}");
  31. ClothComponentMesh(AZ::EntityId entityId, const ClothConfiguration& config);
  32. ~ClothComponentMesh();
  33. AZ_DISABLE_COPY_MOVE(ClothComponentMesh);
  34. // Rendering data.
  35. // It stores the tangent space information of each vertex, which is calculated every frame.
  36. struct RenderData
  37. {
  38. AZStd::vector<SimParticleFormat> m_particles;
  39. AZStd::vector<AZ::Vector3> m_tangents;
  40. AZStd::vector<AZ::Vector3> m_bitangents;
  41. AZStd::vector<AZ::Vector3> m_normals;
  42. };
  43. const RenderData& GetRenderData() const;
  44. RenderData& GetRenderData();
  45. void UpdateConfiguration(AZ::EntityId entityId, const ClothConfiguration& config);
  46. void CopyRenderDataToModel();
  47. protected:
  48. // Functions used to setup and tear down cloth component mesh
  49. void Setup(AZ::EntityId entityId, const ClothConfiguration& config);
  50. void TearDown();
  51. // ICloth notifications
  52. void OnPreSimulation(ClothId clothId, float deltaTime);
  53. void OnPostSimulation(ClothId clothId, float deltaTime, const AZStd::vector<SimParticleFormat>& updatedParticles);
  54. // AZ::TransformNotificationBus::Handler overrides ...
  55. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  56. // AZ::TickBus::Handler overrides ...
  57. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  58. int GetTickOrder() override;
  59. // Physics::WindNotificationsBus::Handler overrides ...
  60. void OnGlobalWindChanged() override;
  61. void OnWindChanged(const AZ::Aabb& aabb) override;
  62. private:
  63. void UpdateSimulationCollisions();
  64. void UpdateSimulationSkinning(float deltaTime);
  65. void UpdateSimulationConstraints();
  66. void UpdateRenderData(const AZStd::vector<SimParticleFormat>& particles);
  67. bool CreateCloth();
  68. void ApplyConfigurationToCloth();
  69. void MoveCloth(const AZ::Transform& worldTransform);
  70. void TeleportCloth(const AZ::Transform& worldTransform);
  71. void EnableSkinning() const;
  72. void DisableSkinning() const;
  73. AZ::Vector3 GetWindBusVelocity();
  74. // Entity Id of the cloth component
  75. AZ::EntityId m_entityId;
  76. // Current position in world space
  77. AZ::Vector3 m_worldPosition;
  78. // Configuration parameters for cloth simulation
  79. ClothConfiguration m_config;
  80. // Instance of cloth simulation
  81. ICloth* m_cloth = nullptr;
  82. // Cloth event handlers
  83. ICloth::PreSimulationEvent::Handler m_preSimulationEventHandler;
  84. ICloth::PostSimulationEvent::Handler m_postSimulationEventHandler;
  85. // Use a double buffer of render data to always have access to the previous frame's data.
  86. // The previous frame's data is used to workaround that debug draw is one frame delayed.
  87. static const AZ::u32 RenderDataBufferSize = 2;
  88. AZ::u32 m_renderDataBufferIndex = 0;
  89. AZStd::array<RenderData, RenderDataBufferSize> m_renderDataBuffer;
  90. // Vertex mapping between full mesh and simplified mesh used in cloth simulation.
  91. // Negative elements means the vertex has been removed.
  92. AZStd::vector<int> m_meshRemappedVertices;
  93. // Information to map the simulation particles to render mesh nodes.
  94. MeshNodeInfo m_meshNodeInfo;
  95. // Original cloth information from the mesh.
  96. MeshClothInfo m_meshClothInfo;
  97. // Cloth Colliders from the character
  98. AZStd::unique_ptr<ActorClothColliders> m_actorClothColliders;
  99. // Cloth Skinning from the character
  100. AZStd::unique_ptr<ActorClothSkinning> m_actorClothSkinning;
  101. float m_timeClothSkinningUpdates = 0.0f;
  102. // Cloth Constraints
  103. AZStd::unique_ptr<ClothConstraints> m_clothConstraints;
  104. AZStd::vector<AZ::Vector4> m_motionConstraints;
  105. AZStd::vector<AZ::Vector4> m_separationConstraints;
  106. AZStd::unique_ptr<ClothDebugDisplay> m_clothDebugDisplay;
  107. friend class ClothDebugDisplay; // Give access to data to draw debug information
  108. };
  109. } // namespace NvCloth