12#include <util/ue-header-guard-begin.h>
14#include "Async/Async.h"
15#include "Components/StaticMeshComponent.h"
16#include "Engine/DirectionalLight.h"
17#include "Engine/Engine.h"
18#include "Engine/LocalPlayer.h"
19#include "Engine/PostProcessVolume.h"
20#include "Engine/StaticMesh.h"
21#include "Engine/GameViewportClient.h"
22#include "GameFramework/HUD.h"
23#include "InstancedFoliageActor.h"
24#include "Kismet/GameplayStatics.h"
26#include "Scalability.h"
27#include <util/ue-header-guard-end.h>
59 Scalability::ToggleTemporaryQualityLevels(
true);
76 APlayerController *playercontroller = UGameplayStatics::GetPlayerController(InWorld, 0);
79 ULocalPlayer *player = playercontroller->GetLocalPlayer();
83 AHUD *hud = playercontroller->GetHUD();
94 return GEngine->GetWorldFromContextObjectChecked(
this);
107 check(world !=
nullptr);
108 auto GameInstance = Cast<UCarlaGameInstance>(world->GetGameInstance());
109 check(GameInstance !=
nullptr);
116 const float max_draw_distance,
117 const TArray<FStaticMaterial> &road_pieces_materials)
const
123 AsyncTask(ENamedThreads::GameThread, [=]() {
128 TArray<AActor *> actors;
131 for (int32 i = 0; i < actors.Num(); i++)
133 AActor *actor = actors[i];
138 TArray<UStaticMeshComponent *> components;
139 actor->GetComponents(components);
140 for (int32 j = 0; j < components.Num(); j++)
142 UStaticMeshComponent *staticmeshcomponent = Cast<UStaticMeshComponent>(components[j]);
143 if (staticmeshcomponent)
145 staticmeshcomponent->bAllowCullDistanceVolume = (max_draw_distance > 0);
146 staticmeshcomponent->bUseAsOccluder =
false;
147 staticmeshcomponent->LDMaxDrawDistance = max_draw_distance;
148 staticmeshcomponent->CastShadow = (max_draw_distance == 0);
149 if (road_pieces_materials.Num() > 0)
151 TArray<FName> meshslotsnames = staticmeshcomponent->GetMaterialSlotNames();
152 for (int32 k = 0; k < meshslotsnames.Num(); k++)
154 const FName &slotname = meshslotsnames[k];
155 (void)road_pieces_materials.ContainsByPredicate(
156 [staticmeshcomponent, slotname](
const FStaticMaterial &material)
158 if (material.MaterialSlotName.IsEqual(slotname))
160 staticmeshcomponent->SetMaterial(
161 staticmeshcomponent->GetMaterialIndex(slotname),
162 material.MaterialInterface);
180 const float max_draw_distance)
const
186 TArray<UPrimitiveComponent *> components;
187 actor->GetComponents(components,
false);
188 float dist = max_draw_distance;
189 const float maxscale = actor->GetActorScale().GetMax();
194 for (int32 j = 0; j < components.Num(); j++)
196 UPrimitiveComponent *primitivecomponent = Cast<UPrimitiveComponent>(components[j]);
197 if (
IsValid(primitivecomponent))
199 primitivecomponent->SetCullDistance(dist);
200 primitivecomponent->bAllowCullDistanceVolume = dist > 0;
214 AsyncTask(ENamedThreads::GameThread, [=,
this]() {
219 TArray<AActor *> actors;
221 UGameplayStatics::GetAllActorsOfClass(world, AActor::StaticClass(), actors);
222 for (int32 i = 0; i < actors.Num(); i++)
224 AActor *actor = actors[i];
226 actor->IsA<AInstancedFoliageActor>() ||
228 actor->IsA<ALandscape>() ||
241 TArray<AActor *> actors;
242 UGameplayStatics::GetAllActorsOfClass(world, APostProcessVolume::StaticClass(), actors);
243 for (int32 i = 0; i < actors.Num(); i++)
245 AActor *actor = actors[i];
250 APostProcessVolume *postprocessvolume = Cast<APostProcessVolume>(actor);
251 if (postprocessvolume)
253 postprocessvolume->bEnabled = enabled;
260 const float max_distance_fade,
261 const bool cast_shadows,
262 const bool hide_non_directional)
const
268 AsyncTask(ENamedThreads::GameThread, [=]() {
273 TArray<AActor *> actors;
274 UGameplayStatics::GetAllActorsOfClass(world, ALight::StaticClass(), actors);
275 for (int32 i = 0; i < actors.Num(); i++)
282 ADirectionalLight *directionallight = Cast<ADirectionalLight>(actors[i]);
283 if (directionallight)
285 directionallight->SetCastShadows(cast_shadows);
286 directionallight->SetLightFunctionFadeDistance(max_distance_fade);
290 actors[i]->SetActorHiddenInGame(hide_non_directional);
298 const float light_fade_distance,
299 const bool cast_directional_shadows,
300 const bool hide_non_directional_lights,
301 const float draw_distance)
const
307 AsyncTask(ENamedThreads::GameThread, [=,
this]() {
312 TArray<AActor *> actors;
313 UGameplayStatics::GetAllActorsOfClass(world, AActor::StaticClass(), actors);
314 for (int32 i = 0; i < actors.Num(); i++)
316 AActor *actor = actors[i];
322 if (ADirectionalLight *directional = Cast<ADirectionalLight>(actor))
324 directional->SetCastShadows(cast_directional_shadows);
325 directional->SetLightFunctionFadeDistance(light_fade_distance);
328 if (actor->IsA<ALight>())
330 actor->SetActorHiddenInGame(hide_non_directional_lights);
334 if (actor->IsA<AInstancedFoliageActor>() ||
335 actor->IsA<ALandscape>() ||
static constexpr float CARLA_SETTINGS_MAX_SCALE_SIZE
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
void SetAllRoads(UWorld *world, float max_draw_distance, const TArray< FStaticMaterial > &road_pieces_materials) const
void SetPostProcessEffectsEnabled(UWorld *world, bool enabled) const
void ApplyQualityLevelPreRestart()
Before loading a level, apply the current settings.
void ApplyQualityLevelPostRestart()
After loading a level, apply the current settings.
void Reset()
Reset settings to default.
UCarlaSettings * CarlaSettings
void CheckCarlaSettings(UWorld *world)
Check that the world, instance and settings are valid and save the CarlaSettings instance.
void ApplyPerActorQualitySettings(UWorld *world, float light_fade_distance, bool cast_directional_shadows, bool hide_non_directional_lights, float draw_distance) const
Single-pass replacement for SetAllLights + SetAllActorsDrawDistance when applying a quality tier.
void SetAllActorsDrawDistance(UWorld *world, float max_draw_distance) const
void RegisterSpawnHandler(UWorld *World)
Create the event trigger handler for all the newly spawned actors to be processed with a custom funct...
static EQualityLevel AppliedLowPostResetQualityLevel
Currently applied quality level after level is restarted.
void SetAllLights(UWorld *world, float max_distance_fade, bool cast_shadows, bool hide_non_directional) const
void SetActorComponentsDrawDistance(AActor *actor, float max_draw_distance) const
bool bDisableRendering
Enable or disable the viewport rendering of the world.
EQualityLevel GetQualityLevel() const
static const FName CARLA_SKY_TAG
CARLA_SKY name to tag the sky sphere (BPS) actors in the scenes.
static const FName CARLA_ROAD_TAG
CARLA_ROAD name to tag road mesh actors.