Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Wisteso on April 30, 2014, 01:13:27 AM

Title: Possible bug with UISprite.MakePixelPerfect() + 0.5 Pixel Size
Post by: Wisteso on April 30, 2014, 01:13:27 AM
Let me start backwards...

It seems that lines 467 and 468 in UISprite.cs (which look like "if ((x & 1) == 1) ++x;" and "if ((x & y) == 1) ++y;") are sometimes causing the behavior that the logic exists to prevent. In this case, I am using a pixel size of 0.5 and sprites with dimensions that are multiples of 2 (and should not require the extra padded pixel).

The atlas entry is 178x206 original size, so with the atlas at 0.5 pixels the UISprite ends up with a dimension of 89x103. There is no center-based anchoring going on. However, due to the logic in the above mentioned lines, this Sprite's dimensions are being snapped to 90x104 - which is 1% too large and causing it to become non-pixel-perfect when previously it actually was pixel perfect (at 89x103).

I do understand the intent for the original ++x ++y logic, but I think there is some further logic that is missing to prevent it from taking effect in cases like mine and actually breaking the pixel perfect state. When I comment the two lines out, everything looks great again since the dimensions remain unchanged.

In the screenshot you will see that with the bug taking effect, the sprite has an extra column of sprites on the right side which doesn't look too noticeable. However, sometimes it manifests as a "crack" right in the middle of the sprite and can look pretty bad with mixed with sprites that have clean line art.
Title: Re: Possible bug with UISprite.MakePixelPerfect() + 0.5 Pixel Size
Post by: Wisteso on April 30, 2014, 06:05:47 PM
I just found this post which describes *almost* the same problem:

http://www.tasharen.com/forum/index.php?topic=6898.0 (http://www.tasharen.com/forum/index.php?topic=6898.0)

Though it appears that this is intended behavior, and done to prevent issues when centering the sprite. In my case, there's no centering going on and the behavior is not needed at all, so this would be another good use case for having an option to disable the ++x/++y padding behavior. Perhaps in the short term I can do this myself - if I do and it works I'll post my solution here.

Alternatively, I think I could just add 2 pixels of padding to every sprite, but with dozens of sprites like this, it would be wasteful with the texture memory. Perhaps I can ask our artists to resample the sprite to be 2 pixels smaller though.
Title: Re: Possible bug with UISprite.MakePixelPerfect() + 0.5 Pixel Size
Post by: ArenMook on April 30, 2014, 06:32:48 PM
NGUI actually trims the alpha anyway, so those extra pixels wouldn't matter.

In your case, select the sprite and edit it. Give it some padding to increase its dimensions to where they become dividable by 4, and you should effectively fix this. It's something you would generally do anyway, since it's the lowest-res images that should be used as the "base", not the higher ones. Multiply the lower res dimensions to get the higher res image values. This way you will never run into the problem of having odd numbers for dimensions.