3
0

ActorComponent.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <AzCore/Interface/Interface.h>
  15. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  16. #include <AzFramework/Physics/RagdollPhysicsBus.h>
  17. #include <AzFramework/Physics/CharacterPhysicsDataBus.h>
  18. #include <AzFramework/Physics/Common/PhysicsEvents.h>
  19. #include <Integration/Assets/ActorAsset.h>
  20. #include <Integration/ActorComponentBus.h>
  21. #include <Integration/Rendering/RenderActorInstance.h>
  22. #include <Atom/Feature/LightingChannel/LightingChannelConfiguration.h>
  23. #include <EMotionFX/Source/ActorBus.h>
  24. #include <LmbrCentral/Animation/AttachmentComponentBus.h>
  25. namespace EMotionFX
  26. {
  27. namespace Integration
  28. {
  29. class ActorComponent
  30. : public AZ::Component
  31. , private AZ::Data::AssetBus::Handler
  32. , private AZ::TransformNotificationBus::MultiHandler
  33. , private AZ::TickBus::Handler
  34. , private ActorComponentRequestBus::Handler
  35. , private ActorComponentNotificationBus::Handler
  36. , private LmbrCentral::AttachmentComponentNotificationBus::Handler
  37. , private AzFramework::CharacterPhysicsDataRequestBus::Handler
  38. , private AzFramework::RagdollPhysicsNotificationBus::Handler
  39. , private AzFramework::EntityDebugDisplayEventBus::Handler
  40. {
  41. public:
  42. AZ_COMPONENT(ActorComponent, "{BDC97E7F-A054-448B-A26F-EA2B5D78E377}");
  43. friend class EditorActorComponent;
  44. class BoundingBoxConfiguration
  45. {
  46. public:
  47. AZ_TYPE_INFO(BoundingBoxConfiguration, "{EBCFF975-00A5-4578-85C7-59909F52067C}");
  48. BoundingBoxConfiguration() = default;
  49. EMotionFX::ActorInstance::EBoundsType m_boundsType = EMotionFX::ActorInstance::BOUNDS_STATIC_BASED;
  50. float m_expandBy = 25.0f; ///< Expand the bounding volume by the given percentage.
  51. bool m_autoUpdateBounds = true;
  52. float m_updateTimeFrequency = 0.0f;
  53. AZ::u32 m_updateItemFrequency = 1;
  54. // Set the bounding box configuration of the given actor instance to the parameters given by 'this'. The actor instance must not be null (this is not checked).
  55. void Set(ActorInstance* actorInstance) const;
  56. // Set the bounding box configuration, then update the bounds of the actor instance
  57. void SetAndUpdate(ActorInstance* actorInstance) const;
  58. static void Reflect(AZ::ReflectContext* context);
  59. AZ::Crc32 GetVisibilityAutoUpdate() const;
  60. AZ::Crc32 GetVisibilityAutoUpdateSettings() const;
  61. };
  62. /**
  63. * Configuration struct for procedural configuration of Actor Components.
  64. */
  65. struct Configuration
  66. {
  67. AZ_TYPE_INFO(Configuration, "{053BFBC0-ABAA-4F4E-911F-5320F941E1A8}")
  68. AZ::Data::Asset<ActorAsset> m_actorAsset{AZ::Data::AssetLoadBehavior::NoLoad}; ///< Selected actor asset.
  69. AZ::EntityId m_attachmentTarget{}; ///< Target entity this actor should attach to.
  70. size_t m_attachmentJointIndex = InvalidIndex; ///< Index of joint on target skeleton for actor attachments.
  71. AttachmentType m_attachmentType = AttachmentType::None; ///< Type of attachment.
  72. SkinningMethod m_skinningMethod = SkinningMethod::DualQuat; ///< The skinning method for this actor
  73. size_t m_lodLevel = 0;
  74. ActorRenderFlags m_renderFlags = ActorRenderFlags::Default; ///< Actor render flag
  75. bool m_rayTracingEnabled = true; ///< Enable raytracing for an actor's mesh
  76. // Force updating the joints when it is out of camera view. By
  77. // default, joints level update (beside the root joint) on
  78. // actor are disabled when the actor is out of view.
  79. bool m_forceUpdateJointsOOV = false;
  80. BoundingBoxConfiguration m_bboxConfig; ///< Configuration for bounding box type and updates
  81. bool m_excludeFromReflectionCubeMaps = true;
  82. AZ::Render::LightingChannelConfiguration m_lightingChannelConfig;
  83. static void Reflect(AZ::ReflectContext* context);
  84. };
  85. ActorComponent(const Configuration* configuration = nullptr);
  86. ~ActorComponent() override;
  87. //////////////////////////////////////////////////////////////////////////
  88. // AZ::Component interface implementation
  89. void Activate() override;
  90. void Deactivate() override;
  91. //////////////////////////////////////////////////////////////////////////
  92. // ActorComponentRequestBus::Handler
  93. size_t GetNumJoints() const override;
  94. size_t GetJointIndexByName(const char* name) const override;
  95. AZ::Transform GetJointTransform(size_t jointIndex, Space space) const override;
  96. void GetJointTransformComponents(size_t jointIndex, Space space, AZ::Vector3& outPosition, AZ::Quaternion& outRotation, AZ::Vector3& outScale) const override;
  97. Physics::AnimationConfiguration* GetPhysicsConfig() const override;
  98. ActorInstance* GetActorInstance() override { return m_actorInstance.get(); }
  99. void AttachToEntity(AZ::EntityId targetEntityId, AttachmentType attachmentType) override;
  100. void DetachFromEntity() override;
  101. bool GetRenderCharacter() const override;
  102. void SetRenderCharacter(bool enable) override;
  103. bool GetRenderActorVisible() const override;
  104. void SetRayTracingEnabled(bool enabled) override;
  105. SkinningMethod GetSkinningMethod() const override;
  106. void SetActorAsset(AZ::Data::Asset<ActorAsset> actorAsset) override;
  107. void EnableInstanceUpdate(bool enable) override;
  108. //////////////////////////////////////////////////////////////////////////
  109. // ActorComponentNotificationBus::Handler
  110. void OnActorInstanceCreated(ActorInstance* actorInstance) override;
  111. void OnActorInstanceDestroyed(ActorInstance* actorInstance) override;
  112. //////////////////////////////////////////////////////////////////////////
  113. // The entity has attached to the target.
  114. void OnAttached(AZ::EntityId targetId) override;
  115. // The entity is detaching from the target.
  116. void OnDetached(AZ::EntityId targetId) override;
  117. //////////////////////////////////////////////////////////////////////////
  118. // AzFramework::CharacterPhysicsDataBus::Handler
  119. bool GetRagdollConfiguration(Physics::RagdollConfiguration& config) const override;
  120. Physics::RagdollState GetBindPose(const Physics::RagdollConfiguration& config) const override;
  121. AZStd::string GetParentNodeName(const AZStd::string& childName) const override;
  122. //////////////////////////////////////////////////////////////////////////
  123. // AzFramework::RagdollPhysicsNotificationBus::Handler
  124. void OnRagdollActivated() override;
  125. void OnRagdollDeactivated() override;
  126. //////////////////////////////////////////////////////////////////////////
  127. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  128. {
  129. provided.push_back(AZ_CRC_CE("EMotionFXActorService"));
  130. provided.push_back(AZ_CRC_CE("MeshService"));
  131. provided.push_back(AZ_CRC_CE("CharacterPhysicsDataService"));
  132. provided.push_back(AZ_CRC_CE("MaterialConsumerService"));
  133. }
  134. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  135. {
  136. incompatible.push_back(AZ_CRC_CE("EMotionFXActorService"));
  137. incompatible.push_back(AZ_CRC_CE("MeshService"));
  138. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  139. }
  140. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  141. {
  142. required.push_back(AZ_CRC_CE("TransformService"));
  143. }
  144. static void Reflect(AZ::ReflectContext* context);
  145. // AZ::Data::AssetBus::Handler
  146. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  147. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  148. bool IsPhysicsSceneSimulationFinishEventConnected() const;
  149. AZ::Data::Asset<ActorAsset> GetActorAsset() const { return m_configuration.m_actorAsset; }
  150. void SetRenderFlag(ActorRenderFlags renderFlags);
  151. private:
  152. // AZ::TransformNotificationBus::MultiHandler
  153. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  154. // AZ::TickBus::Handler
  155. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  156. int GetTickOrder() override;
  157. // AzFramework::EntityDebugDisplayEventBus overrides ...
  158. void DisplayEntityViewport(
  159. const AzFramework::ViewportInfo& viewportInfo,
  160. AzFramework::DebugDisplayRequests& debugDisplay) override;
  161. void CheckActorCreation();
  162. void DestroyActor();
  163. void CheckAttachToEntity();
  164. Configuration m_configuration; ///< Component configuration.
  165. AZ::EntityId m_attachmentTargetEntityId; ///< Target actor entity ID
  166. AZ::EntityId m_attachmentPreviousParent; ///< The parent entity id before attaching to the attachment target.
  167. ActorAsset::ActorInstancePtr m_actorInstance; ///< Live actor instance.
  168. AZStd::vector<AZ::EntityId> m_attachments;
  169. AZStd::unique_ptr<RenderActorInstance> m_renderActorInstance;
  170. AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_sceneFinishSimHandler;
  171. bool m_processLoadedAsset = false;
  172. };
  173. } //namespace Integration
  174. } // namespace EMotionFX