Motion vectors in HDRP

I’m trying to understand motion vectors so I can make them work for my custom skinned mesh renderer. I read somewhere that all you need to do is supply the previous frame’s mesh data inside TEXCOORD4. Seems simple enough.

Currently, I’m rendering using Graphics.DrawMesh(). There doesn’t seem to be any way to indicate that you want to use motion vectors with DrawMesh(), but I suppose you could always fall back on using a MeshRenderer instead. However, I can’t even seem to get them to work on a regular MeshRenderer, like a cube created inside the Unity interface.

I have this very simple testing scene:

The character is standard skinned mesh, the cube is MeshRenderer.

In the frame debugger, only the character will be rendered into the motion vectors buffer. This happens regardless if the character moves or not. However, the cube moving has no effect on whether or not it is included in this pass.

The cube has motion vectors enabled, and yet nothing:

What exactly am I doing wrong here? Why isn’t the cube included?

And is there a way to get motion vectors working with Graphics.DrawMesh()?

edit:
The cube won’t be included in motion buffers regardless of what shader is used. I can use HDRP/Lit and yet nothing.

Ok this makes no sense to me, but if disabling motion vectors for the cube, it will be included in the motion vectors pass. Not sure if this is a bug or if I simply don’t understand how this all is supposed to work:

Note, this was in built-in, but HDRP behaves the same.

Ok, I guess the Cube has its motion vectors written in another pass or along with another buffer, because I just found the Motion Vectors debug view (Window > Analysis > Rendering debugger) and it works beautifully.

If you want to add custom motion vectors, you can do that through shader graph by enabling the velocity output:

Note that the material I was using didn’t update after enabling this option. It wasn’t until I unchecked and checked “Motion vectors for vertex animation” in the material that it finally actually started rendering the motion vectors. So if you have that problem, just change the material!

2 Likes

Motion vectors will not work with Graphics.DrawMesh(), but you can use the new Graphics.RenderMesh() to get them working.

Note though that RenderMesh() seems rather buggy atm. I’ve had it crash Unity quite easily and submitted a bug report (#IN-5058).