SFML community forums

Help => Graphics => Topic started by: Lodea on June 04, 2022, 05:05:52 pm

Title: TextureRect sometimes rendered wrong
Post by: Lodea on June 04, 2022, 05:05:52 pm
(https://i.imgur.com/xxAw9e2.png)
I have this problem that once in a while the TextureRect I'm trying to display is rendered with artifacts from the neighboring pixels. (as can be seen in the top-left corner of the sprite)
I checked and the TextureRect itself only includes the relevant pixels.
The sprite is scaled in the x axis by a factor of -1 (to make it horizontally flipped) and rendered on a 2x zoomed in View, maybe it's related for some reason, idk.
Thanks for the help in advance.
Title: Re: TextureRect sometimes rendered wrong
Post by: Stauricus on June 04, 2022, 05:27:28 pm
is the texture set to smooth? if it is, try switching it off
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Texture.php#a0c3bd6825b9a99714f10d44179d74324
Title: Re: TextureRect sometimes rendered wrong
Post by: Lodea on June 06, 2022, 03:02:15 pm
is the texture set to smooth? if it is, try switching it off
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Texture.php#a0c3bd6825b9a99714f10d44179d74324
I tried switching all the smooth textures off but it's still doing that..
Title: Re: TextureRect sometimes rendered wrong
Post by: kojack on June 06, 2022, 04:37:42 pm
This can happen if your sprite isn't placed at integer pixel coordinates, pixels outside of the texture rectangle can bleed over due to floating point errors.

There's other fun that can happen such as mip maps for non-power of 2 texture regions, but that's probably not happening here (you'd need to shrink the sprite for it to happen).


Title: Re: TextureRect sometimes rendered wrong
Post by: Lodea on June 06, 2022, 09:59:50 pm
This can happen if your sprite isn't placed at integer pixel coordinates, pixels outside of the texture rectangle can bleed over due to floating point errors.

There's other fun that can happen such as mip maps for non-power of 2 texture regions, but that's probably not happening here (you'd need to shrink the sprite for it to happen).

Thanks, I tried flooring the position and it seems to work perfectly so far. Though I would like to have a non-int position. Is there a way to fix this without sacrificing rounding the sprite's position?
Title: Re: TextureRect sometimes rendered wrong
Post by: kojack on June 06, 2022, 10:46:27 pm
One method I've seen done is add padding around each sprite in a sprite sheet with a repeat of the edge pixels, so if they bleed over they have the same colour (or transparency) as what they bleed onto, so there's no effect.

Other engines (like Unity) can have this happen too.
https://answers.unity.com/questions/1566057/unity-2d-sprite-bleeding-glitch.html
https://stackoverflow.com/questions/15023158/how-to-prevent-pixel-bleeding-from-rendering-sprite-sheet-generated-with-zwoptex

Or a custom pixel shader should be able to force sampling from the texture rectangle.