MeshletsExampleComponent.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <MeshletsExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  10. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  11. #include <Atom/RHI/Device.h>
  12. #include <Atom/RHI/Factory.h>
  13. #include <Atom/RPI.Public/View.h>
  14. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  15. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  16. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  17. #include <AzCore/Asset/AssetManagerBus.h>
  18. #include <AzCore/Component/Entity.h>
  19. #include <AzCore/IO/IOUtils.h>
  20. #include <AzCore/Serialization/SerializeContext.h>
  21. #include <AzCore/std/smart_ptr/make_shared.h>
  22. #include <AzCore/std/sort.h>
  23. #include <AzFramework/Components/TransformComponent.h>
  24. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  25. #include <SampleComponentManager.h>
  26. #include <SampleComponentConfig.h>
  27. #include <EntityUtilityFunctions.h>
  28. #include <Automation/ScriptableImGui.h>
  29. #include <Automation/ScriptRunnerBus.h>
  30. #include <RHI/BasicRHIComponent.h>
  31. #include <MeshletsFeatureProcessor.h>
  32. namespace AtomSampleViewer
  33. {
  34. const char* MeshletsExampleComponent::CameraControllerNameTable[CameraControllerCount] =
  35. {
  36. "ArcBall",
  37. "NoClip"
  38. };
  39. void MeshletsExampleComponent::Reflect(AZ::ReflectContext* context)
  40. {
  41. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  42. {
  43. serializeContext->Class<MeshletsExampleComponent, AZ::Component>()
  44. ->Version(0)
  45. ;
  46. }
  47. }
  48. MeshletsExampleComponent::MeshletsExampleComponent()
  49. : m_materialBrowser("@user@/MeshExampleComponent/material_browser.xml")
  50. , m_modelBrowser("@user@/MeshExampleComponent/model_browser.xml")
  51. , m_imguiSidebar("@user@/MeshExampleComponent/sidebar.xml")
  52. {
  53. m_changedHandler = AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler
  54. {
  55. [&](AZ::Data::Instance<AZ::RPI::Model> model)
  56. {
  57. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  58. // This handler will be connected to the feature processor so that when the model is updated, the camera
  59. // controller will reset. This ensures the camera is a reasonable distance from the model when it resizes.
  60. ResetCameraController();
  61. UpdateGroundPlane();
  62. }
  63. };
  64. }
  65. void MeshletsExampleComponent::DefaultWindowCreated()
  66. {
  67. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext, &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  68. }
  69. void MeshletsExampleComponent::Activate()
  70. {
  71. UseArcBallCameraController();
  72. m_materialBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
  73. {
  74. if (!AzFramework::StringFunc::Path::IsExtension(assetInfo.m_relativePath.c_str(), "azmaterial"))
  75. {
  76. return false;
  77. }
  78. if (m_showModelMaterials)
  79. {
  80. return true;
  81. }
  82. // Return true only if the azmaterial was generated from a ".material" file.
  83. // Materials with subid == 0, are 99.99% guaranteed to be generated from a ".material" file.
  84. // Without this assurance We would need to call AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourceUUID()
  85. // to figure out what's the source of this azmaterial. But, Atom can not include AzToolsFramework.
  86. return assetInfo.m_assetId.m_subId == 0;
  87. });
  88. m_modelBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
  89. {
  90. return assetInfo.m_assetType == azrtti_typeid<AZ::RPI::ModelAsset>();
  91. });
  92. m_materialBrowser.Activate();
  93. m_modelBrowser.Activate();
  94. m_imguiSidebar.Activate();
  95. InitLightingPresets(true);
  96. AZ::Data::Asset<AZ::RPI::MaterialAsset> groundPlaneMaterialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  97. m_groundPlaneMaterial = AZ::RPI::Material::FindOrCreate(groundPlaneMaterialAsset);
  98. m_groundPlaneModelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  99. AZ::TickBus::Handler::BusConnect();
  100. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  101. }
  102. void MeshletsExampleComponent::Deactivate()
  103. {
  104. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  105. AZ::TickBus::Handler::BusDisconnect();
  106. m_imguiSidebar.Deactivate();
  107. m_materialBrowser.Deactivate();
  108. m_modelBrowser.Deactivate();
  109. RemoveController();
  110. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  111. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  112. if (m_meshetsModel)
  113. {
  114. GetMeshFeatureProcessor()->ReleaseMesh(m_meshletsMeshHandle);
  115. delete m_meshetsModel;
  116. m_meshetsModel = nullptr;
  117. if (GetMeshletsFeatureProcessor())
  118. {
  119. m_meshletsFeatureProcessor->RemoveMeshletsRenderObject(m_meshetsRenderObject);
  120. // No deletion - this will be done by the feature processor
  121. m_meshetsRenderObject = nullptr;
  122. }
  123. }
  124. m_modelAsset = {};
  125. m_groundPlaneModelAsset = {};
  126. m_materialOverrideInstance = nullptr;
  127. ShutdownLightingPresets();
  128. }
  129. AZ::Meshlets::MeshletsFeatureProcessor* MeshletsExampleComponent::GetMeshletsFeatureProcessor()
  130. {
  131. if (!m_meshletsFeatureProcessor && m_scene)
  132. {
  133. m_meshletsFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Meshlets::MeshletsFeatureProcessor>();
  134. }
  135. return m_meshletsFeatureProcessor;
  136. }
  137. void MeshletsExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  138. {
  139. bool modelNeedsUpdate = false;
  140. if (m_imguiSidebar.Begin())
  141. {
  142. ImGuiLightingPreset();
  143. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  144. modelNeedsUpdate |= ScriptableImGui::Checkbox("Enable Material Override", &m_enableMaterialOverride);
  145. if (ScriptableImGui::Checkbox("Show Ground Plane", &m_showGroundPlane))
  146. {
  147. if (m_showGroundPlane)
  148. {
  149. CreateGroundPlane();
  150. UpdateGroundPlane();
  151. }
  152. else
  153. {
  154. RemoveGroundPlane();
  155. }
  156. }
  157. if (ScriptableImGui::Checkbox("Show Model Materials", &m_showModelMaterials))
  158. {
  159. modelNeedsUpdate = true;
  160. m_materialBrowser.SetNeedsRefresh();
  161. }
  162. assetBrowserSettings.m_labels.m_root = "Materials";
  163. modelNeedsUpdate |= m_materialBrowser.Tick(assetBrowserSettings);
  164. ImGui::Spacing();
  165. ImGui::Separator();
  166. ImGui::Spacing();
  167. assetBrowserSettings.m_labels.m_root = "Models";
  168. bool modelChanged = m_modelBrowser.Tick(assetBrowserSettings);
  169. modelNeedsUpdate |= modelChanged;
  170. if (modelChanged)
  171. {
  172. // Reset LOD override when the model changes.
  173. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::Default;
  174. }
  175. ImGui::Spacing();
  176. ImGui::Separator();
  177. ImGui::Spacing();
  178. // Camera controls
  179. {
  180. int32_t* currentControllerTypeIndex = reinterpret_cast<int32_t*>(&m_currentCameraControllerType);
  181. ImGui::LabelText("##CameraControllerLabel", "Camera Controller:");
  182. if (ScriptableImGui::Combo("##CameraController", currentControllerTypeIndex, CameraControllerNameTable, CameraControllerCount))
  183. {
  184. ResetCameraController();
  185. }
  186. }
  187. ImGui::Spacing();
  188. ImGui::Separator();
  189. ImGui::Spacing();
  190. if (m_materialOverrideInstance && ImGui::Button("Material Details..."))
  191. {
  192. m_imguiMaterialDetails.OpenDialog();
  193. }
  194. m_imguiSidebar.End();
  195. }
  196. m_imguiMaterialDetails.Tick(&GetMeshFeatureProcessor()->GetDrawPackets(m_meshHandle));
  197. if (modelNeedsUpdate)
  198. {
  199. ModelChange();
  200. }
  201. }
  202. void MeshletsExampleComponent::ModelChange()
  203. {
  204. if (!m_modelBrowser.GetSelectedAssetId().IsValid())
  205. {
  206. m_modelAsset = {};
  207. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  208. GetMeshFeatureProcessor()->ReleaseMesh(m_meshletsMeshHandle);
  209. if (GetMeshletsFeatureProcessor())
  210. {
  211. m_meshletsFeatureProcessor->RemoveMeshletsRenderObject(m_meshetsRenderObject);
  212. // No deletion - this will be done by the feature processor
  213. m_meshetsRenderObject = nullptr;
  214. }
  215. return;
  216. }
  217. // If a material hasn't been selected, just choose the first one
  218. // If for some reason no materials are available log an error
  219. AZ::Data::AssetId selectedMaterialAssetId = m_materialBrowser.GetSelectedAssetId();
  220. if (!selectedMaterialAssetId.IsValid())
  221. {
  222. selectedMaterialAssetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  223. if (!selectedMaterialAssetId.IsValid())
  224. {
  225. AZ_Error("MeshExampleComponent", false, "Failed to select model, no material available to render with.");
  226. return;
  227. }
  228. }
  229. if (m_enableMaterialOverride && selectedMaterialAssetId.IsValid())
  230. {
  231. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset;
  232. materialAsset.Create(selectedMaterialAssetId);
  233. m_materialOverrideInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  234. }
  235. else
  236. {
  237. m_materialOverrideInstance = nullptr;
  238. }
  239. if (m_modelAsset.GetId() != m_modelBrowser.GetSelectedAssetId())
  240. {
  241. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  242. m_modelAsset.Create(m_modelBrowser.GetSelectedAssetId());
  243. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  244. if (m_meshetsModel)
  245. { // delete the meshlet model so it will be recreated on the next tick
  246. if (GetMeshletsFeatureProcessor())
  247. {
  248. m_meshletsFeatureProcessor->RemoveMeshletsRenderObject(m_meshetsRenderObject);
  249. // No deletion - this will be done by the feature processor
  250. m_meshetsRenderObject = nullptr;
  251. }
  252. GetMeshFeatureProcessor()->ReleaseMesh(m_meshletsMeshHandle);
  253. delete m_meshetsModel;
  254. m_meshetsModel = nullptr;
  255. }
  256. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset }, m_materialOverrideInstance);
  257. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  258. GetMeshFeatureProcessor()->ConnectModelChangeEventHandler(m_meshHandle, m_changedHandler);
  259. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  260. }
  261. else
  262. {
  263. GetMeshFeatureProcessor()->SetMaterialAssignmentMap(m_meshHandle, m_materialOverrideInstance);
  264. }
  265. }
  266. void MeshletsExampleComponent::CreateGroundPlane()
  267. {
  268. m_groundPlandMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_groundPlaneModelAsset }, m_groundPlaneMaterial);
  269. }
  270. void MeshletsExampleComponent::UpdateGroundPlane()
  271. {
  272. if (m_groundPlandMeshHandle.IsValid())
  273. {
  274. AZ::Transform groundPlaneTransform = AZ::Transform::CreateIdentity();
  275. if (m_modelAsset)
  276. {
  277. AZ::Vector3 modelCenter;
  278. float modelRadius;
  279. m_modelAsset->GetAabb().GetAsSphere(modelCenter, modelRadius);
  280. static const float GroundPlaneRelativeScale = 4.0f;
  281. static const float GroundPlaneOffset = 0.01f;
  282. groundPlaneTransform.SetUniformScale(GroundPlaneRelativeScale * modelRadius);
  283. groundPlaneTransform.SetTranslation(AZ::Vector3(0.0f, 0.0f, m_modelAsset->GetAabb().GetMin().GetZ() - GroundPlaneOffset));
  284. }
  285. GetMeshFeatureProcessor()->SetTransform(m_groundPlandMeshHandle, groundPlaneTransform);
  286. }
  287. }
  288. void MeshletsExampleComponent::RemoveGroundPlane()
  289. {
  290. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  291. }
  292. void MeshletsExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  293. {
  294. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  295. }
  296. void MeshletsExampleComponent::UseArcBallCameraController()
  297. {
  298. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  299. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  300. }
  301. void MeshletsExampleComponent::UseNoClipCameraController()
  302. {
  303. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  304. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  305. }
  306. void MeshletsExampleComponent::RemoveController()
  307. {
  308. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  309. }
  310. void MeshletsExampleComponent::SetArcBallControllerParams()
  311. {
  312. if (!m_modelBrowser.GetSelectedAssetId().IsValid() || !m_modelAsset.IsReady())
  313. {
  314. return;
  315. }
  316. if (!m_meshetsModel)
  317. {
  318. m_meshetsModel = new AZ::Meshlets::MeshletsModel(m_modelAsset);
  319. if (m_meshetsModel->GetMeshletsModel())
  320. {
  321. static constexpr const char meshletDebugMaterialPath[] = "materials/debugshadermaterial_01.azmaterial";
  322. AZ::Data::Asset<AZ::RPI::MaterialAsset> meshletDebugMaterialAsset =
  323. AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(meshletDebugMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  324. m_meshletsDebugMaterial = AZ::RPI::Material::FindOrCreate(meshletDebugMaterialAsset);
  325. m_meshletsModelAsset = m_meshetsModel->GetMeshletsModel()->GetModelAsset();
  326. m_meshletsMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_meshletsModelAsset }, m_meshletsDebugMaterial);// m_materialOverrideInstance);
  327. AZ::Transform translation = AZ::Transform::CreateTranslation(AZ::Vector3(0.75, 1.5, 0));
  328. GetMeshFeatureProcessor()->SetTransform(m_meshletsMeshHandle, translation);
  329. }
  330. if (GetMeshletsFeatureProcessor())
  331. {
  332. m_meshetsRenderObject = new AZ::Meshlets::MeshletsRenderObject(m_modelAsset, m_meshletsFeatureProcessor);
  333. if (m_meshetsRenderObject->GetMeshletsCount())
  334. {
  335. m_meshletObjectId = m_meshletsFeatureProcessor->AddMeshletsRenderObject(m_meshetsRenderObject);
  336. AZ::Transform translation = AZ::Transform::CreateTranslation(AZ::Vector3(-0.75, 1.5, 0));
  337. m_meshletsFeatureProcessor->SetTransform(m_meshletObjectId, translation);
  338. }
  339. }
  340. AZ_Error("Meshlets", m_meshletsFeatureProcessor && m_meshetsRenderObject->GetMeshletsCount(),
  341. "Could not get MeshletsFeatureProcessor or meshlets were not generated");
  342. }
  343. // Adjust the arc-ball controller so that it has bounds that make sense for the current model
  344. AZ::Vector3 center;
  345. float radius;
  346. m_modelAsset->GetAabb().GetAsSphere(center, radius);
  347. const float startingDistance = radius * ArcballRadiusDefaultModifier;
  348. const float minDistance = radius * ArcballRadiusMinModifier;
  349. const float maxDistance = radius * ArcballRadiusMaxModifier;
  350. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  351. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, startingDistance);
  352. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMinDistance, minDistance);
  353. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMaxDistance, maxDistance);
  354. }
  355. void MeshletsExampleComponent::ResetCameraController()
  356. {
  357. RemoveController();
  358. if (m_currentCameraControllerType == CameraControllerType::ArcBall)
  359. {
  360. UseArcBallCameraController();
  361. SetArcBallControllerParams();
  362. }
  363. else if (m_currentCameraControllerType == CameraControllerType::NoClip)
  364. {
  365. UseNoClipCameraController();
  366. }
  367. }
  368. } // namespace AtomSampleViewer