DepthOfFieldExampleComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <DepthOfFieldExampleComponent.h>
  8. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  9. #include <Atom/RPI.Public/Model/Model.h>
  10. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  11. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  12. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  13. #include <AzCore/Component/Entity.h>
  14. #include <AzFramework/Components/CameraBus.h>
  15. #include <AzFramework/Components/TransformComponent.h>
  16. #include <SampleComponentManager.h>
  17. #include <SampleComponentConfig.h>
  18. #include <EntityUtilityFunctions.h>
  19. #include <RHI/BasicRHIComponent.h>
  20. namespace AtomSampleViewer
  21. {
  22. void DepthOfFieldExampleComponent::Reflect(AZ::ReflectContext* context)
  23. {
  24. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  25. {
  26. serializeContext->Class<DepthOfFieldExampleComponent, AZ::Component>()
  27. ->Version(0)
  28. ;
  29. }
  30. }
  31. void DepthOfFieldExampleComponent::Activate()
  32. {
  33. using namespace AZ;
  34. RPI::Scene* scene = RPI::RPISystemInterface::Get()->GetDefaultScene().get();
  35. m_postProcessFeatureProcessor = scene->GetFeatureProcessor<Render::PostProcessFeatureProcessorInterface>();
  36. m_directionalLightFeatureProcessor = scene->GetFeatureProcessor<Render::DirectionalLightFeatureProcessorInterface>();
  37. // Create the assets
  38. m_bunnyModelAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::ModelAsset>("objects/bunny.azmodel", RPI::AssetUtils::TraceLevel::Assert);
  39. m_materialAsset = RPI::AssetUtils::GetAssetByProductPath<RPI::MaterialAsset>("shaders/staticmesh.azmaterial", 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(
  154. GetCameraEntityId(),
  155. &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  156. }
  157. void DepthOfFieldExampleComponent::CreateMeshes()
  158. {
  159. using namespace AZ;
  160. Render::MaterialAssignmentMap materials;
  161. Render::MaterialAssignment& defaultMaterial = materials[AZ::Render::DefaultMaterialAssignmentId];
  162. defaultMaterial.m_materialAsset = m_materialAsset;
  163. defaultMaterial.m_materialInstance = RPI::Material::FindOrCreate(defaultMaterial.m_materialAsset);
  164. Vector3 translation = Vector3::CreateZero();
  165. Transform scaleTransform = Transform::CreateUniformScale(ModelScaleRatio);
  166. for (MeshHandle& meshHandle : m_meshHandles)
  167. {
  168. meshHandle = GetMeshFeatureProcessor()->AcquireMesh(Render::MeshHandleDescriptor{ m_bunnyModelAsset }, materials);
  169. auto transform = AZ::Transform::CreateTranslation(translation);
  170. transform *= scaleTransform;
  171. GetMeshFeatureProcessor()->SetTransform(meshHandle, transform);
  172. translation += Vector3(DistanceBetweenUnits, DistanceBetweenUnits, 0.0f);
  173. }
  174. }
  175. void DepthOfFieldExampleComponent::CreateLight()
  176. {
  177. using namespace AZ;
  178. Render::DirectionalLightFeatureProcessorInterface* const fp = m_directionalLightFeatureProcessor;
  179. DirectionalLightHandle handle = fp->AcquireLight();
  180. fp->SetRgbIntensity(handle, Render::PhotometricColor<Render::PhotometricUnit::Lux>(Color(5.f, 5.f, 5.f, 1.f)));
  181. fp->SetDirection(handle, Vector3(-1.f, 1.f, -1.f).GetNormalized());
  182. m_directionalLightHandle = handle;
  183. }
  184. void DepthOfFieldExampleComponent::CreateDepthOfFieldEntity()
  185. {
  186. using namespace AZ;
  187. m_depthOfFieldEntity = CreateEntity("DepthOfField", GetEntityContextId());
  188. // Transform
  189. Component* transformComponent = nullptr;
  190. ComponentDescriptorBus::EventResult(
  191. transformComponent,
  192. azrtti_typeid<AzFramework::TransformComponent>(),
  193. &ComponentDescriptorBus::Events::CreateComponent);
  194. m_depthOfFieldEntity->AddComponent(transformComponent);
  195. // DepthOfField
  196. m_postProcessSettings = m_postProcessFeatureProcessor->GetOrCreateSettingsInterface(m_depthOfFieldEntity->GetId());
  197. m_depthOfFieldSettings = m_postProcessSettings->GetOrCreateDepthOfFieldSettingsInterface();
  198. m_depthOfFieldSettings->SetQualityLevel(1);
  199. m_depthOfFieldSettings->SetApertureF(0.5f);
  200. m_depthOfFieldSettings->SetFocusDistance(FocusDefault);
  201. m_depthOfFieldSettings->SetEnableDebugColoring(false);
  202. m_depthOfFieldSettings->SetEnableAutoFocus(false);
  203. m_depthOfFieldSettings->SetAutoFocusScreenPosition(AZ::Vector2{ 0.5f, 0.5f });
  204. m_depthOfFieldSettings->SetAutoFocusSensitivity(1.0f);
  205. m_depthOfFieldSettings->SetAutoFocusSpeed(Render::DepthOfField::AutoFocusSpeedMax);
  206. m_depthOfFieldSettings->SetAutoFocusDelay(0.2f);
  207. m_depthOfFieldSettings->SetCameraEntityId(GetCameraEntityId());
  208. m_depthOfFieldSettings->SetEnabled(true);
  209. m_depthOfFieldSettings->OnConfigChanged();
  210. m_depthOfFieldEntity->Activate();
  211. AZ::EntityBus::MultiHandler::BusConnect(m_depthOfFieldEntity->GetId());
  212. }
  213. void DepthOfFieldExampleComponent::DrawSidebar()
  214. {
  215. using namespace AZ::Render;
  216. if (GetCameraEntityId() != m_depthOfFieldSettings->GetCameraEntityId())
  217. {
  218. m_depthOfFieldSettings->SetCameraEntityId(GetCameraEntityId());
  219. m_depthOfFieldSettings->OnConfigChanged();
  220. }
  221. if (!m_imguiSidebar.Begin())
  222. {
  223. return;
  224. }
  225. ImGui::Spacing();
  226. ImGui::Text("Depth of Field");
  227. ImGui::Indent();
  228. {
  229. int32_t qualityLevel = aznumeric_cast<int32_t>(m_depthOfFieldSettings->GetQualityLevel());
  230. if (ImGui::SliderInt("Quality Level", &qualityLevel, 0, DepthOfField::QualityLevelMax - 1, "%d") || !m_isInitParameters)
  231. {
  232. m_depthOfFieldSettings->SetQualityLevel(aznumeric_cast<uint32_t>(qualityLevel));
  233. m_depthOfFieldSettings->OnConfigChanged();
  234. }
  235. ImGui::Spacing();
  236. float apertureF = m_depthOfFieldSettings->GetApertureF();
  237. if (ImGui::SliderFloat("Aperture F", &apertureF, 0.0f, 1.0f, "%0.4f") || !m_isInitParameters)
  238. {
  239. m_depthOfFieldSettings->SetApertureF(apertureF);
  240. m_depthOfFieldSettings->OnConfigChanged();
  241. }
  242. constexpr float Min = DepthOfField::ApertureFMin;
  243. constexpr float Max = DepthOfField::ApertureFMax;
  244. float viewApertureF = 1.0f / Max + (1.0f / Min - 1.0f / Max) * apertureF;
  245. viewApertureF = 1.0f / viewApertureF;
  246. ImGui::Text("f / %f", viewApertureF);
  247. ImGui::Spacing();
  248. float viewNear = 1.f;
  249. float viewFar = 100.f;
  250. Camera::CameraRequestBus::EventResult(viewNear, GetCameraEntityId(), &Camera::CameraRequestBus::Events::GetNearClipDistance);
  251. Camera::CameraRequestBus::EventResult(viewFar, GetCameraEntityId(), &Camera::CameraRequestBus::Events::GetFarClipDistance);
  252. float focusDistance = m_depthOfFieldSettings->GetFocusDistance();
  253. if (ImGui::SliderFloat("Focus Distance", &focusDistance, ViewNear, ViewFar, "%0.3f") || !m_isInitParameters)
  254. {
  255. m_depthOfFieldSettings->SetFocusDistance(focusDistance);
  256. m_depthOfFieldSettings->OnConfigChanged();
  257. }
  258. ImGui::Spacing();
  259. bool dofEnabled = m_depthOfFieldSettings->GetEnabled();
  260. if (ImGui::Checkbox("Enabled Depth of Field", &dofEnabled) || !m_isInitParameters)
  261. {
  262. m_depthOfFieldSettings->SetEnabled(dofEnabled);
  263. m_depthOfFieldSettings->OnConfigChanged();
  264. }
  265. ImGui::Spacing();
  266. bool enabledDebugColoring = m_depthOfFieldSettings->GetEnableDebugColoring();
  267. if (ImGui::Checkbox("Enabled Debug Color", &enabledDebugColoring) || !m_isInitParameters)
  268. {
  269. m_depthOfFieldSettings->SetEnableDebugColoring(enabledDebugColoring);
  270. m_depthOfFieldSettings->OnConfigChanged();
  271. }
  272. ImGui::Spacing();
  273. ImGui::Spacing();
  274. ImGui::Spacing();
  275. ImGui::Text("Auto Focus");
  276. ImGui::Spacing();
  277. bool enabledAutoFocus = m_depthOfFieldSettings->GetEnableAutoFocus();
  278. if (ImGui::Checkbox("Enabled Auto Focus", &enabledAutoFocus) || !m_isInitParameters)
  279. {
  280. m_depthOfFieldSettings->SetEnableAutoFocus(enabledAutoFocus);
  281. m_depthOfFieldSettings->OnConfigChanged();
  282. }
  283. ImGui::Spacing();
  284. float screenPosition[2];
  285. AZ::Vector2 autoFocusScreenPosition = m_depthOfFieldSettings->GetAutoFocusScreenPosition();
  286. screenPosition[0] = autoFocusScreenPosition.GetX();
  287. screenPosition[1] = autoFocusScreenPosition.GetY();
  288. if (ImGui::SliderFloat2("Screen Position", screenPosition, 0.0f, 1.0f, "%0.3f") || !m_isInitParameters)
  289. {
  290. autoFocusScreenPosition.SetX(screenPosition[0]);
  291. autoFocusScreenPosition.SetY(screenPosition[1]);
  292. m_depthOfFieldSettings->SetAutoFocusScreenPosition(autoFocusScreenPosition);
  293. m_depthOfFieldSettings->OnConfigChanged();
  294. }
  295. ImGui::Spacing();
  296. float autoFocusSensitivity = m_depthOfFieldSettings->GetAutoFocusSensitivity();
  297. if (ImGui::SliderFloat("Sensitivity", &autoFocusSensitivity, 0.0f, 1.0f , "%0.3f") || !m_isInitParameters)
  298. {
  299. m_depthOfFieldSettings->SetAutoFocusSensitivity(autoFocusSensitivity);
  300. m_depthOfFieldSettings->OnConfigChanged();
  301. }
  302. ImGui::Spacing();
  303. float autoFocusSpeed = m_depthOfFieldSettings->GetAutoFocusSpeed();
  304. if (ImGui::SliderFloat("Speed", &autoFocusSpeed, 0.0f, DepthOfField::AutoFocusSpeedMax, "%0.3f") || !m_isInitParameters)
  305. {
  306. m_depthOfFieldSettings->SetAutoFocusSpeed(autoFocusSpeed);
  307. m_depthOfFieldSettings->OnConfigChanged();
  308. }
  309. ImGui::Spacing();
  310. float autoFocusDelay = m_depthOfFieldSettings->GetAutoFocusDelay();
  311. if (ImGui::SliderFloat("Delay", &autoFocusDelay, 0.0f, DepthOfField::AutoFocusDelayMax, "%0.3f") || !m_isInitParameters)
  312. {
  313. m_depthOfFieldSettings->SetAutoFocusDelay(autoFocusDelay);
  314. m_depthOfFieldSettings->OnConfigChanged();
  315. }
  316. m_isInitParameters = true;
  317. }
  318. ImGui::Unindent();
  319. ImGui::Separator();
  320. m_imguiSidebar.End();
  321. }
  322. } // namespace AtomSampleViewer