ClothComponent.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/Console/Console.h>
  10. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  11. #include <Components/ClothComponent.h>
  12. namespace NvCloth
  13. {
  14. void ClothComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. ClothConfiguration::Reflect(context);
  17. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<ClothComponent, AZ::Component>()
  20. ->Version(0)
  21. ->Field("ClothConfiguration", &ClothComponent::m_config)
  22. ;
  23. }
  24. }
  25. ClothComponent::ClothComponent(const ClothConfiguration& config)
  26. : m_config(config)
  27. {
  28. }
  29. void ClothComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  30. {
  31. provided.push_back(AZ_CRC_CE("ClothMeshService"));
  32. }
  33. void ClothComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  34. {
  35. required.push_back(AZ_CRC_CE("MeshService"));
  36. required.push_back(AZ_CRC_CE("TransformService"));
  37. }
  38. void ClothComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  39. {
  40. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  41. }
  42. void ClothComponent::Activate()
  43. {
  44. // Cloth components do not run on dedicated servers.
  45. if (auto* console = AZ::Interface<AZ::IConsole>::Get())
  46. {
  47. bool isDedicated = false;
  48. if (const auto result = console->GetCvarValue("sv_isDedicated", isDedicated);
  49. result == AZ::GetValueResult::Success && isDedicated)
  50. {
  51. return;
  52. }
  53. }
  54. AZ::Render::MeshComponentNotificationBus::Handler::BusConnect(GetEntityId());
  55. }
  56. void ClothComponent::Deactivate()
  57. {
  58. AZ::Render::MeshComponentNotificationBus::Handler::BusDisconnect();
  59. m_clothComponentMesh.reset();
  60. }
  61. void ClothComponent::OnModelReady(
  62. const AZ::Data::Asset<AZ::RPI::ModelAsset>& asset,
  63. [[maybe_unused]] const AZ::Data::Instance<AZ::RPI::Model>& model)
  64. {
  65. if (!asset.IsReady())
  66. {
  67. return;
  68. }
  69. m_clothComponentMesh = AZStd::make_unique<ClothComponentMesh>(GetEntityId(), m_config);
  70. }
  71. void ClothComponent::OnModelPreDestroy()
  72. {
  73. m_clothComponentMesh.reset();
  74. }
  75. } // namespace NvCloth