Skip to content

Instantly share code, notes, and snippets.

@behreajj
Created July 28, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behreajj/18900b43c48a8a055a09417c66df7247 to your computer and use it in GitHub Desktop.
Save behreajj/18900b43c48a8a055a09417c66df7247 to your computer and use it in GitHub Desktop.
Contain UVs
void setup() {
/* ... */
containUvs(txtr, width, height);
}
void containUvs(PImage txtr, float w, float h) {
float wc = txtr.width;
float hc = txtr.height;
float sketchAspect = w / h;
float invSketchAspect = 1.0 / sketchAspect;
float txtrAspect = wc / hc;
float invTxtrAspect = hc / wc;
float wf = sketchAspect * invTxtrAspect;
float hf = invSketchAspect * txtrAspect;
boolean txtrWide = wc > hc;
boolean sketchWide = w > h;
u = txtrWide ? sketchWide ?
wf : 1.0 : sketchWide ? wf : 1.0;
v = txtrWide ? sketchWide ?
1.0 : hf : sketchWide ? 1.0 : hf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment