[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
@@ -10,6 +10,8 @@
*
*/
#include <AzCore/std/containers/set.h>
#include <Atom/RPI.Reflect/Buffer/BufferAssetCreator.h>
#include <Atom/RPI.Reflect/Model/ModelLodAssetCreator.h>
#include <AzCore/Asset/AssetManager.h>
@@ -240,5 +242,88 @@ namespace AZ
return true;
}
bool ModelLodAssetCreator::Clone(const Data::Asset<ModelLodAsset>& sourceAsset, Data::Asset<ModelLodAsset>& clonedResult, Data::AssetId& inOutLastCreatedAssetId)
{
AZStd::array_view<ModelLodAsset::Mesh> sourceMeshes = sourceAsset->GetMeshes();
if (sourceMeshes.empty())
{
return true;
}
ModelLodAssetCreator creator;
inOutLastCreatedAssetId.m_subId = inOutLastCreatedAssetId.m_subId + 1;
creator.Begin(inOutLastCreatedAssetId);
// Add the index buffer
const Data::Asset<BufferAsset> sourceIndexBufferAsset = sourceMeshes[0].GetIndexBufferAssetView().GetBufferAsset();
Data::Asset<BufferAsset> clonedIndexBufferAsset;
BufferAssetCreator::Clone(sourceIndexBufferAsset, clonedIndexBufferAsset, inOutLastCreatedAssetId);
creator.SetLodIndexBuffer(clonedIndexBufferAsset);
// Add meshes
AZStd::unordered_map<AZ::Data::AssetId, Data::Asset<BufferAsset>> oldToNewBufferAssets;
for (const ModelLodAsset::Mesh& sourceMesh : sourceMeshes)
{
// Add stream buffers
for (const AZ::RPI::ModelLodAsset::Mesh::StreamBufferInfo& streamBufferInfo : sourceMesh.GetStreamBufferInfoList())
{
const Data::Asset<BufferAsset>& sourceStreamBuffer = streamBufferInfo.m_bufferAssetView.GetBufferAsset();
const AZ::Data::AssetId sourceBufferAssetId = sourceStreamBuffer.GetId();
// In case the buffer asset id is not part of our old to new asset id mapping, we did not convert and add it yet.
if (oldToNewBufferAssets.find(sourceBufferAssetId) == oldToNewBufferAssets.end())
{
Data::Asset<BufferAsset> streamBufferAsset;
if (!BufferAssetCreator::Clone(sourceStreamBuffer, streamBufferAsset, inOutLastCreatedAssetId))
{
AZ_Error("ModelLodAssetCreator", false,
"Cannot clone buffer asset for '%s'.", sourceBufferAssetId.ToString<AZStd::string>().c_str());
return false;
}
oldToNewBufferAssets[sourceBufferAssetId] = streamBufferAsset;
creator.AddLodStreamBuffer(streamBufferAsset);
}
}
// Add mesh
creator.BeginMesh();
creator.SetMeshName(sourceMesh.GetName());
AZ::Aabb aabb = sourceMesh.GetAabb();
creator.SetMeshAabb(AZStd::move(aabb));
creator.SetMeshMaterialAsset(sourceMesh.GetMaterialAsset());
// Mesh index buffer view
const BufferAssetView& sourceIndexBufferView = sourceMesh.GetIndexBufferAssetView();
BufferAssetView indexBufferAssetView(clonedIndexBufferAsset, sourceIndexBufferView.GetBufferViewDescriptor());
creator.SetMeshIndexBuffer(indexBufferAssetView);
// Mesh stream buffer views
for (const AZ::RPI::ModelLodAsset::Mesh::StreamBufferInfo& streamBufferInfo : sourceMesh.GetStreamBufferInfoList())
{
// Get the corresponding new buffer asset id from the source buffer.
const AZ::Data::AssetId sourceBufferAssetId = streamBufferInfo.m_bufferAssetView.GetBufferAsset().GetId();
const auto assetIdIterator = oldToNewBufferAssets.find(sourceBufferAssetId);
if (assetIdIterator != oldToNewBufferAssets.end())
{
const Data::Asset<BufferAsset>& clonedBufferAsset = assetIdIterator->second;
BufferAssetView bufferAssetView(clonedBufferAsset, streamBufferInfo.m_bufferAssetView.GetBufferViewDescriptor());
creator.AddMeshStreamBuffer(streamBufferInfo.m_semantic, streamBufferInfo.m_customName, bufferAssetView);
}
else
{
AZ_Error("ModelLodAssetCreator", false,
"Cannot find cloned buffer asset for source buffer asset '%s'.",
sourceBufferAssetId.ToString<AZStd::string>().c_str());
return false;
}
}
creator.EndMesh();
}
return creator.End(clonedResult);
}
} // namespace RPI
} // namespace AZ