diff --git a/Source/Carla/Game/CarlaGameMode.cpp b/Source/Carla/Game/CarlaGameMode.cpp index cee954c05..9be7e0034 100644 --- a/Source/Carla/Game/CarlaGameMode.cpp +++ b/Source/Carla/Game/CarlaGameMode.cpp @@ -14,9 +14,10 @@ #include "CarlaSettings.h" #include "CarlaVehicleController.h" #include "Tagger.h" +#include "TaggerDelegate.h" -ACarlaGameMode::ACarlaGameMode() : - Super(), +ACarlaGameMode::ACarlaGameMode(const FObjectInitializer& ObjectInitializer) : + Super(ObjectInitializer), GameController(nullptr), PlayerController(nullptr) { @@ -27,6 +28,8 @@ ACarlaGameMode::ACarlaGameMode() : GameStateClass = ACarlaGameState::StaticClass(); PlayerStateClass = ACarlaPlayerState::StaticClass(); HUDClass = ACarlaHUD::StaticClass(); + + TaggerDelegate = CreateDefaultSubobject(TEXT("TaggerDelegate")); } void ACarlaGameMode::InitGame( @@ -44,6 +47,12 @@ void ACarlaGameMode::InitGame( GameController = &GameInstance->GetGameController(); GameController->Initialize(GameInstance->GetCarlaSettings()); GameInstance->GetCarlaSettings().LogSettings(); + + if (TaggerDelegate != nullptr) { + TaggerDelegate->RegisterSpawnHandler(GetWorld()); + } else { + UE_LOG(LogCarla, Error, TEXT("Missing TaggerDelegate!")); + } } void ACarlaGameMode::RestartPlayer(AController* NewPlayer) @@ -72,6 +81,7 @@ void ACarlaGameMode::BeginPlay() Super::BeginPlay(); if (GameInstance->GetCarlaSettings().bSemanticSegmentationEnabled) { TagActorsForSemanticSegmentation(); + TaggerDelegate->SetSemanticSegmentationEnabled(); } GameController->BeginPlay(); } diff --git a/Source/Carla/Game/TaggerDelegate.cpp b/Source/Carla/Game/TaggerDelegate.cpp new file mode 100644 index 000000000..4432cadd0 --- /dev/null +++ b/Source/Carla/Game/TaggerDelegate.cpp @@ -0,0 +1,29 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#include "Carla.h" +#include "TaggerDelegate.h" + +#include "Engine/World.h" +#include "Tagger.h" + +UTaggerDelegate::UTaggerDelegate() : + ActorSpawnedDelegate(FOnActorSpawned::FDelegate::CreateUObject(this, &UTaggerDelegate::OnActorSpawned)) {} + +void UTaggerDelegate::RegisterSpawnHandler(UWorld *InWorld) +{ + InWorld->AddOnActorSpawnedHandler(ActorSpawnedDelegate); +} + +void UTaggerDelegate::OnActorSpawned(AActor* InActor) +{ + if (InActor != nullptr) { + ATagger::TagActor(*InActor, bSemanticSegmentationEnabled); + + TArray Tags; + ATagger::GetTagsOfTaggedActor(*InActor, Tags); + UE_LOG(LogCarla, Warning, TEXT("Tagged recently spawned actor \"%s\""), *InActor->GetName()); + for (auto Tag : Tags) { + UE_LOG(LogCarla, Warning, TEXT(" - %d"), (uint8)Tag); + } + } +} diff --git a/Source/Carla/Game/TaggerDelegate.h b/Source/Carla/Game/TaggerDelegate.h new file mode 100644 index 000000000..36540e061 --- /dev/null +++ b/Source/Carla/Game/TaggerDelegate.h @@ -0,0 +1,31 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#pragma once + +#include "TaggerDelegate.generated.h" + +/// Used to tag every actor that is spawned into the world. +UCLASS() +class CARLA_API UTaggerDelegate : public UObject +{ + GENERATED_BODY() + +public: + + UTaggerDelegate(); + + void RegisterSpawnHandler(UWorld *World); + + void SetSemanticSegmentationEnabled(bool Enable = true) + { + bSemanticSegmentationEnabled = Enable; + } + + void OnActorSpawned(AActor *Actor); + +private: + + FOnActorSpawned::FDelegate ActorSpawnedDelegate; + + bool bSemanticSegmentationEnabled = false; +}; diff --git a/Source/Carla/Tagger.cpp b/Source/Carla/Tagger.cpp index 4443de4db..18fce00b4 100644 --- a/Source/Carla/Tagger.cpp +++ b/Source/Carla/Tagger.cpp @@ -68,10 +68,10 @@ static void SetStencilValue( UPrimitiveComponent &Component, const ECityObjectLabel &Label, const bool bSetRenderCustomDepth) { + Component.SetCustomDepthStencilValue(CastEnum(Label)); Component.SetRenderCustomDepth( bSetRenderCustomDepth && - !ATagger::MatchComponent(Component, ECityObjectLabel::None)); - Component.SetCustomDepthStencilValue(CastEnum(Label)); + (Label != ECityObjectLabel::None)); } // =============================================================================