2D Toolkit Forum

2D Toolkit => Support => Topic started by: pixelpatron on June 21, 2013, 01:25:03 am

Title: Sprite batcher sorting?
Post by: pixelpatron on June 21, 2013, 01:25:03 am
Using the Sprite batcher I'm working on a isometric game view. I have misc sprites setup in my 3D viewport yet upon "commit" of the Sprite batcher the sorting is incorrect based on what I see during my game view editing.

How would I sort these misc sprite layers so they always draw in a particular order once I commit the batch to be put on the sheet?
Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on June 21, 2013, 10:59:33 am
Did you try that sorting snippet I sent you? That should make it sort based on the camera that's seeing it - it will be consistent before and after committing.
Title: Re: Sprite batcher sorting?
Post by: pixelpatron on June 21, 2013, 04:33:54 pm
You referring to this?

"You could probably insert a custom sorting order in the sprite collection sort function if you like........it will be consistent before and after committing."

Sounds great! How the heck do you do that? I'm about a week into this program so unless it's very basic your gonna loose me.

Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on June 21, 2013, 04:45:57 pm
No, looks like the reply is still stuck in the outbox for some reason, and refuses to send.

Code: [Select]
tk2dStaticSpriteBatcherEditor.cs, line 20, change it to:
allTransforms = (from t in allTransforms orderby Camera.main.WorldToScreenPoint((t.renderer != null) ? t.renderer.bounds.center : t.position).z descending select t).ToArray();

This will sort the sprites to the main camera in your scene.
Title: Re: Sprite batcher sorting?
Post by: pixelpatron on June 21, 2013, 05:39:21 pm
Works like a champ! THANK YOU!!!!!!!!  :D ;D :) ::)
Title: Re: Sprite batcher sorting?
Post by: pixelpatron on July 11, 2013, 06:24:32 pm
Unfortunately this is no longer sorting correctly.  :( >:(

Even with the line of code added.

Nearest I can tell it's having issues since it separated into two separate atlases when my sprite collection became large. Not sure if that is the issue or not, but only thing that looks different between one Batcher that looks correct when I commit compared to one that doesn't.

I guess the only other difference to one that works is that the level I'm using this batcher set with is a prefab. Does sprite batcher lose it's sorting when converted to a prefab? (Even with the fix?)

HELP!
Title: Re: Sprite batcher sorting?
Post by: rewindgamestudio on December 11, 2013, 01:34:16 pm
We've also just come across this issue.

We're batching sprites from the same atlas but batched sprites from other atlases are not rendering in the correct order.

We're using Unity 3.x
Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on December 11, 2013, 10:29:12 pm
I don't think that is the same issue. What exactly is in your batcher / scene, and what are the z coordinates on them?
Title: Re: Sprite batcher sorting?
Post by: rewindgamestudio on December 11, 2013, 10:35:25 pm
Hi,

Our trees are all batched up together.

We noticed that if we switch from the LitBlendVertexColor shader to the LitCutoutVertexColor shader the sorting works, but it obviously looks terrible!

Should the z value be the same on all batches sprites? We're using 2D sprites in a 3D scene with a perspective camera set to orthographic mode.
Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on December 11, 2013, 10:41:14 pm
Sprite batcher only sorts within itself. Normal sprites can't sort INTO a static batcher, its just not possible. In this case you should probably split it up into foreground and bg sprites.
Title: Re: Sprite batcher sorting?
Post by: rewindgamestudio on December 11, 2013, 10:46:41 pm
Ah, ok. Can you tell us why the cutout shader works though?
Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on December 11, 2013, 10:47:29 pm
Cutout shader writes to depth, it doesn't need to be sorted.
Title: Re: Sprite batcher sorting?
Post by: rewindgamestudio on December 11, 2013, 10:54:51 pm
Ok, thanks for the info.

Just one last oddity(slightly off from the original point ).

If a SpriteBatcher sorts within itself, how come we see this blending?
Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on December 11, 2013, 10:55:42 pm
Just a guess, but perhaps your tree is slightly transparent? It certainly doesn't look like its drawing on top of the sprite.
Title: Re: Sprite batcher sorting?
Post by: rewindgamestudio on December 11, 2013, 10:59:52 pm
No, it's solid.

This is what we get if we un-batch those sprites, using the same shader.

Title: Re: Sprite batcher sorting?
Post by: unikronsoftware on December 11, 2013, 11:04:39 pm
Out of ideas. The batcher doesn't do anything special, just merges data. If you can create a repro case I'll be happy to look. support at unikronsoftware dot com.