Skip to content

Commit

Permalink
settings appliable to external camera as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Jan 12, 2018
1 parent cb5f8fa commit 71c99cf
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 70 deletions.
16 changes: 10 additions & 6 deletions AirLib/include/common/AirSimSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <string>
#include <vector>
#include <exception>
#include <functional>
#include "Settings.hpp"
#include "CommonStructs.hpp"
Expand Down Expand Up @@ -75,6 +76,7 @@ struct AirSimSettings {

unsigned int width = 256, height = 144; //960 X 540
float fov_degrees = Utils::nan<float>(); //90.0f
int auto_exposure_method = -1; //histogram
float auto_exposure_speed = Utils::nan<float>(); // 100.0f;
float auto_exposure_bias = Utils::nan<float>(); // 0;
float auto_exposure_max_brightness = Utils::nan<float>(); // 0.64f;
Expand Down Expand Up @@ -347,16 +349,17 @@ struct AirSimSettings {
void loadCaptureSettings(const Settings& settings)
{
Settings json_settings_parent;
if (settings.getChild("CaptureSetting", json_settings_parent)) {
if (settings.getChild("CaptureSettings", json_settings_parent)) {
for (size_t child_index = 0; child_index < json_settings_parent.size(); ++child_index) {
Settings json_settings_child;
if (json_settings_parent.getChild(child_index, json_settings_child)) {
CaptureSetting capture_setting;
createCaptureSettings(json_settings_child, capture_setting);
if (capture_setting.image_type >= 0 && capture_setting.image_type < capture_settings.size())
if (capture_setting.image_type >= -1 && capture_setting.image_type < static_cast<int>(capture_settings.size()))
capture_settings[capture_setting.image_type] = capture_setting;
else
throw std::invalid_argument("ImageType must be >= 0 and < " + std::to_string(capture_settings.size()));
//TODO: below exception doesn't actually get raised right now because of issue in Unreal Engine?
throw std::invalid_argument(std::string("ImageType must be >= -1 and < ") + std::to_string(capture_settings.size()));
}
}
}
Expand All @@ -365,16 +368,17 @@ struct AirSimSettings {
void loadCameraNoiseSettings(const Settings& settings)
{
Settings json_settings_parent;
if (settings.getChild("NoiseSetting", json_settings_parent)) {
if (settings.getChild("NoiseSettings", json_settings_parent)) {
for (size_t child_index = 0; child_index < json_settings_parent.size(); ++child_index) {
Settings json_settings_child;
if (json_settings_parent.getChild(child_index, json_settings_child)) {
NoiseSetting noise_setting;
createNoiseSettings(json_settings_child, noise_setting);
if (noise_setting.ImageType >= 0 && noise_setting.ImageType < noise_settings.size())
if (noise_setting.ImageType >= -1 && noise_setting.ImageType < static_cast<int>(noise_settings.size()))
noise_settings[noise_setting.ImageType] = noise_setting;
else
throw std::invalid_argument("ImageType must be >= 0 and < " + std::to_string(noise_settings.size()));
//TODO: below exception doesn't actually get raised right now because of issue in Unreal Engine?
throw std::invalid_argument("ImageType must be >= -1 and < " + std::to_string(noise_settings.size()));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Unreal/Plugins/AirSim/Source/AirSim.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public AirSim(ReadOnlyTargetRules Target) : base(Target)
{
//bEnforceIWYU = true; //to support 4.16
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
//below is no longer supported in 4.16
bEnableExceptions = true;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ImageWrapper", "RenderCore", "RHI", "PhysXVehicles", "Landscape" });
Expand Down
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/AirSimGameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "common/Common.hpp"
#include "AirBlueprintLib.h"



class AUnrealLog : public msr::airlib::Utils::Logger
{
Expand Down
17 changes: 17 additions & 0 deletions Unreal/Plugins/AirSim/Source/CameraDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ void ACameraDirector::initializeForBeginPlay(ECameraDirectorMode view_mode, Vehi
setCameras(external_camera, vehicle_pawn_wrapper);
}

void ACameraDirector::setupCameraFromSettings()
{
typedef msr::airlib::AirSimSettings AirSimSettings;
typedef msr::airlib::ImageCaptureBase::ImageType ImageType;

if (!external_camera_)
return;

int image_count = static_cast<int>(Utils::toNumeric(ImageType::Count));
for (int image_type = -1; image_type < image_count; ++image_type) {
external_camera_->setImageTypeSettings(image_type, AirSimSettings::singleton().capture_settings[image_type],
AirSimSettings::singleton().noise_settings[image_type]);
}
}

void ACameraDirector::setCameras(APIPCamera* external_camera, VehiclePawnWrapper* vehicle_pawn_wrapper)
{
external_camera_ = external_camera;
Expand All @@ -76,6 +91,8 @@ void ACameraDirector::setCameras(APIPCamera* external_camera, VehiclePawnWrapper
default:
throw std::out_of_range("Unknown view mode specified in CameraDirector::initializeForBeginPlay");
}

setupCameraFromSettings();
}

void ACameraDirector::attachSpringArm(bool attach)
Expand Down
5 changes: 5 additions & 0 deletions Unreal/Plugins/AirSim/Source/CameraDirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "PIPCamera.h"
#include "GameFramework/Actor.h"
#include "ManualPoseController.h"
#include "common/common_utils/Utils.hpp"
#include "GameFramework/SpringArmComponent.h"
#include "CameraDirector.generated.h"

Expand Down Expand Up @@ -68,9 +69,13 @@ class AIRSIM_API ACameraDirector : public AActor
void setupInputBindings();
void attachSpringArm(bool attach);
void disableNonExternalCameras();
void setupCameraFromSettings();


private:
typedef common_utils::Utils Utils;


APIPCamera* fpv_camera_;
APIPCamera* backup_camera_;
APIPCamera* external_camera_;
Expand Down
139 changes: 80 additions & 59 deletions Unreal/Plugins/AirSim/Source/PIPCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,6 @@ void APIPCamera::BeginPlay()
}
}

void APIPCamera::setNoiseMaterial(FPostProcessSettings& obj, const NoiseSetting& settings)
{
if (!settings.Enabled)
return;

noise_material_ = UMaterialInstanceDynamic::Create(noise_material_static_, GetWorld());

noise_material_->SetScalarParameterValue("HorzWaveStrength", settings.HorzWaveStrength);
noise_material_->SetScalarParameterValue("RandSpeed", settings.RandSpeed);
noise_material_->SetScalarParameterValue("RandSize", settings.RandSize);
noise_material_->SetScalarParameterValue("RandDensity", settings.RandDensity);
noise_material_->SetScalarParameterValue("RandContrib", settings.RandContrib);
noise_material_->SetScalarParameterValue("HorzWaveContrib", settings.HorzWaveContrib);
noise_material_->SetScalarParameterValue("HorzWaveVertSize", settings.HorzWaveVertSize);
noise_material_->SetScalarParameterValue("HorzWaveScreenSize", settings.HorzWaveScreenSize);
noise_material_->SetScalarParameterValue("HorzNoiseLinesContrib", settings.HorzNoiseLinesContrib);
noise_material_->SetScalarParameterValue("HorzNoiseLinesDensityY", settings.HorzNoiseLinesDensityY);
noise_material_->SetScalarParameterValue("HorzNoiseLinesDensityXY", settings.HorzNoiseLinesDensityXY);
noise_material_->SetScalarParameterValue("HorzDistortionStrength", settings.HorzDistortionStrength);
noise_material_->SetScalarParameterValue("HorzDistortionContrib", settings.HorzDistortionContrib);

obj.AddBlendable(noise_material_, 1.0f);
}

void APIPCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
noise_material_static_ = nullptr;
Expand Down Expand Up @@ -129,47 +105,92 @@ void APIPCamera::setCameraTypeEnabled(ImageType type, bool enabled)
void APIPCamera::setImageTypeSettings(int image_type, const APIPCamera::CaptureSetting& capture_setting,
const APIPCamera::NoiseSetting& noise_setting)
{
updateCaptureComponentSetting(captures_[image_type], render_targets_[image_type],
capture_setting);
if (image_type >= 0) { //scene captue components
updateCaptureComponentSetting(captures_[image_type], render_targets_[image_type],
capture_setting);

setNoiseMaterial(captures_[image_type]->PostProcessSettings, noise_setting);
}
else { //camera component
updateCameraSetting(camera_, capture_setting);

setNoiseMaterial(camera_->PostProcessSettings, noise_setting);
}



setNoiseMaterial(captures_[image_type]->PostProcessSettings, noise_setting);
}

void APIPCamera::updateCaptureComponentSetting(USceneCaptureComponent2D* capture, UTextureRenderTarget2D* render_target, const CaptureSetting& setting)
{
if (render_target) {
render_target->InitAutoFormat(setting.width, setting.height); //256 X 144, X 480
if (!std::isnan(setting.target_gamma))
render_target->TargetGamma = setting.target_gamma;
}
//else we will set this after this components get created

if (capture) {
if (!std::isnan(setting.fov_degrees))
capture->FOVAngle = setting.fov_degrees;
if (!std::isnan(setting.motion_blur_amount))
capture->PostProcessSettings.MotionBlurAmount = setting.motion_blur_amount;

capture->PostProcessSettings.AutoExposureMethod = EAutoExposureMethod::AEM_Histogram;
if (!std::isnan(setting.auto_exposure_speed))
capture->PostProcessSettings.AutoExposureSpeedDown = capture->PostProcessSettings.AutoExposureSpeedUp = setting.auto_exposure_speed;
if (!std::isnan(setting.auto_exposure_max_brightness))
capture->PostProcessSettings.AutoExposureMaxBrightness = setting.auto_exposure_max_brightness;
if (!std::isnan(setting.auto_exposure_min_brightness))
capture->PostProcessSettings.AutoExposureMinBrightness = setting.auto_exposure_min_brightness;
if (!std::isnan(setting.auto_exposure_bias))
capture->PostProcessSettings.AutoExposureBias = setting.auto_exposure_bias;
if (!std::isnan(setting.auto_exposure_low_percent))
capture->PostProcessSettings.AutoExposureLowPercent = setting.auto_exposure_low_percent;
if (!std::isnan(setting.auto_exposure_high_percent))
capture->PostProcessSettings.AutoExposureHighPercent = setting.auto_exposure_high_percent;
if (!std::isnan(setting.auto_exposure_histogram_log_min))
capture->PostProcessSettings.HistogramLogMin = setting.auto_exposure_histogram_log_min;
if (!std::isnan(setting.auto_exposure_histogram_log_max))
capture->PostProcessSettings.HistogramLogMax = setting.auto_exposure_histogram_log_max;
render_target->InitAutoFormat(setting.width, setting.height); //256 X 144, X 480
if (!std::isnan(setting.target_gamma))
render_target->TargetGamma = setting.target_gamma;

}
//else we will set this after this components get created
if (!std::isnan(setting.fov_degrees))
capture->FOVAngle = setting.fov_degrees;

updateCameraPostProcessingSetting(capture->PostProcessSettings, setting);
}

void APIPCamera::updateCameraSetting(UCameraComponent* camera, const CaptureSetting& setting)
{
//if (!std::isnan(setting.target_gamma))
// camera-> = setting.target_gamma;

if (!std::isnan(setting.fov_degrees))
camera->SetFieldOfView(setting.fov_degrees);

updateCameraPostProcessingSetting(camera->PostProcessSettings, setting);
}


void APIPCamera::updateCameraPostProcessingSetting(FPostProcessSettings& obj, const CaptureSetting& setting)
{
if (!std::isnan(setting.motion_blur_amount))
obj.MotionBlurAmount = setting.motion_blur_amount;
if (setting.auto_exposure_method >= 0)
obj.AutoExposureMethod = Utils::toEnum<EAutoExposureMethod>(setting.auto_exposure_method);
if (!std::isnan(setting.auto_exposure_speed))
obj.AutoExposureSpeedDown = obj.AutoExposureSpeedUp = setting.auto_exposure_speed;
if (!std::isnan(setting.auto_exposure_max_brightness))
obj.AutoExposureMaxBrightness = setting.auto_exposure_max_brightness;
if (!std::isnan(setting.auto_exposure_min_brightness))
obj.AutoExposureMinBrightness = setting.auto_exposure_min_brightness;
if (!std::isnan(setting.auto_exposure_bias))
obj.AutoExposureBias = setting.auto_exposure_bias;
if (!std::isnan(setting.auto_exposure_low_percent))
obj.AutoExposureLowPercent = setting.auto_exposure_low_percent;
if (!std::isnan(setting.auto_exposure_high_percent))
obj.AutoExposureHighPercent = setting.auto_exposure_high_percent;
if (!std::isnan(setting.auto_exposure_histogram_log_min))
obj.HistogramLogMin = setting.auto_exposure_histogram_log_min;
if (!std::isnan(setting.auto_exposure_histogram_log_max))
obj.HistogramLogMax = setting.auto_exposure_histogram_log_max;
}

void APIPCamera::setNoiseMaterial(FPostProcessSettings& obj, const NoiseSetting& settings)
{
if (!settings.Enabled)
return;

noise_material_ = UMaterialInstanceDynamic::Create(noise_material_static_, GetWorld());

noise_material_->SetScalarParameterValue("HorzWaveStrength", settings.HorzWaveStrength);
noise_material_->SetScalarParameterValue("RandSpeed", settings.RandSpeed);
noise_material_->SetScalarParameterValue("RandSize", settings.RandSize);
noise_material_->SetScalarParameterValue("RandDensity", settings.RandDensity);
noise_material_->SetScalarParameterValue("RandContrib", settings.RandContrib);
noise_material_->SetScalarParameterValue("HorzWaveContrib", settings.HorzWaveContrib);
noise_material_->SetScalarParameterValue("HorzWaveVertSize", settings.HorzWaveVertSize);
noise_material_->SetScalarParameterValue("HorzWaveScreenSize", settings.HorzWaveScreenSize);
noise_material_->SetScalarParameterValue("HorzNoiseLinesContrib", settings.HorzNoiseLinesContrib);
noise_material_->SetScalarParameterValue("HorzNoiseLinesDensityY", settings.HorzNoiseLinesDensityY);
noise_material_->SetScalarParameterValue("HorzNoiseLinesDensityXY", settings.HorzNoiseLinesDensityXY);
noise_material_->SetScalarParameterValue("HorzDistortionStrength", settings.HorzDistortionStrength);
noise_material_->SetScalarParameterValue("HorzDistortionContrib", settings.HorzDistortionContrib);

obj.AddBlendable(noise_material_, 1.0f);
}

void APIPCamera::enableCaptureComponent(const APIPCamera::ImageType type, bool is_enabled)
Expand Down
7 changes: 6 additions & 1 deletion Unreal/Plugins/AirSim/Source/PIPCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AIRSIM_API APIPCamera : public ACameraActor


private:
typedef common_utils::Utils Utils;


UPROPERTY() TArray<USceneCaptureComponent2D*> captures_;
Expand All @@ -56,8 +55,14 @@ class AIRSIM_API APIPCamera : public ACameraActor
std::vector<bool> camera_type_enabled_;

private:
typedef common_utils::Utils Utils;

static unsigned int imageTypeCount();
void enableCaptureComponent(const ImageType type, bool is_enabled);
static void updateCaptureComponentSetting(USceneCaptureComponent2D* capture, UTextureRenderTarget2D* render_target, const CaptureSetting& setting);
void setNoiseMaterial(FPostProcessSettings& obj, const NoiseSetting& settings);
static void updateCameraPostProcessingSetting(FPostProcessSettings& obj, const CaptureSetting& setting);
static void updateCameraSetting(UCameraComponent* camera, const CaptureSetting& setting);


};
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/SimHUD/SimHUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void ASimHUD::BeginPlay()
setupInputBindings();
}
catch (std::exception& ex) {
UAirBlueprintLib::LogMessage(FString("Error loading settings"), FString(ex.what()), LogDebugLevel::Failure);
UAirBlueprintLib::LogMessageString("Error at startup: ", ex.what(), LogDebugLevel::Failure);
//FGenericPlatformMisc::PlatformInit();
//FGenericPlatformMisc::MessageBoxExt(EAppMsgType::Ok, TEXT("Error at Startup"), ANSI_TO_TCHAR(ex.what()));
UAirBlueprintLib::ShowMessage(EAppMsgType::Ok, std::string("Error at startup: ") + ex.what(), "Error");
Expand Down
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/VehiclePawnWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void VehiclePawnWrapper::setupCamerasFromSettings()
typedef msr::airlib::AirSimSettings AirSimSettings;

int image_count = static_cast<int>(Utils::toNumeric(ImageType::Count));
for (int image_type = 0; image_type < image_count; ++image_type) {
for (int image_type = -1; image_type < image_count; ++image_type) {
for (int camera_index = 0; camera_index < getCameraCount(); ++camera_index) {
APIPCamera* camera = getCamera(camera_index);
camera->setImageTypeSettings(image_type, AirSimSettings::singleton().capture_settings[image_type],
Expand Down

0 comments on commit 71c99cf

Please sign in to comment.