MaterialAssignmentId.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/MaterialAssignmentId.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. bool MaterialAssignmentId::ConvertVersion(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  17. {
  18. if (classElement.GetVersion() < 2)
  19. {
  20. constexpr AZ::u32 materialAssetIdCrc = AZ_CRC_CE("materialAssetId");
  21. AZ::Data::AssetId materialAssetId;
  22. if (!classElement.GetChildData(materialAssetIdCrc, materialAssetId))
  23. {
  24. AZ_Error("AZ::Render::MaterialAssignmentId::ConvertVersion", false, "Failed to get AssetId element");
  25. return false;
  26. }
  27. if (!classElement.RemoveElementByName(materialAssetIdCrc))
  28. {
  29. AZ_Error("AZ::Render::MaterialAssignmentId::ConvertVersion", false, "Failed to remove deprecated element materialAssetId");
  30. // No need to early-return, the object will still load successfully, it will just report more errors about the unrecognized element.
  31. }
  32. if (materialAssetId.IsValid())
  33. {
  34. classElement.AddElementWithData(context, "materialSlotStableId", materialAssetId.m_subId);
  35. }
  36. else
  37. {
  38. classElement.AddElementWithData(context, "materialSlotStableId", RPI::ModelMaterialSlot::InvalidStableId);
  39. }
  40. }
  41. return true;
  42. }
  43. void MaterialAssignmentId::Reflect(ReflectContext* context)
  44. {
  45. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  46. {
  47. serializeContext->Class<MaterialAssignmentId>()
  48. ->Version(2, &MaterialAssignmentId::ConvertVersion)
  49. ->Field("lodIndex", &MaterialAssignmentId::m_lodIndex)
  50. ->Field("materialSlotStableId", &MaterialAssignmentId::m_materialSlotStableId)
  51. ;
  52. if (auto editContext = serializeContext->GetEditContext())
  53. {
  54. editContext->Class<MaterialAssignmentId>(
  55. "Material Assignment Id", "Material Assignment Id")
  56. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  57. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  58. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::Show)
  59. ->DataElement(AZ::Edit::UIHandlers::Default, &MaterialAssignmentId::m_lodIndex, "LOD Index", "")
  60. ->DataElement(AZ::Edit::UIHandlers::Default, &MaterialAssignmentId::m_materialSlotStableId, "Material Slot Stable Id", "")
  61. ;
  62. }
  63. }
  64. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  65. {
  66. behaviorContext->Class<MaterialAssignmentId>("MaterialAssignmentId")
  67. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  68. ->Attribute(AZ::Script::Attributes::Category, "render")
  69. ->Attribute(AZ::Script::Attributes::Module, "render")
  70. ->Constructor()
  71. ->Constructor<const MaterialAssignmentId&>()
  72. ->Constructor<MaterialAssignmentLodIndex, RPI::ModelMaterialSlot::StableId>()
  73. ->Method("IsDefault", &MaterialAssignmentId::IsDefault)
  74. ->Method("IsAssetOnly", &MaterialAssignmentId::IsSlotIdOnly) // Included for compatibility. Use "IsSlotIdOnly" instead.
  75. ->Method("IsLodAndAsset", &MaterialAssignmentId::IsLodAndSlotId) // Included for compatibility. Use "IsLodAndSlotId" instead.
  76. ->Method("IsSlotIdOnly", &MaterialAssignmentId::IsSlotIdOnly)
  77. ->Method("IsLodAndSlotId", &MaterialAssignmentId::IsLodAndSlotId)
  78. ->Method("ToString", &MaterialAssignmentId::ToString)
  79. ->Property("lodIndex", BehaviorValueProperty(&MaterialAssignmentId::m_lodIndex))
  80. ->Property("materialSlotStableId", BehaviorValueProperty(&MaterialAssignmentId::m_materialSlotStableId))
  81. ;
  82. }
  83. }
  84. MaterialAssignmentId::MaterialAssignmentId(MaterialAssignmentLodIndex lodIndex, RPI::ModelMaterialSlot::StableId materialSlotStableId)
  85. : m_lodIndex(lodIndex)
  86. , m_materialSlotStableId(materialSlotStableId)
  87. {
  88. }
  89. MaterialAssignmentId MaterialAssignmentId::CreateDefault()
  90. {
  91. return MaterialAssignmentId(NonLodIndex, RPI::ModelMaterialSlot::InvalidStableId);
  92. }
  93. MaterialAssignmentId MaterialAssignmentId::CreateFromStableIdOnly(RPI::ModelMaterialSlot::StableId materialSlotStableId)
  94. {
  95. return MaterialAssignmentId(NonLodIndex, materialSlotStableId);
  96. }
  97. MaterialAssignmentId MaterialAssignmentId::CreateFromLodAndStableId(
  98. MaterialAssignmentLodIndex lodIndex, RPI::ModelMaterialSlot::StableId materialSlotStableId)
  99. {
  100. return MaterialAssignmentId(lodIndex, materialSlotStableId);
  101. }
  102. bool MaterialAssignmentId::IsDefault() const
  103. {
  104. return m_lodIndex == NonLodIndex && m_materialSlotStableId == RPI::ModelMaterialSlot::InvalidStableId;
  105. }
  106. bool MaterialAssignmentId::IsSlotIdOnly() const
  107. {
  108. return m_lodIndex == NonLodIndex && m_materialSlotStableId != RPI::ModelMaterialSlot::InvalidStableId;
  109. }
  110. bool MaterialAssignmentId::IsLodAndSlotId() const
  111. {
  112. return m_lodIndex != NonLodIndex && m_materialSlotStableId != RPI::ModelMaterialSlot::InvalidStableId;
  113. }
  114. AZStd::string MaterialAssignmentId::ToString() const
  115. {
  116. return AZStd::string::format("%u:%llu", m_materialSlotStableId, m_lodIndex);
  117. }
  118. size_t MaterialAssignmentId::GetHash() const
  119. {
  120. size_t seed = 0;
  121. AZStd::hash_combine(seed, m_lodIndex);
  122. AZStd::hash_combine(seed, m_materialSlotStableId);
  123. return seed;
  124. }
  125. bool MaterialAssignmentId::operator==(const MaterialAssignmentId& rhs) const
  126. {
  127. return m_lodIndex == rhs.m_lodIndex && m_materialSlotStableId == rhs.m_materialSlotStableId;
  128. }
  129. bool MaterialAssignmentId::operator!=(const MaterialAssignmentId& rhs) const
  130. {
  131. return !(*this == rhs);
  132. }
  133. } // namespace Render
  134. } // namespace AZ