Hello Guest

Author Topic: Is Sprite sorting in perspective mode broken?  (Read 7098 times)

Tarks

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Is Sprite sorting in perspective mode broken?
« on: May 19, 2013, 04:50:54 pm »


I'm trying to figure out why my circle sprite is displaying infront of the body sprites but not the head despite clearly being infront of it. If I move it a little further forward then it pops infornt, but still appears behind it at certain angles :(

I've tried having the sprites on the same spriteCollection, changing the transparency sorting mode of the camera etc but no dice. I'm sure there's some clever batching or something going on here as if I just slapped a texture onto a mesh it'd have no trouble displaying correctly, but I can't get the desired behaviour.

I also notice that when I'm animating the sprites position, it pops in front of and then back behind the texture:



Any help is appreciated,
Az

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is Sprite sorting in perspective mode broken?
« Reply #1 on: May 19, 2013, 09:20:22 pm »
There isn't any clever batching going on - just what Unity normally does for dynamically batched sprites. The sorting is performed based on the origin of the sprite, and the distance of that to the camera.

Presumably moving the white ring closer to the camera "fixes" the sorting?
There are a few ways of "working around" this issue, to trick Unity into sorting it in a different order. Eg. using a shader which outputs a slightly closer z value to what is actually stored in the geometry, modifying the bounding box so that Unity thinks the origin is closer to the camera (i.e. in front of the actual physical geometry), etc.

Thing is though, from what I can see your sprites look like they have a thick outline - if you can get away with alpha test, then you will not have to deal with ANY of that.

ruffenman

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is Sprite sorting in perspective mode broken?
« Reply #2 on: June 06, 2013, 09:48:55 pm »
I am also seeing this issue in a similar setup. Forgive me for missing something obvious, but I was wondering, what exactly did you mean by "get away with alpha test"?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is Sprite sorting in perspective mode broken?
« Reply #3 on: June 06, 2013, 09:56:56 pm »
If your sprites don't have a feathered edge, then you can switch to an alpha test shader. If you can use an alpha test shader (test it out by switching to tk2d/CutoutVertexColor). If that works, then you've got yourself a solution there. If not you'll have to resort to other solutions.

I'm guessing you've got camera.transparencySortMode set to orthographic already? http://docs.unity3d.com/Documentation/ScriptReference/Camera-transparencySortMode.html


ruffenman

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is Sprite sorting in perspective mode broken?
« Reply #4 on: June 06, 2013, 09:59:50 pm »
Aye. T'was the first thing I tried based on some other posts I saw. I'll have to give this a try later. Thanks! Are there issues with alpha testing shaders and iOS or Android?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is Sprite sorting in perspective mode broken?
« Reply #5 on: June 06, 2013, 10:56:09 pm »
Yes, alpha testing is slowish on powervr hardware (which iOS devices and some Android devices use). But, thats not to say you can't use it. If it works, it works :) And if it does, you have room for optimization. 2D toolkit lets you create geometry and use a solid shader instead. But first, you need to work out if alpha test is suitable....