MeshExampleComponent.cpp 22 KB

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