HairComponentController.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Math/Vector2.h>
  10. #include <AzCore/Math/Vector3.h>
  11. #include <AzCore/Component/Component.h>
  12. #include <AzCore/Component/TickBus.h>
  13. // Hair specific
  14. #include <Components/HairBus.h>
  15. #include <Rendering/HairGlobalSettingsBus.h>
  16. #include <Components/HairComponentConfig.h>
  17. #include <Rendering/HairRenderObject.h>
  18. // EMotionFX
  19. #include <Integration/ActorComponentBus.h>
  20. namespace AMD
  21. {
  22. class TressFXSimulationSettings;
  23. class TressFXRenderingSettings;
  24. class TressFXAsset;
  25. }
  26. namespace AZ
  27. {
  28. namespace Render
  29. {
  30. namespace Hair
  31. {
  32. class HairFeatureProcessor;
  33. //! This is the controller class for both EditorComponent and in game Component.
  34. //! It is responsible for the creation and activation of the hair object itself
  35. //! and the update and synchronization of any changed configuration.
  36. //! It also responsible to the connection with the entity's Actor to whom the hair
  37. //! is associated and gets the skinning matrices and visibility.
  38. class HairComponentController final
  39. : public HairRequestsBus::Handler
  40. , public HairGlobalSettingsNotificationBus::Handler
  41. , private AZ::Data::AssetBus::MultiHandler
  42. , private AZ::TickBus::Handler
  43. , private EMotionFX::Integration::ActorComponentNotificationBus::Handler
  44. {
  45. public:
  46. friend class EditorHairComponent;
  47. AZ_TYPE_INFO(HairComponentController, "{81D3EA93-7EAC-44B7-B8CB-0B573DD8D634}");
  48. static void Reflect(AZ::ReflectContext* context);
  49. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  50. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  51. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  52. HairComponentController() = default;
  53. HairComponentController(const HairComponentConfig& config);
  54. ~HairComponentController();
  55. void Activate(EntityId entityId);
  56. void Deactivate();
  57. void SetConfiguration(const HairComponentConfig& config);
  58. const HairComponentConfig& GetConfiguration() const;
  59. HairFeatureProcessor* GetFeatureProcessor() { return m_featureProcessor; }
  60. private:
  61. AZ_DISABLE_COPY(HairComponentController);
  62. void OnHairConfigChanged();
  63. void OnHairAssetChanged();
  64. // AZ::Render::Hair::HairGlobalSettingsNotificationBus Overrides
  65. void OnHairGlobalSettingsChanged(const HairGlobalSettings& hairGlobalSettings) override;
  66. // AZ::Data::AssetBus::Handler
  67. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  68. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  69. // EMotionFX::Integration::ActorComponentNotificationBus::Handler
  70. void OnActorInstanceCreated(EMotionFX::ActorInstance* actorInstance) override;
  71. void OnActorInstanceDestroyed(EMotionFX::ActorInstance* actorInstance) override;
  72. // AZ::TickBus::Handler
  73. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  74. int GetTickOrder() override;
  75. bool GenerateLocalToGlobalBoneIndex( EMotionFX::ActorInstance* actorInstance, AMD::TressFXAsset* hairAsset);
  76. bool CreateHairObject();
  77. void RemoveHairObject();
  78. // Extract actor matrix from the actor instance.
  79. bool UpdateActorMatrices();
  80. HairFeatureProcessor* m_featureProcessor = nullptr;
  81. bool m_configChanged = false; // Flag used to defer the configuration change to onTick.
  82. HairComponentConfig m_configuration; // Settings per hair component
  83. //! Hair render object for connecting to the skeleton and connecting to the feature processor.
  84. Data::Instance<HairRenderObject> m_renderObject; // unique to this component - this is the data source.
  85. EntityId m_entityId;
  86. // Store a cache of the bone index lookup we generated during the creation of hair object.
  87. AMD::LocalToGlobalBoneIndexLookup m_hairBoneIndexLookup;
  88. AMD::LocalToGlobalBoneIndexLookup m_collisionBoneIndexLookup;
  89. // Cache the bone matrices array to avoid frequent allocation.
  90. AZStd::vector<AZ::Matrix3x4> m_cachedHairBoneMatrices;
  91. AZStd::vector<AZ::Matrix3x4> m_cachedCollisionBoneMatrices;
  92. AZ::Matrix3x4 m_entityWorldMatrix;
  93. };
  94. } // namespace Hair
  95. }
  96. }