TerrainLayerSpawnerComponent.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <Components/TerrainLayerSpawnerComponent.h>
  9. #include <AzCore/Asset/AssetManagerBus.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Asset/AssetManager.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Math/Aabb.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. #include <GradientSignal/Ebuses/GradientRequestBus.h>
  18. #include <SurfaceData/SurfaceDataProviderRequestBus.h>
  19. #include <TerrainSystem/TerrainSystemBus.h>
  20. namespace Terrain
  21. {
  22. void TerrainLayerSpawnerConfig::Reflect(AZ::ReflectContext* context)
  23. {
  24. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  25. if (serialize)
  26. {
  27. serialize->Class<TerrainLayerSpawnerConfig, AZ::ComponentConfig>()
  28. ->Version(2)
  29. ->Field("Layer", &TerrainLayerSpawnerConfig::m_layer)
  30. ->Field("Priority", &TerrainLayerSpawnerConfig::m_priority)
  31. ->Field("UseGroundPlane", &TerrainLayerSpawnerConfig::m_useGroundPlane)
  32. ;
  33. AZ::EditContext* edit = serialize->GetEditContext();
  34. if (edit)
  35. {
  36. edit->Class<TerrainLayerSpawnerConfig>(
  37. "Terrain Layer Spawner Component", "Provide terrain data for a region of the world")
  38. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  39. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  40. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  41. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &TerrainLayerSpawnerConfig::m_layer, "Layer Priority", "Defines a high level order that terrain spawners are applied")
  42. ->Attribute(AZ::Edit::Attributes::EnumValues, &TerrainLayerSpawnerConfig::GetSelectableLayers)
  43. ->DataElement(AZ::Edit::UIHandlers::Slider, &TerrainLayerSpawnerConfig::m_priority, "Sub Priority", "Defines order terrain spawners are applied within a layer. Larger numbers = higher priority")
  44. ->Attribute(AZ::Edit::Attributes::Min, AreaConstants::s_priorityMin)
  45. ->Attribute(AZ::Edit::Attributes::Max, AreaConstants::s_priorityMax)
  46. ->Attribute(AZ::Edit::Attributes::SoftMin, AreaConstants::s_prioritySoftMin)
  47. ->Attribute(AZ::Edit::Attributes::SoftMax, AreaConstants::s_prioritySoftMax)
  48. ->DataElement(AZ::Edit::UIHandlers::Default, &TerrainLayerSpawnerConfig::m_useGroundPlane, "Use Ground Plane", "Determines whether or not to provide a default ground plane")
  49. ;
  50. }
  51. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  52. {
  53. behaviorContext->Class<TerrainLayerSpawnerConfig>()
  54. ->Attribute(AZ::Script::Attributes::Category, "Terrain")
  55. ->Constructor()
  56. ->Property("layer", BehaviorValueProperty(&TerrainLayerSpawnerConfig::m_layer))
  57. ->Property("priority", BehaviorValueProperty(&TerrainLayerSpawnerConfig::m_priority))
  58. ->Property("useGroundPlane", BehaviorValueProperty(&TerrainLayerSpawnerConfig::m_useGroundPlane))
  59. ->Method("GetSelectableLayers", &TerrainLayerSpawnerConfig::GetSelectableLayers);
  60. }
  61. }
  62. }
  63. AZStd::vector<AZStd::pair<AZ::u32, AZStd::string>> TerrainLayerSpawnerConfig::GetSelectableLayers() const
  64. {
  65. AZStd::vector<AZStd::pair<AZ::u32, AZStd::string>> selectableLayers;
  66. selectableLayers.push_back({ AreaConstants::s_backgroundLayer, AZStd::string("Background") });
  67. selectableLayers.push_back({ AreaConstants::s_foregroundLayer, AZStd::string("Foreground") });
  68. return selectableLayers;
  69. }
  70. void TerrainLayerSpawnerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  71. {
  72. services.push_back(AZ_CRC_CE("TerrainAreaService"));
  73. }
  74. void TerrainLayerSpawnerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  75. {
  76. services.push_back(AZ_CRC_CE("TerrainAreaService"));
  77. }
  78. void TerrainLayerSpawnerComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  79. {
  80. services.push_back(AZ_CRC_CE("AxisAlignedBoxShapeService"));
  81. }
  82. void TerrainLayerSpawnerComponent::Reflect(AZ::ReflectContext* context)
  83. {
  84. TerrainLayerSpawnerConfig::Reflect(context);
  85. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  86. if (serialize)
  87. {
  88. serialize->Class<TerrainLayerSpawnerComponent, AZ::Component>()
  89. ->Version(0)
  90. ->Field("Configuration", &TerrainLayerSpawnerComponent::m_configuration)
  91. ;
  92. }
  93. }
  94. TerrainLayerSpawnerComponent::TerrainLayerSpawnerComponent(const TerrainLayerSpawnerConfig& configuration)
  95. : m_configuration(configuration)
  96. {
  97. }
  98. void TerrainLayerSpawnerComponent::Activate()
  99. {
  100. LmbrCentral::ShapeComponentNotificationsBus::Handler::BusConnect(GetEntityId());
  101. TerrainSpawnerRequestBus::Handler::BusConnect(GetEntityId());
  102. TerrainSystemServiceRequestBus::Broadcast(&TerrainSystemServiceRequestBus::Events::RegisterArea, GetEntityId());
  103. }
  104. void TerrainLayerSpawnerComponent::Deactivate()
  105. {
  106. TerrainSystemServiceRequestBus::Broadcast(&TerrainSystemServiceRequestBus::Events::UnregisterArea, GetEntityId());
  107. TerrainSpawnerRequestBus::Handler::BusDisconnect();
  108. LmbrCentral::ShapeComponentNotificationsBus::Handler::BusDisconnect();
  109. }
  110. bool TerrainLayerSpawnerComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  111. {
  112. if (auto config = azrtti_cast<const TerrainLayerSpawnerConfig*>(baseConfig))
  113. {
  114. m_configuration = *config;
  115. return true;
  116. }
  117. return false;
  118. }
  119. bool TerrainLayerSpawnerComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  120. {
  121. if (auto config = azrtti_cast<TerrainLayerSpawnerConfig*>(outBaseConfig))
  122. {
  123. *config = m_configuration;
  124. return true;
  125. }
  126. return false;
  127. }
  128. void TerrainLayerSpawnerComponent::OnShapeChanged([[maybe_unused]] ShapeChangeReasons changeReason)
  129. {
  130. // This will notify us of both shape changes and transform changes.
  131. // It's important to use this event for transform changes instead of listening to OnTransformChanged, because we need to guarantee
  132. // the shape has received the transform change message and updated its internal state before passing it along to us.
  133. RefreshArea();
  134. }
  135. void TerrainLayerSpawnerComponent::GetPriority(uint32_t& outLayer, int32_t& outPriority)
  136. {
  137. outLayer = m_configuration.m_layer;
  138. outPriority = m_configuration.m_priority;
  139. }
  140. bool TerrainLayerSpawnerComponent::GetUseGroundPlane()
  141. {
  142. return m_configuration.m_useGroundPlane;
  143. }
  144. void TerrainLayerSpawnerComponent::RefreshArea()
  145. {
  146. using Terrain = AzFramework::Terrain::TerrainDataNotifications;
  147. // Notify the terrain system that the entire layer has changed, so both height and surface data can be affected.
  148. TerrainSystemServiceRequestBus::Broadcast(
  149. &TerrainSystemServiceRequestBus::Events::RefreshArea,
  150. GetEntityId(),
  151. Terrain::TerrainDataChangedMask::HeightData | Terrain::TerrainDataChangedMask::SurfaceData);
  152. }
  153. }