Made material property auto-rename procedure apply to Material Component at runtime. This ensures that an material property overrides and any gameplay scripts that work with property overrides can get the benefit of the material type version update procedure.
I added an ApplyPropertyRenames function to MaterialTypeAsset very similar to the one in MaterialTypeSourceData. Updated the MaterialAssignment class to apply any property renames when it discovers the old name doesn't work. This will be written to disk when the level or prefab is saved. Signed-off-by: santorac <55155825+santorac@users.noreply.github.com>
This commit is contained in:
@@ -129,6 +129,10 @@ namespace AZ
|
||||
|
||||
const AZStd::vector<MaterialVersionUpdate>& GetMaterialVersionUpdateList() const { return m_materialVersionUpdates; }
|
||||
|
||||
//! Possibly renames @propertyId based on the material version update steps.
|
||||
//! @return true if the property was renamed
|
||||
bool ApplyPropertyRenames(AZ::Name& propertyId) const;
|
||||
|
||||
private:
|
||||
bool PostLoadInit() override;
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ namespace AZ
|
||||
|
||||
uint32_t GetVersion() const;
|
||||
void SetVersion(uint32_t toVersion);
|
||||
|
||||
//! Possibly renames @propertyId based on the material version update steps.
|
||||
//! @return true if the property was renamed
|
||||
bool ApplyPropertyRenames(AZ::Name& propertyId) const;
|
||||
|
||||
//! Apply version updates to the given material asset.
|
||||
//! @return true if any changes were made
|
||||
|
||||
@@ -169,6 +169,22 @@ namespace AZ
|
||||
return m_version;
|
||||
}
|
||||
|
||||
|
||||
bool MaterialTypeAsset::ApplyPropertyRenames(AZ::Name& propertyId) const
|
||||
{
|
||||
bool renamed = false;
|
||||
|
||||
for (const auto& versionUpdates : m_materialVersionUpdates)
|
||||
{
|
||||
if (versionUpdates.ApplyPropertyRenames(propertyId))
|
||||
{
|
||||
renamed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return renamed;
|
||||
}
|
||||
|
||||
void MaterialTypeAsset::SetReady()
|
||||
{
|
||||
m_status = AssetStatus::Ready;
|
||||
|
||||
@@ -56,6 +56,22 @@ namespace AZ
|
||||
{
|
||||
m_toVersion = toVersion;
|
||||
}
|
||||
|
||||
bool MaterialVersionUpdate::ApplyPropertyRenames(AZ::Name& propertyId) const
|
||||
{
|
||||
bool renamed = false;
|
||||
|
||||
for (const auto& action : m_actions)
|
||||
{
|
||||
if (action.m_fromPropertyId == propertyId)
|
||||
{
|
||||
propertyId = action.m_toPropertyId;
|
||||
renamed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return renamed;
|
||||
}
|
||||
|
||||
bool MaterialVersionUpdate::ApplyVersionUpdates(MaterialAsset& materialAsset) const
|
||||
{
|
||||
|
||||
@@ -1041,5 +1041,88 @@ namespace UnitTest
|
||||
EXPECT_FALSE(materialTypeAsset->GetShaderCollection()[1].MaterialOwnsShaderOption(Name{"o_globalOption_inShaderB"}));
|
||||
}
|
||||
|
||||
TEST_F(MaterialTypeAssetTests, ApplyPropertyRenames)
|
||||
{
|
||||
Data::Asset<MaterialTypeAsset> materialTypeAsset;
|
||||
|
||||
auto addRenameAction = [](MaterialVersionUpdate& versionUpdate, const char* from, const char* to)
|
||||
{
|
||||
versionUpdate.AddAction(MaterialVersionUpdate::RenamePropertyAction(
|
||||
{
|
||||
Name{ from },
|
||||
Name{ to }
|
||||
}));
|
||||
};
|
||||
|
||||
MaterialTypeAssetCreator materialTypeCreator;
|
||||
materialTypeCreator.Begin(Uuid::CreateRandom());
|
||||
|
||||
// Version updates
|
||||
materialTypeCreator.SetVersion(10);
|
||||
|
||||
MaterialVersionUpdate versionUpdate2(2);
|
||||
addRenameAction(versionUpdate2, "general.fooA", "general.fooB");
|
||||
materialTypeCreator.AddVersionUpdate(versionUpdate2);
|
||||
|
||||
MaterialVersionUpdate versionUpdate4(4);
|
||||
addRenameAction(versionUpdate4, "general.barA", "general.barB");
|
||||
materialTypeCreator.AddVersionUpdate(versionUpdate4);
|
||||
|
||||
MaterialVersionUpdate versionUpdate6(6);
|
||||
addRenameAction(versionUpdate6, "general.fooB", "general.fooC");
|
||||
addRenameAction(versionUpdate6, "general.barB", "general.barC");
|
||||
materialTypeCreator.AddVersionUpdate(versionUpdate6);
|
||||
|
||||
MaterialVersionUpdate versionUpdate7(7);
|
||||
addRenameAction(versionUpdate7, "general.bazA", "otherGroup.bazB");
|
||||
materialTypeCreator.AddVersionUpdate(versionUpdate7);
|
||||
|
||||
materialTypeCreator.BeginMaterialProperty(Name{ "general.fooC" }, MaterialPropertyDataType::Bool);
|
||||
materialTypeCreator.EndMaterialProperty();
|
||||
materialTypeCreator.BeginMaterialProperty(Name{ "general.barC" }, MaterialPropertyDataType::Bool);
|
||||
materialTypeCreator.EndMaterialProperty();
|
||||
materialTypeCreator.BeginMaterialProperty(Name{ "otherGroup.bazB" }, MaterialPropertyDataType::Bool);
|
||||
materialTypeCreator.EndMaterialProperty();
|
||||
|
||||
EXPECT_TRUE(materialTypeCreator.End(materialTypeAsset));
|
||||
|
||||
AZ::Name propertyId;
|
||||
|
||||
propertyId = AZ::Name{"doesNotExist"};
|
||||
EXPECT_FALSE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "doesNotExist");
|
||||
|
||||
propertyId = AZ::Name{"general.fooA"};
|
||||
EXPECT_TRUE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.fooC");
|
||||
|
||||
propertyId = AZ::Name{"general.fooB"};
|
||||
EXPECT_TRUE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.fooC");
|
||||
|
||||
propertyId = AZ::Name{"general.fooC"};
|
||||
EXPECT_FALSE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.fooC");
|
||||
|
||||
propertyId = AZ::Name{"general.barA"};
|
||||
EXPECT_TRUE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.barC");
|
||||
|
||||
propertyId = AZ::Name{"general.barB"};
|
||||
EXPECT_TRUE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.barC");
|
||||
|
||||
propertyId = AZ::Name{"general.barC"};
|
||||
EXPECT_FALSE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "general.barC");
|
||||
|
||||
propertyId = AZ::Name{"general.bazA"};
|
||||
EXPECT_TRUE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "otherGroup.bazB");
|
||||
|
||||
propertyId = AZ::Name{"otherGroup.bazB"};
|
||||
EXPECT_FALSE(materialTypeAsset->ApplyPropertyRenames(propertyId));
|
||||
EXPECT_STREQ(propertyId.GetCStr(), "otherGroup.bazB");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user