Hello Guest

Author Topic: Extract only sprite pixels of an Atlas  (Read 4636 times)

lvictorino

  • Newbie
  • *
  • Posts: 7
    • View Profile
Extract only sprite pixels of an Atlas
« on: February 21, 2014, 01:16:51 pm »
Hi,

I'd like to extract only the pixel informations used by the current sprite.
The idea would be to analyze colors used by a sprite, and loop through its pixels.
Unfortunately renderer.material.mainTexture returns only the atlas (1024x1024) for my 27x27 texture. GetPixels an iterating through all pixels of the atlas could be overkill. Is there a bettre way?

Thank you.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Extract only sprite pixels of an Atlas
« Reply #1 on: February 22, 2014, 12:38:25 pm »
You can get it in a roundabout way by deriving using the UVs on the sprite.
sprite.CurrentSprite.uvs[0] and [3] should contain the uvs used by the sprite - you will need to scale this by the atlas dimensions to get the subrect in pixels which you can then look up in your mainTexture.

The source image isn't available at that point.

lvictorino

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Extract only sprite pixels of an Atlas
« Reply #2 on: February 22, 2014, 06:08:28 pm »
Thanks for the answer.
I've tried the uvs workaround before seeing your post... however the results I had in sprite definitions were "weird". My atlas is 1024x1024, my sprite is 27x27 and uvs given are :
  • (0.0, 0.9)
  • [1] (0.0, 0.9)
    [2] (0.0, 1.0)
    [3] (0.0, 1.0)

    That's why I didn't work too hard on that.
    Anyway, do you plan to have that kind of feature in the next release? It could be super-handy to have that.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Extract only sprite pixels of an Atlas
« Reply #3 on: February 22, 2014, 10:38:38 pm »
The uv thing isn't a workaround - the sprites don't store the source textures and there isn't any plan to change that. It really would defeat the purpose of having an atlasing system to keep the source images around. As mentioned before you just need to multiply x with mainTexture.width and uv.y with mainTexture.height to get the coordinates.

lvictorino

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Extract only sprite pixels of an Atlas
« Reply #4 on: February 22, 2014, 11:50:11 pm »
Sorry for my broken english, workaround wasn't the wisest word to use.
Anyway thank you very much for you help!