Hello Guest

Author Topic: Get an array of Sprite pixel colors?  (Read 6530 times)

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Get an array of Sprite pixel colors?
« on: March 12, 2015, 07:31:49 pm »
Hi,

I was just wondering if there is a way I can get an array of the pixels of a tk2dSprite, whether the array is 2D or just one long flattened array. I don't need to modify the colors, but I'd like to write some effects for my game that, for instance,  instantiates a particle of the average color of each 2x2 block of pixels or something in order to do vaporization or teleportation effects or something. Can I do this?

Thanks
« Last Edit: March 17, 2015, 02:55:58 am by dogboydog »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get an array of Sprite pixel colors?
« Reply #1 on: March 12, 2015, 11:31:11 pm »
No it isn't possible without a fair bit of work. If you wanted to do something like this the most effective thing would be to modify the tk2d building routines to precalculate and save these values together in the sprite collection data. Since you don't really need all the pixels in a sprite (you can discard empty / clear pixels too...) you can get a pretty effective results like this.

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #2 on: March 13, 2015, 01:45:16 am »
Hmm okay. I might look into that but not sure I'll be able to pull that off. Thanks!

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #3 on: March 17, 2015, 02:49:52 am »
Hi,

So I have a naive attempt at doing this, but I was wondering if you could give me some guidance on where to put these changes.

I have a Color[][,] added to tk2dSpriteCollection.cs (should it be in tk2dSpriteCollectionData?) called pixelColorMaps

Then in Rebuild() in tk2dSpriteCollectionBuilder I have this written out to  try to populate those color maps.
http://pastebin.com/UiEeAPgp

I'm guessing referring to and changing currentBuild is not what I'm supposed to be doing, but I feel like if I can define the Color[][,] variable in the right place and put the above code in the right place, and modify the correct instance I might have what I need
« Last Edit: March 17, 2015, 02:56:40 am by dogboydog »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get an array of Sprite pixel colors?
« Reply #4 on: March 17, 2015, 02:39:55 pm »
The pixel color map wants to probably be per sprite (put into tk2dSpriteDefinition). You can fill up the structure after rebuild is complete. You also don't need to store a [,], a flat array[] will suffice. Not sure if a multi dimensional array will serialise properly in unity. Still seems to be missing a lot more data - you need to find the images for each texture (there may be more than one) using SpriteLut...

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #5 on: March 17, 2015, 05:04:56 pm »
Okay, I will add a flattened array into tk2dSpriteDefinition.

The [,] was more for my own convenience so that I would have to do the modular/int division for every pixel when accessing it.  So for each texture2d there will potentially be more than one image? I will have to look at SpriteLut  to see what's going on with that.

So would this code go at the end of Rebuild(), after all the existing code? And how would I access and overwrite the pixel color map for each sprite from the Rebuild() method, like is there a list of sprite definitions that I should modify, and am I right to be changing values of currentBuild?

Thanks a lot.

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #6 on: March 18, 2015, 06:07:00 pm »
I've looked around some more but don't totally understand how SpriteLut is working/which data structures if any correspond 1:1 to sprites.

So here's my thoughts/questions:

Each texture in the textureList can have one or more SpriteLut objects referring to it.  The SpriteLuts have the rx, ry (origin of upper left of an image?) and rw, rh (width and height away from that origin?)  in case one image like a sprite sheet is split into multiple sprites.  Even if I loop through each Texture2D in the textureList, how will I know which sprite they correspond to by name or spriteId?

Once I know how to get which sprites are related to which textures in the textureList, it shouldn't be too hard to make a new flattened array from parts of the GetPixels() return value,  and then as long as I put the array itself in the right place I should be able to access it in my scripts.
« Last Edit: March 18, 2015, 07:51:15 pm by dogboydog »

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #7 on: March 19, 2015, 12:41:45 am »
I had another try at this, but still need some direction I think.

http://pastebin.com/6qZi4Un7

When you have the time, please help me correct this to how it should be working. I think the logic should be down for getting the color data for a subsection of an image of the same or larger size, but still confused about the issues above (pretty sure I'm getting the wrong larger image and don't know how to line up spriteIds with this data I'm grabbing). I currently have this after the "save changes" section
« Last Edit: March 19, 2015, 12:45:17 am by dogboydog »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get an array of Sprite pixel colors?
« Reply #8 on: March 21, 2015, 11:29:42 pm »
SpriteLut.sourceTex is the source texture that is going to end up in the atlas.
If you look in UpdateVertexCache, you can see the relationship between the lut and atlas. You could just convert the pixels into your texture stuff right there.

dogboydog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • My website
Re: Get an array of Sprite pixel colors?
« Reply #9 on: March 22, 2015, 06:37:12 am »
Thanks a lot. I'll check that out. Sorry for all the questions.