From 9c9ef245772e9fe02fc06c395f4f25d154bff975 Mon Sep 17 00:00:00 2001 From: Brian L <130494071+csciguy8@users.noreply.github.com> Date: Fri, 1 Sep 2023 15:17:21 -0600 Subject: [PATCH] Refactor scene setup code into separate files --- .../Private/Tests/CesiumLoadTest.cpp | 192 ++---------------- .../Private/Tests/CesiumSceneGeneration.cpp | 169 +++++++++++++++ .../Private/Tests/CesiumSceneGeneration.h | 40 ++++ 3 files changed, 222 insertions(+), 179 deletions(-) create mode 100644 Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.cpp create mode 100644 Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.h diff --git a/Source/CesiumRuntime/Private/Tests/CesiumLoadTest.cpp b/Source/CesiumRuntime/Private/Tests/CesiumLoadTest.cpp index 65aa16f9a..c033b2d7f 100644 --- a/Source/CesiumRuntime/Private/Tests/CesiumLoadTest.cpp +++ b/Source/CesiumRuntime/Private/Tests/CesiumLoadTest.cpp @@ -6,23 +6,13 @@ #include "Tests/AutomationCommon.h" #include "Tests/AutomationEditorCommon.h" -#include "Editor.h" -#include "Engine/World.h" -#include "EngineUtils.h" -#include "GameFramework/PlayerStart.h" - -#include "Cesium3DTileset.h" -#include "CesiumCameraManager.h" -#include "CesiumGeoreference.h" +#include "CesiumTestHelpers.h" +#include "CesiumSceneGeneration.h" #include "CesiumGltfComponent.h" -#include "CesiumIonRasterOverlay.h" #include "CesiumRuntime.h" -#include "CesiumSunSky.h" -#include "CesiumTestHelpers.h" #include "GlobeAwareDefaultPawn.h" -FString testIonToken( - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3NjU3OGE4Zi0xOGM4LTQ4NjYtODc4ZS02YWNkMDZmY2Y1M2YiLCJpZCI6MjU5LCJpYXQiOjE2OTA4Nzg3MjB9.uxePYJL59S4pG5aqJHb9goikVSO-Px6xA7kZH8oM1eM"); +using namespace Cesium; IMPLEMENT_SIMPLE_AUTOMATION_TEST( FCesiumLoadTestDenver, @@ -39,40 +29,9 @@ IMPLEMENT_SIMPLE_AUTOMATION_TEST( "Cesium.Performance.LoadTestMontrealPointCloud", EAutomationTestFlags::EditorContext | EAutomationTestFlags::PerfFilter) -struct LoadTestContext { - UWorld* world; - ACesiumGeoreference* georeference; - ACesiumCameraManager* cameraManager; - AGlobeAwareDefaultPawn* pawn; - std::vector tilesets; - FRequestPlaySessionParams playSessionParams; - - void setCamera(const FCesiumCamera& camera) { - // Take over first camera, or add if it doesn't exist - const TMap cameras = cameraManager->GetCameras(); - if (cameras.IsEmpty()) { - cameraManager->AddCamera(camera); - } else { - cameraManager->UpdateCamera(0, camera); - } - } - - void refreshTilesets() { - std::vector::iterator it; - for (it = tilesets.begin(); it != tilesets.end(); ++it) - (*it)->RefreshTileset(); - } - - void setSuspendUpdate(bool suspend) { - std::vector::iterator it; - for (it = tilesets.begin(); it != tilesets.end(); ++it) - (*it)->SuspendUpdate = suspend; - } -}; - -bool neverBreak(LoadTestContext& context) { return false; } +bool neverBreak(SceneGenerationContext& context) { return false; } -bool breakWhenTilesetsLoaded(LoadTestContext& context) { +bool breakWhenTilesetsLoaded(SceneGenerationContext& context) { std::vector::const_iterator it; for (it = context.tilesets.begin(); it != context.tilesets.end(); ++it) { ACesium3DTileset* tileset = *it; @@ -85,9 +44,9 @@ bool breakWhenTilesetsLoaded(LoadTestContext& context) { } bool tickWorldUntil( - LoadTestContext& context, + SceneGenerationContext& context, size_t timeoutSecs, - std::function breakFunction) { + std::function breakFunction) { const double minStepTime = 0.050; // Don't loop faster than 20 fps const double testStartMark = FPlatformTime::Seconds(); @@ -137,114 +96,16 @@ bool tickWorldUntil( }; } -void setupForGoogleTiles(LoadTestContext& context) { - - FVector targetOrigin(-122.083969, 37.424492, 142.859116); - FString targetUrl( - "https://tile.googleapis.com/v1/3dtiles/root.json?key=AIzaSyCnRPXWDIj1LuX6OWIweIqZFHHoXVgdYss"); - - FCesiumCamera camera; - camera.ViewportSize = FVector2D(1024, 768); - camera.Location = FVector(0, 0, 0); - camera.Rotation = FRotator(-25, 95, 0); - camera.FieldOfViewDegrees = 90; - context.setCamera(camera); - - context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( - targetOrigin); - - context.pawn->SetActorLocation(FVector(0, 0, 0)); - context.pawn->SetActorRotation(FRotator(-25, 95, 0)); - - ACesium3DTileset* tileset = context.world->SpawnActor(); - tileset->SetUrl(targetUrl); - tileset->SetTilesetSource(ETilesetSource::FromUrl); - tileset->SetActorLabel(TEXT("Google Photorealistic 3D Tiles")); - - context.tilesets.push_back(tileset); -} - -void setupForDenver(LoadTestContext& context) { - - FVector targetOrigin(-104.988892, 39.743462, 1798.679443); - - FCesiumCamera camera; - camera.ViewportSize = FVector2D(1024, 768); - camera.Location = FVector(0, 0, 0); - camera.Rotation = FRotator(-5.2, -149.4, 0); - camera.FieldOfViewDegrees = 90; - context.setCamera(camera); - - context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( - targetOrigin); - - context.pawn->SetActorLocation(FVector(0, 0, 0)); - context.pawn->SetActorRotation(FRotator(-5.2, -149.4, 0)); - - // Add Cesium World Terrain - ACesium3DTileset* worldTerrainTileset = - context.world->SpawnActor(); - worldTerrainTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); - worldTerrainTileset->SetIonAssetID(1); - worldTerrainTileset->SetIonAccessToken(testIonToken); - worldTerrainTileset->SetActorLabel(TEXT("Cesium World Terrain")); - - // Bing Maps Aerial overlay - UCesiumIonRasterOverlay* pOverlay = NewObject( - worldTerrainTileset, - FName("Bing Maps Aerial"), - RF_Transactional); - pOverlay->MaterialLayerKey = TEXT("Overlay0"); - pOverlay->IonAssetID = 2; - pOverlay->SetActive(true); - pOverlay->OnComponentCreated(); - worldTerrainTileset->AddInstanceComponent(pOverlay); - - // Aerometrex Denver - ACesium3DTileset* aerometrexTileset = - context.world->SpawnActor(); - aerometrexTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); - aerometrexTileset->SetIonAssetID(354307); - aerometrexTileset->SetIonAccessToken(testIonToken); - aerometrexTileset->SetMaximumScreenSpaceError(2.0); - aerometrexTileset->SetActorLabel(TEXT("Aerometrex Denver")); - - context.tilesets.push_back(worldTerrainTileset); - context.tilesets.push_back(aerometrexTileset); -} - -void createCommonWorldObjects(LoadTestContext& context) { - - context.world = FAutomationEditorCommonUtils::CreateNewMap(); - - ACesiumSunSky* sunSky = context.world->SpawnActor(); - APlayerStart* playerStart = context.world->SpawnActor(); - - context.cameraManager = - ACesiumCameraManager::GetDefaultCameraManager(context.world); - - FSoftObjectPath objectPath( - TEXT("Class'/CesiumForUnreal/DynamicPawn.DynamicPawn_C'")); - TSoftObjectPtr DynamicPawn = TSoftObjectPtr(objectPath); - - context.georeference = - ACesiumGeoreference::GetDefaultGeoreference(context.world); - context.pawn = context.world->SpawnActor( - Cast(DynamicPawn.LoadSynchronous())); - - context.pawn->AutoPossessPlayer = EAutoReceiveInput::Player0; -} - bool RunLoadTest( - std::function locationSetup, - std::function afterTest = {}) { + std::function locationSetup, + std::function afterTest = {}) { // // Programmatically set up the world // UE_LOG(LogCesium, Display, TEXT("Creating world objects...")); - LoadTestContext context; - createCommonWorldObjects(context); + Cesium::SceneGenerationContext context; + Cesium::createCommonWorldObjects(context); // Configure location specific objects locationSetup(context); @@ -321,35 +182,8 @@ bool FCesiumLoadTestGoogleplex::RunTest(const FString& Parameters) { } bool FCesiumLoadTestMontrealPointCloud::RunTest(const FString& Parameters) { - auto setup = [this](LoadTestContext& context) { - FVector targetOrigin(-73.616526, 45.57335, 95.048859); - - FCesiumCamera camera; - camera.ViewportSize = FVector2D(1024, 768); - camera.Location = FVector(0, 0, 0); - camera.Rotation = FRotator(-90.0, 0.0, 0.0); - camera.FieldOfViewDegrees = 90; - context.setCamera(camera); - - context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( - targetOrigin); - - context.pawn->SetActorLocation(FVector(0, 0, 0)); - context.pawn->SetActorRotation(FRotator(-90.0, 0.0, 0.0)); - - // Montreal Point Cloud - ACesium3DTileset* montrealTileset = - context.world->SpawnActor(); - montrealTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); - montrealTileset->SetIonAssetID(28945); - montrealTileset->SetIonAccessToken(testIonToken); - montrealTileset->SetMaximumScreenSpaceError(16.0); - montrealTileset->SetActorLabel(TEXT("Montreal Point Cloud")); - - context.tilesets.push_back(montrealTileset); - }; - auto after = [this](LoadTestContext& context) { + auto after = [this](SceneGenerationContext& context) { // Zoom way out FCesiumCamera zoomedOut; zoomedOut.ViewportSize = FVector2D(1024, 768); @@ -389,7 +223,7 @@ bool FCesiumLoadTestMontrealPointCloud::RunTest(const FString& Parameters) { } }; - return RunLoadTest(setup, after); + return RunLoadTest(setupForMelbourne, after); } #endif diff --git a/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.cpp b/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.cpp new file mode 100644 index 000000000..dd8603357 --- /dev/null +++ b/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.cpp @@ -0,0 +1,169 @@ +// Copyright 2020-2023 CesiumGS, Inc. and Contributors + +#include "CesiumSceneGeneration.h" + +#include "Tests/AutomationEditorCommon.h" + +#include "GameFramework/PlayerStart.h" + +#include "Cesium3DTileset.h" +#include "CesiumCameraManager.h" +#include "CesiumGeoreference.h" +#include "CesiumIonRasterOverlay.h" +#include "CesiumSunSky.h" +#include "GlobeAwareDefaultPawn.h" + +namespace Cesium { + +FString SceneGenerationContext::testIonToken( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3NjU3OGE4Zi0xOGM4LTQ4NjYtODc4ZS02YWNkMDZmY2Y1M2YiLCJpZCI6MjU5LCJpYXQiOjE2OTA4Nzg3MjB9.uxePYJL59S4pG5aqJHb9goikVSO-Px6xA7kZH8oM1eM"); + +void SceneGenerationContext::setCamera(const FCesiumCamera& camera) { + // Take over first camera, or add if it doesn't exist + const TMap cameras = cameraManager->GetCameras(); + if (cameras.IsEmpty()) { + cameraManager->AddCamera(camera); + } else { + cameraManager->UpdateCamera(0, camera); + } +} + +void SceneGenerationContext::refreshTilesets() { + std::vector::iterator it; + for (it = tilesets.begin(); it != tilesets.end(); ++it) + (*it)->RefreshTileset(); +} + +void SceneGenerationContext::setSuspendUpdate(bool suspend) { + std::vector::iterator it; + for (it = tilesets.begin(); it != tilesets.end(); ++it) + (*it)->SuspendUpdate = suspend; +} + +void createCommonWorldObjects(SceneGenerationContext& context) { + + context.world = FAutomationEditorCommonUtils::CreateNewMap(); + + ACesiumSunSky* sunSky = context.world->SpawnActor(); + APlayerStart* playerStart = context.world->SpawnActor(); + + context.cameraManager = + ACesiumCameraManager::GetDefaultCameraManager(context.world); + + FSoftObjectPath objectPath( + TEXT("Class'/CesiumForUnreal/DynamicPawn.DynamicPawn_C'")); + TSoftObjectPtr DynamicPawn = TSoftObjectPtr(objectPath); + + context.georeference = + ACesiumGeoreference::GetDefaultGeoreference(context.world); + context.pawn = context.world->SpawnActor( + Cast(DynamicPawn.LoadSynchronous())); + + context.pawn->AutoPossessPlayer = EAutoReceiveInput::Player0; +} + +void setupForGoogleTiles(SceneGenerationContext& context) { + + FVector targetOrigin(-122.083969, 37.424492, 142.859116); + FString targetUrl( + "https://tile.googleapis.com/v1/3dtiles/root.json?key=AIzaSyCnRPXWDIj1LuX6OWIweIqZFHHoXVgdYss"); + + FCesiumCamera camera; + camera.ViewportSize = FVector2D(1024, 768); + camera.Location = FVector(0, 0, 0); + camera.Rotation = FRotator(-25, 95, 0); + camera.FieldOfViewDegrees = 90; + context.setCamera(camera); + + context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( + targetOrigin); + + context.pawn->SetActorLocation(FVector(0, 0, 0)); + context.pawn->SetActorRotation(FRotator(-25, 95, 0)); + + ACesium3DTileset* tileset = context.world->SpawnActor(); + tileset->SetUrl(targetUrl); + tileset->SetTilesetSource(ETilesetSource::FromUrl); + tileset->SetActorLabel(TEXT("Google Photorealistic 3D Tiles")); + + context.tilesets.push_back(tileset); +} + +void setupForDenver(SceneGenerationContext& context) { + + FVector targetOrigin(-104.988892, 39.743462, 1798.679443); + + FCesiumCamera camera; + camera.ViewportSize = FVector2D(1024, 768); + camera.Location = FVector(0, 0, 0); + camera.Rotation = FRotator(-5.2, -149.4, 0); + camera.FieldOfViewDegrees = 90; + context.setCamera(camera); + + context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( + targetOrigin); + + context.pawn->SetActorLocation(FVector(0, 0, 0)); + context.pawn->SetActorRotation(FRotator(-5.2, -149.4, 0)); + + // Add Cesium World Terrain + ACesium3DTileset* worldTerrainTileset = + context.world->SpawnActor(); + worldTerrainTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); + worldTerrainTileset->SetIonAssetID(1); + worldTerrainTileset->SetIonAccessToken(SceneGenerationContext::testIonToken); + worldTerrainTileset->SetActorLabel(TEXT("Cesium World Terrain")); + + // Bing Maps Aerial overlay + UCesiumIonRasterOverlay* pOverlay = NewObject( + worldTerrainTileset, + FName("Bing Maps Aerial"), + RF_Transactional); + pOverlay->MaterialLayerKey = TEXT("Overlay0"); + pOverlay->IonAssetID = 2; + pOverlay->SetActive(true); + pOverlay->OnComponentCreated(); + worldTerrainTileset->AddInstanceComponent(pOverlay); + + // Aerometrex Denver + ACesium3DTileset* aerometrexTileset = + context.world->SpawnActor(); + aerometrexTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); + aerometrexTileset->SetIonAssetID(354307); + aerometrexTileset->SetIonAccessToken(SceneGenerationContext::testIonToken); + aerometrexTileset->SetMaximumScreenSpaceError(2.0); + aerometrexTileset->SetActorLabel(TEXT("Aerometrex Denver")); + + context.tilesets.push_back(worldTerrainTileset); + context.tilesets.push_back(aerometrexTileset); +} + +void setupForMelbourne(SceneGenerationContext& context) { + FVector targetOrigin(-73.616526, 45.57335, 95.048859); + + FCesiumCamera camera; + camera.ViewportSize = FVector2D(1024, 768); + camera.Location = FVector(0, 0, 0); + camera.Rotation = FRotator(-90.0, 0.0, 0.0); + camera.FieldOfViewDegrees = 90; + context.setCamera(camera); + + context.georeference->SetGeoreferenceOriginLongitudeLatitudeHeight( + targetOrigin); + + context.pawn->SetActorLocation(FVector(0, 0, 0)); + context.pawn->SetActorRotation(FRotator(-90.0, 0.0, 0.0)); + + // Montreal Point Cloud + ACesium3DTileset* montrealTileset = + context.world->SpawnActor(); + montrealTileset->SetTilesetSource(ETilesetSource::FromCesiumIon); + montrealTileset->SetIonAssetID(28945); + montrealTileset->SetIonAccessToken(SceneGenerationContext::testIonToken); + montrealTileset->SetMaximumScreenSpaceError(16.0); + montrealTileset->SetActorLabel(TEXT("Montreal Point Cloud")); + + context.tilesets.push_back(montrealTileset); +} + +} // namespace Cesium diff --git a/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.h b/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.h new file mode 100644 index 000000000..54a2fc4be --- /dev/null +++ b/Source/CesiumRuntime/Private/Tests/CesiumSceneGeneration.h @@ -0,0 +1,40 @@ +// Copyright 2020-2023 CesiumGS, Inc. and Contributors + +#pragma once + +#if WITH_EDITOR + +#include "CesiumCameraManager.h" +#include "Editor.h" + +class UWorld; +class ACesiumGeoreference; +class AGlobeAwareDefaultPawn; +class ACesium3DTileset; + +namespace Cesium { + +struct SceneGenerationContext { + UWorld* world; + ACesiumGeoreference* georeference; + ACesiumCameraManager* cameraManager; + AGlobeAwareDefaultPawn* pawn; + std::vector tilesets; + FRequestPlaySessionParams playSessionParams; + + void setCamera(const FCesiumCamera& camera); + void refreshTilesets(); + void setSuspendUpdate(bool suspend); + + static FString testIonToken; +}; + +void createCommonWorldObjects(SceneGenerationContext& context); + +void setupForDenver(SceneGenerationContext& context); +void setupForGoogleTiles(SceneGenerationContext& context); +void setupForMelbourne(SceneGenerationContext& context); + +}; // namespace Cesium + +#endif // #if WITH_EDITOR