MeshExampleComponent.cpp 22 KB

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