SystemComponent.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <NvCloth/IClothSystem.h>
  12. #include <NvCloth/ICloth.h>
  13. #include <NvCloth/ISolver.h>
  14. #include <System/Factory.h>
  15. #include <System/Solver.h>
  16. #include <System/Fabric.h>
  17. #include <System/Cloth.h>
  18. namespace NvCloth
  19. {
  20. //! Implementation of the IClothSystem interface.
  21. //!
  22. //! This class has the responsibility to initialize and tear down NvCloth library.
  23. //! It owns all Solvers, Cloths and Fabrics, and it manages their creation and destruction.
  24. //! It's also the responsible for updating (on Physics Tick) all the solvers that are not flagged as "user simulated".
  25. class SystemComponent
  26. : public AZ::Component
  27. , protected IClothSystem
  28. , protected AZ::TickBus::Handler
  29. {
  30. public:
  31. AZ_COMPONENT(SystemComponent, "{89DF5C48-64AC-4B8E-9E61-0D4C7A7B5491}");
  32. static void Reflect(AZ::ReflectContext* context);
  33. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  34. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  35. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  36. static void InitializeNvClothLibrary();
  37. static void TearDownNvClothLibrary();
  38. //! Returns true when there is no error reported.
  39. static bool CheckLastClothError();
  40. //! Resets the last error reported by NvCloth.
  41. static void ResetLastClothError();
  42. protected:
  43. // AZ::Component overrides ...
  44. void Activate() override;
  45. void Deactivate() override;
  46. // IClothSystem overrides ...
  47. ISolver* FindOrCreateSolver(const AZStd::string& name) override;
  48. void DestroySolver(ISolver*& solver) override;
  49. ISolver* GetSolver(const AZStd::string& name) override;
  50. ICloth* CreateCloth(
  51. const AZStd::vector<SimParticleFormat>& initialParticles,
  52. const FabricCookedData& fabricCookedData) override;
  53. void DestroyCloth(ICloth*& cloth) override;
  54. ICloth* GetCloth(ClothId clothId) override;
  55. bool AddCloth(ICloth* cloth, const AZStd::string& solverName = DefaultSolverName) override;
  56. void RemoveCloth(ICloth* cloth) override;
  57. // AZ::TickBus::Handler overrides ...
  58. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  59. int GetTickOrder() override;
  60. private:
  61. void InitializeSystem();
  62. void DestroySystem();
  63. FabricId FindOrCreateFabric(const FabricCookedData& fabricCookedData);
  64. void DestroyFabric(FabricId fabricId);
  65. // Factory that creates all the solvers, fabric and cloths.
  66. AZStd::unique_ptr<Factory> m_factory;
  67. // List of all the solvers created.
  68. AZStd::vector<AZStd::unique_ptr<Solver>> m_solvers;
  69. // List of all the fabrics created.
  70. AZStd::unordered_map<FabricId, AZStd::unique_ptr<Fabric>> m_fabrics;
  71. // List of all the cloths created.
  72. AZStd::unordered_map<ClothId, AZStd::unique_ptr<Cloth>> m_cloths;
  73. };
  74. } // namespace NvCloth