diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset index 244761b42..28ddda36f 100644 Binary files a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset and b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp index 5d830b2f3..a25b31a7c 100644 --- a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp @@ -95,6 +95,85 @@ void UMapGeneratorWidget::CookVegetationToCurrentTile(const TArrayGetEditorWorldContext().World(); + // UWorld* World = GEngine->GetWorld(); + if(World == nullptr) + return false; + + ALandscape* Landscape = (ALandscape*) UGameplayStatics::GetActorOfClass( + World, + ALandscape::StaticClass()); + // LandscapePostEditEvent(Landscape); + + FProperty* PropertyToUpdate = Landscape->GetClass()->FindPropertyByName(TEXT("CollisionMipLevel")); + int32* PropertyValue = PropertyToUpdate->ContainerPtrToValuePtr(Landscape); + if(!PropertyToUpdate){ + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("%s: Could not found the specified property"), + *CUR_CLASS_FUNC_LINE); + return false; + } + + *PropertyValue += 1; // Increment + + FPropertyChangedEvent LandscapeCustomPropertyChangedEvent(PropertyToUpdate); + Landscape->PostEditChangeProperty(LandscapeCustomPropertyChangedEvent); + Landscape->PostEditChange(); + + + Landscape->RecreateCollisionComponents(); + + if (MapPath == "") + { + MapPath = World->GetPathName(); + } + Landscape->ReregisterAllComponents(); + /* bool bSaveMapSuccess = FEditorFileUtils::SaveMap(World, + World->GetPathName());*/ + UEditorAssetLibrary::SaveAsset(World->GetPathName(), true); + bool bSuccess = FEditorFileUtils::SaveDirtyPackages(true, true, true, true, true, true); + + return true; +} + +void UMapGeneratorWidget::CookTilesCollisions(const FMapGeneratorMetaInfo& MetaInfo) +{ + for (int i = 0; i < MetaInfo.SizeX; i++) + { + for (int j = 0; j < MetaInfo.SizeY; j++) + { + const FString MapName = + MetaInfo.MapName + "_Tile_" + FString::FromInt(i) + "_" + FString::FromInt(j); + const FString MapNameToLoad = MetaInfo.DestinationPath + "/" + MapName + "." + MapName; + + bool bLoadedSuccess = FEditorFileUtils::LoadMap(*MapNameToLoad, false, true); + + if (!bLoadedSuccess) + UE_LOG(LogCarlaToolsMapGenerator, Log, TEXT("%s: Error loading to %s tiles"), + *CUR_CLASS_FUNC_LINE, *MetaInfo.MapName); + + UWorld* EditorWorld = GEditor->GetEditorWorldContext().World(); + + UE_LOG(LogCarlaToolsMapGenerator, Log, TEXT("%s: HERE! %s -- %s to %s tiles"), + *CUR_CLASS_FUNC_LINE, *MapNameToLoad, *GEditor->GetEditorWorldContext().World()->GetName(), *MetaInfo.MapName); + + bool bRecalculateResult = RecalculateCollision(MapNameToLoad); + + if (!bRecalculateResult) + UE_LOG(LogCarlaToolsMapGenerator, Error, TEXT("%s: Error recalculating to %s tiles"), + *CUR_CLASS_FUNC_LINE, *MetaInfo.MapName); + + //bool bSaveMapSuccess = FEditorFileUtils::SaveMap(GEditor->GetEditorWorldContext().World(), + //MapNameToLoad); + + //if(!bSaveMapSuccess) + //UE_LOG(LogCarlaToolsMapGenerator, Error, TEXT("%s: Error Saving to %s tiles"), + //*CUR_CLASS_FUNC_LINE, *MetaInfo.MapName); + } + } +} + FString UMapGeneratorWidget::SanitizeDirectory(FString InDirectory) { UE_LOG(LogCarlaToolsMapGenerator, Log, TEXT("%s: Sanitazing directory: %s"), @@ -729,7 +808,7 @@ bool UMapGeneratorWidget::CreateTilesMaps(const FMapGeneratorMetaInfo& MetaInfo) HeightmapDataPerLayers, TEXT("NONE"), MaterialLayerDataPerLayer, ELandscapeImportAlphamapType::Layered); // Landscape->Import(Landscape->GetLandscapeGuid(), 0, 0, 2016, 2016, Landscape->NumSubsections, Landscape->SubsectionSizeQuads, // HeightmapDataPerLayers, TEXT("NONE"), MaterialLayerDataPerLayer, ELandscapeImportAlphamapType::Layered); - + // FVector LandscapeOrigin; // FVector LandscapeBounds; @@ -744,6 +823,8 @@ bool UMapGeneratorWidget::CreateTilesMaps(const FMapGeneratorMetaInfo& MetaInfo) // Apply material AssignLandscapeMaterial(Landscape); + Landscape->RecreateCollisionComponents(); + const FString PackageFileName = FPackageName::LongPackageNameToFilename( PackageName, FPackageName::GetMapPackageExtension()); @@ -762,6 +843,8 @@ bool UMapGeneratorWidget::CreateTilesMaps(const FMapGeneratorMetaInfo& MetaInfo) } } + CookTilesCollisions(MetaInfo); + return true; } diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h index d16ff8275..4dfba3e73 100644 --- a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h @@ -100,6 +100,12 @@ public: UFUNCTION(Category="Map Generator", BlueprintCallable) void CookVegetationToCurrentTile(const TArray FoliageSpawners); + UFUNCTION(Category="Map Generator", BlueprintCallable) + bool RecalculateCollision(FString MapPath = ""); + + UFUNCTION(Category = "MapGenerator", BlueprintCallable) + void CookTilesCollisions(const FMapGeneratorMetaInfo& MetaInfo); + /// Utils funtion to format @a InDirectory so it gets sanitized in a /// format that unreal can access the directory, deleting unnecesary /// characters such as final '/' or '\' @@ -168,6 +174,8 @@ private: UFUNCTION() bool CreateTilesMaps(const FMapGeneratorMetaInfo& MetaInfo); + + /// Searches for the specified map in the specified path in @a MetaInfo /// and starts the vegetation cooking process for each of the tile. /// IMPORTANT: Only maps with '_Tile_' tag in it name are processed as