Files
o3de/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/MaterialComponentConfig.cpp
T
Chris Santora 14d2e38b90 Refactored how model material slots work in preparation to support more flexible material conversion options for the scene asset pipeline. The material slot IDs are based on the MaterialUid that come from SceneAPI. Since these IDs are also used as the AssetId sub-ID for the converted material assets, the system was just checking the material asset sub-ID to determine the material slot ID. But in order to support certain FBX material conversion options, we needed to break this tie, so the slot ID is separate from the AssetId of the material in that slot. This will allow some other material to be used in the slot, instead of being forced to use one that was generated from the FBX.
Here we inttroduce a new struct ModelMaterialSlot which formalizes the concept of material slot, with an ID, display name, and default material assignment. The ID still comes from the MaterialUid like before. The display name is built-in, rather than being parsed out from the asset file name. And the default material assignment can be any material asset, it doesn't have to come from the FBX (or other scene file).

This commit is just the preliminary set of changes. Cursory testing shows that it works pretty well but more testing is needed (and likely some fixes) before merging.

Here is what's left to do...
Add serialization version converters to preserve prior prefab data.
See if we can get rid of GetLabelByAssetId function only rely on the display name inside ModelMaterialSlot.
I'm not sure if the condition for enabling the "Edit Material Instance..." context menu item is correct.
Test actors
Lots more testing in general

Signed-off-by: santorac <55155825+santorac@users.noreply.github.com>
2021-07-30 11:40:51 -07:00

85 lines
3.8 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentConfig.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
namespace AZ
{
namespace Render
{
using DeprecatedMaterialAssignmentId = AZStd::pair<MaterialAssignmentLodIndex, AZ::Data::AssetId>;
using DeprecatedMaterialAssignmentMap = AZStd::unordered_map<DeprecatedMaterialAssignmentId, MaterialAssignment>;
// Update serialized data to the new format and data types
bool MaterialComponentConfigVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
{
if (classElement.GetVersion() < 3)
{
constexpr AZ::u32 materialDataCrc = AZ_CRC("Materials", 0x9b1716b5);
// MaterialAssignmentId was changed from an AZStd::pair to an explicit structure
// Any previously stored data needs to be converted to preserve existing levels and slices
DeprecatedMaterialAssignmentMap oldMaterials;
if (!classElement.GetChildData(materialDataCrc, oldMaterials))
{
AZ_Error("AZ::Render::MaterialComponentConfigVersionConverter", false, "Failed to get Materials element");
return false;
}
if (!classElement.RemoveElementByName(materialDataCrc))
{
AZ_Error("AZ::Render::MaterialComponentConfigVersionConverter", false, "Failed to remove Materials element");
return false;
}
// Transform the old map to the new format
MaterialAssignmentMap newMaterials;
for (const auto& oldPair : oldMaterials)
{
const DeprecatedMaterialAssignmentId& oldId = oldPair.first;
const MaterialAssignmentId newId(oldId.first, oldId.second.m_subId);
newMaterials[newId] = oldPair.second;
}
classElement.AddElementWithData(context, "materials", newMaterials);
}
return true;
}
void MaterialComponentConfig::Reflect(ReflectContext* context)
{
if (auto serializeContext = azrtti_cast<SerializeContext*>(context))
{
// The types being replaced must be reflected to deserialize old data
serializeContext->RegisterGenericType<DeprecatedMaterialAssignmentId>();
serializeContext->RegisterGenericType<DeprecatedMaterialAssignmentMap>();
serializeContext->Class<MaterialComponentConfig, ComponentConfig>()
->Version(3, MaterialComponentConfigVersionConverter)
->Field("materials", &MaterialComponentConfig::m_materials)
;
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Class<MaterialComponentConfig>("MaterialComponentConfig")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "render")
->Attribute(AZ::Script::Attributes::Module, "render")
->Constructor()
->Constructor<const MaterialComponentConfig&>()
->Property("materials", BehaviorValueProperty(&MaterialComponentConfig::m_materials))
;
}
}
} // namespace Render
} // namespace AZ