EditorImageBasedLightComponent.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <ImageBasedLights/EditorImageBasedLightComponent.h>
  9. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  10. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  11. #include <AzCore/Asset/AssetManager.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  14. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
  15. #include <QApplication>
  16. #include <QMessageBox>
  17. AZ_POP_DISABLE_WARNING
  18. namespace AZ
  19. {
  20. namespace Render
  21. {
  22. void EditorImageBasedLightComponent::Reflect(AZ::ReflectContext* context)
  23. {
  24. BaseClass::Reflect(context);
  25. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  26. {
  27. serializeContext->Class<EditorImageBasedLightComponent, BaseClass>()
  28. ->Version(2, ConvertToEditorRenderComponentAdapter<1>)
  29. ->Field("diffuseImageAsset", &EditorImageBasedLightComponent::m_diffuseImageAsset)
  30. ->Field("specularImageAsset", &EditorImageBasedLightComponent::m_specularImageAsset)
  31. ->Field("exposure", &EditorImageBasedLightComponent::m_exposure)
  32. ;
  33. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  34. {
  35. editContext->Class<EditorImageBasedLightComponent>(
  36. "Global Skylight (IBL)", "Adds image based illumination to the scene")
  37. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  38. ->Attribute(AZ::Edit::Attributes::Category, "Graphics/Lighting")
  39. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  40. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Component_Placeholder.svg")
  41. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  42. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  43. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/atom/global-skylight-ibl/")
  44. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorImageBasedLightComponent::m_diffuseImageAsset, "Diffuse Image", "Cubemap image asset for determining diffuse lighting")
  45. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorImageBasedLightComponent::OnDiffuseImageAssetChanged)
  46. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorImageBasedLightComponent::m_specularImageAsset, "Specular Image", "Cubemap image asset for determining specular lighting")
  47. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorImageBasedLightComponent::OnSpecularImageAssetChanged)
  48. ->DataElement(AZ::Edit::UIHandlers::Slider, &EditorImageBasedLightComponent::m_exposure, "Exposure", "Exposure in stops")
  49. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorImageBasedLightComponent::OnExposureChanged)
  50. ->Attribute(AZ::Edit::Attributes::SoftMin, -5.0f)
  51. ->Attribute(AZ::Edit::Attributes::SoftMax, 5.0f)
  52. ->Attribute(AZ::Edit::Attributes::Min, -20.0f)
  53. ->Attribute(AZ::Edit::Attributes::Max, 20.0f)
  54. ;
  55. editContext->Class<ImageBasedLightComponentController>(
  56. "ImageBasedLightComponentController", "")
  57. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  58. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  59. ->DataElement(AZ::Edit::UIHandlers::Default, &ImageBasedLightComponentController::m_configuration, "Configuration", "")
  60. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  61. ;
  62. }
  63. }
  64. if (auto behaviorContext = azrtti_cast<BehaviorContext*>(context))
  65. {
  66. behaviorContext->Class<EditorImageBasedLightComponent>()->RequestBus("ImageBasedLightComponentRequestBus");
  67. behaviorContext->ConstantProperty("EditorImageBasedLightComponentTypeId", BehaviorConstant(Uuid(EditorImageBasedLightComponentTypeId)))
  68. ->Attribute(AZ::Script::Attributes::Module, "render")
  69. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  70. }
  71. }
  72. EditorImageBasedLightComponent::EditorImageBasedLightComponent(const ImageBasedLightComponentConfig& config)
  73. : BaseClass(config)
  74. {
  75. }
  76. void EditorImageBasedLightComponent::Activate()
  77. {
  78. BaseClass::Activate();
  79. ImageBasedLightComponentConfig& configuration = m_controller.m_configuration;
  80. m_diffuseImageAsset = configuration.m_diffuseImageAsset;
  81. m_specularImageAsset = configuration.m_specularImageAsset;
  82. m_exposure = configuration.m_exposure;
  83. }
  84. AZ::u32 EditorImageBasedLightComponent::OnDiffuseImageAssetChanged()
  85. {
  86. m_controller.SetDiffuseImageAsset(m_diffuseImageAsset);
  87. if (UpdateImageAsset(m_diffuseImageAsset, "_ibldiffuse", "Diffuse",
  88. m_specularImageAsset, "_iblspecular", "Specular"))
  89. {
  90. m_controller.SetSpecularImageAsset(m_specularImageAsset);
  91. }
  92. return AZ::Edit::PropertyRefreshLevels::ValuesOnly;
  93. }
  94. AZ::u32 EditorImageBasedLightComponent::OnSpecularImageAssetChanged()
  95. {
  96. m_controller.SetSpecularImageAsset(m_specularImageAsset);
  97. if (UpdateImageAsset(m_specularImageAsset, "_iblspecular", "Specular",
  98. m_diffuseImageAsset, "_ibldiffuse", "Diffuse"))
  99. {
  100. m_controller.SetDiffuseImageAsset(m_diffuseImageAsset);
  101. }
  102. return AZ::Edit::PropertyRefreshLevels::ValuesOnly;
  103. }
  104. bool EditorImageBasedLightComponent::UpdateImageAsset(
  105. Data::Asset<RPI::StreamingImageAsset>& asset1,
  106. const char* asset1Suffix,
  107. const char* asset1Name,
  108. Data::Asset<RPI::StreamingImageAsset>& asset2,
  109. const char* asset2Suffix,
  110. const char* asset2Name)
  111. {
  112. AZStd::string assetPath;
  113. if (asset1.GetId().IsValid())
  114. {
  115. // retrieve the file path for asset1
  116. AZ::Data::AssetInfo assetInfo;
  117. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, asset1.GetId());
  118. AZ_Assert(assetInfo.m_assetId == asset1.GetId(), "Failed to retrieve AssetInfo for image asset");
  119. assetPath = assetInfo.m_relativePath;
  120. }
  121. AZ::Data::AssetId assetId;
  122. if (!assetPath.empty())
  123. {
  124. // try to load the matching image asset by replacing the asset1 suffix with the asset2 suffix in the file path
  125. AZ::StringFunc::Replace(assetPath, asset1Suffix, asset2Suffix);
  126. assetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(assetPath.c_str(), AZ::RPI::AssetUtils::TraceLevel::None);
  127. if (!assetId.IsValid())
  128. {
  129. // unable to locate the matching image asset
  130. return false;
  131. }
  132. }
  133. if (asset2.GetId().IsValid())
  134. {
  135. // prompt to see if the user wants to overwrite the image asset
  136. AZStd::string message = AZStd::string::format("Update %s image to match the selected %s image?", asset2Name, asset1Name);
  137. if (QMessageBox::question(QApplication::activeWindow(), "Global Skylight", message.c_str(), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
  138. {
  139. return false;
  140. }
  141. }
  142. // create the matching image asset
  143. // Note: this will clear asset2 if asset1 was cleared
  144. asset2.Create(assetId);
  145. return true;
  146. }
  147. AZ::u32 EditorImageBasedLightComponent::OnExposureChanged()
  148. {
  149. m_controller.SetExposure(m_exposure);
  150. return AZ::Edit::PropertyRefreshLevels::ValuesOnly;
  151. }
  152. } // namespace Render
  153. } // namespace AZ