Added functions and structures to update textures in runtime.
This commit is contained in:
parent
a1ecf54f96
commit
c99e5adf2a
|
@ -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<std::string> World::GetNamesOfAllObjects() const {
|
||||
return _episode.Lock()->GetNamesOfAllObjects();
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace carla
|
||||
|
|
|
@ -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 <boost/optional.hpp>
|
||||
|
||||
|
@ -187,6 +189,22 @@ namespace client {
|
|||
std::vector<SharedPtr<Actor>> GetTrafficLightsInJunction(
|
||||
const road::JuncId junc_id) const;
|
||||
|
||||
// std::vector<std::string> 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<std::string> GetNamesOfAllObjects() const;
|
||||
|
||||
private:
|
||||
|
||||
detail::EpisodeProxy _episode;
|
||||
|
|
|
@ -162,6 +162,26 @@ namespace detail {
|
|||
_pimpl->CallAndWait<void>("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<void>("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<void>("apply_float_color_texture_to_object", actor_name, parameter, Texture, material_index);
|
||||
}
|
||||
|
||||
std::vector<std::string> Client::GetNamesOfAllObjects() const {
|
||||
return _pimpl->CallAndWait<std::vector<std::string>>("get_names_of_all_objects");
|
||||
}
|
||||
|
||||
rpc::EpisodeInfo Client::GetEpisodeInfo() {
|
||||
return _pimpl->CallAndWait<rpc::EpisodeInfo>("get_episode_info");
|
||||
}
|
||||
|
|
|
@ -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 <functional>
|
||||
#include <memory>
|
||||
|
@ -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<std::string> GetNamesOfAllObjects() const;
|
||||
|
||||
rpc::EpisodeInfo GetEpisodeInfo();
|
||||
|
||||
rpc::MapInfo GetMapInfo();
|
||||
|
|
|
@ -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<std::string> Simulator::GetNamesOfAllObjects() const {
|
||||
return _client.GetNamesOfAllObjects();
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace client
|
||||
} // namespace carla
|
||||
|
|
|
@ -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 <boost/optional.hpp>
|
||||
|
||||
|
@ -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<std::string> GetNamesOfAllObjects() const;
|
||||
|
||||
/// @}
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -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 <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "carla/MsgPack.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#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<uint8>(r * 255),
|
||||
static_cast<uint8>(g * 255),
|
||||
static_cast<uint8>(b * 255),
|
||||
static_cast<uint8>(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
|
|
@ -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 <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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 <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "carla/MsgPack.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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);
|
|
@ -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 <https://opensource.org/licenses/MIT>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "carla/MsgPack.h"
|
||||
#include "carla/rpc/FloatColor.h"
|
||||
#include "carla/sensor/data/Color.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace carla {
|
||||
namespace rpc {
|
||||
|
||||
template<typename T>
|
||||
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<T> _texture_data;
|
||||
|
||||
public:
|
||||
|
||||
MSGPACK_DEFINE_ARRAY(_width, _height, _texture_data);
|
||||
};
|
||||
|
||||
using TextureColor = Texture<sensor::data::Color>;
|
||||
using TextureFloatColor = Texture<FloatColor>;
|
||||
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "carla/rpc/Color.h"
|
||||
#include "carla/rpc/FloatColor.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
|
|
@ -103,6 +103,17 @@ void export_blueprint() {
|
|||
.def(self_ns::str(self_ns::self))
|
||||
;
|
||||
|
||||
class_<crpc::FloatColor>("FloatColor")
|
||||
.def(init<float, float, float, float>(
|
||||
(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_<csd::OpticalFlowPixel>("OpticalFlowPixel")
|
||||
.def(init<float, float>(
|
||||
(arg("x")=0, arg("y")=0)))
|
||||
|
|
|
@ -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>("FakeImage", no_init)
|
||||
.def(vector_indexing_suite<std::vector<uint8_t>>())
|
||||
.add_property("width", &FakeImage::Width)
|
||||
|
|
|
@ -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_<cc::Timestamp>("Timestamp")
|
||||
.def(init<size_t, double, double, double>(
|
||||
|
@ -245,6 +246,39 @@ void export_world() {
|
|||
.value("All", cr::MapLayer::All)
|
||||
;
|
||||
|
||||
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("Emissive", cr::MaterialParameter::Tex_Emissive)
|
||||
;
|
||||
|
||||
class_<cr::TextureColor>("TextureColor")
|
||||
.def(init<uint32_t, uint32_t>())
|
||||
.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<uint32_t>(x), static_cast<uint32_t>(y));
|
||||
})
|
||||
.def("set", +[](cr::TextureColor &self, int x, int y, csd::Color& value) {
|
||||
self.At(static_cast<uint32_t>(x), static_cast<uint32_t>(y)) = value;
|
||||
})
|
||||
;
|
||||
|
||||
class_<cr::TextureFloatColor>("TextureFloatColor")
|
||||
.def(init<uint32_t, uint32_t>())
|
||||
.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<uint32_t>(x), static_cast<uint32_t>(y));
|
||||
})
|
||||
.def("set", +[](cr::TextureFloatColor &self, int x, int y, cr::FloatColor& value) {
|
||||
self.At(static_cast<uint32_t>(x), static_cast<uint32_t>(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))
|
||||
;
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include "Engine/DecalActor.h"
|
||||
#include "Engine/LevelStreaming.h"
|
||||
#include "Engine/LocalPlayer.h"
|
||||
#include "Materials/MaterialInstanceDynamic.h"
|
||||
|
||||
#include <compiler/disable-ue4-macros.h>
|
||||
#include "carla/opendrive/OpenDriveParser.h"
|
||||
|
@ -228,6 +229,135 @@ void ACarlaGameModeBase::BeginPlay()
|
|||
EnableOverlapEvents();
|
||||
}
|
||||
|
||||
TArray<FString> ACarlaGameModeBase::GetNamesOfAllActors()
|
||||
{
|
||||
TArray<FString> Names;
|
||||
TArray<AActor*> Actors;
|
||||
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors);
|
||||
for (AActor* Actor : Actors)
|
||||
{
|
||||
TArray<UStaticMeshComponent*> StaticMeshes;
|
||||
Actor->GetComponents(StaticMeshes);
|
||||
if (StaticMeshes.Num())
|
||||
{
|
||||
Names.Add(Actor->GetName());
|
||||
}
|
||||
}
|
||||
return Names;
|
||||
}
|
||||
|
||||
AActor* ACarlaGameModeBase::FindActorByName(const FString& ActorName)
|
||||
{
|
||||
TArray<AActor*> 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<FColor> 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<FFloat16Color> 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<UStaticMeshComponent*> StaticMeshes;
|
||||
Actor->GetComponents(StaticMeshes);
|
||||
for (UStaticMeshComponent* Mesh : StaticMeshes)
|
||||
{
|
||||
for (int i = 0; i < Mesh->GetNumMaterials(); ++i)
|
||||
{
|
||||
UMaterialInterface* OriginalMaterial = Mesh->GetMaterial(i);
|
||||
UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(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);
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include <compiler/disable-ue4-macros.h>
|
||||
#include <boost/optional.hpp>
|
||||
#include <carla/rpc/Texture.h>
|
||||
#include <carla/rpc/MaterialParameter.h>
|
||||
#include <compiler/enable-ue4-macros.h>
|
||||
|
||||
#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<FString> GetNamesOfAllActors();
|
||||
|
||||
protected:
|
||||
|
||||
void InitGame(const FString &MapName, const FString &Options, FString &ErrorMessage) override;
|
||||
|
|
|
@ -60,6 +60,8 @@
|
|||
#include <carla/rpc/VehicleWheels.h>
|
||||
#include <carla/rpc/WeatherParameters.h>
|
||||
#include <carla/streaming/Server.h>
|
||||
#include <carla/rpc/Texture.h>
|
||||
#include <carla/rpc/MaterialParameter.h>
|
||||
#include <compiler/enable-ue4-macros.h>
|
||||
|
||||
#include <vector>
|
||||
|
@ -333,6 +335,79 @@ void FCarlaServer::FPimpl::BindActions()
|
|||
return R<void>::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<void>
|
||||
{
|
||||
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<void>::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<void>
|
||||
{
|
||||
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<void>::Success();
|
||||
};
|
||||
|
||||
BIND_SYNC(get_names_of_all_objects) << [this]() -> R<std::vector<std::string>>
|
||||
{
|
||||
REQUIRE_CARLA_EPISODE();
|
||||
ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld());
|
||||
if (!GameMode)
|
||||
{
|
||||
RESPOND_ERROR("unable to find CARLA game mode");
|
||||
}
|
||||
TArray<FString> NamesFString = GameMode->GetNamesOfAllActors();
|
||||
std::vector<std::string> 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<cr::EpisodeInfo>
|
||||
|
|
Loading…
Reference in New Issue