MeshExampleComponent.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <MeshExampleComponent.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. namespace AtomSampleViewer
  32. {
  33. const char* MeshExampleComponent::CameraControllerNameTable[CameraControllerCount] =
  34. {
  35. "ArcBall",
  36. "NoClip"
  37. };
  38. void MeshExampleComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  41. {
  42. serializeContext->Class<MeshExampleComponent, AZ::Component>()
  43. ->Version(0)
  44. ;
  45. }
  46. }
  47. MeshExampleComponent::MeshExampleComponent()
  48. : m_materialBrowser("@user@/MeshExampleComponent/material_browser.xml")
  49. , m_modelBrowser("@user@/MeshExampleComponent/model_browser.xml")
  50. , m_imguiSidebar("@user@/MeshExampleComponent/sidebar.xml")
  51. {
  52. m_changedHandler = AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler
  53. {
  54. [&](AZ::Data::Instance<AZ::RPI::Model> model)
  55. {
  56. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  57. // This handler will be connected to the feature processor so that when the model is updated, the camera
  58. // controller will reset. This ensures the camera is a reasonable distance from the model when it resizes.
  59. ResetCameraController();
  60. UpdateGroundPlane();
  61. }
  62. };
  63. }
  64. void MeshExampleComponent::DefaultWindowCreated()
  65. {
  66. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext, &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  67. }
  68. void MeshExampleComponent::CreateLowEndPipeline()
  69. {
  70. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  71. pipelineDesc.m_mainViewTagName = "MainCamera";
  72. pipelineDesc.m_name = "LowEndPipeline";
  73. pipelineDesc.m_rootPassTemplate = "LowEndPipelineTemplate";
  74. pipelineDesc.m_renderSettings.m_multisampleState.m_samples = 4;
  75. m_lowEndPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  76. }
  77. void MeshExampleComponent::DestroyLowEndPipeline()
  78. {
  79. m_lowEndPipeline = nullptr;
  80. }
  81. void MeshExampleComponent::ActivateLowEndPipeline()
  82. {
  83. m_originalPipeline = m_scene->GetDefaultRenderPipeline();
  84. m_scene->AddRenderPipeline(m_lowEndPipeline);
  85. m_lowEndPipeline->SetDefaultView(m_originalPipeline->GetDefaultView());
  86. m_scene->RemoveRenderPipeline(m_originalPipeline->GetId());
  87. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ m_lowEndPipeline->GetId().GetCStr(), "ImGuiPass" });
  88. }
  89. void MeshExampleComponent::DeactivateLowEndPipeline()
  90. {
  91. m_imguiScope = {}; // restores previous ImGui context.
  92. m_scene->AddRenderPipeline(m_originalPipeline);
  93. m_scene->RemoveRenderPipeline(m_lowEndPipeline->GetId());
  94. }
  95. void MeshExampleComponent::Activate()
  96. {
  97. UseArcBallCameraController();
  98. m_materialBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
  99. {
  100. if (!AzFramework::StringFunc::Path::IsExtension(assetInfo.m_relativePath.c_str(), "azmaterial"))
  101. {
  102. return false;
  103. }
  104. if (m_showModelMaterials)
  105. {
  106. return true;
  107. }
  108. // Return true only if the azmaterial was generated from a ".material" file.
  109. // Materials with subid == 0, are 99.99% guaranteed to be generated from a ".material" file.
  110. // Without this assurance We would need to call AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourceUUID()
  111. // to figure out what's the source of this azmaterial. But, Atom can not include AzToolsFramework.
  112. return assetInfo.m_assetId.m_subId == 0;
  113. });
  114. m_modelBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
  115. {
  116. return assetInfo.m_assetType == azrtti_typeid<AZ::RPI::ModelAsset>();
  117. });
  118. m_materialBrowser.Activate();
  119. m_modelBrowser.Activate();
  120. m_imguiSidebar.Activate();
  121. InitLightingPresets(true);
  122. AZ::Data::Asset<AZ::RPI::MaterialAsset> groundPlaneMaterialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  123. m_groundPlaneMaterial = AZ::RPI::Material::FindOrCreate(groundPlaneMaterialAsset);
  124. m_groundPlaneModelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  125. AZ::TickBus::Handler::BusConnect();
  126. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  127. CreateLowEndPipeline();
  128. }
  129. void MeshExampleComponent::Deactivate()
  130. {
  131. if (m_useLowEndPipeline)
  132. {
  133. DeactivateLowEndPipeline();
  134. }
  135. DestroyLowEndPipeline();
  136. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  137. AZ::TickBus::Handler::BusDisconnect();
  138. m_imguiSidebar.Deactivate();
  139. m_materialBrowser.Deactivate();
  140. m_modelBrowser.Deactivate();
  141. RemoveController();
  142. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  143. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  144. m_modelAsset = {};
  145. m_groundPlaneModelAsset = {};
  146. m_materialOverrideInstance = nullptr;
  147. ShutdownLightingPresets();
  148. }
  149. void MeshExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  150. {
  151. bool modelNeedsUpdate = false;
  152. // Switch pipeline before any imGui actions (switching pipelines switches imGui scope)
  153. if (m_switchPipeline)
  154. {
  155. if (m_useLowEndPipeline)
  156. {
  157. ActivateLowEndPipeline();
  158. }
  159. else
  160. {
  161. DeactivateLowEndPipeline();
  162. }
  163. m_switchPipeline = false;
  164. }
  165. if (m_imguiSidebar.Begin())
  166. {
  167. ImGuiLightingPreset();
  168. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  169. m_switchPipeline = ScriptableImGui::Checkbox("Use Low End Pipeline", &m_useLowEndPipeline) || m_switchPipeline;
  170. modelNeedsUpdate |= ScriptableImGui::Checkbox("Enable Material Override", &m_enableMaterialOverride);
  171. if (ScriptableImGui::Checkbox("Show Ground Plane", &m_showGroundPlane))
  172. {
  173. if (m_showGroundPlane)
  174. {
  175. CreateGroundPlane();
  176. UpdateGroundPlane();
  177. }
  178. else
  179. {
  180. RemoveGroundPlane();
  181. }
  182. }
  183. if (ScriptableImGui::Checkbox("Show Model Materials", &m_showModelMaterials))
  184. {
  185. modelNeedsUpdate = true;
  186. m_materialBrowser.SetNeedsRefresh();
  187. }
  188. assetBrowserSettings.m_labels.m_root = "Materials";
  189. modelNeedsUpdate |= m_materialBrowser.Tick(assetBrowserSettings);
  190. ImGui::Spacing();
  191. ImGui::Separator();
  192. ImGui::Spacing();
  193. assetBrowserSettings.m_labels.m_root = "Models";
  194. bool modelChanged = m_modelBrowser.Tick(assetBrowserSettings);
  195. modelNeedsUpdate |= modelChanged;
  196. if (modelChanged)
  197. {
  198. // Reset LOD override when the model changes.
  199. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::Default;
  200. }
  201. AZ::Data::Instance<AZ::RPI::Model> model = GetMeshFeatureProcessor()->GetModel(m_meshHandle);
  202. if (model)
  203. {
  204. const char* NoLodOverrideText = "No LOD Override";
  205. const char* LodFormatString = "LOD %i";
  206. AZStd::string previewText = m_lodConfig.m_lodType == AZ::RPI::Cullable::LodType::Default ?
  207. NoLodOverrideText :
  208. AZStd::string::format(LodFormatString, m_lodConfig.m_lodOverride);
  209. if (ScriptableImGui::BeginCombo("", previewText.c_str()))
  210. {
  211. if (ScriptableImGui::Selectable(NoLodOverrideText, m_lodConfig.m_lodType == AZ::RPI::Cullable::LodType::Default))
  212. {
  213. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::Default;
  214. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  215. }
  216. for (uint32_t i = 0; i < model->GetLodCount(); ++i)
  217. {
  218. AZStd::string name = AZStd::string::format(LodFormatString, i);
  219. if (ScriptableImGui::Selectable(name.c_str(), m_lodConfig.m_lodOverride == i))
  220. {
  221. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::SpecificLod;
  222. m_lodConfig.m_lodOverride = static_cast<AZ::RPI::Cullable::LodOverride>(i);
  223. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  224. }
  225. }
  226. ScriptableImGui::EndCombo();
  227. }
  228. }
  229. ImGui::Spacing();
  230. ImGui::Separator();
  231. ImGui::Spacing();
  232. // Camera controls
  233. {
  234. int32_t* currentControllerTypeIndex = reinterpret_cast<int32_t*>(&m_currentCameraControllerType);
  235. ImGui::LabelText("##CameraControllerLabel", "Camera Controller:");
  236. if (ScriptableImGui::Combo("##CameraController", currentControllerTypeIndex, CameraControllerNameTable, CameraControllerCount))
  237. {
  238. ResetCameraController();
  239. }
  240. }
  241. ImGui::Spacing();
  242. ImGui::Separator();
  243. ImGui::Spacing();
  244. if (m_materialOverrideInstance && ImGui::Button("Material Details..."))
  245. {
  246. m_imguiMaterialDetails.SetMaterial(m_materialOverrideInstance);
  247. m_imguiMaterialDetails.OpenDialog();
  248. }
  249. m_imguiSidebar.End();
  250. }
  251. m_imguiMaterialDetails.Tick();
  252. if (modelNeedsUpdate)
  253. {
  254. ModelChange();
  255. }
  256. }
  257. void MeshExampleComponent::ModelChange()
  258. {
  259. if (!m_modelBrowser.GetSelectedAssetId().IsValid())
  260. {
  261. m_modelAsset = {};
  262. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  263. return;
  264. }
  265. // If a material hasn't been selected, just choose the first one
  266. // If for some reason no materials are available log an error
  267. AZ::Data::AssetId selectedMaterialAssetId = m_materialBrowser.GetSelectedAssetId();
  268. if (!selectedMaterialAssetId.IsValid())
  269. {
  270. selectedMaterialAssetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  271. if (!selectedMaterialAssetId.IsValid())
  272. {
  273. AZ_Error("MeshExampleComponent", false, "Failed to select model, no material available to render with.");
  274. return;
  275. }
  276. }
  277. if (m_enableMaterialOverride && selectedMaterialAssetId.IsValid())
  278. {
  279. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset;
  280. materialAsset.Create(selectedMaterialAssetId);
  281. m_materialOverrideInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  282. }
  283. else
  284. {
  285. m_materialOverrideInstance = nullptr;
  286. }
  287. if (m_modelAsset.GetId() != m_modelBrowser.GetSelectedAssetId())
  288. {
  289. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  290. m_modelAsset.Create(m_modelBrowser.GetSelectedAssetId());
  291. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  292. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset }, m_materialOverrideInstance);
  293. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  294. GetMeshFeatureProcessor()->ConnectModelChangeEventHandler(m_meshHandle, m_changedHandler);
  295. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  296. }
  297. else
  298. {
  299. GetMeshFeatureProcessor()->SetMaterialAssignmentMap(m_meshHandle, m_materialOverrideInstance);
  300. }
  301. }
  302. void MeshExampleComponent::CreateGroundPlane()
  303. {
  304. m_groundPlandMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_groundPlaneModelAsset }, m_groundPlaneMaterial);
  305. }
  306. void MeshExampleComponent::UpdateGroundPlane()
  307. {
  308. if (m_groundPlandMeshHandle.IsValid())
  309. {
  310. AZ::Transform groundPlaneTransform = AZ::Transform::CreateIdentity();
  311. AZ::Vector3 modelCenter;
  312. float modelRadius;
  313. m_modelAsset->GetAabb().GetAsSphere(modelCenter, modelRadius);
  314. static const float GroundPlaneRelativeScale = 4.0f;
  315. static const float GroundPlaneOffset = 0.01f;
  316. groundPlaneTransform.SetUniformScale(GroundPlaneRelativeScale * modelRadius);
  317. groundPlaneTransform.SetTranslation(AZ::Vector3(0.0f, 0.0f, m_modelAsset->GetAabb().GetMin().GetZ() - GroundPlaneOffset));
  318. GetMeshFeatureProcessor()->SetTransform(m_groundPlandMeshHandle, groundPlaneTransform);
  319. }
  320. }
  321. void MeshExampleComponent::RemoveGroundPlane()
  322. {
  323. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  324. }
  325. void MeshExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  326. {
  327. OnLightingPresetEntityShutdown(entityId);
  328. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  329. }
  330. void MeshExampleComponent::UseArcBallCameraController()
  331. {
  332. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  333. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  334. }
  335. void MeshExampleComponent::UseNoClipCameraController()
  336. {
  337. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  338. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  339. }
  340. void MeshExampleComponent::RemoveController()
  341. {
  342. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  343. }
  344. void MeshExampleComponent::SetArcBallControllerParams()
  345. {
  346. if (!m_modelBrowser.GetSelectedAssetId().IsValid() || !m_modelAsset.IsReady())
  347. {
  348. return;
  349. }
  350. // Adjust the arc-ball controller so that it has bounds that make sense for the current model
  351. AZ::Vector3 center;
  352. float radius;
  353. m_modelAsset->GetAabb().GetAsSphere(center, radius);
  354. const float startingDistance = radius * ArcballRadiusDefaultModifier;
  355. const float minDistance = radius * ArcballRadiusMinModifier;
  356. const float maxDistance = radius * ArcballRadiusMaxModifier;
  357. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  358. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, startingDistance);
  359. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMinDistance, minDistance);
  360. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMaxDistance, maxDistance);
  361. }
  362. void MeshExampleComponent::ResetCameraController()
  363. {
  364. RemoveController();
  365. if (m_currentCameraControllerType == CameraControllerType::ArcBall)
  366. {
  367. UseArcBallCameraController();
  368. SetArcBallControllerParams();
  369. }
  370. else if (m_currentCameraControllerType == CameraControllerType::NoClip)
  371. {
  372. UseNoClipCameraController();
  373. }
  374. }
  375. } // namespace AtomSampleViewer