Skip to content

Instantly share code, notes, and snippets.

@doxas
Last active July 5, 2020 09:56
Show Gist options
  • Save doxas/32004dedf1d7a650a4588ba75af0ded5 to your computer and use it in GitHub Desktop.
Save doxas/32004dedf1d7a650a4588ba75af0ded5 to your computer and use it in GitHub Desktop.
/**
* @param {vec2} coord - base texture coord
* @param {vec2} view - resolution of viewport
* @param {vec2} resource - resolution of resource
* @return {vec2} like a `background-size: cover` on css
*/
vec2 cover(vec2 coord, vec2 view, vec2 resource){
vec2 ratio = vec2(
min((view.x / view.y) / (resource.x / resource.y), 1.0),
min((view.y / view.x) / (resource.y / resource.x), 1.0)
);
return vec2(
coord.x * ratio.x + (1.0 - ratio.x) * 0.5,
coord.y * ratio.y + (1.0 - ratio.y) * 0.5
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment