atteneder / KtxUnity

Load KTX and Basis Universal textures at runtime
Apache License 2.0
221 stars 42 forks source link

Sprite Flipping & Sizing Issue when Preserve Aspect Ratio is enabled #28

Closed DerrickBarra closed 3 years ago

DerrickBarra commented 3 years ago

Quick question @atteneder , can you test this .basis file loaded as a sprite with your sprite flipping logic and let me know what you see? I'm expecting it to match this png version, but the results I'm seeing are a bit strange (flipped horizontally, larger scale).

If I force the IsXFlipped attribute to true, the basis sprite is the same size as the .png loaded version on the right, but is still not flipped on the horizontal (not facing the right).

atteneder commented 3 years ago

Hi @DerrickBarra

I tried loading with KtxUnity 0.9.0 and this script.

I looked identical to the PNG put on an Image component (mario facing right). Maybe look at my script and compare to yours.

hth

DerrickBarra commented 3 years ago

@atteneder Sorry for the weekend delay. So my original code was a modified version of your Image Sprite loading logic, but just in case I copied the logic over to what is basically a duplicate of the same class you linked to (just with some KTX define symbol checks), and the sprite is still loading on my Windows PC flipped and larger than the .png version.

Are you testing on a Mac? I'm going to make an Android or WebGL build next to see if the flipping works differently on those exports. Don't know why it would, but it can't hurt to elimiate those possibilities.

KtxFlip

EDIT: So I have a better idea of what's going wrong. The WebGL export was the same as my editor, and I found out that when turning off [preserveAspect] for the Image component the basis texture is correctly flipped, but then the aspect ratio is incorrect. I'm going to see if I can find a solution, but let me know if you have any ideas.

KtxPreserveAspectOff

EDIT: This also happens with the Trout test image [ .basis, .png ]

TroutFlip

TroutPreserveAspectOff

Although in another test, we can see that Trout_YFlip works as expected with preserve aspect ratio. (.basis or .ktx, makes no difference)

Trout_YFlip_Success

DerrickBarra commented 3 years ago

@atteneder So I ran another test with new .basis files just to confirm the bug can be recreated.

Mario NoFlip [default basisU configuration, no flip]

mario_noflip

Mario NoFlip - Preserve Aspect Ratio Disabled On Image Component

mario_noflip_preserveAspectOff

Mario yflip [-yflip command added to basisU]

mario_yflip

Mario YFlip - Preserve Aspect Ratio Disabled On Image Component

mario_yflip_preserveAspectOff

DerrickBarra commented 3 years ago

@atteneder Hey bud, anything you want me to try on this bug?

atteneder commented 3 years ago

@DerrickBarra The script counter-acts the flipping by setting negative size values on the Sprite. Those negative values confuse the Image component (or break its Sprite placement calculations) and I couldn't find any other ordinary way of teaching either Sprite or Image to simply flip it.

You have to bite the bullet and do the RectTransform position calculations yourself if you need preserved aspect ratio. Here's a simple example (to be added at the end of ApplyTexture);

float scale = 0.5f; // Set this to whatever size you need it - best make it a serialized class field
var rt = GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(texture.width*scale, texture.height*scale);

hth

DerrickBarra commented 3 years ago

@atteneder I recommend the documentation make note of this incompatibility with the Unity Image preserveAspect attribute. You can point to the possible solutions (pre-flipping your textures, or as a backup modifying the rectTransform to deal with the scaling.)

For our team's use, it'll be best to just pre-flip the textures in basisu or toktx to ensure we don't need to deal with the changes to our UI prefab hierarchies. Thanks!