MaterialComponentConfig.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentConfig.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. using DeprecatedMaterialAssignmentId = AZStd::pair<MaterialAssignmentLodIndex, AZ::Data::AssetId>;
  16. using DeprecatedMaterialAssignmentMap = AZStd::unordered_map<DeprecatedMaterialAssignmentId, MaterialAssignment>;
  17. // Update serialized data to the new format and data types
  18. bool MaterialComponentConfigVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  19. {
  20. if (classElement.GetVersion() < 3)
  21. {
  22. constexpr AZ::u32 materialDataCrc = AZ_CRC_CE("Materials");
  23. // MaterialAssignmentId was changed from an AZStd::pair to an explicit structure
  24. // Any previously stored data needs to be converted to preserve existing levels and slices
  25. DeprecatedMaterialAssignmentMap oldMaterials;
  26. if (!classElement.GetChildData(materialDataCrc, oldMaterials))
  27. {
  28. AZ_Error("AZ::Render::MaterialComponentConfigVersionConverter", false, "Failed to get Materials element");
  29. return false;
  30. }
  31. if (!classElement.RemoveElementByName(materialDataCrc))
  32. {
  33. AZ_Error("AZ::Render::MaterialComponentConfigVersionConverter", false, "Failed to remove Materials element");
  34. return false;
  35. }
  36. // Transform the old map to the new format
  37. MaterialAssignmentMap newMaterials;
  38. for (const auto& oldPair : oldMaterials)
  39. {
  40. const DeprecatedMaterialAssignmentId& oldId = oldPair.first;
  41. const MaterialAssignmentId newId(oldId.first, oldId.second.m_subId);
  42. newMaterials[newId] = oldPair.second;
  43. }
  44. classElement.AddElementWithData(context, "materials", newMaterials);
  45. }
  46. return true;
  47. }
  48. void MaterialComponentConfig::Reflect(ReflectContext* context)
  49. {
  50. if (auto serializeContext = azrtti_cast<SerializeContext*>(context))
  51. {
  52. // The types being replaced must be reflected to deserialize old data
  53. serializeContext->RegisterGenericType<DeprecatedMaterialAssignmentId>();
  54. serializeContext->RegisterGenericType<DeprecatedMaterialAssignmentMap>();
  55. serializeContext->Class<MaterialComponentConfig, ComponentConfig>()
  56. ->Version(3, MaterialComponentConfigVersionConverter)
  57. ->Field("materials", &MaterialComponentConfig::m_materials)
  58. ;
  59. }
  60. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  61. {
  62. behaviorContext->Class<MaterialComponentConfig>("MaterialComponentConfig")
  63. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  64. ->Attribute(AZ::Script::Attributes::Category, "render")
  65. ->Attribute(AZ::Script::Attributes::Module, "render")
  66. ->Constructor()
  67. ->Constructor<const MaterialComponentConfig&>()
  68. ->Property("materials", BehaviorValueProperty(&MaterialComponentConfig::m_materials))
  69. ;
  70. }
  71. }
  72. } // namespace Render
  73. } // namespace AZ