CubeMapCaptureComponentController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <CubeMapCapture/CubeMapCaptureComponentController.h>
  9. #include <CubeMapCapture/CubeMapCaptureComponentConstants.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. void CubeMapCaptureComponentConfig::Reflect(ReflectContext* context)
  17. {
  18. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  19. {
  20. serializeContext->Class<CubeMapCaptureComponentConfig>()
  21. ->Version(1)
  22. ->Field("CaptureType", &CubeMapCaptureComponentConfig::m_captureType)
  23. ->Field("SpecularQualityLevel", &CubeMapCaptureComponentConfig::m_specularQualityLevel)
  24. ->Field("RelativePath", &CubeMapCaptureComponentConfig::m_relativePath)
  25. ->Field("Exposure", &CubeMapCaptureComponentConfig::m_exposure);
  26. }
  27. }
  28. AZ::u32 CubeMapCaptureComponentConfig::OnCaptureTypeChanged()
  29. {
  30. m_relativePath.clear();
  31. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  32. }
  33. AZ::u32 CubeMapCaptureComponentConfig::GetSpecularQualityVisibilitySetting() const
  34. {
  35. // the specular quality level UI control is only visible when the capture type is Specular
  36. return m_captureType == CubeMapCaptureType::Specular ? AZ::Edit::PropertyVisibility::Show : AZ::Edit::PropertyVisibility::Hide;
  37. }
  38. AZ::u32 CubeMapCaptureComponentConfig::OnSpecularQualityChanged()
  39. {
  40. m_relativePath.clear();
  41. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  42. }
  43. void CubeMapCaptureComponentController::Reflect(ReflectContext* context)
  44. {
  45. CubeMapCaptureComponentConfig::Reflect(context);
  46. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  47. {
  48. serializeContext->Class<CubeMapCaptureComponentController>()
  49. ->Version(0)
  50. ->Field("Configuration", &CubeMapCaptureComponentController::m_configuration);
  51. }
  52. }
  53. void CubeMapCaptureComponentController::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  54. {
  55. dependent.push_back(AZ_CRC_CE("TransformService"));
  56. }
  57. void CubeMapCaptureComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  58. {
  59. provided.push_back(AZ_CRC_CE("CubeMapCaptureService"));
  60. }
  61. CubeMapCaptureComponentController::CubeMapCaptureComponentController(const CubeMapCaptureComponentConfig& config)
  62. : m_configuration(config)
  63. {
  64. }
  65. void CubeMapCaptureComponentController::Activate(AZ::EntityId entityId)
  66. {
  67. m_entityId = entityId;
  68. TransformNotificationBus::Handler::BusConnect(m_entityId);
  69. m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<CubeMapCaptureFeatureProcessorInterface>(entityId);
  70. AZ_Assert(m_featureProcessor, "CubeMapCaptureComponentController was unable to find a CubeMapCaptureFeatureProcessor on the EntityContext provided.");
  71. m_transformInterface = TransformBus::FindFirstHandler(entityId);
  72. AZ_Warning("CubeMapCaptureComponentController", m_transformInterface, "Unable to attach to a TransformBus handler.");
  73. // special handling is required if this component is being cloned in the editor:
  74. // check to see if it is already referenced by another CubeMapCapture component
  75. if (!m_configuration.m_relativePath.empty() && m_featureProcessor->IsCubeMapReferenced(m_configuration.m_relativePath))
  76. {
  77. // clear the cubeMapRelativePath to prevent the newly cloned CubeMapCapture
  78. // from using the same cubemap path as the original CubeMapCapture
  79. m_configuration.m_relativePath.clear();
  80. }
  81. // add this CubeMapCapture to the feature processor
  82. const AZ::Transform& transform = m_transformInterface->GetWorldTM();
  83. m_handle = m_featureProcessor->AddCubeMapCapture(transform);
  84. m_featureProcessor->SetRelativePath(m_handle, m_configuration.m_relativePath);
  85. }
  86. void CubeMapCaptureComponentController::Deactivate()
  87. {
  88. if (m_featureProcessor)
  89. {
  90. m_featureProcessor->RemoveCubeMapCapture(m_handle);
  91. m_handle = nullptr;
  92. }
  93. TransformNotificationBus::Handler::BusDisconnect();
  94. m_transformInterface = nullptr;
  95. m_featureProcessor = nullptr;
  96. }
  97. void CubeMapCaptureComponentController::SetConfiguration(const CubeMapCaptureComponentConfig& config)
  98. {
  99. m_configuration = config;
  100. }
  101. const CubeMapCaptureComponentConfig& CubeMapCaptureComponentController::GetConfiguration() const
  102. {
  103. return m_configuration;
  104. }
  105. void CubeMapCaptureComponentController::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  106. {
  107. if (!m_featureProcessor)
  108. {
  109. return;
  110. }
  111. m_featureProcessor->SetTransform(m_handle, world);
  112. }
  113. void CubeMapCaptureComponentController::SetExposure(float exposure)
  114. {
  115. if (!m_featureProcessor)
  116. {
  117. return;
  118. }
  119. m_featureProcessor->SetExposure(m_handle, exposure);
  120. }
  121. void CubeMapCaptureComponentController::RenderCubeMap(RenderCubeMapCallback callback, const AZStd::string& relativePath)
  122. {
  123. if (!m_featureProcessor)
  124. {
  125. return;
  126. }
  127. m_featureProcessor->RenderCubeMap(m_handle, callback, relativePath);
  128. }
  129. } // namespace Render
  130. } // namespace AZ