Hello Guest

Author Topic: Shader, outline and texture size  (Read 5005 times)

URZq

  • Newbie
  • *
  • Posts: 2
    • View Profile
Shader, outline and texture size
« on: January 30, 2013, 11:51:35 am »
Hi, it's my first post here. I have been using 2Dtoolkit for 4 months, and so far I really like it ;) !

So what I would like to do is to write a Shader to outline the outer edges of a sprite, like this:

Before:


After:


So far I've wrote a really simple shader effect (a blur), but I reallized that in order to have precision, I needed the texture size in pixels.

My question is: How do I get the texture size in pixels from a sprite or an animation.

Thanks in advance :)

Also, if anyone have a better idea to outline a sprite/animation edges... feel free to post :D

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Shader, outline and texture size
« Reply #1 on: January 30, 2013, 11:58:46 pm »
I'm not sure what exactly you want, but...
If you want the number of texture pixels in a sprite, you can get this from:
var def = sprite.GetCurrentSpriteDef();
width =  def.boundsData[1].x / def.texelSize.x; // you can do the same with y to get height in texels.
How big it is in screen pixels depends on a number of variables, how big your camera is, etc. An easy way to work it out is to get each corner, use the camera to get the screen space point of the extents of the sprite, and then scale this by the number above to get the size. If its being displayed 1:1, the values should be the same.


One cheeky way to get outlines for nearly free - draw an outline around your sprites, and have alpha set to 1 in those areas. Subtract 1 in alpha in normal shaders to draw your normal sprites - you can't have perfectly solid sprites, but I highly doubt you'd be able to notice an alpha of 254 on everything. Before you do that though, use the same texture on a second shader - now simply draw the alpha channel + a solid color (ignore the rgb channels on the texture). If you place this behind the other sprite, you'll have perfect outlines for nearly free..

URZq

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Shader, outline and texture size
« Reply #2 on: January 31, 2013, 02:55:30 pm »
Quote
I'm not sure what exactly you want, but...
If you want the number of texture pixels in a sprite, you can get this from:
var def = sprite.GetCurrentSpriteDef();
width =  def.boundsData[1].x / def.texelSize.x; // you can do the same with y to get height in texels.

Thanks, it's what I was looking for :)

Quote
One cheeky way to get outlines for nearly free - draw an outline around your sprites, and have alpha set to 1 in those areas. Subtract 1 in alpha in normal shaders to draw your normal sprites - you can't have perfectly solid sprites, but I highly doubt you'd be able to notice an alpha of 254 on everything. Before you do that though, use the same texture on a second shader - now simply draw the alpha channel + a solid color (ignore the rgb channels on the texture). If you place this behind the other sprite, you'll have perfect outlines for nearly free..

It's a very good idea, I'll try it :) The only problem with that is its involve more work for artists