Search Unity

SpriteRenderer Z Sorting in 3D

Discussion in 'Shaders' started by GuitarBro, Dec 22, 2016.

  1. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Quick backstory: I'm attempting to create a character similar to those in Paper Mario: Thousand Year Door which are essentially individual sprites that overlap and are animated in a 3D world.

    My issue is that when using SpriteRenderers in 3D, sorting issues occur (due to Unity using the sprite's origin for depth checking, rather than the whole sprite using the Z buffer, from what I can tell). I attempted to solve this by modifying the Sprites-Diffuse shader to ZWrite On, but that resulted in transparency bugs as illustrated here:
    https://gfycat.com/ThatSlushyDwarfrabbit

    I'm a relative newbie when it comes to shader writing and as such I can't figure out what I need to do to get both correct sorting and no transparency issues at the same time. Any help would be appreciated.
     
  2. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
  3. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Unfortunately that didn't seem to do anything noticeable. Thanks for the suggestion though.
     
  4. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    That's odd - we're positioning 2d sprites in 3d space and don't have any sorting or transparency issues (although we're not using the default sprite shader), and I've never heard of your hypothesis that Unity sorts the entire quad mesh based on only the position of the origin. Are you using forward or deferred rendering?
     
  5. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Currently using forward rendering, though theoretically deferred should work as well. I won't be able to test it for a while unfortunately, but I'll report back if it has any effect once I do.

    Also, the origin sorting theory I got from doing a few Google searches; it may be incorrect, but it supported my observations so it seemed likely.
     
  6. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Unfortunately it appears the results are identical in both forward and deferred rendering (at least, it's consistent).

    If it helps any, here's a slowed-down recording of the current behavior in the editor (which matches what's seen in-game):
    https://gfycat.com/CoolAdvancedDonkey
     
  7. DrSkipper7

    DrSkipper7

    Joined:
    Apr 18, 2017
    Posts:
    13
    @GuitarBro Sorry for the 4 year later ping, just wondering if you found a good solution to this? Currently I'm planning on converting the sprite renderers in my project to mesh renderers, with uvs indexing a texture atlas on a shared material. Would be cool if there were a simpler solution though if you've found one.
     
  8. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Yes, actually. Use the Sorting Group component on the root gameobject, then specify the sorting layer and order in layer for each sprite which will now be relative to the sorting group.
    upload_2021-11-10_22-3-4.png
     
  9. DrSkipper7

    DrSkipper7

    Joined:
    Apr 18, 2017
    Posts:
    13
    As in you put it on a root game object for the whole scene? And that fixes Unity using the sprite origin depth checking?
     
  10. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    No the root of the object that has multiple Sprite renderers.
     
  11. DrSkipper7

    DrSkipper7

    Joined:
    Apr 18, 2017
    Posts:
    13
    Oh ok, yeah the issue I'm experiencing is specifically sorting 2d sprites in a 3d scene (that also has 3d objects), and making sure transparency works between those two types of elements. So I guess it's not quite the same issue. Thanks for the quick responses though!
     
  12. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Yeah sounds like a slightly different problem, unfortunately.