HDRP Material Sorting Priority C# Scripting

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:

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:

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:

public Material material;
public int sortingPriority;

[ContextMenu("ApplySortingPriority")]
private void ChangeSortingPriority()
{
    material.renderQueue = 3000 + sortingPriority;
    material.SetFloat("_TransparentSortPriority", sortingPriority);
}

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?

1 Like

Same question!

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.

Any solutions?

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.

Hey all i hope this helps but in urp i was able to change the sorting priority whith

material.SetFloat("_QueueOffset", sortingPriority);

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:

material.SetFloat("_TransparentSortPriority", 1);
HDMaterial.ValidateMaterial(material);

More info here: https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@16.0/manual/Material-API.html

1 Like

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