UVsRule.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <SceneAPI/SceneData/Rules/UVsRule.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  12. #include <AzCore/Settings/SettingsRegistry.h>
  13. namespace AZ
  14. {
  15. namespace SceneAPI
  16. {
  17. namespace SceneData
  18. {
  19. static constexpr AZStd::string_view DefaultUVsGenerationMethodKeyIfNoRulePresent{
  20. "/O3DE/SceneAPI/UVsGenerateComponent/DefaultGenerationMethodIfNoRulePresent"
  21. };
  22. static constexpr AZStd::string_view DefaultUVsGenerationMethodKeyWhenAddingNewRules{
  23. "/O3DE/SceneAPI/UVsGenerateComponent/DefaultGenerationMethodWhenRuleIsPresent"
  24. };
  25. UVsRule::UVsRule()
  26. : DataTypes::IRule()
  27. {
  28. m_generationMethod = GetDefaultGenerationMethodWhenAddingNewRule();
  29. }
  30. AZ::SceneAPI::DataTypes::UVsGenerationMethod GetGenerationMethodFromRegistry(
  31. AZStd::string_view regKey, AZ::SceneAPI::DataTypes::UVsGenerationMethod defaultValue)
  32. {
  33. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  34. {
  35. AZStd::string stringFromRegistry;
  36. if (settingsRegistry->Get(stringFromRegistry, regKey))
  37. {
  38. const bool isCaseSensitive = false;
  39. if (AZ::StringFunc::Equal(stringFromRegistry, "LeaveSceneDataAsIs", isCaseSensitive))
  40. {
  41. return AZ::SceneAPI::DataTypes::UVsGenerationMethod::LeaveSceneDataAsIs;
  42. }
  43. else if (AZ::StringFunc::Equal(stringFromRegistry, "SphericalProjection", isCaseSensitive))
  44. {
  45. return AZ::SceneAPI::DataTypes::UVsGenerationMethod::SphericalProjection;
  46. }
  47. else
  48. {
  49. AZ_Warning(
  50. AZ::SceneAPI::Utilities::WarningWindow,
  51. false,
  52. "'%s' is not a valid default UV generation method. Check the value of " AZ_STRING_FORMAT " in your "
  53. "settings registry, and change "
  54. "it to 'LeaveSceneDataAsIs' or 'SphericalProjection'",
  55. stringFromRegistry.c_str(),
  56. AZ_STRING_ARG(regKey));
  57. }
  58. }
  59. }
  60. return defaultValue;
  61. }
  62. //! return the default method for when a new rule is explicitly created by script or user
  63. AZ::SceneAPI::DataTypes::UVsGenerationMethod UVsRule::GetDefaultGenerationMethodWhenAddingNewRule()
  64. {
  65. // When someone goes to the effort of actually adding a new rule, make the default actually do something
  66. return GetGenerationMethodFromRegistry(DefaultUVsGenerationMethodKeyWhenAddingNewRules,
  67. AZ::SceneAPI::DataTypes::UVsGenerationMethod::SphericalProjection);
  68. }
  69. AZ::SceneAPI::DataTypes::UVsGenerationMethod UVsRule::GetDefaultGenerationMethodWithNoRule()
  70. {
  71. // when there is no rule on the mesh, do nothing by default
  72. return GetGenerationMethodFromRegistry(DefaultUVsGenerationMethodKeyIfNoRulePresent,
  73. AZ::SceneAPI::DataTypes::UVsGenerationMethod::LeaveSceneDataAsIs);
  74. }
  75. AZ::SceneAPI::DataTypes::UVsGenerationMethod UVsRule::GetGenerationMethod() const
  76. {
  77. return m_generationMethod;
  78. }
  79. bool UVsRule::GetReplaceExisting() const
  80. {
  81. return m_replaceExisting;
  82. }
  83. void UVsRule::Reflect(AZ::ReflectContext* context)
  84. {
  85. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  86. if (!serializeContext)
  87. {
  88. return;
  89. }
  90. serializeContext->Class<UVsRule, DataTypes::IRule>()
  91. ->Version(1)
  92. ->Field("generationMethod", &UVsRule::m_generationMethod)
  93. ->Field("replaceExisting", &UVsRule::m_replaceExisting);
  94. AZ::EditContext* editContext = serializeContext->GetEditContext();
  95. if (editContext)
  96. {
  97. editContext->Class<UVsRule>("UVs", "Specify how UVs are imported or generated.")
  98. ->ClassElement(Edit::ClassElements::EditorData, "")
  99. ->Attribute("AutoExpand", true)
  100. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  101. ->DataElement(
  102. AZ::Edit::UIHandlers::ComboBox,
  103. &AZ::SceneAPI::SceneData::UVsRule::m_generationMethod,
  104. "Generation Method",
  105. "Specify the UVs generation method when UVs are generated.")
  106. ->EnumAttribute(AZ::SceneAPI::DataTypes::UVsGenerationMethod::LeaveSceneDataAsIs, "Do not generate UVs")
  107. ->EnumAttribute(AZ::SceneAPI::DataTypes::UVsGenerationMethod::SphericalProjection, "Spherical Projection")
  108. ->DataElement(0, &AZ::SceneAPI::SceneData::UVsRule::m_replaceExisting,
  109. "Replace existing UVs",
  110. "If true, will replace UVs in the source scene even if present in the incoming data.")
  111. ;
  112. }
  113. }
  114. } // SceneData
  115. } // SceneAPI
  116. } // AZ