12#include <util/ue-header-guard-begin.h>
13#include "Components/LocalLightComponent.h"
14#include "Components/StaticMeshComponent.h"
15#include "HAL/IConsoleManager.h"
16#include "Materials/MaterialInstanceDynamic.h"
17#include <util/ue-header-guard-end.h>
40 TEXT(
"carla.Light.MinAttenuationRadius"),
42 TEXT(
"Minimum attenuation radius (cm) enforced on the light components of every ")
43 TEXT(
"registered CarlaLight. Set 0 to leave authored radii untouched."),
55 TEXT(
"carla.Light.StreetIntensityScale"),
57 TEXT(
"Multiplier from authored UE4-era STREET light intensities (below 1000, i.e. kilolumens) ")
58 TEXT(
"to lumens, applied to the owner's light components after every blueprint intensity ")
59 TEXT(
"push. Values of 1000 and above are taken as lumens already."),
68 TEXT(
"carla.Light.LegacyIntensityScale"),
70 TEXT(
"Multiplier converting small UE4-era CarlaLight intensities of non-street lights ")
71 TEXT(
"(building, vehicle, other) to UE5 photometric units, applied to the owner's light ")
72 TEXT(
"components after every blueprint intensity push. Set 1 to leave component ")
73 TEXT(
"intensities untouched."),
85UCarlaLight::UCarlaLight()
87 PrimaryComponentTick.bCanEverTick =
false;
90void UCarlaLight::OnRegister()
103void UCarlaLight::BeginPlay()
117void UCarlaLight::RegisterLight()
124 if (GetOwner() !=
nullptr)
126 ActivateAndConfigureLightComponents(GetOwner());
129 ApplyLegacyComponentConversion();
130 ApplyLightAssetDefault();
132 UWorld *World = GetWorld();
133 if (World !=
nullptr)
141 CarlaLightSubsystem->RegisterLight(
this);
147void UCarlaLight::OnUnregister()
149 UWorld* World = GetWorld();
150 if (World !=
nullptr)
154 CarlaLightSubsystem->UnregisterLight(
this);
158 Super::OnUnregister();
161void UCarlaLight::OnComponentDestroyed(
bool bDestroyingHierarchy)
170 UWorld* World = GetWorld();
171 if (World !=
nullptr)
174 CarlaLightSubsystem->UnregisterLight(
this);
178 Super::OnComponentDestroyed(bDestroyingHierarchy);
181void UCarlaLight::EndPlay(
const EEndPlayReason::Type EndPlayReason)
183 UWorld *World = GetWorld();
192 Super::EndPlay(EndPlayReason);
195void UCarlaLight::ApplyLegacyComponentConversion()
197 if (GetOwner() ==
nullptr)
201 ScaleLightComponentIntensities(GetOwner(), LightType, &ConvertedIntensities);
204void UCarlaLight::ActivateAndConfigureLightComponents(
AActor* Owner)
215 if (MinRadius <= 0.0f || Owner ==
nullptr)
219 TArray<ULocalLightComponent*> LightComponents;
220 Owner->GetComponents<ULocalLightComponent>(LightComponents);
221 for (ULocalLightComponent* LightComponent : LightComponents)
223 if (LightComponent->AttenuationRadius < MinRadius)
225 LightComponent->SetAttenuationRadius(MinRadius);
231 LightComponent->SetCastShadows(
false);
235 LightComponent->SetIESTexture(
nullptr);
239 LightComponent->SetMobility(EComponentMobility::Movable);
246 if (!LightComponent->IsActive())
248 LightComponent->SetActive(
true);
253float UCarlaLight::GetLegacyIntensityScale(
ELightType LightType)
260float UCarlaLight::StreetIntensityToLumens(
float Intensity)
269void UCarlaLight::ScaleLightComponentIntensities(
AActor* Owner,
ELightType LightType,
270 TMap<FObjectKey, float>* Converted)
272 if (Owner ==
nullptr)
276 TArray<ULocalLightComponent*> LightComponents;
277 Owner->GetComponents<ULocalLightComponent>(LightComponents);
278 const float Scale = GetLegacyIntensityScale(LightType);
279 for (ULocalLightComponent* LightComponent : LightComponents)
281 const float Current = LightComponent->Intensity;
283 if (Converted !=
nullptr)
285 const float* Written = Converted->Find(LightComponent);
286 if (Written !=
nullptr && *Written == Current)
289 float Result = Current;
293 if (LightComponent->IntensityUnits != ELightUnits::Lumens)
294 LightComponent->SetIntensityUnits(ELightUnits::Lumens);
295 Result = StreetIntensityToLumens(Current);
299 UE_LOG(LogCarla, VeryVerbose, TEXT(
"CarlaLight conversion: owner %s component %s intensity %f visible %d"),
300 *Owner->GetName(), *LightComponent->GetName(), Current, LightComponent->IsVisible() ? 1 : 0);
301 Result = Current * Scale;
303 if (Result != Current)
304 LightComponent->SetIntensity(Result);
305 if (Converted !=
nullptr)
306 Converted->Add(LightComponent, Result);
310void UCarlaLight::ApplyIntensityToComponents(
float Intensity)
320 AActor* Owner = GetOwner();
321 if (Owner ==
nullptr)
325 TArray<ULocalLightComponent*> LightComponents;
326 Owner->GetComponents<ULocalLightComponent>(LightComponents);
330 const float Lumens = StreetIntensityToLumens(Intensity);
331 for (ULocalLightComponent* LightComponent : LightComponents)
333 if (LightComponent->IntensityUnits != ELightUnits::Lumens)
334 LightComponent->SetIntensityUnits(ELightUnits::Lumens);
335 LightComponent->SetIntensity(Lumens);
336 ConvertedIntensities.Add(LightComponent, Lumens);
340 const float Scale = GetLegacyIntensityScale(LightType);
341 for (ULocalLightComponent* LightComponent : LightComponents)
343 LightComponent->SetIntensity(Intensity * Scale);
344 ConvertedIntensities.Add(LightComponent, Intensity * Scale);
348void UCarlaLight::ApplyColorToComponents(FLinearColor Color)
350 AActor* Owner = GetOwner();
351 if (Owner ==
nullptr)
355 TArray<ULocalLightComponent*> LightComponents;
356 Owner->GetComponents<ULocalLightComponent>(LightComponents);
357 for (ULocalLightComponent* LightComponent : LightComponents)
359 LightComponent->SetLightColor(Color);
363void UCarlaLight::SetLightIntensity(
float Intensity)
365 LightIntensity = Intensity;
366 ApplyIntensityToComponents(Intensity);
371 Episode->MarkSceneLightChanged(GetId());
375float UCarlaLight::GetLightIntensity()
const
377 return LightIntensity;
380void UCarlaLight::SetLightColor(FLinearColor Color)
383 ApplyColorToComponents(Color);
387FLinearColor UCarlaLight::GetLightColor()
const
392void UCarlaLight::SetLightOn(
bool bOn)
396 ApplyLightOnToComponents(bOn);
397 ApplyLegacyComponentConversion();
401void UCarlaLight::HandleDayTimeChanged(
bool bIsDay)
403 DayTimeChanged(bIsDay);
427 ApplyEmissiveVisualState(bIsDay ? 0.0f : EmissiveIntensity);
431void UCarlaLight::ApplyLightAssetDefault()
433 AActor* Owner = GetOwner();
434 if (Owner ==
nullptr)
445 bool bIsBuildingClass =
false;
452 SetLightIntensity(
Default.Intensity);
453 SetEmissiveIntensity(
Default.EmissiveIntensity);
456void UCarlaLight::SetEmissiveIntensity(
float Value)
458 EmissiveIntensity = Value;
459 ApplyEmissiveVisualState(Value);
462void UCarlaLight::ApplyEmissiveVisualState(
float Value)
479 ApplyEmissiveToComponents(Value);
480 if (UWorld* World = GetWorld())
487 World->GetTimerManager().ClearTimer(DeferredEmissiveTimerHandle);
488 TWeakObjectPtr<UCarlaLight> WeakThis(
this);
489 DeferredEmissiveTimerHandle = World->GetTimerManager().SetTimerForNextTick([WeakThis, Value]()
491 if (WeakThis.IsValid())
493 WeakThis->ApplyEmissiveToComponents(Value);
499float UCarlaLight::GetEmissiveIntensity()
const
501 return EmissiveIntensity;
504void UCarlaLight::ApplyEmissiveToComponents(
float Value)
506 AActor* Owner = GetOwner();
507 if (Owner ==
nullptr)
514 const float OnOff = (Value > 0.0f) ? 1.0f : 0.0f;
515 TArray<UStaticMeshComponent*> MeshComponents;
516 Owner->GetComponents<UStaticMeshComponent>(MeshComponents);
517 for (UStaticMeshComponent* MeshComponent : MeshComponents)
519 const int32 NumMaterials = MeshComponent->GetNumMaterials();
520 for (int32 MaterialIndex = 0; MaterialIndex < NumMaterials; ++MaterialIndex)
522 if (UMaterialInstanceDynamic* MID = MeshComponent->CreateAndSetMaterialInstanceDynamic(MaterialIndex))
524 MID->SetScalarParameterValue(TEXT(
"EmissiveIntensity"), Value);
525 MID->SetScalarParameterValue(TEXT(
"On/Off"), OnOff);
531void UCarlaLight::ApplyLightOnToComponents(
bool bOn)
533 AActor* Owner = GetOwner();
534 if (Owner ==
nullptr)
538 TArray<ULocalLightComponent*> LightComponents;
539 Owner->GetComponents<ULocalLightComponent>(LightComponents);
540 for (ULocalLightComponent* LightComponent : LightComponents)
542 LightComponent->SetVisibility(bOn);
546bool UCarlaLight::GetLightOn()
const
551void UCarlaLight::SetLightType(
ELightType Type)
585void UCarlaLight::SetLightState(
float Intensity, FLinearColor Color,
ELightType Type,
bool bOn)
587 LightIntensity = Intensity;
592 ApplyLightOnToComponents(bOn);
593 ApplyLegacyComponentConversion();
597FVector UCarlaLight::GetLocation()
const
602 const AActor* Owner = GetOwner();
603 auto Location = Owner ? Owner->GetActorLocation() : FVector::ZeroVector;
613int UCarlaLight::GetId()
const
618void UCarlaLight::SetId(
int InId)
623void UCarlaLight::RecordLightChange()
const
628 auto* Recorder = Episode->GetRecorder();
629 if (Recorder && Recorder->IsEnabled())
631 Recorder->AddEventLightSceneChanged(
this);
633 Episode->MarkSceneLightChanged(GetId());
static TAutoConsoleVariable< float > CVarCarlaLightMinAttenuationRadius(TEXT("carla.Light.MinAttenuationRadius"), 7000.0f, TEXT("registered CarlaLight. Set 0 to leave authored radii untouched."), ECVF_Default)
static constexpr float CarlaLightLegacyStreetThreshold
static constexpr float CarlaLightLegacyStreetDivisor
static constexpr float CarlaLightMaxAuthoredIntensity
static TAutoConsoleVariable< float > CVarCarlaLightLegacyIntensityScale(TEXT("carla.Light.LegacyIntensityScale"), 10000.0f, TEXT("intensities untouched."), ECVF_Default)
static TAutoConsoleVariable< float > CVarCarlaLightStreetIntensityScale(TEXT("carla.Light.StreetIntensityScale"), 1000.0f, TEXT("push. Values of 1000 and above are taken as lumens already."), ECVF_Default)
Base class for the CARLA Game Mode.
ALargeMapManager * GetLMManager() const
FVector LocalToGlobalLocation(const FVector &InLocation) const
void UnregisterLight(UCarlaLight *CarlaLight)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
static FString GetLightClassKey(AActor *Owner, bool &bOutIsBuildingClass)
static bool GetLightDefault(const FString &ClassName, ELightType Group, UPARAM(ref) FLightAssetDefault &OutDefault)