MeshBlockerComponent.h 6.4 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/NonUniformScaleBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <Vegetation/Ebuses/AreaRequestBus.h>
  14. #include <Vegetation/Ebuses/MeshBlockerRequestBus.h>
  15. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  16. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  17. #include <SurfaceData/SurfaceDataSystemNotificationBus.h>
  18. #include <AzCore/Component/TransformBus.h>
  19. #include <AzCore/std/containers/unordered_map.h>
  20. #include <Vegetation/Ebuses/AreaNotificationBus.h>
  21. #include <Vegetation/AreaComponentBase.h>
  22. namespace LmbrCentral
  23. {
  24. template<typename, typename>
  25. class EditorWrappedComponentBase;
  26. }
  27. namespace Vegetation
  28. {
  29. class MeshBlockerConfig
  30. : public AreaConfig
  31. {
  32. public:
  33. AZ_CLASS_ALLOCATOR(MeshBlockerConfig, AZ::SystemAllocator);
  34. AZ_RTTI(MeshBlockerConfig, "{1D00F234-8134-4A42-A357-ADAC865CF63A}", AreaConfig);
  35. static void Reflect(AZ::ReflectContext* context);
  36. MeshBlockerConfig() : AreaConfig() { m_priority = AreaConstants::s_priorityMax; m_layer = AreaConstants::s_foregroundLayer; }
  37. bool m_inheritBehavior = true;
  38. float m_meshHeightPercentMin = 0.0f;
  39. float m_meshHeightPercentMax = 1.0f;
  40. bool m_blockWhenInvisible = true;
  41. };
  42. inline constexpr AZ::TypeId MeshBlockerComponentTypeId{ "{06A1ABB3-F2CD-47FC-BDE3-A13E37F3D760}" };
  43. class MeshBlockerComponent
  44. : public AreaComponentBase
  45. , private AZ::Render::MeshComponentNotificationBus::Handler
  46. , private AZ::TickBus::Handler
  47. , private MeshBlockerRequestBus::Handler
  48. , private SurfaceData::SurfaceDataSystemNotificationBus::Handler
  49. {
  50. public:
  51. friend class EditorMeshBlockerComponent;
  52. template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
  53. AZ_COMPONENT(MeshBlockerComponent, MeshBlockerComponentTypeId, AreaComponentBase);
  54. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  55. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  56. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  57. static void Reflect(AZ::ReflectContext* context);
  58. MeshBlockerComponent(const MeshBlockerConfig& configuration);
  59. MeshBlockerComponent();
  60. ~MeshBlockerComponent() = default;
  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. // AreaRequestBus
  69. bool PrepareToClaim(EntityIdStack& stackIds) override;
  70. void ClaimPositions(EntityIdStack& stackIds, ClaimContext& context) override;
  71. void UnclaimPosition(const ClaimHandle handle) override;
  72. //////////////////////////////////////////////////////////////////////////
  73. // AreaInfoBus
  74. AZ::Aabb GetEncompassingAabb() const override;
  75. AZ::u32 GetProductCount() const override;
  76. //////////////////////////////////////////////////////////////////////////
  77. // DependencyNotificationBus
  78. void OnCompositionChanged() override;
  79. //////////////////////////////////////////////////////////////////////////
  80. // MeshComponentNotificationBus
  81. void OnModelReady(const AZ::Data::Asset<AZ::RPI::ModelAsset>& modelAsset, const AZ::Data::Instance<AZ::RPI::Model>& model) override;
  82. //////////////////////////////////////////////////////////////////////////
  83. // SurfaceData::SurfaceDataSystemNotificationBus
  84. void OnSurfaceChanged(
  85. const AZ::EntityId& entityId,
  86. const AZ::Aabb& oldBounds,
  87. const AZ::Aabb& newBounds,
  88. const SurfaceData::SurfaceTagSet& changedSurfaceTags) override;
  89. ////////////////////////////////////////////////////////////////////////
  90. // AZ::TickBus
  91. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  92. protected:
  93. ////////////////////////////////////////////////////////////////////////
  94. // MeshBlockerRequestBus
  95. AZ::u32 GetAreaPriority() const override;
  96. void SetAreaPriority(AZ::u32 priority) override;
  97. AZ::u32 GetAreaLayer() const override;
  98. void SetAreaLayer(AZ::u32 type) override;
  99. AZ::u32 GetAreaProductCount() const override;
  100. bool GetInheritBehavior() const override;
  101. void SetInheritBehavior(bool value) override;
  102. float GetMeshHeightPercentMin() const override;
  103. void SetMeshHeightPercentMin(float meshHeightPercentMin) override;
  104. float GetMeshHeightPercentMax() const override;
  105. void SetMeshHeightPercentMax(float meshHeightPercentMax) override;
  106. bool GetBlockWhenInvisible() const override;
  107. void SetBlockWhenInvisible(bool value) override;
  108. private:
  109. bool ClaimPosition(EntityIdStack& processedIds, const ClaimPoint& point, InstanceData& instanceData);
  110. void UpdateMeshData();
  111. MeshBlockerConfig m_configuration;
  112. AZStd::atomic_bool m_refresh{ false };
  113. AZ::NonUniformScaleChangedEvent::Handler m_nonUniformScaleChangedHandler; ///< Responds to changes in non-uniform scale.
  114. // cached data
  115. mutable AZStd::recursive_mutex m_cacheMutex;
  116. AZ::Data::Asset<AZ::Data::AssetData> m_meshAssetData;
  117. AZ::Transform m_meshWorldTM = AZ::Transform::CreateIdentity();
  118. AZ::Transform m_meshWorldTMInverse = AZ::Transform::CreateIdentity();
  119. AZ::Aabb m_meshBounds = AZ::Aabb::CreateNull();
  120. AZ::Aabb m_meshBoundsForIntersection = AZ::Aabb::CreateNull();
  121. AZ::Vector3 m_meshNonUniformScale = AZ::Vector3::CreateOne();
  122. bool m_meshVisible = false;
  123. using CachedRayHits = AZStd::unordered_map<ClaimHandle, bool>;
  124. CachedRayHits m_cachedRayHits;
  125. static constexpr float s_rayAABBHeightPadding = 0.1f;
  126. };
  127. }