SFML community forums

Help => Graphics => Topic started by: oomek on July 09, 2018, 12:33:43 am

Title: [SOLVED] Cheap antialiased sprite edges
Post by: oomek 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 );
}
Title: Re: Shape::setTextureRect(const FloatRect& rect)
Post by: oomek 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 )