defer rebuilding the white box mesh by a frame to prevent reentrancy causing issues

This commit is contained in:
hultonha
2021-05-05 16:55:48 +01:00
parent b708bc2c10
commit 1d900947f5
3 changed files with 21 additions and 13 deletions
@@ -181,6 +181,9 @@ namespace WhiteBox
// disconnect from any previously connected asset id
AZ::Data::AssetBus::Handler::BusDisconnect();
WhiteBoxMeshAssetNotificationBus::Handler::BusDisconnect();
// ensure we're disconnected from the tick bus
AZ::TickBus::Handler::BusDisconnect();
}
void EditorWhiteBoxMeshAsset::Release()
@@ -215,11 +218,20 @@ namespace WhiteBox
if (asset == m_meshAsset)
{
m_meshAsset = asset;
EditorWhiteBoxComponentRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
// defer rebuilding the mesh by a frame by connecting to the tick bus - this prevents issues
// with reentrancy when rebuilding the white box mesh
AZ::TickBus::Handler::BusConnect();
}
}
void EditorWhiteBoxMeshAsset::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
// after rebuilding the white box mesh, immediately disconnect from the tick bus (we only use it for deferred rebuilding)
EditorWhiteBoxComponentRequestBus::Event(m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
AZ::TickBus::Handler::BusDisconnect();
}
void EditorWhiteBoxMeshAsset::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
OnAssetReady(asset);
@@ -16,6 +16,7 @@
#include "Asset/WhiteBoxMeshAssetBus.h"
#include <AzCore/Component/ComponentBus.h>
#include <AzCore/Component/TickBus.h>
#include <IEditor.h>
namespace WhiteBox
@@ -26,6 +27,7 @@ namespace WhiteBox
: private AZ::Data::AssetBus::Handler
, private WhiteBoxMeshAssetNotificationBus::Handler
, private IEditorNotifyListener
, private AZ::TickBus::Handler
{
public:
AZ_CLASS_ALLOCATOR_DECL
@@ -93,6 +95,9 @@ namespace WhiteBox
//! Disconnect from buses/listeners before either releasing or destroying the asset.
void Disconnect();
// AZ::TickBus overrides ...
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
// Listeners for legacy editor events when the level is saved.
void RegisterForEditorEvents();
void UnregisterForEditorEvents();
@@ -119,17 +119,8 @@ namespace WhiteBox
modelLodCreator.BeginMesh();
modelLodCreator.SetMeshAabb(meshData.GetAabb());
const AZ::Data::AssetId materialAssetId =
AZ::RPI::AssetUtils::GetAssetIdForProductPath(TexturedMaterialPath.data(), AZ::RPI::AssetUtils::TraceLevel::Warning);
auto materialAsset = AZ::Data::AssetManager::Instance().FindAsset(materialAssetId, AZ::Data::AssetLoadBehavior::Default);
if (!materialAsset)
{
// set the default material
materialAsset = AZ::RPI::AssetUtils::LoadAssetById<AZ::RPI::MaterialAsset>(materialAssetId);
}
if (materialAsset)
// set the default material
if (auto materialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(TexturedMaterialPath.data()))
{
modelLodCreator.SetMeshMaterialAsset(materialAsset);
}