123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Atom/RPI.Public/Scene.h>
- namespace AZ
- {
- namespace Render
- {
- template <typename FeatureProcessorType>
- LightDelegateBase<FeatureProcessorType>::LightDelegateBase(EntityId entityId, bool isVisible)
- {
- m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<FeatureProcessorType>(entityId);
- AZ_Error("LightDelegateBase", m_featureProcessor, "Unable to find a %s on the scene.", FeatureProcessorType::RTTI_TypeName());
- if (m_featureProcessor && isVisible)
- {
- m_lightHandle = m_featureProcessor->AcquireLight();
- }
- }
- template <typename FeatureProcessorType>
- LightDelegateBase<FeatureProcessorType>::~LightDelegateBase()
- {
- TransformNotificationBus::Handler::BusDisconnect();
- LmbrCentral::ShapeComponentNotificationsBus::Handler::BusDisconnect();
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->ReleaseLight(m_lightHandle);
- }
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::InitBase(EntityId entityId)
- {
- m_photometricValue.SetEffectiveSolidAngle(GetEffectiveSolidAngle());
- m_shapeBus = LmbrCentral::ShapeComponentRequestsBus::FindFirstHandler(entityId);
- AZ::Transform entityTransform = AZ::Transform::CreateIdentity();
- TransformBus::EventResult(entityTransform, entityId, &TransformBus::Events::GetWorldTM);
- m_transform = ComputeOverallTransform(entityTransform);
- if (m_shapeBus != nullptr)
- {
- LmbrCentral::ShapeComponentNotificationsBus::Handler::BusConnect(entityId);
- OnShapeChanged(ShapeChangeReasons::TransformChanged);
- }
- else if (m_lightHandle.IsValid())
- {
- // Only connect to the transform bus if there's no shape bus, otherwise the shape bus handles transforms.
- TransformNotificationBus::Handler::BusConnect(entityId);
- HandleShapeChanged();
- m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<FeatureProcessorType::PhotometricUnitType>());
- }
- }
-
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetConfig(const AreaLightComponentConfig* config)
- {
- m_componentConfig = config;
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetChroma(const Color& color)
- {
- m_photometricValue.SetChroma(color);
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<FeatureProcessorType::PhotometricUnitType>());
- }
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetIntensity(float intensity)
- {
- m_photometricValue.SetIntensity(intensity);
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<FeatureProcessorType::PhotometricUnitType>());
- }
- }
- template <typename FeatureProcessorType>
- float LightDelegateBase<FeatureProcessorType>::SetPhotometricUnit(PhotometricUnit unit)
- {
- m_photometricValue.SetArea(GetSurfaceArea());
- m_photometricValue.ConvertToPhotometricUnit(unit);
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<FeatureProcessorType::PhotometricUnitType>());
- }
- return m_photometricValue.GetIntensity();
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::OnShapeChanged([[maybe_unused]] ShapeChangeReasons changeReason)
- {
- AZ_Assert(m_shapeBus, "OnShapeChanged called without a shape bus present.");
- // need to update the overall transform whether the change reason is TransformChanged or ShapeChanged, because changes to the
- // translation offset trigger the event with ShapeChanged
- Aabb aabb; // unused, but required for GetTransformAndLocalBounds()
- AZ::Transform entityTransform = AZ::Transform::CreateIdentity();
- m_shapeBus->GetTransformAndLocalBounds(entityTransform, aabb);
- m_transform = ComputeOverallTransform(entityTransform);
- m_photometricValue.SetArea(GetSurfaceArea());
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<FeatureProcessorType::PhotometricUnitType>());
- }
- HandleShapeChanged();
- }
-
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::OnTransformChanged(const Transform& /*local*/, const Transform& world)
- {
- m_transform = ComputeOverallTransform(world);
- HandleShapeChanged();
- }
- template <typename FeatureProcessorType>
- AZ::Transform LightDelegateBase<FeatureProcessorType>::ComputeOverallTransform(const Transform& world)
- {
- const AZ::Vector3 translationOffset = m_shapeBus ? m_shapeBus->GetTranslationOffset() : AZ::Vector3::CreateZero();
- return world * AZ::Transform::CreateTranslation(translationOffset);
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetVisibility(bool isVisible)
- {
- if (m_lightHandle.IsValid() && !isVisible)
- {
- // no longer visible, release light handle
- m_featureProcessor->ReleaseLight(m_lightHandle);
- }
- else if (!m_lightHandle.IsValid() && isVisible && m_featureProcessor)
- {
- // now visible, acquire light handle and update values.
- m_lightHandle = m_featureProcessor->AcquireLight();
- if (m_shapeBus)
- {
- // For lights that get their transform from the shape bus, force an OnShapeChanged to update the transform.
- OnShapeChanged(ShapeChangeReasons::TransformChanged);
- }
- else
- {
- // OnShapeChanged() already calls this for delegates with a shape bus
- HandleShapeChanged();
- }
- }
- }
- template <typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetAttenuationRadius(float radius)
- {
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetAttenuationRadius(m_lightHandle, radius);
- }
- }
- template<typename FeatureProcessorType>
- void LightDelegateBase<FeatureProcessorType>::SetLightingChannelMask(uint32_t lightingChannelMask)
- {
- if (m_lightHandle.IsValid())
- {
- m_featureProcessor->SetLightingChannelMask(m_lightHandle, lightingChannelMask);
- }
- }
- }
- }
|