DepthOfFieldExampleComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <DepthOfFieldExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  10. #include <Atom/RPI.Public/Model/Model.h>
  11. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  12. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  13. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  14. #include <AzCore/Component/Entity.h>
  15. #include <AzFramework/Components/CameraBus.h>
  16. #include <AzFramework/Components/TransformComponent.h>
  17. #include <SampleComponentManager.h>
  18. #include <SampleComponentConfig.h>
  19. #include <EntityUtilityFunctions.h>
  20. #include <RHI/BasicRHIComponent.h>
  21. namespace AtomSampleViewer
  22. {
  23. void DepthOfFieldExampleComponent::Reflect(AZ::ReflectContext* context)
  24. {
  25. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  26. {
  27. serializeContext->Class<DepthOfFieldExampleComponent, AZ::Component>()
  28. ->Version(0)
  29. ;
  30. }
  31. }
  32. void DepthOfFieldExampleComponent::Activate()
  33. {
  34. using namespace AZ;
  35. m_postProcessFeatureProcessor = m_scene->GetFeatureProcessor<Render::PostProcessFeatureProcessorInterface>();
  36. m_directionalLightFeatureProcessor = m_scene->GetFeatureProcessor<Render::DirectionalLightFeatureProcessorInterface>();
  37. // Create the assets
  38. m_bunnyModelAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::ModelAsset>("objects/bunny.fbx.azmodel", RPI::AssetUtils::TraceLevel::Assert);
  39. m_materialAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::MaterialAsset>(DefaultPbrMaterialPath, RPI::AssetUtils::TraceLevel::Assert);
  40. CreateMeshes();
  41. CreateLight();
  42. CreateDepthOfFieldEntity();
  43. UseArcBallCameraController();
  44. m_imguiSidebar.Activate();
  45. AZ::TickBus::Handler::BusConnect();
  46. }
  47. void DepthOfFieldExampleComponent::Deactivate()
  48. {
  49. Camera::CameraRequestBus::Event(
  50. GetCameraEntityId(),
  51. &Camera::CameraRequestBus::Events::SetNearClipDistance,
  52. m_saveDefaultNearOnAtomSampleViewer);
  53. Camera::CameraRequestBus::Event(
  54. GetCameraEntityId(),
  55. &Camera::CameraRequestBus::Events::SetFarClipDistance,
  56. m_saveDefaultFarOnAtomSampleViewer);
  57. Camera::CameraRequestBus::Event(
  58. GetCameraEntityId(),
  59. &Camera::CameraRequestBus::Events::SetFovDegrees,
  60. m_saveDefaultFovDegreesOnAtomSampleViewer);
  61. AZ::TickBus::Handler::BusDisconnect();
  62. RemoveController();
  63. AZ::EntityBus::MultiHandler::BusDisconnect();
  64. if (m_postProcessSettings)
  65. {
  66. m_postProcessSettings->RemoveDepthOfFieldSettingsInterface();
  67. }
  68. m_postProcessSettings = nullptr;
  69. if (m_depthOfFieldEntity)
  70. {
  71. DestroyEntity(m_depthOfFieldEntity, GetEntityContextId());
  72. }
  73. if (m_directionalLightHandle.IsValid())
  74. {
  75. m_directionalLightFeatureProcessor->ReleaseLight(m_directionalLightHandle);
  76. }
  77. for (MeshHandle& meshHandle : m_meshHandles)
  78. {
  79. GetMeshFeatureProcessor()->ReleaseMesh(meshHandle);
  80. }
  81. m_imguiSidebar.Deactivate();
  82. }
  83. void DepthOfFieldExampleComponent::OnTick([[maybe_unused]] float deltaTime, AZ::ScriptTimePoint)
  84. {
  85. if (!m_isInitCamera)
  86. {
  87. const AZ::Vector3 InitTranslation = DistanceBetweenUnits * AZ::Vector3(BunnyNumber / 8, BunnyNumber / 16, 0.5f);
  88. constexpr float InitPitch = 0.0f;
  89. constexpr float InitHeading = -0.65f;
  90. AZ::Debug::ArcBallControllerRequestBus::Event(
  91. GetCameraEntityId(),
  92. &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter,
  93. InitTranslation);
  94. AZ::Debug::ArcBallControllerRequestBus::Event(
  95. GetCameraEntityId(),
  96. &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch,
  97. InitPitch);
  98. AZ::Debug::ArcBallControllerRequestBus::Event(
  99. GetCameraEntityId(),
  100. &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading,
  101. InitHeading);
  102. }
  103. m_isInitCamera = true;
  104. DrawSidebar();
  105. }
  106. void DepthOfFieldExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
  107. {
  108. AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
  109. if (m_depthOfFieldEntity && m_depthOfFieldEntity->GetId() == entityId)
  110. {
  111. m_postProcessFeatureProcessor->RemoveSettingsInterface(m_depthOfFieldEntity->GetId());
  112. m_depthOfFieldEntity = nullptr;
  113. }
  114. else
  115. {
  116. AZ_Assert(false, "unexpected entity destruction is signaled.");
  117. }
  118. }
  119. void DepthOfFieldExampleComponent::UseArcBallCameraController()
  120. {
  121. using namespace AZ;
  122. Debug::CameraControllerRequestBus::Event(
  123. GetCameraEntityId(),
  124. &Debug::CameraControllerRequestBus::Events::Enable,
  125. azrtti_typeid<Debug::ArcBallControllerComponent>());
  126. Camera::CameraRequestBus::EventResult(
  127. m_saveDefaultNearOnAtomSampleViewer,
  128. GetCameraEntityId(),
  129. &Camera::CameraRequestBus::Events::GetNearClipDistance);
  130. Camera::CameraRequestBus::EventResult(
  131. m_saveDefaultFarOnAtomSampleViewer,
  132. GetCameraEntityId(),
  133. &Camera::CameraRequestBus::Events::GetFarClipDistance);
  134. Camera::CameraRequestBus::EventResult(
  135. m_saveDefaultFovDegreesOnAtomSampleViewer,
  136. GetCameraEntityId(),
  137. &Camera::CameraRequestBus::Events::GetFovDegrees);
  138. Camera::CameraRequestBus::Event(
  139. GetCameraEntityId(),
  140. &Camera::CameraRequestBus::Events::SetNearClipDistance,
  141. ViewNear);
  142. Camera::CameraRequestBus::Event(
  143. GetCameraEntityId(),
  144. &Camera::CameraRequestBus::Events::SetFarClipDistance,
  145. ViewFar);
  146. Camera::CameraRequestBus::Event(
  147. GetCameraEntityId(),
  148. &Camera::CameraRequestBus::Events::SetFovDegrees,
  149. ViewFovDegrees);
  150. }
  151. void DepthOfFieldExampleComponent::RemoveController()
  152. {
  153. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  154. }
  155. void DepthOfFieldExampleComponent::CreateMeshes()
  156. {
  157. using namespace AZ;
  158. auto materialInstance = RPI::Material::FindOrCreate(m_materialAsset);
  159. Vector3 translation = Vector3::CreateZero();
  160. Transform scaleTransform = Transform::CreateUniformScale(ModelScaleRatio);
  161. for (MeshHandle& meshHandle : m_meshHandles)
  162. {
  163. meshHandle = GetMeshFeatureProcessor()->AcquireMesh(Render::MeshHandleDescriptor(m_bunnyModelAsset, materialInstance));
  164. auto transform = AZ::Transform::CreateTranslation(translation);
  165. transform *= scaleTransform;
  166. GetMeshFeatureProcessor()->SetTransform(meshHandle, transform);
  167. translation += Vector3(DistanceBetweenUnits, DistanceBetweenUnits, 0.0f);
  168. }
  169. }
  170. void DepthOfFieldExampleComponent::CreateLight()
  171. {
  172. using namespace AZ;
  173. Render::DirectionalLightFeatureProcessorInterface* const fp = m_directionalLightFeatureProcessor;
  174. DirectionalLightHandle handle = fp->AcquireLight();
  175. fp->SetRgbIntensity(handle, Render::PhotometricColor<Render::PhotometricUnit::Lux>(Color(5.f, 5.f, 5.f, 1.f)));
  176. fp->SetDirection(handle, Vector3(-1.f, 1.f, -1.f).GetNormalized());
  177. m_directionalLightHandle = handle;
  178. }
  179. void DepthOfFieldExampleComponent::CreateDepthOfFieldEntity()
  180. {
  181. using namespace AZ;
  182. m_depthOfFieldEntity = CreateEntity("DepthOfField", GetEntityContextId());
  183. // Transform
  184. Component* transformComponent = nullptr;
  185. ComponentDescriptorBus::EventResult(
  186. transformComponent,
  187. azrtti_typeid<AzFramework::TransformComponent>(),
  188. &ComponentDescriptorBus::Events::CreateComponent);
  189. m_depthOfFieldEntity->AddComponent(transformComponent);
  190. // DepthOfField
  191. m_postProcessSettings = m_postProcessFeatureProcessor->GetOrCreateSettingsInterface(m_depthOfFieldEntity->GetId());
  192. m_depthOfFieldSettings = m_postProcessSettings->GetOrCreateDepthOfFieldSettingsInterface();
  193. m_depthOfFieldSettings->SetQualityLevel(1);
  194. m_depthOfFieldSettings->SetApertureF(0.5f);
  195. m_depthOfFieldSettings->SetFocusDistance(FocusDefault);
  196. m_depthOfFieldSettings->SetEnableDebugColoring(false);
  197. m_depthOfFieldSettings->SetEnableAutoFocus(false);
  198. m_depthOfFieldSettings->SetAutoFocusScreenPosition(AZ::Vector2{ 0.5f, 0.5f });
  199. m_depthOfFieldSettings->SetAutoFocusSensitivity(1.0f);
  200. m_depthOfFieldSettings->SetAutoFocusSpeed(Render::DepthOfField::AutoFocusSpeedMax);
  201. m_depthOfFieldSettings->SetAutoFocusDelay(0.2f);
  202. m_depthOfFieldSettings->SetCameraEntityId(GetCameraEntityId());
  203. m_depthOfFieldSettings->SetEnabled(true);
  204. m_depthOfFieldSettings->OnConfigChanged();
  205. m_depthOfFieldEntity->Activate();
  206. AZ::EntityBus::MultiHandler::BusConnect(m_depthOfFieldEntity->GetId());
  207. }
  208. void DepthOfFieldExampleComponent::DrawSidebar()
  209. {
  210. using namespace AZ::Render;
  211. if (GetCameraEntityId() != m_depthOfFieldSettings->GetCameraEntityId())
  212. {
  213. m_depthOfFieldSettings->SetCameraEntityId(GetCameraEntityId());
  214. m_depthOfFieldSettings->OnConfigChanged();
  215. }
  216. if (!m_imguiSidebar.Begin())
  217. {
  218. return;
  219. }
  220. ImGui::Spacing();
  221. ImGui::Text("Depth of Field");
  222. ImGui::Indent();
  223. {
  224. int32_t qualityLevel = aznumeric_cast<int32_t>(m_depthOfFieldSettings->GetQualityLevel());
  225. if (ImGui::SliderInt("Quality Level", &qualityLevel, 0, DepthOfField::QualityLevelMax - 1, "%d") || !m_isInitParameters)
  226. {
  227. m_depthOfFieldSettings->SetQualityLevel(aznumeric_cast<uint32_t>(qualityLevel));
  228. m_depthOfFieldSettings->OnConfigChanged();
  229. }
  230. ImGui::Spacing();
  231. float apertureF = m_depthOfFieldSettings->GetApertureF();
  232. if (ImGui::SliderFloat("Aperture F", &apertureF, 0.0f, 1.0f, "%0.4f") || !m_isInitParameters)
  233. {
  234. m_depthOfFieldSettings->SetApertureF(apertureF);
  235. m_depthOfFieldSettings->OnConfigChanged();
  236. }
  237. constexpr float Min = DepthOfField::ApertureFMin;
  238. constexpr float Max = DepthOfField::ApertureFMax;
  239. float viewApertureF = 1.0f / Max + (1.0f / Min - 1.0f / Max) * apertureF;
  240. viewApertureF = 1.0f / viewApertureF;
  241. ImGui::Text("f / %f", viewApertureF);
  242. ImGui::Spacing();
  243. float viewNear = 1.f;
  244. float viewFar = 100.f;
  245. Camera::CameraRequestBus::EventResult(viewNear, GetCameraEntityId(), &Camera::CameraRequestBus::Events::GetNearClipDistance);
  246. Camera::CameraRequestBus::EventResult(viewFar, GetCameraEntityId(), &Camera::CameraRequestBus::Events::GetFarClipDistance);
  247. float focusDistance = m_depthOfFieldSettings->GetFocusDistance();
  248. if (ImGui::SliderFloat("Focus Distance", &focusDistance, ViewNear, ViewFar, "%0.3f") || !m_isInitParameters)
  249. {
  250. m_depthOfFieldSettings->SetFocusDistance(focusDistance);
  251. m_depthOfFieldSettings->OnConfigChanged();
  252. }
  253. ImGui::Spacing();
  254. bool dofEnabled = m_depthOfFieldSettings->GetEnabled();
  255. if (ImGui::Checkbox("Enabled Depth of Field", &dofEnabled) || !m_isInitParameters)
  256. {
  257. m_depthOfFieldSettings->SetEnabled(dofEnabled);
  258. m_depthOfFieldSettings->OnConfigChanged();
  259. }
  260. ImGui::Spacing();
  261. bool enabledDebugColoring = m_depthOfFieldSettings->GetEnableDebugColoring();
  262. if (ImGui::Checkbox("Enabled Debug Color", &enabledDebugColoring) || !m_isInitParameters)
  263. {
  264. m_depthOfFieldSettings->SetEnableDebugColoring(enabledDebugColoring);
  265. m_depthOfFieldSettings->OnConfigChanged();
  266. }
  267. ImGui::Spacing();
  268. ImGui::Spacing();
  269. ImGui::Spacing();
  270. ImGui::Text("Auto Focus");
  271. ImGui::Spacing();
  272. bool enabledAutoFocus = m_depthOfFieldSettings->GetEnableAutoFocus();
  273. if (ImGui::Checkbox("Enabled Auto Focus", &enabledAutoFocus) || !m_isInitParameters)
  274. {
  275. m_depthOfFieldSettings->SetEnableAutoFocus(enabledAutoFocus);
  276. m_depthOfFieldSettings->OnConfigChanged();
  277. }
  278. ImGui::Spacing();
  279. float screenPosition[2];
  280. AZ::Vector2 autoFocusScreenPosition = m_depthOfFieldSettings->GetAutoFocusScreenPosition();
  281. screenPosition[0] = autoFocusScreenPosition.GetX();
  282. screenPosition[1] = autoFocusScreenPosition.GetY();
  283. if (ImGui::SliderFloat2("Screen Position", screenPosition, 0.0f, 1.0f, "%0.3f") || !m_isInitParameters)
  284. {
  285. autoFocusScreenPosition.SetX(screenPosition[0]);
  286. autoFocusScreenPosition.SetY(screenPosition[1]);
  287. m_depthOfFieldSettings->SetAutoFocusScreenPosition(autoFocusScreenPosition);
  288. m_depthOfFieldSettings->OnConfigChanged();
  289. }
  290. ImGui::Spacing();
  291. float autoFocusSensitivity = m_depthOfFieldSettings->GetAutoFocusSensitivity();
  292. if (ImGui::SliderFloat("Sensitivity", &autoFocusSensitivity, 0.0f, 1.0f , "%0.3f") || !m_isInitParameters)
  293. {
  294. m_depthOfFieldSettings->SetAutoFocusSensitivity(autoFocusSensitivity);
  295. m_depthOfFieldSettings->OnConfigChanged();
  296. }
  297. ImGui::Spacing();
  298. float autoFocusSpeed = m_depthOfFieldSettings->GetAutoFocusSpeed();
  299. if (ImGui::SliderFloat("Speed", &autoFocusSpeed, 0.0f, DepthOfField::AutoFocusSpeedMax, "%0.3f") || !m_isInitParameters)
  300. {
  301. m_depthOfFieldSettings->SetAutoFocusSpeed(autoFocusSpeed);
  302. m_depthOfFieldSettings->OnConfigChanged();
  303. }
  304. ImGui::Spacing();
  305. float autoFocusDelay = m_depthOfFieldSettings->GetAutoFocusDelay();
  306. if (ImGui::SliderFloat("Delay", &autoFocusDelay, 0.0f, DepthOfField::AutoFocusDelayMax, "%0.3f") || !m_isInitParameters)
  307. {
  308. m_depthOfFieldSettings->SetAutoFocusDelay(autoFocusDelay);
  309. m_depthOfFieldSettings->OnConfigChanged();
  310. }
  311. m_isInitParameters = true;
  312. }
  313. ImGui::Unindent();
  314. ImGui::Separator();
  315. m_imguiSidebar.End();
  316. }
  317. } // namespace AtomSampleViewer