14 #include "Async/Async.h" 15 #include "Renderer/Public/GBufferView.h" 17 #include <type_traits> 19 #include "SceneCaptureSensor.generated.h" 23 class UDrawFrustumComponent;
24 class UStaticMeshComponent;
25 class UTextureRenderTarget2D;
36 Stream = std::move(InStream);
42 Stream = std::move(InStream);
60 template <
typename SensorT>
79 Stream = std::move(InStream);
85 Stream = std::move(InStream);
102 template <
typename SensorT>
132 friend class FPixelReader2;
140 void SetImageSize(uint32 Width, uint32 Height);
142 uint32 GetImageWidth()
const 152 UFUNCTION(BlueprintCallable)
153 void EnablePostProcessingEffects(
bool Enable = true)
155 bEnablePostProcessingEffects = Enable;
158 UFUNCTION(BlueprintCallable)
159 bool ArePostProcessingEffectsEnabled()
const 161 return bEnablePostProcessingEffects;
164 UFUNCTION(BlueprintCallable)
165 void Enable16BitFormat(
bool Enable = false)
167 bEnable16BitFormat = Enable;
170 UFUNCTION(BlueprintCallable)
171 bool Is16BitFormatEnabled()
const 173 return bEnable16BitFormat;
176 UFUNCTION(BlueprintCallable)
177 void SetFOVAngle(
float FOVAngle);
179 UFUNCTION(BlueprintCallable)
180 float GetFOVAngle()
const;
182 UFUNCTION(BlueprintCallable)
183 void SetTargetGamma(
float InTargetGamma)
185 TargetGamma = InTargetGamma;
188 UFUNCTION(BlueprintCallable)
189 float GetTargetGamma()
const 194 UFUNCTION(BlueprintCallable)
195 void SetExposureMethod(EAutoExposureMethod Method);
197 UFUNCTION(BlueprintCallable)
198 EAutoExposureMethod GetExposureMethod()
const;
200 UFUNCTION(BlueprintCallable)
201 void SetExposureCompensation(
float Compensation);
203 UFUNCTION(BlueprintCallable)
204 float GetExposureCompensation()
const;
206 UFUNCTION(BlueprintCallable)
207 void SetShutterSpeed(
float Speed);
209 UFUNCTION(BlueprintCallable)
210 float GetShutterSpeed()
const;
212 UFUNCTION(BlueprintCallable)
213 void SetISO(
float ISO);
215 UFUNCTION(BlueprintCallable)
216 float GetISO()
const;
218 UFUNCTION(BlueprintCallable)
219 void SetAperture(
float Aperture);
221 UFUNCTION(BlueprintCallable)
222 float GetAperture()
const;
224 UFUNCTION(BlueprintCallable)
225 void SetFocalDistance(
float Distance);
227 UFUNCTION(BlueprintCallable)
228 float GetFocalDistance()
const;
230 UFUNCTION(BlueprintCallable)
231 void SetDepthBlurAmount(
float Amount);
233 UFUNCTION(BlueprintCallable)
234 float GetDepthBlurAmount()
const;
236 UFUNCTION(BlueprintCallable)
237 void SetDepthBlurRadius(
float Radius);
239 UFUNCTION(BlueprintCallable)
240 float GetDepthBlurRadius()
const;
242 UFUNCTION(BlueprintCallable)
243 void SetBladeCount(
int Count);
245 UFUNCTION(BlueprintCallable)
246 int GetBladeCount()
const;
248 UFUNCTION(BlueprintCallable)
249 void SetDepthOfFieldMinFstop(
float MinFstop);
251 UFUNCTION(BlueprintCallable)
252 float GetDepthOfFieldMinFstop()
const;
254 UFUNCTION(BlueprintCallable)
255 void SetFilmSlope(
float Slope);
257 UFUNCTION(BlueprintCallable)
258 float GetFilmSlope()
const;
260 UFUNCTION(BlueprintCallable)
261 void SetFilmToe(
float Toe);
263 UFUNCTION(BlueprintCallable)
264 float GetFilmToe()
const;
266 UFUNCTION(BlueprintCallable)
267 void SetFilmShoulder(
float Shoulder);
269 UFUNCTION(BlueprintCallable)
270 float GetFilmShoulder()
const;
272 UFUNCTION(BlueprintCallable)
273 void SetFilmBlackClip(
float BlackClip);
275 UFUNCTION(BlueprintCallable)
276 float GetFilmBlackClip()
const;
278 UFUNCTION(BlueprintCallable)
279 void SetFilmWhiteClip(
float WhiteClip);
281 UFUNCTION(BlueprintCallable)
282 float GetFilmWhiteClip()
const;
284 UFUNCTION(BlueprintCallable)
285 void SetExposureMinBrightness(
float Brightness);
287 UFUNCTION(BlueprintCallable)
288 float GetExposureMinBrightness()
const;
290 UFUNCTION(BlueprintCallable)
291 void SetExposureMaxBrightness(
float Brightness);
293 UFUNCTION(BlueprintCallable)
294 float GetExposureMaxBrightness()
const;
296 UFUNCTION(BlueprintCallable)
297 void SetExposureSpeedDown(
float Speed);
299 UFUNCTION(BlueprintCallable)
300 float GetExposureSpeedDown()
const;
302 UFUNCTION(BlueprintCallable)
303 void SetExposureSpeedUp(
float Speed);
305 UFUNCTION(BlueprintCallable)
306 float GetExposureSpeedUp()
const;
308 UFUNCTION(BlueprintCallable)
309 void SetExposureCalibrationConstant(
float Constant);
311 UFUNCTION(BlueprintCallable)
312 float GetExposureCalibrationConstant()
const;
314 UFUNCTION(BlueprintCallable)
315 void SetMotionBlurIntensity(
float Intensity);
317 UFUNCTION(BlueprintCallable)
318 float GetMotionBlurIntensity()
const;
320 UFUNCTION(BlueprintCallable)
321 void SetMotionBlurMaxDistortion(
float MaxDistortion);
323 UFUNCTION(BlueprintCallable)
324 float GetMotionBlurMaxDistortion()
const;
326 UFUNCTION(BlueprintCallable)
327 void SetMotionBlurMinObjectScreenSize(
float ScreenSize);
329 UFUNCTION(BlueprintCallable)
330 float GetMotionBlurMinObjectScreenSize()
const;
332 UFUNCTION(BlueprintCallable)
333 void SetLensFlareIntensity(
float Intensity);
335 UFUNCTION(BlueprintCallable)
336 float GetLensFlareIntensity()
const;
338 UFUNCTION(BlueprintCallable)
339 void SetBloomIntensity(
float Intensity);
341 UFUNCTION(BlueprintCallable)
342 float GetBloomIntensity()
const;
344 UFUNCTION(BlueprintCallable)
345 void SetWhiteTemp(
float Temp);
347 UFUNCTION(BlueprintCallable)
348 float GetWhiteTemp()
const;
350 UFUNCTION(BlueprintCallable)
351 void SetWhiteTint(
float Tint);
353 UFUNCTION(BlueprintCallable)
354 float GetWhiteTint()
const;
356 UFUNCTION(BlueprintCallable)
357 void SetChromAberrIntensity(
float Intensity);
359 UFUNCTION(BlueprintCallable)
360 float GetChromAberrIntensity()
const;
362 UFUNCTION(BlueprintCallable)
363 void SetChromAberrOffset(
float ChromAberrOffset);
365 UFUNCTION(BlueprintCallable)
366 float GetChromAberrOffset()
const;
369 UFUNCTION(BlueprintCallable)
370 bool ReadPixels(TArray<FColor> &BitMap)
const 372 check(CaptureRenderTarget !=
nullptr);
377 UFUNCTION(BlueprintCallable)
378 void SaveCaptureToDisk(const FString &FilePath)
const 380 check(CaptureRenderTarget !=
nullptr);
384 UFUNCTION(BlueprintCallable)
387 return CaptureComponent2D;
390 UFUNCTION(BlueprintCallable)
391 UTextureRenderTarget2D *GetCaptureRenderTarget()
393 return CaptureRenderTarget;
397 void EnqueueRenderSceneImmediate();
424 void CaptureSceneExtended();
426 virtual void SendGBufferTextures(FGBufferRequest& GBuffer);
428 virtual void BeginPlay()
override;
430 virtual void PrePhysTick(
float DeltaSeconds)
override;
431 virtual void PostPhysTick(UWorld *World, ELevelTick TickType,
float DeltaTime)
override;
433 virtual void EndPlay(
const EEndPlayReason::Type EndPlayReason)
override;
438 UPROPERTY(EditAnywhere)
439 UTextureRenderTarget2D *CaptureRenderTarget =
nullptr;
442 UPROPERTY(EditAnywhere)
445 UPROPERTY(EditAnywhere)
446 float TargetGamma = 2.4f;
449 UPROPERTY(EditAnywhere)
450 uint32 ImageWidth = 800u;
453 UPROPERTY(EditAnywhere)
454 uint32 ImageHeight = 600u;
457 UPROPERTY(EditAnywhere)
458 bool bEnablePostProcessingEffects = true;
461 UPROPERTY(EditAnywhere)
462 bool bEnable16BitFormat = false;
468 typename CameraGBufferT>
469 static
void SendGBuffer(
471 CameraGBufferT& CameraGBuffer,
472 FGBufferRequest& GBufferData,
473 EGBufferTextureID TextureID)
475 using PixelType =
typename std::conditional<
480 TArray<PixelType> Pixels;
481 if (GBufferData.WaitForTextureTransfer(TextureID))
483 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"GBuffer Decode");
486 FIntPoint SourceExtent;
487 GBufferData.MapTextureData(
492 auto Format = GBufferData.Readbacks[(size_t)TextureID]->GetFormat();
493 ViewSize = GBufferData.ViewRect.Size();
494 Pixels.AddUninitialized(ViewSize.X * ViewSize.Y);
495 FReadSurfaceDataFlags Flags = {};
496 Flags.SetLinearToGamma(
true);
505 GBufferData.UnmapTextureData(TextureID);
509 ViewSize = GBufferData.ViewRect.Size();
510 Pixels.SetNum(ViewSize.X * ViewSize.Y);
511 for (
auto& Pixel : Pixels)
512 Pixel = PixelType::Black;
514 auto GBufferStream = CameraGBuffer.GetDataStream(Self);
515 auto Buffer = GBufferStream.PopBufferFromPool();
522 SCOPE_CYCLE_COUNTER(STAT_CarlaSensorStreamSend);
523 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"Stream Send");
524 GBufferStream.SerializeAndSend(
534 template <
typename T>
537 for (
size_t i = 0; i != FGBufferRequest::TextureCount; ++i)
539 if ((GBufferData.DesiredTexturesMask & (UINT64_C(1) << i)) == 0) {
542 auto& C = CameraGBuffers;
543 EGBufferTextureID ID = (EGBufferTextureID)i;
546 case EGBufferTextureID::SceneColor:
547 SendGBuffer(Self, C.SceneColor, GBufferData, ID);
549 case EGBufferTextureID::SceneDepth:
550 SendGBuffer(Self, C.SceneDepth, GBufferData, ID);
552 case EGBufferTextureID::SceneStencil:
553 SendGBuffer(Self, C.SceneStencil, GBufferData, ID);
555 case EGBufferTextureID::GBufferA:
556 SendGBuffer(Self, C.GBufferA, GBufferData, ID);
558 case EGBufferTextureID::GBufferB:
559 SendGBuffer(Self, C.GBufferB, GBufferData, ID);
561 case EGBufferTextureID::GBufferC:
562 SendGBuffer(Self, C.GBufferC, GBufferData, ID);
564 case EGBufferTextureID::GBufferD:
565 SendGBuffer(Self, C.GBufferD, GBufferData, ID);
567 case EGBufferTextureID::GBufferE:
568 SendGBuffer(Self, C.GBufferE, GBufferData, ID);
570 case EGBufferTextureID::GBufferF:
571 SendGBuffer(Self, C.GBufferF, GBufferData, ID);
573 case EGBufferTextureID::Velocity:
574 SendGBuffer(Self, C.Velocity, GBufferData, ID);
576 case EGBufferTextureID::SSAO:
577 SendGBuffer(Self, C.SSAO, GBufferData, ID);
579 case EGBufferTextureID::CustomDepth:
580 SendGBuffer(Self, C.CustomDepth, GBufferData, ID);
582 case EGBufferTextureID::CustomStencil:
583 SendGBuffer(Self, C.CustomStencil, GBufferData, ID);
FCameraGBufferUint8 GBufferF
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
static bool WritePixelsToArray(UTextureRenderTarget2D &RenderTarget, TArray< FColor > &BitMap)
Copy the pixels in RenderTarget into BitMap.
void not_spawnable
Prevent this sensor to be spawned by users.
FCameraGBufferUint8 SceneDepth
FAsyncDataStreamTmpl< T > MakeAsyncDataStream(const SensorT &Sensor, double Timestamp)
Create a FAsyncDataStream object.
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get
FCameraGBufferUint8 CustomStencil
FCameraGBufferUint8 SceneStencil
void SetDataStream(FDataStream InStream)
auto GetToken() const
Return the token that allows subscribing to this sensor's stream.
FCameraGBufferUint8 GBufferE
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
Utils for reading pixels from UTextureRenderTarget2D.
void DecodePixelsByFormat(void *PixelData, int32 SourcePitch, FIntPoint SourceExtent, FIntPoint DestinationExtent, EPixelFormat Format, FReadSurfaceDataFlags Flags, TArrayView< FLinearColor > Out)
FTransform GetActorTransform() const
Dummy. Required for compatibility with other sensors only.
FCameraGBufferUint8 GBufferD
FCameraGBufferUint8 GBufferA
void not_spawnable
Prevent this sensor to be spawned by users.
auto GetToken() const
Return the token that allows subscribing to this stream.
virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture)
FCameraGBufferUint8 SceneColor
A description of a Carla Actor with all its variation.
FTransform GetActorTransform() const
Dummy. Required for compatibility with other sensors only.
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
FCameraGBufferUint8 CustomDepth
uint32 GetImageHeight() const
Base class for sensors using a USceneCaptureComponent2D for rendering the scene.
void SetDataStream(FDataStream InStream)
A streaming channel for sending sensor data to clients, supports sending data asynchronously.
FCameraGBufferUint8 GBufferC
FCameraGBufferUint8 Velocity
void WaitForRenderThreadToFinish()
Blocks until the render thread has finished all it's tasks.
Base class for the CARLA Game Mode.
void SetStream(FDataMultiStream InStream)
Replace the Stream associated with this sensor.
void SendGBufferTexturesInternal(T &Self, FGBufferRequest &GBufferData)
void SetStream(FDataMultiStream InStream)
Replace the Stream associated with this sensor.
static TFuture< bool > SavePixelsToDisk(UTextureRenderTarget2D &RenderTarget, const FString &FilePath)
Asynchronously save the pixels in RenderTarget to disk.
auto GetToken() const
Return the token that allows subscribing to this sensor's stream.
FCameraGBufferUint8 GBufferB