Depth Sorting Problem (Pics + Code)

I have a strange problem I am unable to diagnose. The sorting of my scene objects gets messed up based on the material used.

In this image, you can see the middle tile overlaps the ball (which should be in front):

In this image, when I change the material of the tile, it draws as expected:

I am using 2 materials, but they both have the exact same shader applied. Here is the shader code:

 Shader "Unlit/Unlit RGB x Tex"
 {
    Properties {
       _MainTex ("Texture", 2D) = "white" {}
       _Color ("Color", Color) = (1,1,1)
    }
    SubShader {
       Lighting Off
       cull off
       ztest always
       Zwrite on
       Tags {"Queue" = "Geometry" }
       Pass {
          SetTexture [_MainTex] {
             constantColor [_Color]
             Combine texture * constant, texture * constant
          }
       }
    }
 }

Both materials are set up identically, with only the textures being different:

Neither texture has an alpha map. Both are the same dimensions. Both have the same import settings, and filter settings. They are identical in format, only differing in the pixel values.

I’ve even tried forcing the RenderQueue via a script, setting all Tiles to renderQueue 1010 and the player ball to renderQueue 1020. Exactly the same result.

Now while the texture is the only thing that differs between these two materials, if I change the texture of the problem material, it STILL draws in front. If I change the material on the object, it draws correctly.

Any suggestions?

This has to be the 20th time I’ve done this… Straight after posting, I thought “Just delete the damn material and try again”, which I did and it fixed the problem.

EDIT: Urgh, so it didn’t fix the problem. Or rather, it just created a new one. So totally recreating the material from scratch seemed to suddenly draw the tiles BEHIND the player as expected. But then I noticed that the player was drawing in front of ALL of the tiles, regardless of the player’s position (so if the player fell behind the tiles, it was still draw in front of them).

So, I recreated the Player material, and it reintroduced exactly the same problem.

I’m going to assume this is all a problem with my shader?

EDIT: Changed the shader on all material to the default Diffuse and depth sorting works as expected. Time to debug my shader…

Probably ought to be on the transparent queue.

That fixed it, thanks Hippo.

Well, the problem is the ‘ztest always’. This will cause it to always be drawn regardless of the z depth. Most likely you want to do ‘ztest lequal’ which means that pixels behind other pixels aren’t drawn.

The reason why putting it in the transparent queue works, is because the sorting is then done on the cpu and the objects are drawn in order from back to front. This works, but adds an extra performance overhead.