CARLA
SceneCaptureSensor.h
Go to the documentation of this file.
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 #pragma once
8 
10 #include "Carla/Sensor/Sensor.h"
11 
12 #include "Runtime/RenderCore/Public/RenderCommandFence.h"
13 #include "SceneCaptureSensor.generated.h"
14 
15 class UDrawFrustumComponent;
16 class USceneCaptureComponent2D;
17 class UStaticMeshComponent;
18 class UTextureRenderTarget2D;
19 
20 /// Base class for sensors using a USceneCaptureComponent2D for rendering the
21 /// scene. This class does not capture data, use
22 /// `FPixelReader::SendPixelsInRenderThread(*this)` in derived classes.
23 ///
24 /// To access the USceneCaptureComponent2D override the
25 /// SetUpSceneCaptureComponent function.
26 ///
27 /// @warning All the setters should be called before BeginPlay.
28 UCLASS(Abstract)
29 class CARLA_API ASceneCaptureSensor : public ASensor
30 {
31  GENERATED_BODY()
32 
33  friend class ACarlaGameModeBase;
34  friend class FPixelReader;
35 
36 public:
37 
38  ASceneCaptureSensor(const FObjectInitializer &ObjectInitializer);
39 
40  void Set(const FActorDescription &ActorDescription) override;
41 
42  void SetImageSize(uint32 Width, uint32 Height);
43 
44  uint32 GetImageWidth() const
45  {
46  return ImageWidth;
47  }
48 
49  uint32 GetImageHeight() const
50  {
51  return ImageHeight;
52  }
53 
54  UFUNCTION(BlueprintCallable)
55  void EnablePostProcessingEffects(bool Enable = true)
56  {
57  bEnablePostProcessingEffects = Enable;
58  }
59 
60  UFUNCTION(BlueprintCallable)
61  bool ArePostProcessingEffectsEnabled() const
62  {
63  return bEnablePostProcessingEffects;
64  }
65 
66  UFUNCTION(BlueprintCallable)
67  void Enable16BitFormat(bool Enable = false)
68  {
69  bEnable16BitFormat = Enable;
70  }
71 
72  UFUNCTION(BlueprintCallable)
73  bool Is16BitFormatEnabled() const
74  {
75  return bEnable16BitFormat;
76  }
77 
78  UFUNCTION(BlueprintCallable)
79  void SetFOVAngle(float FOVAngle);
80 
81  UFUNCTION(BlueprintCallable)
82  float GetFOVAngle() const;
83 
84  UFUNCTION(BlueprintCallable)
85  void SetTargetGamma(float InTargetGamma)
86  {
87  TargetGamma = InTargetGamma;
88  }
89 
90  UFUNCTION(BlueprintCallable)
91  float GetTargetGamma() const
92  {
93  return TargetGamma;
94  }
95 
96  UFUNCTION(BlueprintCallable)
97  void SetExposureMethod(EAutoExposureMethod Method);
98 
99  UFUNCTION(BlueprintCallable)
100  EAutoExposureMethod GetExposureMethod() const;
101 
102  UFUNCTION(BlueprintCallable)
103  void SetExposureCompensation(float Compensation);
104 
105  UFUNCTION(BlueprintCallable)
106  float GetExposureCompensation() const;
107 
108  UFUNCTION(BlueprintCallable)
109  void SetShutterSpeed(float Speed);
110 
111  UFUNCTION(BlueprintCallable)
112  float GetShutterSpeed() const;
113 
114  UFUNCTION(BlueprintCallable)
115  void SetISO(float ISO);
116 
117  UFUNCTION(BlueprintCallable)
118  float GetISO() const;
119 
120  UFUNCTION(BlueprintCallable)
121  void SetAperture(float Aperture);
122 
123  UFUNCTION(BlueprintCallable)
124  float GetAperture() const;
125 
126  UFUNCTION(BlueprintCallable)
127  void SetFocalDistance(float Distance);
128 
129  UFUNCTION(BlueprintCallable)
130  float GetFocalDistance() const;
131 
132  UFUNCTION(BlueprintCallable)
133  void SetDepthBlurAmount(float Amount);
134 
135  UFUNCTION(BlueprintCallable)
136  float GetDepthBlurAmount() const;
137 
138  UFUNCTION(BlueprintCallable)
139  void SetDepthBlurRadius(float Radius);
140 
141  UFUNCTION(BlueprintCallable)
142  float GetDepthBlurRadius() const;
143 
144  UFUNCTION(BlueprintCallable)
145  void SetBladeCount(int Count);
146 
147  UFUNCTION(BlueprintCallable)
148  int GetBladeCount() const;
149 
150  UFUNCTION(BlueprintCallable)
151  void SetDepthOfFieldMinFstop(float MinFstop);
152 
153  UFUNCTION(BlueprintCallable)
154  float GetDepthOfFieldMinFstop() const;
155 
156  UFUNCTION(BlueprintCallable)
157  void SetFilmSlope(float Slope);
158 
159  UFUNCTION(BlueprintCallable)
160  float GetFilmSlope() const;
161 
162  UFUNCTION(BlueprintCallable)
163  void SetFilmToe(float Toe);
164 
165  UFUNCTION(BlueprintCallable)
166  float GetFilmToe() const;
167 
168  UFUNCTION(BlueprintCallable)
169  void SetFilmShoulder(float Shoulder);
170 
171  UFUNCTION(BlueprintCallable)
172  float GetFilmShoulder() const;
173 
174  UFUNCTION(BlueprintCallable)
175  void SetFilmBlackClip(float BlackClip);
176 
177  UFUNCTION(BlueprintCallable)
178  float GetFilmBlackClip() const;
179 
180  UFUNCTION(BlueprintCallable)
181  void SetFilmWhiteClip(float WhiteClip);
182 
183  UFUNCTION(BlueprintCallable)
184  float GetFilmWhiteClip() const;
185 
186  UFUNCTION(BlueprintCallable)
187  void SetExposureMinBrightness(float Brightness);
188 
189  UFUNCTION(BlueprintCallable)
190  float GetExposureMinBrightness() const;
191 
192  UFUNCTION(BlueprintCallable)
193  void SetExposureMaxBrightness(float Brightness);
194 
195  UFUNCTION(BlueprintCallable)
196  float GetExposureMaxBrightness() const;
197 
198  UFUNCTION(BlueprintCallable)
199  void SetExposureSpeedDown(float Speed);
200 
201  UFUNCTION(BlueprintCallable)
202  float GetExposureSpeedDown() const;
203 
204  UFUNCTION(BlueprintCallable)
205  void SetExposureSpeedUp(float Speed);
206 
207  UFUNCTION(BlueprintCallable)
208  float GetExposureSpeedUp() const;
209 
210  UFUNCTION(BlueprintCallable)
211  void SetExposureCalibrationConstant(float Constant);
212 
213  UFUNCTION(BlueprintCallable)
214  float GetExposureCalibrationConstant() const;
215 
216  UFUNCTION(BlueprintCallable)
217  void SetMotionBlurIntensity(float Intensity);
218 
219  UFUNCTION(BlueprintCallable)
220  float GetMotionBlurIntensity() const;
221 
222  UFUNCTION(BlueprintCallable)
223  void SetMotionBlurMaxDistortion(float MaxDistortion);
224 
225  UFUNCTION(BlueprintCallable)
226  float GetMotionBlurMaxDistortion() const;
227 
228  UFUNCTION(BlueprintCallable)
229  void SetMotionBlurMinObjectScreenSize(float ScreenSize);
230 
231  UFUNCTION(BlueprintCallable)
232  float GetMotionBlurMinObjectScreenSize() const;
233 
234  UFUNCTION(BlueprintCallable)
235  void SetLensFlareIntensity(float Intensity);
236 
237  UFUNCTION(BlueprintCallable)
238  float GetLensFlareIntensity() const;
239 
240  UFUNCTION(BlueprintCallable)
241  void SetBloomIntensity(float Intensity);
242 
243  UFUNCTION(BlueprintCallable)
244  float GetBloomIntensity() const;
245 
246  UFUNCTION(BlueprintCallable)
247  void SetWhiteTemp(float Temp);
248 
249  UFUNCTION(BlueprintCallable)
250  float GetWhiteTemp() const;
251 
252  UFUNCTION(BlueprintCallable)
253  void SetWhiteTint(float Tint);
254 
255  UFUNCTION(BlueprintCallable)
256  float GetWhiteTint() const;
257 
258  UFUNCTION(BlueprintCallable)
259  void SetChromAberrIntensity(float Intensity);
260 
261  UFUNCTION(BlueprintCallable)
262  float GetChromAberrIntensity() const;
263 
264  UFUNCTION(BlueprintCallable)
265  void SetChromAberrOffset(float ChromAberrOffset);
266 
267  UFUNCTION(BlueprintCallable)
268  float GetChromAberrOffset() const;
269 
270  /// Use for debugging purposes only.
271  UFUNCTION(BlueprintCallable)
272  bool ReadPixels(TArray<FColor> &BitMap) const
273  {
274  check(CaptureRenderTarget != nullptr);
275  return FPixelReader::WritePixelsToArray(*CaptureRenderTarget, BitMap);
276  }
277 
278  /// Use for debugging purposes only.
279  UFUNCTION(BlueprintCallable)
280  void SaveCaptureToDisk(const FString &FilePath) const
281  {
282  check(CaptureRenderTarget != nullptr);
283  FPixelReader::SavePixelsToDisk(*CaptureRenderTarget, FilePath);
284  }
285 
286  UFUNCTION(BlueprintCallable)
287  USceneCaptureComponent2D *GetCaptureComponent2D()
288  {
289  return CaptureComponent2D;
290  }
291 
292  UFUNCTION(BlueprintCallable)
293  UTextureRenderTarget2D *GetCaptureRenderTarget()
294  {
295  return CaptureRenderTarget;
296  }
297 
298  /// Immediate enqueues render commands of the scene at the current time.
299  void EnqueueRenderSceneImmediate();
300 
301  /// Blocks until the render thread has finished all it's tasks.
303  TRACE_CPUPROFILER_EVENT_SCOPE(ASceneCaptureSensor::WaitForRenderThreadToFinish);
304  // FlushRenderingCommands();
305  }
306 
307 protected:
308 
309  virtual void BeginPlay() override;
310 
311  virtual void PrePhysTick(float DeltaSeconds) override;
312  virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime) override;
313 
314  virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
315 
316  virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) {}
317 
318  /// Render target necessary for scene capture.
319  UPROPERTY(EditAnywhere)
320  UTextureRenderTarget2D *CaptureRenderTarget = nullptr;
321 
322  /// Scene capture component.
323  UPROPERTY(EditAnywhere)
324  USceneCaptureComponent2D *CaptureComponent2D = nullptr;
325 
326  UPROPERTY(EditAnywhere)
327  float TargetGamma = 2.4f;
328 
329  /// Image width in pixels.
330  UPROPERTY(EditAnywhere)
331  uint32 ImageWidth = 800u;
332 
333  /// Image height in pixels.
334  UPROPERTY(EditAnywhere)
335  uint32 ImageHeight = 600u;
336 
337  /// Whether to render the post-processing effects present in the scene.
338  UPROPERTY(EditAnywhere)
339  bool bEnablePostProcessingEffects = true;
340 
341  /// Whether to change render target format to PF_A16B16G16R16, offering 16bit / channel
342  UPROPERTY(EditAnywhere)
343  bool bEnable16BitFormat = false;
344 
345  FRenderCommandFence RenderFence;
346 
347 };
static bool WritePixelsToArray(UTextureRenderTarget2D &RenderTarget, TArray< FColor > &BitMap)
Copy the pixels in RenderTarget into BitMap.
Utils for reading pixels from UTextureRenderTarget2D.
Definition: PixelReader.h:28
virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture)
A description of a Carla Actor with all its variation.
uint32 GetImageHeight() const
Base class for sensors using a USceneCaptureComponent2D for rendering the scene.
void WaitForRenderThreadToFinish()
Blocks until the render thread has finished all it&#39;s tasks.
Base class for the CARLA Game Mode.
static TFuture< bool > SavePixelsToDisk(UTextureRenderTarget2D &RenderTarget, const FString &FilePath)
Asynchronously save the pixels in RenderTarget to disk.