NVIDIA DeepLearning Dataset Synthesizer (NDDS)
 All Classes Namespaces Functions Variables Typedefs Pages
NVSceneCaptureComponent2D.h
1 /*
2 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10 
11 #pragma once
12 
13 #include "Components/SceneCaptureComponent2D.h"
14 #include "NVSceneCapturerUtils.h"
15 #include "NVTextureReader.h"
16 #include "Engine/TextureRenderTarget2D.h"
17 #include "NVSceneCaptureComponent2D.generated.h"
18 
19 DECLARE_LOG_CATEGORY_EXTERN(LogNVSceneCapturerComponent2D, Log, All)
20 
21 /// @cond DOXYGEN_SUPPRESSED_CODE
22 UCLASS(Blueprintable, ClassGroup = (NVIDIA), meta = (BlueprintSpawnableComponent),
23  HideCategories = (Replication, ComponentReplication, Cooking, Events, ComponentTick,
24  Actor, Input, Collision, PhysX, Activation, Sockets,
25  MobileTonemapper, PostProcessVolume,
26  Projection, Rendering, Transform, PlanarReflection, LOD))
27 /// @endcond DOXYGEN_SUPPRESSED_CODE
28 class NVSCENECAPTURER_API UNVSceneCaptureComponent2D : public USceneCaptureComponent2D
29 {
30  GENERATED_BODY()
31 
32  /// Callback function get called after the scene capture component finished capturing scene and read back its pixels data
33  /// FNVTexturePixelData - The struct contain the captured scene's pixels data
34  typedef TFunction<void(const FNVTexturePixelData&)> OnFinishedCaptureScenePixelsDataCallback;
35 
36 
37 public:
38  UNVSceneCaptureComponent2D(const FObjectInitializer& ObjectInitializer);
39 
40  virtual void UpdateSceneCaptureContents(FSceneInterface* Scene) override;
41 
42  /// Build a projection matrix (instrinsic) base on the camera settings (aspect ratio, projection type, fov and orthogonal width)
43  static FMatrix BuildProjectionMatrix(const FNVImageSize& RenderTargetSize, ECameraProjectionMode::Type ProjectionType,
44  float FOV, float OrthoWidth);
45 
46  /// Build a projection matrix from a specific view location and rotation
47  static FMatrix BuildViewProjectionMatrix(const FTransform& ViewTransform,
48  const FNVImageSize& RenderTargetSize,
49  ECameraProjectionMode::Type ProjectionType,
50  float FOVAngle,
51  float OrthoWidth,
52  FMatrix& ProjectionMatrix);
53 
54  /// Build a projection matrix from a specific view transform and projection matrix
55  static FMatrix BuildViewProjectionMatrix(const FTransform& ViewTransform, const FMatrix& ProjectionMatrix);
56 
57  FMatrix GetProjectionMatrix() const;
58 
59  /// Order the component to capture the scene to the render target texture, after the scene is captured, read back the pixels data
60  /// NOTE: This function run asynchronously, the callback function should use the context data to decide what to do with the pixels data
61  /// This function is combination of CaptureSceneToTexture and ReadPixelsDataFromTexture
62  void CaptureSceneToPixelsData(UNVSceneCaptureComponent2D::OnFinishedCaptureScenePixelsDataCallback Callback);
63 
64  UFUNCTION(BlueprintCallable, Category = "Exporter")
65  void StartCapturing();
66  UFUNCTION(BlueprintCallable, Category = "Exporter")
67  void StopCapturing();
68 
69 protected:
70  void BeginPlay() override;
71  void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
72  void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
73 
74 #if WITH_EDITOR
75  virtual bool GetEditorPreviewInfo(float DeltaTime, FMinimalViewInfo& ViewOut) override;
76 #endif //WITH_EDITOR
77 
78  /// Command tell this scene capturer to capture the scene into its render target texture later in the rendering phase
79  void CaptureSceneToTexture();
80 
81  /// Read back the pixels data from the captured texture
82  /// NOTE: This function run synchronously (it flush rendering commands) which can cause hitches on the game thread
83  bool ReadPixelsDataFromTexture(FNVTexturePixelData& OutPixelsData);
84  /// Async function to read back the pixels data from the captured texture
85  /// NOTE: This function run asynchronously, the callback function should use the context data to decide what to do with the pixels data
86  void ReadPixelsDataFromTexture(OnFinishedCaptureScenePixelsDataCallback Callback);
87 
88  bool ShouldCaptureCurrentFrame() const;
89  bool ShouldReadbackPixelsData() const;
90 
91  void OnSceneCaptured();
92  void InitTextureRenderTarget();
93 
94 public: // Editor properties
95  /// The size (width x height in pixels) of the captured TextureTarget
96  // NOTE: If a valid TextureTarget is specified then this property will be ignored
97  UPROPERTY(VisibleAnywhere, Category = "SceneCapture")
98  FNVImageSize TextureTargetSize;
99 
100  /// Pixel format of the captured TextureTarget
101  /// NOTE: If a valid TextureTarget is specified then this property will be ignored
102  UPROPERTY(EditAnywhere, Category = "SceneCapture")
103  TEnumAsByte<ETextureRenderTargetFormat> TextureTargetFormat;
104 
105  UPROPERTY(EditAnywhere, Category = "SceneCapture")
106  TEnumAsByte<EPixelFormat> OverrideTexturePixelFormat;
107 
108  /// If true, don't read back the raw alpha value from the render target but set it to 1
109  UPROPERTY(EditAnywhere, Category = "SceneCapture")
110  bool bIgnoreReadbackAlpha;
111 protected: // Transient properties
112  FNVTextureRenderTargetReader RenderTargetReader;
113  TArray<UNVSceneCaptureComponent2D::OnFinishedCaptureScenePixelsDataCallback> ReadbackCallbackList;
114 };