Custom sprite shader not overlaying existing default sprites

I have need of a custom shader. I’m starting with just a duplicate of the default sprite shader from the official source code archive: http://unity3d.com/get-unity/download/archive

This builds fine, but the sprite drawn with this shader is always behind sprites drawn with the default shader. I’ve tried changing sorting value, z pos, and sorting layer, but the sprite is always behind others. The sorting values only adjust sprites drawn with this shader.

How do I move this shader to always above default sprites?

Shader "Sprites/TricolourSpriteShader"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile _ PIXELSNAP_ON
            #include "UnityCG.cginc"
          
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                half2 texcoord  : TEXCOORD0;
            };
          
            fixed4 _Color;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            sampler2D _MainTex;
            sampler2D _AlphaTex;
            float _AlphaSplitEnabled;

            fixed4 SampleSpriteTexture (float2 uv)
            {
                fixed4 color = tex2D (_MainTex, uv);
                if (_AlphaSplitEnabled)
                    color.a = tex2D (_AlphaTex, uv).r;

                return color;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
                c.rgb *= c.a;
                return c;
            }
        ENDCG
        }
    }
}

In order for the sprite’s distance from the camera to matter, you need to write out the depths of each pixel, which you’ve disabled with the “ZWrite Off” command.

If a quick and easy solution would be to make this sprite draw on top of everything that renders before it, add “ZTest Off”. Note that this will NOT prevent things to draw on top of it if they are drawn after this sprite in the render queue.

(“ZTest Off” means it doesn’t check if it’s in front or behind other objects… it just draws)

The above code is the Unity default sprite shader. It doesn’t need Z testing because it’s drawn on top of everything (or behind, depending on draw pass). The sprite sorting value determines draw order.

What’s strange here is the same shader code has a different response when in a custom material rather than the default sprite material.

Okay, seems it’s a bug. If I change the material’s shader back to default sprite and then my custom sprite, it works using layer ordering value.

Does that solve your problem?

I had this problem with Unity 5.2.1f1 and 5.2.1p3 (the latest patch release). And yes, changing the shader on my material to Sprites/Default and back to my custom shader fixed the issue. Looking at the diff for the .mat files, toggling the shader changed the render queue from 2000 (“Geometry”) to 3000 (“Transparent”) as specified in my shader. So yes, definitely a Unity bug, but it’s an easy fix.

Yes. Filed a bug report.