I am trying to compute dense optical flow in Unreal 5 using the Scene Capture Component 2D. What this means is that for each pixel in frame t-1, I want to know the vector (dx, dy) that will get me the matching pixel in frame t. I want this to account for both camera motion and the movement of meshes in the scene.
I have followed these older discussions to form my general approach (see link at bottom), but I’ve run into some snags where the output is not making sense. My general approach is to first create a custom material that uses SceneTexture:Velocity like so:
I then create a render target of format RGBA32F.
Lastly, I create a scene capture component 2D that targets my render target. It has the following settings:
Capture Source: Final Color (HDR) in Linear Working Color Space
Capture Every Frame: false
Capture on Movement: false
Always Persist Rendering State: true
In the PostProcessVolume for the Scene Capture, I add my material from above.
To render scenes, I have a C++ actor with the scene capture as a child, which I use to efficiently write out raw renders from the scene capture - I only write the R and G channels of the FLinearColor because I assume these are the motion vectors.
I am trying a basic experiment where I move forward in a straight line, which should give me flow vectors that point radially out from the center of the image. The raw image I get has very small min and max values:
MinX, MaxX: 0, 0.002677
MinY, MaxY: 0, 0.004555
If I visualize these vectors as a heatmap I get an image that has structure and information of some kind, but they don’t seem to point in the right direction:
I have also tried the DecodeVelocityFromTexture transformation that the velocity shader uses to map from (0,1) range to (-2,2), but this just maps the min/max values above to values close to -2 which doesn’t make sense:
const float InvDiv = 1.0f / (0.499f * 0.5f);
float3 V;
V.xy = EncodedV.xy * InvDiv - 32767.0f / 65535.0f * InvDiv;
I am a bit at a loss. Am I following the right approach for what I’m trying to do? Should I be writing a custom material shader instead? Note I am using Unreal 5.3.2.
Past discussion I have been following for reference:
Problem interpreting SceneTexture:Velocity Data - Development / World Creation - Epic Developer Community Forums (unrealengine.com)