AtomActorInstance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <AtomActor.h>
  10. #include <AzCore/RTTI/RTTI.h>
  11. #include <AzFramework/Visibility/BoundsBus.h>
  12. #include <Integration/Rendering/RenderActorInstance.h>
  13. #include <EMotionFX/Source/MorphTargetStandard.h>
  14. #include <LmbrCentral/Animation/SkeletalHierarchyRequestBus.h>
  15. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  16. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshHandleStateBus.h>
  17. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  18. #include <AtomLyIntegration/CommonFeatures/SkinnedMesh/SkinnedMeshOverrideBus.h>
  19. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorBus.h>
  20. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorInterface.h>
  21. #include <Atom/Feature/SkinnedMesh/SkinnedMeshInputBuffers.h>
  22. #include <Atom/Feature/SkinnedMesh/SkinnedMeshOutputStreamManagerInterface.h>
  23. #include <Atom/Feature/SkinnedMesh/SkinnedMeshShaderOptions.h>
  24. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  25. #include <Atom/RHI.Reflect/ShaderResourceGroupLayoutDescriptor.h>
  26. #include <AzCore/Component/TransformBus.h>
  27. #include <AzCore/std/smart_ptr/intrusive_base.h>
  28. namespace EMotionFX
  29. {
  30. class Actor;
  31. class ActorInstance;
  32. }
  33. namespace AZ::RPI
  34. {
  35. class AuxGeomDraw;
  36. class AuxGeomFeatureProcessorInterface;
  37. class Model;
  38. class Buffer;
  39. class StreamingImage;
  40. }
  41. namespace AZ
  42. {
  43. namespace Render
  44. {
  45. class SkinnedMeshFeatureProcessorInterface;
  46. class SkinnedMeshInputBuffers;
  47. class MeshFeatureProcessorInterface;
  48. class AtomActor;
  49. class AtomActorDebugDraw;
  50. //! Render node for managing and rendering actor instances. Each Actor Component
  51. //! creates an ActorRenderNode. The render node is responsible for drawing meshes and
  52. //! passing skinning transforms to the skinning pipeline.
  53. class AtomActorInstance
  54. : public EMotionFX::Integration::RenderActorInstance
  55. , public AZ::TransformNotificationBus::Handler
  56. , public AZ::Render::MaterialConsumerRequestBus::Handler
  57. , public AzFramework::BoundsRequestBus::Handler
  58. , public AZ::Render::MaterialComponentNotificationBus::Handler
  59. , public AZ::Render::MeshComponentRequestBus::Handler
  60. , public AZ::Render::SkinnedMeshOverrideRequestBus::Handler
  61. , private AZ::Render::SkinnedMeshFeatureProcessorNotificationBus::Handler
  62. , private AZ::Render::SkinnedMeshOutputStreamNotificationBus::Handler
  63. , private LmbrCentral::SkeletalHierarchyRequestBus::Handler
  64. , private Data::AssetBus::MultiHandler
  65. , private MeshHandleStateRequestBus::Handler
  66. {
  67. public:
  68. AZ_CLASS_ALLOCATOR_DECL;
  69. AZ_RTTI(AZ::Render::AtomActorInstance, "{6C933B44-8D4A-43B0-9F0F-C1932A257ABC}", EMotionFX::Integration::RenderActorInstance)
  70. AZ_DISABLE_COPY_MOVE(AtomActorInstance);
  71. AtomActorInstance() = delete;
  72. AtomActorInstance(AZ::EntityId entityId,
  73. const EMotionFX::Integration::EMotionFXPtr<EMotionFX::ActorInstance>& actorInstance,
  74. const AZ::Data::Asset<EMotionFX::Integration::ActorAsset>& asset,
  75. const AZ::Transform& worldTransform,
  76. EMotionFX::Integration::SkinningMethod skinningMethod,
  77. bool rayTracingEnabled);
  78. ~AtomActorInstance() override;
  79. // RenderActorInstance overrides ...
  80. void OnTick(float timeDelta) override;
  81. void DebugDraw(const EMotionFX::ActorRenderFlags& renderFlags);
  82. void UpdateBounds() override;
  83. void SetSkinningMethod(EMotionFX::Integration::SkinningMethod emfxSkinningMethod) override;
  84. SkinningMethod GetAtomSkinningMethod() const;
  85. void SetIsVisible(bool isVisible) override;
  86. // BoundsRequestBus overrides ...
  87. AZ::Aabb GetWorldBounds() const override;
  88. AZ::Aabb GetLocalBounds() const override;
  89. AtomActor* GetRenderActor() const;
  90. /////////////////////////////////////////////
  91. void Activate();
  92. void Deactivate();
  93. // Create all the buffers necessary for a skinned mesh render proxy and mesh render proxy, then register it with Atom
  94. void Create();
  95. // Release render proxies and destroy all the buffers
  96. void Destroy();
  97. // Acquire render proxies from Atom. Assumes the input buffers already exist.
  98. void RegisterActor();
  99. // Release render proxies, but don't destroy the buffers used to create them. Can be used to hide the actor while keeping its resources around for later.
  100. void UnregisterActor();
  101. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. // TransformNotificationBus::Handler overrides...
  103. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  104. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. // MaterialConsumerRequestBus::Handler overrides...
  106. MaterialAssignmentId FindMaterialAssignmentId(
  107. const MaterialAssignmentLodIndex lod, const AZStd::string& label) const override;
  108. MaterialAssignmentLabelMap GetMaterialLabels() const override;
  109. MaterialAssignmentMap GetDefaultMaterialMap() const override;
  110. AZStd::unordered_set<AZ::Name> GetModelUvNames() const override;
  111. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. // MaterialComponentNotificationBus::Handler overrides...
  113. void OnMaterialsUpdated(const MaterialAssignmentMap& materials) override;
  114. void OnMaterialPropertiesUpdated(const MaterialAssignmentMap& materials) override;
  115. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. // MeshComponentRequestBus::Handler overrides...
  117. void SetModelAsset(Data::Asset<RPI::ModelAsset> modelAsset) override;
  118. Data::Asset<const RPI::ModelAsset> GetModelAsset() const override;
  119. void SetModelAssetId(Data::AssetId modelAssetId) override;
  120. Data::AssetId GetModelAssetId() const override;
  121. void SetModelAssetPath(const AZStd::string& modelAssetPath) override;
  122. AZStd::string GetModelAssetPath() const override;
  123. AZ::Data::Instance<RPI::Model> GetModel() const override;
  124. void SetSortKey(RHI::DrawItemSortKey sortKey) override;
  125. RHI::DrawItemSortKey GetSortKey() const override;
  126. void SetIsAlwaysDynamic([[maybe_unused]] bool isAlwaysDynamic) override {}
  127. bool GetIsAlwaysDynamic() const { return true; }
  128. void SetLodType(RPI::Cullable::LodType lodType) override;
  129. RPI::Cullable::LodType GetLodType() const override;
  130. void SetLodOverride(RPI::Cullable::LodOverride lodOverride) override;
  131. RPI::Cullable::LodOverride GetLodOverride() const override;
  132. void SetMinimumScreenCoverage(float minimumScreenCoverage) override;
  133. float GetMinimumScreenCoverage() const override;
  134. void SetQualityDecayRate(float qualityDecayRate) override;
  135. float GetQualityDecayRate() const override;
  136. void SetVisibility(bool visible) override;
  137. bool GetVisibility() const override;
  138. void SetRayTracingEnabled(bool enabled) override;
  139. bool GetRayTracingEnabled() const override;
  140. void SetExcludeFromReflectionCubeMaps(bool excludeFromReflectionCubeMaps) override;
  141. bool GetExcludeFromReflectionCubeMaps() const override;
  142. // GetWorldBounds/GetLocalBounds already overridden by BoundsRequestBus::Handler
  143. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. // SkinnedMeshOverrideRequestBus::Handler overrides...
  145. void EnableSkinning(uint32_t lodIndex, uint32_t meshIndex) override;
  146. void DisableSkinning(uint32_t lodIndex, uint32_t meshIndex) override;
  147. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  148. // SkeletalHierarchyRequestBus::Handler overrides...
  149. AZ::u32 GetJointCount() override;
  150. const char* GetJointNameByIndex(AZ::u32 jointIndex) override;
  151. AZ::s32 GetJointIndexByName(const char* jointName) override;
  152. AZ::Transform GetJointTransformCharacterRelative(AZ::u32 jointIndex) override;
  153. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. // Data::AssetBus::MultiHandler::Handler overrides...
  155. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  156. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. // SkinnedMeshFeatureProcessorNotificationBus
  158. void OnUpdateSkinningMatrices() override;
  159. void CreateRenderProxy(const MaterialAssignmentMap& materials);
  160. private:
  161. // MeshHandleStateRequestBus overrides ...
  162. const MeshFeatureProcessorInterface::MeshHandle* GetMeshHandle() const override;
  163. void CreateSkinnedMeshInstance();
  164. // Skip skinning for certain meshes (like those with cloth)
  165. void OverrideSkinning();
  166. // SkinnedMeshOutputStreamNotificationBus
  167. void OnSkinnedMeshOutputStreamMemoryAvailable() override;
  168. // Check to see if the skin material is being used,
  169. // and if there are blend shapes with wrinkle masks that should be applied to it
  170. void InitWrinkleMasks();
  171. void UpdateWrinkleMasks();
  172. void HandleObjectSrgCreate(const Data::Instance<RPI::ShaderResourceGroup>& objectSrg);
  173. void HandleModelChange(const Data::Instance<RPI::Model>& model);
  174. void UpdateLightingChannelMask();
  175. // Debug geometry rendering
  176. AZStd::unique_ptr<AtomActorDebugDraw> m_atomActorDebugDraw;
  177. AZStd::intrusive_ptr<AZ::Render::SkinnedMeshInputBuffers> m_skinnedMeshInputBuffers = nullptr;
  178. AZStd::intrusive_ptr<SkinnedMeshInstance> m_skinnedMeshInstance;
  179. AZ::Data::Instance<AZ::RPI::Buffer> m_boneTransforms = nullptr;
  180. AZ::Render::SkinnedMeshFeatureProcessorInterface::SkinnedMeshHandle m_skinnedMeshHandle;
  181. AZ::Render::SkinnedMeshFeatureProcessorInterface* m_skinnedMeshFeatureProcessor = nullptr;
  182. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  183. //m_meshHandle is wrapped in a shared pointer so that it can be shared between this and the SkinnedMeshRenderProxy (the handle itself cannot be copied)
  184. AZStd::shared_ptr<MeshFeatureProcessorInterface::MeshHandle> m_meshHandle;
  185. AZ::TransformInterface* m_transformInterface = nullptr;
  186. AZStd::set<Data::AssetId> m_waitForMaterialLoadIds;
  187. AZStd::vector<float> m_morphTargetWeights;
  188. typedef AZStd::unordered_map<EMotionFX::MorphTargetStandard*, Data::Instance<RPI::Image>> MorphTargetWrinkleMaskMap;
  189. AZStd::vector<MorphTargetWrinkleMaskMap> m_morphTargetWrinkleMaskMapsByLod;
  190. AZStd::vector<Data::Instance<RPI::Image>> m_wrinkleMasks;
  191. AZStd::vector<float> m_wrinkleMaskWeights;
  192. MeshHandleDescriptor::ModelChangedEvent::Handler m_modelChangedEventHandler
  193. {
  194. [&](const Data::Instance<RPI::Model>& model) { HandleModelChange(model); }
  195. };
  196. MeshHandleDescriptor::ObjectSrgCreatedEvent::Handler m_objectSrgCreatedHandler
  197. {
  198. [&](const Data::Instance<RPI::ShaderResourceGroup>& objectSrg) { HandleObjectSrgCreate(objectSrg); }
  199. };
  200. bool m_rayTracingEnabled = true;
  201. };
  202. } // namespace Render
  203. } // namespace AZ