Velocity / Motion vector in SceneCapturePixelShader

Hey,

I am trying to render a velocity map (including velocity of moving objects and velocity of camera).

Currently our pipeline is using a USceneCaptureComponentCube that implements the SceneCapturePixelShader.usf pixel shader:

IMPLEMENT_SHADER_TYPE(template<>, TSceneCapturePS<SCS_Velocity>, TEXT("/Engine/Private/SceneCapturePixelShader.usf"), TEXT("Main"), SF_Pixel);

Where SCS_Velocity is a new rendering mode I have added to SceneCapturePixelShader.usf:

#elif SOURCE_MODE_VELOCITY
	FGBufferData GBufferData = GetGBufferData(Input.UV);
	float2 camera_induced_velocity = CameraInducedVelocity(CalcSceneDepth(Input.UV), Input.UV);
	float2 moving_objects_velocity = GBufferData.Velocity.xy;
	OutColor = float4(moving_objects_velocity + camera_induced_velocity, 0, 0);

So, I’m trying to modify SceneCapturePixelShader.usf, such that the screen-space velocity is saved in OutColor.

But so far, I only managed to get the GBuffer velocity (after some hacks) which apparently only includes velocity of moving objects.

To get the pixel velocity due to camera motion, I tried using code from other shader:

float2 CameraInducedVelocity(float depth, float2 ViewportUV)
{
	// Compute velocity due to camera motion.
	float2 ScreenPos = 2 * float2(ViewportUV.x, 1 - ViewportUV.y) - 1;
	float4 ThisClip = float4(ScreenPos, depth, 1);
	float4 PrevClip = mul(ThisClip, View.ClipToPrevClip);
	float2 PrevScreen = PrevClip.xy / PrevClip.w;
	float2 Velocity = ScreenPos - PrevScreen;

	return Velocity;
}

But this just returns zero.
Maybe View.ClipToPrevClip is always zero, because I’m using a USceneCaptureComponent?

I have read through numerous posts regarding calculation of motion vectors in UE4, but non of the suggested solutions seemed to work, in my particular case.

You are my only hope…

Hey,
did you solve this issue in the mean time? I am also trying to achieve proper velocity output and I found a pull request, which might solve your issue of camera induced velocity: https://github.com/EpicGames/UnrealEngine/pull/6933 . They exchange your lines of codes with something slightly different, using a proper projection involving the absolute world position.

However, I cannot confirm that the GBuffer Velocity data is correct for moving objects. Could you explain in more detail what do you mean by “after a few hacks”? And why did you modify SceneCapturePixelShader? I am new to UE4 and am wondering if this can be achieved by a custom Global Shader? (Yet another blog...: Adding Global shaders to UE4)

Any solutions? I’m concerned about this problem either.