# Velocity / Motion vector in SceneCapturePixelShader

**URL:** <https://forums.unrealengine.com/t/velocity-motion-vector-in-scenecapturepixelshader/471024>\
**Category:** Rendering\
**Tags:** scene-capture-cube, Scene-Capture, unreal-engine, Motion-Blur, shader, question\
**Created:** [September 8, 2020, 2:50pm UTC](https://forums.unrealengine.com/t/velocity-motion-vector-in-scenecapturepixelshader/471024 "2020-09-08T14:50:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_0117a655](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0117a655](https://forums.unrealengine.com/u/anonymous_user_0117a655)\
**Post date:** [September 8, 2020, 2:50pm UTC](https://forums.unrealengine.com/t/velocity-motion-vector-in-scenecapturepixelshader/471024/1 "2020-09-08T14:50:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_06fcd7ce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_06fcd7ce](https://forums.unrealengine.com/u/anonymous_user_06fcd7ce)\
**Post date:** [October 28, 2020, 10:52am UTC](https://forums.unrealengine.com/t/velocity-motion-vector-in-scenecapturepixelshader/471024/2 "2020-10-28T10:52:01Z")

</div>

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](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](https://rcaloca.blogspot.com/2017/05/adding-global-shaders-to-ue4.html))

---

<div class="post-metadata">

**Author:** ![entertainer\_mei](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@entertainer\_mei](https://forums.unrealengine.com/u/entertainer_mei)\
**Post date:** [January 20, 2021, 2:32am UTC](https://forums.unrealengine.com/t/velocity-motion-vector-in-scenecapturepixelshader/471024/3 "2021-01-20T02:32:42Z")

</div>

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