Sample motion vectors in a shader in URP
Request the motion vector texture as an input in a custom shader.
Read time 1 minuteLast updated 23 days ago
Any implementation can request the motion vector texture as input. To do that, add the flag in the ScriptableRenderPass.ConfigureInput method in the AddRenderPasses callback of your custom Renderer Feature. If no other effect in the frame is using motion vectors, setting this input flag forces the URP renderer to inject the motion vector render pass into the frame.
ScriptableRenderPassScriptableRenderPassInput.MotionTo sample the motion vector texture in your shader pass, declare the shader resource for it in the section:
HLSLPROGRAMTEXTURE2D_X(_MotionVectorTexture); SAMPLER(sampler_MotionVectorTexture);
To perform the sampling, use the following macro:
SAMPLE_TEXTURE2D_X(_MotionVectorTexture, sampler_MotionVectorTexture, uv);
The postfix ensures that the texture is correctly declared and sampled when targeting XR platforms.
_X