Search Unity

Get access to SkinnedMeshRenderers prev-vert-pos?

Discussion in 'Shaders' started by zezba9000, Jun 3, 2021.

  1. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    Unity docs say skinning is double-buffered mesh verts.
    So how do I get access to the last-frames verts in a shader?

    This doesn't work for example. So in what buffer or buffer-stream are these stored?
    Code (CSharp):
    1. float3 positionOS_Last : POSITION1;
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    TEXCOORD4
     
  3. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    Are those stored in "float3"? I'm not getting expected results.

    In my custom SRP with "context.DrawRenderers" I'm setting "drawSettings.perObjectData = PerObjectData.MotionVectors;". I'm not sure if this matters or if some other setting is needed for it to generate these correctly in the SkinnedMeshRenderer.

    Here are the last positions represented as RGB (I don't see any blue values [aka Z])
    upload_2021-6-3_10-43-49.png

    If I subtract the "current (POSITION) & last-position (TEXCOORD4)" from one another I would expect everything to be at zero for the most part but as you can see in this image the last-position data is wrong.
    upload_2021-6-3_10-46-13.png
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Yes, it's a
    float3
    . Those however look like standard UVs.

    For the built in renderer,
    skinnedMotionVectors
    needed to be enabled on the
    SkinnedMeshRenderer
    component itself for them to show up. I assume the same is true for the SRPs, but I have little experience with those. However once
    skinnedMotionVectors
    was enabled,
    TEXCOORD4
    was filled with the previous frame's object space position regardless of if motion vectors were enabled on the camera or used elsewhere.
     
  5. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    I will hunt more though the URP to see if I can find whats missing but its got to be something simple.

    There is probably some ScriptableRenderContext.DrawRenderers setting that needs to be on for this but I don't see anything in more than "PerObjectData.MotionVectors" thats relevent. Really wish there were better docs on something so fundamental to the system.

    Looks like I'm not the only one with this issue since 2018: https://forum.unity.com/threads/get-skinned-meshs-previous-frame-vertex-position-in-shader.1097551/
     
  6. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    Ok figured it out. Because a custom SRP doesn't expose the "depthTextureMode" property on the camera for some reason. In the SRP I just set it "camera.depthTextureMode = DepthTextureMode.MotionVectors;" Then invoke "ScriptableRenderContext.DrawRenderers" with "DrawingSettings.perObjectData = PerObjectData.MotionVectors"

    Now skinned last-pose "float3 positionOS_Last : TEXCOORD4" and "float4x4 unity_MatrixPreviousM", "float4 unity_MotionVectorsParams" seems to be coming through correctly. As I figured, something super simple.
     
    Last edited: Jun 4, 2021
  7. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    And motion blur works. Thanks for the initial tip. Was able to find the rest of what I needed from that.
    upload_2021-6-4_11-0-45.png
     
  8. RaphaelHunter

    RaphaelHunter

    Joined:
    Nov 20, 2018
    Posts:
    1
    I'd like to ask that , how to enable skinnedMotionVectors option in URP editor.
     
  9. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    LTS 2020.3.32f1, lastPos at TEXCOORD4 can be accessed only when the camera MotionVectors is on. I tried On/Off in both runtime and editor. There should be an option to last access this lastPos without having to use the texture.
     
    Last edited: Mar 26, 2022
  10. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    upload_2022-3-26_20-7-33.png
    Would be fun for these kind of cheap, vertex manipulation effects :)