Search Unity

HDRP Visual Effect Renderer Priority via C# Scripting

Discussion in 'Visual Effect Graph' started by Korindian, Jan 9, 2021.

  1. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    How do you access the Visual Effect component's Renderer section to be able to set Priority via scripting, rather than just through the inspector?
     
  2. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Is accessing the Priority via script even possible? Putting the inspector in debug mode doesn't show the variables for the renderer section exists in the component itself. Also using Visual Studio Intellisense for the Visual Effect component doesn't show any public options to set it either.

    Also, is the Priority in the Visual Effect component related to material render queue sorting, or is it related to Mesh Renderer sorting priority for transparent objects? This page in the docs isn't clear which one, but I'm assuming transparent material render queue?:
    https://docs.unity3d.com/Packages/com.unity.visualeffectgraph@10.2/manual/VisualEffectComponent.html

    But if so, it's not working correctly. I filed a bug on this the other day: 1305134

    How does one set a Visual Effect material render queue to appear behind an HDRP transparent material that has its material transparent sort priority set to -5, for example?
     
    Last edited: Jan 13, 2021
  3. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
  4. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Hi @Korindian ,

    Apologies for the delay, @PaulDemeulenaere kindly looked into it and you can access the renderer priority via script like this:

    Code (CSharp):
    1.    
    2.     var renderer = gameObject.GetComponent<Renderer>(); //VFXRenderer inherits from Renderer
    3.     if (renderer != null)
    4.     {
    5.         renderer.rendererPriority = 15;
    6.     }
    VFX renderer priority, sorting, etc. are not very well explored at this stage, so if you run into any issues, please let us know or log a bug.
     
  5. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Thank you so much for responding. As mentioned in the second post, I did log a bug 2 weeks ago with a simple easy-to-reproduce project, but no one has taken a look at it yet: #1305134
     
  6. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    QA got back to me, and said that sorting by material is not supported yet... however, after more testing, I found that it can be done, at least in version HDRP 10.2.2.

    The rendererPriority in post #4 above is the same as the "Priority" property in the Visual Effect component's inspector under the Renderer section. This property refers to the MeshRenderer's priority as shown on this page in the docs under the section "Sorting By Priority", NOT the material's sorting priority.

    The VFX Graph material's sorting priority can currently only be set by scripting, using the method @VladVNeykov posted above to get the renderer. I can confirm that this also works with VFX Shader Graph shaders:

    Code (CSharp):
    1. private int sortPriority = -10; // Or whatever HDRP sortPriority you want to set it to.
    2.  
    3. var renderer = vfxGameObject.GetComponent<Renderer>(); //VFXRenderer inherits from Renderer
    4. if (renderer != null)
    5. {
    6.     renderer.sharedMaterial.renderQueue = 3000 + sortPriority;
    7.     renderer.sharedMaterial.SetFloat("_TransparentSortPriority", sortPriority);
    8. }

    The sortPriority's value is from -100 to 100 in HDRP according to this page in the docs under "Sorting by Material".

    Note that both the material renderQueue and float property "_TransparentSortPriority" must be set as one can get reset while refreshing the material's inspector if the other is not set also, for example when switching the inspector debug mode, or expanding/collapsing the material inspector. At least this is the case with regular HDRP materials, not sure about VFX Graph shaders, but it's there for completeness.

    It would be great if either this could be added to the docs, or if this functionality could be added to the Visual Effect component's inspector, since sorting with HDRP shaders already works.
     
    Last edited: Jan 26, 2021
    Marou1 likes this.
  7. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Hi @Korindian ,

    Apologies, missed the part where you mentioned the bug, but glad you received a reply. So yes, in a nutshell, sorting still needs more attention on the VFX side as it needs to be address in a way which works for both HDRP and URP.

    Some examples:
    - sorting by render queue, i.e. Opaque/Transparent (supported in VFX/HDRP/URP)
    - sorting by render queue offset, i.e. Transparent + n (supported in HDRP/URP, not yet exposed/officially supported by VFX. You've added your own queue offset, and can even implement it in your outputs like mentioned on this forum post, but we still need to look into the level it should be exposed (per output and/or per system), ensure it's robust for all scenarios (e.g. what if the render queue contradicts the vfx output sorting order), etc.
    - sorting by renderer priority (works in HDRP, don't think URP has it).

    TL;DR: it's planned :)
     
    MCLiving88 and Korindian like this.
  8. MCLiving88

    MCLiving88

    Joined:
    Aug 26, 2020
    Posts:
    7
    Bump for sorting by mesh renderer priority similar to HDRP for URP
     
  9. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Please feel free to submit it as an idea request with some info on your use case so we prioritize this better compared to other requests.
     
  10. AndrewStyan

    AndrewStyan

    Joined:
    Apr 6, 2020
    Posts:
    14
    Am I reading this correctly (not up with all the terminology yet) and is this what you are referring to @MCLiving88 ?
    I want to render a VFX Graph in front of an object with transparency in URP (URP/Lit with Metallic+Transparent). See the images below.

    With the earth spherical object set to Opaque I get the correct rendering as seen (blue VFX surrounding earth globe).
    upload_2021-6-30_13-9-36.png

    If I set the earth object to Transparent, so that I can fade it in and out selectively, I only get VFX that is not hidden by the earth object. Post #4 makes no difference. Haven't tried Post #6.

    Screen Shot 2021-06-30 at 1.14.28 pm.png
    Thanks!
     

    Attached Files: