SpriteRenderer flip causes misaligned pixels

I am experiencing a major issue where at certain position, when flipped, pixels will be renderered at the wrong location. I am using the SpriteRenderer.flipX variable.

I have made certain that the 2 objects are at the exact same position.
This happens when the screen resolution is 480x270, 960x540 and 1440x810. However no issue happens at 1920x1080.

See image attached (note that the size of the sprite, 16x16, is the entire area outlined in grey, but that area is transparent):

Top left: isolated sprite normally
Bottom left: isolated sprite after flipping
Top right: overlaying the 2 sprites
Bottom right: overlaying, but inner sprite flipped

The issue becomes pretty clear in that bottom right case.

This happens when the x position is 123.8438 for example. No issue occurs on something like 123.5. That is to say when (x % 1 = .8438).

Is there something I can add to my shader code to account for this? Or maybe a setting in Unity?

I should point out that while I could lock the position to match up with my game’s native 480p120, it would mean losing the feature of nearest-neighbour/point interpolation for higher, integeral multiple resolutions.

I can’t just do (1 - uv) in my shader either, since I am using sprite atlases.

Would love to hear of a solution if there is one out there.
Any suggetions are also welcome.

I thought perhaps this was due to a floating point rounding error and I did not catch it happening without that, but further testing reveals the issue does occur at .5 too!

I was able to capture this behaviour occuring at multiples of 0.0625! :rage: :face_with_spiral_eyes:

Would love if a Unity dev could look into this. Or really just anybody who might be able to help.

I should mention I am using Unity 2019.4.1f1

Another update while I wait for help:

I disabled atlas and wrote a UV flipping function into my shader. Unfortunatly even when directly manipulating the UV coordinates, I still have the same issue. :frowning:

As a bonus, I also tried setting the scale to -1. Same result.

The bad news: no help has come and all I have done so far is bump my own thread with new info.

The good news: I found a solution of creating a flipped sprite for each and every sprite.

While this obviously means having 4x the amount of texture data for every single sprite, I can make it work by using an array. I check the current texture and use bitwise operators to grab the flipped version.

Incase someone finds this an also wishes to use the same solution, here is a code example:

public class OrthoSpider : MonoBehaviour
{
private SpriteRenderer rend = null;
public Sprite[ ] Sprites;

void Awake() {
rend = gameObject.GetComponent<SpriteRenderer>().sprite;
}

void OnEnable() {
FlipSprite(rend.sprite, Sprites);
}

void FlipSprite(Sprite sprite, Sprite[ ] sprites) {
int index = FindMatchingObjectInArray(sprite, sprites);
sprite = sprites[index ^ 0b1];
}
}

int FindMatchingObjectInArray(object obj, object[ ] objs) {
for (int i = 0; i < objs.Length; i++) {
if (obj == objs*) {*
*return i;*
*}*
*}*
*return -1;*
*}*
*}*
*```*
*You can then fill up the array with your sprites as normal and flipped.*