Lower quality max draw culling distance for road pieces moved from road builder to settings. Roads will no longer contain static instanced meshes when rebuild from scene, just static mesh components.

This commit is contained in:
CVC\jbelon 2018-03-07 18:50:11 +01:00
parent de9048ad22
commit 53533dd402
4 changed files with 51 additions and 66 deletions

View File

@ -11,6 +11,7 @@
#include "Engine/StaticMesh.h"
#include <vector>
#include "Settings/CarlaSettings.h"
#include "Game/CarlaGameInstance.h"
using tag_size_t = std::underlying_type<ECityMapMeshTag>::type;
@ -54,14 +55,17 @@ void ACityMapMeshHolder::OnConstruction(const FTransform &Transform)
void ACityMapMeshHolder::PostInitializeComponents()
{
Super::PostInitializeComponents();
if(IsValid(GetLevel())&&!GetLevel()->IsPendingKill())
{
if (MeshInstatiators.Num() == 0) {
ResetInstantiators();
UpdateMapScale();
UpdateMap();
}
{
TArray<AActor*> roadpieces;
GetAttachedActors(roadpieces);
if(roadpieces.Num()==0)
{
UpdateMapScale();
UpdateMap();
}
}
}
@ -72,7 +76,7 @@ void ACityMapMeshHolder::PostEditChangeProperty(FPropertyChangedEvent& PropertyC
Super::PostEditChangeProperty(PropertyChangedEvent);
if (PropertyChangedEvent.Property)
{
ResetInstantiators();
DeletePieces();
UpdateMapScale();
UpdateMap();
}
@ -122,12 +126,10 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, uint32 X, uint32 Y, fl
const FQuat rotation(FVector(0.0f, 0.0f, 1.0f), Angle);
const FVector location = GetTileLocation(X, Y);
AddInstance(Tag, FTransform(rotation, location));
}
void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
{
auto &instantiator = GetInstantiator(Tag);
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
FVector location = Transform.GetLocation();
@ -137,16 +139,9 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
StaticMeshActor->SetMobility(EComponentMobility::Static);
UStaticMeshComponent* staticmeshcomponent = StaticMeshActor->GetStaticMeshComponent();
staticmeshcomponent->SetMobility(EComponentMobility::Static);
staticmeshcomponent->SetStaticMesh(instantiator.GetStaticMesh());
//occlussion
staticmeshcomponent->SetStaticMesh(GetStaticMesh(Tag));
StaticMeshActor->Tags.Add(UCarlaSettings::CARLA_ROAD_TAG);
staticmeshcomponent->bAllowCullDistanceVolume=true;
staticmeshcomponent->bUseAsOccluder=false;
staticmeshcomponent->LDMaxDrawDistance = MaxDrawDistance;
staticmeshcomponent->CastShadow=false;
StaticMeshActor->bEnableAutoLODGeneration=true;
StaticMeshActor->bEnableAutoLODGeneration = true;
}
// =============================================================================
@ -155,23 +150,14 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
void ACityMapMeshHolder::UpdateMap() {}
void ACityMapMeshHolder::ResetInstantiators()
void ACityMapMeshHolder::DeletePieces()
{
for (auto *instantiator : MeshInstatiators) {
if (instantiator != nullptr) {
instantiator->ClearInstances();
}
//this part will be deprecated: remove the instanced static mesh components
TArray<UActorComponent*> oldcomponents = GetComponentsByClass(UInstancedStaticMeshComponent::StaticClass());
for(int32 i=0;i<oldcomponents.Num();i++)
{
oldcomponents[i]->DestroyComponent();
}
if (MeshInstatiators.Num() != NUMBER_OF_TAGS) {
MeshInstatiators.Empty();
MeshInstatiators.Init(nullptr, NUMBER_OF_TAGS);
}
check(MeshInstatiators.Num() == NUMBER_OF_TAGS);
for (tag_size_t i = 0u; i < NUMBER_OF_TAGS; ++i) {
auto &instantiator = GetInstantiator(CityMapMeshTag::FromUInt(i));
instantiator.SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i)));
}
TArray<AActor*> roadpieces;
GetAttachedActors(roadpieces);
@ -203,19 +189,4 @@ void ACityMapMeshHolder::UpdateMapScale()
}
}
UInstancedStaticMeshComponent &ACityMapMeshHolder::GetInstantiator(ECityMapMeshTag Tag)
{
UInstancedStaticMeshComponent *instantiator = MeshInstatiators[CityMapMeshTag::ToUInt(Tag)];
if (instantiator == nullptr) {
// Create and register an instantiator.
instantiator = NewObject<UInstancedStaticMeshComponent>(this);
instantiator->SetMobility(EComponentMobility::Static);
instantiator->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
instantiator->SetupAttachment(SceneRootComponent);
instantiator->SetStaticMesh(GetStaticMesh(Tag));
MeshInstatiators[CityMapMeshTag::ToUInt(Tag)] = instantiator;
instantiator->RegisterComponent();
}
check(instantiator != nullptr);
return *instantiator;
}

