From 33c044d49330f54574fd49cd4a50d91e0a73d5d7 Mon Sep 17 00:00:00 2001 From: Axel Date: Fri, 29 Oct 2021 12:57:06 +0200 Subject: [PATCH] Added option to change textures of multiple objects with a single call (saving texture memory). --- LibCarla/source/carla/client/World.cpp | 80 +++++++++++++++++-- LibCarla/source/carla/client/World.h | 30 ++++++- .../source/carla/client/detail/Client.cpp | 18 ++--- LibCarla/source/carla/client/detail/Client.h | 14 ++-- .../source/carla/client/detail/Simulator.cpp | 18 ++--- .../source/carla/client/detail/Simulator.h | 14 ++-- .../source/carla/rpc/MaterialParameter.cpp | 2 +- LibCarla/source/carla/rpc/MaterialParameter.h | 2 +- PythonAPI/carla/source/libcarla/World.cpp | 16 +++- .../Source/Carla/Game/CarlaGameModeBase.cpp | 9 +-- .../Source/Carla/Game/CarlaGameModeBase.h | 3 +- .../Carla/Source/Carla/Server/CarlaServer.cpp | 64 ++++++++++----- 12 files changed, 190 insertions(+), 80 deletions(-) diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index e099a53ac..4e0768680 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -294,22 +294,90 @@ namespace client { 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); + const rpc::TextureColor& Texture) { + _episode.Lock()->ApplyColorTextureToObjects({object_name}, parameter, Texture); + } + + void World::ApplyColorTextureToObjects( + const std::vector &objects_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture) { + _episode.Lock()->ApplyColorTextureToObjects(objects_name, parameter, Texture); } 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); + const rpc::TextureFloatColor& Texture) { + _episode.Lock()->ApplyColorTextureToObjects({object_name}, parameter, Texture); + } + + void World::ApplyFloatColorTextureToObjects( + const std::vector &objects_name, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture) { + _episode.Lock()->ApplyColorTextureToObjects(objects_name, parameter, Texture); } std::vector World::GetNamesOfAllObjects() const { return _episode.Lock()->GetNamesOfAllObjects(); } + void World::ApplyTexturesToObject( + const std::string &object_name, + const rpc::TextureColor& diffuse_texture, + const rpc::TextureFloatColor& emissive_texture, + const rpc::TextureFloatColor& normal_texture, + const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture) + { + if (diffuse_texture.GetWidth() && diffuse_texture.GetHeight()) { + ApplyColorTextureToObject( + object_name, rpc::MaterialParameter::Tex_Diffuse, diffuse_texture); + } + if (normal_texture.GetWidth() && normal_texture.GetHeight()) { + ApplyFloatColorTextureToObject( + object_name, rpc::MaterialParameter::Tex_Normal, normal_texture); + } + if (ao_roughness_metallic_emissive_texture.GetWidth() && + ao_roughness_metallic_emissive_texture.GetHeight()) { + ApplyFloatColorTextureToObject( + object_name, + rpc::MaterialParameter::Tex_Ao_Roughness_Metallic_Emissive, + ao_roughness_metallic_emissive_texture); + } + if (emissive_texture.GetWidth() && emissive_texture.GetHeight()) { + ApplyFloatColorTextureToObject( + object_name, rpc::MaterialParameter::Tex_Emissive, emissive_texture); + } + } + + void World::ApplyTexturesToObjects( + const std::vector &objects_names, + const rpc::TextureColor& diffuse_texture, + const rpc::TextureFloatColor& emissive_texture, + const rpc::TextureFloatColor& normal_texture, + const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture) + { + if (diffuse_texture.GetWidth() && diffuse_texture.GetHeight()) { + ApplyColorTextureToObjects( + objects_names, rpc::MaterialParameter::Tex_Diffuse, diffuse_texture); + } + if (normal_texture.GetWidth() && normal_texture.GetHeight()) { + ApplyFloatColorTextureToObjects( + objects_names, rpc::MaterialParameter::Tex_Normal, normal_texture); + } + if (ao_roughness_metallic_emissive_texture.GetWidth() && + ao_roughness_metallic_emissive_texture.GetHeight()) { + ApplyFloatColorTextureToObjects( + objects_names, + rpc::MaterialParameter::Tex_Ao_Roughness_Metallic_Emissive, + ao_roughness_metallic_emissive_texture); + } + if (emissive_texture.GetWidth() && emissive_texture.GetHeight()) { + ApplyFloatColorTextureToObjects( + objects_names, rpc::MaterialParameter::Tex_Emissive, emissive_texture); + } + } + } // namespace client } // namespace carla diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index c758243d8..0b5ded61e 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -194,14 +194,36 @@ namespace client { void ApplyColorTextureToObject( const std::string &actor_name, const rpc::MaterialParameter& parameter, - const rpc::TextureColor& Texture, - int material_index); + const rpc::TextureColor& Texture); + + void ApplyColorTextureToObjects( + const std::vector &objects_names, + const rpc::MaterialParameter& parameter, + const rpc::TextureColor& Texture); void ApplyFloatColorTextureToObject( const std::string &actor_name, const rpc::MaterialParameter& parameter, - const rpc::TextureFloatColor& Texture, - int material_index); + const rpc::TextureFloatColor& Texture); + + void ApplyFloatColorTextureToObjects( + const std::vector &objects_names, + const rpc::MaterialParameter& parameter, + const rpc::TextureFloatColor& Texture); + + void ApplyTexturesToObject( + const std::string &actor_name, + const rpc::TextureColor& diffuse_texture, + const rpc::TextureFloatColor& emissive_texture, + const rpc::TextureFloatColor& normal_texture, + const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture); + + void ApplyTexturesToObjects( + const std::vector &objects_names, + const rpc::TextureColor& diffuse_texture, + const rpc::TextureFloatColor& emissive_texture, + const rpc::TextureFloatColor& normal_texture, + const rpc::TextureFloatColor& ao_roughness_metallic_emissive_texture); std::vector GetNamesOfAllObjects() const; diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index ab0f01092..eaa11f758 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -162,20 +162,18 @@ namespace detail { _pimpl->CallAndWait("copy_opendrive_to_file", std::move(opendrive), params); } - void Client::ApplyColorTextureToObject( - const std::string &actor_name, + void Client::ApplyColorTextureToObjects( + const std::vector &objects_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); + const rpc::TextureColor& Texture) { + _pimpl->CallAndWait("apply_color_texture_to_objects", objects_name, parameter, Texture); } - void Client::ApplyColorTextureToObject( - const std::string &actor_name, + void Client::ApplyColorTextureToObjects( + const std::vector &objects_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); + const rpc::TextureFloatColor& Texture) { + _pimpl->CallAndWait("apply_float_color_texture_to_objects", objects_name, parameter, Texture); } std::vector Client::GetNamesOfAllObjects() const { diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index 01c97f40a..4ea2c7633 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -105,17 +105,15 @@ namespace detail { void CopyOpenDriveToServer( std::string opendrive, const rpc::OpendriveGenerationParameters & params); - void ApplyColorTextureToObject( - const std::string &actor_name, + void ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureColor& Texture, - int material_index); + const rpc::TextureColor& Texture); - void ApplyColorTextureToObject( - const std::string &actor_name, + void ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureFloatColor& Texture, - int material_index); + const rpc::TextureFloatColor& Texture); std::vector GetNamesOfAllObjects() const; diff --git a/LibCarla/source/carla/client/detail/Simulator.cpp b/LibCarla/source/carla/client/detail/Simulator.cpp index 440b1c1a7..99848578e 100644 --- a/LibCarla/source/carla/client/detail/Simulator.cpp +++ b/LibCarla/source/carla/client/detail/Simulator.cpp @@ -384,20 +384,18 @@ namespace detail { /// -- Texture updating operations // ========================================================================= - void Simulator::ApplyColorTextureToObject( - const std::string &actor_name, + void Simulator::ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureColor& Texture, - int material_index) { - _client.ApplyColorTextureToObject(actor_name, parameter, Texture, material_index); + const rpc::TextureColor& Texture) { + _client.ApplyColorTextureToObjects(objects_name, parameter, Texture); } - void Simulator::ApplyColorTextureToObject( - const std::string &actor_name, + void Simulator::ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureFloatColor& Texture, - int material_index) { - _client.ApplyColorTextureToObject(actor_name, parameter, Texture, material_index); + const rpc::TextureFloatColor& Texture) { + _client.ApplyColorTextureToObjects(objects_name, parameter, Texture); } std::vector Simulator::GetNamesOfAllObjects() const { diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index d0009fa19..424839948 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -682,17 +682,15 @@ namespace detail { // ========================================================================= /// @{ - void ApplyColorTextureToObject( - const std::string &actor_name, + void ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureColor& Texture, - int material_index); + const rpc::TextureColor& Texture); - void ApplyColorTextureToObject( - const std::string &actor_name, + void ApplyColorTextureToObjects( + const std::vector &objects_name, const rpc::MaterialParameter& parameter, - const rpc::TextureFloatColor& Texture, - int material_index); + const rpc::TextureFloatColor& Texture); std::vector GetNamesOfAllObjects() const; diff --git a/LibCarla/source/carla/rpc/MaterialParameter.cpp b/LibCarla/source/carla/rpc/MaterialParameter.cpp index d453faa5f..eb8c7634d 100644 --- a/LibCarla/source/carla/rpc/MaterialParameter.cpp +++ b/LibCarla/source/carla/rpc/MaterialParameter.cpp @@ -15,7 +15,7 @@ std::string MaterialParameterToString(MaterialParameter 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_Diffuse: return "Diffuse"; 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 index 27278d215..0c4d50ac9 100644 --- a/LibCarla/source/carla/rpc/MaterialParameter.h +++ b/LibCarla/source/carla/rpc/MaterialParameter.h @@ -17,7 +17,7 @@ enum class MaterialParameter { Tex_Normal, Tex_Ao_Roughness_Metallic_Emissive, - Tex_BaseColor, + Tex_Diffuse, Tex_Emissive }; diff --git a/PythonAPI/carla/source/libcarla/World.cpp b/PythonAPI/carla/source/libcarla/World.cpp index 731f56f3d..c71892ea7 100644 --- a/PythonAPI/carla/source/libcarla/World.cpp +++ b/PythonAPI/carla/source/libcarla/World.cpp @@ -249,7 +249,7 @@ void export_world() { 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("Diffuse", cr::MaterialParameter::Tex_Diffuse) .value("Emissive", cr::MaterialParameter::Tex_Emissive) ; @@ -334,8 +334,18 @@ void export_world() { .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("apply_color_texture_to_object", &cc::World::ApplyColorTextureToObject, (arg("object_name"), arg("material_parameter"), arg("texture"))) + .def("apply_float_color_texture_to_object", &cc::World::ApplyFloatColorTextureToObject, (arg("object_name"), arg("material_parameter"), arg("texture"))) + .def("apply_textures_to_object", &cc::World::ApplyTexturesToObject, (arg("object_name"), arg("diffuse_texture"), arg("emissive_texture"), arg("normal_texture"), arg("ao_roughness_metallic_emissive_texture"))) + .def("apply_color_texture_to_objects", +[](cc::World &self, boost::python::list &list, const cr::MaterialParameter& parameter, const cr::TextureColor& Texture) { + self.ApplyColorTextureToObjects(PythonLitstToVector(list), parameter, Texture); + }, (arg("objects_name_list"), arg("material_parameter"), arg("texture"))) + .def("apply_float_color_texture_to_objects", +[](cc::World &self, boost::python::list &list, const cr::MaterialParameter& parameter, const cr::TextureFloatColor& Texture) { + self.ApplyFloatColorTextureToObjects(PythonLitstToVector(list), parameter, Texture); + }, (arg("objects_name_list"), arg("material_parameter"), arg("texture"))) + .def("apply_textures_to_objects", +[](cc::World &self, boost::python::list &list, const cr::TextureColor& diffuse_texture, const cr::TextureFloatColor& emissive_texture, const cr::TextureFloatColor& normal_texture, const cr::TextureFloatColor& ao_roughness_metallic_emissive_texture) { + self.ApplyTexturesToObjects(PythonLitstToVector(list), diffuse_texture, emissive_texture, normal_texture, ao_roughness_metallic_emissive_texture); + }, (arg("objects_name_list"), arg("diffuse_texture"), arg("emissive_texture"), arg("normal_texture"), arg("ao_roughness_metallic_emissive_texture"))) .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 9545d1857..1d0d7d9b0 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp @@ -288,9 +288,9 @@ UTexture2D* ACarlaGameModeBase::CreateUETexture(const carla::rpc::TextureFloatCo { FlushRenderingCommands(); TArray Colors; - for (int x = 0; x < Texture.GetWidth(); x++) + for (int y = 0; y < Texture.GetHeight(); y++) { - for (int y = 0; y < Texture.GetHeight(); y++) + for (int x = 0; x < Texture.GetWidth(); x++) { auto& Color = Texture.At(x,y); Colors.Add(FLinearColor(Color.r, Color.g, Color.b, Color.a)); @@ -310,8 +310,7 @@ UTexture2D* ACarlaGameModeBase::CreateUETexture(const carla::rpc::TextureFloatCo void ACarlaGameModeBase::ApplyTextureToActor( AActor* Actor, UTexture2D* Texture, - const carla::rpc::MaterialParameter& TextureParam, - int MaterialIndex) + const carla::rpc::MaterialParameter& TextureParam) { namespace cr = carla::rpc; TArray StaticMeshes; @@ -330,7 +329,7 @@ void ACarlaGameModeBase::ApplyTextureToActor( switch(TextureParam) { - case cr::MaterialParameter::Tex_BaseColor: + case cr::MaterialParameter::Tex_Diffuse: DynamicMaterial->SetTextureParameterValue("BaseColor", Texture); DynamicMaterial->SetTextureParameterValue("Difuse", Texture); DynamicMaterial->SetTextureParameterValue("Difuse 2", Texture); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h index a57d0513e..d0d6bc084 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h @@ -108,8 +108,7 @@ public: void ApplyTextureToActor( AActor* Actor, UTexture2D* Texture, - const carla::rpc::MaterialParameter& TextureParam, - int MaterialIndex); + const carla::rpc::MaterialParameter& TextureParam); TArray GetNamesOfAllActors(); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index abfd555de..f303b2f34 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -335,11 +335,10 @@ void FCarlaServer::FPimpl::BindActions() return R::Success(); }; - BIND_SYNC(apply_color_texture_to_object) << [this]( - const std::string &actor_name, + BIND_SYNC(apply_color_texture_to_objects) << [this]( + const std::vector &actors_name, const cr::MaterialParameter& parameter, - const cr::TextureColor& Texture, - int material_index) -> R + const cr::TextureColor& Texture) -> R { REQUIRE_CARLA_EPISODE(); ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); @@ -347,27 +346,37 @@ void FCarlaServer::FPimpl::BindActions() { RESPOND_ERROR("unable to find CARLA game mode"); } - AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); - if(ActorToPaint == nullptr) + TArray ActorsToPaint; + for(const std::string& actor_name : actors_name) + { + AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); + if (ActorToPaint) + { + ActorsToPaint.Add(ActorToPaint); + } + } + + if(!ActorsToPaint.Num()) { RESPOND_ERROR("unable to find Actor to apply the texture"); } UTexture2D* UETexture = GameMode->CreateUETexture(Texture); - GameMode->ApplyTextureToActor( - ActorToPaint, - UETexture, - parameter, - material_index); + for(AActor* ActorToPaint : ActorsToPaint) + { + GameMode->ApplyTextureToActor( + ActorToPaint, + UETexture, + parameter); + } return R::Success(); }; - BIND_SYNC(apply_float_color_texture_to_object) << [this]( - const std::string &actor_name, + BIND_SYNC(apply_float_color_texture_to_objects) << [this]( + const std::vector &actors_name, const cr::MaterialParameter& parameter, - const cr::TextureFloatColor& Texture, - int material_index) -> R + const cr::TextureFloatColor& Texture) -> R { REQUIRE_CARLA_EPISODE(); ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); @@ -375,19 +384,30 @@ void FCarlaServer::FPimpl::BindActions() { RESPOND_ERROR("unable to find CARLA game mode"); } - AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); - if(ActorToPaint == nullptr) + TArray ActorsToPaint; + for(const std::string& actor_name : actors_name) + { + AActor* ActorToPaint = GameMode->FindActorByName(cr::ToFString(actor_name)); + if (ActorToPaint) + { + ActorsToPaint.Add(ActorToPaint); + } + } + + if(!ActorsToPaint.Num()) { RESPOND_ERROR("unable to find Actor to apply the texture"); } UTexture2D* UETexture = GameMode->CreateUETexture(Texture); - GameMode->ApplyTextureToActor( - ActorToPaint, - UETexture, - parameter, - material_index); + for(AActor* ActorToPaint : ActorsToPaint) + { + GameMode->ApplyTextureToActor( + ActorToPaint, + UETexture, + parameter); + } return R::Success(); };