MeshExampleComponent.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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::CreateDeferredPipeline()
  82. {
  83. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  84. pipelineDesc.m_mainViewTagName = "MainCamera";
  85. pipelineDesc.m_name = "DeferredPipeline";
  86. pipelineDesc.m_rootPassTemplate = "DeferredPipelineTemplate";
  87. pipelineDesc.m_renderSettings.m_multisampleState.m_samples = 4;
  88. pipelineDesc.m_allowModification = true; // MainPipeline allows modifications, so the DeferredPipeline must as well, to get a consistent result.
  89. m_deferredPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  90. }
  91. void MeshExampleComponent::DestroyDeferredPipeline()
  92. {
  93. m_deferredPipeline = nullptr;
  94. }
  95. void MeshExampleComponent::CreateMultiViewXRPipeline()
  96. {
  97. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  98. pipelineDesc.m_mainViewTagName = "MainCamera";
  99. pipelineDesc.m_name = "MultiViewPipeline";
  100. pipelineDesc.m_rootPassTemplate = "MultiViewPipelineTemplate";
  101. pipelineDesc.m_renderSettings.m_multisampleState.m_samples = 1;
  102. m_multiViewXRPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  103. }
  104. void MeshExampleComponent::DestroyMultiViewXRPipeline()
  105. {
  106. m_multiViewXRPipeline = nullptr;
  107. }
  108. void MeshExampleComponent::ActivateLowEndPipeline()
  109. {
  110. AZ::RPI::RenderPipelinePtr prevPipeline = m_scene->GetDefaultRenderPipeline();
  111. if (!m_originalPipeline)
  112. {
  113. m_originalPipeline = prevPipeline;
  114. }
  115. m_lowEndPipeline->GetRootPass()->SetEnabled(true); // PassSystem::RemoveRenderPipeline was calling SetEnabled(false)
  116. m_scene->AddRenderPipeline(m_lowEndPipeline);
  117. m_lowEndPipeline->SetDefaultView(prevPipeline->GetDefaultView());
  118. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  119. m_imguiScope = {}; // I'm not sure why this is needed. Something must be wrong in the ImGuiActiveContextScope class
  120. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ m_lowEndPipeline->GetId().GetCStr(), "ImGuiPass" });
  121. }
  122. void MeshExampleComponent::ActivateDeferredPipeline()
  123. {
  124. AZ::RPI::RenderPipelinePtr prevPipeline = m_scene->GetDefaultRenderPipeline();
  125. if (!m_originalPipeline)
  126. {
  127. m_originalPipeline = prevPipeline;
  128. }
  129. m_deferredPipeline->GetRootPass()->SetEnabled(true); // PassSystem::RemoveRenderPipeline was calling SetEnabled(false)
  130. m_scene->AddRenderPipeline(m_deferredPipeline);
  131. m_deferredPipeline->SetDefaultView(prevPipeline->GetDefaultView());
  132. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  133. m_imguiScope = {}; // I'm not sure why this is needed. Something must be wrong in the ImGuiActiveContextScope class
  134. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({m_deferredPipeline->GetId().GetCStr(), "ImGuiPass"});
  135. }
  136. void MeshExampleComponent::ActivateOriginalPipeline()
  137. {
  138. m_imguiScope = {}; // restores previous ImGui context.
  139. AZ::RPI::RenderPipelinePtr prevPipeline = m_scene->GetDefaultRenderPipeline();
  140. m_originalPipeline->GetRootPass()->SetEnabled(true); // PassSystem::RemoveRenderPipeline was calling SetEnabled(false)
  141. m_scene->AddRenderPipeline(m_originalPipeline);
  142. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  143. }
  144. void MeshExampleComponent::ActivateMultiViewXRPipeline()
  145. {
  146. AZ::RPI::RenderPipelinePtr prevPipeline = m_scene->GetDefaultRenderPipeline();
  147. if (!m_originalPipeline)
  148. {
  149. m_originalPipeline = prevPipeline;
  150. }
  151. m_multiViewXRPipeline->GetRootPass()->SetEnabled(true);
  152. m_scene->AddRenderPipeline(m_multiViewXRPipeline);
  153. m_multiViewXRPipeline->SetDefaultView(prevPipeline->GetDefaultView());
  154. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  155. m_imguiScope = {};
  156. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ m_multiViewXRPipeline->GetId().GetCStr(), "ImGuiPass" });
  157. }
  158. void MeshExampleComponent::Activate()
  159. {
  160. UseArcBallCameraController();
  161. m_materialBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
  162. {
  163. if (!AzFramework::StringFunc::Path::IsExtension(assetInfo.m_relativePath.c_str(), "azmaterial"))
  164. {
  165. return false;
  166. }
  167. if (m_showModelMaterials)
  168. {
  169. return true;
  170. }
  171. // Return true only if the azmaterial was generated from a ".material" file.
  172. // Materials with subid == 0, are 99.99% guaranteed to be generated from a ".material" file.
  173. // Without this assurance We would need to call AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourceUUID()
  174. // to figure out what's the source of this azmaterial. But, Atom can not include AzToolsFramework.
  175. return assetInfo.m_assetId.m_subId == 0;
  176. });
  177. m_modelBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
  178. {
  179. return assetInfo.m_assetType == azrtti_typeid<AZ::RPI::ModelAsset>();
  180. });
  181. m_materialBrowser.Activate();
  182. m_modelBrowser.Activate();
  183. m_imguiSidebar.Activate();
  184. InitLightingPresets(true);
  185. AZ::Data::Asset<AZ::RPI::MaterialAsset> groundPlaneMaterialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  186. m_groundPlaneMaterial = AZ::RPI::Material::FindOrCreate(groundPlaneMaterialAsset);
  187. m_groundPlaneModelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  188. AZ::TickBus::Handler::BusConnect();
  189. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  190. CreateLowEndPipeline();
  191. CreateDeferredPipeline();
  192. CreateMultiViewXRPipeline();
  193. }
  194. void MeshExampleComponent::Deactivate()
  195. {
  196. if (m_useLowEndPipeline || m_useDeferredPipeline || m_useMultiViewXRPipeline)
  197. {
  198. ActivateOriginalPipeline();
  199. }
  200. DestroyLowEndPipeline();
  201. DestroyDeferredPipeline();
  202. DestroyMultiViewXRPipeline();
  203. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  204. AZ::TickBus::Handler::BusDisconnect();
  205. m_imguiSidebar.Deactivate();
  206. m_materialBrowser.Deactivate();
  207. m_modelBrowser.Deactivate();
  208. RemoveController();
  209. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  210. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  211. m_modelAsset = {};
  212. m_groundPlaneModelAsset = {};
  213. m_materialOverrideInstance = nullptr;
  214. ShutdownLightingPresets();
  215. }
  216. void MeshExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  217. {
  218. bool modelNeedsUpdate = false;
  219. // Switch pipeline before any imGui actions (switching pipelines switches imGui scope)
  220. if (m_switchPipeline)
  221. {
  222. if (m_useLowEndPipeline)
  223. {
  224. ActivateLowEndPipeline();
  225. }
  226. else if (m_useDeferredPipeline)
  227. {
  228. ActivateDeferredPipeline();
  229. }
  230. else if (m_useMultiViewXRPipeline)
  231. {
  232. ActivateMultiViewXRPipeline();
  233. }
  234. else
  235. {
  236. ActivateOriginalPipeline();
  237. }
  238. m_switchPipeline = false;
  239. }
  240. if (m_imguiSidebar.Begin())
  241. {
  242. ImGuiLightingPreset();
  243. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  244. if (ScriptableImGui::Checkbox("Use Low End Pipeline", &m_useLowEndPipeline))
  245. {
  246. m_switchPipeline = true;
  247. m_useDeferredPipeline = false;
  248. m_useMultiViewXRPipeline = false;
  249. }
  250. if (ScriptableImGui::Checkbox("Use Deferred Pipeline", &m_useDeferredPipeline))
  251. {
  252. m_switchPipeline = true;
  253. m_useLowEndPipeline = false;
  254. m_useMultiViewXRPipeline = false;
  255. }
  256. if (ScriptableImGui::Checkbox("Use MultiViewXR Pipeline", &m_useMultiViewXRPipeline))
  257. {
  258. m_switchPipeline = true;
  259. m_useLowEndPipeline = false;
  260. m_useDeferredPipeline = false;
  261. }
  262. modelNeedsUpdate |= ScriptableImGui::Checkbox("Enable Material Override", &m_enableMaterialOverride);
  263. if (ScriptableImGui::Checkbox("Show Ground Plane", &m_showGroundPlane))
  264. {
  265. if (m_showGroundPlane)
  266. {
  267. CreateGroundPlane();
  268. UpdateGroundPlane();
  269. }
  270. else
  271. {
  272. RemoveGroundPlane();
  273. }
  274. }
  275. if (ScriptableImGui::Checkbox("Show Model Materials", &m_showModelMaterials))
  276. {
  277. modelNeedsUpdate = true;
  278. m_materialBrowser.SetNeedsRefresh();
  279. }
  280. assetBrowserSettings.m_labels.m_root = "Materials";
  281. modelNeedsUpdate |= m_materialBrowser.Tick(assetBrowserSettings);
  282. ImGui::Spacing();
  283. ImGui::Separator();
  284. ImGui::Spacing();
  285. assetBrowserSettings.m_labels.m_root = "Models";
  286. bool modelChanged = m_modelBrowser.Tick(assetBrowserSettings);
  287. modelNeedsUpdate |= modelChanged;
  288. if (modelChanged)
  289. {
  290. // Reset LOD override when the model changes.
  291. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::Default;
  292. }
  293. AZ::Data::Instance<AZ::RPI::Model> model = GetMeshFeatureProcessor()->GetModel(m_meshHandle);
  294. if (model)
  295. {
  296. const char* NoLodOverrideText = "No LOD Override";
  297. const char* LodFormatString = "LOD %i";
  298. AZStd::string previewText = m_lodConfig.m_lodType == AZ::RPI::Cullable::LodType::Default ?
  299. NoLodOverrideText :
  300. AZStd::string::format(LodFormatString, m_lodConfig.m_lodOverride);
  301. if (ScriptableImGui::BeginCombo("", previewText.c_str()))
  302. {
  303. if (ScriptableImGui::Selectable(NoLodOverrideText, m_lodConfig.m_lodType == AZ::RPI::Cullable::LodType::Default))
  304. {
  305. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::Default;
  306. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  307. }
  308. for (uint32_t i = 0; i < model->GetLodCount(); ++i)
  309. {
  310. AZStd::string name = AZStd::string::format(LodFormatString, i);
  311. if (ScriptableImGui::Selectable(name.c_str(), m_lodConfig.m_lodOverride == i))
  312. {
  313. m_lodConfig.m_lodType = AZ::RPI::Cullable::LodType::SpecificLod;
  314. m_lodConfig.m_lodOverride = static_cast<AZ::RPI::Cullable::LodOverride>(i);
  315. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  316. }
  317. }
  318. ScriptableImGui::EndCombo();
  319. }
  320. }
  321. ImGui::Spacing();
  322. ImGui::Separator();
  323. ImGui::Spacing();
  324. // Camera controls
  325. {
  326. int32_t* currentControllerTypeIndex = reinterpret_cast<int32_t*>(&m_currentCameraControllerType);
  327. ImGui::LabelText("##CameraControllerLabel", "Camera Controller:");
  328. if (ScriptableImGui::Combo("##CameraController", currentControllerTypeIndex, CameraControllerNameTable, CameraControllerCount))
  329. {
  330. ResetCameraController();
  331. }
  332. }
  333. ImGui::Spacing();
  334. ImGui::Separator();
  335. ImGui::Spacing();
  336. if (m_materialOverrideInstance && ImGui::Button("Material Details..."))
  337. {
  338. m_imguiMaterialDetails.OpenDialog();
  339. }
  340. m_imguiSidebar.End();
  341. }
  342. m_imguiMaterialDetails.Tick(&GetMeshFeatureProcessor()->GetDrawPackets(m_meshHandle));
  343. if (modelNeedsUpdate)
  344. {
  345. ModelChange();
  346. }
  347. }
  348. void MeshExampleComponent::ModelChange()
  349. {
  350. if (!m_modelBrowser.GetSelectedAssetId().IsValid())
  351. {
  352. m_modelAsset = {};
  353. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  354. return;
  355. }
  356. // If a material hasn't been selected, just choose the first one
  357. // If for some reason no materials are available log an error
  358. AZ::Data::AssetId selectedMaterialAssetId = m_materialBrowser.GetSelectedAssetId();
  359. if (!selectedMaterialAssetId.IsValid())
  360. {
  361. selectedMaterialAssetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  362. if (!selectedMaterialAssetId.IsValid())
  363. {
  364. AZ_Error("MeshExampleComponent", false, "Failed to select model, no material available to render with.");
  365. return;
  366. }
  367. }
  368. if (m_enableMaterialOverride && selectedMaterialAssetId.IsValid())
  369. {
  370. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset;
  371. materialAsset.Create(selectedMaterialAssetId);
  372. m_materialOverrideInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  373. }
  374. else
  375. {
  376. m_materialOverrideInstance = nullptr;
  377. }
  378. if (m_modelAsset.GetId() != m_modelBrowser.GetSelectedAssetId())
  379. {
  380. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  381. m_modelAsset.Create(m_modelBrowser.GetSelectedAssetId());
  382. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  383. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset }, m_materialOverrideInstance);
  384. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  385. GetMeshFeatureProcessor()->ConnectModelChangeEventHandler(m_meshHandle, m_changedHandler);
  386. GetMeshFeatureProcessor()->SetMeshLodConfiguration(m_meshHandle, m_lodConfig);
  387. }
  388. else
  389. {
  390. GetMeshFeatureProcessor()->SetMaterialAssignmentMap(m_meshHandle, m_materialOverrideInstance);
  391. }
  392. }
  393. void MeshExampleComponent::CreateGroundPlane()
  394. {
  395. m_groundPlandMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_groundPlaneModelAsset }, m_groundPlaneMaterial);
  396. }
  397. void MeshExampleComponent::UpdateGroundPlane()
  398. {
  399. if (m_groundPlandMeshHandle.IsValid())
  400. {
  401. AZ::Transform groundPlaneTransform = AZ::Transform::CreateIdentity();
  402. if (m_modelAsset)
  403. {
  404. AZ::Vector3 modelCenter;
  405. float modelRadius;
  406. m_modelAsset->GetAabb().GetAsSphere(modelCenter, modelRadius);
  407. static const float GroundPlaneRelativeScale = 4.0f;
  408. static const float GroundPlaneOffset = 0.01f;
  409. groundPlaneTransform.SetUniformScale(GroundPlaneRelativeScale * modelRadius);
  410. groundPlaneTransform.SetTranslation(AZ::Vector3(0.0f, 0.0f, m_modelAsset->GetAabb().GetMin().GetZ() - GroundPlaneOffset));
  411. }
  412. GetMeshFeatureProcessor()->SetTransform(m_groundPlandMeshHandle, groundPlaneTransform);
  413. }
  414. }
  415. void MeshExampleComponent::RemoveGroundPlane()
  416. {
  417. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  418. }
  419. void MeshExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  420. {
  421. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  422. }
  423. void MeshExampleComponent::UseArcBallCameraController()
  424. {
  425. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  426. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  427. }
  428. void MeshExampleComponent::UseNoClipCameraController()
  429. {
  430. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  431. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  432. }
  433. void MeshExampleComponent::RemoveController()
  434. {
  435. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  436. }
  437. void MeshExampleComponent::SetArcBallControllerParams()
  438. {
  439. if (!m_modelBrowser.GetSelectedAssetId().IsValid() || !m_modelAsset.IsReady())
  440. {
  441. return;
  442. }
  443. // Adjust the arc-ball controller so that it has bounds that make sense for the current model
  444. AZ::Vector3 center;
  445. float radius;
  446. m_modelAsset->GetAabb().GetAsSphere(center, radius);
  447. const float startingDistance = radius * ArcballRadiusDefaultModifier;
  448. const float minDistance = radius * ArcballRadiusMinModifier;
  449. const float maxDistance = radius * ArcballRadiusMaxModifier;
  450. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  451. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, startingDistance);
  452. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMinDistance, minDistance);
  453. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMaxDistance, maxDistance);
  454. }
  455. void MeshExampleComponent::ResetCameraController()
  456. {
  457. RemoveController();
  458. if (m_currentCameraControllerType == CameraControllerType::ArcBall)
  459. {
  460. UseArcBallCameraController();
  461. SetArcBallControllerParams();
  462. }
  463. else if (m_currentCameraControllerType == CameraControllerType::NoClip)
  464. {
  465. UseNoClipCameraController();
  466. }
  467. }
  468. } // namespace AtomSampleViewer