Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Specify sortingLayer/order in Graphics.DrawMesh?

Discussion in 'General Graphics' started by vexe, Nov 11, 2019.

  1. vexe


    May 18, 2013
    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:

    Code (csharp):
    2. 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:

    Code (csharp):
    2. Shader "TinyTitanStudios/XDebugShader"
    3. {
    4.     SubShader
    5.     {
    6.         Tags
    7.         {
    8.             "Queue"="Overlay"
    9.         }
    11.         Cull Off
    12.         Lighting Off
    13.         ZWrite ON
    14.         ZTest Always
    15.         Blend SrcAlpha OneMinusSrcAlpha
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma target 2.0
    24.             #include "UnityCG.cginc"
    25.             #include "UnityUI.cginc"
    27.             struct VertexInput
    28.             {
    29.                 float4 Vertex : POSITION;
    30.                 float4 Color : COLOR;
    31.             };
    33.             struct VertexOutput
    34.             {
    35.                 float4 Vertex : SV_POSITION;
    36.                 fixed4 Color : COLOR;
    37.             };
    39.             VertexOutput vert(VertexInput IN)
    40.             {
    41.                 VertexOutput OUT;
    43.                 OUT.Vertex = UnityObjectToClipPos(IN.Vertex);
    44.                 OUT.Color = IN.Color;
    46.                 return(OUT);
    47.             }
    49.             fixed4 frag(VertexOutput IN) : SV_Target
    50.             {
    51.                 fixed4 OUT = IN.Color;
    53.                 return(OUT);
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    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!

    Attached Files:

  2. Kronnect


    Nov 16, 2014
    Unfortunately DrawMesh always draws to sortingLayer 0 and sortingOrder 0. There's no way to specify those parameters AFAIK.
  3. ZenTeapot


    Oct 19, 2014
    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.
    Last edited: Sep 1, 2021
    kxgamedev, viruseg and Kronnect like this.