Hello Guest

Author Topic: Speed and Sprite Meshes  (Read 9777 times)

TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Speed and Sprite Meshes
« on: February 04, 2014, 05:33:39 pm »
Hi,

I am relatively new to 2D toolkit. I have been using it to build a game for mobile devices so I have been trying to squeeze as much performance out as possible.

Right now I am using a bunch of additive shaded sprites (own custom shader), so basically my sprites have no alpha, but black areas act as transparent. I was wondering if there is a way to get 2Dtk to create a custom polygonal mesh for the sprites so that I can save time on rendering portions of the sprite.

From my tests, the native Unity sprites perform 20% faster than 2Dtk sprites, using rectangular meshes. I am using 2Dtk for the atlasing, but I would love to have the same performance as with the Unity sprites. I also have Uni2D and it did render faster when using polygonal meshes in another game.

If this is not possible, are there any suggestions for speeding up rendering to the max?

(I am probably using all the tricks already but I ask just in case I am missing some)

Thank you in advance!
« Last Edit: February 05, 2014, 12:44:00 am by TheDreamMaster »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Speed and Sprite Meshes
« Reply #1 on: February 05, 2014, 10:38:38 am »
There are 2 features which can probably help here:
1. Mesh shapes for sprites (http://2dtoolkit.com/docs/latest/advanced/sprite_optimizations/sprite_optimizations.html)
2. Sprite dicing, which is useful for backgrounds and larger areas - http://2dtoolkit.com/docs/latest/advanced/sprite_dicing.html You stand to make significant gains with this if it will work with your sprites (i.e. predominantly large solid areas)


TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #2 on: February 08, 2014, 08:44:45 am »
Hi thank you for your reply.

Sprite dicing is probably not going to help me, I am dealing with small animated sprites, not large areas.

As for mesh shapes, that's exactly what I was asking for, but I could not find a way to get the shape done automatically (like in Unity sprites or Uni2D). Doing them by hand is no use, as I have more than 300 frames of animation.

Is there a way to automate the mesh generation? Or if at least there was a way to copy the mesh of the first frame in the first sprite of the animation to the rest of the frames.

My game is about armies, so I have lots of army units for the player and for the enemy. Besides the mesh optimization which could help deal with the fill rate bottleneck, I am also experiencing problems with batching.

For some reason, batching will work fine as long as I don't change the transform.position of the sprites. But the army units have to move :) As soon as I call transform.position = x, batching stops working, and the draw calls ramp up from 30 to 250.

Any ideas?
« Last Edit: February 08, 2014, 09:00:21 am by TheDreamMaster »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Speed and Sprite Meshes
« Reply #3 on: February 08, 2014, 11:50:22 am »
No automatic mesh generation, I'm afraid and theres no motivation to implement that now that Unity has it built in - waiting for them to expose the necessary bits so it can be added as a feature. You can however multiselect sprites, modify the first one and "Apply" the changes to the rest of them. The little apply button is what transfers the data to the rest of the selection, so don't forget to click that.

About batching - how many sprite collections are you using and are you using a perspective camera (do you have transparency sorting mode set to orthographic)?

TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #4 on: February 08, 2014, 06:37:33 pm »
Oh the multi-selection works great! I am also thinking that a fine diced mesh will also work. So far so good, making progress :)

Batching... I have 3 sprite collections: General stuff, player army, enemy army.

I am using a perspective main camera, the battlefront is viewed at an angle from above, while the army units are flat on the XY plane. I am not using transparency, my materials are additive (transparent materials have slower performance). I have 3 cameras and all 3 are visible at the same time on the screen:

1)Main camera, perspective. Shows the battlefield. Clear flags= solid color.
2)tk2dCamera, perspective, with Sort mode= perspective. Displays the UI and HUD on top of the battlefield. Clear flags=Depth only.
3)Minimap camera, orthographic. Shows minimap in the corner. Clear flags = Depth only. Only certain objects render to this camera (army units but no special effects or GUI).

Should I be using tk2DCameras for all of them?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Speed and Sprite Meshes
« Reply #5 on: February 08, 2014, 10:30:07 pm »
Fine diced mesh for small sprites would be a bad idea, it would add more overhead than necessary.

If your materials are additive they can be drawn in any order. Try using Unity sorting layers to group everything from one collection together. That way the draw order will be deterministic and not dependent on distance to camera.

I wouldn't bother using tk2dCameras here, it won't add anything.

TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #6 on: February 09, 2014, 04:57:49 am »
I have been searching and looking for a while now and cannot figure out where you set the sorting layers for sprite collections.

Any hints? :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Speed and Sprite Meshes
« Reply #7 on: February 09, 2014, 11:49:32 am »
You need to manage that yourself and set it on each sprite. You'll probably need to write a manager to manager it all.

TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #8 on: February 09, 2014, 06:28:03 pm »
Ah you mean via code. I thought it was exposed somewhere.

That's no problem. I will let you know how that goes. I still can't get batching to work when the sprites move. (I just don't understand why moving them breaks it).

Thank you so much for the help so far!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Speed and Sprite Meshes
« Reply #9 on: February 09, 2014, 06:36:17 pm »
Unity batches by distance to camera. Moving the sprite with a perspective camera will change sort bucketing. By putting all sprites from one collection into one sorting order number it won't attempt to sort there. You control the order in which things are drawn that way.

TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #10 on: February 10, 2014, 05:20:31 am »
That makes sense, I didn't know that was the case.

Well, I tried:

renderer.sortingLayerName = collectionId.ToString();
renderer.sortingOrder = collectionId;

and it doesnt seem to be doing anything :(
 


TheDreamMaster

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Speed and Sprite Meshes
« Reply #11 on: February 10, 2014, 06:01:01 am »
Ok I got a solution.

After the sortingOrder did nothing, a friend suggested trying to remove the depth check in the shader. That did it. I edited my additive shaders and now it all batches perfectly. My draw calls are now as low as 14 :)

Thank you unikron for all the support on this matter, I have learned a lot from all this.