3
0

AtomActorInstance.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 <AtomActorInstance.h>
  9. #include <AtomActor.h>
  10. #include <AtomActorDebugDraw.h>
  11. #include <ActorAsset.h>
  12. #include <Atom/Feature/SkinnedMesh/SkinnedMeshInputBuffers.h>
  13. #include <AtomLyIntegration/CommonFeatures/SkinnedMesh/SkinnedMeshOverrideBus.h>
  14. #include <Integration/System/SystemCommon.h>
  15. #include <Integration/System/SystemComponent.h>
  16. #include <EMotionFX/Source/ActorInstance.h>
  17. #include <EMotionFX/Source/DebugDraw.h>
  18. #include <EMotionFX/Source/MorphSetup.h>
  19. #include <EMotionFX/Source/MorphSetupInstance.h>
  20. #include <EMotionFX/Source/MorphTargetStandard.h>
  21. #include <EMotionFX/Source/TransformData.h>
  22. #include <EMotionFX/Source/Skeleton.h>
  23. #include <EMotionFX/Source/Mesh.h>
  24. #include <EMotionFX/Source/Node.h>
  25. #include <MCore/Source/AzCoreConversions.h>
  26. #include <Atom/RHI/RHIUtils.h>
  27. #include <Atom/RPI.Public/AuxGeom/AuxGeomDraw.h>
  28. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.h>
  29. #include <Atom/RPI.Public/Scene.h>
  30. #include <Atom/RPI.Public/Image/StreamingImage.h>
  31. #include <AzCore/Casting/numeric_cast.h>
  32. #include <AzCore/Component/EntityId.h>
  33. #include <AzCore/Component/TickBus.h>
  34. #include <AzCore/Math/Transform.h>
  35. #include <AzCore/base.h>
  36. #include <numeric>
  37. namespace AZ::Render
  38. {
  39. static constexpr uint32_t s_maxActiveWrinkleMasks = 16;
  40. AZ_CLASS_ALLOCATOR_IMPL(AtomActorInstance, EMotionFX::Integration::EMotionFXAllocator)
  41. AtomActorInstance::AtomActorInstance(AZ::EntityId entityId,
  42. const EMotionFX::Integration::EMotionFXPtr<EMotionFX::ActorInstance>& actorInstance,
  43. const AZ::Data::Asset<EMotionFX::Integration::ActorAsset>& asset,
  44. [[maybe_unused]] const AZ::Transform& worldTransform,
  45. EMotionFX::Integration::SkinningMethod skinningMethod,
  46. bool rayTracingEnabled)
  47. : m_rayTracingEnabled(rayTracingEnabled), RenderActorInstance(asset, actorInstance.get(), entityId)
  48. {
  49. RenderActorInstance::SetSkinningMethod(skinningMethod);
  50. if (m_entityId.IsValid())
  51. {
  52. Activate();
  53. AzFramework::BoundsRequestBus::Handler::BusConnect(m_entityId);
  54. }
  55. m_atomActorDebugDraw = AZStd::make_unique<AtomActorDebugDraw>(entityId);
  56. }
  57. AtomActorInstance::~AtomActorInstance()
  58. {
  59. if (m_entityId.IsValid())
  60. {
  61. AzFramework::BoundsRequestBus::Handler::BusDisconnect();
  62. Deactivate();
  63. }
  64. Data::AssetBus::MultiHandler::BusDisconnect();
  65. }
  66. void AtomActorInstance::OnTick([[maybe_unused]] float timeDelta)
  67. {
  68. UpdateBounds();
  69. m_atomActorDebugDraw->UpdateActorInstance(m_actorInstance, timeDelta);
  70. }
  71. void AtomActorInstance::DebugDraw(const EMotionFX::ActorRenderFlags& renderFlags)
  72. {
  73. m_atomActorDebugDraw->DebugDraw(renderFlags, m_actorInstance);
  74. }
  75. void AtomActorInstance::UpdateBounds()
  76. {
  77. // Update RenderActorInstance world bounding box
  78. // The bounding box is moving with the actor instance.
  79. // The entity and actor transforms are kept in sync already.
  80. m_worldAABB = m_actorInstance->GetAabb();
  81. // Update RenderActorInstance local bounding box
  82. // NB: computing the local bbox from the world bbox makes the local bbox artificially larger than it should be
  83. // instead EMFX should support getting the local bbox from the actor instance directly
  84. m_localAABB = m_worldAABB.GetTransformedAabb(m_transformInterface->GetWorldTM().GetInverse());
  85. // Update bbox on mesh instance if it exists
  86. if (m_meshFeatureProcessor && m_meshHandle && m_meshHandle->IsValid() && m_skinnedMeshInstance)
  87. {
  88. m_meshFeatureProcessor->SetLocalAabb(*m_meshHandle, m_localAABB);
  89. }
  90. AZ::Interface<AzFramework::IEntityBoundsUnion>::Get()->RefreshEntityLocalBoundsUnion(m_entityId);
  91. }
  92. AZ::Aabb AtomActorInstance::GetWorldBounds() const
  93. {
  94. return m_worldAABB;
  95. }
  96. AZ::Aabb AtomActorInstance::GetLocalBounds() const
  97. {
  98. return m_localAABB;
  99. }
  100. void AtomActorInstance::SetSkinningMethod(EMotionFX::Integration::SkinningMethod emfxSkinningMethod)
  101. {
  102. RenderActorInstance::SetSkinningMethod(emfxSkinningMethod);
  103. m_boneTransforms = CreateBoneTransformBufferFromActorInstance(m_actorInstance, emfxSkinningMethod);
  104. // Release the Atom skinned mesh and acquire a new one to apply the new skinning method
  105. UnregisterActor();
  106. RegisterActor();
  107. }
  108. SkinningMethod AtomActorInstance::GetAtomSkinningMethod() const
  109. {
  110. switch (GetSkinningMethod())
  111. {
  112. case EMotionFX::Integration::SkinningMethod::DualQuat:
  113. return SkinningMethod::DualQuaternion;
  114. case EMotionFX::Integration::SkinningMethod::Linear:
  115. return SkinningMethod::LinearSkinning;
  116. default:
  117. AZ_Error("AtomActorInstance", false, "Unsupported skinning method. Defaulting to linear");
  118. }
  119. return SkinningMethod::LinearSkinning;
  120. }
  121. void AtomActorInstance::SetIsVisible(bool isVisible)
  122. {
  123. if (IsVisible() != isVisible)
  124. {
  125. RenderActorInstance::SetIsVisible(isVisible);
  126. if (m_meshFeatureProcessor && m_meshHandle)
  127. {
  128. m_meshFeatureProcessor->SetVisible(*m_meshHandle, isVisible);
  129. }
  130. }
  131. }
  132. AtomActor* AtomActorInstance::GetRenderActor() const
  133. {
  134. EMotionFX::Integration::ActorAsset* actorAsset = m_actorAsset.Get();
  135. if (!actorAsset)
  136. {
  137. AZ_Assert(false, "Actor asset is not loaded.");
  138. return nullptr;
  139. }
  140. AtomActor* renderActor = azdynamic_cast<AtomActor*>(actorAsset->GetRenderActor());
  141. if (!renderActor)
  142. {
  143. AZ_Assert(false, "Expecting a Atom render backend actor.");
  144. return nullptr;
  145. }
  146. return renderActor;
  147. }
  148. void AtomActorInstance::Activate()
  149. {
  150. m_skinnedMeshFeatureProcessor = RPI::Scene::GetFeatureProcessorForEntity<SkinnedMeshFeatureProcessorInterface>(m_entityId);
  151. AZ_Assert(m_skinnedMeshFeatureProcessor, "AtomActorInstance was unable to find a SkinnedMeshFeatureProcessor on the EntityContext provided.");
  152. m_meshFeatureProcessor = RPI::Scene::GetFeatureProcessorForEntity<MeshFeatureProcessorInterface>(m_entityId);
  153. AZ_Assert(m_meshFeatureProcessor, "AtomActorInstance was unable to find a MeshFeatureProcessor on the EntityContext provided.");
  154. m_transformInterface = TransformBus::FindFirstHandler(m_entityId);
  155. AZ_Warning("AtomActorInstance", m_transformInterface, "Unable to attach to a TransformBus handler. This skinned mesh will always be rendered at the origin.");
  156. SkinnedMeshFeatureProcessorNotificationBus::Handler::BusConnect();
  157. MaterialConsumerRequestBus::Handler::BusConnect(m_entityId);
  158. LmbrCentral::SkeletalHierarchyRequestBus::Handler::BusConnect(m_entityId);
  159. Create();
  160. }
  161. void AtomActorInstance::Deactivate()
  162. {
  163. SkinnedMeshOutputStreamNotificationBus::Handler::BusDisconnect();
  164. LmbrCentral::SkeletalHierarchyRequestBus::Handler::BusDisconnect();
  165. MaterialConsumerRequestBus::Handler::BusDisconnect();
  166. SkinnedMeshFeatureProcessorNotificationBus::Handler::BusDisconnect();
  167. Destroy();
  168. m_meshFeatureProcessor = nullptr;
  169. m_skinnedMeshFeatureProcessor = nullptr;
  170. }
  171. MaterialAssignmentLabelMap AtomActorInstance::GetMaterialLabels() const
  172. {
  173. return GetMaterialSlotLabelsFromModelAsset(GetModelAsset());
  174. }
  175. MaterialAssignmentId AtomActorInstance::FindMaterialAssignmentId(
  176. const MaterialAssignmentLodIndex lod, const AZStd::string& label) const
  177. {
  178. return GetMaterialSlotIdFromModelAsset(GetModelAsset(), lod, label);
  179. }
  180. MaterialAssignmentMap AtomActorInstance::GetDefaultMaterialMap() const
  181. {
  182. return GetDefaultMaterialMapFromModelAsset(GetModelAsset());
  183. }
  184. AZStd::unordered_set<AZ::Name> AtomActorInstance::GetModelUvNames() const
  185. {
  186. if (m_skinnedMeshInstance && m_skinnedMeshInstance->m_model)
  187. {
  188. return m_skinnedMeshInstance->m_model->GetUvNames();
  189. }
  190. return AZStd::unordered_set<AZ::Name>();
  191. }
  192. void AtomActorInstance::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& world)
  193. {
  194. // The mesh transform is used to determine where the actor instance is actually rendered
  195. m_meshFeatureProcessor->SetTransform(*m_meshHandle, world); // handle validity is checked internally.
  196. }
  197. void AtomActorInstance::OnMaterialsUpdated(const MaterialAssignmentMap& materials)
  198. {
  199. if (m_meshFeatureProcessor)
  200. {
  201. m_meshFeatureProcessor->SetCustomMaterials(*m_meshHandle, ConvertToCustomMaterialMap(materials));
  202. }
  203. }
  204. void AtomActorInstance::OnMaterialPropertiesUpdated([[maybe_unused]] const MaterialAssignmentMap& materials)
  205. {
  206. if (m_meshFeatureProcessor)
  207. {
  208. m_meshFeatureProcessor->SetRayTracingDirty(*m_meshHandle);
  209. }
  210. }
  211. void AtomActorInstance::SetModelAsset([[maybe_unused]] Data::Asset<RPI::ModelAsset> modelAsset)
  212. {
  213. // Changing model asset is not supported by Atom Actor Instance.
  214. // The model asset is obtained from the Actor inside the ActorAsset,
  215. // which is passed to the constructor. To set a different model asset
  216. // this instance should use a different Actor.
  217. AZ_Assert(false, "AtomActorInstance::SetModelAsset not supported");
  218. }
  219. Data::Asset<const RPI::ModelAsset> AtomActorInstance::GetModelAsset() const
  220. {
  221. AZ_Assert(GetActor(), "Expecting a Atom Actor Instance having a valid Actor.");
  222. return GetActor()->GetMeshAsset();
  223. }
  224. void AtomActorInstance::SetModelAssetId([[maybe_unused]] Data::AssetId modelAssetId)
  225. {
  226. // Changing model asset is not supported by Atom Actor Instance.
  227. // The model asset is obtained from the Actor inside the ActorAsset,
  228. // which is passed to the constructor. To set a different model asset
  229. // this instance should use a different Actor.
  230. AZ_Assert(false, "AtomActorInstance::SetModelAssetId not supported");
  231. }
  232. Data::AssetId AtomActorInstance::GetModelAssetId() const
  233. {
  234. return GetModelAsset().GetId();
  235. }
  236. void AtomActorInstance::SetModelAssetPath([[maybe_unused]] const AZStd::string& modelAssetPath)
  237. {
  238. // Changing model asset is not supported by Atom Actor Instance.
  239. // The model asset is obtained from the Actor inside the ActorAsset,
  240. // which is passed to the constructor. To set a different model asset
  241. // this instance should use a different Actor.
  242. AZ_Assert(false, "AtomActorInstance::SetModelAssetPath not supported");
  243. }
  244. AZStd::string AtomActorInstance::GetModelAssetPath() const
  245. {
  246. return GetModelAsset().GetHint();
  247. }
  248. AZ::Data::Instance<RPI::Model> AtomActorInstance::GetModel() const
  249. {
  250. return m_skinnedMeshInstance->m_model;
  251. }
  252. void AtomActorInstance::SetSortKey(RHI::DrawItemSortKey sortKey)
  253. {
  254. m_meshFeatureProcessor->SetSortKey(*m_meshHandle, sortKey);
  255. }
  256. RHI::DrawItemSortKey AtomActorInstance::GetSortKey() const
  257. {
  258. return m_meshFeatureProcessor->GetSortKey(*m_meshHandle);
  259. }
  260. void AtomActorInstance::SetLodType(RPI::Cullable::LodType lodType)
  261. {
  262. RPI::Cullable::LodConfiguration config = m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle);
  263. config.m_lodType = lodType;
  264. m_meshFeatureProcessor->SetMeshLodConfiguration(*m_meshHandle, config);
  265. }
  266. RPI::Cullable::LodType AtomActorInstance::GetLodType() const
  267. {
  268. return m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle).m_lodType;
  269. }
  270. void AtomActorInstance::SetLodOverride(RPI::Cullable::LodOverride lodOverride)
  271. {
  272. RPI::Cullable::LodConfiguration config = m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle);
  273. config.m_lodOverride = lodOverride;
  274. m_meshFeatureProcessor->SetMeshLodConfiguration(*m_meshHandle, config);
  275. }
  276. RPI::Cullable::LodOverride AtomActorInstance::GetLodOverride() const
  277. {
  278. return m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle).m_lodOverride;
  279. }
  280. void AtomActorInstance::SetMinimumScreenCoverage(float minimumScreenCoverage)
  281. {
  282. RPI::Cullable::LodConfiguration config = m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle);
  283. config.m_minimumScreenCoverage = minimumScreenCoverage;
  284. m_meshFeatureProcessor->SetMeshLodConfiguration(*m_meshHandle, config);
  285. }
  286. float AtomActorInstance::GetMinimumScreenCoverage() const
  287. {
  288. return m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle).m_minimumScreenCoverage;
  289. }
  290. void AtomActorInstance::SetQualityDecayRate(float qualityDecayRate)
  291. {
  292. RPI::Cullable::LodConfiguration config = m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle);
  293. config.m_qualityDecayRate = qualityDecayRate;
  294. m_meshFeatureProcessor->SetMeshLodConfiguration(*m_meshHandle, config);
  295. }
  296. float AtomActorInstance::GetQualityDecayRate() const
  297. {
  298. return m_meshFeatureProcessor->GetMeshLodConfiguration(*m_meshHandle).m_qualityDecayRate;
  299. }
  300. void AtomActorInstance::SetVisibility(bool visible)
  301. {
  302. SetIsVisible(visible);
  303. }
  304. bool AtomActorInstance::GetVisibility() const
  305. {
  306. return IsVisible();
  307. }
  308. void AtomActorInstance::SetRayTracingEnabled(bool enabled)
  309. {
  310. if (m_meshHandle->IsValid() && m_meshFeatureProcessor)
  311. {
  312. m_rayTracingEnabled = enabled;
  313. m_meshFeatureProcessor->SetRayTracingEnabled(*m_meshHandle, m_rayTracingEnabled);
  314. }
  315. }
  316. bool AtomActorInstance::GetRayTracingEnabled() const
  317. {
  318. if (m_meshHandle->IsValid() && m_meshFeatureProcessor)
  319. {
  320. return m_meshFeatureProcessor->GetRayTracingEnabled(*m_meshHandle);
  321. }
  322. return false;
  323. }
  324. void AtomActorInstance::SetExcludeFromReflectionCubeMaps(bool enabled)
  325. {
  326. if (m_meshHandle->IsValid() && m_meshFeatureProcessor)
  327. {
  328. m_meshFeatureProcessor->SetExcludeFromReflectionCubeMaps(*m_meshHandle, enabled);
  329. }
  330. }
  331. bool AtomActorInstance::GetExcludeFromReflectionCubeMaps() const
  332. {
  333. if (m_meshHandle->IsValid() && m_meshFeatureProcessor)
  334. {
  335. return m_meshFeatureProcessor->GetExcludeFromReflectionCubeMaps(*m_meshHandle);
  336. }
  337. return false;
  338. }
  339. AZ::u32 AtomActorInstance::GetJointCount()
  340. {
  341. return aznumeric_caster(m_actorInstance->GetActor()->GetSkeleton()->GetNumNodes());
  342. }
  343. const char* AtomActorInstance::GetJointNameByIndex(AZ::u32 jointIndex)
  344. {
  345. EMotionFX::Skeleton* skeleton = m_actorInstance->GetActor()->GetSkeleton();
  346. const size_t numNodes = skeleton->GetNumNodes();
  347. if (jointIndex < numNodes)
  348. {
  349. return skeleton->GetNode(jointIndex)->GetName();
  350. }
  351. return nullptr;
  352. }
  353. AZ::s32 AtomActorInstance::GetJointIndexByName(const char* jointName)
  354. {
  355. if (jointName)
  356. {
  357. EMotionFX::Skeleton* skeleton = m_actorInstance->GetActor()->GetSkeleton();
  358. const size_t numNodes = skeleton->GetNumNodes();
  359. for (size_t nodeIndex = 0; nodeIndex < numNodes; ++nodeIndex)
  360. {
  361. if (0 == azstricmp(jointName, skeleton->GetNode(nodeIndex)->GetName()))
  362. {
  363. return aznumeric_caster(nodeIndex);
  364. }
  365. }
  366. }
  367. return -1;
  368. }
  369. AZ::Transform AtomActorInstance::GetJointTransformCharacterRelative(AZ::u32 jointIndex)
  370. {
  371. const EMotionFX::TransformData* transforms = m_actorInstance->GetTransformData();
  372. if (transforms && jointIndex < transforms->GetNumTransforms())
  373. {
  374. return MCore::EmfxTransformToAzTransform(transforms->GetCurrentPose()->GetModelSpaceTransform(jointIndex));
  375. }
  376. return AZ::Transform::CreateIdentity();
  377. }
  378. void AtomActorInstance::Create()
  379. {
  380. Destroy();
  381. m_skinnedMeshInputBuffers = GetRenderActor()->FindOrCreateSkinnedMeshInputBuffers();
  382. AZ_Warning("AtomActorInstance", m_skinnedMeshInputBuffers, "Failed to create SkinnedMeshInputBuffers from Actor. It is likely that this actor doesn't have any meshes");
  383. if (m_skinnedMeshInputBuffers)
  384. {
  385. m_boneTransforms = CreateBoneTransformBufferFromActorInstance(m_actorInstance, GetSkinningMethod());
  386. AZ_Error("AtomActorInstance", m_boneTransforms || AZ::RHI::IsNullRHI(), "Failed to create bone transform buffer.");
  387. // If the instance is created before the default materials on the model have finished loading, the mesh feature processor will ignore it.
  388. // Wait for them all to be ready before creating the instance
  389. uint32_t lodCount = m_skinnedMeshInputBuffers->GetLodCount();
  390. for (uint32_t lodIndex = 0; lodIndex < lodCount; ++lodIndex)
  391. {
  392. const SkinnedMeshInputLod& inputLod = m_skinnedMeshInputBuffers->GetLod(lodIndex);
  393. Data::Asset<RPI::ModelLodAsset> modelLodAsset = inputLod.GetModelLodAsset();
  394. for (const RPI::ModelLodAsset::Mesh& submesh : modelLodAsset->GetMeshes())
  395. {
  396. Data::Asset<RPI::MaterialAsset> defaultSubmeshMaterial = m_skinnedMeshInputBuffers->GetModelAsset()->FindMaterialSlot(submesh.GetMaterialSlotId()).m_defaultMaterialAsset;
  397. if (defaultSubmeshMaterial && !defaultSubmeshMaterial.IsReady())
  398. {
  399. // Start listening for the material's OnAssetReady event.
  400. // AtomActorInstance::Create is called on the main thread, so there should be no need to synchronize with the OnAssetReady event handler
  401. // since those events will also come from the main thread
  402. m_waitForMaterialLoadIds.insert(defaultSubmeshMaterial.GetId());
  403. Data::AssetBus::MultiHandler::BusConnect(defaultSubmeshMaterial.GetId());
  404. }
  405. }
  406. }
  407. // If all the default materials are ready, create the skinned mesh instance
  408. if (m_waitForMaterialLoadIds.empty())
  409. {
  410. CreateSkinnedMeshInstance();
  411. }
  412. }
  413. }
  414. void AtomActorInstance::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  415. {
  416. Data::AssetBus::MultiHandler::BusDisconnect(asset->GetId());
  417. m_waitForMaterialLoadIds.erase(asset->GetId());
  418. // If all the default materials are ready, create the skinned mesh instance
  419. if (m_waitForMaterialLoadIds.empty())
  420. {
  421. CreateSkinnedMeshInstance();
  422. }
  423. }
  424. void AtomActorInstance::Destroy()
  425. {
  426. if (m_skinnedMeshInstance)
  427. {
  428. UnregisterActor();
  429. m_skinnedMeshInputBuffers.reset();
  430. m_skinnedMeshInstance.reset();
  431. m_boneTransforms.reset();
  432. }
  433. }
  434. template<class X>
  435. void swizzle_unique(AZStd::vector<X>& values, const AZStd::vector<size_t>& indices)
  436. {
  437. AZStd::vector<X> out;
  438. out.reserve(indices.size());
  439. for (size_t i : indices)
  440. {
  441. out.push_back(AZStd::move(values[i]));
  442. }
  443. values = AZStd::move(out);
  444. }
  445. void AtomActorInstance::OnUpdateSkinningMatrices()
  446. {
  447. if (m_skinnedMeshHandle.IsValid())
  448. {
  449. AZStd::vector<float> boneTransforms;
  450. GetBoneTransformsFromActorInstance(m_actorInstance, boneTransforms, GetSkinningMethod());
  451. m_skinnedMeshFeatureProcessor->SetSkinningMatrices(m_skinnedMeshHandle, boneTransforms);
  452. // Update the morph weights for every lod. This does not mean they will all be dispatched, but they will all have up to date weights
  453. // TODO: once culling is hooked up such that EMotionFX and Atom are always in sync about which lod to update, only update the currently visible lods [ATOM-13564]
  454. const auto lodCount = aznumeric_cast<uint32_t>(m_actorInstance->GetActor()->GetNumLODLevels());
  455. for (uint32_t lodIndex = 0; lodIndex < lodCount; ++lodIndex)
  456. {
  457. EMotionFX::MorphSetup* morphSetup = m_actorInstance->GetActor()->GetMorphSetup(lodIndex);
  458. if (morphSetup)
  459. {
  460. // Track all the masks/weights that are currently active
  461. m_wrinkleMasks.clear();
  462. m_wrinkleMaskWeights.clear();
  463. size_t morphTargetCount = morphSetup->GetNumMorphTargets();
  464. m_morphTargetWeights.clear();
  465. for (size_t morphTargetIndex = 0; morphTargetIndex < morphTargetCount; ++morphTargetIndex)
  466. {
  467. EMotionFX::MorphTarget* morphTarget = morphSetup->GetMorphTarget(morphTargetIndex);
  468. // check if we are dealing with a standard morph target
  469. if (morphTarget->GetType() != EMotionFX::MorphTargetStandard::TYPE_ID)
  470. {
  471. continue;
  472. }
  473. // down cast the morph target
  474. EMotionFX::MorphTargetStandard* morphTargetStandard = static_cast<EMotionFX::MorphTargetStandard*>(morphTarget);
  475. EMotionFX::MorphSetupInstance::MorphTarget* morphTargetSetupInstance = m_actorInstance->GetMorphSetupInstance()->FindMorphTargetByID(morphTargetStandard->GetID());
  476. // Each morph target is split into several deform datas, all of which share the same weight but have unique min/max delta values
  477. // and impact a unique mesh and thus correspond with unique dispatches in the morph target pass
  478. for (size_t deformDataIndex = 0; deformDataIndex < morphTargetStandard->GetNumDeformDatas(); ++deformDataIndex)
  479. {
  480. // Morph targets that don't deform any vertices (e.g. joint-based morph targets) are not registered in the render proxy. Skip adding their weights.
  481. const EMotionFX::MorphTargetStandard::DeformData* deformData = morphTargetStandard->GetDeformData(deformDataIndex);
  482. if (deformData->m_numVerts > 0)
  483. {
  484. float weight = morphTargetSetupInstance->GetWeight();
  485. m_morphTargetWeights.push_back(weight);
  486. // If the morph target is active and it has a wrinkle mask
  487. auto wrinkleMaskIter = m_morphTargetWrinkleMaskMapsByLod[lodIndex].find(morphTargetStandard);
  488. if (weight > 0 && wrinkleMaskIter != m_morphTargetWrinkleMaskMapsByLod[lodIndex].end())
  489. {
  490. // Add the wrinkle mask and weight, to be set on the material
  491. m_wrinkleMasks.push_back(wrinkleMaskIter->second);
  492. m_wrinkleMaskWeights.push_back(weight);
  493. }
  494. }
  495. }
  496. }
  497. AZ_Assert(m_wrinkleMasks.size() == m_wrinkleMaskWeights.size(), "Must have equal # of masks and weights");
  498. // If there's too many masks, truncate
  499. if (m_wrinkleMasks.size() > s_maxActiveWrinkleMasks)
  500. {
  501. // Build a remapping of indices (because we want to sort two vectors)
  502. AZStd::vector<size_t> remapped;
  503. remapped.resize_no_construct(m_wrinkleMasks.size());
  504. std::iota(remapped.begin(), remapped.end(), 0);
  505. // Sort index remapping by weight (highest first)
  506. std::sort(remapped.begin(), remapped.end(), [&](size_t ia, size_t ib) {
  507. return m_wrinkleMaskWeights[ia] > m_wrinkleMaskWeights[ib];
  508. });
  509. // Truncate indices list
  510. remapped.resize(s_maxActiveWrinkleMasks);
  511. // Remap wrinkle masks list and weights list
  512. swizzle_unique(m_wrinkleMasks, remapped);
  513. swizzle_unique(m_wrinkleMaskWeights, remapped);
  514. }
  515. m_skinnedMeshFeatureProcessor->SetMorphTargetWeights(m_skinnedMeshHandle, lodIndex, m_morphTargetWeights);
  516. // Until EMotionFX and Atom lods are synchronized [ATOM-13564] we don't know which EMotionFX lod to pull the weights from
  517. // Until that is fixed, just use lod 0 [ATOM-15251]
  518. if (lodIndex == 0)
  519. {
  520. UpdateWrinkleMasks();
  521. }
  522. }
  523. }
  524. UpdateLightingChannelMask();
  525. }
  526. }
  527. void AtomActorInstance::RegisterActor()
  528. {
  529. if (!m_skinnedMeshInstance)
  530. {
  531. AZ_Error("AtomActorInstance", m_skinnedMeshInstance, "SkinnedMeshInstance must be created before register this actor.");
  532. return;
  533. }
  534. MaterialAssignmentMap materials;
  535. MaterialComponentRequestBus::EventResult(materials, m_entityId, &MaterialComponentRequests::GetMaterialMap);
  536. CreateRenderProxy(materials);
  537. InitWrinkleMasks();
  538. TransformNotificationBus::Handler::BusConnect(m_entityId);
  539. MaterialComponentNotificationBus::Handler::BusConnect(m_entityId);
  540. MeshComponentRequestBus::Handler::BusConnect(m_entityId);
  541. SkinnedMeshOverrideRequestBus::Handler::BusConnect(m_entityId);
  542. MeshHandleStateRequestBus::Handler::BusConnect(m_entityId);
  543. const Data::Instance<RPI::Model> model = m_meshFeatureProcessor->GetModel(*m_meshHandle);
  544. MeshComponentNotificationBus::Event(m_entityId, &MeshComponentNotificationBus::Events::OnModelReady, GetModelAsset(), model);
  545. MeshHandleStateNotificationBus::Event(m_entityId, &MeshHandleStateNotificationBus::Events::OnMeshHandleSet, &(*m_meshHandle));
  546. m_meshFeatureProcessor->SetVisible(*m_meshHandle, IsVisible());
  547. }
  548. void AtomActorInstance::UnregisterActor()
  549. {
  550. MeshComponentNotificationBus::Event(m_entityId, &MeshComponentNotificationBus::Events::OnModelPreDestroy);
  551. MeshHandleStateRequestBus::Handler::BusDisconnect();
  552. SkinnedMeshOverrideRequestBus::Handler::BusDisconnect(m_entityId);
  553. MeshComponentRequestBus::Handler::BusDisconnect();
  554. MaterialComponentNotificationBus::Handler::BusDisconnect();
  555. TransformNotificationBus::Handler::BusDisconnect();
  556. m_skinnedMeshFeatureProcessor->ReleaseSkinnedMesh(m_skinnedMeshHandle);
  557. if (m_meshHandle)
  558. {
  559. m_meshFeatureProcessor->ReleaseMesh(*m_meshHandle);
  560. MeshHandleStateNotificationBus::Event(m_entityId, &MeshHandleStateNotificationBus::Events::OnMeshHandleSet, &(*m_meshHandle));
  561. m_meshHandle = nullptr;
  562. }
  563. }
  564. void AtomActorInstance::CreateRenderProxy(const MaterialAssignmentMap& materials)
  565. {
  566. auto meshFeatureProcessor = RPI::Scene::GetFeatureProcessorForEntity<MeshFeatureProcessorInterface>(m_entityId);
  567. AZ_Error("ActorComponentController", meshFeatureProcessor, "Unable to find a MeshFeatureProcessorInterface on the entityId.");
  568. if (meshFeatureProcessor)
  569. {
  570. MeshHandleDescriptor meshDescriptor;
  571. meshDescriptor.m_modelAsset = m_skinnedMeshInstance->m_model->GetModelAsset();
  572. meshDescriptor.m_customMaterials = ConvertToCustomMaterialMap(materials);
  573. meshDescriptor.m_isRayTracingEnabled = m_rayTracingEnabled;
  574. meshDescriptor.m_isAlwaysDynamic = true;
  575. meshDescriptor.m_excludeFromReflectionCubeMaps = true;
  576. meshDescriptor.m_isSkinnedMesh = true;
  577. meshDescriptor.m_supportRayIntersection = true; // we need to keep the buffer data in order to initialize the actor.
  578. meshDescriptor.m_objectSrgCreatedHandler = m_objectSrgCreatedHandler;
  579. m_meshHandle = AZStd::make_shared<MeshFeatureProcessorInterface::MeshHandle>(m_meshFeatureProcessor->AcquireMesh(meshDescriptor));
  580. }
  581. // If render proxies already exist, they will be auto-freed
  582. SkinnedMeshFeatureProcessorInterface::SkinnedMeshHandleDescriptor desc{ m_skinnedMeshInputBuffers, m_skinnedMeshInstance, m_meshHandle, m_boneTransforms, {GetAtomSkinningMethod()} };
  583. m_skinnedMeshHandle = m_skinnedMeshFeatureProcessor->AcquireSkinnedMesh(desc);
  584. if (m_transformInterface)
  585. {
  586. OnTransformChanged(Transform::Identity(), m_transformInterface->GetWorldTM());
  587. }
  588. else
  589. {
  590. OnTransformChanged(Transform::Identity(), Transform::Identity());
  591. }
  592. }
  593. void AtomActorInstance::CreateSkinnedMeshInstance()
  594. {
  595. SkinnedMeshOutputStreamNotificationBus::Handler::BusDisconnect();
  596. m_skinnedMeshInstance = m_skinnedMeshInputBuffers->CreateSkinnedMeshInstance();
  597. if (m_skinnedMeshInstance && m_skinnedMeshInstance->m_model)
  598. {
  599. MaterialConsumerNotificationBus::Event(m_entityId, &MaterialConsumerNotificationBus::Events::OnMaterialAssignmentSlotsChanged);
  600. RegisterActor();
  601. }
  602. else
  603. {
  604. AZ_Warning("AtomActorInstance", m_skinnedMeshInstance, "Failed to create target skinned model. Will automatically attempt to re-create when skinned mesh memory is freed up.");
  605. SkinnedMeshOutputStreamNotificationBus::Handler::BusConnect();
  606. }
  607. }
  608. void AtomActorInstance::EnableSkinning(uint32_t lodIndex, uint32_t meshIndex)
  609. {
  610. if (m_skinnedMeshHandle.IsValid())
  611. {
  612. m_skinnedMeshFeatureProcessor->EnableSkinning(m_skinnedMeshHandle, lodIndex, meshIndex);
  613. }
  614. }
  615. void AtomActorInstance::DisableSkinning(uint32_t lodIndex, uint32_t meshIndex)
  616. {
  617. if (m_skinnedMeshHandle.IsValid())
  618. {
  619. m_skinnedMeshFeatureProcessor->DisableSkinning(m_skinnedMeshHandle, lodIndex, meshIndex);
  620. }
  621. }
  622. void AtomActorInstance::OnSkinnedMeshOutputStreamMemoryAvailable()
  623. {
  624. CreateSkinnedMeshInstance();
  625. }
  626. void AtomActorInstance::InitWrinkleMasks()
  627. {
  628. EMotionFX::Actor* actor = m_actorAsset->GetActor();
  629. m_morphTargetWrinkleMaskMapsByLod.resize(m_skinnedMeshInputBuffers->GetLodCount());
  630. m_wrinkleMasks.reserve(s_maxActiveWrinkleMasks);
  631. m_wrinkleMaskWeights.reserve(s_maxActiveWrinkleMasks);
  632. for (size_t lodIndex = 0; lodIndex < m_skinnedMeshInputBuffers->GetLodCount(); ++lodIndex)
  633. {
  634. EMotionFX::MorphSetup* morphSetup = actor->GetMorphSetup(static_cast<uint32>(lodIndex));
  635. if (morphSetup)
  636. {
  637. const AZStd::vector<AZ::RPI::MorphTargetMetaAsset::MorphTarget>& metaDatas = actor->GetMorphTargetMetaAsset()->GetMorphTargets();
  638. // Loop over all the EMotionFX morph targets
  639. size_t numMorphTargets = morphSetup->GetNumMorphTargets();
  640. for (size_t morphTargetIndex = 0; morphTargetIndex < numMorphTargets; ++morphTargetIndex)
  641. {
  642. EMotionFX::MorphTargetStandard* morphTarget = static_cast<EMotionFX::MorphTargetStandard*>(morphSetup->GetMorphTarget(morphTargetIndex));
  643. for (const RPI::MorphTargetMetaAsset::MorphTarget& metaData : metaDatas)
  644. {
  645. // Find the metaData associated with this morph target
  646. if (metaData.m_morphTargetName == morphTarget->GetNameString() && metaData.m_wrinkleMask && metaData.m_numVertices > 0)
  647. {
  648. // If the metaData has a wrinkle mask, add it to the map
  649. Data::Instance<RPI::StreamingImage> streamingImage = RPI::StreamingImage::FindOrCreate(metaData.m_wrinkleMask);
  650. if (streamingImage)
  651. {
  652. m_morphTargetWrinkleMaskMapsByLod[lodIndex][morphTarget] = streamingImage;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. }
  660. void AtomActorInstance::UpdateWrinkleMasks()
  661. {
  662. if (m_meshHandle)
  663. {
  664. const AZStd::vector<Data::Instance<RPI::ShaderResourceGroup>>& wrinkleMaskObjectSrgs = m_meshFeatureProcessor->GetObjectSrgs(*m_meshHandle);
  665. for (auto& wrinkleMaskObjectSrg : wrinkleMaskObjectSrgs)
  666. {
  667. RHI::ShaderInputImageIndex wrinkleMasksIndex = wrinkleMaskObjectSrg->FindShaderInputImageIndex(Name{ "m_wrinkle_masks" });
  668. RHI::ShaderInputConstantIndex wrinkleMaskWeightsIndex = wrinkleMaskObjectSrg->FindShaderInputConstantIndex(Name{ "m_wrinkle_mask_weights" });
  669. RHI::ShaderInputConstantIndex wrinkleMaskCountIndex = wrinkleMaskObjectSrg->FindShaderInputConstantIndex(Name{ "m_wrinkle_mask_count" });
  670. if (wrinkleMasksIndex.IsValid() || wrinkleMaskWeightsIndex.IsValid() || wrinkleMaskCountIndex.IsValid())
  671. {
  672. AZ_Error("AtomActorInstance", wrinkleMasksIndex.IsValid(), "m_wrinkle_masks not found on the ObjectSrg, but m_wrinkle_mask_weights and/or m_wrinkle_mask_count are being used.");
  673. AZ_Error("AtomActorInstance", wrinkleMaskWeightsIndex.IsValid(), "m_wrinkle_mask_weights not found on the ObjectSrg, but m_wrinkle_masks and/or m_wrinkle_mask_count are being used.");
  674. AZ_Error("AtomActorInstance", wrinkleMaskCountIndex.IsValid(), "m_wrinkle_mask_count not found on the ObjectSrg, but m_wrinkle_mask_weights and/or m_wrinkle_masks are being used.");
  675. if (m_wrinkleMasks.size())
  676. {
  677. wrinkleMaskObjectSrg->SetImageArray(wrinkleMasksIndex, AZStd::span<const Data::Instance<RPI::Image>>(m_wrinkleMasks.data(), m_wrinkleMasks.size()));
  678. // Set the weights for any active masks
  679. for (size_t i = 0; i < m_wrinkleMaskWeights.size(); ++i)
  680. {
  681. wrinkleMaskObjectSrg->SetConstant(wrinkleMaskWeightsIndex, m_wrinkleMaskWeights[i], static_cast<uint32_t>(i));
  682. }
  683. AZ_Error("AtomActorInstance", m_wrinkleMaskWeights.size() <= s_maxActiveWrinkleMasks, "The skinning shader supports no more than %d active morph targets with wrinkle masks.", s_maxActiveWrinkleMasks);
  684. }
  685. wrinkleMaskObjectSrg->SetConstant(wrinkleMaskCountIndex, aznumeric_cast<uint32_t>(m_wrinkleMasks.size()));
  686. m_meshFeatureProcessor->QueueObjectSrgForCompile(*m_meshHandle);
  687. }
  688. }
  689. }
  690. }
  691. void AtomActorInstance::HandleObjectSrgCreate(const Data::Instance<RPI::ShaderResourceGroup>& objectSrg)
  692. {
  693. MeshComponentNotificationBus::Event(m_entityId, &MeshComponentNotificationBus::Events::OnObjectSrgCreated, objectSrg);
  694. }
  695. void AtomActorInstance::UpdateLightingChannelMask()
  696. {
  697. if (m_meshHandle)
  698. {
  699. const AZStd::vector<Data::Instance<RPI::ShaderResourceGroup>>& objectSrgs =
  700. m_meshFeatureProcessor->GetObjectSrgs(*m_meshHandle);
  701. for (auto& objectSrg : objectSrgs)
  702. {
  703. RHI::ShaderInputConstantIndex lightingChannelMaskIndex =
  704. objectSrg->FindShaderInputConstantIndex(AZ::Name("m_lightingChannelMask"));
  705. if (lightingChannelMaskIndex.IsValid())
  706. {
  707. objectSrg->SetConstant(lightingChannelMaskIndex, m_actorInstance->GetLightingChannelMask());
  708. }
  709. }
  710. m_meshFeatureProcessor->SetLightingChannelMask(*m_meshHandle, m_actorInstance->GetLightingChannelMask());
  711. m_meshFeatureProcessor->QueueObjectSrgForCompile(*m_meshHandle);
  712. }
  713. }
  714. const MeshFeatureProcessorInterface::MeshHandle* AtomActorInstance::GetMeshHandle() const
  715. {
  716. return m_meshHandle.get();
  717. }
  718. } // namespace AZ::Render