[ATOM-14477] Add clone function to model and/or model asset (#135)

* Added clone methods for the buffer, model lod and model assets.
* Cloning works via the creators.
* Mesh handle/instance now knows about the model asset it originated from as well as the cloned model asset that we need for instancing until instancing works without the dependencies to the asset ids.
* MeshComponentRequestBus returns the original asset id and the model asset. If you need access to the clone, go by the model.
* Cloth component mesh now gettings its model asset from the model as it needs to work on the clone.
* Moved the requires cloning function from the mesh loader in the mesh feature processor to the mesh component controller.
* As we need the model asset to be loaded before we can check if it requires cloning or not, we couldn't pass in a bool from the controller but had to use a callback.
* Using asset hint instead of asset id for the model lod asset creator error.
* Storing a map of source buffer asset ids and the actual cloned buffer assets rather than just their ids.
* Using the buffer assets from the new map directly and removed the search process in the mesh loop where the asset views are created.
* Fixed the vegetation mocks.
* Using the actor's mesh asset when emitting the on model ready event for the mesh component notification bus.
* Mesh components notifications connection policy changed to adapt to cloned model asset.
* Handling empty meshes in model lod asset creator.
* Removed the requires cloning callback from the mesh feature processor and made it a parameter to the acquire mesh function
* Fixing mocks and unit tests
This commit is contained in:
Benjamin Jillich
2021-04-23 18:33:00 +02:00
committed by GitHub
parent e772afa06e
commit 0fa00a117c
18 changed files with 651 additions and 288 deletions
@@ -268,12 +268,32 @@ namespace AZ
}
}
bool MeshComponentController::RequiresCloning(const Data::Asset<RPI::ModelAsset>& modelAsset)
{
// Is the model asset containing a cloth buffer? If yes, we need to clone the model asset for instancing.
const AZStd::array_view<AZ::Data::Asset<AZ::RPI::ModelLodAsset>> lodAssets = modelAsset->GetLodAssets();
for (const AZ::Data::Asset<AZ::RPI::ModelLodAsset>& lodAsset : lodAssets)
{
const AZStd::array_view<AZ::RPI::ModelLodAsset::Mesh> meshes = lodAsset->GetMeshes();
for (const AZ::RPI::ModelLodAsset::Mesh& mesh : meshes)
{
if (mesh.GetSemanticBufferAssetView(AZ::Name("CLOTH_DATA")) != nullptr)
{
return true;
}
}
}
return false;
}
void MeshComponentController::HandleModelChange(Data::Instance<RPI::Model> model)
{
if (model)
Data::Asset<RPI::ModelAsset> modelAsset = m_meshFeatureProcessor->GetModelAsset(m_meshHandle);
if (model && modelAsset)
{
m_configuration.m_modelAsset = model->GetModelAsset();
MeshComponentNotificationBus::Event(m_entityId, &MeshComponentNotificationBus::Events::OnModelReady, model->GetModelAsset(), model);
m_configuration.m_modelAsset = modelAsset;
MeshComponentNotificationBus::Event(m_entityId, &MeshComponentNotificationBus::Events::OnModelReady, m_configuration.m_modelAsset, model);
MaterialReceiverNotificationBus::Event(m_entityId, &MaterialReceiverNotificationBus::Events::OnMaterialAssignmentsChanged);
AzFramework::EntityBoundsUnionRequestBus::Broadcast(
&AzFramework::EntityBoundsUnionRequestBus::Events::RefreshEntityLocalBoundsUnion, m_entityId);
@@ -288,7 +308,8 @@ namespace AZ
MaterialComponentRequestBus::EventResult(materials, m_entityId, &MaterialComponentRequests::GetMaterialOverrides);
m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
m_meshHandle = m_meshFeatureProcessor->AcquireMesh(m_configuration.m_modelAsset, materials);
m_meshHandle = m_meshFeatureProcessor->AcquireMesh(m_configuration.m_modelAsset, materials,
/*skinnedMeshWithMotion=*/false, /*rayTracingEnabled=*/true, RequiresCloning);
m_meshFeatureProcessor->ConnectModelChangeEventHandler(m_meshHandle, m_changeEventHandler);
const AZ::Transform& transform = m_transformInterface ? m_transformInterface->GetWorldTM() : AZ::Transform::CreateIdentity();
@@ -345,9 +366,9 @@ namespace AZ
}
}
const Data::Asset<RPI::ModelAsset>& MeshComponentController::GetModelAsset() const
Data::Asset<const RPI::ModelAsset> MeshComponentController::GetModelAsset() const
{
return GetModel() ? GetModel()->GetModelAsset() : m_configuration.m_modelAsset;
return m_configuration.m_modelAsset;
}
Data::AssetId MeshComponentController::GetModelAssetId() const
@@ -369,7 +390,7 @@ namespace AZ
return assetPathString;
}
const Data::Instance<RPI::Model> MeshComponentController::GetModel() const
Data::Instance<RPI::Model> MeshComponentController::GetModel() const
{
return m_meshFeatureProcessor ? m_meshFeatureProcessor->GetModel(m_meshHandle) : Data::Instance<RPI::Model>();
}
@@ -83,17 +83,16 @@ namespace AZ
const MeshComponentConfig& GetConfiguration() const;
private:
AZ_DISABLE_COPY(MeshComponentController);
// MeshComponentRequestBus::Handler overrides ...
void SetModelAsset(Data::Asset<RPI::ModelAsset> modelAsset) override;
const Data::Asset<RPI::ModelAsset>& GetModelAsset() const override;
Data::Asset<const RPI::ModelAsset> GetModelAsset() const override;
void SetModelAssetId(Data::AssetId modelAssetId) override;
Data::AssetId GetModelAssetId() const override;
void SetModelAssetPath(const AZStd::string& modelAssetPath) override;
AZStd::string GetModelAssetPath() const override;
const AZ::Data::Instance<RPI::Model> GetModel() const override;
AZ::Data::Instance<RPI::Model> GetModel() const override;
void SetSortKey(RHI::DrawItemSortKey sortKey) override;
RHI::DrawItemSortKey GetSortKey() const override;
@@ -118,6 +117,13 @@ namespace AZ
// MaterialComponentNotificationBus::Handler overrides ...
void OnMaterialsUpdated(const MaterialAssignmentMap& materials) override;
//! Check if the model asset requires to be cloned (e.g. cloth) for unique model instances.
//! @param modelAsset The model asset to check.
//! @result True in case the model asset needs to be cloned before creating the model. False if there is a 1:1 relationship between
//! the model asset and the model and it is static and shared. In the second case the m_originalModelAsset of the mesh handle is
//! equal to the model asset that the model is linked to.
static bool RequiresCloning(const Data::Asset<RPI::ModelAsset>& modelAsset);
void HandleModelChange(Data::Instance<RPI::Model> model);
void RegisterModel();
void UnregisterModel();