2D Toolkit Forum

2D Toolkit => Support => Topic started by: lvictorino on February 21, 2014, 01:16:51 pm

Title: Extract only sprite pixels of an Atlas
Post by: lvictorino 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.
Title: Re: Extract only sprite pixels of an Atlas
Post by: unikronsoftware 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.
Title: Re: Extract only sprite pixels of an Atlas
Post by: lvictorino 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 :
Title: Re: Extract only sprite pixels of an Atlas
Post by: unikronsoftware 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.
Title: Re: Extract only sprite pixels of an Atlas
Post by: lvictorino 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!