StarsComponentController.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "StarsComponentController.h"
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <StarsFeatureProcessor.h>
  11. namespace AZ::Render
  12. {
  13. void StarsComponentConfig::Reflect(AZ::ReflectContext* context)
  14. {
  15. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  16. {
  17. serializeContext->Class<StarsComponentConfig, AZ::ComponentConfig>()
  18. ->Version(0)
  19. ->Field("Exposure", &StarsComponentConfig::m_exposure)
  20. ->Field("RadiusFactor", &StarsComponentConfig::m_radiusFactor)
  21. ->Field("StarsAsset", &StarsComponentConfig::m_starsAsset)
  22. ->Field("TwinkleRate", &StarsComponentConfig::m_twinkleRate)
  23. ;
  24. }
  25. }
  26. void StarsComponentController::Reflect(ReflectContext* context)
  27. {
  28. StarsComponentConfig::Reflect(context);
  29. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  30. {
  31. serializeContext->Class<StarsComponentController>()
  32. ->Version(0)
  33. ->Field("Configuration", &StarsComponentController::m_configuration);
  34. }
  35. }
  36. void StarsComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  37. {
  38. provided.push_back(AZ_CRC("StarsService"));
  39. }
  40. void StarsComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  41. {
  42. incompatible.push_back(AZ_CRC("StarsService"));
  43. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  44. }
  45. void StarsComponentController::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  46. {
  47. dependent.push_back(AZ_CRC("TransformService"));
  48. }
  49. StarsComponentController::StarsComponentController(const StarsComponentConfig& config)
  50. : m_configuration(config)
  51. {
  52. }
  53. void StarsComponentController::Activate(EntityId entityId)
  54. {
  55. RegisterFeatureProcessor(entityId);
  56. TransformNotificationBus::Handler::BusConnect(entityId);
  57. }
  58. void StarsComponentController::RegisterFeatureProcessor(EntityId entityId)
  59. {
  60. RPI::FeatureProcessorFactory::Get()->RegisterFeatureProcessorWithInterface<StarsFeatureProcessor, StarsFeatureProcessorInterface>();
  61. m_scene = RPI::Scene::GetSceneForEntityId(entityId);
  62. if (m_scene)
  63. {
  64. m_starsFeatureProcessor = m_scene->EnableFeatureProcessor<StarsFeatureProcessor>();
  65. }
  66. if (m_starsFeatureProcessor)
  67. {
  68. if (m_configuration.m_starsAsset.IsReady())
  69. {
  70. UpdateStarsFromAsset(m_configuration.m_starsAsset);
  71. }
  72. else
  73. {
  74. OnStarsAssetChanged();
  75. }
  76. if(auto transformInterface = TransformBus::FindFirstHandler(entityId))
  77. {
  78. m_starsFeatureProcessor->SetOrientation(transformInterface->GetWorldRotationQuaternion());
  79. }
  80. }
  81. OnConfigChanged();
  82. }
  83. void StarsComponentController::UnregisterFeatureProcessor()
  84. {
  85. if (m_scene && m_starsFeatureProcessor)
  86. {
  87. m_scene->DisableFeatureProcessor<StarsFeatureProcessor>();
  88. m_starsFeatureProcessor = nullptr;
  89. RPI::FeatureProcessorFactory::Get()->UnregisterFeatureProcessor<StarsFeatureProcessor>();
  90. }
  91. }
  92. void StarsComponentController::OnStarsAssetChanged()
  93. {
  94. Data::AssetBus::MultiHandler::BusDisconnect();
  95. if (m_configuration.m_starsAsset.GetId().IsValid())
  96. {
  97. Data::AssetBus::MultiHandler::BusConnect(m_configuration.m_starsAsset.GetId());
  98. m_configuration.m_starsAsset.QueueLoad();
  99. }
  100. }
  101. void StarsComponentController::OnAssetReady(Data::Asset<Data::AssetData> asset)
  102. {
  103. UpdateStarsFromAsset(asset);
  104. }
  105. void StarsComponentController::OnAssetReloaded(Data::Asset<Data::AssetData> asset)
  106. {
  107. UpdateStarsFromAsset(asset);
  108. }
  109. void StarsComponentController::UpdateStarsFromAsset(Data::Asset<Data::AssetData> asset)
  110. {
  111. if (asset.GetId() != m_configuration.m_starsAsset.GetId())
  112. {
  113. return;
  114. }
  115. m_configuration.m_starsAsset = asset;
  116. StarsAsset *starsAsset = asset.GetAs<StarsAsset>();
  117. if (starsAsset && starsAsset->m_data.size() > StarsAsset::HeaderSize && m_starsFeatureProcessor)
  118. {
  119. Star star{ 0 };
  120. AZStd::array<float,3> position;
  121. constexpr int verticesPerStar{ 6 };
  122. const size_t starsAssetSize = starsAsset->m_data.size();
  123. const size_t starSize = sizeof(star);
  124. const size_t numStars = (starsAssetSize - StarsAsset::HeaderSize) / starSize;
  125. AZStd::vector<StarsFeatureProcessorInterface::StarVertex> stars;
  126. stars.resize(numStars * verticesPerStar);
  127. IO::MemoryStream stream(starsAsset->m_data.data(), starsAssetSize);
  128. // skip the header
  129. stream.Seek(AZ::IO::OffsetType(StarsAsset::HeaderSize), IO::GenericStream::SeekMode::ST_SEEK_BEGIN);
  130. for (uint32_t i = 0; i < numStars; ++i)
  131. {
  132. stream.Read(starSize, &star);
  133. position[0] = -cosf(AZ::DegToRad(star.declination)) * sinf(AZ::DegToRad(star.ascension * 15.0f));
  134. position[1] = cosf(AZ::DegToRad(star.declination)) * cosf(AZ::DegToRad(star.ascension * 15.0f));
  135. position[2] = sinf(AZ::DegToRad(star.declination));
  136. for (int k = 0; k < verticesPerStar; k++)
  137. {
  138. stars[verticesPerStar * i + k].m_position = position;
  139. stars[verticesPerStar * i + k].m_color = (star.magnitude << 24) + (star.blue << 16) + (star.green << 8) + star.red;
  140. }
  141. }
  142. m_starsFeatureProcessor->SetStars(stars);
  143. }
  144. }
  145. void StarsComponentController::Deactivate()
  146. {
  147. TransformNotificationBus::Handler::BusDisconnect();
  148. Data::AssetBus::MultiHandler::BusDisconnect();
  149. UnregisterFeatureProcessor();
  150. }
  151. void StarsComponentController::SetConfiguration(const StarsComponentConfig& config)
  152. {
  153. m_configuration = config;
  154. OnConfigChanged();
  155. }
  156. void StarsComponentController::OnConfigChanged()
  157. {
  158. if (m_starsFeatureProcessor)
  159. {
  160. m_starsFeatureProcessor->SetExposure(m_configuration.m_exposure);
  161. m_starsFeatureProcessor->SetRadiusFactor(m_configuration.m_radiusFactor);
  162. m_starsFeatureProcessor->SetTwinkleRate(m_configuration.m_twinkleRate);
  163. }
  164. }
  165. const StarsComponentConfig& StarsComponentController::GetConfiguration() const
  166. {
  167. return m_configuration;
  168. }
  169. void StarsComponentController::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  170. {
  171. if (m_starsFeatureProcessor)
  172. {
  173. m_starsFeatureProcessor->SetOrientation(world.GetRotation());
  174. }
  175. }
  176. }