DepthOfFieldExampleComponent.cpp 16 KB

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