CARLA
 
载入中...
搜索中...
未找到
Weather.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "Carla.h"
10#include "Components/SceneCaptureComponent2D.h"
11#include "Kismet/GameplayStatics.h"
12#include "ConstructorHelpers.h"
13
14// AWeather类的构造函数,接受一个FObjectInitializer对象用于初始化类成员
15AWeather::AWeather(const FObjectInitializer& ObjectInitializer)
16 : Super(ObjectInitializer)
17{
18 // 使用ConstructorHelpers的FObjectFinder在指定路径下查找一个材质资源(用于降水效果的后处理材质)
19 // 并将找到的材质对象赋值给PrecipitationPostProcessMaterial成员变量
20 PrecipitationPostProcessMaterial = ConstructorHelpers::FObjectFinder<UMaterial>(
21 TEXT("Material'/Game/Carla/Static/GenericMaterials/00_MastersOpt/Screen_posProcess/M_screenDrops.M_screenDrops'")).Object;
22
23 // 同样使用ConstructorHelpers的FObjectFinder在指定路径下查找一个材质资源(用于沙尘暴效果的后处理材质)
24 // 并将找到的材质对象赋值给DustStormPostProcessMaterial成员变量
25 DustStormPostProcessMaterial = ConstructorHelpers::FObjectFinder<UMaterial>(
26 TEXT("Material'/Game/Carla/Static/GenericMaterials/00_MastersOpt/Screen_posProcess/M_screenDust_wind.M_screenDust_wind'")).Object;
27
28 // 设置该Actor的Tick函数是否可被调用,这里设置为false,表示不需要每帧更新
29 PrimaryActorTick.bCanEverTick = false;
30 // 使用传入的ObjectInitializer创建一个默认的场景组件作为根组件,并将其命名为"RootComponent"
31 RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("RootComponent"));
32}
33
34// 检查并应用与天气相关的后处理效果的函数
36{
37 // 如果天气的降水参数大于0.0f
38 if (Weather.Precipitation > 0.0f)
39 // 将降水效果的后处理材质和降水强度(除以100.0f进行归一化)组成一个元组,添加到ActiveBlendables列表中
41 else
42 // 如果降水参数不大于0.0f,从ActiveBlendables列表中移除降水效果的后处理材质
44
45 // 如果天气的沙尘暴参数大于0.0f
46 if (Weather.DustStorm > 0.0f)
47 // 将沙尘暴效果的后处理材质和沙尘暴强度(除以100.0f进行归一化)组成一个元组,添加到ActiveBlendables列表
49 else
50 // 如果沙尘暴参数不大于0.0f,从ActiveBlendables列表中移除沙尘暴效果的后处理材质
52
53 // 创建一个数组用于存储场景中所有的传感器Actor
54 TArray<AActor*> SensorActors;
55 // 使用UGameplayStatics的GetAllActorsOfClass函数获取场景中所有ASceneCaptureCamera类的Actor,并存储到SensorActors数组中
56 UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASceneCaptureCamera::StaticClass(), SensorActors);
57 // 遍历传感器Actor数组
58 for (AActor* SensorActor : SensorActors)
59 {
60 // 将当前遍历到的Actor转换为ASceneCaptureCamera类型的指针
61 ASceneCaptureCamera* Sensor = Cast<ASceneCaptureCamera>(SensorActor);
62 // 遍历ActiveBlendables列表中的每个元素(即每个后处理材质和强度的元组)
63 for (auto& ActiveBlendable : ActiveBlendables)
64 // 将当前元组中的后处理材质和强度添加到传感器的场景捕获组件2D的后处理设置中
65 Sensor->GetCaptureComponent2D()->PostProcessSettings.AddBlendable(ActiveBlendable.Key, ActiveBlendable.Value);
66 }
67}
68
69// 应用指定天气参数的函数
71{
72 // 设置当前天气参数为传入的天气参数
73 SetWeather(InWeather);
74 // 检查并应用与天气相关的后处理效果
76
77#ifdef CARLA_WEATHER_EXTRA_LOG
78 // 如果定义了CARLA_WEATHER_EXTRA_LOG宏,则输出以下日志信息,记录当前天气参数的各项值
79 UE_LOG(LogCarla, Log, TEXT("Changing weather:"));
80 UE_LOG(LogCarla, Log, TEXT(" - Cloudiness = %.2f"), Weather.Cloudiness);
81 UE_LOG(LogCarla, Log, TEXT(" - Precipitation = %.2f"), Weather.Precipitation);
82 UE_LOG(LogCarla, Log, TEXT(" - PrecipitationDeposits = %.2f"), Weather.PrecipitationDeposits);
83 UE_LOG(LogCarla, Log, TEXT(" - WindIntensity = %.2f"), Weather.WindIntensity);
84 UE_LOG(LogCarla, Log, TEXT(" - SunAzimuthAngle = %.2f"), Weather.SunAzimuthAngle);
85 UE_LOG(LogCarla, Log, TEXT(" - SunAltitudeAngle = %.2f"), Weather.SunAltitudeAngle);
86 UE_LOG(LogCarla, Log, TEXT(" - FogDensity = %.2f"), Weather.FogDensity);
87 UE_LOG(LogCarla, Log, TEXT(" - FogDistance = %.2f"), Weather.FogDistance);
88 UE_LOG(LogCarla, Log, TEXT(" - FogFalloff = %.2f"), Weather.FogFalloff);
89 UE_LOG(LogCarla, Log, TEXT(" - Wetness = %.2f"), Weather.Wetness);
90 UE_LOG(LogCarla, Log, TEXT(" - ScatteringIntensity = %.2f"), Weather.ScatteringIntensity);
91 UE_LOG(LogCarla, Log, TEXT(" - MieScatteringScale = %.2f"), Weather.MieScatteringScale);
92 UE_LOG(LogCarla, Log, TEXT(" - RayleighScatteringScale = %.2f"), Weather.RayleighScatteringScale);
93 UE_LOG(LogCarla, Log, TEXT(" - DustStorm = %.2f"), Weather.DustStorm);
94#endif // CARLA_WEATHER_EXTRA_LOG
95
96 // 调用能真正改变天气的蓝图。
98}
99
100// 通知天气相关变化给传感器的函数
102{
103 // 检查并应用与天气相关的后处理效果
105
106 // 调用能真正改变天气的蓝图。
108}
109
110// 设置当前天气参数的函数
112{
113 Weather = InWeather;
114}
115
116// 设置日夜循环状态的函数
117void AWeather::SetDayNightCycle(const bool& active)
118{
119 DayNightCycle = active;
120}
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
A sensor that captures images from the scene.
USceneCaptureComponent2D * GetCaptureComponent2D()
void SetWeather(const FWeatherParameters &WeatherParameters)
在不通知蓝图事件的情况下更新天气参数
Definition Weather.cpp:111
FWeatherParameters Weather
Definition Weather.h:69
void SetDayNightCycle(const bool &active)
更新昼夜周期
Definition Weather.cpp:117
void ApplyWeather(const FWeatherParameters &WeatherParameters)
更新天气参数并将其通知到蓝图的事件
Definition Weather.cpp:70
UMaterial * PrecipitationPostProcessMaterial
Definition Weather.h:72
void CheckWeatherPostProcessEffects()
Definition Weather.cpp:35
AWeather(const FObjectInitializer &ObjectInitializer)
Definition Weather.cpp:15
void NotifyWeather(ASensor *Sensor=nullptr)
将天气通知到蓝图的事件
Definition Weather.cpp:101
bool DayNightCycle
Definition Weather.h:85
UMaterial * DustStormPostProcessMaterial
Definition Weather.h:75
void RefreshWeather(const FWeatherParameters &WeatherParameters)
TMap< UMaterial *, float > ActiveBlendables
Definition Weather.h:80