Hello Guest

Author Topic: Trying to read sprite pixel data from an atlas  (Read 3642 times)

Tiarny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Trying to read sprite pixel data from an atlas
« on: August 14, 2013, 05:53:25 pm »
I'm currently using the 2DToolkit for all my character animations and sprites, but I have created an "explosion" system which causes the characters to break into their individual pixels upon certain game over states. To make it more dynamic I want to be able to iterate through each pixel of a sprite and find its colour, mapping it to the individual "pixel" that will then explode. This way, with a bit of smoke and mirrors in hiding the actual sprite, it looks like the actual character sprite is exploding apart.

My current thinking is that the best way to do this is to simply ready directly from the sprite / atlas in some way, but I'm not sure the best way to go about it. I was looking through the documentation but I got a bit muddled, so I was hoping someone might be able to point me in the right direction.

I know directly reading is probably not the most efficient way of doing things, but the character atlases are only 128*128 (256*256 in some cases) and each individual sprite is only about 30*80 so it seems like a better way of doing it than trying to manually build "pixels" for every possible sprite each character has.

So, basically - A dynamic way of getting the colours for the current sprite for the current character from the atlas, so I can apply those same colours to an explosion system!

Thanks.  :D

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Trying to read sprite pixel data from an atlas
« Reply #1 on: August 14, 2013, 07:59:03 pm »
You should probably be OK if you are aware that making a texture readable at least doubles the memory footprint at runtime.

Once you have that, you can get the min and max UVs by looking at:
sprite.CurrentSprite.uvs
Find the min and max of all the UVs and simply read out that UV rect from the texture - in your case you don't care about the orientation of it, so that makes it simpler.

you can map UV to GetTexture(x, y) by multiplying by the width / height.
int x = (int)(uv.x * texture.width);

Tiarny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trying to read sprite pixel data from an atlas
« Reply #2 on: August 16, 2013, 09:37:22 am »
That sounds perfect! Thanks.

Only a handful of character sprites are impacted by this system, so I'll make sure to only enable readable textures for those cases to prevent the memory footprint from bloating unnecessarily.