Hello Guest

Author Topic: A few questions on static sprite batcher  (Read 7653 times)

pan_henryk

  • Newbie
  • *
  • Posts: 20
    • View Profile
A few questions on static sprite batcher
« on: March 06, 2014, 10:02:46 am »
Hi,

I have just discovered feature called static sprite batcher and am trying to test it in our project (from code, not editor)
http://www.unikronsoftware.com/2dtoolkit/doc/2.00/advanced/scripting_static_sprite_batcher.html

I have a few questions on this
1.   How to load tk2dSpriteCollectionData using Resources.Load?
My tk2dSpriteCollectionData sits in Textures directory, when moved to Resources, editor reports warning.

2.   Is sprite batching help me as compared to Unity3d static batching? In other words - is it the same or something completely different?
3.   Can I dynamically allocate size of the batcher (with list), or it has to be an array?
I want to build it on the fly, using data from xml file

Thank you in advance,

Jakub

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: A few questions on static sprite batcher
« Reply #1 on: March 06, 2014, 01:04:22 pm »
1. What warning? If its the one about including all the source sprites - its likely you've moved the wrong object - make sure its the spriteCollectionData object and not the spriteCollection object.

2. Its not the same its very different. The sprites get merged into one mesh, that means the game objects for the individual sprites that make up the batcher don't exist at all.

3. It has to be an array, but you can build it with a list and assign using ToArray().

pan_henryk

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: A few questions on static sprite batcher
« Reply #2 on: March 09, 2014, 07:28:03 pm »
Quote
1. What warning? If its the one about including all the source sprites - its likely you've moved the wrong object - make sure its the spriteCollectionData object and not the spriteCollection object.

Yes, that was it. Thanks for the tip, it helped me got it working :)

My initial tests are very promising, but I have also run into more problems:

1. How can add colliders to batched sprites, when scripting the batcher?
Building level elements from static square blocks I would like to add box collider to each sprite and then make batcher combine them for me. There fields BoxColliderExtentZ  and BoxColliderOffsetZ but I cannot figure out how to use them

2. In one case, batching a few dozen sprites resulted in doubling the draw calls needed for the scene (from 30 to 60).
Are there cases where sprite batcher could interfere with dynamic batching?

Thanks for your help!



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: A few questions on static sprite batcher
« Reply #3 on: March 10, 2014, 11:07:49 am »
1. Add a collider in the sprite collection, that should be it. BoxColliderExtentZ and BoxColliderOffsetZ are only relevant for 3d colliders. Offset = 0 ,extent = 0.01 will work OK, 0,0will kinda work too - you should see the colliders, but they'll be flat.

2. Check the docs about dynamic batching (https://docs.unity3d.com/Documentation/Manual/DrawCallBatching.html) it stops batching once vertex attributes exceed a certain count. What will likely happen is the batcher will stop batching with everything else, but that shouldn't be an issue. If you're seeing weird draw call jumps, try pressing play and stop - i've seen that counter return random numbers in more than one occasion.

pan_henryk

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: A few questions on static sprite batcher
« Reply #4 on: March 11, 2014, 04:41:11 pm »
1. Add a collider in the sprite collection, that should be it. BoxColliderExtentZ and BoxColliderOffsetZ are only relevant for 3d colliders. Offset = 0 ,extent = 0.01 will work OK, 0,0will kinda work too - you should see the colliders, but they'll be flat.

That's where I am getting lost. When I would create it in editor, I understand commiting the batcher will extract colliders from game objects and merge them into mesh collider.
But when I do it from code, your sample looks like this:

Code: [Select]
for (int i = 0; i < batcher.batchedSprites.Length; ++i) {
            tk2dBatchedSprite bs = new tk2dBatchedSprite();

            // assign sprite collection and sprite Id for this batched sprite
            bs.spriteCollection = spriteCollection;
            bs.spriteId = spriteCollection.GetSpriteIdByName("crate");

            Vector3 pos = new Vector3((i - batcher.batchedSprites.Length / 2) * 0.1f, Random.value * 0.2f, 0);

            // Assign the relative matrix. Use this in place of bs.position
            bs.relativeMatrix.SetTRS(pos, Quaternion.identity, Vector3.one);

            batcher.batchedSprites[i] = bs;
        }

what I would need would be something like this:

Code: [Select]
for (int i = 0; i < batcher.batchedSprites.Length; ++i) {
            tk2dBatchedSprite bs = new tk2dBatchedSprite();

            // assign sprite collection and sprite Id for this batched sprite
            bs.spriteCollection = spriteCollection;
            bs.spriteId = spriteCollection.GetSpriteIdByName("crate");

            //specify a collider for sprite
            bs.collider = new BoxCollider();

            Vector3 pos = new Vector3((i - batcher.batchedSprites.Length / 2) * 0.1f, Random.value * 0.2f, 0);

            // Assign the relative matrix. Use this in place of bs.position
            bs.relativeMatrix.SetTRS(pos, Quaternion.identity, Vector3.one);

            batcher.batchedSprites[i] = bs;
        }

or possibility to create colliders based on sprite rectangle.

Thanks again,

Jakub
« Last Edit: March 11, 2014, 04:46:24 pm by pan_henryk »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: A few questions on static sprite batcher
« Reply #5 on: March 12, 2014, 11:32:58 am »
The box collider is created based on the sprite. If the sprite has a colliider set up in the collection, the batcher's commit function will create one. The Offset and extents are solely to store the z offset and extents of the box collider. Even if they were just set to zero you should see a flat box collider at that location.

Basically, if you create the sprite collection in the editor, and a collider gets created the same should happen when you create it in code.

pan_henryk

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: A few questions on static sprite batcher
« Reply #6 on: March 13, 2014, 05:57:20 pm »
The box collider is created based on the sprite. If the sprite has a colliider set up in the collection, the batcher's commit function will create one. The Offset and extents are solely to store the z offset and extents of the box collider. Even if they were just set to zero you should see a flat box collider at that location.

I see. We are using external colliders (to contain all the info in prefabs) and I forgot about build in collider functionality for tk2d.
Now I wonder if I could add colliders to sprite collection from script, converting from data I have in prefabs?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: A few questions on static sprite batcher
« Reply #7 on: March 13, 2014, 10:35:01 pm »
You could - the data is accessible, but it will be fairly involved and you will have to wade through undocumented innards of 2D Toolkit.
tk2dSpriteCollectionDefinition.colliderType, boxColliderMin & boxColliderMax are the ones which are of particular relevance here.

pan_henryk

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: A few questions on static sprite batcher
« Reply #8 on: April 04, 2014, 02:58:00 pm »
It took some time, but I got it working!
We have just send our latest update to AppStore, with serious fps and load time optimization thanks to batcher use.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: A few questions on static sprite batcher
« Reply #9 on: April 06, 2014, 12:13:50 pm »
Awesome :)