MeshExampleComponent.cpp 22 KB

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