Specify sortingLayer/order in Graphics.DrawMesh?

Hi all,

So I’m writing a 2d debug primitives renderer for runtime debug viewing (Lines, circles, quads etc)

I can’t figure out for the life of me how to make it render on top of everything else in the scene.

I generate a mesh dynamically and create a material for it that uses a debug shader.

This is the rendering call:

Graphics.DrawMesh(ShapesMesh, Matrix4x4.identity, ShapesMaterial, layer: RENDER_LAYER);

I tried setting the “Queue” to “Overlay” in the shader, setting ZWrite ON and ZTest Always, etc. No matter what I do, there’s always things in the scene that render on top of my mesh. Those things are MeshRenderers that have a higher than “Default” sorting layer (Foreground) (generated from TilemapEditor plugin). I can’t see anyway to access sortingLayers from the Graphics.Draw* calls. Here’s my debug shader:

Shader "TinyTitanStudios/XDebugShader"
{
    SubShader
    {
        Tags
        {
            "Queue"="Overlay"
        }

        Cull Off
        Lighting Off
        ZWrite ON
        ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            struct VertexInput
            {
                float4 Vertex : POSITION;
                float4 Color : COLOR;
            };

            struct VertexOutput
            {
                float4 Vertex : SV_POSITION;
                fixed4 Color : COLOR;
            };

            VertexOutput vert(VertexInput IN)
            {
                VertexOutput OUT;

                OUT.Vertex = UnityObjectToClipPos(IN.Vertex);
                OUT.Color = IN.Color;

                return(OUT);
            }

            fixed4 frag(VertexOutput IN) : SV_Target
            {
                fixed4 OUT = IN.Color; 

                return(OUT);
            }
            ENDCG
        }
    }
}

In the mean time I can’t see any way to do this without creating a new GameObject with a MeshRenderer attached with its sortingLayer set to something higher than Foreground.

Attached image shows the debug lines rendering underneath the trees.

Thanks for any tips!

5163281--512036--LAYER.png

1 Like

Unfortunately DrawMesh always draws to sortingLayer 0 and sortingOrder 0. There’s no way to specify those parameters AFAIK.

Unity really need to make this work. This is an important feature for developing working renderer for Unity. For example I want to implement a variant of the SpriteRenderer to allow more custom data per vertex, and there is simply no easy way to do this for something this basic, because

  1. SpriteRenderer quickly descends into native code and has no interface for managing custom sprite mesh.
  2. There is no way to inherit or extend a renderer. I can’t inherit from Renderer and add it as a component.
  3. Graphics.DrawMesh doesn’t work with Sorting Layer. Sorting layer is tied to renderer per instance, and without the renderer nothing works with the rest of Unity.
  4. I ended up writing a companion SpriteExtendedRenderer to manage the states of a MeshRenderer to simulate the behavior of an extended SpriteRenderer, except the sorting point option which seems exclusive to SpriteRenderer. Annoying as hell and extremely cumbersome. I would say something this simple requires this much of hoop jumping in 2021 is not very acceptable.
4 Likes