GridComponentController.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <Grid/GridComponentController.h>
  9. #include <AtomLyIntegration/CommonFeatures/Grid/GridComponentConstants.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <Atom/Utils/Utils.h>
  14. #include <Atom/RPI.Public/Scene.h>
  15. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.h>
  16. #include <Atom/RPI.Public/AuxGeom/AuxGeomDraw.h>
  17. namespace AZ
  18. {
  19. namespace Render
  20. {
  21. void GridComponentController::Reflect(ReflectContext* context)
  22. {
  23. GridComponentConfig::Reflect(context);
  24. if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
  25. {
  26. serializeContext->Class<GridComponentController>()
  27. ->Version(0)
  28. ->Field("Configuration", &GridComponentController::m_configuration)
  29. ;
  30. }
  31. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  32. {
  33. behaviorContext->EBus<GridComponentRequestBus>("GridComponentRequestBus")
  34. ->Event("GetSize", &GridComponentRequestBus::Events::GetSize)
  35. ->Event("SetSize", &GridComponentRequestBus::Events::SetSize)
  36. ->Event("GetAxisColor", &GridComponentRequestBus::Events::GetAxisColor)
  37. ->Event("SetAxisColor", &GridComponentRequestBus::Events::SetAxisColor)
  38. ->Event("GetPrimaryColor", &GridComponentRequestBus::Events::GetPrimaryColor)
  39. ->Event("SetPrimaryColor", &GridComponentRequestBus::Events::SetPrimaryColor)
  40. ->Event("GetPrimarySpacing", &GridComponentRequestBus::Events::GetPrimarySpacing)
  41. ->Event("SetPrimarySpacing", &GridComponentRequestBus::Events::SetPrimarySpacing)
  42. ->Event("GetSecondaryColor", &GridComponentRequestBus::Events::GetSecondaryColor)
  43. ->Event("SetSecondaryColor", &GridComponentRequestBus::Events::SetSecondaryColor)
  44. ->Event("GetSecondarySpacing", &GridComponentRequestBus::Events::GetSecondarySpacing)
  45. ->Event("SetSecondarySpacing", &GridComponentRequestBus::Events::SetSecondarySpacing)
  46. ->VirtualProperty("Size", "GetSize", "SetSize")
  47. ->VirtualProperty("AxisColor", "GetAxisColor", "SetAxisColor")
  48. ->VirtualProperty("PrimaryColor", "GetPrimaryColor", "SetPrimaryColor")
  49. ->VirtualProperty("PrimarySpacing", "GetPrimarySpacing", "SetPrimarySpacing")
  50. ->VirtualProperty("SecondaryColor", "GetSecondaryColor", "SetSecondaryColor")
  51. ->VirtualProperty("SecondarySpacing", "GetSecondarySpacing", "SetSecondarySpacing")
  52. ;
  53. }
  54. }
  55. void GridComponentController::GetProvidedServices(ComponentDescriptor::DependencyArrayType& provided)
  56. {
  57. provided.push_back(AZ_CRC("GridService", 0x3844bbe0));
  58. }
  59. void GridComponentController::GetIncompatibleServices(ComponentDescriptor::DependencyArrayType& incompatible)
  60. {
  61. incompatible.push_back(AZ_CRC("GridService", 0x3844bbe0));
  62. }
  63. GridComponentController::GridComponentController(const GridComponentConfig& config)
  64. : m_configuration(config)
  65. {
  66. }
  67. void GridComponentController::Activate(EntityId entityId)
  68. {
  69. m_entityId = entityId;
  70. m_dirty = true;
  71. RPI::ScenePtr scene = RPI::RPISystemInterface::Get()->GetDefaultScene();
  72. if (scene)
  73. {
  74. AZ::RPI::SceneNotificationBus::Handler::BusConnect(scene->GetId());
  75. }
  76. GridComponentRequestBus::Handler::BusConnect(m_entityId);
  77. AZ::TransformNotificationBus::Handler::BusConnect(m_entityId);
  78. }
  79. void GridComponentController::Deactivate()
  80. {
  81. AZ::TransformNotificationBus::Handler::BusDisconnect();
  82. GridComponentRequestBus::Handler::BusDisconnect();
  83. AZ::RPI::SceneNotificationBus::Handler::BusDisconnect();
  84. m_entityId = EntityId(EntityId::InvalidEntityId);
  85. }
  86. void GridComponentController::SetConfiguration(const GridComponentConfig& config)
  87. {
  88. m_configuration = config;
  89. m_dirty = true;
  90. }
  91. const GridComponentConfig& GridComponentController::GetConfiguration() const
  92. {
  93. return m_configuration;
  94. }
  95. void GridComponentController::SetSize(float gridSize)
  96. {
  97. m_configuration.m_gridSize = AZStd::clamp(gridSize, MinGridSize, MaxGridSize);
  98. m_dirty = true;
  99. }
  100. float GridComponentController::GetSize() const
  101. {
  102. return m_configuration.m_gridSize;
  103. }
  104. void GridComponentController::SetPrimarySpacing(float gridPrimarySpacing)
  105. {
  106. m_configuration.m_primarySpacing = AZStd::max(gridPrimarySpacing, MinSpacing);
  107. m_dirty = true;
  108. }
  109. float GridComponentController::GetPrimarySpacing() const
  110. {
  111. return m_configuration.m_primarySpacing;
  112. }
  113. void GridComponentController::SetSecondarySpacing(float gridSecondarySpacing)
  114. {
  115. m_configuration.m_secondarySpacing = AZStd::max(gridSecondarySpacing, MinSpacing);
  116. m_dirty = true;
  117. }
  118. float GridComponentController::GetSecondarySpacing() const
  119. {
  120. return m_configuration.m_secondarySpacing;
  121. }
  122. void GridComponentController::SetAxisColor(const AZ::Color& gridAxisColor)
  123. {
  124. m_configuration.m_axisColor = gridAxisColor;
  125. }
  126. AZ::Color GridComponentController::GetAxisColor() const
  127. {
  128. return m_configuration.m_axisColor;
  129. }
  130. void GridComponentController::SetPrimaryColor(const AZ::Color& gridPrimaryColor)
  131. {
  132. m_configuration.m_primaryColor = gridPrimaryColor;
  133. }
  134. AZ::Color GridComponentController::GetPrimaryColor() const
  135. {
  136. return m_configuration.m_primaryColor;
  137. }
  138. void GridComponentController::SetSecondaryColor(const AZ::Color& gridSecondaryColor)
  139. {
  140. m_configuration.m_secondaryColor = gridSecondaryColor;
  141. }
  142. AZ::Color GridComponentController::GetSecondaryColor() const
  143. {
  144. return m_configuration.m_secondaryColor;
  145. }
  146. void GridComponentController::OnBeginPrepareRender()
  147. {
  148. auto* auxGeomFP = AZ::RPI::Scene::GetFeatureProcessorForEntity<AZ::RPI::AuxGeomFeatureProcessorInterface>(m_entityId);
  149. if (!auxGeomFP)
  150. {
  151. return;
  152. }
  153. if (auto auxGeom = auxGeomFP->GetDrawQueue())
  154. {
  155. BuildGrid();
  156. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  157. drawArgs.m_verts = m_secondaryGridPoints.data();
  158. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_secondaryGridPoints.size());
  159. drawArgs.m_colors = &m_configuration.m_secondaryColor;
  160. drawArgs.m_colorCount = 1;
  161. auxGeom->DrawLines(drawArgs);
  162. drawArgs.m_verts = m_primaryGridPoints.data();
  163. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_primaryGridPoints.size());
  164. drawArgs.m_colors = &m_configuration.m_primaryColor;
  165. auxGeom->DrawLines(drawArgs);
  166. drawArgs.m_verts = m_axisGridPoints.data();
  167. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_axisGridPoints.size());
  168. drawArgs.m_colors = &m_configuration.m_axisColor;
  169. auxGeom->DrawLines(drawArgs);
  170. }
  171. }
  172. void GridComponentController::OnTransformChanged(const Transform& local, const Transform& world)
  173. {
  174. AZ_UNUSED(local);
  175. AZ_UNUSED(world);
  176. m_dirty = true;
  177. }
  178. void GridComponentController::BuildGrid()
  179. {
  180. if (m_dirty)
  181. {
  182. m_dirty = false;
  183. AZ::Transform transform;
  184. AZ::TransformBus::EventResult(transform, m_entityId, &AZ::TransformBus::Events::GetWorldTM);
  185. const float halfLength = m_configuration.m_gridSize / 2.0f;
  186. m_axisGridPoints.clear();
  187. m_axisGridPoints.reserve(4);
  188. m_axisGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-halfLength, 0, 0.0f)));
  189. m_axisGridPoints.push_back(transform.TransformPoint(AZ::Vector3(halfLength, 0, 0.0f)));
  190. m_axisGridPoints.push_back(transform.TransformPoint(AZ::Vector3(0, -halfLength, 0.0f)));
  191. m_axisGridPoints.push_back(transform.TransformPoint(AZ::Vector3(0, halfLength, 0.0f)));
  192. m_primaryGridPoints.clear();
  193. m_primaryGridPoints.reserve(aznumeric_cast<size_t>(4.0f * m_configuration.m_gridSize / m_configuration.m_primarySpacing));
  194. for (float position = m_configuration.m_primarySpacing; position <= halfLength; position += m_configuration.m_primarySpacing)
  195. {
  196. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-halfLength, -position, 0.0f)));
  197. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(halfLength, -position, 0.0f)));
  198. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-halfLength, position, 0.0f)));
  199. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(halfLength, position, 0.0f)));
  200. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-position, -halfLength, 0.0f)));
  201. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-position, halfLength, 0.0f)));
  202. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(position, -halfLength, 0.0f)));
  203. m_primaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(position, halfLength, 0.0f)));
  204. }
  205. m_secondaryGridPoints.clear();
  206. m_secondaryGridPoints.reserve(aznumeric_cast<size_t>(4.0f * m_configuration.m_gridSize / m_configuration.m_secondarySpacing));
  207. for (float position = m_configuration.m_secondarySpacing; position <= halfLength; position += m_configuration.m_secondarySpacing)
  208. {
  209. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-halfLength, -position, 0.0f)));
  210. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(halfLength, -position, 0.0f)));
  211. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-halfLength, position, 0.0f)));
  212. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(halfLength, position, 0.0f)));
  213. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-position, -halfLength, 0.0f)));
  214. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(-position, halfLength, 0.0f)));
  215. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(position, -halfLength, 0.0f)));
  216. m_secondaryGridPoints.push_back(transform.TransformPoint(AZ::Vector3(position, halfLength, 0.0f)));
  217. }
  218. GridComponentNotificationBus::Event(m_entityId, &GridComponentNotificationBus::Events::OnGridChanged);
  219. }
  220. }
  221. } // namespace Render
  222. } // namespace AZ