You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+39-37
Original file line number
Diff line number
Diff line change
@@ -5,25 +5,24 @@
5
5
6
6
7
7
# A Small Introduction
8
-
UnrealEngine4 is known to be a powerful tool to create virtual worlds as it is a AAA production game engine. Generating temporally consistent data, with automatic pixel-wise annotations from complex scenes, such as traffic scenarios, is a capability worth leveragingfor machine learning, or more explicitly deep learning, contexts, and has been explored for a series of projects already. There are plugins available that handle rendering images from UE4 to disk at runtime, such as [UnrealCV](https://unrealcv.org/)and [AirSim](https://github.com/microsoft/AirSim).
8
+
UnrealEngine4 is known to be a powerful tool to create virtual worlds as it is a highly valued AAA production game engine. Generating temporally consistent data with automatic pixel-wise annotations from complex scenes, such as traffic scenarios, is a capability worth leveraging, especially for training and validation of machine learning, or more explicitly deep learning, applications, and has been explored in a variety of projects already. Already, there are plugins available that allow rendering images from UE4 to disk at runtime, such as prominently [UnrealCV](https://unrealcv.org/) and [AirSim](https://github.com/microsoft/AirSim). This repository aims to be a tutorial that demonstrates such an 'image capturing' mechanism in detail fo you to understand its inner workings, and in turn enable you to reuse it in a custom fashions that suit the needs of your project.
9
9
10
-
When I was setting up a scene for my research these plugins were just not yet supporting the latest engine version I wanted/needed to use for various feature reasons, and I was missing a place where knowledge of how to setup a capturing component for rendering images to disk myself was explains for non graphics-programmers. There is but of course a lot of source code available from the projects mentioned earlier and there are a lot of postings scattered across multiple platforms explaining parts of the problem and giving code for possible solutions even though they may be meant for a different issue.\
11
-
[Image of Scene with Segmentation]\
12
-
In this post I want to condense my findings on how to implement a component to capture images to disk from an arbitrary UE4 scene **from scratch** lowering the bar for UE4 beginners. This will include:\
10
+
*When I was setting up scenes for my research the formerly mentioned plugins were just not yet supporting the latest engine versions I wanted/needed to use, and also I was missing a place where the knowledge of how to render images to disk myself was explains for non-advanced graphics-programmers. Of course, there are lots of sources for code available online and also there are community blog-entries scattered across multiple platforms explaining parts of the problem as well as possible solutions, even though they typically are targeted for different issues.*
11
+
In this repository I want to condense my findings on how to implement a component to capture images to disk from an arbitrary UE4 scene **from scratch** lowering the bar for UE4 beginners. This will include:\
13
12
* Rendering images at high FPS without blocking the UE4 rendering thread
14
13
* Rendering segmentation (or other graphics buffers) at the same time
15
14
16
-
**Disclaimer: I do not own any of the code. I merely condensed the sources already available online for easier use!**\
17
-
**Also huge thanks to the UE4 AnswerHub community!**
15
+
**Disclaimer: I do not own any of the code. I merely condensed the sources already available online for easier use and provide an overview to the general functionality of this particular approach!**\
16
+
**Kudos to the UE4 AnswerHub community!**
18
17
19
18
# Plugin Support
20
-
Where the general idea of this repository is to communicate how to setup a custom image capturing in code, providing a baseline for further development to adapt the code to ones individual needs, I understand that Unreal's Blueprint interface is powerful and some people have their reasons not to dive into C++ development with UE4.
19
+
The general idea of this repository is to communicate a possible setup for custom image capturing in code. This shall provide a baseline for further development to adapt the code to ones individual needs. I understand that Unreal's Blueprint interface is powerful and some people have their reasons not to dive into C++ development with UE4.
21
20
22
-
Now there is also a **Plugin** version of the code available. It generally provides the same functionality as the tutorial code and is structured in the same way, with minor tweaks for more straight-forward use in blueprints.
21
+
This is why now there is also a **Plugin** version of the code available. It generally provides the same functionality as the tutorial code and is structured in the same way, with minor tweaks for more straight-forward use in blueprints.
23
22
24
23
It comes with open sources so that everybody may compile it for their platform. (Only Win64 Engine version 4.22 binaries are included, more will follow soon (hopefully...))
25
24
26
-
To incorporate the Plugin in to your project: Create a **Plugins** directory in your project and copy the ```\UnrealImageCapture\Plugins\CameraCaptureToDisk``` directory. Load the plugin in your project, if not automatically done by the editor, and simply place the `CameraCaptureManager_BP`, which is to be found in the plugin's contents, in the scene and fill in its required slots as depicted below. This will require you to place a ```SceneCapture2D``` in your scene.
25
+
To incorporate the Plugin in to your project: Create a **Plugins** directory in your project and copy the ```\UnrealImageCapture\Plugins\CameraCaptureToDisk``` directory. Load the plugin in your project, if not automatically done by the editor, and place the `CameraCaptureManager_BP`, which is to be found in the plugin's contents, in the scene and fill in its required slots as depicted below. This will require you to place a ```SceneCapture2D``` in your scene.
27
26
A ```PostProcessMaterial``` for segmentation is also located in the plugin's contents.
@@ -37,22 +36,24 @@ An image-capturing-command can be triggered from Blueprint as exemplary depicted
37
36
38
37
39
38
# How to Save Images to Disk In UE4 (without blocking the rendering thread)
40
-
I will go through the code step by step so that it is hopefully easier to implement each step as you are following along. The full source code is placed in this git.
39
+
I will go through the code step-by-step so that hopefully it will be easier to implement each step as you are following along. The full source code is placed in this repository.
41
40
42
41
## Prerequisite
43
-
You will need a UE4 C++ project.
42
+
You will need a UE4 (or UE5) C++ project.
44
43
45
-
Also, you might have to add a few packages to your `'YourProjectName'.Build.cs` file. These are already included into UnrealEngine, but they are sometimes not loaded automatically which will lead to unpleasant linker errors. Find the `'YourProjectName'.Build.cs` file in the `Source/'YourProjectName/` directory, and add or extend it to inlcude all modules listed in the following line:
44
+
Also, you might have to add a few packages to your `'YourProjectName'.Build.cs` file. These are already included into UnrealEngine, however, sometimes they are not added automatically, resulting in unpleasant linker errors. Find the `'YourProjectName'.Build.cs` file in the `Source/'YourProjectName/` directory, and add or extend it to include all modules listed in the following line:
The component I am using for capturing is the ```SceneCaptureComponent2D``` provided as default by the UE4Editor. Placing one of these into your scene will give you a ```ASceneCaptureComponent``` which is its Actor instance. It basically behaves like any other camera component, except for having a viewport that is not restricted by your computer's monitor or main camera viewport, giving the possibility to render images larger than than the actual screen resolution.
52
+
I am using a ```SceneCaptureComponent2D``` as the basis for capturing images. Placing one of these into your scene will give you an ```ASceneCaptureComponent``` which is its `Actor` instance. It basically behaves like any other camera component, but its viewport is not restricted by your computer's monitor or main camera viewport. This provides us the possibility to render images of arbitrary resolution independent from the actual screen resolution.
54
53
55
-
Add a ```CaptureManager``` class of type Actor to your project.
54
+
#
55
+
56
+
> Add a ```CaptureManager``` class of type Actor to your project.
56
57
57
58
In the ```CaptureManager.h``` we add the following:\
This enables you to assign a ```CaptureComponent2d``` to your ```CaptureManager``` code.
73
74
74
-
Compile and place a ```CaptureManager``` in your scene. As it does not have any primitive to render you will only see it in the editor's outline. In the details panel of the placed ```CaptureManager``` you can now see the ```ColorCaptureComponent``` assigned to ```None```. From the drop down menu select the ```CaptureComponent2D``` you already placed in the scene.
75
+
> Compile and place a ```CaptureManager``` in your scene.
76
+
77
+
As it does not have any primitive to render you will only see it in the editor's outline. In the details panel of the placed ```CaptureManager``` you can now see the ```ColorCaptureComponent``` assigned to ```None```. From the drop down menu select the ```CaptureComponent2D``` you already placed in the scene.
75
78
76
-
Back to code: We will now prepare our yet "naked" CaptureComponent2Dfor capturing images, creating and assigning a RenderTarget, which is basically a Texture to store our image data to, and setting the camera properties. *Note: You could also do the this in the Editor but if you deal with, for example, multiple capture components etc., you may find it handy to not worry about creating and assigning all the components by hand*.
79
+
Back to code: We will now prepare our yet "naked" `CaptureComponent2D` class for capturing images, creating and assigning a `RenderTarget`, which is basically a `Texture` to store our image data to, and setting the camera properties. *Note: You could also do this in the Editor but if you deal with, i.e. multiple capture components, you may find it handy not to worry about creating and assigning all the components by hand!*
77
80
78
-
Create a setup function to put all your setup code for the CaptureComponents in the CaptureManger:
81
+
> Create a setup function to put all your setup code for the CaptureComponents in the CaptureManger:
Now that we have a RenderTarget applied to our CaptureComponent we can read its data to disk.
144
+
Now that because we have a `RenderTarget` applied to our `CaptureComponent` we can read its data to disk.
142
145
143
146
#
144
147
## Organize RenderRequests
145
-
We do this by basically re-implementing UE4's code for taking screenshotswith the addition of not flushing our rendering pipeline to prevent rendering hiccups dropping the framerate to 3 - 5 FPS.
148
+
We do this by basically re-implementing UE4's code for taking screenshots. Importantly, with the addition of not flushing our rendering pipeline. This prevents rendering *hiccups* that drop the framerate to 3 - 5 FPS.
146
149
147
-
This comes with the price of needing to handle the waiting before an image is done being copied from GPU so that we do not read an old or uninitialized buffer (Render Thread and GameThread are not synchronous). We do this by keeping a queue of ```RenderRequest``` that we can probe for being completed.
150
+
This addition will come with the price of needing to handle 'waiting times' before an image is done and copied from GPU. This is important to prevent reading old or uninitialized buffers (remember that `RenderThread`and `GameThread` are asynchronous). We do this by keeping a queue of ```RenderRequest``` that we can probe for being completed.
148
151
149
-
We add the following ```struct``` to our CaptureManager class:
152
+
> We add the following ```struct``` to our `CaptureManager` class:
150
153
151
154
**CaptureManager.h**
152
155
```cpp
@@ -172,9 +175,9 @@ UCLASS()
172
175
class ...
173
176
[...]
174
177
```
175
-
The ```Image``` will be the color buffer our CaptureComponent writes to. ```RenderFence``` is a neat feature of UE4 letting you put a "fence" into the render pipeline that knows when it has passed the full pipeline, giving a way to assess whether our render command must have been passed as well. The ```isPNG``` flag will be important later when we want to also store semantic labels which should not be stored as JPEG as the compression introduces small errors into the color/label data...
178
+
The ```Image``` will be the color buffer our `CaptureComponent` writes to. ```RenderFence``` is a neat feature of UE4 letting you put a 'fence' into the render pipeline that can be checked to notify when it has passed the full pipeline. This gives a way to determine whether our render command has passed as well. The ```isPNG``` flag will be important later when we want to also store semantic labels which should not be stored as JPEG as the compression introduces artifacts into the color/label data...
176
179
177
-
Also we need to add our ```TQueue```, keeping track of our render requests:
180
+
> We need to add our ```TQueue```, keeping track of our render requests:
178
181
179
182
**CaptureManger.h**
180
183
@@ -186,7 +189,7 @@ protected:
186
189
187
190
#
188
191
## Implement the image capturing function:
189
-
This function will place a render request on the UE4 rendering pipeline asking the data captured from our CaptureComponent to be copied in our Image buffer so that we can further process it in GameThread.
192
+
This function will place a render request on the UE4 rendering pipeline asking the data captured from our `CaptureComponent` to be copied in our Image buffer so that we can further process it in `GameThread`.
With this the image data is already stored in our queue, we now need to store it to disk. *Note: UFUNCTION(BlueprintCallable, Category = "ImageCapture") exposes this function to blueprint, so that you could easily test it*
261
+
With this, the image data is already stored in our queue, and we now need to store it to disk. *Note: UFUNCTION(BlueprintCallable, Category = "ImageCapture") exposes this function to blueprint, so that you can easily test it*
259
262
260
263
#
261
264
## Save Image Data to Disk
262
-
To do so, each tick of the `CaptureManager` we look up the first element of the `RenderQueue`, if its `RenderFence` is completed then we save the image to disk, else we do nothing.
265
+
To do so, in each tick of the `CaptureManager` we look up the first element of the `RenderQueue`, if its `RenderFence` is completed then we save the image to disk, else we do nothing.
263
266
264
-
The last thing we need is a procedure to write to disk, this time without blocking our game thread. For this we implement an asynchronous procedure storing
265
-
the data to disk.
266
-
[Link to UnrealWiki](https://wiki.unrealengine.com/Using_AsyncTasks)
267
+
The last thing we need is a procedure to write to disk, preferably without blocking our `GameThread`.
268
+
>We implement an [asynchronous](https://wiki.unrealengine.com/Using_AsyncTasks) procedure storing the data to disk.
For test purposes we can call the `CaptureColorNonBlocking()` from the `LevelBlueprint` attaching it to a button press.
399
+
For testing purposes we can call the `CaptureColorNonBlocking()` from the `LevelBlueprint`by attaching it to a button pressed event.
398
400
399
401
[Image of the level blueprint]
400
402
401
-
The images captured will now be saved into your project's `Saved` directory.
403
+
The captured images will now be saved into your project's `Saved` directory.
402
404
403
405
#
404
406
#
405
407
406
408
# Capturing Segmentation
407
-
To get labels for our images we will add a second CaptureComponent equipped with a `PostProcessMaterial` that will render `CustomDepth` that is settable for each actor in the scene, effectively letting us label and visualize categories of actors.
409
+
To get labels for our images we will add a second `CaptureComponent` equipped with a `PostProcessMaterial` that visualizes `CustomDepth`. The `CustomDepthStencil`is settable for each actor in the scene, effectively letting us label and visualize categories of, as well as individual, actors.
408
410
409
411
## Enable Custom Depth Stencils
410
412
Find the **ProjectSettings** in your editor and search for *stencil* which will bring up `Custom Depth-Stencil Pass`. Switch this option from `Enabled` to `Enabled with Stencil`.
@@ -424,7 +426,7 @@ Add a `Division` node and connect the `SceneTexture`'s `Color` output to the div
424
426
Apply and save the material.
425
427
426
428
## Setting up Custom-Depth Stencils
427
-
You can set the custom-depth in editor or from code, for simplicity I will this time use the editor. Place an arbitrary object(MeshActor) into the scene, and search for `custom depth` in its details panel. Under `Rendering` enable `Render CustomDepth Pass`, and set `CustomDepth Stencil Value` to whatever you like. For illustration purposes set it to 200.
429
+
You can set the customdepth in editor or from code. For simplicity I chose the editor. Place an arbitrary object(MeshActor) into the scene, and search for `custom depth` in its details panel. Under `Rendering` enable `Render CustomDepth Pass`, and set `CustomDepth Stencil Value` to whatever you like. For illustration purposes set it to 200.
428
430
429
431
#
430
432
@@ -526,6 +528,6 @@ To save the image information from SegmentationCapture we can simply use the `Ca
526
528
#
527
529
528
530
# Known Issues
529
-
The `IImageWrapperModule`'s wrapping of the data is still done in GameThread rather than in a async call, which can actually consume more runtime than the saving to disk. Simply pushing the WrapperModule into the async procedure does suffice since 1) it is a shared pointer, 2) the `ImageWrapperModule.CreateImageWrapper(...)` needs to be called from GameThread. I am grateful for any ideas on that..
531
+
The `IImageWrapperModule`'s wrapping of the data is still done in `GameThread` rather than in an async call, which can actually consume more runtime than the saving to disk. Simply pushing the WrapperModule into the async procedure does suffice since 1) it is a shared pointer, 2) the `ImageWrapperModule.CreateImageWrapper(...)` needs to be called from GameThread. I am grateful for any ideas on that..
530
532
531
-
It is possible that an image is saved every game tick at high fps. If saving to disk is actually slower than the delta time of the game tick another call to the shared IImageWrapper is made while its buffer is read for saving to disk. This results in a game crash. This should be fixable by adding semaphores, I just did not have the time to test this yet.
533
+
It is possible that an image is saved every game tick at high fps. If saving to disk is actually slower than the delta time of the game tick another call to the shared `IImageWrapper` is made while its buffer is read for saving to disk. This results in a game crash. This should be fixable by adding semaphores, I just did not have the time to test this yet.
0 commit comments