Change in draw distances for CarlaSettings

This commit is contained in:
juaxix 2018-02-28 16:44:05 +01:00
parent 39f7fc9d25
commit 3aecac81bc
4 changed files with 208 additions and 130 deletions

View File

@ -10,8 +10,7 @@
#include "Components/InstancedStaticMeshComponent.h" #include "Components/InstancedStaticMeshComponent.h"
#include "Engine/StaticMesh.h" #include "Engine/StaticMesh.h"
#include <vector> #include <vector>
#include "Settings/CarlaSettings.h"
using tag_size_t = std::underlying_type<ECityMapMeshTag>::type; using tag_size_t = std::underlying_type<ECityMapMeshTag>::type;
@ -141,7 +140,7 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
staticmeshcomponent->SetStaticMesh(instantiator.GetStaticMesh()); staticmeshcomponent->SetStaticMesh(instantiator.GetStaticMesh());
//occlussion //occlussion
StaticMeshActor->Tags.Add(FName("CARLA_ROAD")); StaticMeshActor->Tags.Add(UCarlaSettings::CARLA_ROAD_TAG);
staticmeshcomponent->bAllowCullDistanceVolume=true; staticmeshcomponent->bAllowCullDistanceVolume=true;
staticmeshcomponent->bUseAsOccluder=false; staticmeshcomponent->bUseAsOccluder=false;
staticmeshcomponent->LDMaxDrawDistance = MaxDrawDistance; staticmeshcomponent->LDMaxDrawDistance = MaxDrawDistance;
@ -173,15 +172,13 @@ void ACityMapMeshHolder::ResetInstantiators()
instantiator.SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i))); instantiator.SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i)));
} }
const FName roadtag = FName("CARLA_ROAD");
TArray<AActor*> roadpieces; TArray<AActor*> roadpieces;
GetAttachedActors(roadpieces); GetAttachedActors(roadpieces);
for(int32 i=roadpieces.Num()-1; i>=0; i--) for(int32 i=roadpieces.Num()-1; i>=0; i--)
{ {
if(roadpieces[i]->Tags.Contains(UCarlaSettings::CARLA_ROAD_TAG))
//UE_LOG(LogTemp,Display,TEXT("ACityMapMeshHolder::ResetInstantiators Destroyed %s"),*Children[i]->GetName());
if(roadpieces[i]->Tags.Contains(roadtag))
{ {
roadpieces[i]->Destroy(); roadpieces[i]->Destroy();
} }

View File

@ -25,7 +25,7 @@ static auto MakeCharBuffer(const FString &String)
{ {
const char *Ptr = TCHAR_TO_UTF8(*String); const char *Ptr = TCHAR_TO_UTF8(*String);
auto Buffer = MakeUnique<char[]>(std::strlen(Ptr) + 1u); // + null terminator. auto Buffer = MakeUnique<char[]>(std::strlen(Ptr) + 1u); // + null terminator.
#if _WIN32 #if defined(_WIN32)
strcpy_s(Buffer.Get(),String.Len()+1, Ptr); strcpy_s(Buffer.Get(),String.Len()+1, Ptr);
#else #else
std::strcpy(Buffer.Get(), Ptr); std::strcpy(Buffer.Get(), Ptr);

View File

@ -6,7 +6,6 @@
#include "Carla.h" #include "Carla.h"
#include "CarlaSettings.h" #include "CarlaSettings.h"
#include "DynamicWeather.h" #include "DynamicWeather.h"
#include "Settings/CameraDescription.h" #include "Settings/CameraDescription.h"
#include "Settings/LidarDescription.h" #include "Settings/LidarDescription.h"
@ -19,7 +18,8 @@
#include "Engine/DirectionalLight.h" #include "Engine/DirectionalLight.h"
#include "Engine/PointLight.h" #include "Engine/PointLight.h"
//#include "Landscape.h" //--> needs Lanscape module in the build cs //#include "Landscape.h" //--> needs Lanscape module in the build cs
#include "Components/StaticMeshComponent.h" #include "Engine/StaticMesh.h"
#include "Materials/MaterialInstance.h"
// INI file sections. // INI file sections.
@ -28,6 +28,12 @@
#define S_CARLA_SENSOR TEXT("CARLA/Sensor") #define S_CARLA_SENSOR TEXT("CARLA/Sensor")
#define S_CARLA_QUALITYSETTINGS TEXT("CARLA/QualitySettings") #define S_CARLA_QUALITYSETTINGS TEXT("CARLA/QualitySettings")
// =============================================================================
// -- Static variables & constants ---------------------------------------------
// =============================================================================
const FName UCarlaSettings::CARLA_ROAD_TAG = FName("CARLA_ROAD");
// ============================================================================= // =============================================================================
// -- Static methods ----------------------------------------------------------- // -- Static methods -----------------------------------------------------------
// ============================================================================= // =============================================================================
@ -118,7 +124,10 @@ static void LoadSettingsFromConfig(
ConfigFile.GetString(S_CARLA_QUALITYSETTINGS, TEXT("DefaultLevel"), sDefaultLevel); ConfigFile.GetString(S_CARLA_QUALITYSETTINGS, TEXT("DefaultLevel"), sDefaultLevel);
if(!Settings.SetQualitySettingsLevel(FQualitySettings::FromString(sDefaultLevel))) if(!Settings.SetQualitySettingsLevel(FQualitySettings::FromString(sDefaultLevel)))
{ {
///apply pre-restart.... @todo //error
} else
{
//apply pre-restart....
} }
@ -186,121 +195,11 @@ bool UCarlaSettings::SetQualitySettingsLevel(EQualitySettingsLevel newDefaultLev
void UCarlaSettings::ApplyQualitySettingsLevelPostRestart() const void UCarlaSettings::ApplyQualitySettingsLevelPostRestart() const
{ {
UWorld *world = GetWorld();
if(!world) return ;
//set the quality settings now
switch(DefaultQualitySettingsLevel) switch(DefaultQualitySettingsLevel)
{ {
case EQualitySettingsLevel::Low: {//r.SSR.qualitylaunch commands to lower quality settings case EQualitySettingsLevel::Low:
world->Exec(world,TEXT("r.DefaultFeature.MotionBlur 0")); ApplyLowQualitySettings();
world->Exec(world,TEXT("r.DefaultFeature.Bloom 0"));
world->Exec(world,TEXT("r.DefaultFeature.AmbientOcclusion 0"));
world->Exec(world,TEXT("r.AmbientOcclusionLevels 0"));
world->Exec(world,TEXT("r.DefaultFeature.AmbientOcclusionStaticFraction 0"));
world->Exec(world,TEXT("r.DefaultFeature.AutoExposure 0"));
world->Exec(world,TEXT("r.RHICmdBypass 0"));
world->Exec(world,TEXT("r.DefaultFeature.AntiAliasing 2"));
world->Exec(world,TEXT("r.Streaming.PoolSize 2000"));
world->Exec(world,TEXT("r.HZBOcclusion 0"));
world->Exec(world,TEXT("r.MinScreenRadiusForLights 0.01"));
world->Exec(world,TEXT("r.SeparateTranslucency 0"));
world->Exec(world,TEXT("r.FinishCurrentFrame 0"));
world->Exec(world,TEXT("r.MotionBlurQuality 0"));
world->Exec(world,TEXT("r.PostProcessAAQuality 0"));
world->Exec(world,TEXT("r.BloomQuality 1"));
world->Exec(world,TEXT("r.SSR.Quality 0"));
world->Exec(world,TEXT("r.DepthOfFieldQuality 0"));
world->Exec(world,TEXT("r.SceneColorFormat 2"));
world->Exec(world,TEXT("r.TranslucencyVolumeBlur 0"));
world->Exec(world,TEXT("r.TranslucencyLightingVolumeDim 4"));
world->Exec(world,TEXT("r.MaxAnisotropy 8"));
world->Exec(world,TEXT("r.LensFlareQuality 0"));
world->Exec(world,TEXT("r.SceneColorFringeQuality 0"));
world->Exec(world,TEXT("r.FastBlurThreshold 0"));
world->Exec(world,TEXT("r.SSR.MaxRoughness 0.1"));
world->Exec(world,TEXT("r.AllowOcclusionQueries 1"));
world->Exec(world,TEXT("r.SSR 0"));
world->Exec(world,TEXT("r.StencilForLODDither 1"));
world->Exec(world,TEXT("r.EarlyZPass 1"));
world->Exec(world,TEXT("r.EarlyZPassMovable 1"));
world->Exec(world,TEXT("r.Foliage.DitheredLOD 0"));
world->Exec(world,TEXT("r.ForwardShading 0"));
world->Exec(world,TEXT("sg.PostProcessQuality 0"));
world->Exec(world,TEXT("r.ViewDistanceScale 0.1"));
world->Exec(world,TEXT("sg.ShadowQuality 0"));
world->Exec(world,TEXT("sg.TextureQuality 0"));
world->Exec(world,TEXT("sg.EffectsQuality 0"));
world->Exec(world,TEXT("FoliageQuality 0"));
world->Exec(world,TEXT("foliage.DensityScale 0"));
world->Exec(world,TEXT("grass.DensityScale 0"));
world->Exec(world,TEXT("r.TranslucentLightingVolume 0"));
world->Exec(world,TEXT("r.LightShaftDownSampleFactor 4"));
world->Exec(world,TEXT("r.OcclusionQueryLocation 1"));
world->Exec(world,TEXT("r.BasePassOutputsVelocity 0"));
//world->Exec(world,TEXT("r.DetailMode 0")); //-->will change to lods 0
//iterate all vehicles and people, set the draw distance
TArray<AActor*> actors;
int32 i;
//iterate all terrain, deactivate the actor
/*UGameplayStatics::GetAllActorsOfClass(world, ALandscape::StaticClass(), actors);
for(i=0; i<actors.Num(); i++)
{
ALandscape* landscape = Cast<ALandscape>(actors[i]);
if(landscape)
{
landscape->DisableComponentsSimulatePhysics();
landscape->bUseLandscapeForCullingInvisibleHLODVertices=true;
}
}*/
//iterate all directional lights, deactivate shadows
UGameplayStatics::GetAllActorsOfClass(world, ALight::StaticClass(), actors);
for(i=0;i<actors.Num();i++)
{
//tweak directional lights
ADirectionalLight* directionallight = Cast<ADirectionalLight>(actors[i]);
if(directionallight)
{
directionallight->SetCastShadows(false);
directionallight->SetLightFunctionFadeDistance(LowLightFadeDistance);
continue;
}
//disable point lights
/*
APointLight* pointlight = Cast<APointLight>(actors[i]);
if(pointlight)
{
actors[i]->SetActorHiddenInGame(true);
}
*/
//disable any other type of light
actors[i]->SetActorHiddenInGame(true);
}
//Set all the roads the low quality material
/*UGameplayStatics::GetAllActorsWithTag(world, FName("CARLA_ROAD"),actors);
for(i=0; i<actors.Num(); i++)
{
TArray<UActorComponent*> components = actors[i]->GetComponentsByClass(UStaticMeshComponent::StaticClass());
for(int32 j=0; j<components.Num(); j++)
{
UStaticMeshComponent* staticmesh = Cast<UStaticMeshComponent>(components[j]);
if(staticmesh)
{
for(int32 k=0; k<staticmesh->GetNumMaterials(); k++)
{
if(!RoadMaterials.IsValidIndex(k)) break;
staticmesh->SetMaterial(k, Cast<UMaterialInterface>(RoadMaterials[k]));
}
}
}
}*/
break; break;
}
case EQualitySettingsLevel::Medium: break; case EQualitySettingsLevel::Medium: break;
case EQualitySettingsLevel::High: break; case EQualitySettingsLevel::High: break;
case EQualitySettingsLevel::Epic: break; case EQualitySettingsLevel::Epic: break;
@ -431,6 +330,156 @@ void UCarlaSettings::ResetSensorDescriptions()
bSemanticSegmentationEnabled = false; bSemanticSegmentationEnabled = false;
} }
void UCarlaSettings::LaunchLowQualityCommands(UWorld * world) const
{
//launch commands to lower quality settings
world->Exec(world,TEXT("r.DefaultFeature.MotionBlur 0"));
world->Exec(world,TEXT("r.DefaultFeature.Bloom 0"));
world->Exec(world,TEXT("r.DefaultFeature.AmbientOcclusion 0"));
world->Exec(world,TEXT("r.AmbientOcclusionLevels 0"));
world->Exec(world,TEXT("r.DefaultFeature.AmbientOcclusionStaticFraction 0"));
world->Exec(world,TEXT("r.DefaultFeature.AutoExposure 0"));
world->Exec(world,TEXT("r.RHICmdBypass 0"));
world->Exec(world,TEXT("r.DefaultFeature.AntiAliasing 2"));
world->Exec(world,TEXT("r.Streaming.PoolSize 2000"));
world->Exec(world,TEXT("r.HZBOcclusion 0"));
world->Exec(world,TEXT("r.MinScreenRadiusForLights 0.01"));
world->Exec(world,TEXT("r.SeparateTranslucency 0"));
world->Exec(world,TEXT("r.FinishCurrentFrame 0"));
world->Exec(world,TEXT("r.MotionBlurQuality 0"));
world->Exec(world,TEXT("r.PostProcessAAQuality 0"));
world->Exec(world,TEXT("r.BloomQuality 1"));
world->Exec(world,TEXT("r.SSR.Quality 0"));
world->Exec(world,TEXT("r.DepthOfFieldQuality 0"));
world->Exec(world,TEXT("r.SceneColorFormat 2"));
world->Exec(world,TEXT("r.TranslucencyVolumeBlur 0"));
world->Exec(world,TEXT("r.TranslucencyLightingVolumeDim 4"));
world->Exec(world,TEXT("r.MaxAnisotropy 8"));
world->Exec(world,TEXT("r.LensFlareQuality 0"));
world->Exec(world,TEXT("r.SceneColorFringeQuality 0"));
world->Exec(world,TEXT("r.FastBlurThreshold 0"));
world->Exec(world,TEXT("r.SSR.MaxRoughness 0.1"));
world->Exec(world,TEXT("r.AllowOcclusionQueries 1"));
world->Exec(world,TEXT("r.SSR 0"));
world->Exec(world,TEXT("r.StencilForLODDither 1"));
world->Exec(world,TEXT("r.EarlyZPass 2")); //transparent before opaque
world->Exec(world,TEXT("r.EarlyZPassMovable 1"));
world->Exec(world,TEXT("r.Foliage.DitheredLOD 0"));
world->Exec(world,TEXT("r.ForwardShading 0"));
world->Exec(world,TEXT("sg.PostProcessQuality 0"));
world->Exec(world,TEXT("r.ViewDistanceScale 0.1"));
world->Exec(world,TEXT("sg.ShadowQuality 0"));
world->Exec(world,TEXT("sg.TextureQuality 0"));
world->Exec(world,TEXT("sg.EffectsQuality 0"));
world->Exec(world,TEXT("FoliageQuality 0"));
world->Exec(world,TEXT("foliage.DensityScale 0"));
world->Exec(world,TEXT("grass.DensityScale 0"));
world->Exec(world,TEXT("r.TranslucentLightingVolume 0"));
world->Exec(world,TEXT("r.LightShaftDownSampleFactor 4"));
world->Exec(world,TEXT("r.OcclusionQueryLocation 1"));
world->Exec(world,TEXT("r.BasePassOutputsVelocity 0"));
//world->Exec(world,TEXT("r.DetailMode 0")); //-->will change to lods 0
}
void UCarlaSettings::SetAllLightsLowQuality(UWorld* world) const
{
TArray<AActor*> actors;
UGameplayStatics::GetAllActorsOfClass(world, ALight::StaticClass(), actors);
for(int32 i=0;i<actors.Num();i++)
{
//tweak directional lights
ADirectionalLight* directionallight = Cast<ADirectionalLight>(actors[i]);
if(directionallight)
{
directionallight->SetCastShadows(false);
directionallight->SetLightFunctionFadeDistance(LowLightFadeDistance);
continue;
}
//disable any other type of light
actors[i]->SetActorHiddenInGame(true);
}
}
void UCarlaSettings::SetAllRoadsLowQuality(UWorld* world) const
{
TArray<AActor*> actors;
UGameplayStatics::GetAllActorsWithTag(world, CARLA_ROAD_TAG,actors);
for(int32 i=0; i<actors.Num(); i++)
{
TArray<UActorComponent*> components = actors[i]->GetComponentsByClass(UStaticMeshComponent::StaticClass());
for(int32 j=0; j<components.Num(); j++)
{
UStaticMeshComponent* staticmesh = Cast<UStaticMeshComponent>(components[j]);
if(staticmesh)
{
for(int32 k=0; k<staticmesh->GetNumMaterials(); k++)
{
if(LowRoadMaterials.IsValidIndex(k) && LowRoadMaterials[k].MaterialInterface && LowRoadMaterials[k].MaterialInterface->IsValidLowLevel())
{
staticmesh->SetMaterial(k, LowRoadMaterials[k].MaterialInterface);
}
}
}
}
}
}
void UCarlaSettings::SetAllActorsDrawDistance(UWorld* world, const float static_mesh_max_draw_distance) const
{
///@TODO: use semantics to grab all actors by type (vehicles,ground,people,props) and set different distances configured in the global properties
TArray<AActor*> actors;
#define _MAX_SCALE_SIZE 50.0f
//set the lower quality - max draw distance
UGameplayStatics::GetAllActorsOfClass(world, AActor::StaticClass(),actors);
for(int32 i=0; i<actors.Num(); i++)
{
if(!IsValid(actors[i]) || actors[i]->IsPendingKillPending() || actors[i]->ActorHasTag(CARLA_ROAD_TAG)) continue;
TArray<UActorComponent*> components = actors[i]->GetComponentsByClass(UStaticMeshComponent::StaticClass());
float dist = static_mesh_max_draw_distance;
const float maxscale = actors[i]->GetActorScale().GetMax();
if(maxscale>_MAX_SCALE_SIZE) dist *= 100.0f;
for(int32 j=0; j<components.Num(); j++)
{
UStaticMeshComponent* staticmeshcomp = Cast<UStaticMeshComponent>(components[j]);
if(IsValid(staticmeshcomp))
{
staticmeshcomp->SetCullDistance(dist);
}
}
}
}
void UCarlaSettings::ApplyLowQualitySettings() const
{
UWorld *world = GetWorld();
if(!world) return ;
LaunchLowQualityCommands(world);
//iterate all terrain, deactivate the actor
/*UGameplayStatics::GetAllActorsOfClass(world, ALandscape::StaticClass(), actors);
for(i=0; i<actors.Num(); i++)
{
ALandscape* landscape = Cast<ALandscape>(actors[i]);
if(landscape)
{
landscape->DisableComponentsSimulatePhysics();
landscape->bUseLandscapeForCullingInvisibleHLODVertices=true;
}
}*/
//iterate all directional lights, deactivate shadows
SetAllLightsLowQuality(world);
//Set all the roads the low quality material
SetAllRoadsLowQuality(world);
//Set all actors with static meshes a max disntace configured in the global settings for the low quality
SetAllActorsDrawDistance(world, LowStaticMeshMaxDrawDistance);
}
void UCarlaSettings::LoadSettingsFromFile(const FString &FilePath, const bool bLogOnFailure) void UCarlaSettings::LoadSettingsFromFile(const FString &FilePath, const bool bLogOnFailure)
{ {
if (FPaths::FileExists(FilePath)) { if (FPaths::FileExists(FilePath)) {

View File

@ -36,7 +36,7 @@ public:
}; };
class USensorDescription; class USensorDescription;
class UMaterial; struct FStaticMaterial;
/** Global settings for CARLA. /** Global settings for CARLA.
* Setting object used to hold both config settings and editable ones in one place * Setting object used to hold both config settings and editable ones in one place
* To ensure the settings are saved to the specified config file make sure to add * To ensure the settings are saved to the specified config file make sure to add
@ -84,6 +84,7 @@ public:
return nullptr; return nullptr;
} }
// Special overload for blueprints. // Special overload for blueprints.
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void GetActiveWeatherDescription( void GetActiveWeatherDescription(
@ -92,9 +93,25 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
const FWeatherDescription &GetWeatherDescriptionByIndex(int32 Index); const FWeatherDescription &GetWeatherDescriptionByIndex(int32 Index);
///----------- constants ------------------
public:
/**
* CARLA_ROAD name to tag road mesh actors
*/
static const FName CARLA_ROAD_TAG;
private: private:
/***/
void LaunchLowQualityCommands(UWorld* world) const;
/***/
void SetAllLightsLowQuality(UWorld* world) const;
/***/
void SetAllRoadsLowQuality(UWorld* world) const;
/***/
void SetAllActorsDrawDistance(UWorld* world, const float static_mesh_max_draw_distance) const;
/***/
void ApplyLowQualitySettings() const;
/***/
void LoadSettingsFromFile(const FString &FilePath, bool bLogOnFailure); void LoadSettingsFromFile(const FString &FilePath, bool bLogOnFailure);
void ResetSensorDescriptions(); void ResetSensorDescriptions();
@ -104,7 +121,7 @@ private:
FString CurrentFileName; FString CurrentFileName;
// =========================================================================== // ===========================================================================
/// @name CARLA Server /// @name CARLA Server
// =========================================================================== // ===========================================================================
/// @{ /// @{
@ -180,13 +197,28 @@ private:
EQualitySettingsLevel DefaultQualitySettingsLevel = EQualitySettingsLevel::None; EQualitySettingsLevel DefaultQualitySettingsLevel = EQualitySettingsLevel::None;
public: public:
UPROPERTY(Category = "Quality Settings", EditAnywhere, config) /** @TODO : Move Low quality vars to a generic map of structs with the quality level as key*/
TArray<TSubclassOf<UMaterial>> RoadMaterials;
/** Low quality Road Materials.
* Using the index of array it will assign to the material with the same index in the mesh for each piece of road
* @TODO: Use slots name to set it for each part of the road
*/
UPROPERTY(Category = "Quality Settings/Low", EditAnywhere, config, DisplayName="Road Materials List for Low Quality")
TArray<FStaticMaterial> LowRoadMaterials;
//distances //distances
/**
* Distance at which the light function should be completely faded to DisabledBrightness.
* This is useful for hiding aliasing from light functions applied in the distance.
*/
UPROPERTY(Category = "Quality Settings/Low", EditAnywhere, config) UPROPERTY(Category = "Quality Settings/Low", EditAnywhere, config)
float LowLightFadeDistance; float LowLightFadeDistance = 1000.0f;
/**
*
*/
UPROPERTY(Category = "Quality Settings/Low", EditAnywhere, config, meta = (ClampMin = "500.0", ClampMax = "15000.0", UIMin = "500.0", UIMax = "15000.0"))
float LowStaticMeshMaxDrawDistance = 10000.0f;
/// @} /// @}