Hello Guest

Author Topic: Perfect gap with tk2dStaticSpriteBatcher  (Read 5496 times)

Zakwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Zakstudio
Perfect gap with tk2dStaticSpriteBatcher
« on: May 05, 2013, 08:50:15 pm »
Hello,

I try to use the tk2dStaticSpriteBatcher. But I've got a problem with the gap between the sprites composing the Static Sprite Batcher.

First I'm using a unity camera to keep the perspective view (add 3d model & background).

I want to create a background with tiled sprites and use the StaticSpriteBatcher to avoid to create many gameobject.

I succeed to display 4 by 4 tiles but the gap is a problem, position or scale I think...

How can I get the right sprite height & width ?

Code: [Select]
GameObject spriteBatcherGO = new GameObject("BGSpriteBatcher");
tk2dStaticSpriteBatcher staticSB = spriteBatcherGO.AddComponent<tk2dStaticSpriteBatcher>();

staticSB.spriteCollection = collectionLevel;
spriteBatcherGO.transform.localPosition = new Vector3(1, 1, 1);
spriteBatcherGO.transform.localScale = new Vector3(1f,1f,1f);

tk2dBatchedSprite[] batchedSpriteList = new tk2dBatchedSprite[4 * 4];
int index = 0;
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();
batchedSprite.spriteId = (i % 2 == 0)?1:0;// black or white tile
batchedSprite.localScale = new Vector3(5.5f,5.5f,5.5f);
// 5.5f is a manual and not perfect number

batchedSpriteList[index] = batchedSprite;
++index;
}
}

staticSB.batchedSprites = batchedSpriteList;

staticSB.Build();
staticSB.transform.localScale = new Vector3(50f,50f,1f);

Edit : I tried with 2 cameras : one 2dtk (ortho) and a unity (perspective), worked fine in the editor, but the 2dtk camera didn't display anything on the built exe.
« Last Edit: May 05, 2013, 09:10:25 pm by Zakwil »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Perfect gap with tk2dStaticSpriteBatcher
« Reply #1 on: May 05, 2013, 10:16:16 pm »
1. Why is there a seemingly random scale there?

2. Normally you can evaluate exactly the extrema of the sprite by doing:
Code: [Select]
tk2dSprite sprite;
var bounds = sprite.GetUntrimmedBounds();
target.position = sprite.transform.position + new Vector3(bounds.center.x + bounds.extents.x, 0, 0); // this gets you the midle right extent but I'm sure you can work out the rest.

But since you don't have the sprite object you'll have to dig the bounds data out of the sprite definition
Code: [Select]
var sprite = collectionInst.spriteDefinitions[_spriteId];
Bounds bounds = new Bounds( Vector3.Scale(sprite.untrimmedBoundsData[0], localScale), Vector3.Scale(sprite.untrimmedBoundsData[1], localScale) );

Zakwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Zakstudio
Re: Perfect gap with tk2dStaticSpriteBatcher
« Reply #2 on: May 06, 2013, 07:19:25 am »
1. I just test with these numbers, but the dimensions of the sprite were a little bit random, I confess

2 .I'll check with this function

3. The idea having 2 cameras is "normal" or not ?
« Last Edit: May 06, 2013, 07:23:08 am by Zakwil »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Perfect gap with tk2dStaticSpriteBatcher
« Reply #3 on: May 06, 2013, 11:04:59 am »
You can have as many cameras as you like / need. Just remember that Unity will not batch across cameras, and also remember to set up depth on the camera, and visibility layers properly :)

Zakwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Zakstudio
Re: Perfect gap with tk2dStaticSpriteBatcher
« Reply #4 on: May 11, 2013, 04:48:37 pm »
I succeed to get the bounds data into the sprite definition.

But I have to set the Filter Mode to Point to avoid gap between the sprites (from PNG non transparent).

If I try Bilinear the gap disapear but when I zoom the sprite is not smothed. Any chance to have smothed zoom and no gap?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile

Zakwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Zakstudio
Re: Perfect gap with tk2dStaticSpriteBatcher
« Reply #6 on: May 11, 2013, 07:27:37 pm »
Thanks, working perfectly.