Hello Guest

Author Topic: General question regarding static sprite batching  (Read 7490 times)

lambdamu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
General question regarding static sprite batching
« on: February 08, 2013, 05:05:26 am »
Hi all!

I have been reading the documentation about static sprite batching, and I admit I don't really have much background knowledge about rendering. I have several questions regarding the use of sprite batching:

1. Can static sprite batches be created programatically?
2. For the static sprite batch, does it mean I can't do anything to the individual sprites inside the batch in the game scene while it is running? Like to move the position of the sprites or animate the sprite?
3. Imagine a case where I have to create a background, whose size is determined at runtime, by tiling a single texture. If I were to do this programmatically, will sprite batching be faster than creating via prefab instantiating?
4. In general, what kind of scenarios can static sprite batching be applied to?

Thanks in advance for any kind response!
« Last Edit: February 08, 2013, 05:18:41 am by lambdamu »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: General question regarding static sprite batching
« Reply #1 on: February 08, 2013, 03:04:00 pm »
1. Yes, but its undocumented (and could possibly change in the future) - Check this link for more info http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1064.msg5083.html#msg5083

2. Yup. Its like static batching in Unity, but you get a bit more control over it, and its more efficient, as there aren't as many objects to process, everything is folded into the one static sprite batcher.

3. Yes, with the sprite batcher, only one of them (multiplied by the number of associated components...) exists. Undoubtedly will be faster than multiple instantiation.

4. I think its most useful for backgrounds, but could be used for other things too - creating fake tiled surfaces, for instance.

lambdamu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: General question regarding static sprite batching
« Reply #2 on: February 13, 2013, 03:57:28 am »
Thanks unikron, your post has been very helpful!

Just an additional question: Must all the sprites in the sprite batcher come from the same sprite collection?

--edit--
I realized yes, for each sprite batcher the sprite must be of the same collection. However if I want to extend beyond a single sprite collection I can create multiple sprite batchers, have I understood this portion correctly?

--edit2--
I've managed to get the basic static sprite batcher working, but just to clarify, can I change the colour (like have some sort of change in colour with respect to time) of the batched sprites while the game is running?

--edit3--
I have run some tests so far and results are a little... weird. It seems performance is better in terms of rendering time without the sprite batcher? Is this expected due to the savings tradeoff with VBO? Or could it be I have been doing something wrong...?

Without static sprite batcher:
Graphics: 89.1 FPS (11.2ms)
Draw Calls: 2 batched: 14
Tris: 44 Verts:92
Used Textures 2 - 192.3 KB
Render Textures: 0 - 0B switches: 0
Screen: 320x480 - 1.8 MB
VRAM usage: 1.8 MB to 2.1 MB (of 256.0 MB)
VBO Total: 236 - 171.1 KB
Shadow Casters: 0
Visible Skinned Meshes: 0 Animations:0

With static sprite batcher:
Graphics: 86.5 FPS (11.6ms)
Draw Calls: 2 batched: 14
Tris: 44 Verts: 92
Used Textures: 2 - 192.3 KB
Render Textures: 0 - 0 B switches: 0
Screen: 320 x 480 - 1.8 MB
VRAM usage: 1.8 MB to 2.1 MB (of 256.0 MB)
VBO Total: 211 - 137.6 KB
Shadow Casters: 0
Visible Skinned Meshes: 0 Animations: 0

I have screenshots but I don't know how to upload them here...
« Last Edit: February 13, 2013, 05:25:30 am by lambdamu »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: General question regarding static sprite batching
« Reply #3 on: February 13, 2013, 10:54:36 am »
You can't change the color on a static sprite batcher, it uses the built in colors of the individual sprites.

You haven't really got enough sprites to worry about performance with static sprite batchers - you'd have to go into hundreds of sprites before you'd see any gain (or slow down from having many gameobjects).

Secondly, your batch count isn't changing - if you were merging a few sprites into a static sprite batcher, I'd expect those numbers to change after you've committed the static sprite batcher.


lambdamu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: General question regarding static sprite batching
« Reply #4 on: February 14, 2013, 02:41:53 am »
I don't really understand what do you mean by commit, since I'm creating the static sprite batch programmatically. I did call the build function though. Just in case, here's the snippet of my code in question:

Code: [Select]
tk2dSpriteCollectionData scd = (tk2dSpriteCollectionData) Resources.Load("BGCollection", typeof(tk2dSpriteCollectionData));
GameObject spriteBatcherGO = new GameObject("BGSpriteBatcher");
tk2dStaticSpriteBatcher staticSB = spriteBatcherGO.AddComponent<tk2dStaticSpriteBatcher>();
staticSB.spriteCollection = scd;
spriteBatcherGO.transform.parent = transform;

tk2dBatchedSprite[] batchedSpriteList = new tk2dBatchedSprite[noOfRows * noPerRow];
int index = 0;
for (int i = 0; i < noOfRows; ++i)
{
currentHeight = startHeight + i * spriteHeight;
for (int j = 0; j < noPerRow; ++j)
{
tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();
batchedSprite.spriteId = 0;
batchedSprite.position =  new Vector3((i % 2 == 0)?widthArray[j]:widthOddArray[j], currentHeight, tempZ);
batchedSprite.color = _color;
batchedSpriteList[index] = batchedSprite;
++index;
}
}

staticSB.batchedSprites = batchedSpriteList;
staticSB.Build();

I've done a quick check and there is no commit function for tk2dStaticSpriteBatcher... or am I looking in the wrong place?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: General question regarding static sprite batching
« Reply #5 on: February 14, 2013, 09:34:49 am »
Oh you've created it in code? That's fine then. Ignore the commit, calling build does the same thing as clicking "commit" in the inspector.