EditorStarsComponent.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "EditorStarsComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. #include <StarsFeatureProcessor.h>
  12. namespace AZ::Render
  13. {
  14. void EditorStarsComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. BaseClass::Reflect(context);
  17. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<EditorStarsComponent, BaseClass>()
  20. ->Version(1)
  21. ;
  22. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  23. {
  24. editContext->Class<StarsComponentConfig>("Stars Config", "Star Config Data")
  25. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  26. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZStd::vector<AZ::Crc32>({ AZ_CRC_CE("Game") }))
  27. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  28. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  29. ->DataElement(AZ::Edit::UIHandlers::Slider, &StarsComponentConfig::m_exposure, "Exposure", "Exposure")
  30. ->Attribute(AZ::Edit::Attributes::SoftMin, 0.0f)
  31. ->Attribute(AZ::Edit::Attributes::SoftMax, 10.0f)
  32. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  33. ->Attribute(AZ::Edit::Attributes::Max, 32.0f)
  34. ->DataElement(AZ::Edit::UIHandlers::Slider, &StarsComponentConfig::m_radiusFactor, "Radius factor", "Star radius factor")
  35. ->Attribute(AZ::Edit::Attributes::SoftMin, 0.0f)
  36. ->Attribute(AZ::Edit::Attributes::SoftMax, 10.0f)
  37. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  38. ->Attribute(AZ::Edit::Attributes::Max, 64.0f)
  39. ->DataElement(AZ::Edit::UIHandlers::Slider, &StarsComponentConfig::m_twinkleRate, "Twinkle rate", "How quickly the stars twinkle")
  40. ->Attribute(AZ::Edit::Attributes::SoftMin, 0.0f)
  41. ->Attribute(AZ::Edit::Attributes::SoftMax, 3.0f)
  42. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  43. ->Attribute(AZ::Edit::Attributes::Max, 10.0f)
  44. ->DataElement(AZ::Edit::UIHandlers::Default, &StarsComponentConfig::m_starsAsset, "Stars Asset", "Stars asset")
  45. ;
  46. editContext->Class<StarsComponentController>(
  47. "StarsComponentController", "")
  48. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  49. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  50. ->DataElement(AZ::Edit::UIHandlers::Default, &StarsComponentController::m_configuration, "Configuration", "")
  51. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  52. ;
  53. editContext->Class<EditorStarsComponent>(
  54. "Stars", "Renders stars in the background")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  56. ->Attribute(AZ::Edit::Attributes::Category, "Graphics/Environment")
  57. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  58. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Component_Placeholder.svg")
  59. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"));
  60. }
  61. }
  62. }
  63. void EditorStarsComponent::Init()
  64. {
  65. StarsComponentConfig& config = m_controller.m_configuration;
  66. // prefill with the default stars asset if no other is specified
  67. if (!config.m_starsAsset.GetId().IsValid())
  68. {
  69. Data::AssetId assetId;
  70. const auto type = azrtti_typeid<StarsAsset>();
  71. const auto path = m_defaultAssetPath.c_str();
  72. Data::AssetCatalogRequestBus::BroadcastResult( assetId, &Data::AssetCatalogRequests::GetAssetIdByPath, path, type, false);
  73. if (assetId.IsValid())
  74. {
  75. config.m_starsAsset = Data::AssetManager::Instance().FindOrCreateAsset<StarsAsset>(assetId, Data::AssetLoadBehavior::Default);
  76. }
  77. }
  78. // remember the stars asset id so we can detect when it changes
  79. m_prevAssetId = config.m_starsAsset.GetId();
  80. }
  81. u32 EditorStarsComponent::OnConfigurationChanged()
  82. {
  83. if (m_prevAssetId != m_controller.GetConfiguration().m_starsAsset.GetId())
  84. {
  85. m_controller.OnStarsAssetChanged();
  86. m_prevAssetId = m_controller.GetConfiguration().m_starsAsset.GetId();
  87. }
  88. m_controller.OnConfigChanged();
  89. return Edit::PropertyRefreshLevels::AttributesAndValues;
  90. }
  91. void EditorStarsComponent::OnEntityVisibilityChanged(bool visibility)
  92. {
  93. if (visibility)
  94. {
  95. m_controller.EnableFeatureProcessor(GetEntityId());
  96. }
  97. else
  98. {
  99. m_controller.DisableFeatureProcessor();
  100. }
  101. }
  102. }