AtomActorInstance.cpp 38 KB

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