Velocity / Motion vector in SceneCapturePixelShader

anonymous_user_0117a655
Tue, 08 Sep 2020 14:50:50 +0000
Rendering

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…

3 posts - 1 participant