StarsComponentController.cpp 7.0 KB

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