MeshExampleComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <MeshExampleComponent.h>
  13. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  14. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  15. #include <Atom/RHI/Device.h>
  16. #include <Atom/RHI/Factory.h>
  17. #include <Atom/RPI.Public/View.h>
  18. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  19. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  20. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  21. #include <AzCore/Asset/AssetManagerBus.h>
  22. #include <AzCore/Component/Entity.h>
  23. #include <AzCore/IO/IOUtils.h>
  24. #include <AzCore/Serialization/SerializeContext.h>
  25. #include <AzCore/std/smart_ptr/make_shared.h>
  26. #include <AzCore/std/sort.h>
  27. #include <AzFramework/Components/TransformComponent.h>
  28. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  29. #include <SampleComponentManager.h>
  30. #include <SampleComponentConfig.h>
  31. #include <EntityUtilityFunctions.h>
  32. #include <Automation/ScriptableImGui.h>
  33. #include <Automation/ScriptRunnerBus.h>
  34. #include <RHI/BasicRHIComponent.h>
  35. namespace AtomSampleViewer
  36. {
  37. const char* MeshExampleComponent::CameraControllerNameTable[CameraControllerCount] =
  38. {
  39. "ArcBall",
  40. "NoClip"
  41. };
  42. void MeshExampleComponent::Reflect(AZ::ReflectContext* context)
  43. {
  44. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  45. {
  46. serializeContext->Class<MeshExampleComponent, AZ::Component>()
  47. ->Version(0)
  48. ;
  49. }
  50. }
  51. MeshExampleComponent::MeshExampleComponent()
  52. : m_materialBrowser("@user@/MeshExampleComponent/material_browser.xml")
  53. , m_modelBrowser("@user@/MeshExampleComponent/model_browser.xml")
  54. , m_imguiSidebar("@user@/MeshExampleComponent/sidebar.xml")
  55. {
  56. m_changedHandler = AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler
  57. {
  58. [&](AZ::Data::Instance<AZ::RPI::Model> model)
  59. {
  60. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  61. // This handler will be connected to the feature processor so that when the model is updated, the camera
  62. // controller will reset. This ensures the camera is a reasonable distance from the model when it resizes.
  63. ResetCameraController();
  64. UpdateGroundPlane();
  65. }
  66. };
  67. }
  68. void MeshExampleComponent::Activate()
  69. {
  70. UseArcBallCameraController();
  71. m_materialBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
  72. {
  73. if (!AzFramework::StringFunc::Path::IsExtension(assetInfo.m_relativePath.c_str(), "azmaterial"))
  74. {
  75. return false;
  76. }
  77. if (m_showModelMaterials)
  78. {
  79. return true;
  80. }
  81. // Return true only if the azmaterial was generated from a ".material" file.
  82. // Materials with subid == 0, are 99.99% guaranteed to be generated from a ".material" file.
  83. // Without this assurance We would need to call AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourceUUID()
  84. // to figure out what's the source of this azmaterial. But, Atom can not include AzToolsFramework.
  85. return assetInfo.m_assetId.m_subId == 0;
  86. });
  87. m_modelBrowser.SetFilter([](const AZ::Data::AssetInfo& assetInfo)
  88. {
  89. return assetInfo.m_assetType == azrtti_typeid<AZ::RPI::ModelAsset>();
  90. });
  91. m_materialBrowser.Activate();
  92. m_modelBrowser.Activate();
  93. m_imguiSidebar.Activate();
  94. InitLightingPresets(true);
  95. AZ::Data::Asset<AZ::RPI::MaterialAsset> groundPlaneMaterialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  96. m_groundPlaneMaterial = AZ::RPI::Material::FindOrCreate(groundPlaneMaterialAsset);
  97. m_groundPlaneModelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  98. AZ::TickBus::Handler::BusConnect();
  99. }
  100. void MeshExampleComponent::Deactivate()
  101. {
  102. AZ::TickBus::Handler::BusDisconnect();
  103. m_imguiSidebar.Deactivate();
  104. m_materialBrowser.Deactivate();
  105. m_modelBrowser.Deactivate();
  106. RemoveController();
  107. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  108. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  109. m_modelAsset = {};
  110. m_groundPlaneModelAsset = {};
  111. m_materialOverrideInstance = nullptr;
  112. ShutdownLightingPresets();
  113. }
  114. void MeshExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  115. {
  116. bool modelNeedsUpdate = false;
  117. if (m_imguiSidebar.Begin())
  118. {
  119. ImGuiLightingPreset();
  120. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  121. modelNeedsUpdate |= ScriptableImGui::Checkbox("Enable Material Override", &m_enableMaterialOverride);
  122. if (ScriptableImGui::Checkbox("Show Ground Plane", &m_showGroundPlane))
  123. {
  124. if (m_showGroundPlane)
  125. {
  126. CreateGroundPlane();
  127. UpdateGroundPlane();
  128. }
  129. else
  130. {
  131. RemoveGroundPlane();
  132. }
  133. }
  134. if (ScriptableImGui::Checkbox("Show Model Materials", &m_showModelMaterials))
  135. {
  136. modelNeedsUpdate = true;
  137. m_materialBrowser.SetNeedsRefresh();
  138. }
  139. assetBrowserSettings.m_labels.m_root = "Materials";
  140. modelNeedsUpdate |= m_materialBrowser.Tick(assetBrowserSettings);
  141. ImGui::Spacing();
  142. ImGui::Separator();
  143. ImGui::Spacing();
  144. assetBrowserSettings.m_labels.m_root = "Models";
  145. bool modelChanged = m_modelBrowser.Tick(assetBrowserSettings);
  146. modelNeedsUpdate |= modelChanged;
  147. if (modelChanged)
  148. {
  149. // Reset LOD override when the model changes.
  150. m_lodOverride = AZ::RPI::Cullable::NoLodOverride;
  151. }
  152. AZ::Data::Instance<AZ::RPI::Model> model = GetMeshFeatureProcessor()->GetModel(m_meshHandle);
  153. if (model)
  154. {
  155. const char* NoLodOverrideText = "No LOD Override";
  156. const char* LodFormatString = "LOD %i";
  157. AZStd::string previewText = m_lodOverride == AZ::RPI::Cullable::NoLodOverride ? NoLodOverrideText : AZStd::string::format(LodFormatString, m_lodOverride);
  158. if (ScriptableImGui::BeginCombo("", previewText.c_str()))
  159. {
  160. if (ScriptableImGui::Selectable(NoLodOverrideText, m_lodOverride == AZ::RPI::Cullable::NoLodOverride))
  161. {
  162. m_lodOverride = AZ::RPI::Cullable::NoLodOverride;
  163. GetMeshFeatureProcessor()->SetLodOverride(m_meshHandle, m_lodOverride);
  164. }
  165. for (uint32_t i = 0; i < model->GetLodCount(); ++i)
  166. {
  167. AZStd::string name = AZStd::string::format(LodFormatString, i);
  168. if (ScriptableImGui::Selectable(name.c_str(), m_lodOverride == i))
  169. {
  170. m_lodOverride = i;
  171. GetMeshFeatureProcessor()->SetLodOverride(m_meshHandle, m_lodOverride);
  172. }
  173. }
  174. ScriptableImGui::EndCombo();
  175. }
  176. }
  177. ImGui::Spacing();
  178. ImGui::Separator();
  179. ImGui::Spacing();
  180. // Camera controls
  181. {
  182. int32_t* currentControllerTypeIndex = reinterpret_cast<int32_t*>(&m_currentCameraControllerType);
  183. ImGui::LabelText("##CameraControllerLabel", "Camera Controller:");
  184. if (ScriptableImGui::Combo("##CameraController", currentControllerTypeIndex, CameraControllerNameTable, CameraControllerCount))
  185. {
  186. ResetCameraController();
  187. }
  188. }
  189. ImGui::Spacing();
  190. ImGui::Separator();
  191. ImGui::Spacing();
  192. if (m_materialOverrideInstance && ImGui::Button("Material Details..."))
  193. {
  194. m_imguiMaterialDetails.SetMaterial(m_materialOverrideInstance);
  195. m_imguiMaterialDetails.OpenDialog();
  196. }
  197. m_imguiSidebar.End();
  198. }
  199. m_imguiMaterialDetails.Tick();
  200. if (modelNeedsUpdate)
  201. {
  202. ModelChange();
  203. }
  204. }
  205. void MeshExampleComponent::ModelChange()
  206. {
  207. if (!m_modelBrowser.GetSelectedAssetId().IsValid())
  208. {
  209. m_modelAsset = {};
  210. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  211. return;
  212. }
  213. // If a material hasn't been selected, just choose the first one
  214. // If for some reason no materials are available log an error
  215. AZ::Data::AssetId selectedMaterialAssetId = m_materialBrowser.GetSelectedAssetId();
  216. if (!selectedMaterialAssetId.IsValid())
  217. {
  218. selectedMaterialAssetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(DefaultPbrMaterialPath, AZ::RPI::AssetUtils::TraceLevel::Error);
  219. if (!selectedMaterialAssetId.IsValid())
  220. {
  221. AZ_Error("MeshExampleComponent", false, "Failed to select model, no material available to render with.");
  222. return;
  223. }
  224. }
  225. if (m_enableMaterialOverride && selectedMaterialAssetId.IsValid())
  226. {
  227. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset;
  228. materialAsset.Create(selectedMaterialAssetId);
  229. m_materialOverrideInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  230. }
  231. else
  232. {
  233. m_materialOverrideInstance = nullptr;
  234. }
  235. if (m_modelAsset.GetId() != m_modelBrowser.GetSelectedAssetId())
  236. {
  237. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  238. m_modelAsset.Create(m_modelBrowser.GetSelectedAssetId());
  239. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  240. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(m_modelAsset, m_materialOverrideInstance);
  241. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  242. GetMeshFeatureProcessor()->ConnectModelChangeEventHandler(m_meshHandle, m_changedHandler);
  243. GetMeshFeatureProcessor()->SetLodOverride(m_meshHandle, m_lodOverride);
  244. }
  245. else
  246. {
  247. GetMeshFeatureProcessor()->SetMaterialAssignmentMap(m_meshHandle, m_materialOverrideInstance);
  248. }
  249. }
  250. void MeshExampleComponent::CreateGroundPlane()
  251. {
  252. m_groundPlandMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(m_groundPlaneModelAsset, m_groundPlaneMaterial);
  253. }
  254. void MeshExampleComponent::UpdateGroundPlane()
  255. {
  256. if (m_groundPlandMeshHandle.IsValid())
  257. {
  258. AZ::Transform groundPlaneTransform = AZ::Transform::CreateIdentity();
  259. AZ::Vector3 modelCenter;
  260. float modelRadius;
  261. m_modelAsset->GetAabb().GetAsSphere(modelCenter, modelRadius);
  262. static const float GroundPlaneRelativeScale = 4.0f;
  263. static const float GroundPlaneOffset = 0.01f;
  264. groundPlaneTransform.SetScale(AZ::Vector3(GroundPlaneRelativeScale * modelRadius));
  265. groundPlaneTransform.SetTranslation(AZ::Vector3(0.0f, 0.0f, m_modelAsset->GetAabb().GetMin().GetZ() - GroundPlaneOffset));
  266. GetMeshFeatureProcessor()->SetTransform(m_groundPlandMeshHandle, groundPlaneTransform);
  267. }
  268. }
  269. void MeshExampleComponent::RemoveGroundPlane()
  270. {
  271. GetMeshFeatureProcessor()->ReleaseMesh(m_groundPlandMeshHandle);
  272. }
  273. void MeshExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  274. {
  275. OnLightingPresetEntityShutdown(entityId);
  276. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  277. }
  278. void MeshExampleComponent::UseArcBallCameraController()
  279. {
  280. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  281. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  282. }
  283. void MeshExampleComponent::UseNoClipCameraController()
  284. {
  285. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  286. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  287. }
  288. void MeshExampleComponent::RemoveController()
  289. {
  290. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  291. }
  292. void MeshExampleComponent::SetArcBallControllerParams()
  293. {
  294. if (!m_modelBrowser.GetSelectedAssetId().IsValid() || !m_modelAsset.IsReady())
  295. {
  296. return;
  297. }
  298. // Adjust the arc-ball controller so that it has bounds that make sense for the current model
  299. AZ::Vector3 center;
  300. float radius;
  301. m_modelAsset->GetAabb().GetAsSphere(center, radius);
  302. const float startingDistance = radius * ArcballRadiusDefaultModifier;
  303. const float minDistance = radius * ArcballRadiusMinModifier;
  304. const float maxDistance = radius * ArcballRadiusMaxModifier;
  305. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  306. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, startingDistance);
  307. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMinDistance, minDistance);
  308. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetMaxDistance, maxDistance);
  309. }
  310. void MeshExampleComponent::ResetCameraController()
  311. {
  312. RemoveController();
  313. if (m_currentCameraControllerType == CameraControllerType::ArcBall)
  314. {
  315. UseArcBallCameraController();
  316. SetArcBallControllerParams();
  317. }
  318. else if (m_currentCameraControllerType == CameraControllerType::NoClip)
  319. {
  320. UseNoClipCameraController();
  321. }
  322. }
  323. } // namespace AtomSampleViewer