2
0

ExposureExampleComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <ExposureExampleComponent.h>
  13. #include <Atom/Component/DebugCamera/CameraControllerBus.h>
  14. #include <Atom/Component/DebugCamera/NoClipControllerBus.h>
  15. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  16. #include <Atom/RPI.Public/Model/Model.h>
  17. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  18. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  19. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  20. #include <AzCore/Component/Entity.h>
  21. #include <AzFramework/Components/CameraBus.h>
  22. #include <AzFramework/Components/TransformComponent.h>
  23. #include <SampleComponentManager.h>
  24. #include <SampleComponentConfig.h>
  25. #include <EntityUtilityFunctions.h>
  26. #include <RHI/BasicRHIComponent.h>
  27. namespace AtomSampleViewer
  28. {
  29. void ExposureExampleComponent::Reflect(AZ::ReflectContext* context)
  30. {
  31. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  32. {
  33. serializeContext->Class<ExposureExampleComponent, AZ::Component>()
  34. ->Version(0)
  35. ;
  36. }
  37. }
  38. void ExposureExampleComponent::Activate()
  39. {
  40. using namespace AZ;
  41. RPI::Scene* scene = RPI::RPISystemInterface::Get()->GetDefaultScene().get();
  42. m_postProcessFeatureProcessor = scene->GetFeatureProcessor<AZ::Render::PostProcessFeatureProcessorInterface>();
  43. m_pointLightFeatureProcessor = scene->GetFeatureProcessor<AZ::Render::PointLightFeatureProcessorInterface>();
  44. EnableCameraController();
  45. m_cameraTransformInitialized = false;
  46. SetupScene();
  47. CreateExposureEntity();
  48. m_imguiSidebar.Activate();
  49. AZ::TickBus::Handler::BusConnect();
  50. }
  51. void ExposureExampleComponent::Deactivate()
  52. {
  53. using namespace AZ;
  54. AZ::TickBus::Handler::BusDisconnect();
  55. DisableCameraController();
  56. AZ::EntityBus::MultiHandler::BusDisconnect();
  57. if (m_exposureControlSettings)
  58. {
  59. m_exposureControlSettings->SetEnabled(false);
  60. m_exposureControlSettings->OnConfigChanged();
  61. }
  62. if (m_exposureEntity)
  63. {
  64. DestroyEntity(m_exposureEntity, GetEntityContextId());
  65. m_postProcessFeatureProcessor = nullptr;
  66. }
  67. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  68. m_pointLightFeatureProcessor->ReleaseLight(m_pointLight);
  69. m_pointLightFeatureProcessor = nullptr;
  70. m_imguiSidebar.Deactivate();
  71. }
  72. void ExposureExampleComponent::OnTick([[maybe_unused]] float deltaTime, AZ::ScriptTimePoint)
  73. {
  74. DrawSidebar();
  75. }
  76. void ExposureExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  77. {
  78. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  79. if (m_exposureEntity && m_exposureEntity->GetId() == entityId)
  80. {
  81. m_postProcessFeatureProcessor->RemoveSettingsInterface(m_exposureEntity->GetId());
  82. m_exposureEntity = nullptr;
  83. }
  84. else
  85. {
  86. AZ_Assert(false, "unexpected entity destruction is signaled.");
  87. }
  88. }
  89. void ExposureExampleComponent::SetupScene()
  90. {
  91. using namespace AZ;
  92. const char* bistroPath = "Objects/Bistro/Bistro_Research_Exterior.azmodel";
  93. Data::Asset<RPI::ModelAsset> modelAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::ModelAsset>(bistroPath, RPI::AssetUtils::TraceLevel::Assert);
  94. Data::Asset<RPI::MaterialAsset> materialAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::MaterialAsset>(DefaultPbrMaterialPath, RPI::AssetUtils::TraceLevel::Assert);
  95. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(modelAsset, AZ::RPI::Material::FindOrCreate(materialAsset));
  96. // rotate the entity 180 degrees about Z (the vertical axis)
  97. // This makes it consistent with how it was positioned in the world when the world was Y-up.
  98. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, Transform::CreateRotationZ(AZ::Constants::Pi));
  99. Data::Instance<RPI::Model> model = GetMeshFeatureProcessor()->GetModel(m_meshHandle);
  100. if (model)
  101. {
  102. OnModelReady(model);
  103. }
  104. else
  105. {
  106. GetMeshFeatureProcessor()->ConnectModelChangeEventHandler(m_meshHandle, m_meshChangedHandler);
  107. }
  108. SetupLights();
  109. }
  110. void ExposureExampleComponent::SetupLights()
  111. {
  112. auto lightHandle = m_pointLightFeatureProcessor->AcquireLight();
  113. m_pointLightFeatureProcessor->SetPosition(lightHandle, AZ::Vector3(12.f, -12.6f, 4.3f));
  114. AZ::Render::PhotometricColor<AZ::Render::PhotometricUnit::Candela> color(40.0f * AZ::Color(1.f, 1.f, 1.f, 1.f));
  115. m_pointLightFeatureProcessor->SetRgbIntensity(lightHandle, color);
  116. m_pointLightFeatureProcessor->SetBulbRadius(lightHandle, 3.f);
  117. m_pointLight = lightHandle;
  118. }
  119. void ExposureExampleComponent::OnModelReady(AZ::Data::Instance<AZ::RPI::Model> model)
  120. {
  121. m_bistroExteriorAssetLoaded = true;
  122. SetInitialCameraTransform();
  123. }
  124. void ExposureExampleComponent::SetInitialCameraTransform()
  125. {
  126. using namespace AZ;
  127. if (!m_cameraTransformInitialized)
  128. {
  129. const AZ::Vector3 InitPosition = AZ::Vector3(5.f, -10.f, 1.3f);
  130. constexpr float InitPitch = 0.0f;
  131. constexpr float InitHeading = -1.85f;
  132. AZ::Transform cameraTrans = AZ::Transform::CreateIdentity();
  133. cameraTrans.SetTranslation(InitPosition);
  134. TransformBus::Event(
  135. GetCameraEntityId(),
  136. &TransformBus::Events::SetWorldTM,
  137. cameraTrans);
  138. AZ::Debug::NoClipControllerRequestBus::Event(
  139. GetCameraEntityId(),
  140. &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch,
  141. InitPitch);
  142. AZ::Debug::NoClipControllerRequestBus::Event(
  143. GetCameraEntityId(),
  144. &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading,
  145. InitHeading);
  146. m_cameraTransformInitialized = true;
  147. }
  148. }
  149. void ExposureExampleComponent::EnableCameraController()
  150. {
  151. AZ::Debug::CameraControllerRequestBus::Event(
  152. GetCameraEntityId(),
  153. &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  154. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  155. }
  156. void ExposureExampleComponent::DisableCameraController()
  157. {
  158. AZ::Debug::CameraControllerRequestBus::Event(
  159. GetCameraEntityId(),
  160. &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  161. }
  162. void ExposureExampleComponent::CreateExposureEntity()
  163. {
  164. using namespace AZ;
  165. m_exposureEntity = CreateEntity("Exposure", GetEntityContextId());
  166. // Exposure
  167. auto* exposureSettings = m_postProcessFeatureProcessor->GetOrCreateSettingsInterface(m_exposureEntity->GetId());
  168. m_exposureControlSettings = exposureSettings->GetOrCreateExposureControlSettingsInterface();
  169. m_exposureControlSettings->SetEnabled(true);
  170. m_exposureControlSettings->SetExposureControlType(Render::ExposureControl::ExposureControlType::EyeAdaptation);
  171. m_exposureEntity->Activate();
  172. AZ::EntityBus::MultiHandler::BusConnect(m_exposureEntity->GetId());
  173. }
  174. void ExposureExampleComponent::DrawSidebar()
  175. {
  176. using namespace AZ::Render;
  177. if (!m_imguiSidebar.Begin())
  178. {
  179. return;
  180. }
  181. ImGui::Spacing();
  182. ImGui::Text("Exposure");
  183. ImGui::Indent();
  184. {
  185. bool exposureEnabled = m_exposureControlSettings->GetEnabled();
  186. if (ImGui::Checkbox("Enabled Exposure", &exposureEnabled) || !m_isInitParameters)
  187. {
  188. m_exposureControlSettings->SetEnabled(exposureEnabled);
  189. m_exposureControlSettings->OnConfigChanged();
  190. }
  191. ImGui::Spacing();
  192. float manualCompensation = m_exposureControlSettings->GetManualCompensation();
  193. if (ImGui::SliderFloat("Manual Compensation", &manualCompensation, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
  194. {
  195. m_exposureControlSettings->SetManualCompensation(manualCompensation);
  196. m_exposureControlSettings->OnConfigChanged();
  197. }
  198. if (ImGui::CollapsingHeader("EyeAdaptation", ImGuiTreeNodeFlags_DefaultOpen))
  199. {
  200. ImGui::Indent();
  201. bool eyeAdaptationEnabled = m_exposureControlSettings->GetExposureControlType() == ExposureControl::ExposureControlType::EyeAdaptation;
  202. if (ImGui::Checkbox("Enabled Eye Adaptation", &eyeAdaptationEnabled) || !m_isInitParameters)
  203. {
  204. m_exposureControlSettings->SetExposureControlType(eyeAdaptationEnabled ? ExposureControl::ExposureControlType::EyeAdaptation
  205. : ExposureControl::ExposureControlType::ManualOnly);
  206. m_exposureControlSettings->OnConfigChanged();
  207. }
  208. ImGui::Spacing();
  209. float minimumExposure = m_exposureControlSettings->GetEyeAdaptationExposureMin();
  210. if (ImGui::SliderFloat("Minumum Exposure", &minimumExposure, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
  211. {
  212. m_exposureControlSettings->SetEyeAdaptationExposureMin(minimumExposure);
  213. m_exposureControlSettings->OnConfigChanged();
  214. }
  215. float maximumExposure = m_exposureControlSettings->GetEyeAdaptationExposureMax();
  216. if (ImGui::SliderFloat("Maximum Exposure", &maximumExposure, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
  217. {
  218. m_exposureControlSettings->SetEyeAdaptationExposureMax(maximumExposure);
  219. m_exposureControlSettings->OnConfigChanged();
  220. }
  221. float speedUp = m_exposureControlSettings->GetEyeAdaptationSpeedUp();
  222. if (ImGui::SliderFloat("Speed Up", &speedUp, 0.01, 10.0f, "%0.4f") || !m_isInitParameters)
  223. {
  224. m_exposureControlSettings->SetEyeAdaptationSpeedUp(speedUp);
  225. m_exposureControlSettings->OnConfigChanged();
  226. }
  227. float speedDown = m_exposureControlSettings->GetEyeAdaptationSpeedDown();
  228. if (ImGui::SliderFloat("Speed Down", &speedDown, 0.01, 10.0f, "%0.4f") || !m_isInitParameters)
  229. {
  230. m_exposureControlSettings->SetEyeAdaptationSpeedDown(speedDown);
  231. m_exposureControlSettings->OnConfigChanged();
  232. }
  233. ImGui::Unindent();
  234. }
  235. m_isInitParameters = true;
  236. }
  237. ImGui::Unindent();
  238. ImGui::Separator();
  239. m_imguiSidebar.End();
  240. }
  241. } // namespace AtomSampleViewer