Hello Guest

Author Topic: Sprite batcher sorting?  (Read 9619 times)

pixelpatron

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprite batcher sorting?
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #1 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.

pixelpatron

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sprite batcher sorting?
« Reply #2 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.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #3 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.

pixelpatron

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sprite batcher sorting?
« Reply #4 on: June 21, 2013, 05:39:21 pm »
Works like a champ! THANK YOU!!!!!!!!  :D ;D :) ::)

pixelpatron

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sprite batcher sorting?
« Reply #5 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!
« Last Edit: July 11, 2013, 10:14:11 pm by pixelpatron »

rewindgamestudio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite batcher sorting?
« Reply #6 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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #7 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?

rewindgamestudio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite batcher sorting?
« Reply #8 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #9 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.

rewindgamestudio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite batcher sorting?
« Reply #10 on: December 11, 2013, 10:46:41 pm »
Ah, ok. Can you tell us why the cutout shader works though?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #11 on: December 11, 2013, 10:47:29 pm »
Cutout shader writes to depth, it doesn't need to be sorted.

rewindgamestudio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite batcher sorting?
« Reply #12 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite batcher sorting?
« Reply #13 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.

rewindgamestudio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite batcher sorting?
« Reply #14 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.