Search Unity

HDRP Material Sorting Priority C# Scripting

Discussion in 'High Definition Render Pipeline' started by Korindian, Jan 6, 2021.

  1. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    What is the appropriate way to set HDRP Material Sorting Priority using C# scripts for transparent materials?

    This documentation page only explains setting the render priority of Mesh Renderers via scripting.

    After a simple scripting test, here's what I've discovered:

    Using the following code:

    Code (CSharp):
    1. material.SetFloat("_TransparentSortPriority", 1);
    will set only that property, but will not change the actual sorting.

    After using that code, if I select a different GameObject, and then the one with the material on it, or If I change the inspector window's mode to "Debug" and then back to "Normal", the material will "refresh", and the renderQueue will also be set to 3001, and the actual sorting will update.

    If I use the following code instead:

    Code (CSharp):
    1. material.renderQueue = 3001;
    renderQueue updates and the actual sorting does get changed.

    However, the _TransparentSortPriority doesn't update, so if I select another GameObject and then select the one with the material on it, or if I change the inspector window's debug mode and back (causing the material to "refresh"), the actual sorting AND renderQueue reverts back to the original state, because _TransparentSortPriority never changed.

    Obviously both of these are not ideal, so I'm using the following:

    Code (CSharp):
    1. public Material material;
    2. public int sortingPriority;
    3.  
    4. [ContextMenu("ApplySortingPriority")]
    5. private void ChangeSortingPriority()
    6. {
    7.     material.renderQueue = 3000 + sortingPriority;
    8.     material.SetFloat("_TransparentSortPriority", sortingPriority);
    9. }
    This seems to handle the problems of using either individually, but surely this is not what the HDRP devs intended? Will this stay the same, or will this change in future versions of HDRP?
     
    Armegalo likes this.
  2. Armegalo

    Armegalo

    Joined:
    May 14, 2018
    Posts:
    30
    Same question!
     
  3. doctorclue1024

    doctorclue1024

    Joined:
    Jul 25, 2021
    Posts:
    3
    I thinnk hdrp dosen't support render priority in 3d.
    use transparent mat = backfacecull / zdepth = priority doesn't work
    It's been a few years since the HDRP was released, and at this point, they don't seem to have the ability to correct the problem.
     
  4. UrbanNuke

    UrbanNuke

    Joined:
    Jun 11, 2019
    Posts:
    21
    Any solutions?
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Im having a beech of a time trying to get my car to render always on top of terrain details, instead of grass always coming in the car interior. Additionally, i cant seem to get my rain to stop falling 'inside' the car either. Im at the art / polish stage of my little game, and this is killing me.

    I read over the HDRP material sorting and renderer sorting, but it seems incomplete, and not all encompassing, and i cant make any sense of custom passes, so im at a loss here.

    This was really easy to accomplish in the standard rp, and probably is in HDRP too, if you know how. If anyone knows of a video, or guide, document on the subject aside from the Unity Docs, as that just isnt working out for me.

    Ty.
     
  6. KillMobil

    KillMobil

    Joined:
    Dec 3, 2013
    Posts:
    29
    Hey all i hope this helps but in urp i was able to change the sorting priority whith

    Code (CSharp):
    1. material.SetFloat("_QueueOffset", sortingPriority);
     
  7. adrien-de-tocqueville

    adrien-de-tocqueville

    Unity Technologies

    Joined:
    Mar 16, 2020
    Posts:
    270
    The sorting is handled through the render queue utlimately, so this is the property that needs to change to see any effect. But as you can see it needs to be kept in sync with _TransparentSortPriority to be displayed correctly.

    The recommended way to change any property in HDRP is to perform the validation step afterwards, what you call 'refresh'. It happens automatically when you open an inspector but to trigger it from script it would look like that:

    Code (CSharp):
    1. material.SetFloat("_TransparentSortPriority", 1);
    2. HDMaterial.ValidateMaterial(material);
    More info here: https://docs.unity3d.com/Packages/c...high-definition@16.0/manual/Material-API.html
     
    BraveAlice likes this.
  8. simonrcodrington

    simonrcodrington

    Joined:
    Aug 25, 2018
    Posts:
    8
    Just wanted to comment here, this worked perfectly for me in my URP project. I just needed some way to manually set the ordering programmatically and this worked. I couldn't find much documentation on how to change this