Skip to content

Commit

Permalink
Update WeatherLib, SimModeBase
Browse files Browse the repository at this point in the history
  • Loading branch information
rajat2004 committed Dec 30, 2020
1 parent 8457eb8 commit 94b282f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 54 deletions.
74 changes: 55 additions & 19 deletions Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "sensors/lidar/LidarSimple.hpp"
#include "sensors/distance/DistanceSimple.hpp"

#include "Weather/WeatherLib.h"

#include "DrawDebugHelpers.h"

//TODO: this is going to cause circular references which is fine here but
Expand Down Expand Up @@ -65,7 +63,22 @@ ASimModeBase::ASimModeBase()

}
else
loading_screen_widget_ = nullptr;
loading_screen_widget_ = nullptr;

// Load Weather assets as well
static ConstructorHelpers::FObjectFinder<UMaterialParameterCollection> weather_params_object(UWeatherLib::getWeatherParamsObjectPath());
weather_params_collection_ = weather_params_object.Succeeded() ? weather_params_object.Object : nullptr;

// if (weather_params_object.Object) {
// weather_param_collection_ = weather_params_object.Object;
// // UAirBlueprintLib::LogMessageString("DateTime: ", Utils::to_string(ClockFactory::get()->nowNanos() / 1E9), LogDebugLevel::Informational);
// // std::printf("Weather Params: %d", weather_params_object.Object == nullptr);
// // UAirBlueprintLib::LogMessageString("Weather Params: ", Utils::to_string(weather_params_object.Object == nullptr), LogDebugLevel::Informational);
// // // if (weather_param_collection)
// // weather_params_collection_instance_ = GetWorld()->GetParameterCollectionInstance(weather_params_object.Object);
// }
// else
// weather_params_collection_ = nullptr;
}

void ASimModeBase::toggleLoadingScreen(bool is_visible)
Expand Down Expand Up @@ -111,7 +124,7 @@ void ASimModeBase::BeginPlay()
this->GetWorld()->SetNewWorldOrigin(FIntVector(player_loc) + this->GetWorld()->OriginLocation);
// Regrab the player's position after the offset has been added (which should be 0,0,0 now)
player_start_transform = fpv_pawn->GetActorTransform();
global_ned_transform_.reset(new NedTransform(player_start_transform,
global_ned_transform_.reset(new NedTransform(player_start_transform,
UAirBlueprintLib::GetWorldToMetersScale(this)));

UAirBlueprintLib::GenerateAssetRegistryMap(this, asset_map);
Expand All @@ -126,7 +139,7 @@ void ASimModeBase::BeginPlay()
setupClockSpeed();

setStencilIDs();

record_tick_count = 0;
setupInputBindings();

Expand All @@ -145,6 +158,18 @@ void ASimModeBase::BeginPlay()
{
UWeatherLib::initWeather(World, spawned_actors_);
//UWeatherLib::showWeatherMenu(World);
if (weather_params_collection_) {
weather_params_collection_instance_ = World->GetParameterCollectionInstance(weather_params_collection_);
// UE_LOG(LogTemp, Warning, TEXT("Warning, WeatherAPI could NOT get MaterialCollectionInstance!"));
UE_LOG(LogTemp, Warning, TEXT("WeatherParamsCollInstance: YES") );

// if (weather_params_collection_instance_ != nullptr) {
// UE_LOG(LogTemp, Warning, TEXT("WeatherParamsCollInstance: YES") );
// }
// else {
// UE_LOG(LogTemp, Warning, TEXT("WeatherParamsCollInstance: NO") );
// }
}
}
UAirBlueprintLib::GenerateActorMap(this, scene_object_map);

Expand Down Expand Up @@ -178,7 +203,7 @@ void ASimModeBase::setStencilIDs()
UAirBlueprintLib::SetMeshNamingMethod(getSettings().segmentation_setting.mesh_naming_method);

if (getSettings().segmentation_setting.init_method ==
AirSimSettings::SegmentationSetting::InitMethodType::CommonObjectsRandomIDs) {
AirSimSettings::SegmentationSetting::InitMethodType::CommonObjectsRandomIDs) {
UAirBlueprintLib::InitializeMeshStencilIDs(!getSettings().segmentation_setting.override_existing);
}
//else don't init
Expand Down Expand Up @@ -223,15 +248,15 @@ void ASimModeBase::initializeTimeOfDay()
UObject* sun_obj = sun_prop->GetObjectPropertyValue_InContainer(sky_sphere_);
sun_ = Cast<ADirectionalLight>(sun_obj);
if (sun_)
default_sun_rotation_ = sun_->GetActorRotation();
default_sun_rotation_ = sun_->GetActorRotation();
}
}

void ASimModeBase::setTimeOfDay(bool is_enabled, const std::string& start_datetime, bool is_start_datetime_dst,
float celestial_clock_speed, float update_interval_secs, bool move_sun)
{
bool enabled_currently = tod_enabled_;

if (is_enabled) {

if (!sun_) {
Expand All @@ -241,7 +266,7 @@ void ASimModeBase::setTimeOfDay(bool is_enabled, const std::string& start_dateti
else {
sun_->GetRootComponent()->Mobility = EComponentMobility::Movable;

// this is a bit odd but given how advanceTimeOfDay() works currently,
// this is a bit odd but given how advanceTimeOfDay() works currently,
// tod_sim_clock_start_ needs to be reset here.
tod_sim_clock_start_ = ClockFactory::get()->nowNanos();

Expand Down Expand Up @@ -345,8 +370,8 @@ void ASimModeBase::showClockStats()
{
float clock_speed = getSettings().clock_speed;
if (clock_speed != 1) {
UAirBlueprintLib::LogMessageString("ClockSpeed config, actual: ",
Utils::stringf("%f, %f", clock_speed, ClockFactory::get()->getTrueScaleWrtWallClock()),
UAirBlueprintLib::LogMessageString("ClockSpeed config, actual: ",
Utils::stringf("%f, %f", clock_speed, ClockFactory::get()->getTrueScaleWrtWallClock()),
LogDebugLevel::Informational);
}
}
Expand Down Expand Up @@ -426,13 +451,13 @@ void ASimModeBase::initializeCameraDirector(const FTransform& camera_transform,
FActorSpawnParameters camera_spawn_params;
camera_spawn_params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
camera_spawn_params.Name = "CameraDirector";
CameraDirector = this->GetWorld()->SpawnActor<ACameraDirector>(camera_director_class_,
CameraDirector = this->GetWorld()->SpawnActor<ACameraDirector>(camera_director_class_,
camera_transform, camera_spawn_params);
CameraDirector->setFollowDistance(follow_distance);
CameraDirector->setCameraRotationLagEnabled(false);
//create external camera required for the director
camera_spawn_params.Name = "ExternalCamera";
CameraDirector->ExternalCamera = this->GetWorld()->SpawnActor<APIPCamera>(external_camera_class_,
CameraDirector->ExternalCamera = this->GetWorld()->SpawnActor<APIPCamera>(external_camera_class_,
camera_transform, camera_spawn_params);
}
else {
Expand Down Expand Up @@ -467,6 +492,17 @@ bool ASimModeBase::isRecording() const
return FRecordingThread::isRecording();
}

void ASimModeBase::enableWeather(bool enable)
{
UWeatherLib::setWeatherEnabled(weather_params_collection_instance_, enable);
}

void ASimModeBase::setWeatherParameter(EWeatherParamScalar param, float amount)
{
UWeatherLib::setWeatherParamScalar(weather_params_collection_instance_, param, amount);
}


//API server start/stop
void ASimModeBase::startApiServer()
{
Expand Down Expand Up @@ -549,9 +585,9 @@ void ASimModeBase::setupVehiclesAndCamera()

//determine camera director camera default pose and spawn it
const auto& camera_director_setting = getSettings().camera_director;
FVector camera_director_position_uu = uu_origin.GetLocation() +
FVector camera_director_position_uu = uu_origin.GetLocation() +
getGlobalNedTransform().fromLocalNed(camera_director_setting.position);
FTransform camera_transform(toFRotator(camera_director_setting.rotation, FRotator::ZeroRotator),
FTransform camera_transform(toFRotator(camera_director_setting.rotation, FRotator::ZeroRotator),
camera_director_position_uu);
initializeCameraDirector(camera_transform, camera_director_setting.follow_distance);

Expand All @@ -561,7 +597,7 @@ void ASimModeBase::setupVehiclesAndCamera()
getExistingVehiclePawns(pawns);
bool haveUEPawns = pawns.Num() > 0;
APawn* fpv_pawn = nullptr;

if (haveUEPawns) {
fpv_pawn = static_cast<APawn*>(pawns[0]);
} else {
Expand Down Expand Up @@ -612,7 +648,7 @@ void ASimModeBase::setupVehiclesAndCamera()
const std::string vehicle_name = std::string(TCHAR_TO_UTF8(*(vehicle_pawn->GetName())));

PawnSimApi::Params pawn_sim_api_params(vehicle_pawn, &getGlobalNedTransform(),
getVehiclePawnEvents(vehicle_pawn), getVehiclePawnCameras(vehicle_pawn), pip_camera_class,
getVehiclePawnEvents(vehicle_pawn), getVehiclePawnCameras(vehicle_pawn), pip_camera_class,
collision_display_template, home_geopoint, vehicle_name);

auto vehicle_sim_api = createVehicleSimApi(pawn_sim_api_params);
Expand Down Expand Up @@ -771,7 +807,7 @@ void ASimModeBase::drawDistanceSensorDebugPoints()
Pose vehicle_pose = pawn_sim_api->getGroundTruthKinematics()->pose;

for (msr::airlib::uint i=0; i<count_distance_sensors; i++) {
const msr::airlib::DistanceSimple* distance_sensor =
const msr::airlib::DistanceSimple* distance_sensor =
static_cast<const msr::airlib::DistanceSimple*>(api->getSensors().getByType(SensorType::Distance, i));

if (distance_sensor != nullptr && distance_sensor->getParams().draw_debug_points) {
Expand All @@ -782,7 +818,7 @@ void ASimModeBase::drawDistanceSensorDebugPoints()
// order of Pose addition is important here because it also adds quaternions which is not commutative!
Pose distance_sensor_pose = distance_sensor_data.relative_pose + vehicle_pose;
Vector3r start = distance_sensor_pose.position;
Vector3r point = start + VectorMath::rotateVector(VectorMath::front(),
Vector3r point = start + VectorMath::rotateVector(VectorMath::front(),
distance_sensor_pose.orientation, true) * distance_sensor_data.distance;

FVector uu_point = pawn_sim_api->getNedTransform().fromLocalNed(point);
Expand Down
7 changes: 7 additions & 0 deletions Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "PawnSimApi.h"
#include "common/StateReporterWrapper.hpp"
#include "LoadingScreenWidget.h"
#include "Weather/WeatherLib.h"
#include "SimModeBase.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FLevelLoaded);
Expand Down Expand Up @@ -74,6 +75,10 @@ class AIRSIM_API ASimModeBase : public AActor
void stopApiServer();
bool isApiServerStarted();

// Wrapper methods for WeatherLib
void enableWeather(bool enable);
void setWeatherParameter(EWeatherParamScalar param, float amount);

const NedTransform& getGlobalNedTransform();

msr::airlib::ApiProvider* getApiProvider() const
Expand Down Expand Up @@ -142,6 +147,8 @@ class AIRSIM_API ASimModeBase : public AActor
UPROPERTY() UClass* camera_director_class_;
UPROPERTY() UClass* sky_sphere_class_;
UPROPERTY() ULoadingScreenWidget* loading_screen_widget_;
UPROPERTY() UMaterialParameterCollection* weather_params_collection_;
UPROPERTY() UMaterialParameterCollectionInstance* weather_params_collection_instance_;


UPROPERTY() AActor* sky_sphere_;
Expand Down
41 changes: 21 additions & 20 deletions Unreal/Plugins/AirSim/Source/Weather/WeatherLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ UMaterialParameterCollectionInstance* UWeatherLib::getWeatherMaterialCollectionI
{
UE_LOG(LogTemp, Warning, TEXT("Warning, WeatherAPI could NOT get WeatherParameterCollectionInstance1!"));
}

}
else
{
Expand Down Expand Up @@ -63,7 +63,7 @@ void UWeatherLib::initWeather(UWorld* World, TArray<AActor*> ActorsToAttachTo)
{
UE_LOG(LogTemp, Warning, TEXT("Warning, WeatherAPI got invalid weather actor class!"));
}
// still need the menu class for f10
// still need the menu class for f10
UClass* MenuActorClass = getWeatherMenuObjectPath().TryLoadClass<AActor>();
if (MenuActorClass)
{
Expand All @@ -84,9 +84,9 @@ void UWeatherLib::initWeather(UWorld* World, TArray<AActor*> ActorsToAttachTo)
//showWeatherMenu(WorldContextObject);

}
void UWeatherLib::setWeatherParamScalar(UWorld* World, EWeatherParamScalar Param, float Amount)
void UWeatherLib::setWeatherParamScalar(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance, EWeatherParamScalar Param, float Amount)
{
UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
// UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
if (WeatherMaterialCollectionInstance)
{
FName ParamName = GetWeatherParamScalarName(Param);
Expand All @@ -101,7 +101,7 @@ void UWeatherLib::setWeatherParamScalar(UWorld* World, EWeatherParamScalar Param
// and must be set to true or false before this.
// WeatherEnabled will always be false
// NOTE: weather enabled must be set first, before other params for this to work
if (!getIsWeatherEnabled(World))
if (!getIsWeatherEnabled(WeatherMaterialCollectionInstance))
{
WeatherMaterialCollectionInstance->SetScalarParameterValue(ParamName, 0.0f);
}
Expand All @@ -112,9 +112,9 @@ void UWeatherLib::setWeatherParamScalar(UWorld* World, EWeatherParamScalar Param
UE_LOG(LogTemp, Warning, TEXT("Warning, WeatherAPI could NOT get MaterialCollectionInstance!"));
}
}
float UWeatherLib::getWeatherParamScalar(UWorld* World, EWeatherParamScalar Param)
float UWeatherLib::getWeatherParamScalar(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance, EWeatherParamScalar Param)
{
UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
// UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
if (WeatherMaterialCollectionInstance)
{
FName ParamName = GetWeatherParamScalarName(Param);
Expand All @@ -133,9 +133,9 @@ float UWeatherLib::getWeatherParamScalar(UWorld* World, EWeatherParamScalar Para
}
return 0.0f;
}
FVector UWeatherLib::getWeatherWindDirection(UWorld* World)
FVector UWeatherLib::getWeatherWindDirection(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance)
{
UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
// UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
if (WeatherMaterialCollectionInstance)
{
FName ParamName = GetWeatherParamVectorName(EWeatherParamVector::WEATHER_PARAM_VECTOR_WIND);
Expand All @@ -154,9 +154,9 @@ FVector UWeatherLib::getWeatherWindDirection(UWorld* World)
}
return FVector(0,0,0);
}
void UWeatherLib::setWeatherWindDirection(UWorld* World, FVector NewWind)
void UWeatherLib::setWeatherWindDirection(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance, FVector NewWind)
{
UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
// UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance = UWeatherLib::getWeatherMaterialCollectionInstance(World);
if (WeatherMaterialCollectionInstance)
{
FName ParamName = GetWeatherParamVectorName(EWeatherParamVector::WEATHER_PARAM_VECTOR_WIND);
Expand All @@ -171,22 +171,23 @@ void UWeatherLib::setWeatherWindDirection(UWorld* World, FVector NewWind)
UE_LOG(LogTemp, Warning, TEXT("Warning, WeatherAPI could NOT get MaterialCollectionInstance!"));
}
}
bool UWeatherLib::getIsWeatherEnabled(UWorld* World)
bool UWeatherLib::getIsWeatherEnabled(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance)
{
if (getWeatherParamScalar(World, EWeatherParamScalar::WEATHER_PARAM_SCALAR_WEATHERENABLED) == 1.0f)
{
return true;
}
return false;
// if (getWeatherParamScalar(WeatherMaterialCollectionInstance, EWeatherParamScalar::WEATHER_PARAM_SCALAR_WEATHERENABLED) == 1.0f)
// {
// return true;
// }
// return false;
return getWeatherParamScalar(WeatherMaterialCollectionInstance, EWeatherParamScalar::WEATHER_PARAM_SCALAR_WEATHERENABLED) == 1.0f;
}
void UWeatherLib::setWeatherEnabled(UWorld* World, bool bEnabled)
void UWeatherLib::setWeatherEnabled(UMaterialParameterCollectionInstance* WeatherMaterialCollectionInstance, bool bEnabled)
{
float Value = 0;
if (bEnabled)
{
Value = 1;
}
setWeatherParamScalar(World, EWeatherParamScalar::WEATHER_PARAM_SCALAR_WEATHERENABLED, Value);
setWeatherParamScalar(WeatherMaterialCollectionInstance, EWeatherParamScalar::WEATHER_PARAM_SCALAR_WEATHERENABLED, Value);
}
void UWeatherLib::showWeatherMenu(UWorld* World)
{
Expand Down Expand Up @@ -304,4 +305,4 @@ UWorld* UWeatherLib::actorGetWorld(AActor* Actor)
return Actor->GetWorld();
}
return NULL;
}
}
Loading

0 comments on commit 94b282f

Please sign in to comment.