Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.82k stars 821 forks source link

Texture: Setting vertex UV coordinates doesn't take into account the potential frame of SubTextures #1089

Open Adolio opened 2 years ago

Adolio commented 2 years ago

Hey Daniel,

I recently discovered that the UV mapping of textures part of an atlas (with frame mode active) doesn't fully natively work. I noticed that while trying to implement a missing feature in DragonBonesAS: https://github.com/DragonBones/DragonBonesAS/issues/94 - this might have been the issue the team faced when trying to implement it back then.

Please tell me if you think it's worth integrating in vanilla Starling - I can try to do a pull request then.

Workaround

Here is an utility method that supports SubTexture with the framing capability:

/**
 * Sets the texture coordinates of the vertex at the specified index to the given values.
 *
 * <p>Note: This method also supports `SubTexture` with framing.</p>
 */
public static function setTexCoords(image:Image, vertexID:int, u:Number, v:Number):void
{
    // adjust uv when the texture is a `SubTexture` and contains a frame
    if (image.texture is SubTexture)
    {
        var subTex:SubTexture = image.texture as SubTexture;
        if (subTex.frame != null)
        {
            u = (u * subTex.frame.width + subTex.frame.x) / subTex.region.width;
            v = (v * subTex.frame.height + subTex.frame.y) / subTex.region.height;
        }
    }

    // update vertex uv
    image.setTexCoords(vertexID, u, v);
}

Note: the problem with this method is the fact that if you get the coordinates afterwards you won't get the value you have set at the first place. This might be confusing... Another option would be to fix this even deeper in the rendering logic or implement the reversed logic in the getter.

PrimaryFeather commented 2 years ago

Hey Aurélien!

You're right, this would be ideally fixed right at the core of Starling. I wasn't aware of that limitation — but, on the other hand, that frame rectangle has caused me countless headaches over the years, so it doesn't surprise me that I overlooked that here. 😉

Could you do me a favor and also post a small pseudo-unit test? I.e. some code that produces a specific output, and the values it ought to print out instead? So that I'm sure I'm looking at the right thing.

Thanks in advance!

Adolio commented 2 years ago

Sorry for the late response Daniel.. 🙄

You're right, this would be ideally fixed right at the core of Starling. I wasn't aware of that limitation — but, on the other hand, that frame rectangle has caused me countless headaches over the years, so it doesn't surprise me that I overlooked that here. 😉

Not a big deal, you already did a massive work to hide so much complexity for us - which is really appreciated!

Could you do me a favor and also post a small pseudo-unit test? I.e. some code that produces a specific output, and the values it ought to print out instead? So that I'm sure I'm looking at the right thing.

Sure! Will do it as soon as I find the time. I also wanted to try implementing this right away in Starling but I don't have the right to access the archive you are mentioning here: https://github.com/Gamua/Starling-Framework/blob/master/tests/README.md

Can you give me access to it?

PrimaryFeather commented 2 years ago

Thanks for the update, Aurélien! No worries, look into it only when you've got the time.

It's strange that you can't access that link! Google claims it should be all public. But since there's an abundance of sharing services nowadays, anyway, I simply uploaded it here: https://web.tresorit.com/l/YCpuP#RahnBNC63NOzyYpNtIMGwg

That should work, hopefully! 😄

MNiceback commented 2 years ago

Remove from mailing please!


Van: Daniel Sperl @.> Verzonden: Wednesday, October 27, 2021 3:04:29 PM Aan: Gamua/Starling-Framework @.> CC: Subscribed @.***> Onderwerp: Re: [Gamua/Starling-Framework] Texture: Setting vertex UV coordinates doesn't take into account the potential frame of SubTextures (#1089)

Thanks for the update, Aurélien! No worries, look into it only when you've got the time.

It's strange that you can't access that link! Google claims it should be all public. But since there's an abundance of sharing services nowadays, I simply uploaded it here: https://web.tresorit.com/l/YCpuP#RahnBNC63NOzyYpNtIMGwg

That should work, hopefully! 😄

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/Gamua/Starling-Framework/issues/1089#issuecomment-952906384, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAVBLWCBKRQ3EF44BNKPC6DUJABG3ANCNFSM5F64ATGQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

PrimaryFeather commented 2 years ago

@Maxime, you can click on the "unsubscribe" button at the bottom of every email from GitHub! I don't think it's possible for me to unsubscribe you from this list.