AreaDebugComponent.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <Debugger/AreaDebugComponent.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <Vegetation/Ebuses/AreaSystemRequestBus.h>
  14. #include <AzCore/Debug/Profiler.h>
  15. namespace Vegetation
  16. {
  17. namespace AreaDebugUtil
  18. {
  19. static bool UpdateVersion([[maybe_unused]] AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  20. {
  21. if (classElement.GetVersion() < 1)
  22. {
  23. classElement.RemoveElementByName(AZ_CRC("PropagateDebug", 0xb5675baa));
  24. classElement.RemoveElementByName(AZ_CRC("InheritDebug", 0xd227cd11));
  25. }
  26. return true;
  27. }
  28. }
  29. void AreaDebugConfig::Reflect(AZ::ReflectContext* context)
  30. {
  31. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  32. if (serialize)
  33. {
  34. serialize->Class<AreaDebugConfig, AZ::ComponentConfig>()
  35. ->Version(1, &AreaDebugUtil::UpdateVersion)
  36. ->Field("DebugColor", &AreaDebugConfig::m_debugColor)
  37. ->Field("CubeSize", &AreaDebugConfig::m_debugCubeSize)
  38. ->Field("HideInDebug", &AreaDebugConfig::m_hideDebug)
  39. ;
  40. AZ::EditContext* edit = serialize->GetEditContext();
  41. if (edit)
  42. {
  43. edit->Class<AreaDebugConfig>(
  44. "Vegetation Layer Debugger Config", "")
  45. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  46. ->DataElement(AZ::Edit::UIHandlers::Color, &AreaDebugConfig::m_debugColor, "Debug Visualization Color", "")
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &AreaDebugConfig::m_debugCubeSize, "Debug Visualization Cube Size", "")
  48. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  49. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  50. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &AreaDebugConfig::m_hideDebug, "Hide created instance in the Debug Visualization", "")
  51. ;
  52. }
  53. }
  54. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  55. {
  56. behaviorContext->Class<AreaDebugConfig>()
  57. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  58. ->Constructor()
  59. ->Property("DebugColor", BehaviorValueProperty(&AreaDebugConfig::m_debugColor))
  60. ->Property("DebugCubeSize", BehaviorValueProperty(&AreaDebugConfig::m_debugCubeSize))
  61. ->Property("HideInDebug", BehaviorValueProperty(&AreaDebugConfig::m_hideDebug))
  62. ;
  63. }
  64. }
  65. void AreaDebugComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  66. {
  67. services.push_back(AZ_CRC("VegetationAreaDebugService", 0x2c6f3c5c));
  68. }
  69. void AreaDebugComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  70. {
  71. services.push_back(AZ_CRC("VegetationAreaDebugService", 0x2c6f3c5c));
  72. }
  73. void AreaDebugComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  74. {
  75. }
  76. void AreaDebugComponent::Reflect(AZ::ReflectContext* context)
  77. {
  78. AreaDebugConfig::Reflect(context);
  79. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  80. if (serialize)
  81. {
  82. serialize->Class<AreaDebugComponent, AZ::Component>()
  83. ->Version(0)
  84. ->Field("Configuration", &AreaDebugComponent::m_configuration)
  85. ;
  86. }
  87. }
  88. AreaDebugComponent::AreaDebugComponent(const AreaDebugConfig& configuration)
  89. : m_configuration(configuration)
  90. {
  91. }
  92. void AreaDebugComponent::Activate()
  93. {
  94. ResetBlendedDebugDisplayData();
  95. AreaDebugBus::Handler::BusConnect(GetEntityId());
  96. }
  97. void AreaDebugComponent::Deactivate()
  98. {
  99. AreaDebugBus::Handler::BusDisconnect();
  100. }
  101. bool AreaDebugComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  102. {
  103. if (auto config = azrtti_cast<const AreaDebugConfig*>(baseConfig))
  104. {
  105. m_configuration = *config;
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool AreaDebugComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  111. {
  112. if (auto config = azrtti_cast<AreaDebugConfig*>(outBaseConfig))
  113. {
  114. *config = m_configuration;
  115. return true;
  116. }
  117. return false;
  118. }
  119. AreaDebugDisplayData AreaDebugComponent::GetBaseDebugDisplayData() const
  120. {
  121. AreaDebugDisplayData data;
  122. data.m_instanceColor = m_configuration.m_debugColor;
  123. data.m_instanceSize = m_configuration.m_debugCubeSize;
  124. data.m_instanceRender = !m_configuration.m_hideDebug;
  125. return data;
  126. }
  127. void AreaDebugComponent::ResetBlendedDebugDisplayData()
  128. {
  129. m_hasBlendedDebugDisplayData = false;
  130. m_blendedDebugDisplayData = AreaDebugDisplayData();
  131. }
  132. void AreaDebugComponent::AddBlendedDebugDisplayData(const AreaDebugDisplayData& data)
  133. {
  134. m_hasBlendedDebugDisplayData = true;
  135. //do not render if any render flag is disabled
  136. m_blendedDebugDisplayData.m_instanceRender = m_blendedDebugDisplayData.m_instanceRender && data.m_instanceRender;
  137. //performing a multiply/modulate color blend.
  138. m_blendedDebugDisplayData.m_instanceColor *= data.m_instanceColor;
  139. //setting size to the last size added
  140. m_blendedDebugDisplayData.m_instanceSize = data.m_instanceSize;
  141. }
  142. AreaDebugDisplayData AreaDebugComponent::GetBlendedDebugDisplayData() const
  143. {
  144. return m_hasBlendedDebugDisplayData ? m_blendedDebugDisplayData : GetBaseDebugDisplayData();
  145. }
  146. }