Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Cheap antialiased sprite edges  (Read 1893 times)

0 Members and 1 Guest are viewing this topic.

oomek

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
    • Email
[SOLVED] Cheap antialiased sprite edges
« on: July 09, 2018, 12:33:43 am »
Is there any chance to rewrite setTextureRect() function to accept FloatRect?
I have written a simple shader that adds a cheap antialiasing to the sprite edges. Before running the shader I extend the sprite size by 1 pixel on each side and pass those dimensions to the shader, but what I also need is to adjust the texture uv to shrink by 1 pixel, but in screen resolution, not texture resolution. setTextureRect() is quantised to texture resolution, so if the sprite is stretched the border size I added does not match the modified texture uvs.

Here is the AA shader

Code: [Select]
uniform sampler2D texture;
uniform vec2 size; // sprite size, not texture size

void main()
{
    vec2 coords = gl_TexCoord[0].xy;
    float aa_edge = length( max( abs( coords * size - size / 2.0 ) - ( size / 2.0 - vec2( 1.5, 1.5 )), 0.0 ));
    vec4 pixel = texture2D( texture, gl_TexCoord[0].xy );
    gl_FragColor = gl_Color * pixel;
    gl_FragColor.w = gl_Color.w * ( 1.0 - aa_edge );
}
« Last Edit: July 09, 2018, 02:10:36 am by oomek »

oomek

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
    • Email
Re: Shape::setTextureRect(const FloatRect& rect)
« Reply #1 on: July 09, 2018, 01:45:07 am »
Nevermind, it turned out that I can easily do the same in the shader.

Code: [Select]
uniform sampler2D texture;
uniform vec2 imgSize;

void main()
{
vec2 coords = gl_TexCoord[0].xy;
vec2 uv = ( coords - 0.5 ) * ( imgSize + 2.0 ) / imgSize + vec2( 0.5, 0.5 );
float aa_edge = length( max( abs( coords * imgSize - imgSize / 2.0 ) - ( imgSize / 2.0 - vec2( 1.5, 1.5 )), 0.0 ));
vec4 pixel = texture2D( texture, uv );
gl_FragColor = gl_Color * pixel;
gl_FragColor.w = gl_Color.w * ( 1.0 - aa_edge );
}

Just remember if anyone wants to use it you need to enlarge your sprite size by 2 pixels and adjust the positionto by ( -1, -1 )
« Last Edit: July 09, 2018, 02:09:35 am by oomek »

 

anything