ActorComponentBus.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/EBus/EBus.h>
  10. #include <AzCore/Component/ComponentBus.h>
  11. #include <AzCore/Component/EntityId.h>
  12. #include <AzCore/Math/Transform.h>
  13. #include <AzCore/Outcome/Outcome.h>
  14. #include <AzCore/RTTI/TypeInfo.h>
  15. #include <AzFramework/Physics/AnimationConfiguration.h>
  16. #include <AzFramework/Physics/Character.h>
  17. #include <Integration/Assets/ActorAsset.h>
  18. namespace EMotionFX
  19. {
  20. class ActorInstance;
  21. namespace Integration
  22. {
  23. /**
  24. * EMotion FX attachment type.
  25. */
  26. enum class AttachmentType : AZ::u32
  27. {
  28. None = 0, ///< Do not attach to another actor.
  29. ActorAttachment, ///< Attach to another actor as a separately animating attachment.
  30. SkinAttachment, ///< Attach to another actor as a skinned attachment (using the same skeleton as the attachment target).
  31. };
  32. enum class Space : AZ::u32
  33. {
  34. LocalSpace, ///< Relative to the parent.
  35. ModelSpace, ///< Relative to the origin of the character.
  36. WorldSpace ///< Relative to the world origin.
  37. };
  38. enum class SkinningMethod : AZ::u32
  39. {
  40. DualQuat = 0, ///< Dual Quaternions will be used to blend joints during skinning.
  41. Linear ///< Matrices will be used to blend joints during skinning.
  42. };
  43. /**
  44. * EMotion FX Actor Component Request Bus
  45. * Used for making requests to EMotion FX Actor Components.
  46. */
  47. class ActorComponentRequests
  48. : public AZ::ComponentBus
  49. {
  50. public:
  51. using MutexType = AZStd::recursive_mutex;
  52. /// Retrieve component's actor instance.
  53. /// \return pointer to actor instance.
  54. virtual EMotionFX::ActorInstance* GetActorInstance() { return nullptr; }
  55. /// Retrieve the total number of joints.
  56. virtual size_t GetNumJoints() const { return 0; }
  57. /// Find the name index of a given joint by its name.
  58. /// \param name The name of the join to search for, case insensitive.
  59. /// \return The joint index, or s_invalidJointIndex if no found.
  60. virtual size_t GetJointIndexByName(const char* /*name*/) const { return s_invalidJointIndex; }
  61. /// Retrieve the local transform (relative to the parent) of a given joint.
  62. /// \param jointIndex The joint index to get the transform from.
  63. /// \param Space the space to get the transform in.
  64. virtual AZ::Transform GetJointTransform(size_t /*jointIndex*/, Space /*space*/) const { return AZ::Transform::CreateIdentity(); }
  65. virtual void GetJointTransformComponents(size_t /*jointIndex*/, Space /*space*/, AZ::Vector3& outPosition, AZ::Quaternion& outRotation, AZ::Vector3& outScale) const { outPosition = AZ::Vector3::CreateZero(); outRotation = AZ::Quaternion::CreateIdentity(); outScale = AZ::Vector3::CreateOne(); }
  66. virtual Physics::AnimationConfiguration* GetPhysicsConfig() const { return nullptr; }
  67. /// Attach to the specified entity.
  68. /// \param targetEntityId - Id of the entity to attach to.
  69. /// \param attachmentType - Desired type of attachment.
  70. virtual void AttachToEntity(AZ::EntityId /*targetEntityId*/, AttachmentType /*attachmentType*/) {}
  71. /// Detach from parent entity, if attached.
  72. virtual void DetachFromEntity() {}
  73. /// Enables rendering of the actor.
  74. virtual bool GetRenderCharacter() const = 0;
  75. virtual void SetRenderCharacter(bool enabled) = 0;
  76. virtual bool GetRenderActorVisible() const = 0;
  77. /// Enables raytracing for the actor
  78. virtual void SetRayTracingEnabled(bool enabled) = 0;
  79. /// Returns skinning method used by the actor.
  80. virtual SkinningMethod GetSkinningMethod() const = 0;
  81. // Use this to alter the actor asset.
  82. virtual void SetActorAsset(AZ::Data::Asset<EMotionFX::Integration::ActorAsset> actorAsset) = 0;
  83. // Use this bus to enable or disable the actor instance update in the job scheduler system.
  84. // This could be useful if you want to manually update the actor instance.
  85. virtual void EnableInstanceUpdate(bool enableInstanceUpdate) = 0;
  86. static const size_t s_invalidJointIndex = std::numeric_limits<size_t>::max();
  87. };
  88. using ActorComponentRequestBus = AZ::EBus<ActorComponentRequests>;
  89. /**
  90. * EMotion FX Actor Component Notification Bus
  91. * Used for monitoring events from actor components.
  92. */
  93. class ActorComponentNotifications
  94. : public AZ::ComponentBus
  95. {
  96. public:
  97. //////////////////////////////////////////////////////////////////////////
  98. /**
  99. * Custom connection policy notifies connecting listeners immediately if actor instance is already created.
  100. */
  101. template<class Bus>
  102. struct AssetConnectionPolicy
  103. : public AZ::EBusConnectionPolicy<Bus>
  104. {
  105. static void Connect(typename Bus::BusPtr& busPtr, typename Bus::Context& context, typename Bus::HandlerNode& handler, typename Bus::Context::ConnectLockGuard& connectLock, const typename Bus::BusIdType& id = 0)
  106. {
  107. AZ::EBusConnectionPolicy<Bus>::Connect(busPtr, context, handler, connectLock, id);
  108. EMotionFX::ActorInstance* instance = nullptr;
  109. ActorComponentRequestBus::EventResult(instance, id, &ActorComponentRequestBus::Events::GetActorInstance);
  110. if (instance)
  111. {
  112. handler->OnActorInstanceCreated(instance);
  113. }
  114. }
  115. };
  116. template<typename Bus>
  117. using ConnectionPolicy = AssetConnectionPolicy<Bus>;
  118. //////////////////////////////////////////////////////////////////////////
  119. /// Notifies listeners when the component has created an actor instance.
  120. /// \param actorInstance - pointer to actor instance
  121. virtual void OnActorInstanceCreated(EMotionFX::ActorInstance* /*actorInstance*/) {};
  122. /// Notifies listeners when the component is destroying an actor instance.
  123. /// \param actorInstance - pointer to actor instance
  124. virtual void OnActorInstanceDestroyed(EMotionFX::ActorInstance* /*actorInstance*/) {};
  125. };
  126. using ActorComponentNotificationBus = AZ::EBus<ActorComponentNotifications>;
  127. /**
  128. * EMotion FX Editor Actor Component Request Bus
  129. * Used for making requests to EMotion FX Actor Components.
  130. */
  131. class EditorActorComponentRequests
  132. : public AZ::ComponentBus
  133. {
  134. public:
  135. virtual const AZ::Data::AssetId& GetActorAssetId() = 0;
  136. virtual AZ::EntityId GetAttachedToEntityId() const = 0;
  137. };
  138. using EditorActorComponentRequestBus = AZ::EBus<EditorActorComponentRequests>;
  139. } //namespace Integration
  140. } // namespace EMotionFX
  141. namespace AZ
  142. {
  143. AZ_TYPE_INFO_SPECIALIZE(EMotionFX::Integration::Space, "{7606E4DD-B7CB-408B-BD0D-3A95636BB017}");
  144. }