TerrainHeightGradientListComponent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #pragma once
  9. #include <AzCore/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzCore/Component/TransformBus.h>
  13. #include <AzCore/Jobs/JobManagerBus.h>
  14. #include <AzCore/Jobs/JobFunction.h>
  15. #include <AzCore/Math/Vector3.h>
  16. #include <AzCore/Math/Aabb.h>
  17. #include <AzCore/std/parallel/shared_mutex.h>
  18. #include <LmbrCentral/Dependency/DependencyMonitor.h>
  19. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  20. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  21. #include <AzFramework/Terrain/TerrainDataRequestBus.h>
  22. #include <TerrainSystem/TerrainSystemBus.h>
  23. namespace LmbrCentral
  24. {
  25. template<typename, typename>
  26. class EditorWrappedComponentBase;
  27. }
  28. namespace Terrain
  29. {
  30. class TerrainHeightGradientListConfig
  31. : public AZ::ComponentConfig
  32. {
  33. public:
  34. AZ_CLASS_ALLOCATOR(TerrainHeightGradientListConfig, AZ::SystemAllocator);
  35. AZ_RTTI(TerrainHeightGradientListConfig, "{C5FD71A9-0722-4D4C-B605-EBEBF90C628F}", AZ::ComponentConfig);
  36. static void Reflect(AZ::ReflectContext* context);
  37. AZStd::vector<AZ::EntityId> m_gradientEntities;
  38. };
  39. inline constexpr AZ::TypeId TerrainHeightGradientListComponentTypeId{ "{1BB3BA6C-6D4A-4636-B542-F23ECBA8F2AB}" };
  40. class TerrainHeightGradientListComponent
  41. : public AZ::Component
  42. , private Terrain::TerrainAreaHeightRequestBus::Handler
  43. , private LmbrCentral::DependencyNotificationBus::Handler
  44. , private AzFramework::Terrain::TerrainDataNotificationBus::Handler
  45. {
  46. public:
  47. template<typename, typename>
  48. friend class LmbrCentral::EditorWrappedComponentBase;
  49. AZ_COMPONENT(TerrainHeightGradientListComponent, TerrainHeightGradientListComponentTypeId);
  50. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  51. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  52. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  53. static void Reflect(AZ::ReflectContext* context);
  54. TerrainHeightGradientListComponent(const TerrainHeightGradientListConfig& configuration);
  55. TerrainHeightGradientListComponent() = default;
  56. ~TerrainHeightGradientListComponent() = default;
  57. //////////////////////////////////////////////////////////////////////////
  58. // TerrainAreaHeightRequestBus
  59. void GetHeight(const AZ::Vector3& inPosition, AZ::Vector3& outPosition, bool& terrainExists) override;
  60. void GetHeights(AZStd::span<AZ::Vector3> inOutPositionList, AZStd::span<bool> terrainExistsList) override;
  61. //////////////////////////////////////////////////////////////////////////
  62. // AZ::Component interface implementation
  63. void Activate() override;
  64. void Deactivate() override;
  65. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  66. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  67. //////////////////////////////////////////////////////////////////////////
  68. // LmbrCentral::DependencyNotificationBus
  69. void OnCompositionChanged() override;
  70. void OnCompositionRegionChanged(const AZ::Aabb& dirtyRegion) override;
  71. //////////////////////////////////////////////////////////////////////////
  72. // AzFramework::Terrain::TerrainDataNotificationBus
  73. void OnTerrainDataChanged(const AZ::Aabb& dirtyRegion, TerrainDataChangedMask dataChangedMask) override;
  74. private:
  75. TerrainHeightGradientListConfig m_configuration;
  76. AzFramework::Terrain::FloatRange m_cachedHeightBounds{ 0.0f, 0.0f };
  77. AZ::Aabb m_cachedShapeBounds;
  78. LmbrCentral::DependencyMonitor m_dependencyMonitor;
  79. // The TerrainAreaHeightRequestBus allows parallel dispatches, so make sure that queries don't happen at the same
  80. // time as cached data updates.
  81. AZStd::shared_mutex m_queryMutex;
  82. };
  83. }