Hello Guest

Author Topic: Gaps within tk2dStaticSpriteBatcher  (Read 17908 times)

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Gaps within tk2dStaticSpriteBatcher
« on: February 03, 2013, 06:02:55 pm »
Hi !

I have gap issues when trying to fill a tk2dStaticSpriteBatcher with batched sprites (30x30 pixels).
I am using a custom sprite collection data (needed for dynamic resource loading)

PART 1 : Get the sprite collection data
Code: [Select]
Texture2D texture = tilesetPost.texture; // from WWW

int i = 0, nbSprites = (texture.width / 30) * (texture.height / 30);
Rect[] regions = new Rect[nbSprites];
Vector2[] anchors = new Vector2[nbSprites];
string[] names = new string[nbSprites];

Vector2 anchor = new Vector2(0, 0);
for (int y = 0; y < texture.height; y += 30)
{
for (int x = 0; x < texture.width; x += 30)
{
regions[i] = new Rect(x, y, 30, 30);
anchors[i] = anchor;
names[i] = (x/30) + "_" + (y/30);
++i;
}
}

tk2dRuntime.SpriteCollectionSize size = tk2dRuntime.SpriteCollectionSize.ForTk2dCamera();
tk2dSpriteCollectionData spriteData = tk2dSpriteCollectionData.CreateFromTexture(texture, size, names, regions, anchors);

PART 2 : the batcher
Code: [Select]
GameObject batcherObject = new GameObject("Batcher");
tk2dStaticSpriteBatcher batcher = batcherObject.AddComponent<tk2dStaticSpriteBatcher>();
batcher.spriteCollection = spriteData;

for (int y = 0; y < srcH; ++y)
{
for (int x = 0; x < srcW; ++x)
{
tk2dBatchedSprite sprite = new tk2dBatchedSprite();
sprite.spriteId = sprData.GetSpriteIdByName(x + "_" + y);

sprite.position = new Vector3(destX, destY, 0);
sprites.Add(sprite);
}
}

batcher.batchedSprites = sprites.ToArray();
batcher.Build();


And, as I am getting the collection data from code, i did not found any way to set the Pad option to Extend.
Is there any way to remove these gaps ?


Thanks for your support.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #1 on: February 03, 2013, 06:05:34 pm »
If you do it like that at runtime, your texture won't have any padding and there isn't any way to add it at runtime - it would cost too much, and the code just isn't there.

If you set filtering to point on the texture, you'll get a lot less of those lines... it might even get rid of it completely.

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #2 on: February 03, 2013, 06:29:55 pm »
Quote
If you do it like that at runtime, your texture won't have any padding and there isn't any way to add it at runtime - it would cost too much, and the code just isn't there.
Yes, you are true.

Quote
If you set filtering to point on the texture, you'll get a lot less of those lines... it might even get rid of it completely.
Hum... not really. There are still many artifacts.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #3 on: February 03, 2013, 06:43:22 pm »
Your best bet is to manually add padding to the texture if you can.

If you can send me a repro case to support@unikronsoftware.com I could take a look and see if there is anything that can be done, but its almost impossible to say without actually seeing specific issue.

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #4 on: February 03, 2013, 07:58:43 pm »
Oh my mistake. I was using 0.00x coordinates to move the camera. Everything is OK with 0.0x floats and texture.FilterMode to Point.

Thanks for your assistance !

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #5 on: February 04, 2013, 01:42:41 pm »
I noticed also that my texture is badly rendered using the above code (with point filter mode), there are some strange semi-white borders around the palm tree, as you can see
I tried the same with the basic workflow (manually adding the texture to a sprite collection) and they disappear...

Is this related to the fact I am loading the texture with WWW ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #6 on: February 04, 2013, 02:04:54 pm »
Are you loading it into a compressed texture?
Its totally possible to do it using www, but you need to ensure your texture is correct before doing anything else.


robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #7 on: February 04, 2013, 06:10:28 pm »
So, i have made a few test, without using the WWW method, but just the same original Texture2D (NPOT/RGBA 32-bits/120x60/PNG/filter point) :

using the tk2d editor :


using tk2dSpriteCollectionData.CreateFromTexture :


I could not find any solution. I first though that there was an issue with the PNG alpha channel, but it is really not.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #8 on: February 04, 2013, 07:43:04 pm »

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #9 on: February 04, 2013, 10:44:51 pm »
I dont' know why but using power of two texture did the trick...
I have checked all the texture importer yet, but Unity3D is apparently performing (and forcing) some kind of interpolation internally. 

Very strange.
« Last Edit: February 04, 2013, 10:48:10 pm by robinouu »

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #10 on: November 13, 2014, 10:44:26 pm »
I could not find a way to solve this gaps problem.
I though it was related to the player position, but even with int positions i have these gaps.

The sprite collections are generated dynamically from texture.


What can I do to prevent it ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #11 on: November 13, 2014, 10:49:13 pm »
Is it a perspective / ortho camera?

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #12 on: November 14, 2014, 11:36:59 am »
We use the orthographic projection.
I tried to change the generated texture to Point, and to modify camera override autoscale, without success

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #13 on: November 14, 2014, 11:51:20 am »
Try adding one pixel of padding in the sprite collection editor (extend, not blackzeroalpha), might help.

robinouu

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Gaps within tk2dStaticSpriteBatcher
« Reply #14 on: November 14, 2014, 12:04:14 pm »
We do not use the sprite collection editor, because we nedd dynamically loaded textures (using tk2dSpriteCollectionData.CreateFromTexture)
Is it possible to do this kind of padding by code ?