DepthOfFieldExampleComponent.cpp 15 KB

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