2D Toolkit Forum

2D Toolkit => Support => Topic started by: robinouu on February 03, 2013, 06:02:55 pm

Title: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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 ?
(http://img11.hostingpics.net/pics/943521gapissue.png)

Thanks for your support.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware 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.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware 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.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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 !
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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 ?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware 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.

Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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 :
(http://img15.hostingpics.net/pics/802991skinmanual.png)

using tk2dSpriteCollectionData.CreateFromTexture :
(http://img15.hostingpics.net/pics/911900skinwww.png)

I could not find any solution. I first though that there was an issue with the PNG alpha channel, but it is really not.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on February 04, 2013, 07:43:04 pm
Are mipmaps on with the texture2D?
Also, this:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,612.msg5221.html#new
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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.
(http://img15.hostingpics.net/thumbs/mini_627783gaps.png) (http://www.hostingpics.net/viewer.php?id=627783gaps.png)

What can I do to prevent it ?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 13, 2014, 10:49:13 pm
Is it a perspective / ortho camera?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 14, 2014, 11:51:20 am
Try adding one pixel of padding in the sprite collection editor (extend, not blackzeroalpha), might help.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu 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 ?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 14, 2014, 01:08:41 pm
No the padding is performed during the atlasing stage, you'll need to make sure your source images are dilated appropriately.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 14, 2014, 05:50:10 pm
Hum... How do I do it ?
If I understand correctly, I have to make an extra padding of 1pixel between each sprite in my custom atlas ?

(Seems to be an issue with Unity3D, not specifically tk2d)
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 14, 2014, 08:10:57 pm
This is not exactly an issue with unity either, its just what you'd do with tiled gpu textures. You can do it in photoshop, or just import your sprite collection into tk2d.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 14, 2014, 08:33:09 pm
I have to do it by code, because we use a custom editor for tile mapping and creating atlases :P
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 14, 2014, 09:49:02 pm
I added the feature by code but gaps are still here...
I need to add the texture dynamically, I can't assign it by the tk2d spritesheet editor

Some ideas ?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 14, 2014, 11:32:59 pm
If you need to add it dynamically you'll need to make sure it is there in the source texture itself - you want to do that at asset author time, reading and writing textures is a slow operation you dont want to be doing while the game is loading / running.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 15, 2014, 09:05:10 am
I implemented your solution at author time (in our custom editor).

With 1 pixel padding between sprites :
(http://img4.hostingpics.net/thumbs/mini_274792zonenomade.png) (http://www.hostingpics.net/viewer.php?id=274792zonenomade.png)

The spritesheets are then loaded into Unity3D, but the gaps subsists.
What am I doing wrong ?

Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 15, 2014, 11:32:28 am
Extend padding duplicates the last pixel, I'm guessing the hole you see is the transparent bits there but I could be wrong. Hard to guess whats going on here, post a repro case and I can take a look.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 21, 2014, 08:46:45 pm
I still have gaps with float coordinates.
What is the algorithm for duplicating the last pixel, I don't understand.

If I can't find out, I will reproduce the problem with a minimal code and post it here.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 21, 2014, 09:52:00 pm
Here is the repro case : REMOVED LINK
Use the horizontal or vertical axes to move around.

Thank you for your assistance.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 22, 2014, 11:22:58 am
You have an actual gap in your sprites, not sure how thats happened. Eg. if you zoom in a lot you'll see the gap. I cant see your source image, but it looks like you've not duplicated the pixel but instead added a black border? All you want to do is copy the last pixel into the gap.
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: robinouu on November 22, 2014, 11:49:00 am
I did not add a black border, the source image is the attached image in the above post.
I implemented your solution at author time (in our custom editor).

With 1 pixel padding between sprites :
(http://img4.hostingpics.net/thumbs/mini_274792zonenomade.png) (http://www.hostingpics.net/viewer.php?id=274792zonenomade.png)

The spritesheets are then loaded into Unity3D, but the gaps subsists.
What am I doing wrong ?



i added transparent borders between sprites, but now i have to fill them the same color as the last pixel of the sprite ?
Title: Re: Gaps within tk2dStaticSpriteBatcher
Post by: unikronsoftware on November 22, 2014, 02:51:10 pm
Quote
Extend padding duplicates the last pixel, I'm guessing the hole you see is the transparent bits there but I could be wrong.
You're seeing through into the background. Change the bg color and you can see that is the case.

You need to perform the equivalent of extend padding in tk2d, which is to copy the edge pixels, it should not be transparent. Copy the pixels into the empty space and it should fix the issue.
Title: Re: Gaps on screen
Post by: robinouu on November 23, 2014, 03:19:02 am
It's finally working =) Copying the edge pixels is the right way to do.

Thanks for your help!