Search Unity

Transparent sprites have incorrect depth sorting from one direction

Discussion in 'General Graphics' started by Navarth, Jun 16, 2020.

  1. Navarth

    Navarth

    Joined:
    Dec 17, 2017
    Posts:
    21
    Hey everyone, I'm working on a 2.5D game that uses 2D sprites for characters and small objects, and 3D meshes for geometry. I have a perspective camera that rotates around the map at a locked downward angle.

    In the process of setting up my scenes, I've noticed that transparent sprites in Unity have their backfaces drawn (Cull Off) and seem to sort depth correctly from the front, but when viewed from behind their sorting is reversed, drawing the furthest sprite from the camera on top.

    You can test this out yourself simply by dropping a few Sprite objects in a scene and assigning them a texture with transparency, then lining them up front to back and rotating the camera around. Here's an example using the default sprite shader and some sprites from Disgaea:



    Is there something I can set in my custom shaders to correct this behaviour? I've tried switching to a cutout shader, ZTest options, disabling and enabling batching, and of course using other shaders - but so far no dice.

    I can't assume the sprites will always face the camera even with a billboarding shader, because they'll still invert when I flip them with the animator toggle to face different directions. Is there some way I can simply set all instances of this shader to render in order of their distance from the camera at all times?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  3. Navarth

    Navarth

    Joined:
    Dec 17, 2017
    Posts:
    21
    Ah, you're absolutely right! For some reason my scene was set to use custom axis transparency and I never noticed. Using default the sprites sort by distance to the camera, and everything behaves as expected.

    Thanks so much for your help on this and the billboarding shader - my project's looking a thousand times simpler to develop for because of it!