Hello Guest

Author Topic: Region on texture that sprite is extracted from.  (Read 3360 times)

siroosec

  • Newbie
  • *
  • Posts: 14
    • View Profile
Region on texture that sprite is extracted from.
« on: January 13, 2014, 10:40:36 am »
Hi!

I'm trying to write a shader based on normal sprite shader, but with addition of another texture to use its alpha to cut out the sprite.
I think it would be quite easy if we know where on texture has the sprite been extracted from. Using a simple math formula in shader, we can calculate uvs on alpha texture using uvs on main texture. But I have 2 questions :

a) Do all sprites have a rectangular extract region on atlas? If not, is there any easy way to get some equivalent?
b) How can I get that region in my sprite class? Since I don't want to keep a reference to collectionData in sprite just to pass its region to the shader.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Region on texture that sprite is extracted from.
« Reply #1 on: January 13, 2014, 11:05:40 am »
a. Yes and no - sprites may not be rectangular, eg. if you use custom shapes. Currently the actual uv region will still be rectangular, though. If you turn on dicing, the UVs will be all over the texture, a rectangle for each dice. It may be rotated / flipped as well so keep that in mind - tk2d 2.3 allows you to disable rotation, but this will obviously result in less efficient packing.

b. sprite.CurrentSprite.uvs is an array of the UVs for the texture. You can use this to get the data you need. tk2dSpriteGeomGen.GetClippedSpriteGeomDesc is commented quite comprehensively, should help work out where you can extract the data from directly instead of iterating over all the uvs and finding min/max. To pass it to your shader I strongly recommend using MaterialPropertyBlocks (Renderer.SetPropertyBlock)