View File

@ -13,6 +13,8 @@
class IDetailLayoutBuilder;
class UInstancedStaticMeshComponent;
class AStaticMeshActor;
/// Holds the static meshes and instances necessary for building a city map.
UCLASS(Abstract)
class CARLA_API ACityMapMeshHolder : public AActor
@ -92,14 +94,14 @@ private:
/// Here does nothing, implement in derived classes.
virtual void UpdateMap();
/// Clear all instances in the instantiators and update the static meshes.
void ResetInstantiators();
/// Clear all instances of the static mesh actors.
void DeletePieces();
/// Set the scale to the dimensions of the base mesh.
void UpdateMapScale();
/// Creates a new one if necessary.
UInstancedStaticMeshComponent &GetInstantiator(ECityMapMeshTag Tag);
private:
UPROPERTY()
USceneComponent *SceneRootComponent;
@ -113,9 +115,5 @@ private:
UPROPERTY()
TMap<UStaticMesh *, ECityMapMeshTag> TagMap;
UPROPERTY(Category = "Meshes", VisibleAnywhere)
TArray<UInstancedStaticMeshComponent *> MeshInstatiators;
UPROPERTY(Category = "Meshes", EditAnywhere)
float MaxDrawDistance = 10000.0f; /** @todo: move to carlasettings */
};

View File

@ -205,11 +205,17 @@ private:
float LowLightFadeDistance = 1000.0f;
/**
*
* Default low distance for all primitive components
*/
UPROPERTY(Category = "Quality Settings/Low", BlueprintReadOnly, EditAnywhere, config, meta = (ClampMin = "500.0", ClampMax = "15000.0", UIMin = "500.0", UIMax = "15000.0"))
float LowStaticMeshMaxDrawDistance = 10000.0f;
/**
* Default low distance for roads meshes
*/
UPROPERTY(Category = "Quality Settings/Low", BlueprintReadOnly, EditAnywhere, config, meta = (ClampMin = "5000.0", ClampMax = "17000.0", UIMin = "5000.0", UIMax = "17000.0"))
float LowRoadPieceMeshMaxDrawDistance = 15000.0f;
/// @}
// ===========================================================================

View File

@ -142,27 +142,37 @@ void UCarlaSettingsDelegate::SetAllRoadsLowQuality(UWorld* world) const
if(CarlaSettings->LowRoadMaterials.Num()==0) return; //no materials configured for low quality
TArray<AActor*> actors;
UGameplayStatics::GetAllActorsWithTag(world, UCarlaSettings::CARLA_ROAD_TAG,actors);
const float LowRoadPieceMeshMaxDrawDistance = CarlaSettings->LowRoadPieceMeshMaxDrawDistance;
for(int32 i=0; i<actors.Num(); i++)
{
if(!IsValid(actors[i]) || actors[i]->IsPendingKillPending()) continue;
TArray<UActorComponent*> components = actors[i]->GetComponentsByClass(UStaticMeshComponent::StaticClass());
for(int32 j=0; j<components.Num(); j++)
{
UStaticMeshComponent* staticmesh = Cast<UStaticMeshComponent>(components[j]);
if(staticmesh)
UStaticMeshComponent* staticmeshcomponent = Cast<UStaticMeshComponent>(components[j]);
if(staticmeshcomponent)
{
TArray<FName> slotsnames = staticmesh->GetMaterialSlotNames();
TArray<FName> slotsnames = staticmeshcomponent->GetMaterialSlotNames();
for(int32 k=0; k<slotsnames.Num(); k++)
{
const FName &slotname = slotsnames[k];
bool found = CarlaSettings->LowRoadMaterials.ContainsByPredicate([staticmesh,slotname](const FStaticMaterial& material)
bool found = CarlaSettings->LowRoadMaterials.ContainsByPredicate(
[staticmeshcomponent,slotname,LowRoadPieceMeshMaxDrawDistance](const FStaticMaterial& material)
{
if(material.MaterialSlotName.IsEqual(slotname))
{
staticmesh->SetMaterial(
staticmesh->GetMaterialIndex(slotname),
staticmeshcomponent->SetMaterial(
staticmeshcomponent->GetMaterialIndex(slotname),
material.MaterialInterface
);
staticmeshcomponent->bAllowCullDistanceVolume=true;
staticmeshcomponent->bUseAsOccluder=false;
staticmeshcomponent->LDMaxDrawDistance = LowRoadPieceMeshMaxDrawDistance;
staticmeshcomponent->CastShadow = false;
return true;
} else return false;
});