Last active
January 29, 2024 12:39
-
-
Save supahfunk/2bc0ae5f9bd9613ad619ba118d4f00e7 to your computer and use it in GitHub Desktop.
Cover UV / Contain UV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*------------------------------ | |
Background Cover UV | |
-------------------------------- | |
u = basic UV | |
s = screensize | |
i = image size | |
------------------------------*/ | |
vec2 CoverUV(vec2 u, vec2 s, vec2 i) { | |
float rs = s.x / s.y; // Aspect screen size | |
float ri = i.x / i.y; // Aspect image size | |
vec2 st = rs < ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x); // New st | |
vec2 o = (rs < ri ? vec2((st.x - s.x) / 2.0, 0.0) : vec2(0.0, (st.y - s.y) / 2.0)) / st; // Offset | |
return u * s / st + o; | |
} | |
/*------------------------------ | |
Background Contain UV | |
-------------------------------- | |
u = basic UV | |
s = screensize | |
i = image size | |
------------------------------*/ | |
vec2 ContainUV (vec2 u, vec2 s, vec2 i) { | |
float rs = s.x / s.y; // Aspect screen size | |
float ri = i.x / i.y; // Aspect image size | |
vec2 st = rs > ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x); // New st | |
vec2 o = (rs > ri ? vec2((st.x - s.x) / 2.0, 0.0) : vec2(0.0, (st.y - s.y) / 2.0)) / st; // Offset | |
return u * s / st + o; | |
} |
Author
supahfunk
commented
Aug 10, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment