HairComponentController.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <AzCore/RTTI/BehaviorContext.h>
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <AzFramework/Components/TransformComponent.h>
  11. #include <Integration/Components/ActorComponent.h>
  12. #include <EMotionFX/Source/TransformData.h>
  13. #include <EMotionFX/Source/ActorInstance.h>
  14. #include <EMotionFX/Source/Node.h>
  15. // Hair Specific
  16. #include <TressFX/TressFXAsset.h>
  17. #include <TressFX/TressFXSettings.h>
  18. #include <Rendering/HairFeatureProcessor.h>
  19. #include <Components/HairComponentController.h>
  20. namespace AZ
  21. {
  22. namespace Render
  23. {
  24. namespace Hair
  25. {
  26. HairComponentController::~HairComponentController()
  27. {
  28. RemoveHairObject();
  29. }
  30. void HairComponentController::Reflect(ReflectContext* context)
  31. {
  32. HairComponentConfig::Reflect(context);
  33. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  34. {
  35. serializeContext->Class<HairComponentController>()
  36. ->Version(2)
  37. ->Field("Configuration", &HairComponentController::m_configuration)
  38. ;
  39. }
  40. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  41. {
  42. behaviorContext->EBus<HairRequestsBus>("HairRequestsBus")
  43. ->Attribute(AZ::Script::Attributes::Module, "render")
  44. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  45. // Insert auto-gen behavior context here...
  46. ;
  47. }
  48. }
  49. void HairComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  50. {
  51. provided.push_back(AZ_CRC_CE("HairService"));
  52. }
  53. void HairComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  54. {
  55. incompatible.push_back(AZ_CRC_CE("HairService"));
  56. }
  57. void HairComponentController::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  58. {
  59. // Dependency in the Actor due to the need to get the bone / joint matrices
  60. required.push_back(AZ_CRC_CE("EMotionFXActorService"));
  61. }
  62. HairComponentController::HairComponentController(const HairComponentConfig& config)
  63. : m_configuration(config)
  64. {
  65. }
  66. void HairComponentController::Activate(EntityId entityId)
  67. {
  68. m_entityId = entityId;
  69. m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<Hair::HairFeatureProcessor>(m_entityId);
  70. if (m_featureProcessor)
  71. {
  72. m_featureProcessor->SetHairGlobalSettings(m_configuration.m_hairGlobalSettings);
  73. if (!m_renderObject)
  74. {
  75. // Call this function if object doesn't exist to trigger the load of the existing asset
  76. OnHairAssetChanged();
  77. }
  78. }
  79. EMotionFX::Integration::ActorComponentNotificationBus::Handler::BusConnect(m_entityId);
  80. HairRequestsBus::Handler::BusConnect(m_entityId);
  81. TickBus::Handler::BusConnect();
  82. HairGlobalSettingsNotificationBus::Handler::BusConnect();
  83. }
  84. void HairComponentController::Deactivate()
  85. {
  86. HairRequestsBus::Handler::BusDisconnect(m_entityId);
  87. EMotionFX::Integration::ActorComponentNotificationBus::Handler::BusDisconnect(m_entityId);
  88. Data::AssetBus::MultiHandler::BusDisconnect();
  89. TickBus::Handler::BusDisconnect();
  90. HairGlobalSettingsNotificationBus::Handler::BusDisconnect();
  91. RemoveHairObject();
  92. m_entityId.SetInvalid();
  93. }
  94. void HairComponentController::SetConfiguration(const HairComponentConfig& config)
  95. {
  96. m_configuration = config;
  97. OnHairConfigChanged();
  98. }
  99. const HairComponentConfig& HairComponentController::GetConfiguration() const
  100. {
  101. return m_configuration;
  102. }
  103. void HairComponentController::OnHairAssetChanged()
  104. {
  105. Data::AssetBus::MultiHandler::BusDisconnect();
  106. if (m_configuration.m_hairAsset.GetId().IsValid())
  107. {
  108. Data::AssetBus::MultiHandler::BusConnect(m_configuration.m_hairAsset.GetId());
  109. m_configuration.m_hairAsset.QueueLoad();
  110. }
  111. else
  112. {
  113. RemoveHairObject();
  114. }
  115. }
  116. void HairComponentController::OnHairGlobalSettingsChanged(const HairGlobalSettings& hairGlobalSettings)
  117. {
  118. m_configuration.m_hairGlobalSettings = hairGlobalSettings;
  119. }
  120. void HairComponentController::RemoveHairObject()
  121. {
  122. if (m_featureProcessor)
  123. {
  124. m_featureProcessor->RemoveHairRenderObject(m_renderObject);
  125. }
  126. m_renderObject.reset();
  127. }
  128. void HairComponentController::OnHairConfigChanged()
  129. {
  130. // The actual config change to render object happens in the onTick function. We do this to make sure it
  131. // always happens pre-rendering. There is no need to do it before render object created, because the object
  132. // will always be created with the updated configuration.
  133. if (m_renderObject)
  134. {
  135. m_configChanged = true;
  136. }
  137. }
  138. void HairComponentController::OnAssetReady(Data::Asset<Data::AssetData> asset)
  139. {
  140. if (asset.GetId() == m_configuration.m_hairAsset.GetId())
  141. {
  142. m_configuration.m_hairAsset = asset;
  143. CreateHairObject();
  144. }
  145. }
  146. void HairComponentController::OnAssetReloaded(Data::Asset<Data::AssetData> asset)
  147. {
  148. OnAssetReady(asset);
  149. }
  150. void HairComponentController::OnActorInstanceCreated([[maybe_unused]]EMotionFX::ActorInstance* actorInstance)
  151. {
  152. CreateHairObject();
  153. }
  154. void HairComponentController::OnActorInstanceDestroyed([[maybe_unused]]EMotionFX::ActorInstance* actorInstance)
  155. {
  156. RemoveHairObject();
  157. }
  158. void HairComponentController::OnTick([[maybe_unused]]float deltaTime, [[maybe_unused]]AZ::ScriptTimePoint time)
  159. {
  160. if (!m_renderObject)
  161. {
  162. return;
  163. }
  164. // Config change to renderObject happens on the OnTick, so we know the it occurs before render update.
  165. if (m_configChanged)
  166. {
  167. const float MAX_SIMULATION_TIME_STEP = 0.033f; // Assuming minimal of 30 fps
  168. float currentDeltaTime = AZStd::min(deltaTime, MAX_SIMULATION_TIME_STEP);
  169. m_renderObject->UpdateSimulationParameters(&m_configuration.m_simulationSettings, currentDeltaTime);
  170. // [To Do] Hair - Allow update of the following settings to control dynamic LOD
  171. const float distanceFromCamera = 1.0f;
  172. const float updateShadows = false;
  173. m_renderObject->UpdateRenderingParameters(
  174. &m_configuration.m_renderingSettings, RESERVED_PIXELS_FOR_OIT, distanceFromCamera, updateShadows);
  175. m_configChanged = false;
  176. // Only load the image asset when the dirty flag has been set on the settings.
  177. if (m_configuration.m_renderingSettings.m_imgDirty)
  178. {
  179. m_renderObject->LoadImageAsset(&m_configuration.m_renderingSettings);
  180. m_configuration.m_renderingSettings.m_imgDirty = false;
  181. }
  182. }
  183. // Update the enable flag for hair render object
  184. // The enable flag depends on the visibility of render actor instance and the flag of hair configuration.
  185. bool actorVisible = false;
  186. EMotionFX::Integration::ActorComponentRequestBus::EventResult(
  187. actorVisible, m_entityId, &EMotionFX::Integration::ActorComponentRequestBus::Events::GetRenderActorVisible);
  188. m_renderObject->SetEnabled(actorVisible);
  189. UpdateActorMatrices();
  190. }
  191. int HairComponentController::GetTickOrder()
  192. {
  193. return AZ::TICK_PRE_RENDER;
  194. }
  195. bool HairComponentController::UpdateActorMatrices()
  196. {
  197. if (!m_renderObject->IsEnabled())
  198. {
  199. return false;
  200. }
  201. EMotionFX::ActorInstance* actorInstance = nullptr;
  202. EMotionFX::Integration::ActorComponentRequestBus::EventResult(
  203. actorInstance, m_entityId, &EMotionFX::Integration::ActorComponentRequestBus::Events::GetActorInstance);
  204. if (!actorInstance)
  205. {
  206. return false;
  207. }
  208. const EMotionFX::TransformData* transformData = actorInstance->GetTransformData();
  209. if (!transformData)
  210. {
  211. AZ_WarningOnce("Hair Gem", false, "Error getting the transformData from the actorInstance.");
  212. return false;
  213. }
  214. // In EMotionFX the skinning matrices is stored as a 3x4. The conversion to 4x4 matrices happens at the update bone matrices function.
  215. // In here we use the boneIndexMap to find the correct EMotionFX bone index (also as the global bone index), and passing the
  216. // matrices of those bones to the hair render object. We do this for both hair and collision bone matrices.
  217. const AZ::Matrix3x4* matrices = transformData->GetSkinningMatrices();
  218. for (AZ::u32 tressFXBoneIndex = 0; tressFXBoneIndex < m_cachedHairBoneMatrices.size(); ++tressFXBoneIndex)
  219. {
  220. const AZ::u32 emfxBoneIndex = m_hairBoneIndexLookup[tressFXBoneIndex];
  221. m_cachedHairBoneMatrices[tressFXBoneIndex] = matrices[emfxBoneIndex];
  222. }
  223. for (AZ::u32 tressFXBoneIndex = 0; tressFXBoneIndex < m_cachedCollisionBoneMatrices.size(); ++tressFXBoneIndex)
  224. {
  225. const AZ::u32 emfxBoneIndex = m_collisionBoneIndexLookup[tressFXBoneIndex];
  226. m_cachedCollisionBoneMatrices[tressFXBoneIndex] = matrices[emfxBoneIndex];
  227. }
  228. m_entityWorldMatrix = Matrix3x4::CreateFromTransform(actorInstance->GetWorldSpaceTransform().ToAZTransform());
  229. m_renderObject->UpdateBoneMatrices(m_entityWorldMatrix, m_cachedHairBoneMatrices);
  230. return true;
  231. }
  232. bool HairComponentController::GenerateLocalToGlobalBoneIndex(
  233. EMotionFX::ActorInstance* actorInstance, AMD::TressFXAsset* hairAsset)
  234. {
  235. // Generate local TressFX to global EMFX bone index lookup.
  236. AMD::BoneNameToIndexMap globalNameToIndexMap;
  237. const EMotionFX::Skeleton* skeleton = actorInstance->GetActor()->GetSkeleton();
  238. if (!skeleton)
  239. {
  240. AZ_Error("Hair Gem", false, "Actor could not retrieve his skeleton.");
  241. return false;
  242. }
  243. const uint32_t numBones = uint32_t(skeleton->GetNumNodes());
  244. globalNameToIndexMap.reserve(size_t(numBones));
  245. for (uint32_t i = 0; i < numBones; ++i)
  246. {
  247. const char* boneName = skeleton->GetNode(i)->GetName();
  248. globalNameToIndexMap[boneName] = i;
  249. }
  250. if (!hairAsset->GenerateLocaltoGlobalHairBoneIndexLookup(globalNameToIndexMap, m_hairBoneIndexLookup) ||
  251. !hairAsset->GenerateLocaltoGlobalCollisionBoneIndexLookup(globalNameToIndexMap, m_collisionBoneIndexLookup))
  252. {
  253. AZ_Error("Hair Gem", false, "Cannot convert local bone index to global bone index. The hair asset may not be compatible with the actor.");
  254. return false;
  255. }
  256. return true;
  257. }
  258. // The hair object will only be created if both conditions are met:
  259. // 1. The hair asset is loaded
  260. // 2. The actor instance is created
  261. bool HairComponentController::CreateHairObject()
  262. {
  263. // Do not create a hairRenderObject when actor instance hasn't been created.
  264. EMotionFX::ActorInstance* actorInstance = nullptr;
  265. EMotionFX::Integration::ActorComponentRequestBus::EventResult(
  266. actorInstance, m_entityId, &EMotionFX::Integration::ActorComponentRequestBus::Events::GetActorInstance);
  267. if (!actorInstance)
  268. {
  269. return false;
  270. }
  271. if (!m_featureProcessor)
  272. {
  273. AZ_Error("Hair Gem", false, "Required feature processor does not exist yet");
  274. return false;
  275. }
  276. if (!m_configuration.m_hairAsset.GetId().IsValid() || !m_configuration.m_hairAsset.IsReady())
  277. {
  278. AZ_Warning("Hair Gem", false, "Hair Asset was not ready - second attempt will be made when ready");
  279. return false;
  280. }
  281. AMD::TressFXAsset* hairAsset = m_configuration.m_hairAsset.Get()->m_tressFXAsset.get();
  282. if (!hairAsset)
  283. {
  284. AZ_Error("Hair Gem", false, "Hair asset could not be loaded");
  285. return false;
  286. }
  287. if (!GenerateLocalToGlobalBoneIndex(actorInstance, hairAsset))
  288. {
  289. return false;
  290. }
  291. // First remove the existing hair object - this can happen if the configuration
  292. // or the hair asset selected changes.
  293. RemoveHairObject();
  294. // create a new instance - will remove the old one.
  295. m_renderObject.reset(new HairRenderObject());
  296. AZStd::string hairName;
  297. AzFramework::StringFunc::Path::GetFileName(m_configuration.m_hairAsset.GetHint().c_str(), hairName);
  298. if (!m_renderObject->Init( m_featureProcessor, hairName.c_str(), hairAsset,
  299. &m_configuration.m_simulationSettings, &m_configuration.m_renderingSettings))
  300. {
  301. AZ_Warning("Hair Gem", false, "Hair object was not initialize succesfully");
  302. m_renderObject.reset(); // no instancing yet - remove manually
  303. return false;
  304. }
  305. // Resize the bone matrices array. The size should equal to the number of bones in the tressFXAsset.
  306. m_cachedHairBoneMatrices.resize(m_hairBoneIndexLookup.size());
  307. m_cachedCollisionBoneMatrices.resize(m_collisionBoneIndexLookup.size());
  308. // Feature processor registration that will hold an instance.
  309. // Remark: DO NOT remove the TressFX asset - it's data might be required for
  310. // more instance hair objects.
  311. m_featureProcessor->AddHairRenderObject(m_renderObject);
  312. return true;
  313. }
  314. } // namespace Hair
  315. } // namespace Render
  316. } // namespace AZ