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

Author Topic: TextureRect sometimes rendered wrong  (Read 2148 times)

0 Members and 3 Guests are viewing this topic.

Lodea

  • Newbie
  • *
  • Posts: 4
    • View Profile
TextureRect sometimes rendered wrong
« on: June 04, 2022, 05:05:52 pm »

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.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: TextureRect sometimes rendered wrong
« Reply #1 on: June 04, 2022, 05:27:28 pm »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Lodea

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: TextureRect sometimes rendered wrong
« Reply #2 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..

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: TextureRect sometimes rendered wrong
« Reply #3 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).



Lodea

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: TextureRect sometimes rendered wrong
« Reply #4 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?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: TextureRect sometimes rendered wrong
« Reply #5 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.