SpawnerComponent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/TransformBus.h>
  12. #include <AzCore/Math/Aabb.h>
  13. #include <AzCore/Math/Color.h>
  14. #include <AzCore/Math/Transform.h>
  15. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  16. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  17. #include <Vegetation/DescriptorListAsset.h>
  18. #include <Vegetation/Ebuses/DescriptorSelectorRequestBus.h>
  19. #include <Vegetation/Ebuses/FilterRequestBus.h>
  20. #include <Vegetation/Ebuses/ModifierRequestBus.h>
  21. #include <Vegetation/Ebuses/InstanceSystemRequestBus.h>
  22. #include <Vegetation/Ebuses/AreaNotificationBus.h>
  23. #include <Vegetation/Ebuses/SpawnerRequestBus.h>
  24. #include <Vegetation/AreaComponentBase.h>
  25. #include <SurfaceData/SurfaceDataTypes.h>
  26. #define VEG_SPAWNER_ENABLE_CACHING 0
  27. #define VEG_SPAWNER_ENABLE_RELATIVE 0
  28. namespace LmbrCentral
  29. {
  30. template<typename, typename>
  31. class EditorWrappedComponentBase;
  32. }
  33. namespace Vegetation
  34. {
  35. class SpawnerConfig
  36. : public AreaConfig
  37. {
  38. public:
  39. AZ_CLASS_ALLOCATOR(SpawnerConfig, AZ::SystemAllocator);
  40. AZ_RTTI(SpawnerConfig, "{98A6B0CE-FAD0-4108-B019-6B01931E649F}", AreaConfig);
  41. static void Reflect(AZ::ReflectContext* context);
  42. bool m_inheritBehavior = true;
  43. bool m_allowEmptyMeshes = true;
  44. FilterStage m_filterStage = FilterStage::PreProcess;
  45. };
  46. inline constexpr AZ::TypeId SpawnerComponentTypeId{ "{14BD176C-2E44-4BA6-849A-258674179237}" };
  47. /**
  48. * Default placement logic for vegetation in an area
  49. */
  50. class SpawnerComponent
  51. : public AreaComponentBase
  52. , private SpawnerRequestBus::Handler
  53. {
  54. public:
  55. friend class EditorSpawnerComponent;
  56. template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
  57. AZ_COMPONENT(SpawnerComponent, SpawnerComponentTypeId, AreaComponentBase);
  58. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  59. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  60. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  61. static void Reflect(AZ::ReflectContext* context);
  62. SpawnerComponent(const SpawnerConfig& configuration);
  63. SpawnerComponent() = default;
  64. ~SpawnerComponent() = default;
  65. //////////////////////////////////////////////////////////////////////////
  66. // AZ::Component interface implementation
  67. void Activate() override;
  68. void Deactivate() override;
  69. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  70. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  71. //////////////////////////////////////////////////////////////////////////
  72. // AreaRequestBus
  73. bool PrepareToClaim(EntityIdStack& stackIds) override;
  74. void ClaimPositions(EntityIdStack& stackIds, ClaimContext& context) override;
  75. void UnclaimPosition(const ClaimHandle handle) override;
  76. //////////////////////////////////////////////////////////////////////////
  77. // AreaInfoBus
  78. AZ::Aabb GetEncompassingAabb() const override;
  79. AZ::u32 GetProductCount() const override;
  80. //////////////////////////////////////////////////////////////////////////
  81. // AreaNotificationBus
  82. void OnCompositionChanged() override;
  83. protected:
  84. //////////////////////////////////////////////////////////////////////////
  85. // AreaComponentBase
  86. void OnRegisterArea() override;
  87. void OnUnregisterArea() override;
  88. //////////////////////////////////////////////////////////////////////////
  89. // SpawnerRequestBus
  90. AZ::u32 GetAreaPriority() const override;
  91. void SetAreaPriority(AZ::u32 priority) override;
  92. AZ::u32 GetAreaLayer() const override;
  93. void SetAreaLayer(AZ::u32 type) override;
  94. AZ::u32 GetAreaProductCount() const override;
  95. bool GetInheritBehavior() const override;
  96. void SetInheritBehavior(bool value) override;
  97. bool GetAllowEmptyMeshes() const override;
  98. void SetAllowEmptyMeshes(bool value) override;
  99. FilterStage GetFilterStage() const override;
  100. void SetFilterStage(FilterStage filterStage) override;
  101. private:
  102. void ClearSelectableDescriptors();
  103. bool CreateInstance(const ClaimPoint &point, InstanceData& instanceData);
  104. bool EvaluateFilters(EntityIdStack& processedIds, InstanceData& instanceData, const FilterStage intendedStage) const;
  105. bool ProcessInstance(EntityIdStack& processedIds, const ClaimPoint& point, InstanceData& instanceData, DescriptorPtr descriptorPtr);
  106. bool ClaimPosition(EntityIdStack& processedIds, const ClaimPoint& point, InstanceData& instanceData);
  107. void DestroyAllInstances();
  108. void CalcInstanceDebugColor(const EntityIdStack& processedIds);
  109. SpawnerConfig m_configuration;
  110. //caching vector for reuse per point
  111. AZStd::recursive_mutex m_selectableDescriptorMutex;
  112. DescriptorPtrVec m_selectableDescriptorCache;
  113. DescriptorPtrVec m_selectedDescriptors;
  114. using ClaimInstanceMapping = AZStd::unordered_map<ClaimHandle, InstanceId>;
  115. ClaimInstanceMapping m_claimInstanceMapping;
  116. mutable AZStd::recursive_mutex m_claimInstanceMappingMutex;
  117. #if VEG_SPAWNER_ENABLE_CACHING
  118. // cached data
  119. AZStd::unordered_map<ClaimHandle, InstanceData> m_acceptedClaimCache;
  120. AZStd::unordered_set<ClaimHandle> m_rejectedClaimCache;
  121. mutable AZStd::recursive_mutex m_cacheMutex;
  122. #endif
  123. SurfaceData::SurfaceTagVector m_inclusiveTagsToConsider;
  124. SurfaceData::SurfaceTagVector m_exclusiveTagsToConsider;
  125. };
  126. }