Skip to content

Commit

Permalink
load vehicle BPs from json settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Apr 10, 2018
1 parent 3bf3829 commit dc2da55
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 94 deletions.
63 changes: 55 additions & 8 deletions AirLib/include/common/AirSimSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ struct AirSimSettings {
}
};

struct CarMeshPaths {
std::string skeletal = "/AirSim/VehicleAdv/Vehicle/Vehicle_SkelMesh.Vehicle_SkelMesh";
std::string bp = "/AirSim/VehicleAdv/Vehicle/VehicleAnimationBlueprint";
std::string slippery_mat = "/AirSim/VehicleAdv/PhysicsMaterials/Slippery.Slippery";
std::string non_slippery_mat = "/AirSim/VehicleAdv/PhysicsMaterials/NonSlippery.NonSlippery";

struct PawnPath {
std::string pawn_bp;
std::string slippery_mat;
std::string non_slippery_mat;

PawnPath(const std::string& pawn_bp_val = "",
const std::string& slippery_mat_val = "/AirSim/VehicleAdv/PhysicsMaterials/Slippery.Slippery",
const std::string& non_slippery_mat_val = "/AirSim/VehicleAdv/PhysicsMaterials/NonSlippery.NonSlippery")
: pawn_bp(pawn_bp_val), slippery_mat(slippery_mat_val), non_slippery_mat(non_slippery_mat_val)
{
}
};

struct VehicleSettings {
Expand Down Expand Up @@ -196,7 +201,7 @@ struct AirSimSettings {
bool engine_sound;
bool log_messages_visible;
HomeGeoPoint origin_geopoint;
CarMeshPaths car_mesh_paths;
std::map<std::string, PawnPath> pawn_paths;

public: //methods
static AirSimSettings& singleton()
Expand Down Expand Up @@ -227,6 +232,7 @@ struct AirSimSettings {
loadCaptureSettings(settings);
loadCameraNoiseSettings(settings);
loadSegmentationSettings(settings);
loadPawnPaths(settings);
loadOtherSettings(settings);

return static_cast<unsigned int>(warning_messages.size());
Expand Down Expand Up @@ -371,7 +377,7 @@ struct AirSimSettings {
else if (view_mode_string == "NoDisplay")
initial_view_mode = 7; // ECameraDirectorMode::CAMREA_DIRECTOR_MODE_NODISPLAY;
else if (view_mode_string == "Front")
initial_view_mode = 8; // ECameraDirectorMode::CAMREA_DIRECTOR_MODE_FRONT;
initial_view_mode = 8; // ECameraDirectorMode::CAMREA_DIRECTOR_MODE_FRONT;
else
warning_messages.push_back("ViewMode setting is not recognized: " + view_mode_string);
}
Expand Down Expand Up @@ -435,6 +441,47 @@ struct AirSimSettings {
}
}

void loadPawnPaths(const Settings& settings)
{
pawn_paths.clear();
pawn_paths.emplace("BareboneCar",
PawnPath("/AirSim/VehicleAdv/Vehicle/VehicleAdvPawn"));
pawn_paths.emplace("DefaultCar",
PawnPath("/AirSim/VehicleAdv/SUV/SuvCarPawn"));
pawn_paths.emplace("DefaultQuadrotor",
PawnPath("/AirSim/Blueprints/BP_FlyingPawn"));


msr::airlib::Settings pawn_paths_child;
if (settings.getChild("PawnPaths", pawn_paths_child)) {
std::vector<std::string> keys;
pawn_paths_child.getChildNames(keys);

for (const auto& key : keys) {
msr::airlib::Settings child;
pawn_paths_child.getChild(key, child);

pawn_paths.emplace(key,
createPathPawn(child));
}
}
}

PawnPath createPathPawn(const Settings& settings)
{
auto paths = PawnPath();
paths.pawn_bp = settings.getString("PawnBP", "");
auto slippery_mat = settings.getString("SlipperyMat", "");
auto non_slippery_mat = settings.getString("NonSlipperyMat", "");

if (slippery_mat != "")
paths.slippery_mat = slippery_mat;
if (non_slippery_mat != "")
paths.non_slippery_mat = non_slippery_mat;

return paths;
}

void loadSegmentationSettings(const Settings& settings)
{
Settings json_parent;
Expand Down
8 changes: 8 additions & 0 deletions AirLib/include/common/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class Settings {
return doc_.size();
}

template<typename Container>
void getChildNames(Container& c) const
{
for (auto it = doc_.begin(); it != doc_.end(); ++it) {
c.push_back(it.key());
}
}

bool getChild(size_t index, Settings& child) const
{
if (doc_.size() > index &&
Expand Down
Binary file modified Unreal/Plugins/AirSim/Content/Blueprints/BP_FlyingPawn.uasset
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ void UAirBlueprintLib::LogMessage(const FString &prefix, const FString &suffix,
switch (level) {
case LogDebugLevel::Informational:
color = FColor(147, 231, 237);
UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
//UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Success:
color = FColor(156, 237, 147);
UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
//UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Failure:
color = FColor(237, 147, 168);
UE_LOG(LogAirSim, Error, TEXT("%s%s"), *prefix, *suffix);
//UE_LOG(LogAirSim, Error, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Unimportant:
color = FColor(237, 228, 147);
UE_LOG(LogAirSim, Verbose, TEXT("%s%s"), *prefix, *suffix);
//UE_LOG(LogAirSim, Verbose, TEXT("%s%s"), *prefix, *suffix);
break;
default: color = FColor::Black; break;
}
Expand Down
1 change: 0 additions & 1 deletion Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "Kismet/KismetMathLibrary.h"
#include "Components/MeshComponent.h"
#include "LandscapeProxy.h"
#include "AirSim.h"
#include "common/AirSimSettings.hpp"
#include "AirBlueprintLib.generated.h"

Expand Down
18 changes: 9 additions & 9 deletions Unreal/Plugins/AirSim/Source/CameraDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void ACameraDirector::setCameras(APIPCamera* external_camera, VehiclePawnWrapper
external_camera_ = external_camera;
follow_actor_ = vehicle_pawn_wrapper->getPawn();
fpv_camera_ = vehicle_pawn_wrapper->getCameraCount() > fpv_camera_index_ ? vehicle_pawn_wrapper->getCamera(fpv_camera_index_) : nullptr;
front_camera_ = vehicle_pawn_wrapper->getCameraCount() > front_camera_index_ ? vehicle_pawn_wrapper->getCamera(front_camera_index_) : nullptr;
front_camera_ = vehicle_pawn_wrapper->getCameraCount() > front_camera_index_ ? vehicle_pawn_wrapper->getCamera(front_camera_index_) : nullptr;
backup_camera_ = backup_camera_index_ >= 0 && vehicle_pawn_wrapper->getCameraCount() > backup_camera_index_ ? vehicle_pawn_wrapper->getCamera(backup_camera_index_) : nullptr;
camera_start_location_ = external_camera_->GetActorLocation();
camera_start_rotation_ = external_camera_->GetActorRotation();
Expand All @@ -95,7 +95,7 @@ void ACameraDirector::setCameras(APIPCamera* external_camera, VehiclePawnWrapper
case ECameraDirectorMode::CAMERA_DIRECTOR_MODE_SPRINGARM_CHASE: inputEventSpringArmChaseView(); break;
case ECameraDirectorMode::CAMERA_DIRECTOR_MODE_BACKUP: inputEventBackupView(); break;
case ECameraDirectorMode::CAMERA_DIRECTOR_MODE_NODISPLAY: inputEventNoDisplayView(); break;
case ECameraDirectorMode::CAMERA_DIRECTOR_MODE_FRONT: inputEventFrontView(); break;
case ECameraDirectorMode::CAMERA_DIRECTOR_MODE_FRONT: inputEventFrontView(); break;
default:
throw std::out_of_range("Unsupported view mode specified in CameraDirector::initializeForBeginPlay");
}
Expand Down Expand Up @@ -201,7 +201,7 @@ void ACameraDirector::setupInputBindings()
UAirBlueprintLib::BindActionToKey("inputEventSpringArmChaseView", EKeys::Slash, this, &ACameraDirector::inputEventSpringArmChaseView);
UAirBlueprintLib::BindActionToKey("inputEventBackupView", EKeys::K, this, &ACameraDirector::inputEventBackupView);
UAirBlueprintLib::BindActionToKey("inputEventNoDisplayView", EKeys::Hyphen, this, &ACameraDirector::inputEventNoDisplayView);
UAirBlueprintLib::BindActionToKey("inputEventFrontView", EKeys::I, this, &ACameraDirector::inputEventFrontView);
UAirBlueprintLib::BindActionToKey("inputEventFrontView", EKeys::I, this, &ACameraDirector::inputEventFrontView);
}


Expand All @@ -217,12 +217,12 @@ void ACameraDirector::inputEventFpvView()

void ACameraDirector::inputEventFrontView()
{
setMode(ECameraDirectorMode::CAMERA_DIRECTOR_MODE_FRONT);
external_camera_->disableMain();
if (backup_camera_)
backup_camera_->disableMain();
if (front_camera_)
front_camera_->showToScreen();
setMode(ECameraDirectorMode::CAMERA_DIRECTOR_MODE_FRONT);
external_camera_->disableMain();
if (backup_camera_)
backup_camera_->disableMain();
if (front_camera_)
front_camera_->showToScreen();
}

void ACameraDirector::inputEventSpringArmChaseView()
Expand Down
21 changes: 11 additions & 10 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ ACarPawn::ACarPawn()

UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());

// load assets
if (UseDefaultMesh) {
static MeshContructionHelpers helpers(AirSimSettings::singleton().car_mesh_paths);
GetMesh()->SetSkeletalMesh(helpers.skeleton);
GetMesh()->SetAnimationMode(EAnimationMode::AnimationBlueprint);
GetMesh()->SetAnimInstanceClass(helpers.bp->GeneratedClass);
SlipperyMaterial = helpers.slippery_mat;
NonSlipperyMaterial = helpers.non_slippery_mat;
}

static ConstructorHelpers::FClassFinder<APIPCamera> pip_camera_class(TEXT("Blueprint'/AirSim/Blueprints/BP_PIPCamera'"));
pip_camera_class_ = pip_camera_class.Succeeded() ? pip_camera_class.Class : nullptr;

const auto& car_mesh_paths = AirSimSettings::singleton().pawn_paths["DefaultCar"];
auto slippery_mat = Cast<UPhysicalMaterial>(
UAirBlueprintLib::LoadObject(car_mesh_paths.slippery_mat));
auto non_slippery_mat = Cast<UPhysicalMaterial>(
UAirBlueprintLib::LoadObject(car_mesh_paths.non_slippery_mat));
if (slippery_mat)
SlipperyMaterial = slippery_mat;
if (non_slippery_mat)
NonSlipperyMaterial = non_slippery_mat;

check(Vehicle4W->WheelSetups.Num() == 4);

// Wheels/Tyres
Expand Down Expand Up @@ -178,6 +178,7 @@ void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_se
else
EngineSoundComponent->Deactivate();


//put camera little bit above vehicle
FTransform camera_transform(FVector::ZeroVector);
FActorSpawnParameters camera_spawn_params;
Expand Down
22 changes: 0 additions & 22 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ class ACarPawn : public AWheeledVehicle
UPROPERTY(Category = Display, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UAudioComponent* EngineSoundComponent;


/** Whether to load the default meshes */
UPROPERTY(Category = Display, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
bool UseDefaultMesh = false;

public:
ACarPawn();

Expand Down Expand Up @@ -163,23 +158,6 @@ class ACarPawn : public AWheeledVehicle
private:
typedef msr::airlib::AirSimSettings AirSimSettings;

struct MeshContructionHelpers {
USkeletalMesh* skeleton;
UBlueprint* bp;
UPhysicalMaterial* slippery_mat;
UPhysicalMaterial* non_slippery_mat;

MeshContructionHelpers(const msr::airlib::AirSimSettings::CarMeshPaths& paths)
{
skeleton = Cast<USkeletalMesh>(UAirBlueprintLib::LoadObject(paths.skeletal));
bp = Cast<UBlueprint>(UAirBlueprintLib::LoadObject(paths.bp));
slippery_mat = Cast<UPhysicalMaterial>(UAirBlueprintLib::LoadObject(paths.slippery_mat));
non_slippery_mat = Cast<UPhysicalMaterial>(UAirBlueprintLib::LoadObject(paths.non_slippery_mat));
}

};


UClass* pip_camera_class_;

std::unique_ptr<msr::airlib::CarRpcLibServer> rpclib_server_;
Expand Down
67 changes: 32 additions & 35 deletions Unreal/Plugins/AirSim/Source/Car/SimModeCar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ ASimModeCar::ASimModeCar()
static ConstructorHelpers::FClassFinder<ACameraDirector> camera_director_class(TEXT("Blueprint'/AirSim/Blueprints/BP_CameraDirector'"));
camera_director_class_ = camera_director_class.Succeeded() ? camera_director_class.Class : nullptr;

//Try to find the high polycount vehicle
//If not found, spawn the default class (go-kart)
static ConstructorHelpers::FClassFinder<ACarPawn> vehicle_pawn_class(TEXT("Blueprint'/AirSim/VehicleAdv/SUV/SuvCarPawn'"));
if (vehicle_pawn_class.Succeeded()) {
vehicle_pawn_class_ = vehicle_pawn_class.Class;
follow_distance_ = -800;
}
else {
vehicle_pawn_class_ = ACarPawn::StaticClass();
follow_distance_ = -225;
}
follow_distance_ = -800;
}

void ASimModeCar::BeginPlay()
Expand Down Expand Up @@ -61,29 +51,6 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
//we will either find external camera if it already exist in evironment or create one
APIPCamera* external_camera;

//find all BP camera directors in the environment
{
TArray<AActor*> camera_dirs;
UAirBlueprintLib::FindAllActor<ACameraDirector>(this, camera_dirs);
if (camera_dirs.Num() == 0) {
//create director
FActorSpawnParameters camera_spawn_params;
camera_spawn_params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
CameraDirector = this->GetWorld()->SpawnActor<ACameraDirector>(camera_director_class_, camera_transform, camera_spawn_params);
CameraDirector->setFollowDistance(follow_distance_);
CameraDirector->setCameraRotationLagEnabled(true);
CameraDirector->setFpvCameraIndex(3);
spawned_actors_.Add(CameraDirector);

//create external camera required for the director
external_camera = this->GetWorld()->SpawnActor<APIPCamera>(external_camera_class_, camera_transform, camera_spawn_params);
spawned_actors_.Add(external_camera);
}
else {
CameraDirector = static_cast<ACameraDirector*>(camera_dirs[0]);
external_camera = CameraDirector->getExternalCamera();
}
}

//find all vehicle pawns
{
Expand All @@ -96,8 +63,13 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
FActorSpawnParameters pawn_spawn_params;
pawn_spawn_params.SpawnCollisionHandlingOverride =
ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;

auto vehicle_bp = Cast<UBlueprint>(UAirBlueprintLib::LoadObject(
getSettings().pawn_paths.at("DefaultCar").pawn_bp));

TVehiclePawn* spawned_pawn = this->GetWorld()->SpawnActor<TVehiclePawn>(
vehicle_pawn_class_, actor_transform, pawn_spawn_params);
vehicle_bp->GeneratedClass,
actor_transform, pawn_spawn_params);

spawned_actors_.Add(spawned_pawn);
pawns.Add(spawned_pawn);
Expand All @@ -122,6 +94,31 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
}
}

//find all BP camera directors in the environment
{
TArray<AActor*> camera_dirs;
UAirBlueprintLib::FindAllActor<ACameraDirector>(this, camera_dirs);
if (camera_dirs.Num() == 0) {
//create director
FActorSpawnParameters camera_spawn_params;
camera_spawn_params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
CameraDirector = this->GetWorld()->SpawnActor<ACameraDirector>(camera_director_class_, camera_transform, camera_spawn_params);
CameraDirector->setFollowDistance(follow_distance_);
CameraDirector->setCameraRotationLagEnabled(true);
CameraDirector->setFpvCameraIndex(3);
spawned_actors_.Add(CameraDirector);

//create external camera required for the director
external_camera = this->GetWorld()->SpawnActor<APIPCamera>(external_camera_class_, camera_transform, camera_spawn_params);
spawned_actors_.Add(external_camera);
}
else {
CameraDirector = static_cast<ACameraDirector*>(camera_dirs[0]);
external_camera = CameraDirector->getExternalCamera();
}
}

fpv_vehicle_pawn_wrapper_->possess();
CameraDirector->initializeForBeginPlay(getInitialViewMode(), fpv_vehicle_pawn_wrapper_, external_camera);
}

Expand Down
1 change: 0 additions & 1 deletion Unreal/Plugins/AirSim/Source/Car/SimModeCar.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class AIRSIM_API ASimModeCar : public ASimModeBase
private:
UClass* external_camera_class_;
UClass* camera_director_class_;
UClass* vehicle_pawn_class_;

TArray<AActor*> spawned_actors_;
std::vector<VehiclePtr> vehicles_;
Expand Down
Loading

0 comments on commit dc2da55

Please sign in to comment.