Added option to change textures of multiple objects with a single call (saving texture memory).

This commit is contained in:
Axel 2021-10-29 12:57:06 +02:00 committed by bernat
parent c99e5adf2a
commit 33c044d493
12 changed files with 190 additions and 80 deletions

View File

@ -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<std::string> &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<std::string> &objects_name,
const rpc::MaterialParameter& parameter,
const rpc::TextureFloatColor& Texture) {
_episode.Lock()->ApplyColorTextureToObjects(objects_name, parameter, Texture);
}
std::vector<std::string> 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<std::string> &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

View File

@ -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<std::string> &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<std::string> &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<std::string> &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<std::string> GetNamesOfAllObjects() const;

View File

@ -162,20 +162,18 @@ namespace detail {
_pimpl->CallAndWait<void>("copy_opendrive_to_file", std::move(opendrive), params);
}
void Client::ApplyColorTextureToObject(
const std::string &actor_name,
void Client::ApplyColorTextureToObjects(
const std::vector<std::string> &objects_name,
const rpc::MaterialParameter& parameter,
const rpc::TextureColor& Texture,
int material_index) {
_pimpl->CallAndWait<void>("apply_color_texture_to_object", actor_name, parameter, Texture, material_index);
const rpc::TextureColor& Texture) {
_pimpl->CallAndWait<void>("apply_color_texture_to_objects", objects_name, parameter, Texture);
}
void Client::ApplyColorTextureToObject(
const std::string &actor_name,
void Client::ApplyColorTextureToObjects(
const std::vector<std::string> &objects_name,
const rpc::MaterialParameter& parameter,
const rpc::TextureFloatColor& Texture,
int material_index) {
_pimpl->CallAndWait<void>("apply_float_color_texture_to_object", actor_name, parameter, Texture, material_index);
const rpc::TextureFloatColor& Texture) {
_pimpl->CallAndWait<void>("apply_float_color_texture_to_objects", objects_name, parameter, Texture);
}
std::vector<std::string> Client::GetNamesOfAllObjects() const {

View File

@ -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<std::string> &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<std::string> &objects_name,
const rpc::MaterialParameter& parameter,
const rpc::TextureFloatColor& Texture,
int material_index);
const rpc::TextureFloatColor& Texture);
std::vector<std::string> GetNamesOfAllObjects() const;

View File

@ -384,20 +384,18 @@ namespace detail {
/// -- Texture updating operations
// =========================================================================
void Simulator::ApplyColorTextureToObject(
const std::string &actor_name,
void Simulator::ApplyColorTextureToObjects(
const std::vector<std::string> &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<std::string> &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<std::string> Simulator::GetNamesOfAllObjects() const {

View File

@ -682,17 +682,15 @@ namespace detail {
// =========================================================================
/// @{
void ApplyColorTextureToObject(
const std::string &actor_name,
void ApplyColorTextureToObjects(
const std::vector<std::string> &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<std::string> &objects_name,
const rpc::MaterialParameter& parameter,
const rpc::TextureFloatColor& Texture,
int material_index);
const rpc::TextureFloatColor& Texture);
std::vector<std::string> GetNamesOfAllObjects() const;

View File

@ -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";
}

View File

@ -17,7 +17,7 @@ enum class MaterialParameter
{
Tex_Normal,
Tex_Ao_Roughness_Metallic_Emissive,
Tex_BaseColor,
Tex_Diffuse,
Tex_Emissive
};

View File

@ -249,7 +249,7 @@ void export_world() {
enum_<cr::MaterialParameter>("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<std::string>(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<std::string>(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<std::string>(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))
;

View File

@ -288,9 +288,9 @@ UTexture2D* ACarlaGameModeBase::CreateUETexture(const carla::rpc::TextureFloatCo
{
FlushRenderingCommands();
TArray<FFloat16Color> 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<UStaticMeshComponent*> 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);

View File

@ -108,8 +108,7 @@ public:
void ApplyTextureToActor(
AActor* Actor,
UTexture2D* Texture,
const carla::rpc::MaterialParameter& TextureParam,
int MaterialIndex);
const carla::rpc::MaterialParameter& TextureParam);
TArray<FString> GetNamesOfAllActors();

View File

@ -335,11 +335,10 @@ void FCarlaServer::FPimpl::BindActions()
return R<void>::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<std::string> &actors_name,
const cr::MaterialParameter& parameter,
const cr::TextureColor& Texture,
int material_index) -> R<void>
const cr::TextureColor& Texture) -> R<void>
{
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<AActor*> 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<void>::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<std::string> &actors_name,
const cr::MaterialParameter& parameter,
const cr::TextureFloatColor& Texture,
int material_index) -> R<void>
const cr::TextureFloatColor& Texture) -> R<void>
{
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<AActor*> 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<void>::Success();
};