diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.cpp index f63229894..4144e9451 100755 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.cpp @@ -11,6 +11,7 @@ #include "Engine/StaticMesh.h" #include #include "Settings/CarlaSettings.h" +#include "Game/CarlaGameInstance.h" using tag_size_t = std::underlying_type::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 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 oldcomponents = GetComponentsByClass(UInstancedStaticMeshComponent::StaticClass()); + for(int32 i=0;iDestroyComponent(); } - 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 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(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; -} + diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.h index d442da740..6807b52d8 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/CityMapMeshHolder.h @@ -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 TagMap; - UPROPERTY(Category = "Meshes", VisibleAnywhere) - TArray MeshInstatiators; - - UPROPERTY(Category = "Meshes", EditAnywhere) - float MaxDrawDistance = 10000.0f; /** @todo: move to carlasettings */ + }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettings.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettings.h index 95be930c3..051ce39b4 100755 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettings.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettings.h @@ -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; + /// @} // =========================================================================== diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettingsDelegate.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettingsDelegate.cpp index 71c07a309..eed16958d 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettingsDelegate.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/CarlaSettingsDelegate.cpp @@ -142,27 +142,37 @@ void UCarlaSettingsDelegate::SetAllRoadsLowQuality(UWorld* world) const if(CarlaSettings->LowRoadMaterials.Num()==0) return; //no materials configured for low quality TArray actors; UGameplayStatics::GetAllActorsWithTag(world, UCarlaSettings::CARLA_ROAD_TAG,actors); + const float LowRoadPieceMeshMaxDrawDistance = CarlaSettings->LowRoadPieceMeshMaxDrawDistance; for(int32 i=0; iIsPendingKillPending()) continue; TArray components = actors[i]->GetComponentsByClass(UStaticMeshComponent::StaticClass()); for(int32 j=0; j(components[j]); - if(staticmesh) + UStaticMeshComponent* staticmeshcomponent = Cast(components[j]); + if(staticmeshcomponent) { - TArray slotsnames = staticmesh->GetMaterialSlotNames(); + TArray slotsnames = staticmeshcomponent->GetMaterialSlotNames(); for(int32 k=0; kLowRoadMaterials.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; });