OcclusionCullingPlaneComponentController.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <OcclusionCullingPlane/OcclusionCullingPlaneComponentController.h>
  9. #include <OcclusionCullingPlane/OcclusionCullingPlaneComponentConstants.h>
  10. #include <Atom/RPI.Public/Model/Model.h>
  11. #include <Atom/RPI.Public/Image/StreamingImage.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <AzCore/Asset/AssetManager.h>
  14. #include <AzCore/Asset/AssetManagerBus.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzFramework/Entity/EntityContextBus.h>
  17. #include <AzFramework/Entity/EntityContext.h>
  18. #include <AzFramework/Scene/Scene.h>
  19. #include <AzCore/RTTI/BehaviorContext.h>
  20. namespace AZ
  21. {
  22. namespace Render
  23. {
  24. void OcclusionCullingPlaneComponentConfig::Reflect(ReflectContext* context)
  25. {
  26. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  27. {
  28. serializeContext->Class<OcclusionCullingPlaneComponentConfig>()
  29. ->Version(0)
  30. ->Field("ShowVisualization", &OcclusionCullingPlaneComponentConfig::m_showVisualization)
  31. ->Field("TransparentVisualization", &OcclusionCullingPlaneComponentConfig::m_transparentVisualization)
  32. ;
  33. }
  34. }
  35. void OcclusionCullingPlaneComponentController::Reflect(ReflectContext* context)
  36. {
  37. OcclusionCullingPlaneComponentConfig::Reflect(context);
  38. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  39. {
  40. serializeContext->Class<OcclusionCullingPlaneComponentController>()
  41. ->Version(0)
  42. ->Field("Configuration", &OcclusionCullingPlaneComponentController::m_configuration);
  43. }
  44. }
  45. void OcclusionCullingPlaneComponentController::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  46. {
  47. dependent.push_back(AZ_CRC_CE("TransformService"));
  48. }
  49. void OcclusionCullingPlaneComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  50. {
  51. provided.push_back(AZ_CRC_CE("OcclusionCullingPlaneService"));
  52. }
  53. void OcclusionCullingPlaneComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  54. {
  55. incompatible.push_back(AZ_CRC_CE("OcclusionCullingPlaneService"));
  56. }
  57. void OcclusionCullingPlaneComponentController::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  58. {
  59. required.push_back(AZ_CRC_CE("TransformService"));
  60. }
  61. OcclusionCullingPlaneComponentController::OcclusionCullingPlaneComponentController(const OcclusionCullingPlaneComponentConfig& config)
  62. : m_configuration(config)
  63. {
  64. }
  65. void OcclusionCullingPlaneComponentController::Activate(AZ::EntityId entityId)
  66. {
  67. AZ::ApplicationTypeQuery appType;
  68. ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::QueryApplicationType, appType);
  69. if (appType.IsHeadless())
  70. {
  71. return;
  72. }
  73. m_entityId = entityId;
  74. TransformNotificationBus::Handler::BusConnect(m_entityId);
  75. m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<OcclusionCullingPlaneFeatureProcessorInterface>(entityId);
  76. AZ_Assert(m_featureProcessor, "OcclusionCullingPlaneComponentController was unable to find a OcclusionCullingPlaneFeatureProcessor on the EntityContext provided.");
  77. m_transformInterface = TransformBus::FindFirstHandler(entityId);
  78. AZ_Assert(m_transformInterface, "Unable to attach to a TransformBus handler");
  79. if (!m_transformInterface)
  80. {
  81. return;
  82. }
  83. // add this occlusion plane to the feature processor
  84. const AZ::Transform& transform = m_transformInterface->GetWorldTM();
  85. m_handle = m_featureProcessor->AddOcclusionCullingPlane(transform);
  86. // set visualization
  87. m_featureProcessor->ShowVisualization(m_handle, m_configuration.m_showVisualization);
  88. m_featureProcessor->SetTransparentVisualization(m_handle, m_configuration.m_transparentVisualization);
  89. }
  90. void OcclusionCullingPlaneComponentController::Deactivate()
  91. {
  92. if (m_featureProcessor)
  93. {
  94. m_featureProcessor->RemoveOcclusionCullingPlane(m_handle);
  95. }
  96. Data::AssetBus::MultiHandler::BusDisconnect();
  97. TransformNotificationBus::Handler::BusDisconnect();
  98. m_transformInterface = nullptr;
  99. m_featureProcessor = nullptr;
  100. }
  101. void OcclusionCullingPlaneComponentController::SetConfiguration(const OcclusionCullingPlaneComponentConfig& config)
  102. {
  103. m_configuration = config;
  104. }
  105. const OcclusionCullingPlaneComponentConfig& OcclusionCullingPlaneComponentController::GetConfiguration() const
  106. {
  107. return m_configuration;
  108. }
  109. void OcclusionCullingPlaneComponentController::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  110. {
  111. if (!m_featureProcessor)
  112. {
  113. return;
  114. }
  115. m_featureProcessor->SetTransform(m_handle, world);
  116. }
  117. } // namespace Render
  118. } // namespace AZ