diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index 071f53020..e099a53ac 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -291,5 +291,25 @@ namespace client { return Result; } + void World::ApplyColorTextureToObject( + const std::string &object_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index) { + _episode.Lock()->ApplyColorTextureToObject(object_name, parameter, Texture, material_index); + } + + void World::ApplyFloatColorTextureToObject( + const std::string &object_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index) { + _episode.Lock()->ApplyColorTextureToObject(object_name, parameter, Texture, material_index); + } + + std::vector World::GetNamesOfAllObjects() const { + return _episode.Lock()->GetNamesOfAllObjects(); + } + } // namespace client } // namespace carla diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 90d8d7003..c758243d8 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -26,6 +26,8 @@ #include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/WeatherParameters.h" #include "carla/rpc/VehicleLightStateList.h" +#include "carla/rpc/Texture.h" +#include "carla/rpc/MaterialParameter.h" #include @@ -187,6 +189,22 @@ namespace client { std::vector> GetTrafficLightsInJunction( const road::JuncId junc_id) const; + // std::vector GetObjectNameList(); + + void ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index); + + void ApplyFloatColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index); + + std::vector GetNamesOfAllObjects() const; + private: detail::EpisodeProxy _episode; diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index a7caeabfc..ab0f01092 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -162,6 +162,26 @@ namespace detail { _pimpl->CallAndWait("copy_opendrive_to_file", std::move(opendrive), params); } + void Client::ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index) { + _pimpl->CallAndWait("apply_color_texture_to_object", actor_name, parameter, Texture, material_index); + } + + void Client::ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index) { + _pimpl->CallAndWait("apply_float_color_texture_to_object", actor_name, parameter, Texture, material_index); + } + + std::vector Client::GetNamesOfAllObjects() const { + return _pimpl->CallAndWait>("get_names_of_all_objects"); + } + rpc::EpisodeInfo Client::GetEpisodeInfo() { return _pimpl->CallAndWait("get_episode_info"); } diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index d5bb5e094..01c97f40a 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -31,6 +31,8 @@ #include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/VehicleWheels.h" #include "carla/rpc/WeatherParameters.h" +#include "carla/rpc/Texture.h" +#include "carla/rpc/MaterialParameter.h" #include #include @@ -103,6 +105,20 @@ namespace detail { void CopyOpenDriveToServer( std::string opendrive, const rpc::OpendriveGenerationParameters & params); + void ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index); + + void ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index); + + std::vector GetNamesOfAllObjects() const; + rpc::EpisodeInfo GetEpisodeInfo(); rpc::MapInfo GetMapInfo(); diff --git a/LibCarla/source/carla/client/detail/Simulator.cpp b/LibCarla/source/carla/client/detail/Simulator.cpp index 728160a28..440b1c1a7 100644 --- a/LibCarla/source/carla/client/detail/Simulator.cpp +++ b/LibCarla/source/carla/client/detail/Simulator.cpp @@ -380,6 +380,30 @@ namespace detail { _client.FreezeAllTrafficLights(frozen); } + // ========================================================================= + /// -- Texture updating operations + // ========================================================================= + + void Simulator::ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index) { + _client.ApplyColorTextureToObject(actor_name, parameter, Texture, material_index); + } + + void Simulator::ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index) { + _client.ApplyColorTextureToObject(actor_name, parameter, Texture, material_index); + } + + std::vector Simulator::GetNamesOfAllObjects() const { + return _client.GetNamesOfAllObjects(); + } + } // namespace detail } // namespace client } // namespace carla diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index a2bc5c907..d0009fa19 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -25,6 +25,8 @@ #include "carla/rpc/VehicleLightStateList.h" #include "carla/rpc/LabelledPoint.h" #include "carla/rpc/VehicleWheels.h" +#include "carla/rpc/Texture.h" +#include "carla/rpc/MaterialParameter.h" #include @@ -675,6 +677,26 @@ namespace detail { void FreezeAllTrafficLights(bool frozen); /// @} + // ========================================================================= + /// @name Texture updating operations + // ========================================================================= + /// @{ + + void ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture, + int material_index); + + void ApplyColorTextureToObject( + const std::string &actor_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture, + int material_index); + + std::vector GetNamesOfAllObjects() const; + + /// @} private: diff --git a/LibCarla/source/carla/rpc/FloatColor.h b/LibCarla/source/carla/rpc/FloatColor.h new file mode 100644 index 000000000..09329454b --- /dev/null +++ b/LibCarla/source/carla/rpc/FloatColor.h @@ -0,0 +1,72 @@ +// Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "carla/MsgPack.h" + +#include + +#ifdef LIBCARLA_INCLUDED_FROM_UE4 +# include "Math/Color.h" +#endif // LIBCARLA_INCLUDED_FROM_UE4 + +namespace carla { +namespace rpc { + +#pragma pack(push, 1) + struct FloatColor { + public: + + float r = 0.f; + float g = 0.f; + float b = 0.f; + float a = 1.f; + + FloatColor() = default; + FloatColor(const FloatColor &) = default; + + FloatColor(float r, float g, float b, float a = 1.f) + : r(r), g(g), b(b), a(a) {} + + FloatColor &operator=(const FloatColor &) = default; + + bool operator==(const FloatColor &rhs) const { + return (r == rhs.r) && (g == rhs.g) && (b == rhs.b) && (a == rhs.a); + } + + bool operator!=(const FloatColor &rhs) const { + return !(*this == rhs); + } + +#ifdef LIBCARLA_INCLUDED_FROM_UE4 + + FloatColor(const FColor &color) + : FloatColor(color.R / 255.f, color.G / 255.f, color.B / 255.f, color.A / 255.f) {} + + FloatColor(const FLinearColor &color) + : FloatColor(color.R, color.G, color.B, color.A) {} + + FColor ToFColor() const { + return FColor{ + static_cast(r * 255), + static_cast(g * 255), + static_cast(b * 255), + static_cast(a * 255)}; + } + + operator FLinearColor() const { + return FLinearColor{ r, g, b, a }; + } + +#endif // LIBCARLA_INCLUDED_FROM_UE4 + + MSGPACK_DEFINE_ARRAY(r, g, b, a); + }; +#pragma pack(pop) + +} // namespace rpc +} // namespace carla diff --git a/LibCarla/source/carla/rpc/MaterialParameter.cpp b/LibCarla/source/carla/rpc/MaterialParameter.cpp new file mode 100644 index 000000000..d453faa5f --- /dev/null +++ b/LibCarla/source/carla/rpc/MaterialParameter.cpp @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#include "MaterialParameter.h" + +namespace carla { +namespace rpc { + +std::string MaterialParameterToString(MaterialParameter material_parameter) +{ + switch(material_parameter) + { + case MaterialParameter::Tex_Normal: return "Normal"; + case MaterialParameter::Tex_Ao_Roughness_Metallic_Emissive: return "AO / Roughness / Metallic / Emissive"; + case MaterialParameter::Tex_BaseColor: return "BaseColor"; + case MaterialParameter::Tex_Emissive: return "Emissive"; + default: return "Invalid"; + } +} + +} +} diff --git a/LibCarla/source/carla/rpc/MaterialParameter.h b/LibCarla/source/carla/rpc/MaterialParameter.h new file mode 100644 index 000000000..27278d215 --- /dev/null +++ b/LibCarla/source/carla/rpc/MaterialParameter.h @@ -0,0 +1,29 @@ +// Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "carla/MsgPack.h" + +#include + +namespace carla { +namespace rpc { + +enum class MaterialParameter +{ + Tex_Normal, + Tex_Ao_Roughness_Metallic_Emissive, + Tex_BaseColor, + Tex_Emissive +}; + +std::string MaterialParameterToString(MaterialParameter material_parameter); + +} // namespace rpc +} // namespace carla + +MSGPACK_ADD_ENUM(carla::rpc::MaterialParameter); diff --git a/LibCarla/source/carla/rpc/Texture.h b/LibCarla/source/carla/rpc/Texture.h new file mode 100644 index 000000000..2eae8afcd --- /dev/null +++ b/LibCarla/source/carla/rpc/Texture.h @@ -0,0 +1,70 @@ +// Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "carla/MsgPack.h" +#include "carla/rpc/FloatColor.h" +#include "carla/sensor/data/Color.h" + +#include + +namespace carla { +namespace rpc { + + template + class Texture { + public: + + Texture() = default; + + Texture(uint32_t width, uint32_t height) + : _width(width), _height(height) { + _texture_data.resize(_width*_height); + } + + uint32_t GetWidth() const { + return _width; + } + + uint32_t GetHeight() const { + return _height; + } + + void SetDimensions(uint32_t width, uint32_t height) { + _width = width; + _height = height; + _texture_data.resize(_width*_height); + } + + T& At (uint32_t x, uint32_t y) { + return _texture_data[y*_width + x]; + } + + const T& At (uint32_t x, uint32_t y) const { + return _texture_data[y*_width + x]; + } + + const T* GetDataPtr() const { + return _texture_data.data(); + } + + private: + + uint32_t _width = 0; + uint32_t _height = 0; + std::vector _texture_data; + + public: + + MSGPACK_DEFINE_ARRAY(_width, _height, _texture_data); + }; + + using TextureColor = Texture; + using TextureFloatColor = Texture; + +} +} diff --git a/LibCarla/source/carla/sensor/data/Color.h b/LibCarla/source/carla/sensor/data/Color.h index f9133d29c..30111ebc7 100644 --- a/LibCarla/source/carla/sensor/data/Color.h +++ b/LibCarla/source/carla/sensor/data/Color.h @@ -7,6 +7,7 @@ #pragma once #include "carla/rpc/Color.h" +#include "carla/rpc/FloatColor.h" #include @@ -36,11 +37,15 @@ namespace data { operator rpc::Color() const { return {r, g, b}; } + operator rpc::FloatColor() const { + return {r/255.f, g/255.f, b/255.f, a/255.f}; + } uint8_t b = 0u; uint8_t g = 0u; uint8_t r = 0u; uint8_t a = 0u; + MSGPACK_DEFINE_ARRAY(r, g, b, a); }; #pragma pack(pop) diff --git a/PythonAPI/carla/source/libcarla/Blueprint.cpp b/PythonAPI/carla/source/libcarla/Blueprint.cpp index 6b2f5ac64..31a68ec64 100644 --- a/PythonAPI/carla/source/libcarla/Blueprint.cpp +++ b/PythonAPI/carla/source/libcarla/Blueprint.cpp @@ -103,6 +103,17 @@ void export_blueprint() { .def(self_ns::str(self_ns::self)) ; + class_("FloatColor") + .def(init( + (arg("r")=0, arg("g")=0.f, arg("b")=0.f, arg("a")=1.0f))) + .def_readwrite("r", &crpc::FloatColor::r) + .def_readwrite("g", &crpc::FloatColor::g) + .def_readwrite("b", &crpc::FloatColor::b) + .def_readwrite("a", &crpc::FloatColor::a) + .def("__eq__", &crpc::FloatColor::operator==) + .def("__ne__", &crpc::FloatColor::operator!=) + ; + class_("OpticalFlowPixel") .def(init( (arg("x")=0, arg("y")=0))) diff --git a/PythonAPI/carla/source/libcarla/SensorData.cpp b/PythonAPI/carla/source/libcarla/SensorData.cpp index 3e61224eb..ca11eaa36 100644 --- a/PythonAPI/carla/source/libcarla/SensorData.cpp +++ b/PythonAPI/carla/source/libcarla/SensorData.cpp @@ -365,7 +365,7 @@ void export_sensor_data() { namespace css = carla::sensor::s11n; // Fake image returned from optical flow to color conversion - // fakes the regular image object + // fakes the regular image object. Only used for visual purposes class_("FakeImage", no_init) .def(vector_indexing_suite>()) .add_property("width", &FakeImage::Width) diff --git a/PythonAPI/carla/source/libcarla/World.cpp b/PythonAPI/carla/source/libcarla/World.cpp index 12d799393..731f56f3d 100644 --- a/PythonAPI/carla/source/libcarla/World.cpp +++ b/PythonAPI/carla/source/libcarla/World.cpp @@ -125,6 +125,7 @@ void export_world() { namespace cc = carla::client; namespace cg = carla::geom; namespace cr = carla::rpc; + namespace csd = carla::sensor::data; class_("Timestamp") .def(init( @@ -245,6 +246,39 @@ void export_world() { .value("All", cr::MapLayer::All) ; + enum_("MaterialParameter") + .value("Normal", cr::MaterialParameter::Tex_Normal) + .value("AO_Roughness_Metallic_Emissive", cr::MaterialParameter::Tex_Ao_Roughness_Metallic_Emissive) + .value("BaseColor", cr::MaterialParameter::Tex_BaseColor) + .value("Emissive", cr::MaterialParameter::Tex_Emissive) + ; + + class_("TextureColor") + .def(init()) + .add_property("width", &cr::TextureColor::GetWidth) + .add_property("height", &cr::TextureColor::GetHeight) + .def("set_dimensions", &cr::TextureColor::SetDimensions) + .def("get", +[](const cr::TextureColor &self, int x, int y) -> csd::Color{ + return self.At(static_cast(x), static_cast(y)); + }) + .def("set", +[](cr::TextureColor &self, int x, int y, csd::Color& value) { + self.At(static_cast(x), static_cast(y)) = value; + }) + ; + + class_("TextureFloatColor") + .def(init()) + .add_property("width", &cr::TextureFloatColor::GetWidth) + .add_property("height", &cr::TextureFloatColor::GetHeight) + .def("set_dimensions", &cr::TextureFloatColor::SetDimensions) + .def("get", +[](const cr::TextureFloatColor &self, int x, int y) -> cr::FloatColor{ + return self.At(static_cast(x), static_cast(y)); + }) + .def("set", +[](cr::TextureFloatColor &self, int x, int y, cr::FloatColor& value) { + self.At(static_cast(x), static_cast(y)) = value; + }) + ; + #define SPAWN_ACTOR_WITHOUT_GIL(fn) +[]( \ cc::World &self, \ const cc::ActorBlueprint &blueprint, \ @@ -299,6 +333,9 @@ void export_world() { .def("cast_ray", CALL_RETURNING_LIST_2(cc::World, CastRay, cg::Location, cg::Location), (arg("initial_location"), arg("final_location"))) .def("project_point", CALL_RETURNING_OPTIONAL_3(cc::World, ProjectPoint, cg::Location, cg::Vector3D, float), (arg("location"), arg("direction"), arg("search_distance")=10000.f)) .def("ground_projection", CALL_RETURNING_OPTIONAL_2(cc::World, GroundProjection, cg::Location, float), (arg("location"), arg("search_distance")=10000.f)) + .def("get_names_of_all_objects", CALL_RETURNING_LIST(cc::World, GetNamesOfAllObjects)) + .def("apply_color_texture_to_object", &cc::World::ApplyColorTextureToObject, (arg("object_name"), arg("material_parameter"), arg("texture"), arg("material_index") = 0)) + .def("apply_float_color_texture_to_object", &cc::World::ApplyFloatColorTextureToObject, (arg("object_name"), arg("material_parameter"), arg("texture"), arg("material_index") = 0)) .def(self_ns::str(self_ns::self)) ; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp index 109dc3a3d..9545d1857 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp @@ -11,6 +11,7 @@ #include "Engine/DecalActor.h" #include "Engine/LevelStreaming.h" #include "Engine/LocalPlayer.h" +#include "Materials/MaterialInstanceDynamic.h" #include #include "carla/opendrive/OpenDriveParser.h" @@ -228,6 +229,135 @@ void ACarlaGameModeBase::BeginPlay() EnableOverlapEvents(); } +TArray ACarlaGameModeBase::GetNamesOfAllActors() +{ + TArray Names; + TArray Actors; + UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors); + for (AActor* Actor : Actors) + { + TArray StaticMeshes; + Actor->GetComponents(StaticMeshes); + if (StaticMeshes.Num()) + { + Names.Add(Actor->GetName()); + } + } + return Names; +} + +AActor* ACarlaGameModeBase::FindActorByName(const FString& ActorName) +{ + TArray Actors; + UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors); + for (AActor* Actor : Actors) + { + if(Actor->GetName() == ActorName) + { + return Actor; + break; + } + } + return nullptr; +} + +UTexture2D* ACarlaGameModeBase::CreateUETexture(const carla::rpc::TextureColor& Texture) +{ + FlushRenderingCommands(); + TArray Colors; + for (int y = 0; y < Texture.GetHeight(); y++) + { + for (int x = 0; x < Texture.GetWidth(); x++) + { + auto& Color = Texture.At(x,y); + Colors.Add(FColor(Color.r, Color.g, Color.b, Color.a)); + } + } + UTexture2D* UETexture = UTexture2D::CreateTransient(Texture.GetWidth(), Texture.GetHeight(), EPixelFormat::PF_B8G8R8A8); + FTexture2DMipMap& Mip = UETexture->PlatformData->Mips[0]; + void* Data = Mip.BulkData.Lock( LOCK_READ_WRITE ); + FMemory::Memcpy( Data, + &Colors[0], + Texture.GetWidth()*Texture.GetHeight()*sizeof(FColor)); + Mip.BulkData.Unlock(); + UETexture->UpdateResource(); + return UETexture; +} + +UTexture2D* ACarlaGameModeBase::CreateUETexture(const carla::rpc::TextureFloatColor& Texture) +{ + FlushRenderingCommands(); + TArray Colors; + for (int x = 0; x < Texture.GetWidth(); x++) + { + for (int y = 0; y < Texture.GetHeight(); y++) + { + auto& Color = Texture.At(x,y); + Colors.Add(FLinearColor(Color.r, Color.g, Color.b, Color.a)); + } + } + UTexture2D* UETexture = UTexture2D::CreateTransient(Texture.GetWidth(), Texture.GetHeight(), EPixelFormat::PF_FloatRGBA); + FTexture2DMipMap& Mip = UETexture->PlatformData->Mips[0]; + void* Data = Mip.BulkData.Lock( LOCK_READ_WRITE ); + FMemory::Memcpy( Data, + &Colors[0], + Texture.GetWidth()*Texture.GetHeight()*sizeof(FFloat16Color)); + Mip.BulkData.Unlock(); + UETexture->UpdateResource(); + return UETexture; +} + +void ACarlaGameModeBase::ApplyTextureToActor( + AActor* Actor, + UTexture2D* Texture, + const carla::rpc::MaterialParameter& TextureParam, + int MaterialIndex) +{ + namespace cr = carla::rpc; + TArray StaticMeshes; + Actor->GetComponents(StaticMeshes); + for (UStaticMeshComponent* Mesh : StaticMeshes) + { + for (int i = 0; i < Mesh->GetNumMaterials(); ++i) + { + UMaterialInterface* OriginalMaterial = Mesh->GetMaterial(i); + UMaterialInstanceDynamic* DynamicMaterial = Cast(OriginalMaterial); + if(!DynamicMaterial) + { + DynamicMaterial = UMaterialInstanceDynamic::Create(OriginalMaterial, NULL); + Mesh->SetMaterial(i, DynamicMaterial); + } + + switch(TextureParam) + { + case cr::MaterialParameter::Tex_BaseColor: + DynamicMaterial->SetTextureParameterValue("BaseColor", Texture); + DynamicMaterial->SetTextureParameterValue("Difuse", Texture); + DynamicMaterial->SetTextureParameterValue("Difuse 2", Texture); + DynamicMaterial->SetTextureParameterValue("Difuse 3", Texture); + DynamicMaterial->SetTextureParameterValue("Difuse 4", Texture); + break; + case cr::MaterialParameter::Tex_Normal: + DynamicMaterial->SetTextureParameterValue("Normal", Texture); + DynamicMaterial->SetTextureParameterValue("Normal 2", Texture); + DynamicMaterial->SetTextureParameterValue("Normal 3", Texture); + DynamicMaterial->SetTextureParameterValue("Normal 4", Texture); + break; + case cr::MaterialParameter::Tex_Emissive: + DynamicMaterial->SetTextureParameterValue("Emissive", Texture); + break; + case cr::MaterialParameter::Tex_Ao_Roughness_Metallic_Emissive: + DynamicMaterial->SetTextureParameterValue("AO / Roughness / Metallic / Emissive", Texture); + DynamicMaterial->SetTextureParameterValue("ORMH", Texture); + DynamicMaterial->SetTextureParameterValue("ORMH 2", Texture); + DynamicMaterial->SetTextureParameterValue("ORMH 3", Texture); + DynamicMaterial->SetTextureParameterValue("ORMH 4", Texture); + break; + } + } + } +} + void ACarlaGameModeBase::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h index c3147aadc..a57d0513e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include "Carla/Actor/CarlaActorFactory.h" @@ -98,6 +100,19 @@ public: return LMManager; } + AActor* FindActorByName(const FString& ActorName); + + UTexture2D* CreateUETexture(const carla::rpc::TextureColor& Texture); + UTexture2D* CreateUETexture(const carla::rpc::TextureFloatColor& Texture); + + void ApplyTextureToActor( + AActor* Actor, + UTexture2D* Texture, + const carla::rpc::MaterialParameter& TextureParam, + int MaterialIndex); + + TArray GetNamesOfAllActors(); + protected: void InitGame(const FString &MapName, const FString &Options, FString &ErrorMessage) override; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index 3ff9c6f02..abfd555de 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -60,6 +60,8 @@ #include #include #include +#include +#include #include #include @@ -333,6 +335,79 @@ void FCarlaServer::FPimpl::BindActions() return R::Success(); }; + BIND_SYNC(apply_color_texture_to_object) << [this]( + const std::string &actor_name, + const cr::MaterialParameter& parameter, + const cr::TextureColor& Texture, + int material_index) -> R + { + REQUIRE_CARLA_EPISODE(); + ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); + if (!GameMode) + { + RESPOND_ERROR("unable to find CARLA game mode"); + } + AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); + if(ActorToPaint == nullptr) + { + RESPOND_ERROR("unable to find Actor to apply the texture"); + } + + UTexture2D* UETexture = GameMode->CreateUETexture(Texture); + + GameMode->ApplyTextureToActor( + ActorToPaint, + UETexture, + parameter, + material_index); + return R::Success(); + }; + + BIND_SYNC(apply_float_color_texture_to_object) << [this]( + const std::string &actor_name, + const cr::MaterialParameter& parameter, + const cr::TextureFloatColor& Texture, + int material_index) -> R + { + REQUIRE_CARLA_EPISODE(); + ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); + if (!GameMode) + { + RESPOND_ERROR("unable to find CARLA game mode"); + } + AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); + if(ActorToPaint == nullptr) + { + RESPOND_ERROR("unable to find Actor to apply the texture"); + } + + UTexture2D* UETexture = GameMode->CreateUETexture(Texture); + + GameMode->ApplyTextureToActor( + ActorToPaint, + UETexture, + parameter, + material_index); + return R::Success(); + }; + + BIND_SYNC(get_names_of_all_objects) << [this]() -> R> + { + REQUIRE_CARLA_EPISODE(); + ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); + if (!GameMode) + { + RESPOND_ERROR("unable to find CARLA game mode"); + } + TArray NamesFString = GameMode->GetNamesOfAllActors(); + std::vector NamesStd; + for (const FString &Name : NamesFString) + { + NamesStd.emplace_back(cr::FromFString(Name)); + } + return NamesStd; + }; + // ~~ Episode settings and info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BIND_SYNC(get_episode_info) << [this]() -> R