SphereLightDelegate.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <CoreLights/SphereLightDelegate.h>
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <Atom/Feature/CoreLights/PointLightFeatureProcessorInterface.h>
  11. #include <AtomLyIntegration/CommonFeatures/CoreLights/AreaLightComponentConfig.h>
  12. namespace AZ::Render
  13. {
  14. SphereLightDelegate::SphereLightDelegate(LmbrCentral::SphereShapeComponentRequests* shapeBus, EntityId entityId, bool isVisible)
  15. : LightDelegateBase<PointLightFeatureProcessorInterface>(entityId, isVisible)
  16. , m_sphereShapeBus(shapeBus)
  17. {
  18. InitBase(entityId);
  19. }
  20. float SphereLightDelegate::CalculateAttenuationRadius(float lightThreshold) const
  21. {
  22. // Calculate the radius at which the irradiance will be equal to cutoffIntensity.
  23. float intensity = GetPhotometricValue().GetCombinedIntensity(PhotometricUnit::Lumen);
  24. return sqrt(intensity / lightThreshold);
  25. }
  26. void SphereLightDelegate::HandleShapeChanged()
  27. {
  28. if (GetLightHandle().IsValid())
  29. {
  30. GetFeatureProcessor()->SetPosition(GetLightHandle(), GetTransform().GetTranslation());
  31. GetFeatureProcessor()->SetBulbRadius(GetLightHandle(), GetRadius());
  32. }
  33. }
  34. float SphereLightDelegate::GetSurfaceArea() const
  35. {
  36. float radius = GetRadius();
  37. return 4.0f * Constants::Pi * radius * radius;
  38. }
  39. float SphereLightDelegate::GetRadius() const
  40. {
  41. return m_sphereShapeBus->GetRadius() * GetTransform().GetUniformScale();
  42. }
  43. void SphereLightDelegate::DrawDebugDisplay(const Transform& transform, const Color& color, AzFramework::DebugDisplayRequests& debugDisplay, bool isSelected) const
  44. {
  45. if (isSelected)
  46. {
  47. debugDisplay.SetColor(color);
  48. // Draw a sphere for the attenuation radius
  49. debugDisplay.DrawWireSphere(transform.GetTranslation(), GetConfig()->m_attenuationRadius);
  50. }
  51. }
  52. void SphereLightDelegate::SetEnableShadow(bool enabled)
  53. {
  54. Base::SetEnableShadow(enabled);
  55. if (GetLightHandle().IsValid())
  56. {
  57. GetFeatureProcessor()->SetShadowsEnabled(GetLightHandle(), enabled);
  58. }
  59. }
  60. void SphereLightDelegate::SetShadowBias(float bias)
  61. {
  62. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  63. {
  64. GetFeatureProcessor()->SetShadowBias(GetLightHandle(), bias);
  65. }
  66. }
  67. void SphereLightDelegate::SetShadowmapMaxSize(ShadowmapSize size)
  68. {
  69. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  70. {
  71. GetFeatureProcessor()->SetShadowmapMaxResolution(GetLightHandle(), size);
  72. }
  73. }
  74. void SphereLightDelegate::SetShadowFilterMethod(ShadowFilterMethod method)
  75. {
  76. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  77. {
  78. GetFeatureProcessor()->SetShadowFilterMethod(GetLightHandle(), method);
  79. }
  80. }
  81. void SphereLightDelegate::SetFilteringSampleCount(uint32_t count)
  82. {
  83. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  84. {
  85. GetFeatureProcessor()->SetFilteringSampleCount(GetLightHandle(), static_cast<uint16_t>(count));
  86. }
  87. }
  88. void SphereLightDelegate::SetEsmExponent(float esmExponent)
  89. {
  90. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  91. {
  92. GetFeatureProcessor()->SetEsmExponent(GetLightHandle(), esmExponent);
  93. }
  94. }
  95. void SphereLightDelegate::SetNormalShadowBias(float bias)
  96. {
  97. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  98. {
  99. GetFeatureProcessor()->SetNormalShadowBias(GetLightHandle(), bias);
  100. }
  101. }
  102. void SphereLightDelegate::SetShadowCachingMode(AreaLightComponentConfig::ShadowCachingMode cachingMode)
  103. {
  104. if (GetShadowsEnabled() && GetLightHandle().IsValid())
  105. {
  106. GetFeatureProcessor()->SetUseCachedShadows(GetLightHandle(),
  107. cachingMode == AreaLightComponentConfig::ShadowCachingMode::UpdateOnChange);
  108. }
  109. }
  110. void SphereLightDelegate::SetAffectsGI(bool affectsGI)
  111. {
  112. if (GetLightHandle().IsValid())
  113. {
  114. GetFeatureProcessor()->SetAffectsGI(GetLightHandle(), affectsGI);
  115. }
  116. }
  117. void SphereLightDelegate::SetAffectsGIFactor(float affectsGIFactor)
  118. {
  119. if (GetLightHandle().IsValid())
  120. {
  121. GetFeatureProcessor()->SetAffectsGIFactor(GetLightHandle(), affectsGIFactor);
  122. }
  123. }
  124. Aabb SphereLightDelegate::GetLocalVisualizationBounds() const
  125. {
  126. const AZ::Vector3 translationOffset = m_shapeBus ? m_shapeBus->GetTranslationOffset() : AZ::Vector3::CreateZero();
  127. return Aabb::CreateCenterRadius(translationOffset, GetConfig()->m_attenuationRadius);
  128. }
  129. } // namespace AZ::Render