Physics material system for spectra launch

- Invalidate 'Physics Materials From Mesh' boolean from collider component
- Removed material library from material selector. Default material library will always be used instead.
- Marking failing automated test as xfail
- Added default material to physics configuration.
- Moved material library asset from physx configuration to physics configuration, as it doesn't need to be physx specific.
- Refactor physics material system having into account that there is only one material library in the project.
- Renaming code from DefaultMaterialLibrary to MaterialLibrary.
- All queries about physics materials unified under PhysicsMaterialRequests bus.
- PhysXSystem only manages the material library asset.
- Saving and reloading the same physics material asset with different content didn't trigger a events that the material library has changed.
- Changing Physics Material Request interface to use shared_ptr instead of weak_ptr to be simpler to handle the returned materials and having a more consistent code.
- Refactored Material Manager to improve its implementation. Still following the same approach of "creating materials on the fly as they are requested", but now it's doing it consistently across the interface, with private helpers functions FindOrCreateMaterial that simplify vastly the implementation.
- Material Manager now listens to change event of material library asset and default material configuration so it updates its materials accordingly.
- Complete Material move constructor and operator.
This commit is contained in:
Aaron Ruiz Mora
2021-05-28 20:16:25 +01:00
committed by GitHub
parent ff9a052ce6
commit 00e860f326
54 changed files with 859 additions and 943 deletions
-134
View File
@@ -347,27 +347,6 @@ namespace PhysX
return AZStd::make_shared<PhysX::Material>(materialConfiguration);
}
AZStd::vector<AZStd::shared_ptr<Physics::Material>> SystemComponent::CreateMaterialsFromLibrary(const Physics::MaterialSelection& materialSelection)
{
AZStd::vector<physx::PxMaterial*> pxMaterials;
m_materialManager.GetPxMaterials(materialSelection, pxMaterials);
AZStd::vector<AZStd::shared_ptr<Physics::Material>> genericMaterials;
genericMaterials.reserve(pxMaterials.size());
for (physx::PxMaterial* pxMaterial : pxMaterials)
{
genericMaterials.push_back(static_cast<PhysX::Material*>(pxMaterial->userData)->shared_from_this());
}
return genericMaterials;
}
AZStd::shared_ptr<Physics::Material> SystemComponent::GetDefaultMaterial()
{
return m_materialManager.GetDefaultMaterial();
}
AZStd::vector<AZ::TypeId> SystemComponent::GetSupportedJointTypes()
{
return JointUtils::GetSupportedJointTypes();
@@ -484,58 +463,6 @@ namespace PhysX
return m_physXSystem->GetPxCooking();
}
bool SystemComponent::UpdateMaterialSelection(const Physics::ShapeConfiguration& shapeConfiguration,
Physics::ColliderConfiguration& colliderConfiguration)
{
Physics::MaterialSelection& materialSelection = colliderConfiguration.m_materialSelection;
// If the material library is still not set, we can't update the material selection
if (!materialSelection.IsMaterialLibraryValid())
{
AZ_Warning("PhysX", false,
"UpdateMaterialSelection: Material Selection tried to use an invalid/non-existing Physics material library: \"%s\". "
"Please make sure the file exists or re-assign another library", materialSelection.GetMaterialLibraryAssetHint().c_str());
return false;
}
// If there's no material library data loaded, try to load it
if (materialSelection.GetMaterialLibraryAssetData() == nullptr)
{
AZ::Data::AssetId materialLibraryAssetId = materialSelection.GetMaterialLibraryAssetId();
materialSelection.SetMaterialLibrary(materialLibraryAssetId);
}
// If there's still not material library data, we can't update the material selection
if (materialSelection.GetMaterialLibraryAssetData() == nullptr)
{
AZ::Data::AssetId materialLibraryAssetId = materialSelection.GetMaterialLibraryAssetId();
auto materialLibraryAsset =
AZ::Data::AssetManager::Instance().GetAsset<Physics::MaterialLibraryAsset>(materialLibraryAssetId, AZ::Data::AssetLoadBehavior::Default);
materialLibraryAsset.BlockUntilLoadComplete();
// Log the asset path to help find out the incorrect library reference
AZStd::string assetPath = materialLibraryAsset.GetHint();
AZ_Warning("PhysX", false,
"UpdateMaterialSelection: Unable to load the material library for a material selection."
" Please check if the asset %s exists in the asset cache.", assetPath.c_str());
return false;
}
if (shapeConfiguration.GetShapeType() == Physics::ShapeType::PhysicsAsset)
{
const Physics::PhysicsAssetShapeConfiguration& assetConfiguration =
static_cast<const Physics::PhysicsAssetShapeConfiguration&>(shapeConfiguration);
// Use the materials data from the asset to update the collider data
return UpdateMaterialSelectionFromPhysicsAsset(assetConfiguration, colliderConfiguration);
}
return true;
}
void SystemComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
if (m_physXSystem)
@@ -614,65 +541,4 @@ namespace PhysX
m_windProvider = AZStd::make_unique<WindProvider>();
}
bool SystemComponent::UpdateMaterialSelectionFromPhysicsAsset(
const Physics::PhysicsAssetShapeConfiguration& assetConfiguration,
Physics::ColliderConfiguration& colliderConfiguration)
{
Physics::MaterialSelection& materialSelection = colliderConfiguration.m_materialSelection;
if (!assetConfiguration.m_asset.GetId().IsValid())
{
// Set the default selection if there's no physics asset.
materialSelection.SetMaterialSlots(Physics::MaterialSelection::SlotsArray());
return false;
}
if (!assetConfiguration.m_asset.IsReady())
{
// The asset is valid but is still loading,
// Do not set the empty slots in this case to avoid the entity being in invalid state
return false;
}
Pipeline::MeshAsset* meshAsset = assetConfiguration.m_asset.GetAs<Pipeline::MeshAsset>();
if (!meshAsset)
{
materialSelection.SetMaterialSlots(Physics::MaterialSelection::SlotsArray());
AZ_Warning("PhysX", false, "UpdateMaterialSelectionFromPhysicsAsset: MeshAsset is invalid");
return false;
}
// Set the slots from the mesh asset
materialSelection.SetMaterialSlots(meshAsset->m_assetData.m_surfaceNames);
if (!assetConfiguration.m_useMaterialsFromAsset)
{
return false;
}
const Physics::MaterialLibraryAsset* materialLibrary = materialSelection.GetMaterialLibraryAssetData();
const AZStd::vector<AZStd::string>& meshMaterialNames = meshAsset->m_assetData.m_materialNames;
// Update material IDs in the selection for each slot
int slotIndex = 0;
for (const AZStd::string& meshMaterialName : meshMaterialNames)
{
Physics::MaterialFromAssetConfiguration materialData;
bool found = materialLibrary->GetDataForMaterialName(meshMaterialName, materialData);
AZ_Warning("PhysX", found,
"UpdateMaterialSelectionFromPhysicsAsset: No material found for surfaceType (%s) in the collider material library",
meshMaterialName.c_str());
if (found)
{
materialSelection.SetMaterialId(materialData.m_id, slotIndex);
}
slotIndex++;
}
return true;
}
} // namespace PhysX