From bf42e3f02a935fca3fb63ebd65b68cfeb123b593 Mon Sep 17 00:00:00 2001 From: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> Date: Fri, 20 Aug 2021 11:31:19 -0500 Subject: [PATCH] Non-terrain-gem changes in support of upcoming terrain work. (#3345) In preparation for a prototype Terrain Gem to get submitted, there are a few changes that are needed outside of the Terrain Gem as well: The TerrainDataNotificationBus lives in AzFramework/Terrain, and needed to be extended to contain an optional OnTerrainDataChanged event to notify other systems when a terrain region has changed. The HeightmapUpdateNotificationBus was removed, as this was a legacy file from the old already-removed terrain system. The EditorWrappedComponentBase<> wrapper received a small optimization to ensure that ConfigurationChanged() is only called when the value of visibility actually changes. With prefabs, it appears that sometimes OnEntityVisibilityChanged could be called multiple times in a row with the same visibility value. The TerrainSurfaceDataSystemComponent was updated to use the correct busses, and is ready to be moved to the Terrain Gem in a subsequent PR. Signed-off-by: Mike Balfour 82224783+mbalfour-amzn@users.noreply.github.com --- .../Terrain/TerrainDataRequestBus.h | 22 ++++- .../HeightmapUpdateNotificationBus.h | 34 ------- Code/Legacy/CryCommon/crycommon_files.cmake | 1 - .../Component/EditorWrappedComponentBase.inl | 7 +- .../SurfaceData/Utility/SurfaceDataUtility.h | 1 - .../Code/Source/SurfaceDataModule.cpp | 4 +- .../TerrainSurfaceDataSystemComponent.cpp | 90 +++++++++---------- .../TerrainSurfaceDataSystemComponent.h | 26 ++---- 8 files changed, 75 insertions(+), 110 deletions(-) delete mode 100644 Code/Legacy/CryCommon/HeightmapUpdateNotificationBus.h diff --git a/Code/Framework/AzFramework/AzFramework/Terrain/TerrainDataRequestBus.h b/Code/Framework/AzFramework/AzFramework/Terrain/TerrainDataRequestBus.h index 2e7cd1d170..08e238434e 100644 --- a/Code/Framework/AzFramework/AzFramework/Terrain/TerrainDataRequestBus.h +++ b/Code/Framework/AzFramework/AzFramework/Terrain/TerrainDataRequestBus.h @@ -102,11 +102,25 @@ namespace AzFramework static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; ////////////////////////////////////////////////////////////////////////// - virtual void OnTerrainDataCreateBegin() {}; - virtual void OnTerrainDataCreateEnd() {}; + enum TerrainDataChangedMask : uint8_t + { + None = 0b00000000, + Settings = 0b00000001, + HeightData = 0b00000010, + ColorData = 0b00000100, + SurfaceData = 0b00001000 + }; + + virtual void OnTerrainDataCreateBegin() {} + virtual void OnTerrainDataCreateEnd() {} - virtual void OnTerrainDataDestroyBegin() {}; - virtual void OnTerrainDataDestroyEnd() {}; + virtual void OnTerrainDataDestroyBegin() {} + virtual void OnTerrainDataDestroyEnd() {} + + virtual void OnTerrainDataChanged( + [[maybe_unused]] const AZ::Aabb& dirtyRegion, [[maybe_unused]] TerrainDataChangedMask dataChangedMask) + { + } }; using TerrainDataNotificationBus = AZ::EBus; diff --git a/Code/Legacy/CryCommon/HeightmapUpdateNotificationBus.h b/Code/Legacy/CryCommon/HeightmapUpdateNotificationBus.h deleted file mode 100644 index 618131159e..0000000000 --- a/Code/Legacy/CryCommon/HeightmapUpdateNotificationBus.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ - -#pragma once - -#include -#include - -namespace AZ -{ - /** - * the EBus is used to request information about potential vegetation surfaces - */ - class HeightmapUpdateNotification - : public AZ::EBusTraits - { - public: - //////////////////////////////////////////////////////////////////////// - // EBusTraits - static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; - static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; - //////////////////////////////////////////////////////////////////////// - - // Occurs when the terrain height map is modified. - virtual void HeightmapModified(const AZ::Aabb& bounds) = 0; - }; - - typedef AZ::EBus HeightmapUpdateNotificationBus; -} diff --git a/Code/Legacy/CryCommon/crycommon_files.cmake b/Code/Legacy/CryCommon/crycommon_files.cmake index b8f73d064f..ba11de0215 100644 --- a/Code/Legacy/CryCommon/crycommon_files.cmake +++ b/Code/Legacy/CryCommon/crycommon_files.cmake @@ -53,7 +53,6 @@ set(FILES HMDBus.h VRCommon.h StereoRendererBus.h - HeightmapUpdateNotificationBus.h INavigationSystem.h IMNM.h SFunctor.h diff --git a/Gems/LmbrCentral/Code/include/LmbrCentral/Component/EditorWrappedComponentBase.inl b/Gems/LmbrCentral/Code/include/LmbrCentral/Component/EditorWrappedComponentBase.inl index 2c49710ddd..6a9fceabbf 100644 --- a/Gems/LmbrCentral/Code/include/LmbrCentral/Component/EditorWrappedComponentBase.inl +++ b/Gems/LmbrCentral/Code/include/LmbrCentral/Component/EditorWrappedComponentBase.inl @@ -212,8 +212,11 @@ namespace LmbrCentral template void EditorWrappedComponentBase::OnEntityVisibilityChanged(bool visibility) { - m_visible = visibility; - ConfigurationChanged(); + if (m_visible != visibility) + { + m_visible = visibility; + ConfigurationChanged(); + } } template diff --git a/Gems/SurfaceData/Code/Include/SurfaceData/Utility/SurfaceDataUtility.h b/Gems/SurfaceData/Code/Include/SurfaceData/Utility/SurfaceDataUtility.h index c72f725a07..29b3fac8c7 100644 --- a/Gems/SurfaceData/Code/Include/SurfaceData/Utility/SurfaceDataUtility.h +++ b/Gems/SurfaceData/Code/Include/SurfaceData/Utility/SurfaceDataUtility.h @@ -14,7 +14,6 @@ #include #include #include -#include namespace AZ { diff --git a/Gems/SurfaceData/Code/Source/SurfaceDataModule.cpp b/Gems/SurfaceData/Code/Source/SurfaceDataModule.cpp index 6c96ee9d27..4eef54669a 100644 --- a/Gems/SurfaceData/Code/Source/SurfaceDataModule.cpp +++ b/Gems/SurfaceData/Code/Source/SurfaceDataModule.cpp @@ -20,7 +20,7 @@ namespace SurfaceData SurfaceDataSystemComponent::CreateDescriptor(), SurfaceDataColliderComponent::CreateDescriptor(), SurfaceDataShapeComponent::CreateDescriptor(), - TerrainSurfaceDataSystemComponent::CreateDescriptor(), + Terrain::TerrainSurfaceDataSystemComponent::CreateDescriptor(), }); } @@ -28,7 +28,7 @@ namespace SurfaceData { return AZ::ComponentTypeList{ azrtti_typeid(), - azrtti_typeid(), + azrtti_typeid(), }; } } diff --git a/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.cpp b/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.cpp index 0f6eec73cd..a60bbc4034 100644 --- a/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.cpp +++ b/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.cpp @@ -6,20 +6,16 @@ * */ -#include "TerrainSurfaceDataSystemComponent.h" +#include #include #include #include #include -#include -#include #include #include #include -#include - -namespace SurfaceData +namespace Terrain { ////////////////////////////////////////////////////////////////////////// // TerrainSurfaceDataSystemConfig @@ -98,26 +94,23 @@ namespace SurfaceData void TerrainSurfaceDataSystemComponent::Activate() { - m_providerHandle = InvalidSurfaceDataRegistryHandle; - m_system = GetISystem(); - CrySystemEventBus::Handler::BusConnect(); - AZ::HeightmapUpdateNotificationBus::Handler::BusConnect(); + m_providerHandle = SurfaceData::InvalidSurfaceDataRegistryHandle; + AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusConnect(); UpdateTerrainData(AZ::Aabb::CreateNull()); } void TerrainSurfaceDataSystemComponent::Deactivate() { - if (m_providerHandle != InvalidSurfaceDataRegistryHandle) + if (m_providerHandle != SurfaceData::InvalidSurfaceDataRegistryHandle) { - SurfaceDataSystemRequestBus::Broadcast(&SurfaceDataSystemRequestBus::Events::UnregisterSurfaceDataProvider, m_providerHandle); - m_providerHandle = InvalidSurfaceDataRegistryHandle; + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::UnregisterSurfaceDataProvider, m_providerHandle); + m_providerHandle = SurfaceData::InvalidSurfaceDataRegistryHandle; } - SurfaceDataProviderRequestBus::Handler::BusDisconnect(); - AZ::HeightmapUpdateNotificationBus::Handler::BusDisconnect(); - CrySystemEventBus::Handler::BusDisconnect(); - m_system = nullptr; + SurfaceData::SurfaceDataProviderRequestBus::Handler::BusDisconnect(); + AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusDisconnect(); // Clear the cached terrain bounds data { @@ -146,17 +139,8 @@ namespace SurfaceData return false; } - void TerrainSurfaceDataSystemComponent::OnCrySystemInitialized(ISystem& system, [[maybe_unused]] const SSystemInitParams& systemInitParams) - { - m_system = &system; - } - - void TerrainSurfaceDataSystemComponent::OnCrySystemShutdown([[maybe_unused]] ISystem& system) - { - m_system = nullptr; - } - - void TerrainSurfaceDataSystemComponent::GetSurfacePoints(const AZ::Vector3& inPosition, SurfacePointList& surfacePointList) const + void TerrainSurfaceDataSystemComponent::GetSurfacePoints( + const AZ::Vector3& inPosition, SurfaceData::SurfacePointList& surfacePointList) const { if (m_terrainBoundsIsValid) { @@ -168,12 +152,13 @@ namespace SurfaceData const float terrainHeight = terrain->GetHeight(inPosition, AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, &isTerrainValidAtPoint); const bool isHole = !isTerrainValidAtPoint; - SurfacePoint point; + SurfaceData::SurfacePoint point; point.m_entityId = GetEntityId(); point.m_position = AZ::Vector3(inPosition.GetX(), inPosition.GetY(), terrainHeight); point.m_normal = terrain->GetNormal(inPosition); - const AZ::Crc32 terrainTag = isHole ? Constants::s_terrainHoleTagCrc : Constants::s_terrainTagCrc; - AddMaxValueForMasks(point.m_masks, terrainTag, 1.0f); + const AZ::Crc32 terrainTag = + isHole ? SurfaceData::Constants::s_terrainHoleTagCrc : SurfaceData::Constants::s_terrainTagCrc; + SurfaceData::AddMaxValueForMasks(point.m_masks, terrainTag, 1.0f); surfacePointList.push_back(point); } // Only one handler should exist. @@ -189,11 +174,11 @@ namespace SurfaceData return terrain ? terrain->GetTerrainAabb() : AZ::Aabb::CreateNull(); } - SurfaceTagVector TerrainSurfaceDataSystemComponent::GetSurfaceTags() const + SurfaceData::SurfaceTagVector TerrainSurfaceDataSystemComponent::GetSurfaceTags() const { - SurfaceTagVector tags; - tags.push_back(Constants::s_terrainHoleTagCrc); - tags.push_back(Constants::s_terrainTagCrc); + SurfaceData::SurfaceTagVector tags; + tags.push_back(SurfaceData::Constants::s_terrainHoleTagCrc); + tags.push_back(SurfaceData::Constants::s_terrainTagCrc); return tags; } @@ -203,7 +188,7 @@ namespace SurfaceData bool terrainValidAfterUpdate = false; AZ::Aabb terrainBoundsBeforeUpdate = m_terrainBounds; - SurfaceDataRegistryEntry registryEntry; + SurfaceData::SurfaceDataRegistryEntry registryEntry; registryEntry.m_entityId = GetEntityId(); registryEntry.m_bounds = GetSurfaceAabb(); registryEntry.m_tags = GetSurfaceTags(); @@ -215,38 +200,44 @@ namespace SurfaceData if (terrainValidBeforeUpdate && terrainValidAfterUpdate) { - AZ_Assert((m_providerHandle != InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); + AZ_Assert((m_providerHandle != SurfaceData::InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); // Our terrain was valid before and after, it just changed in some way. If we have a valid dirty region passed in // then it's possible that the heightmap has been modified in the Editor. Otherwise, just notify that the entire // terrain has changed in some way. if (dirtyRegion.IsValid()) { - SurfaceDataSystemRequestBus::Broadcast(&SurfaceDataSystemRequestBus::Events::RefreshSurfaceData, dirtyRegion); + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::RefreshSurfaceData, dirtyRegion); } else { - SurfaceDataSystemRequestBus::Broadcast(&SurfaceDataSystemRequestBus::Events::UpdateSurfaceDataProvider, m_providerHandle, registryEntry); + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::UpdateSurfaceDataProvider, m_providerHandle, registryEntry); } } else if (!terrainValidBeforeUpdate && terrainValidAfterUpdate) { // Our terrain has become valid, so register as a provider and save off the registry handles - AZ_Assert((m_providerHandle == InvalidSurfaceDataRegistryHandle), "Surface Provider data handle is initialized before our terrain became valid"); - SurfaceDataSystemRequestBus::BroadcastResult(m_providerHandle, &SurfaceDataSystemRequestBus::Events::RegisterSurfaceDataProvider, registryEntry); + AZ_Assert( + (m_providerHandle == SurfaceData::InvalidSurfaceDataRegistryHandle), + "Surface Provider data handle is initialized before our terrain became valid"); + SurfaceData::SurfaceDataSystemRequestBus::BroadcastResult( + m_providerHandle, &SurfaceData::SurfaceDataSystemRequestBus::Events::RegisterSurfaceDataProvider, registryEntry); // Start listening for surface data events - AZ_Assert((m_providerHandle != InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); - SurfaceDataProviderRequestBus::Handler::BusConnect(m_providerHandle); + AZ_Assert((m_providerHandle != SurfaceData::InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); + SurfaceData::SurfaceDataProviderRequestBus::Handler::BusConnect(m_providerHandle); } else if (terrainValidBeforeUpdate && !terrainValidAfterUpdate) { // Our terrain has stopped being valid, so unregister and stop listening for surface data events - AZ_Assert((m_providerHandle != InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); - SurfaceDataSystemRequestBus::Broadcast(&SurfaceDataSystemRequestBus::Events::UnregisterSurfaceDataProvider, m_providerHandle); - m_providerHandle = InvalidSurfaceDataRegistryHandle; + AZ_Assert((m_providerHandle != SurfaceData::InvalidSurfaceDataRegistryHandle), "Invalid surface data handle"); + SurfaceData::SurfaceDataSystemRequestBus::Broadcast( + &SurfaceData::SurfaceDataSystemRequestBus::Events::UnregisterSurfaceDataProvider, m_providerHandle); + m_providerHandle = SurfaceData::InvalidSurfaceDataRegistryHandle; - SurfaceDataProviderRequestBus::Handler::BusDisconnect(); + SurfaceData::SurfaceDataProviderRequestBus::Handler::BusDisconnect(); } else { @@ -255,8 +246,9 @@ namespace SurfaceData } - void TerrainSurfaceDataSystemComponent::HeightmapModified(const AZ::Aabb& bounds) + void TerrainSurfaceDataSystemComponent::OnTerrainDataChanged( + const AZ::Aabb& dirtyRegion, [[maybe_unused]] TerrainDataChangedMask dataChangedMask) { - UpdateTerrainData(bounds); + UpdateTerrainData(dirtyRegion); } } diff --git a/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.h b/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.h index aa5f4ab04c..a742eab78c 100644 --- a/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.h +++ b/Gems/SurfaceData/Code/Source/TerrainSurfaceDataSystemComponent.h @@ -10,12 +10,11 @@ #include #include #include -#include -#include +#include #include #include -namespace SurfaceData +namespace Terrain { class TerrainSurfaceDataSystemConfig : public AZ::ComponentConfig @@ -31,9 +30,8 @@ namespace SurfaceData */ class TerrainSurfaceDataSystemComponent : public AZ::Component - , private SurfaceDataProviderRequestBus::Handler - , private AZ::HeightmapUpdateNotificationBus::Handler - , private CrySystemEventBus::Handler + , private SurfaceData::SurfaceDataProviderRequestBus::Handler + , private AzFramework::Terrain::TerrainDataNotificationBus::Handler { friend class EditorTerrainSurfaceDataSystemComponent; TerrainSurfaceDataSystemComponent(const TerrainSurfaceDataSystemConfig&); @@ -58,25 +56,19 @@ namespace SurfaceData ////////////////////////////////////////////////////////////////////////// // SurfaceDataProviderRequestBus - void GetSurfacePoints(const AZ::Vector3& inPosition, SurfacePointList& surfacePointList) const; - - //////////////////////////////////////////////////////////////////////////// - // CrySystemEvents - void OnCrySystemInitialized(ISystem& system, const SSystemInitParams& systemInitParams) override; - void OnCrySystemShutdown(ISystem& system) override; + void GetSurfacePoints(const AZ::Vector3& inPosition, SurfaceData::SurfacePointList& surfacePointList) const; ////////////////////////////////////////////////////////////////////////// - // AZ::HeightmapUpdateNotificationBus - void HeightmapModified(const AZ::Aabb& bounds) override; + // AzFramework::Terrain::TerrainDataNotificationBus + void OnTerrainDataChanged(const AZ::Aabb& dirtyRegion, TerrainDataChangedMask dataChangedMask) override; private: void UpdateTerrainData(const AZ::Aabb& dirtyRegion); AZ::Aabb GetSurfaceAabb() const; - SurfaceTagVector GetSurfaceTags() const; - SurfaceDataRegistryHandle m_providerHandle = InvalidSurfaceDataRegistryHandle; + SurfaceData::SurfaceTagVector GetSurfaceTags() const; + SurfaceData::SurfaceDataRegistryHandle m_providerHandle = SurfaceData::InvalidSurfaceDataRegistryHandle; TerrainSurfaceDataSystemConfig m_configuration; - ISystem* m_system = nullptr; AZ::Aabb m_terrainBounds = AZ::Aabb::CreateNull(); AZStd::atomic_bool m_terrainBoundsIsValid{ false };