Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Enabling Motion Vector Pass For DrawProcedural?

Discussion in 'High Definition Render Pipeline' started by JM_CG, Jul 27, 2020.

  1. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    I'm doing some rendering of some procedural objects in HDRP and finding that I cannot get them to generate motion vectors during rendering.

    I've verified that my shader is working by forcing motion vector values - it works on mesh objects, but not procedural.

    Is there a way to turn this on easily or it is something deeper?

    Thanks
     
  2. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Hi,

    For motion vector to work on procedural it would require to have both the previous mesh frame and current one (or to simply transform the position two times, one with previous transform matrix and one with new transform matrix). We currently don't have anything to handle that. We plan to expose a motion vector input on master node in shader graph (and support draw procedural for shader graph). But I have no answer for you currently.
     
  3. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    247
    Im sorry, but what does it have to do with having access to the mesh? I dont think thats what the question was about. We are handling the motion vectors calculation in our shaders ourselves, thats not the problem. The problem is that even if you have a motion vector pass with correct motion vectors information, its simply ignored, we now have to use custom passes and command buffers to force rendering motion vectors, which bypasses instancing/batching/culling etc. In DrawProcedural there is an option to choose shadow casting mode, which works correctly. There should be an option like that to choose motion vectors mode similar to shadow casting mode.
     
  4. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    >We are handling the motion vectors calculation in our shaders ourselves, thats not the problem
    How do you send the motion vector data to the shader?

    For motion vector pass we expect previous position to be provided in TexCoord4 (for regular skinning / deformation=.

    // Available semantic start from TEXCOORD4
    struct AttributesPass
    {
    float3 previousPositionOS : TEXCOORD4; // Contain previous transform position (in case of skinning for example)
    #if defined (_ADD_PRECOMPUTED_VELOCITY)
    float3 precomputedVelocity : TEXCOORD5; // Add Precomputed Velocity (Alembic computes velocities on runtime side).
    #endif
    };

    in MotionVectorVertexShaderCommon.hlsl.

    Maybe what you could do is crurrently provide your precomputed motion vector the same way we handle Alembic, by providing themin TexCoord5 (see above).

    Then you need to enable the _ADD_PRECOMPUTED_VELOCITY keyword (which is an option on the Material - "Add Precomputed Velocity") . This option will enable the motoin vector pass which is disabled by default on the Material by our inspector
     
  5. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    247
    The shader in question is a custom shader written from scratch. It has a valid custom motion vectors pass as well. Again, the problem is not in the shader code, or how we handle the motion vectors data, but in the fact that that pass is completely ignored by the pipeline. I have a shadow caster pass in there as well - that one is not ignored. But the motion vectors pass is ignored. (Our project is on 2019.4.3 / HDRP 7.2.1 btw, maybe it was fixed in later versions?) I assume it cause it works similar to regular objects, and only renders motion vectors if the object's transform has changed, or it has its motion vectors mode set to Force No Motion (neither of which we can affect with DrawProcedural).
     
    Last edited: Jul 27, 2020
  6. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Ah ok, sorry was misunderstanding the DrawProcedural usage.

    hmm... so it should work. By default builtin Unity indeed rely on having something moving (matrix transform) or force no motion to have a motion vector pass processed, but in HDRP you can also simply have the motion vector pass enabled on the material and it is sufficient to say that you are "forcing" the motion vector pass (this is how we handle vertex animation motion vector).

    So short, it should work.

    Sanity check, can you confirm your pass is named like this (case sensitive) ?

    Name "MotionVectors"
    Tags{ "LightMode" = "MotionVectors" } // Caution, this need to be call like this to setup the correct parameters by C++ (legacy Unity)

    Also can you confirm that the pass is "enabled" on the Material.
    Also the renderqueue? is it opaque?
    Is it not working in editor or only at runtime?

    >HDRP 7.2.1
    my memory is fuzzy on this version, but I would not expect improvement regarding this, code is mainly drive by C++
     
    customphase likes this.
  7. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Note: If you are allowed to share your custom shader or a simplifed version of it, we can investigate. Thanks
     
  8. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    247
    Yup.
    upload_2020-7-28_0-50-22.png

    Yup.
    upload_2020-7-28_0-52-11.png

    upload_2020-7-28_0-56-40.png

    upload_2020-7-28_1-1-32.png

    Opaque. Not working in editor, didnt test at runtime because we need it to work in editor anyway.

    upload_2020-7-28_0-59-17.png
     
    Last edited: Jul 28, 2020
    JM_CG likes this.
  9. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Hi Seb,

    Thanks for your reply.

    It looks like DrawProcedural is not queuing the MotionVector Pass. Looking in FrameDebug I only see 1 ObjectMotionVector pass for a test mesh object that has the same material as the procedural object. So perhaps there is a per-object tag that is not getting set to enable the pass.

    For testing, in the Lit material, I am forcing the outMotionVector like this in the motion vector Frag, which works for the aforementioned mesh object but not the procedural one.
    outMotionVector = float4(0.0, 1.0, 0.0, 0.0);

    I also explicitly set this but it still seems that it is missing the pass.
    MaterialOpaque.SetShaderPassEnabled("MotionVectors", true);

    I'm using Unity 2020.1b, not using Shader Graph.
     
    Last edited: Jul 28, 2020
    customphase likes this.
  10. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
    Hi, I have the same problem. I'm also trying to get he MotionVectors Pass working with DrawProcedural. Maybe it's not supported?
     
    customphase likes this.
  11. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
    Looks like Motion Vectors haven't been support since years! Why does Unity not give a better support for Graphics.Functions they are so powerful and useful for Procedural Objects!
    https://github.com/keijiro/KvantSprayMV
    This shows that the problem already exists 4 years ago!
     
  12. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    i can confirm that using drawmeshinstanced or drawmeshinstancedindirect both do not write motion vectors either but just get ignored by the pipeline.

    in our case we do vertex animations (grass) so it is relatively easy to calculate the motion vectors. the shader of course has a proper and properly named pass but still: the pipeline simply ignores it.
    i was able once to make them render by editing the hdrp renderpipeline and changing somehow:
    Code (CSharp):
    1. var rendererList = RendererList.Create(CreateOpaqueRendererListDesc(cullResults, hdCamera.camera, HDShaderPassNames.s_MotionVectorsName, PerObjectData.MotionVectors, stateBlock: stateBlock));
    i guess i left out the "PerObjectData.MotionVectors" or so. i can't remember exactly...

    hacking hdrp of course is not nice.
    and vfx graph manages to write out motionvectors although it might do something similar to drawmeshinstanced or drawmeshinstancedindirect.
    would be great if we somehow could add a flag to drawmeshinstanced or drawmeshinstancedindirect so it registers properly to the pipeline.
     
    customphase likes this.
  13. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    interesting. which custom pass entry point gives you access to the motion vector buffer? or how do you set it as render target?
     
  14. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    247
    Yeah, we had to modify the pipeline to expose additional injection points
     
  15. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Hi, fyi we are currently investigating the issue.
     
    customphase and JM_CG like this.
  16. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Appreciate the update. Thanks kindly!
     
  17. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    great news, thanks!
     
  18. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    oh, that sounds evil :)
     
  19. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
  20. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
    Btw.: I also found a way to get it working but it needs some additional functions in the HDRP pipeline and also a Custom Pass, which renders the Mesh to the MotionVectors-Texture... so very hacky and dirty. I would recommend to wait until the Unity team fixed it.
     
  21. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
    If anyone is interested, this is how my custom pass looks like:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3. using UnityEngine.Rendering;
    4. using System.Reflection;
    5. using System.Collections.Generic;
    6.  
    7. class WriteMV : CustomPass
    8. {
    9.     [SerializeField] Mesh _mesh;
    10.     [SerializeField] Shader _motionVectorShader;
    11.     [SerializeField] Transform _transform;
    12.  
    13.     Matrix4x4 _previousModelMatrix;
    14.     Material _motionVectorMaterial;
    15.  
    16.     static HDRenderPipeline _hdrp;
    17.     static MethodInfo _mGetMotionVectors;
    18.     static MethodInfo _mGetDepthStencilBuffer;
    19.     static PropertyInfo _sharedRTManager;
    20.     static Dictionary<HDCamera, RenderTargetIdentifier[]> _MotionVectorsPassBuffersRTI_List;
    21.     static Dictionary<HDCamera, RTHandle> _DepthStencilBuffer_List;
    22.  
    23.     protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    24.     {
    25.         if (_motionVectorShader != null) _motionVectorMaterial = new Material(_motionVectorShader);
    26.         if (_transform != null) _previousModelMatrix = _transform.localToWorldMatrix;
    27.         if (_hdrp == null) _hdrp = (RenderPipelineManager.currentPipeline as HDRenderPipeline);
    28.         if (_sharedRTManager == null) _sharedRTManager = _hdrp.GetType().GetProperty("sharedRTManager", BindingFlags.Instance | BindingFlags.NonPublic);
    29.         if (_mGetMotionVectors == null) _mGetMotionVectors = _sharedRTManager.PropertyType.GetMethod("GetMotionVectorsPassBuffersRTI");
    30.         if (_mGetDepthStencilBuffer == null) _mGetDepthStencilBuffer = _sharedRTManager.PropertyType.GetMethod("GetDepthStencilBuffer");
    31.         if (_MotionVectorsPassBuffersRTI_List == null) _MotionVectorsPassBuffersRTI_List = new Dictionary<HDCamera, RenderTargetIdentifier[]>();
    32.         if (_DepthStencilBuffer_List == null) _DepthStencilBuffer_List = new Dictionary<HDCamera, RTHandle>();
    33.     }
    34.  
    35.     public static RenderTargetIdentifier[] GetMotionVectorsPassBuffersRTI(HDCamera hdCamera)
    36.     {
    37.         return (RenderTargetIdentifier[])_mGetMotionVectors.Invoke(_sharedRTManager.GetValue(_hdrp), new object[] { hdCamera.frameSettings });
    38.         //return hdrp.sharedRTManager.GetMotionVectorsPassBuffersRTI(hdCamera.frameSettings);
    39.     }
    40.  
    41.     public static RTHandle GetDepthStencilBuffer(HDCamera hdCamera)
    42.     {
    43.         return (RTHandle)_mGetDepthStencilBuffer.Invoke(_sharedRTManager.GetValue(_hdrp), new object[] { hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA) });
    44.         //return hdrp.sharedRTManager.GetDepthStencilBuffer(hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA));
    45.     }
    46.  
    47.     protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
    48.     {
    49.         // Executed every frame for all the camera inside the pass volume
    50.         if (_motionVectorMaterial == null || _transform == null || _mesh == null)
    51.             return;
    52.  
    53.         _motionVectorMaterial.SetMatrix("_PreviousM", _previousModelMatrix);
    54.         _motionVectorMaterial.SetMatrix("_PreviousVP", CameraMatrixProvider.GetPreviousVPMatrix(hdCamera.camera));
    55.         _motionVectorMaterial.SetMatrix("_NonJitteredVP", CameraMatrixProvider.GetVPMatrix(hdCamera.camera));
    56.  
    57.         //cmd.SetRenderTarget(BuiltinRenderTextureType.MotionVectors, BuiltinRenderTextureType.CameraTarget);
    58.         hdCamera.camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth;
    59.  
    60.         if (!_MotionVectorsPassBuffersRTI_List.ContainsKey(hdCamera)) _MotionVectorsPassBuffersRTI_List.Add(hdCamera, GetMotionVectorsPassBuffersRTI(hdCamera));
    61.         if (!_DepthStencilBuffer_List.ContainsKey(hdCamera)) _DepthStencilBuffer_List.Add(hdCamera, GetDepthStencilBuffer(hdCamera));
    62.  
    63.         //CoreUtils.SetRenderTarget(cmd, HDUtils.GetMotionVectorsPassBuffersRTI(hdCamera), HDUtils.GetDepthStencilBuffer(hdCamera));
    64.         CoreUtils.SetRenderTarget(cmd, _MotionVectorsPassBuffersRTI_List[hdCamera], _DepthStencilBuffer_List[hdCamera]);
    65.  
    66.         cmd.DrawMesh(_mesh, _transform.localToWorldMatrix, _motionVectorMaterial, 0, 0); //<- replace cmd.DrawMesh with your cmd.DrawProcedural
    67.         //cmd.DrawProcedural(...);
    68.  
    69.         _previousModelMatrix = _transform.localToWorldMatrix;
    70.     }
    71.  
    72.     protected override void Cleanup()
    73.     {
    74.         if (_MotionVectorsPassBuffersRTI_List != null) _MotionVectorsPassBuffersRTI_List.Clear();
    75.         if (_DepthStencilBuffer_List != null) _DepthStencilBuffer_List.Clear();
    76.         CoreUtils.Destroy(_motionVectorMaterial);
    77.     }
    78. }
    The rest of the code you can find here:
    https://github.com/keijiro/DrawMeshWithMotionVectors
    You need to fix the shader to be correctly working for HDRP however you can already see results with the existing one.

    Cheers,
    Chris
     
    JM_CG likes this.
  22. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Hi,
    an update on the investigation. The root case of the issue is because when you use DrawProcedural, it create what we call an IntermediateRenderer that we initialize with default hardcoded value (i.e unlike MeshRenderer you don't have any control on light probe, rendering layer mask, motion vector mode etc...). And the hardcoded default value of motion vector mode is Camera motion vector (so on the binary side).

    Short: A draw procedural always ask for a camera motion vector mode and thus never render in the object motion vector pass.

    We are discussing internally about how we can expose those controls, but it also mean that we will not provide a "fix" for 2019.3 version as it require to expose new API etc...

    Note that it apply to all pipelines (HDRP, URP, Builtin)
     
    customphase and JM_CG like this.
  23. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    please also consider "fixing" this for drawmeshinstanced and drawmeshinstancedindirect :)
     
  24. Nexusmaster

    Nexusmaster

    Joined:
    Jun 13, 2015
    Posts:
    365
    Ok, but you are working on a "fix" for newer versions like 2020.1 ?
    I basically fixed the issue with my code snippet, but a solution from your side would be much better, hope this will work soon. Thanks!
     
    Last edited: Aug 19, 2020
    JM_CG likes this.
  25. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Wanted to check when this will be resolved. We'd prefer to avoid a custom pass and have it in the core version.
     
  26. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Hi, sadly no fix will be available before 2021.x as it will be new API to use
     
    SagarPatel likes this.
  27. SagarPatel

    SagarPatel

    Joined:
    Jan 23, 2014
    Posts:
    3
    Hi, just wanted to check if this fix will be available in 2021.x soon?
    I had a quick look at the changelogs for 2021.1 and 2021.2a and it seems like it hasn't happened yet
    (been using DrawProceduralIndrect a lot, it would be great to have the extra controls exposed)
     
  28. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    935
    Hi, we are currently updating the API of drawProcedural and others to fix this issues, it should be available in 2021.2 but is not part of the alpha version yet.
     
    larsbertram1 and customphase like this.
  29. madrobotMK

    madrobotMK

    Joined:
    Nov 19, 2013
    Posts:
    17
    I think I have the same Problem using the Alembic Importer (2.1.3) with the Import Points option which uses the AlembicPointsRenderer in HDRP (11 and 12).
     
  30. DanielZeller

    DanielZeller

    Joined:
    Nov 18, 2014
    Posts:
    18
    Any updates on this? I'd appreciate a nudge in this thread once the api is available in the 2021.2 alpha version :)
     
  31. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    https://github.com/Unity-Technologies/Graphics/pull/4190

    Looks like this branch is related to this.

    BTW. We are using DrawInstancedIndirect for foliage with vertex animation. Does anybody know if "Motion Vector For Vertex Animation" material option works with DrawInstancedIndirect?

    Thanks.
     
    Last edited: Jul 28, 2021
  32. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Still not working as of 2021.2.0b16.

    The motion vector pass still does not seem to be enabled/available.
    Using DrawProcedural / MyMaterial.SetShaderPassEnabled("MotionVectors", true);
     
  33. JarkkoUnity

    JarkkoUnity

    Unity Technologies

    Joined:
    Feb 1, 2021
    Posts:
    17
    Last edited: Oct 28, 2021
    JM_CG and customphase like this.
  34. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    hi jarkko,
    thanks for the heads up.

    first question
    my system uses DrawMeshInstancedIndirect do draw a vast amount of grass. why does it need a new api to support motion vectors? why couldn't it just be added to already existing apis?
    i saw that you were involved in this: https://github.com/Unity-Technologies/Graphics/pull/4190 so what went wrong?

    second question
    having a first glance at RenderPrimitives and RenderParams i only see support for
    instanceCount: The number of instances to render. i use compute to do per instance frustum culling and obviously get back the resulting instance count in a bufferWithArgs: The GPU buffer containing the arguments for how many instances of this mesh to draw.
    how should this be implemented using the new api?
    asyn readback? delay?

    best,
    lars
     
  35. JarkkoUnity

    JarkkoUnity

    Unity Technologies

    Joined:
    Feb 1, 2021
    Posts:
    17
    Hi lars,

    There are total of 8 new Render* functions, so what you are looking for is RenderPrimitivesIndirect. For this function you can use the GPU written draw arguments in the same way as for Draw*Indirect functions.

    These functions were added to better support future expansion of features without having to keep duplicating the draw functions.

    Cheers, Jarkko
     
  36. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    @JarkkoUnity
    Hi,

    I can be wrong but in the first question Lars is probably asking about rendering custom meshes with standard DrawMeshInstancedIndirect but with motion vectors enabled.

    There is also another thread with discussion about motion vectors for DrawMeshInstanced and DrawMeshInstancedIndirect but there is no answer from developers there. Also the new terrain details/trees system use one of this function (probalby the Indirect variant) so it's affected too.

    https://forum.unity.com/threads/sev...sh-trees-do-not-write-motion-vectors.1182244/

    Do you know if it's possible to have motion vectors with DrawMeshInstanced and/or DrawMeshInstancedIndirect somehow?

    Thanks!
     
    Last edited: Oct 28, 2021
  37. JarkkoUnity

    JarkkoUnity

    Unity Technologies

    Joined:
    Feb 1, 2021
    Posts:
    17
    @sqallpl Yes, you are correct. The new function for rendering meshes with indirect args is RenderMeshIndirect.

    Users are advised to move to use the new Render* functions as that's the new draw API going forward.

    Cheers, Jarkko
     
    sqallpl likes this.
  38. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    Good to know! I was worried that this is some abandoned bug or that motion vectors won't be supported.

    So I assume that if you will introduce/add support for any new functions in the future (like ray-tracing for example) they will be added to Graphics.Render functions and not Graphics.Draw functions.
     
  39. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    hi jarkko,

    thanks for the fast answer. any advice how to add the needed code to an HDRP shader graph?
    best,
    lars
     
  40. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Can this be used now in 2021.2 to render instanced meshes with motion vectors?

    Terrain detail mesh & trees needs to use this, currently it does not and there's no motion vectors on all the grass/trees.
     
  41. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    As explained before, the problem with the old APIs is that they don't expose control over the motion vector mode used for that draw command, using the default value "camera" which causes the motion vectors shader pass to be ignored.

    That pull request is just adding shader support for motion vectors calculation for instanced meshes (reading the previous object matrix from the instanced properties), it does not solve the API problem (which is not an HDRP limitation).

    The new APIs offer full replacements of the old ones and allow you to specify the motion vector mode to be used, so they are required for the above pull request to be of any use.
     
  42. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    that is fine :) testing RenderMeshIndirect i just run into the issue that the "InstanceID" node in shader graph always returns 0... any help on this would be great.
    i managed to fix the shadow caster pass simply commenting all the #if unityanyinstancing and manually pushing the instance id to my custom vertex function but this is just a pain!
     
  43. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    hey jarkko, after doing some test with HDRP and shader graph it looks like none of the instancing keywords are set?
    so the meshes get renderd but all on top of each other as instanceID just always is 0.
     
  44. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    @JarkkoUnity
    using something like this in a custom function in shader garph and HDRP 12.1 always return white.
    so no instancing is enabled by the engine?
    thus instanceID will always be 0?

    Code (CSharp):
    1. albedo = 1;
    2. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    3. albedo = float3(1,0,0);
    4. #endif
    5. #if UNITY_ANY_INSTANCING_ENABLED
    6. albedo = float3(0,1,0);
    7. #endif
     
  45. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Thanks very much for letting us know about the new functionality. This and AddPrecomputedVelocity have been a huge help which saves me writing a custom pass and more time for coffee.
     
  46. cajphrase

    cajphrase

    Joined:
    Apr 24, 2021
    Posts:
    12
    @JarkkoUnity
    Reporting a similar issue to @larsbertram1 , I have tried moving my code over from DrawMeshInstancedIndirect to RenderMeshIndirect in order to take advantage of the motion vectors but it does not seem to be working for me. It's hard to see what I could be doing wrong from the limited documentation available. This is using URP in 2021.2.
    Do you have an example scene that you could share that shows a working implementation of these new Render functions?
     
    PutridEx likes this.
  47. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    it works for me know, using hlsl or shadergraph injecting a setp function.
     
  48. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    @JarkkoUnity

    I'm trying to replace DrawMeshInstancedIndirect with RenderMeshIndirect in HDRP but I can't make it work properly with motion vectors mode set to 'Object'.

    I'm using custom cginc in the shader subgraph and set unity_ObjectToWorld and unity_WorldToObject manually.

    It generally works for RenderMeshIndirect. Meshes are rendered with correct RenderParams settings (like shadows, reflection probes mode etc.) but I have a problem with motion vectors.

    When I set motion vectors mode to 'Object' in the RenderParams and the rendered mesh is not at 0, 0 ,0 position it jitters visually (when using DLSS or TAA).

    While using motion vectors full-screen debug mode to see what is happening it's clearly visible that the mesh writes motion vectors every frame and all of them are pointing in the same direction (direction from 0, 0, 0 to this mesh).

    The cube on the attached screenshot is placed at -0.5, 0, 0. I'ts not moving and tehre is no vertex animation. Camera is static too.

    Any help will be much appreciated.

    @larsbertram1

    Have you managed to make it work with object motion vectors and shader graph shader?

     
    Last edited: Apr 8, 2022
  49. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,911
    yes. you have to set unity_MatrixPreviousM and unity_MatrixPreviousMI as well. otherwise motion vectors will be calculated based on the distance to the origin :)
     
    sqallpl likes this.
  50. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    @larsbertram1

    Thanks for the fast reply!

    I'm getting 'undeclared identifier 'unity_MatrixPreviousIM'' error.

    Anyway it looks like it works with setting only the 'unity_MatrixPreviousM'. I'm not sure.

    I've tested object motion and vertex animation too.
    Motion vectors looks correct in the debug mode when moving the mesh position (I'm setting 'unity_MatrixPreviousM' to the position from the previous frame). Same for testing vertex animation.

    I'm on Unity 2021.2.15 by the way. No idea why I can't set unity_MatrixPreviousIM and why it (probably) works without it. Maybe it's not required anymore?
     
    Last edited: Apr 8, 2022